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

@author: root
'''
import os

from PyQt4.QtCore import QThread, SIGNAL

from base.storeinfoparser import StoreInfoParser
from setting.getnetinfo import NetCtl


class WireCtl(QThread) :
    def __init__(self,parent=None):
        super(WireCtl,self).__init__(parent)
        wire_open = u"开 启 无 线 网 卡"
        wire_close = u"禁 用 无 线 网 卡"
        self.netCtl = NetCtl()
        
    def run(self):
        wireOpen = u"open"
        wireClose = u"close"
        
        wireNetStatus = StoreInfoParser.instance().getWirelessNetwork()
    
        if((""==wireNetStatus) or (wireClose == wireNetStatus) ):#启用无线网
            #if(NetUsb::instance()->isNetUsb())//检测是否有3G网卡
            wlanName = self.netCtl.isNetUsb()
        
            if(wlanName):#检测是有3G网卡
              
                self.write_net("open")
               
                os.system("modprobe rndis_host")#安装usb网络
                os.system("ifconfig eth0 down")
                
                cmd = "ifconfig wlan0 up"
                
                ok = os.system(str(cmd))
                
        
                if(ok == 0):
                    #emit wireSuccess()
                    self.emit(SIGNAL("wireSuccess"))
                    return
                else:
                    #emit wireFaild()
                    self.emit(SIGNAL("wireFaild"))
                    return
            else:#没有无线网卡
                #emit noWireNet();
                self.emit(SIGNAL("noWireNet"))
                return
    
        else:#关闭用无线网
            #pass
            
            self.write_net("close")
            os.system("modprobe -r rndis_host")
            os.system("ifconfig wlan0 down")
            ok = os.system("ifconfig eth0 up")
  
            if(ok == 0):
                #emit closeWireSuccess()
                self.emit(SIGNAL("closeWireSuccess"))
                return
            else:
                #emit closeWireFiald()
                self.emit(SIGNAL("closeWireFiald"))
                return

    def write_net(self,status):

        s = status
        StoreInfoParser.instance().setWirelessNetwork(s)
        return
