# -*- coding: utf-8 -*-

from qt import *
import os

from base.gui.ImprovedServerDialogDesign import ImprovedServerDialogDesign
from base.backend.ServerObject import ServerObject
from base.backend.ServerList import ServerList
import environment


class ImprovedServerDialog(ImprovedServerDialogDesign):

    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        ImprovedServerDialogDesign.__init__(self,parent,name,modal,fl)
        
        self.SAVED = False
        
        self.authentificationMethods = [u"Simple", u"SASL Plain", u"SASL CRAM-MD5", 
        u"SASL DIGEST-MD5", u"SASL Login", u"SASL GSSAPI", u"SASL EXTERNAL"]

        self.installationPrefix = environment.lumaInstallationPrefix
        self.iconPath = os.path.join(self.installationPrefix, "share", "luma", "icons")
        self.networkPixmap = QPixmap(os.path.join(self.iconPath, "network32.png"))
        
        # Save lineedit background color for later use
        #self.originalBackGroundColor = self.certFileEdit.paletteBackgroundColor()
        
        self.networkLabel.installEventFilter(self)
        self.credentialLabel.installEventFilter(self)
        self.encryptionLabel.installEventFilter(self)
        self.authLabel.installEventFilter(self)
        self.ldapOptLabel.installEventFilter(self)
        
        self.saveButton.setEnabled(0)
        self.configStack.raiseWidget(6)
        self.originalBackGroundColor = self.networkLabel.paletteBackgroundColor()
        
        self.serverListObject = ServerList()
        self.serverListObject.readServerList()
        
        # Server which is currently selected.
        self.currentServer = None
        
        self.oldServerItem = None
        
        self.displayServerList()
        
###############################################################################

    def displayServerList(self):
        self.serverListView.clear()
        
        if self.serverListObject.serverList == None:
            return
            
        for x in self.serverListObject.serverList:
            tmpItem = QListViewItem(self.serverListView, x.name)
            self.serverListView.insertItem(tmpItem)
            
            subItem = QListViewItem(tmpItem, "Network options")
            subItem.setPixmap(0, self.networkPixmap)
            
###############################################################################

    def serverSelected(self, serverItem):
        if serverItem == None:
            if not (self.oldServerItem == None):
                self.oldServerItem.setOpen(False)
            self.oldServerItem = None
            self.serverLabel.setText(self.trUtf8("<b>No server selected</b>"))
            self.configStack.raiseWidget(6)
            return
    
        if serverItem.parent() == None:
            if not (self.oldServerItem == None):
                self.oldServerItem.setOpen(False)
                
            serverItem.setOpen(True)
            self.oldServerItem = serverItem
            
            self.serverLabel.setText(QString("<b>%1</b>").arg(serverItem.text(0)))
            self.configStack.raiseWidget(0)

###############################################################################
        
    def eventFilter(self, object, event):
        if (event.type() == QEvent.Enter):
            if object == self.networkLabel:
                tmpColor = self.networkLabel.colorGroup().highlightedText()
                self.networkLabel.setPaletteBackgroundColor(tmpColor)
                
            if object == self.credentialLabel:
                tmpColor = self.credentialLabel.colorGroup().highlightedText()
                self.credentialLabel.setPaletteBackgroundColor(tmpColor)
                
            if object == self.encryptionLabel:
                tmpColor = self.encryptionLabel.colorGroup().highlightedText()
                self.encryptionLabel.setPaletteBackgroundColor(tmpColor)
                
            if object == self.authLabel:
                tmpColor = self.authLabel.colorGroup().highlightedText()
                self.authLabel.setPaletteBackgroundColor(tmpColor)
                
            if object == self.ldapOptLabel:
                tmpColor = self.ldapOptLabel.colorGroup().highlightedText()
                self.ldapOptLabel.setPaletteBackgroundColor(tmpColor)
                
                
        if (event.type() == QEvent.Leave):
            if object == self.networkLabel:
                self.networkLabel.setPaletteBackgroundColor(self.originalBackGroundColor)
                
            if object == self.credentialLabel:
                self.credentialLabel.setPaletteBackgroundColor(self.originalBackGroundColor)
                
            if object == self.encryptionLabel:
                self.encryptionLabel.setPaletteBackgroundColor(self.originalBackGroundColor)
                
            if object == self.authLabel:
                self.authLabel.setPaletteBackgroundColor(self.originalBackGroundColor)
                
            if object == self.ldapOptLabel:
                self.ldapOptLabel.setPaletteBackgroundColor(self.originalBackGroundColor)
            
                
        return 0

