Handle CommandLine inputs with GtkApplication

This commit is contained in:
Emmanuel Gil Peyrot 2016-12-22 19:48:48 +01:00 committed by Philipp Hörist
parent 42d4aa2ab4
commit 2298af9dd9
2 changed files with 90 additions and 58 deletions

83
src/application.py Normal file
View File

@ -0,0 +1,83 @@
# -*- coding:utf-8 -*-
## src/application.py
##
## Copyright (C) 2016 Emmanuel Gil Peyrot <linkmauve AT linkmauve.fr>
##
## 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 <http://www.gnu.org/licenses/>.
##
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

View File

@ -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[