#coding:utf-8
'''
Created on Dec 18, 2014

@author: root
'''
from PyQt4.QtCore import QThread,SIGNAL, QString
import commands
import os
import time
from offvm import pathfolder
#CMD="rsync -avzp root@192.168.5.133:/var/lib/libvirt/images/xp-0.img /var/lib/libvirt/images/xp-0.img"
#IMG_PATH = "/var/lib/libvirt/images/"
#XML_PATH = "/var/lib/libvirt/xml/"
IMG_PATH = pathfolder.IMG_PATH
XML_PATH = pathfolder.XML_PATH
IMGNAME = "-0.img"

class ProgressThread(QThread):
    def __init__(self,parent=None):
        super(ProgressThread,self).__init__(parent)
        self.auto = True
    def setNeccInfo(self):
        pass
    def setProcessId(self,vmid):
        self.vmid = vmid.split(":")[0] +":" + vmid.split(":")[1] + ":" + vmid.split(":")[2]
        self.filesize = vmid.split(":")[3]
        self.filename = vmid.split(":")[0]
    def setVmInfoList(self,vmInfoList):
        self.vmInfoList = vmInfoList
    def run(self):
        while self.auto:
            process,vmid = self.getDownloadingImg()
            if vmid == None:
                pass
            else:
                self.emit(SIGNAL("currentprogress"),process,vmid)
            time.sleep(2)
    def getDownloadingImg(self):
        currentSize, imgName = self.imgIsInList()
        if imgName == None:
            return 0,None
        else:
            for i in range(len(self.vmInfoList)):
                if QString(imgName).startsWith(self.vmInfoList[i]["name"]):
                    row = i/3
                    column = i%3
                    process = currentSize*100/self.vmInfoList[i]["img_file_size"]
                    vmid = self.vmInfoList[i]["name"] + ":" + QString.number(row) + ":" + QString.number(column)
                else:
                    process = 0
                    vmid = None
            return process,vmid
        
    def getImgTotalSize(self):
        return self.filesize
    
    def getProgress(self):
        totalSize = int(self.getImgTotalSize())
        #print totalSize
        filePath = IMG_PATH + self.filename + "-0.img"
        xfilePath = XML_PATH + self.filename + ".xml"
        if os.path.exists(filePath):
            if os.path.getsize(filePath) == totalSize :
                time.sleep(1)
                return 100
            else:
                return 99
        else:
            currentSize = self.imgIsInList()
            print 'currentSize',currentSize
            return currentSize*100/totalSize
    def getCurrentDirList(self):
        cmd = "ls -a " + IMG_PATH
        statusoutput = []
        statusoutput = commands.getstatusoutput(cmd)
        if statusoutput[0] == 0:
            output = statusoutput[1]
            return output.split("\n")
        else:
            #print "nonono"
            output = ""
            return []
    def imgIsInList(self):
        fileList = self.getCurrentDirList()
        print fileList
        for item in fileList:
            if QString(item).startsWith(".") and len(item.split("-0.img")) >=2 :
                fileDir = IMG_PATH + item
                #print  fileDir
                return os.path.getsize(fileDir),item
        return 0,None
    
    def stop(self):
        self.auto = False
        '''
Created on Jan 14, 2015

@author: root
'''
