From 2298af9dd9cb24fd655934380af4a730661738b1 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 22 Dec 2016 19:48:48 +0100 Subject: [PATCH] Handle CommandLine inputs with GtkApplication --- src/application.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++ src/gajim.py | 65 ++++-------------------------------- 2 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 src/application.py diff --git a/src/application.py b/src/application.py new file mode 100644 index 000000000..288f67e1a --- /dev/null +++ b/src/application.py @@ -0,0 +1,83 @@ +# -*- coding:utf-8 -*- +## src/application.py +## +## Copyright (C) 2016 Emmanuel Gil Peyrot +## +## This file is part of Gajim. +## +## Gajim 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; version 3 only. +## +## Gajim 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 Gajim. If not, see . +## + +from gi.repository import GLib, Gio, Gtk +from common import logging_helpers + + +class GajimApplication(Gtk.Application): + '''Main class handling activation and command line.''' + + def __init__(self): + Gtk.Application.__init__(self, application_id='org.gajim.Gajim', + flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE) + self.add_main_option('quiet', ord('q'), GLib.OptionFlags.NONE, + GLib.OptionArg.NONE, + _('Show only critical errors')) + self.add_main_option('separate', ord('s'), GLib.OptionFlags.NONE, + GLib.OptionArg.NONE, + _('Separate profile files completely (even ' + 'history db and plugins)')) + self.add_main_option('verbose', ord('v'), GLib.OptionFlags.NONE, + GLib.OptionArg.NONE, + _('Print xml stanzas and other debug ' + 'information')) + self.add_main_option('profile', ord('p'), GLib.OptionFlags.NONE, + GLib.OptionArg.STRING, + _('Use defined profile in configuration ' + 'directory'), 'NAME') + self.add_main_option('config-path', ord('c'), GLib.OptionFlags.NONE, + GLib.OptionArg.STRING, + _('Set configuration directory'), 'PATH') + self.add_main_option('loglevel', ord('l'), GLib.OptionFlags.NONE, + GLib.OptionArg.STRING, + _('Configure logging system'), 'LEVEL') + + self.profile = '' + self.config_path = None + self.profile_separation = False + + def do_startup(self): + Gtk.Application.do_startup(self) + + def do_activate(self): + Gtk.Application.do_activate(self) + + def do_command_line(self, command_line: Gio.ApplicationCommandLine) -> int: + Gtk.Application.do_command_line(self, command_line) + options = command_line.get_options_dict() + if options.contains('quiet'): + logging_helpers.set_quiet() + if options.contains('separate'): + self.profile_separation = True + if options.contains('verbose'): + logging_helpers.set_verbose() + if options.contains('profile'): + variant = options.lookup_value('profile') + self.profile = variant.get_string() + if options.contains('loglevel'): + variant = options.lookup_value('loglevel') + string = variant.get_string() + logging_helpers.set_loglevels(string) + if options.contains('config-path'): + variant = options.lookup_value('config-path') + self.config_path = variant.get_string() + self.activate() + return 0 diff --git a/src/gajim.py b/src/gajim.py index 983dfea72..bfe9be167 100644 --- a/src/gajim.py +++ b/src/gajim.py @@ -110,68 +110,17 @@ import logging # gajim.gui or gajim.gtk more appropriate ? log = logging.getLogger('gajim.gajim') -import getopt + from common import i18n - -def parseOpts(): - profile_ = '' - config_path_ = None - profile_separation_ = False - - try: - shortargs = 'hqsvl:p:c:' - # add gtk/gnome session option as gtk_get_option_group is not wrapped - longargs = 'help quiet separate verbose loglevel= profile= config-path=' - longargs += ' class= name= screen= gtk-module= sync g-fatal-warnings' - longargs += ' sm-client-id= sm-client-state-file= sm-disable' - opts = getopt.getopt(sys.argv[1:], shortargs, longargs.split())[0] - except getopt.error as msg1: - print(str(msg1)) - print('for help use --help') - sys.exit(2) - for o, a in opts: - if o in ('-h', '--help'): - out = _('Usage:') + \ - '\n gajim [options] filename\n\n' + \ - _('Options:') + \ - '\n -h, --help ' + \ - _('Show this help message and exit') + \ - '\n -q, --quiet ' + \ - _('Show only critical errors') + \ - '\n -s, --separate ' + \ - _('Separate profile files completely (even history db and plugins)') + \ - '\n -v, --verbose ' + \ - _('Print xml stanzas and other debug information') + \ - '\n -p, --profile ' + \ - _('Use defined profile in configuration directory') + \ - '\n -c, --config-path ' + \ - _('Set configuration directory') + \ - '\n -l, --loglevel ' + \ - _('Configure logging system') + '\n' - print(out) - sys.exit() - elif o in ('-q', '--quiet'): - logging_helpers.set_quiet() - elif o in ('-s', '--separate'): - profile_separation_ = True - elif o in ('-v', '--verbose'): - logging_helpers.set_verbose() - elif o in ('-p', '--profile'): # gajim --profile name - profile_ = a - elif o in ('-l', '--loglevel'): - logging_helpers.set_loglevels(a) - elif o in ('-c', '--config-path'): - config_path_ = a - return profile_, config_path_, profile_separation_ - import locale -profile, config_path, profile_separation = parseOpts() -del parseOpts +from application import GajimApplication + +app = GajimApplication() +app.run(sys.argv) import common.configpaths -common.configpaths.gajimpaths.init(config_path, profile, profile_separation) -del config_path -del profile +common.configpaths.gajimpaths.init( + app.config_path, app.profile, app.profile_separation) if os.name == 'nt': plugins_locale_dir = os.path.join(common.configpaths.gajimpaths[