Ejemplo n.º 1
0
 def setup_globalkeybinding (self):
     """
     Create GlobalKeyBinding for the 'key_binding' setting
     """
     self.globalkeybinding = GlobalKeyBinding (self, "key_binding")
Ejemplo n.º 2
0
 def setup_globalkeybinding(self):
     """
     Create GlobalKeyBinding for the 'key_binding' setting
     """
     self.globalkeybinding = GlobalKeyBinding(self, "key_binding")
Ejemplo n.º 3
0
class Application (dbus.service.Object):
    """
    An object for managing a FreeSpeak application instance.
    WARNING: You should NOT create directly an application, please take a look at
    the get_instance() function to ensure the Application singleton.
    """
    TEXT_TRANSLATION = 0
    WEB_TRANSLATION = 1
    TRANSLATION_SUGGESTIONS = 2

    def __init__ (self, bus, path, name):
        dbus.service.Object.__init__ (self, bus, path, name)

        self.globalkeybinding = None
        self.translators_path = None
        self.icons_path = None
        self.icon_theme = None
        self.clipboard = None
        self.main_window = None
        self.config = None
        self.translators_manager = None

        self.setup_l10n ()
        self.setup_exception_dialog ()
        self.setup_config ()
        self.setup_paths ()
        self.setup_icons ()
        self.setup_translators_manager ()
        self.setup_clipboard ()
        self.setup_style ()
        self.setup_globalkeybinding ()

        self.running = False
        self.gtkmain = False

    def setup_exception_dialog (self):
        """
        Setup a Python-wide exception hook
        """
        # FIXME: must take care if another excepthook had been installed
        sys.excepthook = exception_dialog.exception_hook

    @staticmethod
    def setup_l10n ():
        """
        Install the _ gettext function
        """
        # FIXME: must take care if it has been already called then we're
        #       going to override the _
        gettext.install (defs.GETTEXT_PACKAGE, unicode=True)

    def setup_config (self):
        """
        Create a configuration object
        """
        self.config = Config ()

    def setup_paths (self):
        """
        Setup common paths used for finding FreeSpeak resources
        """
        self.icons_path = os.path.join (defs.DATA_DIR, defs.PACKAGE, 'art')
        self.translators_path = os.path.dirname (freespeak.translators.__file__)

    def setup_icons (self):
        """
        Setup the GTK+ icon theme, adding the path for FreeSpeak art.
        Set the default icon for all windows.
        """
        self.icon_theme = gtk.icon_theme_get_default ()
        self.icon_theme.append_search_path (self.icons_path)
        # FIXME: must take care if the application was created from another
        #       application.
        window_icon = self.icon_theme.load_icon (defs.PACKAGE, 64, 0)
        gtk.window_set_default_icon (window_icon)

    def setup_translators_manager (self):
        """
        Create a TranslatorsManager instance for managing translators/translating engines.
        """
        self.translators_manager = TranslatorsManager (self)

    def setup_clipboard (self):
        """
        Create a ClipboardController instance for managing the clipboard
        """
        self.clipboard = ClipboardController (self)

    def setup_style (self):
        """
        Tweak the GTK+ widgets style
        """
        style.setup_rc ()

    def setup_globalkeybinding (self):
        """
        Create GlobalKeyBinding for the 'key_binding' setting
        """
        self.globalkeybinding = GlobalKeyBinding (self, "key_binding")

    def configure_actions (self, action_group):
        action = action_group.get_action ("Text")
        action.set_property ('icon-name', 'text-x-generic')
        action.connect ('activate', self.on_action_activate)
        action = action_group.get_action ("Web")
        action.set_property ('icon-name', 'text-html')
        action.connect ('activate', self.on_action_activate)
        action = action_group.get_action ("Suggestions")
        action.set_property ('icon-name', 'package-x-generic')
        action.connect ('activate', self.on_action_activate)

    def on_action_activate (self, action):
        name= action.get_name()
        if name == 'Text':
            type = self.TEXT_TRANSLATION
        elif name == 'Web':
            type = self.WEB_TRANSLATION
        elif name == 'Suggestions':
            type = self.TRANSLATION_SUGGESTIONS
        self.open_translation (type)

    @dbus.service.method ("de.berlios.FreeSpeak",
                          in_signature='', out_signature='b')
    def is_running (self):
        """
        Returns True whether the application is running, False otherwise.
        """
        return self.running

    @dbus.service.method ("de.berlios.FreeSpeak",
                          in_signature='a{sv}asib', out_signature='b')
    def start (self, options=None, args=None, timestamp=None, show_main_window=True):
        """
        Start the application in blocking mode.
        If the application is already running, the main window will be presented
        to the user and the function will return.
        """
        if self.running:
            if not timestamp:
                timestamp = int (time.time ())
            self.main_window.present_with_time (timestamp)
            return False

        gtk.gdk.threads_init()

        self.main_window = MainWindow (self)
        if show_main_window:
            self.main_window.show ()

        self.globalkeybinding.grab ()
        self.globalkeybinding.start ()

        self.running = True
        return True

    @dbus.service.method ("de.berlios.FreeSpeak",
                          in_signature='', out_signature='')
    def stop (self):
        """
        Stop the application from running.
        """
        if self.running:
            self.globalkeybinding.stop ()
            self.main_window.destroy ()
            self.running = False
            self.stopped ()

    @dbus.service.method ("de.berlios.FreeSpeak",
                          in_signature='i', out_signature='b')
    def open_translation (self, type):
        return self.main_window.open_translation (type)

    @dbus.service.signal ("de.berlios.FreeSpeak",
                          signature="")
    def stopped (self):
        """
        Signal the application has been stopped
        """
        pass

    @dbus.service.signal ("de.berlios.FreeSpeak",
                          signature="")
    def closed (self):
        """
        Signal the application has been closed but not stopped
        """
        pass
