# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Mobius Forensic Toolkit
# Copyright (C) 2008,2009,2010,2011 Eduardo Aguiar
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import mobius.extension
import mobius.extension.main
import mobius
import mobius.config
import glob
import os
import os.path

USERDIR = os.path.join (os.path.expanduser ("~"), '.mobiusft')

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# @brief Application class
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class Application (object):

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Return path to application file/directory
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def get_path (self, *args):
    path = USERDIR

    if args:
      path = os.path.join (path, *args)

    return path
      
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Return application directory/subdirectory
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def get_directory (self, dirname=None):
    dir = USERDIR

    if dirname:
      dir = os.path.join (dir, dirname)

    return dir

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Create application subdirectory
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def make_directory (self, dirname=None):
    dir = self.get_path (dirname)

    if not os.path.isdir (dir):
      os.makedirs (dir)

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Run application
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def run (self):

    # advertise services
    mobius.mediator.advertise ('app.get-user-path', self.svc_get_path)
    mobius.mediator.advertise ('app.get-path', self.svc_get_path)
    mobius.mediator.advertise ('app.get-data-path', self.svc_get_data_path)
    mobius.mediator.advertise ('app.get-name', self.svc_get_name)
    mobius.mediator.advertise ('app.get-version', self.svc_get_version)

    mobius.mediator.advertise ('extension.get-metadata', mobius.extension.main.svc_extension_get_metadata)
    mobius.mediator.advertise ('extension.get-metadata-from-file', mobius.extension.main.svc_extension_get_metadata_from_file)
    mobius.mediator.advertise ('extension.get-icon-data-from-file', mobius.extension.main.svc_extension_get_icon_data_from_file)
    mobius.mediator.advertise ('extension.get-icon-path', mobius.extension.main.svc_extension_get_icon_path)
    mobius.mediator.advertise ('extension.get-resource-path', mobius.extension.main.svc_extension_get_resource_path)
    mobius.mediator.advertise ('extension.is-running', mobius.extension.main.svc_extension_is_running)


    mobius.mediator.advertise ('extension.new', mobius.extension.main.svc_extension_new)
    mobius.mediator.advertise ('extension.open', mobius.extension.main.svc_extension_open)
    mobius.mediator.advertise ('extension.save', mobius.extension.main.svc_extension_save)
    mobius.mediator.advertise ('extension.start-api', mobius.extension.main.svc_extension_start_api)
    mobius.mediator.advertise ('extension.stop-api', mobius.extension.main.svc_extension_stop_api)
    mobius.mediator.advertise ('extension.start', mobius.extension.main.svc_extension_start)
    mobius.mediator.advertise ('extension.stop', mobius.extension.main.svc_extension_stop)
    mobius.mediator.advertise ('extension.new-service', mobius.extension.main.svc_extension_new_service)
    mobius.mediator.advertise ('extension.new-callback', mobius.extension.main.svc_extension_new_callback)
    mobius.mediator.advertise ('extension.new-arg', mobius.extension.main.svc_extension_new_arg)
    mobius.mediator.advertise ('extension.start-all', mobius.extension.main.svc_extension_start_all)
    mobius.mediator.advertise ('extension.stop-all', mobius.extension.main.svc_extension_stop_all)
    mobius.mediator.advertise ('extension.list', mobius.extension.main.svc_extension_list)
    mobius.mediator.advertise ('extension.install', mobius.extension.main.svc_extension_install)
    mobius.mediator.advertise ('extension.uninstall', mobius.extension.main.svc_extension_uninstall)

    # configure user account
    self.__configure_user ()

    # start extensions API
    mobius.mediator.call ('extension.start-all')

    # start application
    mobius.mediator.emit ('app.starting')
    mobius.mediator.call ('app.start')

    # stop extensions
    mobius.mediator.emit ('app.stopping')
    mobius.mediator.call ('extension.stop-all')

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Configure application for current user
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def __configure_user (self):

    # if there is no $HOME/.mobiusft/extensions dir, install available extensions
    extensions_dir = self.get_path ('extensions')

    if not os.path.exists (extensions_dir):
      self.make_directory ('extensions')
      dist_dir = mobius.config.get_data_path ('extensions')

      for path in sorted (glob.glob ('%s/*.mobius' % dist_dir)):
        print 'Installing extension %s' % os.path.basename (path)
        mobius.extension.install (path)

    # @begin-deprecated version=0.5.4 brief=force installation of extension-manager
    else:
      extension_manager_dir = self.get_path ('extensions', 'extension-manager')
      if not os.path.exists (extension_manager_dir):
        dist_dir = mobius.config.get_data_path ('extensions')

        for path in sorted (glob.glob ('%s/*.mobius' % dist_dir)):
          print 'Installing extension %s' % os.path.basename (path)
          mobius.extension.install (path)
    # @end-deprecated

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Return path to application file/directory
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def svc_get_path (self, *args):
    path = USERDIR

    if args:
      path = os.path.join (path, *args)

    return path

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Return path to data file/directory
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def svc_get_data_path (self, *args):
    path = mobius.config.get_data_path (*args)
    return path

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Return application name
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def svc_get_name (self):
    return mobius.config.APP_NAME

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief Return application version
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def svc_get_version (self):
    return mobius.config.APP_VERSION
