# Path: umit/icm/agent/utils/FileDownloader.py
# class FileDownloader(object):
#     """Download file from HTTP server"""
# 
#     #----------------------------------------------------------------------
#     def __init__(self, url, local_path):
#         """Constructor"""
#         #if https or ftp?
#         if not url.startswith("http://") or url.startswith("https://"):
#             raise Exception("URL must start with 'http://' or 'https://'")
#         self._url = str(url)
#         self._local_path = local_path
# 
#     #----------------------------------------------------------------------
#     def start(self):
#         g_logger.info("URL: %s" % self._url)
#         g_logger.info("Local Path: %s" % self._local_path)
#         g_logger.info("Download started.")
#         print "test"
#         
#         factory = HTTPDownloader(self._url, self._local_path)
#         factory.deferred.addErrback(g_logger.error)
#         factory.deferred.addCallback(self._downloadComplete)
#         if hasattr(self, '_callback'):
#             factory.deferred.addCallback(self._callback, *self._callback_args,
#                                          **self._callback_kw)
#         
#         if self._url.startswith("https://"):
#             from twisted.internet import ssl
#             content_factory = ssl.ClientContextFactory()
#             reactor.connectSSL(factory.host, factory.port, factory, content_factory, timeout, bindAddress)
#         else:
#             reactor.connectTCP(factory.host, factory.port, factory)
# 
#     #----------------------------------------------------------------------
#     def addCallback(self, callback, *args, **kw):
#         self._callback = callback
#         self._callback_args = args
#         self._callback_kw = kw
# 
#     #----------------------------------------------------------------------
#     def _downloadComplete(self, result):
#         g_logger.info("Download Completed.")
# 
# Path: umit/icm/agent/Application.py
# def main_is_frozen():
#     def __init__(self):
#     def _init_components(self, aggregator):
#     def _load_from_db(self):
#     def init_after_running(self, port=None, username=None, password=None,
#                            server_enabled=True, skip_server_check=False):
#     def check_aggregator_success(self,response):
#     def login_window_show(self):       
#     def check_aggregator_failed(self,message):
#     def login_without_gui(self):
#     def check_software_auto(self):
#     def register_agent(self, username, password):
#     def _handle_register(self, result):
#     def _handle_errback(self, failure):
#     def login(self, username, password, save_login=False, login_only=False):
#     def check_username(self,username="",password=""):
#     def _login_after_register_callback(self, message, username,
#                                        password, save_login, login_only):
#     def _handle_login_errback(self,failure):
#     def _handle_login(self, result, username, password, save_login,login_only=False):
#     def login_simulate(self):
#     def task_loop_manager(self):
#     def logout(self):
#     def _handle_logout(self, result):
#     def start(self, run_reactor=True, managed_mode=False, aggregator=None):
#     def quit_window_in_wrong(self,primary_text = "",secondary_text = ""):
#     def terminate(self):
#     def on_quit(self):
# class Application(object):
# 
# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'


from umit.icm.agent.utils.FileDownloader import FileDownloader
from umit.icm.agent.Application import theApp
from umit.icm.agent.logger import g_logger
            from umit.icm.agent.gui.Notifications import NotificationUpdate,new_release_mode,report_mode
            from umit.icm.agent.gui.Notifications import NotificationUpdate,auto_upgrade_mode,report_mode
    if compare_version(str(message.versionNo),VERSION) == higher_version:
        g_logger.info("New version arrive in Check" ) 
        if auto_upgrade == False: 
            t = NotificationUpdate(mode=new_release_mode,text="test",timeout=15000)
        else:
            t = NotificationUpdate(mode=auto_upgrade_mode,text="test",timeout=30000)
            #software update automatically
            download_update(url=message.downloadURL,version=message.versionNo,check_code=None)
    else:
        g_logger.info("Current version is the newest in Check" ) 
  
def handle_auto_errback(failure):
    """"""
    g_logger.error("auto check error:%s" % str(failure))   

def insert_update_item_in_db(record):
    """"""
    sql_commit = "insert into updates (version,news_date,software_name, "\
                 "description, download_url, is_update, check_code ) "\
                 "values ('%s', '%s' , '%s', '%s','%s', '%s', '%s') " % \
                 (record["version"],
                  record["news_date"],
                  record["software_name"],
                  record["description"],
                  record["download_url"],
                  record["is_update"],
                  record["check_code"]) 
    print  sql_commit             
    g_db_helper.execute(sql_commit) 
    g_db_helper.commit()        