Ejemplo n.º 4
0
class Application(dbus.service.Object):
    """
    An object for managing a FreeSpeak application instance.
    WARNING: You should NOT create directly an application, please take a look at
    the get_instance() function to ensure the Application singleton.
    """
    TEXT_TRANSLATION = 0
    WEB_TRANSLATION = 1
    TRANSLATION_SUGGESTIONS = 2

    def __init__(self, bus, path, name):
        dbus.service.Object.__init__(self, bus, path, name)

        self.globalkeybinding = None
        self.translators_path = None
        self.icons_path = None
        self.icon_theme = None
        self.clipboard = None
        self.main_window = None
        self.config = None
        self.translators_manager = None

        self.setup_l10n()
        self.setup_exception_dialog()
        self.setup_config()
        self.setup_paths()
        self.setup_icons()
        self.setup_translators_manager()
        self.setup_clipboard()
        self.setup_style()
        self.setup_globalkeybinding()

        self.running = False
        self.gtkmain = False

    def setup_exception_dialog(self):
        """
        Setup a Python-wide exception hook
        """
        # FIXME: must take care if another excepthook had been installed
        sys.excepthook = exception_dialog.exception_hook

    @staticmethod
    def setup_l10n():
        """
        Install the _ gettext function
        """
        # FIXME: must take care if it has been already called then we're
        #       going to override the _
        gettext.install(defs.GETTEXT_PACKAGE, unicode=True)

    def setup_config(self):
        """
        Create a configuration object
        """
        self.config = Config()

    def setup_paths(self):
        """
        Setup common paths used for finding FreeSpeak resources
        """
        self.icons_path = os.path.join(defs.DATA_DIR, defs.PACKAGE, 'art')
        self.translators_path = os.path.dirname(freespeak.translators.__file__)

    def setup_icons(self):
        """
        Setup the GTK+ icon theme, adding the path for FreeSpeak art.
        Set the default icon for all windows.
        """
        self.icon_theme = gtk.icon_theme_get_default()
        self.icon_theme.append_search_path(self.icons_path)
        # FIXME: must take care if the application was created from another
        #       application.
        window_icon = self.icon_theme.load_icon(defs.PACKAGE, 64, 0)
        gtk.window_set_default_icon(window_icon)

    def setup_translators_manager(self):
        """
        Create a TranslatorsManager instance for managing translators/translating engines.
        """
        self.translators_manager = TranslatorsManager(self)

    def setup_clipboard(self):
        """
        Create a ClipboardController instance for managing the clipboard
        """
        self.clipboard = ClipboardController(self)

    def setup_style(self):
        """
        Tweak the GTK+ widgets style
        """
        style.setup_rc()

    def setup_globalkeybinding(self):
        """
        Create GlobalKeyBinding for the 'key_binding' setting
        """
        self.globalkeybinding = GlobalKeyBinding(self, "key_binding")

    def configure_actions(self, action_group):
        action = action_group.get_action("Text")
        action.set_property('icon-name', 'text-x-generic')
        action.connect('activate', self.on_action_activate)
        action = action_group.get_action("Web")
        action.set_property('icon-name', 'text-html')
        action.connect('activate', self.on_action_activate)
        action = action_group.get_action("Suggestions")
        action.set_property('icon-name', 'package-x-generic')
        action.connect('activate', self.on_action_activate)

    def on_action_activate(self, action):
        name = action.get_name()
        if name == 'Text':
            type = self.TEXT_TRANSLATION
        elif name == 'Web':
            type = self.WEB_TRANSLATION
        elif name == 'Suggestions':
            type = self.TRANSLATION_SUGGESTIONS
        self.open_translation(type)

    @dbus.service.method("de.berlios.FreeSpeak",
                         in_signature='',
                         out_signature='b')
    def is_running(self):
        """
        Returns True whether the application is running, False otherwise.
        """
        return self.running

    @dbus.service.method("de.berlios.FreeSpeak",
                         in_signature='a{sv}asib',
                         out_signature='b')
    def start(self,
              options=None,
              args=None,
              timestamp=None,
              show_main_window=True):
        """
        Start the application in blocking mode.
        If the application is already running, the main window will be presented
        to the user and the function will return.
        """
        if self.running:
            if not timestamp:
                timestamp = int(time.time())
            self.main_window.present_with_time(timestamp)
            return False

        gtk.gdk.threads_init()

        self.main_window = MainWindow(self)
        if show_main_window:
            self.main_window.show()

        self.globalkeybinding.grab()
        self.globalkeybinding.start()

        self.running = True
        return True

    @dbus.service.method("de.berlios.FreeSpeak",
                         in_signature='',
                         out_signature='')
    def stop(self):
        """
        Stop the application from running.
        """
        if self.running:
            self.globalkeybinding.stop()
            self.main_window.destroy()
            self.running = False
            self.stopped()

    @dbus.service.method("de.berlios.FreeSpeak",
                         in_signature='i',
                         out_signature='b')
    def open_translation(self, type):
        return self.main_window.open_translation(type)

    @dbus.service.signal("de.berlios.FreeSpeak", signature="")
    def stopped(self):
        """
        Signal the application has been stopped
        """
        pass

    @dbus.service.signal("de.berlios.FreeSpeak", signature="")
    def closed(self):
        """
        Signal the application has been closed but not stopped
        """
        pass