#coding=utf-8

from view.setting.netservice import Ui_Form
from PyQt4.Qt import QWidget,SIGNAL,QProcess,QString,QThread,QTextCursor,QTimer,QKeySequence,QRegExp,QRegExpValidator,\
    QKeyEvent
import commands
from PyQt4.QtCore import Qt
# from model.setting.base.infohintdialog import InfoHintDialog
from common.infohintdialog import InfoHintDialog


class MyThreadTop(QThread):
    def __init__(self,parent = None):
        super(MyThreadTop,self).__init__(parent)
        self.output=""
        
    def run(self):
        statusOutput = commands.getstatusoutput("top -b -n 1")
        if statusOutput[0] == 0:
            self.output=statusOutput[1]
            self.emit(SIGNAL("getoutput"), self.output)


class NetService(QWidget,Ui_Form):
    def __init__(self):
        super(NetService,self).__init__()
        self.setupUi(self)
        self.comBox.insertItem(0, u"ping")
        self.comBox.insertItem(1, u"ifconfig")
        self.comBox.insertItem(2, u"xrandr")
        self.comBox.insertItem(4, u"top")
        self.label.setText(u"提示：请在文本框中输入要ping的目标地址，然后点击执行获取结果")
        
        self.threadTop=MyThreadTop()
        self.timer=QTimer()
        self.connect(self.comBox,SIGNAL('activated(QString)'),self.onActivated)
        self.connect(self.runButton,SIGNAL("clicked()"),self.runButtonClicked)
        self.connect(self.timer, SIGNAL("timeout()"),self.threadTop.start)
        self.runButton.setFocus()
        self.comBox.setStyleSheet(QString("border:1px groove rgb(170,214,252);height:25;"))
        self.lineEdit.setStyleSheet(QString("border:1px groove rgb(170,214,252);height:25;"))

    def keyPressEvent(self,event):
        if(event.key()==Qt.Key_Enter or event.key()==Qt.Key_Return):
            self.runButtonClicked()
        
        
    def runButtonClicked(self):
        self.tBrowser.clear()
        self.process=QProcess()
        self.connect(self.process, SIGNAL("readyReadStandardOutput()"),self.readfromProcess)
        self.connect(self.process, SIGNAL("readyReadStandardError()"),self.radfromProcessError)
        self.connect(self.threadTop,SIGNAL("getoutput"),self.appendTo)
        if(self.comBox.currentText()==QString("ping")):
            if(self.runButton.text()==QString.fromUtf8("执行")):
                if(self.lineEdit.text().trimmed()==""):
                    InfoHintDialog(u"请输入相应的IP地址 ！").exec_()
                    return
                self.runButton.setText(QString.fromUtf8("停止ping"))
                tempStr=QString("ping ")+self.lineEdit.text()+QString.fromUtf8("的结果：")
                self.tBrowser.append(tempStr)
                self.comBox.setEnabled(False)
                cmdstr = QString("ping ") +self.lineEdit.text()
                self.process.start(cmdstr)
            elif(self.runButton.text()==QString.fromUtf8("停止ping")):
                self.comBox.setEnabled(True)
                self.runButton.setText(QString.fromUtf8("执行"))
                self.process.close()
                self.tBrowser.setText("")
        elif(self.comBox.currentText()==QString("ifconfig")):
            self.lineEdit.clear()
            self.lineEdit.setEnabled(False)
            self.tBrowser.clear()
            cmdstr=QString("ifconfig")
            self.process.start(cmdstr)
        elif (self.comBox.currentText() == QString("xrandr")):
            self.lineEdit.clear()
            self.lineEdit.setEnabled(False)
            self.tBrowser.clear()
            cmdstr = QString("xrandr")
            self.process.start(cmdstr)
        elif (self.comBox.currentText() == QString("top")):
            if self.runButton.text() == QString.fromUtf8("执行"):
                self.threadTop.start()
                self.timer.start(2500)
                self.comBox.setDisabled(True)
                self.runButton.setText(QString.fromUtf8("停止"))
            elif self.runButton.text() == QString.fromUtf8("停止"):
                self.tBrowser.clear()
                self.timer.stop()
                self.comBox.setDisabled(False)
                self.runButton.setText(QString.fromUtf8("执行"))
        self.runButton.setShortcut(Qt.Key_Enter)

    def appendTo(self,actionType):
        self.tBrowser.clear()
        self.tBrowser.append(actionType)
        cursor = self.tBrowser.textCursor()
        cursor.movePosition(QTextCursor.Start)
        self.tBrowser.setTextCursor(cursor)        

                
    def radfromProcessError(self):
        res = QString.fromLatin1(self.process.readAllStandardOutput()).trimmed()
        self.tBrowser.append(res)
    
    def readfromProcess(self):
        res = QString.fromLatin1(self.process.readAllStandardOutput()).trimmed()
        self.tBrowser.append(res)
        
    def onActivated(self,str):
        if(self.comBox.currentText() == "ping"):
            self.label.setText(u"提示：请在文本框中输入要ping的目标地址，然后点击执行获取结果")
            self.label.adjustSize()
            self.lineEdit.clear()
            self.lineEdit.setDisabled(False)
            self.tBrowser.clear()
        elif(self.comBox.currentText() == "ifconfig"):
            self.label.setText(u"提示：请点击执行，获取终端的网卡和IP等信息")
            self.label.adjustSize()
            self.lineEdit.clear()
            self.lineEdit.setEnabled(False)
            self.tBrowser.clear()
        elif(self.comBox.currentText() == "xrandr"):
            self.label.setText(u"提示：执行xrandr命令")
            self.label.adjustSize()
            self.lineEdit.clear()
            self.lineEdit.setEnabled(False)
            self.tBrowser.clear()
        elif(self.comBox.currentText() == "traceroute"):
            self.label.setText(u"提示：请在文本框中输入要traceroute的目标地址，然后点击执行获取结果")
            self.label.adjustSize()
            self.lineEdit.setEnabled(True)
            self.lineEdit.clear()
            self.tBrowser.clear()
        elif(self.comBox.currentText() == "top"):
            self.label.setText(u"提示：执行top命令")
            self.label.adjustSize()
            self.lineEdit.setEnabled(False)
            self.lineEdit.clear()
            self.tBrowser.clear()
        self.runButton.setShortcut(Qt.Key_Enter)
            
            
    





