# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# 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):


from twisted.internet import reactor
from twisted.web.client import HTTPDownloader
from twisted.web import client
from umit.icm.agent.logger import g_logger
from umit.icm.agent.Global import *
from umit.icm.agent.Application import theApp
            from twisted.internet import ssl

#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
HTTPDownload with progress
"""



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)

# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# 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/utils/FileDownloader.py
from twisted.internet import reactor
from twisted.web.client import HTTPDownloader
from twisted.web import client
from umit.icm.agent.logger import g_logger
from umit.icm.agent.Global import *
from umit.icm.agent.Application import theApp
            from twisted.internet import ssl

#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
HTTPDownload with progress
"""



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)
