#coding=utf-8

from view.setting.usblist import Ui_Form
from PyQt4.Qt import QWidget,QString,QTableWidgetItem
from PyQt4.QtCore import Qt,SIGNAL
import commands

class UsbList(QWidget,Ui_Form):
    def __init__(self):
        super(UsbList,self).__init__()
        self.setupUi(self)
        self.tableWidget.setColumnWidth(0,120)
        self.tableWidget.setColumnWidth(1,120)
        self.tableWidget.setColumnWidth(2,290)
        self.setUsbListData()
        self.tableWidget.horizontalHeader().setStretchLastSection(True)
        self.connect(self.pushButton, SIGNAL("clicked()"),self.updateUsbList)

        
    def updateUsbList(self):
        self.tableWidget.clear()
        self.tableWidget.setColumnWidth(0,120)
        self.tableWidget.setColumnWidth(1,120)
        self.tableWidget.setColumnWidth(2,290)
        strList = [u"总线ID" ,u"设备ID",u"设备描述"]
        self.tableWidget.setHorizontalHeaderLabels(strList)
        self.setUsbListData()
    
    def setUsbListData(self):
        usbList = self.getUsbDevice()
        usbNum = len(usbList)
        self.tableWidget.setRowCount(usbNum)
        #填充数据
        for i in range(usbNum):
            pTWItemBus = QTableWidgetItem(usbList[i]["bus"])
            pTWItemBus.setTextAlignment(Qt.AlignCenter)
            self.tableWidget.setItem(i, 0, pTWItemBus)

            pTWItemName = QTableWidgetItem(usbList[i]["deviceId"])
            pTWItemName.setTextAlignment(Qt.AlignCenter)
            self.tableWidget.setItem(i, 1, pTWItemName)
    
            pTWItemStatus = QTableWidgetItem(usbList[i]["describe"])
            pTWItemStatus.setTextAlignment(Qt.AlignCenter)
            self.tableWidget.setItem(i, 2, pTWItemStatus)
        self.tableWidget.hideColumn(0)
    
        
    def getUsbDevice(self):
        cmd = "lsusb"
        usblist = []
        statusOutput = commands.getstatusoutput(cmd)
        if statusOutput[0] == 0:
            output = statusOutput[1]#第二个元素就是命令行执行后的输出
        else:
            output = ""
        temp = output.split("\n")
        for i in range(len(temp)):
            if not QString(temp[i]).contains("except"):
                usblist.append(temp[i])
        usbcount=len(usblist)
        if(usbcount == 0):
            return []
        else:
            usbList = self.fillUsbList(usblist);
            return usbList
    
    def fillUsbList(self,usbList):
        usbListStr = []
        for i in range(len(usbList)):
            list={}
            usbInfo=usbList[i]
            insec=usbInfo.split("ID ")[1]
            bus=usbInfo.split("ID ")[0]
            if(insec != "" and bus != ""):
                content=insec.split(" ")
                buslist = bus.split(" ")
                deviceId = content[0]
                busId = buslist[1]
                _describe=""
                for j in range(len(content)):
                    if j==0:
                        continue
                    _describe = _describe+" "+content[j]
                list["deviceId"]=deviceId
                list["describe"]=_describe
                list["bus"] = busId
                usbListStr.append(list)
        return usbListStr