#!/usr/bin/python
# coding=utf-8

import time
from common.info_dialog import InfoDialog
from uiWindow.winLogin import WinLoginDlg
from PyQt4 import QtGui
from PyQt4.QtCore import QTextCodec, QTimer, QObject, SIGNAL
from common import init_run
from common import screen_r
from settingModel.multimonitor import MultiMonitor
from logger.logrecord import LogRecord


# 　设置环境变量
def setEnviron():

    from os import environ, getenv                  # 环境变量

    if getenv('WIN_UI_IMAGES_PATH') == None:
        environ["WIN_UI_IMAGES_PATH"] = "../images" # 图片路径

    if getenv('MCOS_APP_ICON_PATH') == None:
        environ["MCOS_APP_ICON_PATH"] = "../images/appIcon"  # 应用图片路径
    # environ["LD_LIBRARY_RDP_PATH"]   = "../" #

    if getenv('LD_LIBRARY_RDP_PATH') == None:  # RDP 库
        environ["LD_LIBRARY_RDP_PATH"] = ""  #

    # if getenv('LOG_STDOUT') == None:
    #     environ["LOG_STDOUT"] = "stdout"  # 允许　日志输出到 输出到终端

    if getenv('MCOS_CONFIG_FILE') == None:
        environ["MCOS_CONFIG_FILE"] = "/mcos/dvclient/cfg/centerset.conf"  #

    environ["PATH"] = "/mcos/dvclient/sbin/usbip/:" + getenv("PATH")  # 添加usbip 环境变量


if __name__ == "__main__":
    import sys,os
    reload(sys)
    sys.setdefaultencoding('utf-8')

    QTextCodec.setCodecForCStrings(QTextCodec.codecForLocale())  # 支持中文
    screen_r.set_screen_r(screen_r.get_ini_value("screen"))

    app = QtGui.QApplication([])
    init_run.init_screen_with()  #  add by pbc on 2016.05.17 for init_screen_with
    init_run.init_xserver_set()  #  add by pbc on 2016.7.12 for init xserver

    # try:
    #     from common.start_network_config import StartNetworkConfig
    #     _StartNetworkConfig = StartNetworkConfig()
    #     _StartNetworkConfig.start_client_config_network()
    # except Exception as e:
    #     LogRecord.instance().logger.info(str(e.message))

    # except:
    #     pass

    # 设置 环境 变量
    setEnviron()

    os.system("sudo ufw disable") # 关闭防火墙
    MultiMonitor.setSingelMonitors()  # 设置单屏显示

    LogRecord.instance().logger.info("\n--------------启动---------------\n")
    logindlg = None

    autologinAllow = None
    if len(sys.argv) > 1 :
        print sys.argv[1]
        autologinAllow = "Disable"  # 禁止 自动登陆
    else:
        pass

    logindlg = WinLoginDlg(autologinAllow)
    logindlg.show_full_screen()
    logindlg.checkAutoLogin(autologinAllow)

    #add by pbc for ts memory up -top
    try:
        from threading import Thread
        import objgraph as objgraph
        import commands
        def objgraph_growth_fun():
            while(1):
                print "-------- this is objgraph -top -------------"
                print objgraph.show_growth()
                print "-------- this is objgraph -end -------------"
                if not os.path.exists("tmp.log"):
                    os.system("touch /mcos/tmp.log")
                fp = open("/mcos/tmp.log",'w')
                value = commands.getstatusoutput("top -b -n 1")
                print value
                if value[0] == 0:
                    value = value[1]
                fp.write(value)
                time.sleep(5)
        sub_thread = Thread(target=objgraph_growth_fun)
        sub_thread.setDaemon(True)
        sub_thread.start()
        import gc
        gc.collect()#memory clean ....
    except:
        pass
    #add by pbc for ts memory up -end

    app.exec_()