# Path: umit/icm/agent/utils/FileDownloader.py
# class FileDownloader(object):
#     """Download file from HTTP server"""
# 
#     #----------------------------------------------------------------------
#     def __init__(self, url, local_path):
#         """Constructor"""
#         #if https or ftp?
#         if not url.startswith("http://") or url.startswith("https://"):
#             raise Exception("URL must start with 'http://' or 'https://'")
#         self._url = str(url)
#         self._local_path = local_path
# 
#     #----------------------------------------------------------------------
#     def start(self):
#         g_logger.info("URL: %s" % self._url)
#         g_logger.info("Local Path: %s" % self._local_path)
#         g_logger.info("Download started.")
#         print "test"
#         
#         factory = HTTPDownloader(self._url, self._local_path)
#         factory.deferred.addErrback(g_logger.error)
#         factory.deferred.addCallback(self._downloadComplete)
#         if hasattr(self, '_callback'):
#             factory.deferred.addCallback(self._callback, *self._callback_args,
#                                          **self._callback_kw)
#         
#         if self._url.startswith("https://"):
#             from twisted.internet import ssl
#             content_factory = ssl.ClientContextFactory()
#             reactor.connectSSL(factory.host, factory.port, factory, content_factory, timeout, bindAddress)
#         else:
#             reactor.connectTCP(factory.host, factory.port, factory)
# 
#     #----------------------------------------------------------------------
#     def addCallback(self, callback, *args, **kw):
#         self._callback = callback
#         self._callback_args = args
#         self._callback_kw = kw
# 
#     #----------------------------------------------------------------------
#     def _downloadComplete(self, result):
#         g_logger.info("Download Completed.")
# 
# Path: umit/icm/agent/Application.py
# def main_is_frozen():
#     def __init__(self):
#     def _init_components(self, aggregator):
#     def _load_from_db(self):
#     def init_after_running(self, port=None, username=None, password=None,
#                            server_enabled=True, skip_server_check=False):
#     def check_aggregator_success(self,response):
#     def login_window_show(self):       
#     def check_aggregator_failed(self,message):
#     def login_without_gui(self):
#     def check_software_auto(self):
#     def register_agent(self, username, password):
#     def _handle_register(self, result):
#     def _handle_errback(self, failure):
#     def login(self, username, password, save_login=False, login_only=False):
#     def check_username(self,username="",password=""):
#     def _login_after_register_callback(self, message, username,
#                                        password, save_login, login_only):
#     def _handle_login_errback(self,failure):
#     def _handle_login(self, result, username, password, save_login,login_only=False):
#     def login_simulate(self):
#     def task_loop_manager(self):
#     def logout(self):
#     def _handle_logout(self, result):
#     def start(self, run_reactor=True, managed_mode=False, aggregator=None):
#     def quit_window_in_wrong(self,primary_text = "",secondary_text = ""):
#     def terminate(self):
#     def on_quit(self):
# class Application(object):
# 
# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'


# Path: umit/icm/agent/core/Updater.py
from umit.icm.agent.utils.FileDownloader import FileDownloader
from umit.icm.agent.Application import theApp
from umit.icm.agent.logger import g_logger
            from umit.icm.agent.gui.Notifications import NotificationUpdate,new_release_mode,report_mode
            from umit.icm.agent.gui.Notifications import NotificationUpdate,auto_upgrade_mode,report_mode

    if compare_version(str(message.versionNo),VERSION) == higher_version:
        g_logger.info("New version arrive in Check" ) 
        if auto_upgrade == False: 
            t = NotificationUpdate(mode=new_release_mode,text="test",timeout=15000)
        else:
            t = NotificationUpdate(mode=auto_upgrade_mode,text="test",timeout=30000)
            #software update automatically
            download_update(url=message.downloadURL,version=message.versionNo,check_code=None)
    else:
        g_logger.info("Current version is the newest in Check" ) 
  
def handle_auto_errback(failure):
    """"""
    g_logger.error("auto check error:%s" % str(failure))   

def insert_update_item_in_db(record):
    """"""
    sql_commit = "insert into updates (version,news_date,software_name, "\
                 "description, download_url, is_update, check_code ) "\
                 "values ('%s', '%s' , '%s', '%s','%s', '%s', '%s') " % \
                 (record["version"],
                  record["news_date"],
                  record["software_name"],
                  record["description"],
                  record["download_url"],
                  record["is_update"],
                  record["check_code"]) 
    print  sql_commit             
    g_db_helper.execute(sql_commit) 
    g_db_helper.commit()        

    g_logger.debug("insert a new record(%s) into updates of DB." % sql_commit)