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

from PyQt4.QtCore import QObject, QString, QTimer
import commands

class WindowMonitor(QTimer):
    _instance = 0 
    def __init__(self):
        super(WindowMonitor,self).__init__()
        self.windowProcessMap = {}
        
    @classmethod 
    def instance(cls):
        if (cls._instance == 0):
            cls._instance = WindowMonitor()
            
        return cls._instance
        

    def insertVmId(self, vmId):
        newPsId = self.getNewProcessId()
        if self.windowProcessMap.has_key(newPsId) or newPsId == "":
            return

        self.windowProcessMap[newPsId] = vmId


    def getVmIdList(self):
        vmIdList = []
        for psId in self.windowProcessMap:
            vmIdList.append(self.windowProcessMap[psId])
        
        return vmIdList

    def getProcessIDList(self):
        return self.windowProcessMap.keys()

    def getMapVmID(self, psId):
        if self.windowProcessMap.has_key(psId):
            return self.windowProcessMap[psId]
        
        return None
    
    def deleteProcessId(self, psId):
        if self.windowProcessMap.has_key(psId):
            del self.windowProcessMap[psId]
            
        return

    def clearProcessMap(self):
        self.windowProcessMap.clear()

    def getCurrentProcessId(self):
        statusOutput = commands.getstatusoutput("ps aux | grep /usr/massclouds/vdi/client/window/window")
        output = None
        idList = []
        if statusOutput[0] == 0:
            output = statusOutput[1].split("\n")
            for content in output:
                if "/usr/massclouds/vdi/client/window/window smep" in content:
                    idList.append(str(QString(content).simplified().split(" ")[1]))

        return idList
    
    def getCurrentWindowsProcess(self,vmname):
        if vmname == None or not vmname:
            vmname = ""
        vmflag = "vmname=" + vmname
        statusOutput = commands.getstatusoutput("ps aux | grep /usr/massclouds/vdi/client/window/window")
        output = None
        if statusOutput[0] == 0:
            output = statusOutput[1].split("\n")
            for content in output:
                if QString(content).contains(vmflag):
                    return True

        return False
        

    def getNewProcessId(self):        
        #获得当前window进程的id列表
        idList = self.getCurrentProcessId()
    
        if len(self.windowProcessMap) == 0 and len(idList) > 0:
            newId = idList[0]
            return newId
    
        #比较字典中存储的id和当前进程中的id列表
        curIdList = self.windowProcessMap.keys()
        for i in range(len(idList)):
            if idList[i] not in curIdList:
                newId = idList[i]
                return newId
    
        return None
        
    
    def checkDisapearWindowId(self):
        #获得当前window进程的id列表
        idList = self.getCurrentProcessId()
        
        #查找关闭的window的id
        curIdList = self.windowProcessMap.keys()
        for i in range(len(curIdList)):
            if curIdList[i] not in idList:
                disappearId = curIdList[i]
                return disappearId
            
        return None
        




