# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/Version.py
# VERSION = g_db_helper.get_information(key='version',default="Dev")
# 
# Path: umit/icm/agent/rpc/MessageFactory.py
# class MessageFactory(object):
#     """"""
#     _reader = BinaryReader()
#     _writer = BinaryWriter()
# 
#     #----------------------------------------------------------------------
#     #def __init__(self):
#         #"""Constructor"""
# 
#     @classmethod
#     def create(cls, msg_type):
#         return message_creator[msg_type]()
# 
#     @classmethod
#     def encode(cls, message):
#         cls._writer.reset()
#         msg_type = message.DESCRIPTOR.name
#         msg_type_id = message_type_to_id[msg_type]
#         msg_str = message.SerializeToString()
#         cls._writer.writeInt32(msg_type_id)
#         cls._writer.writeFixedLengthString(msg_str, len(msg_str))
#         return cls._writer.getString()
# 
#     @classmethod
#     def decode(cls, str_):
#         cls._reader.setString(str_)
#         msg_type_id = cls._reader.readInt32()
#         msg_type = message_id_to_type[msg_type_id]
#         message = cls.create(msg_type)
#         msg_str = cls._reader.readFixedLengthString(len(str_)-4)
#         message.ParseFromString(msg_str)
#         return message
# 
#     @classmethod
#     def encrypt(cls, str_):
#         pass
# 
#     @classmethod
#     def decrypt(cls, str_):
#         pass
# 
# Path: umit/icm/agent/I18N.py
#     LC_ALL = locale.setlocale(locale.LC_ALL,'')
#     LC_ALL = locale.setlocale(locale.LC_ALL, None)        
# LANG = None
# ENC = None
#     LANG = "en_US"
#     LANG = "zh_CN"
#     LANG = "pt"
#     LANG = "ja"
#     LANG, ENC = locale.getdefaultlocale()
#     ENC = "utf8"
#     LANG = "en_US"   


import os
import signal
import sys
import time
import socket
import imp
            import gtk
