#coding=utf-8
from PyQt4.QtCore import  SIGNAL, QObject, QDir, QFile, QIODevice
from controller.desktop.login import LoginDlg
from controller.desktop.vmmainwindow import VMMainWindow
from common.logrecord import LogRecord
from common.commondata import CONFIG_PATH,CONFIG_DIR,LOG_DIR,LOG_PATH

class MainWindow(QObject):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)

        self.initSubWidget()
        self.bandSignalSlot()

    def initSubWidget(self):
        self.beginFileDir()
        self.loginDlg = LoginDlg()
        self.loginDlg.show()
        self.vmWidget = VMMainWindow()

    def beginFileDir(self):
        dir=QDir()
        if(not dir.exists(CONFIG_DIR)):
            dir.mkdir(CONFIG_DIR)

        if(not dir.exists(LOG_DIR)):
            dir.mkdir(LOG_DIR)


        checkMakfilePath=CONFIG_PATH
        file_switch1=QFile(checkMakfilePath)
        if(file_switch1.open(QIODevice.ReadOnly)==0):
            file_switch1.close()
            fp=open(checkMakfilePath,'w')
            fp.close()
        else:
            file_switch1.close()


        logPath=LOG_PATH
        file_switch1=QFile(logPath)
        if(file_switch1.open(QIODevice.ReadOnly)==0):
            file_switch1.close()
            fp=open(logPath,'w')
            fp.close()
        else:
            file_switch1.close()

    def hideLogWidget(self):
        self.logWidget.cancel()
        self.logWidget.close()


    def bandSignalSlot(self):
        self.connect(self.loginDlg, SIGNAL("authSuccess"),self.slotShowVMList)
        self.connect(self.vmWidget, SIGNAL("logout"),self.slotLogout)

    def checkAutoLogin(self):
        if self.loginDlg.checkHasAutoLoginUser():
            self.loginDlg.slotLogin()

    def slotShowVMList(self, vmBaseInfoList):
        LogRecord.instance().recordLog(u"进入 slotShowVMList 函数")
        self.vmWidget.setVisible(True)
        self.loginDlg.setVisible(False)
        self.vmWidget.updateVMList(vmBaseInfoList)

    def slotLogout(self):
        self.vmWidget.setVisible(False)
        self.loginDlg.initCenterList()
        self.loginDlg.setVisible(True)
        self.loginDlg.resetWidgetInfo()

"""
class MainWindow(QWidget):
    def __init__(self,parent=None):  
        super(MainWindow,self).__init__(parent)
        
        self.setWindowTitle(u'安全云桌面')
        self.setWindowFlags(Qt.CustomizeWindowHint | Qt.FramelessWindowHint)
        self.initSubWidget()
        self.bandSignalSlot()

    def initSubWidget(self):
            # 初始化子控件
        self.loginDlg = LoginDlg(self)
        self.loginDlg.installEventFilter(self)

        self.vmWidget = VMMainWindow(self)
        self.vmWidget.setVisible(False)
        self.vmWidget.installEventFilter(self)
        
        self.shutDownBtn = QPushButton(self)
        self.shutDownBtn.setObjectName("shutDownBtn")
        self.shutDownBtn.setFixedSize(60,70)
        self.shutDownBtn.setToolTip(u"关机")

        self.rebootBtn = QPushButton(self)
        self.rebootBtn.setObjectName("rebootBtn")
        self.rebootBtn.setFixedSize(60,70)
        self.rebootBtn.setToolTip(u"重新启动")
        
        self.logWidget = LogWidget()
        self.logWidget.hide()
        self.actionShowLogWidget = QAction(self)
        self.actionHideLogWidget = QAction(self)
        self.actionShowLogWidget.setShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_B))
        self.actionHideLogWidget.setShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_C))
        self.addAction(self.actionShowLogWidget)
        self.addAction(self.actionHideLogWidget)
        self.connect(self.actionShowLogWidget,SIGNAL("triggered()"),self.showLogWidget)
        self.connect(self.actionHideLogWidget,SIGNAL("triggered()"),self.hideLogWidget)
        
        
    def showLogWidget(self):
        self.logWidget.show()
        self.logWidget.startThread()
        self.logWidget.move((self.width()-self.logWidget.width())/2,(self.height()-self.logWidget.height())/2)
    
    
    def hideLogWidget(self):
        self.logWidget.cancel()
        self.logWidget.close()
        

    def bandSignalSlot(self):
        self.connect(self.loginDlg, SIGNAL("authSuccess"),self.slotShowVMList)
        self.connect(self.vmWidget, SIGNAL("logout"),self.slotLogout)
        self.connect(self.rebootBtn, SIGNAL("clicked()"),self.slotReboot)
        self.connect(self.shutDownBtn, SIGNAL("clicked()"),self.slogShutdown)
        self.connect(self.loginDlg, SIGNAL("moveWidgetToScreenCenter"),self.adjustPosition)
        self.connect(self.vmWidget, SIGNAL("moveWidgetToScreenCenter"),self.adjustPosition)
        
    def adjustPosition(self):
        moveWidgetToScreenCenter(self.loginDlg, checkMulMonitor=False)
        moveWidgetToScreenCenter(self.vmWidget, checkMulMonitor=False)
        
    def paintEvent(self,event):
        self.shutDownBtn.move(QPoint(30,self.height() - 100))
        self.rebootBtn.move(QPoint(100,self.height() - 100))
        
        painter = QPainter(self)
        painter.save()
        pix = QPixmap("../images/background.png")
        painter.drawPixmap(QRect(0,0,self.width(),self.height()),pix)
        painter.restore()
        
    def checkAutoLogin(self):
        if self.loginDlg.checkHasAutoLoginUser():
            self.loginDlg.slotLogin()
        
    def slotShowVMList(self, vmBaseInfoList):
        self.vmWidget.setVisible(True)
        self.loginDlg.setVisible(False)
        self.vmWidget.updateVMList(vmBaseInfoList)
        
    def slotLogout(self):
        self.vmWidget.setVisible(False)
        self.loginDlg.setVisible(True)
        self.loginDlg.resetWidgetInfo()
        
        
    def eventFilter(self, obj, event):
        if obj is self.vmWidget and event.type() == QEvent.HoverMove:
            self.vmWidget.showFlavorDetail(event.pos())
            
        return QWidget.eventFilter(self, obj, event);
  
        
    def slotReboot(self):
        if (MessageBox(u"确定要重新启动吗？").exec_() == QDialog.Accepted):
            if os.system("reboot") == 0:
                return
            else:
                InfoHintDialog(u"重启失败！").exec_()
        return
      
    def slogShutdown(self):
        if (MessageBox(u"确定要关机吗？").exec_() == QDialog.Accepted):
            if os.system('init 0') == 0:
                return
            else:
                InfoHintDialog(u"重启失败！").exec_()
                
        return

"""