#coding =utf-8
import os
from threading import Thread
from PyQt4.QtCore import QObject, SIGNAL, QEventLoop, QTimer
from common import  network_set_func

class StartNetworkConfig(QObject):
    def __init__(self, parent = None):
        super(StartNetworkConfig, self).__init__(parent)


    def start_client_config_network(self):
        if not os._exists("/mcos/interfaces"):
            os.system("touch /mcos/interfaces")

        f = open('/mcos/interfaces', 'r')
        value = f.read()
        f.close()

        f2 = open('/etc/network/interfaces', 'w')
        f2.write(value)
        f2.close()

        ret = network_set_func.get_dns_dhcp_or_static()
        if ret == "dhcp":
            net_card = network_set_func.get_currnet_netcard_name()
            net_card = str(net_card)
            if net_card != "":
                network_set_func.stop_all_netcard()
                network_set_func.start_netcard(net_card)
                def do_restart_netcard_func():
                    network_set_func.restart_network(net_card)
                    self.emit(SIGNAL("config_network_over"))
                sub_thread = Thread(target=do_restart_netcard_func)
                sub_thread.setDaemon(True)
                sub_thread.start()
                sub_thread.join(15.0)
                self.timer = QTimer()
                self.eventloop= QEventLoop()
                self.connect(self.timer,SIGNAL("timeout()"),self.eventloop.quit)
                self.connect(self,SIGNAL("config_network_over"),self.eventloop.quit)
                self.timer.start(15000)
                self.eventloop.exec_()

                f = open('/etc/network/interfaces', 'w')
                f.write("")
                f.close()

                os.system("route add -net 192.168.102.0/24 gw 192.168.8.70")
            else:
                net_card_list = network_set_func.get_net_card_list()
                if len(net_card_list) > 0:
                    net_card = net_card_list[0]
                    net_card = str(net_card)
                    network_set_func.stop_all_netcard()
                    network_set_func.start_netcard(net_card)
                    def do_restart_netcard_func():
                        network_set_func.restart_network(net_card)
                        self.emit(SIGNAL("config_network_over"))
                    sub_thread = Thread(target=do_restart_netcard_func)
                    sub_thread.setDaemon(True)
                    sub_thread.start()
                    sub_thread.join(15.0)
                    self.timer = QTimer()
                    self.eventloop= QEventLoop()
                    self.connect(self.timer,SIGNAL("timeout()"),self.eventloop.quit)
                    self.connect(self,SIGNAL("config_network_over"),self.eventloop.quit)
                    self.timer.start(15000)
                    self.eventloop.exec_()
                    os.system("route add -net 192.168.102.0/24 gw 192.168.8.70")

                    f = open('/etc/network/interfaces', 'w')
                    f.write("")
                    f.close()

        else:
            net_card = network_set_func.get_currnet_netcard_name()
            net_card = str(net_card)
            if net_card != "":
                network_set_func.stop_all_netcard()
                network_set_func.start_netcard(net_card)
                network_set_func.restart_network(net_card)
                os.system("route add -net 192.168.102.0/24 gw 192.168.8.70")

                f = open('/etc/network/interfaces', 'w')
                f.write("")
                f.close()

            else:
                net_card_list = network_set_func.get_net_card_list()
                if len(net_card_list) > 0:
                    net_card = net_card_list[0]
                    net_card = str(net_card)
                    network_set_func.stop_all_netcard()
                    network_set_func.start_netcard(net_card)
                    network_set_func.restart_network(net_card)
                    os.system("route add -net 192.168.102.0/24 gw 192.168.8.70")

                    f = open('/etc/network/interfaces', 'w')
                    f.write("")
                    f.close()