from twisted.internet import reactor
from twisted.internet import task
from umit.icm.agent.logger import g_logger
from umit.icm.agent.BasePaths import *
from umit.icm.agent.Global import *
from umit.icm.agent.Version import VERSION
from umit.icm.agent.rpc.message import *
from umit.icm.agent.rpc.MessageFactory import MessageFactory
from umit.icm.agent.I18N import _
        from umit.icm.agent.core.PeerInfo import PeerInfo
        from umit.icm.agent.core.PeerManager import PeerManager
        from umit.icm.agent.core.EventManager import EventManager
        from umit.icm.agent.core.TaskManager import TaskManager
        from umit.icm.agent.core.ReportManager import ReportManager
        from umit.icm.agent.core.ReportUploader import ReportUploader
        from umit.icm.agent.core.TaskScheduler import TaskScheduler
        from umit.icm.agent.core.TaskAssignFetch import TaskAssignFetch
        from umit.icm.agent.core.TestSetsFetcher import TestSetsFetcher
        from umit.icm.agent.secure.KeyManager import KeyManager
        from umit.icm.agent.core.Statistics import Statistics
        from umit.icm.agent.rpc.aggregator import AggregatorAPI
        from umit.icm.agent.super.SuperBehaviourByManual import SuperBehaviourByManual
                from umit.icm.agent.rpc.AgentService import AgentFactory
        from umit.icm.agent.rpc.mobile import MobileAgentService
            from umit.icm.agent.gui.GtkMain import GtkMain
        from umit.icm.agent.core.Updater import auto_check_update
        from higwidgets.higwindows import HIGAlertDialog


        self.quitting = False
        self.is_auto_login = False        
        self.is_successful_login = False #fix the login failure, save DB problem
                       
    def _load_from_db(self):
        """
        """
        self.peer_manager.load_from_db()
        # restore unsent reports
        self.report_manager.load_unsent_reports()
        # desktop agent stats saving
        self.statistics.load_from_db()

    def init_after_running(self, port=None, username=None, password=None,
                           server_enabled=True, skip_server_check=False):
        """
        """
        #####################################################
        # Create agent service(need to add the port confilct)
        if server_enabled:
            self.listen_port = port if port is not None else g_config.getint('network', 'listen_port')
            try:
                self.factory = AgentFactory()
                g_logger.info("Listening on port %d.", self.listen_port)
                reactor.listenTCP(self.listen_port, self.factory)
            except Exception,info:
                #There can add more information

                self.quit_window_in_wrong(primary_text = _("The Listen Port has been used by other applications"), \

# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/Version.py
# VERSION = g_db_helper.get_information(key='version',default="Dev")
# 
# Path: umit/icm/agent/rpc/MessageFactory.py
# class MessageFactory(object):
#     """"""
#     _reader = BinaryReader()
#     _writer = BinaryWriter()
# 
#     #----------------------------------------------------------------------
#     #def __init__(self):
#         #"""Constructor"""
# 
#     @classmethod
#     def create(cls, msg_type):
#         return message_creator[msg_type]()
# 
#     @classmethod
#     def encode(cls, message):
#         cls._writer.reset()
#         msg_type = message.DESCRIPTOR.name
#         msg_type_id = message_type_to_id[msg_type]
#         msg_str = message.SerializeToString()
#         cls._writer.writeInt32(msg_type_id)
#         cls._writer.writeFixedLengthString(msg_str, len(msg_str))
#         return cls._writer.getString()
# 
#     @classmethod
#     def decode(cls, str_):
#         cls._reader.setString(str_)
#         msg_type_id = cls._reader.readInt32()
#         msg_type = message_id_to_type[msg_type_id]
#         message = cls.create(msg_type)
#         msg_str = cls._reader.readFixedLengthString(len(str_)-4)
#         message.ParseFromString(msg_str)
#         return message
# 
#     @classmethod
#     def encrypt(cls, str_):
#         pass
# 
#     @classmethod
#     def decrypt(cls, str_):
#         pass
# 
# Path: umit/icm/agent/I18N.py
#     LC_ALL = locale.setlocale(locale.LC_ALL,'')
#     LC_ALL = locale.setlocale(locale.LC_ALL, None)        
# LANG = None
# ENC = None
#     LANG = "en_US"
#     LANG = "zh_CN"
#     LANG = "pt"
#     LANG = "ja"
#     LANG, ENC = locale.getdefaultlocale()
#     ENC = "utf8"
#     LANG = "en_US"   


# Path: umit/icm/agent/Application.py
import os
import signal
import sys
import time
import socket
import imp
            import gtk
from twisted.internet import reactor
from twisted.internet import task
from umit.icm.agent.logger import g_logger
from umit.icm.agent.BasePaths import *
from umit.icm.agent.Global import *
from umit.icm.agent.Version import VERSION
from umit.icm.agent.rpc.message import *
from umit.icm.agent.rpc.MessageFactory import MessageFactory
from umit.icm.agent.I18N import _
        from umit.icm.agent.core.PeerInfo import PeerInfo
        from umit.icm.agent.core.PeerManager import PeerManager
        from umit.icm.agent.core.EventManager import EventManager
        from umit.icm.agent.core.TaskManager import TaskManager
        from umit.icm.agent.core.ReportManager import ReportManager
        from umit.icm.agent.core.ReportUploader import ReportUploader
        from umit.icm.agent.core.TaskScheduler import TaskScheduler
        from umit.icm.agent.core.TaskAssignFetch import TaskAssignFetch
        from umit.icm.agent.core.TestSetsFetcher import TestSetsFetcher
        from umit.icm.agent.secure.KeyManager import KeyManager
        from umit.icm.agent.core.Statistics import Statistics
        from umit.icm.agent.rpc.aggregator import AggregatorAPI
        from umit.icm.agent.super.SuperBehaviourByManual import SuperBehaviourByManual
                from umit.icm.agent.rpc.AgentService import AgentFactory
        from umit.icm.agent.rpc.mobile import MobileAgentService
            from umit.icm.agent.gui.GtkMain import GtkMain
        from umit.icm.agent.core.Updater import auto_check_update
        from higwidgets.higwindows import HIGAlertDialog


        self.quitting = False
        self.is_auto_login = False        
        self.is_successful_login = False #fix the login failure, save DB problem
                       
    def _load_from_db(self):
        """
        """
        self.peer_manager.load_from_db()
        # restore unsent reports
        self.report_manager.load_unsent_reports()
        # desktop agent stats saving
        self.statistics.load_from_db()

    def init_after_running(self, port=None, username=None, password=None,
                           server_enabled=True, skip_server_check=False):
        """
        """
        #####################################################
        # Create agent service(need to add the port confilct)
        if server_enabled:
            self.listen_port = port if port is not None else g_config.getint('network', 'listen_port')
            try:
                self.factory = AgentFactory()
                g_logger.info("Listening on port %d.", self.listen_port)
                reactor.listenTCP(self.listen_port, self.factory)
            except Exception,info:
                #There can add more information

                self.quit_window_in_wrong(primary_text = _("The Listen Port has been used by other applications"), \

                                          secondary_text = _("Please check the Port") )
