2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/advanced.py
|
2005-04-24 01:57:02 +02:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005 Travis Shirk <travis AT pobox.com>
|
|
|
|
## Vincent Hanquez <tab AT snarc.org>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2010-03-11 16:52:36 +01:00
|
|
|
## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Copyright (C) 2006-2007 Jean-Marie Traissard <jim AT lapin.org>
|
2005-04-24 01:57:02 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-04-24 01:57:02 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-04-24 01:57:02 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-04-24 01:57:02 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-04-24 01:57:02 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-04-24 01:57:02 +02:00
|
|
|
|
2017-03-04 21:22:46 +01:00
|
|
|
from enum import IntEnum, unique
|
2017-02-07 21:18:41 +01:00
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Gtk
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim import gtkgui_helpers
|
2013-07-28 20:50:30 +02:00
|
|
|
from gi.repository import GLib
|
2015-08-10 23:12:12 +02:00
|
|
|
from gi.repository import Pango
|
2005-04-24 01:57:02 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import gajim
|
2005-05-18 14:02:57 +02:00
|
|
|
|
2017-03-04 21:22:46 +01:00
|
|
|
@unique
|
2017-02-07 21:18:41 +01:00
|
|
|
class Column(IntEnum):
|
|
|
|
PREFERENCE_NAME = 0
|
|
|
|
VALUE = 1
|
|
|
|
TYPE = 2
|
2005-09-08 23:29:42 +02:00
|
|
|
|
2008-11-05 21:26:58 +01:00
|
|
|
def rate_limit(rate):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Call func at most *rate* times per second
|
|
|
|
"""
|
|
|
|
def decorator(func):
|
|
|
|
timeout = [None]
|
|
|
|
def f(*args, **kwargs):
|
|
|
|
if timeout[0] is not None:
|
2013-07-28 20:50:30 +02:00
|
|
|
GLib.source_remove(timeout[0])
|
2010-02-08 15:08:40 +01:00
|
|
|
timeout[0] = None
|
|
|
|
def timeout_func():
|
|
|
|
func(*args, **kwargs)
|
|
|
|
timeout[0] = None
|
2013-07-28 20:50:30 +02:00
|
|
|
timeout[0] = GLib.timeout_add(int(1000.0 / rate), timeout_func)
|
2010-02-08 15:08:40 +01:00
|
|
|
return f
|
|
|
|
return decorator
|
2008-11-05 21:26:58 +01:00
|
|
|
|
|
|
|
def tree_model_iter_children(model, treeiter):
|
2010-02-08 15:08:40 +01:00
|
|
|
it = model.iter_children(treeiter)
|
|
|
|
while it:
|
|
|
|
yield it
|
|
|
|
it = model.iter_next(it)
|
2008-11-05 21:26:58 +01:00
|
|
|
|
|
|
|
def tree_model_pre_order(model, treeiter):
|
2010-02-08 15:08:40 +01:00
|
|
|
yield treeiter
|
|
|
|
for childiter in tree_model_iter_children(model, treeiter):
|
|
|
|
for it in tree_model_pre_order(model, childiter):
|
|
|
|
yield it
|
2008-11-05 21:26:58 +01:00
|
|
|
|
|
|
|
|
2006-10-06 02:08:46 +02:00
|
|
|
class AdvancedConfigurationWindow(object):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.xml = gtkgui_helpers.get_gtk_builder('advanced_configuration_window.ui')
|
|
|
|
self.window = self.xml.get_object('advanced_configuration_window')
|
2012-12-31 12:43:53 +01:00
|
|
|
self.window.set_transient_for(
|
2010-02-08 15:08:40 +01:00
|
|
|
gajim.interface.instances['preferences'].window)
|
|
|
|
self.entry = self.xml.get_object('advanced_entry')
|
|
|
|
self.desc_label = self.xml.get_object('advanced_desc_label')
|
2014-05-29 22:46:48 +02:00
|
|
|
self.restart_box = self.xml.get_object('restart_box')
|
2012-04-09 19:45:56 +02:00
|
|
|
self.reset_button = self.xml.get_object('reset_button')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Format:
|
|
|
|
# key = option name (root/subopt/opt separated by \n then)
|
|
|
|
# value = array(oldval, newval)
|
|
|
|
self.changed_opts = {}
|
|
|
|
|
|
|
|
# For i18n
|
|
|
|
self.right_true_dict = {True: _('Activated'), False: _('Deactivated')}
|
|
|
|
self.types = {
|
|
|
|
'boolean': _('Boolean'),
|
|
|
|
'integer': _('Integer'),
|
|
|
|
'string': _('Text'),
|
|
|
|
'color': _('Color')}
|
|
|
|
|
|
|
|
treeview = self.xml.get_object('advanced_treeview')
|
|
|
|
self.treeview = treeview
|
2012-12-23 16:23:43 +01:00
|
|
|
self.model = Gtk.TreeStore(str, str, str)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.fill_model()
|
2012-12-23 16:23:43 +01:00
|
|
|
self.model.set_sort_column_id(0, Gtk.SortType.ASCENDING)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.modelfilter = self.model.filter_new()
|
|
|
|
self.modelfilter.set_visible_func(self.visible_func)
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
renderer_text = Gtk.CellRendererText()
|
2012-12-31 12:43:53 +01:00
|
|
|
col = Gtk.TreeViewColumn(_('Preference Name'), renderer_text, text = 0)
|
|
|
|
treeview.insert_column(col, -1)
|
2010-02-08 15:08:40 +01:00
|
|
|
col.set_resizable(True)
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
renderer_text = Gtk.CellRendererText()
|
2010-02-08 15:08:40 +01:00
|
|
|
renderer_text.connect('edited', self.on_config_edited)
|
2015-08-10 23:12:12 +02:00
|
|
|
renderer_text.set_property('ellipsize', Pango.EllipsizeMode.END)
|
2012-12-31 12:43:53 +01:00
|
|
|
col = Gtk.TreeViewColumn(_('Value'),renderer_text, text = 1)
|
|
|
|
treeview.insert_column(col, -1)
|
2010-02-08 15:08:40 +01:00
|
|
|
col.set_cell_data_func(renderer_text, self.cb_value_column_data)
|
|
|
|
|
|
|
|
col.props.resizable = True
|
2013-09-15 12:16:55 +02:00
|
|
|
col.props.expand = True
|
|
|
|
col.props.sizing = Gtk.TreeViewColumnSizing.FIXED
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
renderer_text = Gtk.CellRendererText()
|
2012-12-31 12:43:53 +01:00
|
|
|
col = Gtk.TreeViewColumn(_('Type'), renderer_text, text = 2)
|
|
|
|
treeview.insert_column(col, -1)
|
2013-09-15 12:16:55 +02:00
|
|
|
col.props.sizing = Gtk.TreeViewColumnSizing.FIXED
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
treeview.set_model(self.modelfilter)
|
|
|
|
|
|
|
|
# connect signal for selection change
|
|
|
|
treeview.get_selection().connect('changed',
|
|
|
|
self.on_advanced_treeview_selection_changed)
|
|
|
|
|
|
|
|
self.xml.connect_signals(self)
|
2014-05-29 22:46:48 +02:00
|
|
|
self.restart_box.set_no_show_all(True)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.window.show_all()
|
|
|
|
gajim.interface.instances['advanced_config'] = self
|
|
|
|
|
2012-12-31 12:43:53 +01:00
|
|
|
def cb_value_column_data(self, col, cell, model, iter_, data):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Check if it's boolen or holds password stuff and if yes make the
|
|
|
|
cellrenderertext not editable, else - it's editable
|
|
|
|
"""
|
2017-02-07 21:18:41 +01:00
|
|
|
optname = model[iter_][Column.PREFERENCE_NAME]
|
|
|
|
opttype = model[iter_][Column.TYPE]
|
2013-01-01 19:44:25 +01:00
|
|
|
if opttype == self.types['boolean'] or optname == 'password':
|
2010-02-08 15:08:40 +01:00
|
|
|
cell.set_property('editable', False)
|
|
|
|
else:
|
|
|
|
cell.set_property('editable', True)
|
|
|
|
|
2017-02-07 20:32:56 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_option_path(model, iter_):
|
2010-02-08 15:08:40 +01:00
|
|
|
# It looks like path made from reversed array
|
|
|
|
# path[0] is the true one optname
|
|
|
|
# path[1] is the key name
|
|
|
|
# path[2] is the root of tree
|
|
|
|
# last two is optional
|
2013-01-01 19:44:25 +01:00
|
|
|
path = [model[iter_][0]]
|
2010-02-08 15:08:40 +01:00
|
|
|
parent = model.iter_parent(iter_)
|
|
|
|
while parent:
|
2013-01-01 19:44:25 +01:00
|
|
|
path.append(model[parent][0])
|
2010-02-08 15:08:40 +01:00
|
|
|
parent = model.iter_parent(parent)
|
|
|
|
return path
|
|
|
|
|
|
|
|
def on_advanced_treeview_selection_changed(self, treeselection):
|
|
|
|
model, iter_ = treeselection.get_selected()
|
|
|
|
# Check for GtkTreeIter
|
|
|
|
if iter_:
|
|
|
|
opt_path = self.get_option_path(model, iter_)
|
|
|
|
# Get text from first column in this row
|
|
|
|
desc = None
|
|
|
|
if len(opt_path) == 3:
|
2017-07-10 21:31:31 +02:00
|
|
|
desc = gajim.config.get_desc_per(opt_path[2], opt_path[0])
|
2010-02-08 15:08:40 +01:00
|
|
|
elif len(opt_path) == 1:
|
|
|
|
desc = gajim.config.get_desc(opt_path[0])
|
|
|
|
if desc:
|
|
|
|
self.desc_label.set_text(desc)
|
|
|
|
else:
|
|
|
|
#we talk about option description in advanced configuration editor
|
|
|
|
self.desc_label.set_text(_('(None)'))
|
2012-04-09 19:45:56 +02:00
|
|
|
if len(opt_path) == 3 or (len(opt_path) == 1 and not \
|
|
|
|
model.iter_has_child(iter_)):
|
|
|
|
self.reset_button.set_sensitive(True)
|
|
|
|
else:
|
|
|
|
self.reset_button.set_sensitive(False)
|
|
|
|
else:
|
|
|
|
self.reset_button.set_sensitive(False)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def remember_option(self, option, oldval, newval):
|
|
|
|
if option in self.changed_opts:
|
|
|
|
self.changed_opts[option] = (self.changed_opts[option][0], newval)
|
|
|
|
else:
|
|
|
|
self.changed_opts[option] = (oldval, newval)
|
|
|
|
|
|
|
|
def on_advanced_treeview_row_activated(self, treeview, path, column):
|
|
|
|
modelpath = self.modelfilter.convert_path_to_child_path(path)
|
|
|
|
modelrow = self.model[modelpath]
|
2013-01-01 19:44:25 +01:00
|
|
|
option = modelrow[0]
|
|
|
|
if modelrow[2] == self.types['boolean']:
|
2010-02-08 15:08:40 +01:00
|
|
|
for key in self.right_true_dict.keys():
|
2013-01-01 19:44:25 +01:00
|
|
|
if self.right_true_dict[key] == modelrow[1]:
|
2012-12-31 13:48:15 +01:00
|
|
|
modelrow[1] = str(key)
|
2010-02-08 15:08:40 +01:00
|
|
|
newval = {'False': True, 'True': False}[modelrow[1]]
|
2012-12-31 13:48:15 +01:00
|
|
|
if len(modelpath.get_indices()) > 1:
|
|
|
|
optnamerow = self.model[modelpath.get_indices()[0]]
|
2013-01-01 19:44:25 +01:00
|
|
|
optname = optnamerow[0]
|
2013-01-08 12:54:20 +01:00
|
|
|
modelpath.up()
|
|
|
|
keyrow = self.model[modelpath]
|
2013-01-01 19:44:25 +01:00
|
|
|
key = keyrow[0]
|
2010-02-08 15:08:40 +01:00
|
|
|
self.remember_option(option + '\n' + key + '\n' + optname,
|
|
|
|
modelrow[1], newval)
|
|
|
|
gajim.config.set_per(optname, key, option, newval)
|
|
|
|
else:
|
|
|
|
self.remember_option(option, modelrow[1], newval)
|
|
|
|
gajim.config.set(option, newval)
|
|
|
|
modelrow[1] = self.right_true_dict[newval]
|
|
|
|
self.check_for_restart()
|
|
|
|
|
|
|
|
def check_for_restart(self):
|
2014-05-29 22:46:48 +02:00
|
|
|
self.restart_box.hide()
|
2010-02-08 15:08:40 +01:00
|
|
|
for opt in self.changed_opts:
|
|
|
|
opt_path = opt.split('\n')
|
|
|
|
if len(opt_path)==3:
|
|
|
|
restart = gajim.config.get_restart_per(opt_path[2], opt_path[1],
|
|
|
|
opt_path[0])
|
|
|
|
else:
|
|
|
|
restart = gajim.config.get_restart(opt_path[0])
|
|
|
|
if restart:
|
|
|
|
if self.changed_opts[opt][0] != self.changed_opts[opt][1]:
|
2014-05-29 22:46:48 +02:00
|
|
|
self.restart_box.set_no_show_all(False)
|
|
|
|
self.restart_box.show_all()
|
2010-02-08 15:08:40 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
def on_config_edited(self, cell, path, text):
|
|
|
|
# convert modelfilter path to model path
|
2012-12-31 12:43:53 +01:00
|
|
|
path=Gtk.TreePath.new_from_string(path)
|
2010-02-08 15:08:40 +01:00
|
|
|
modelpath = self.modelfilter.convert_path_to_child_path(path)
|
|
|
|
modelrow = self.model[modelpath]
|
2013-01-01 19:44:25 +01:00
|
|
|
option = modelrow[0]
|
2013-01-03 19:42:44 +01:00
|
|
|
if modelpath.get_depth() > 2:
|
|
|
|
modelpath.up() # Get parent
|
|
|
|
keyrow = self.model[modelpath]
|
2013-01-01 19:44:25 +01:00
|
|
|
key = keyrow[0]
|
2013-01-03 19:42:44 +01:00
|
|
|
modelpath.up() # Get parent
|
|
|
|
optnamerow = self.model[modelpath]
|
|
|
|
optname = optnamerow[0]
|
2010-02-08 15:08:40 +01:00
|
|
|
self.remember_option(option + '\n' + key + '\n' + optname, modelrow[1],
|
|
|
|
text)
|
|
|
|
gajim.config.set_per(optname, key, option, text)
|
|
|
|
else:
|
|
|
|
self.remember_option(option, modelrow[1], text)
|
|
|
|
gajim.config.set(option, text)
|
|
|
|
modelrow[1] = text
|
|
|
|
self.check_for_restart()
|
|
|
|
|
2017-02-07 20:32:56 +01:00
|
|
|
@staticmethod
|
|
|
|
def on_advanced_configuration_window_destroy(widget):
|
2010-02-08 15:08:40 +01:00
|
|
|
del gajim.interface.instances['advanced_config']
|
|
|
|
|
2012-04-09 19:45:56 +02:00
|
|
|
def on_reset_button_clicked(self, widget):
|
|
|
|
model, iter_ = self.treeview.get_selection().get_selected()
|
|
|
|
# Check for GtkTreeIter
|
|
|
|
if iter_:
|
|
|
|
path = model.get_path(iter_)
|
|
|
|
opt_path = self.get_option_path(model, iter_)
|
|
|
|
if len(opt_path) == 1:
|
|
|
|
default = gajim.config.get_default(opt_path[0])
|
|
|
|
elif len(opt_path) == 3:
|
|
|
|
default = gajim.config.get_default_per(opt_path[2], opt_path[0])
|
|
|
|
|
2017-02-07 21:18:41 +01:00
|
|
|
if model[iter_][Column.TYPE] == self.types['boolean']:
|
|
|
|
if self.right_true_dict[default] == model[iter_][Column.VALUE]:
|
2012-04-09 19:45:56 +02:00
|
|
|
return
|
|
|
|
modelpath = self.modelfilter.convert_path_to_child_path(path)
|
|
|
|
modelrow = self.model[modelpath]
|
2013-01-01 19:44:25 +01:00
|
|
|
option = modelrow[0]
|
2012-04-09 19:45:56 +02:00
|
|
|
if len(modelpath) > 1:
|
|
|
|
optnamerow = self.model[modelpath[0]]
|
2013-01-01 19:44:25 +01:00
|
|
|
optname = optnamerow[0]
|
2012-04-09 19:45:56 +02:00
|
|
|
keyrow = self.model[modelpath[:2]]
|
2013-01-01 19:44:25 +01:00
|
|
|
key = keyrow[0]
|
2012-04-09 19:45:56 +02:00
|
|
|
self.remember_option(option + '\n' + key + '\n' + optname,
|
2017-02-07 21:18:41 +01:00
|
|
|
modelrow[Column.VALUE], default)
|
2012-04-09 19:45:56 +02:00
|
|
|
gajim.config.set_per(optname, key, option, default)
|
|
|
|
else:
|
2017-02-07 21:18:41 +01:00
|
|
|
self.remember_option(option, modelrow[Column.VALUE], default)
|
2012-04-09 19:45:56 +02:00
|
|
|
gajim.config.set(option, default)
|
2017-02-07 21:18:41 +01:00
|
|
|
modelrow[Column.VALUE] = self.right_true_dict[default]
|
2012-04-09 19:45:56 +02:00
|
|
|
self.check_for_restart()
|
|
|
|
else:
|
2017-02-07 21:18:41 +01:00
|
|
|
if str(default) == model[iter_][Column.VALUE]:
|
2012-04-09 19:45:56 +02:00
|
|
|
return
|
2013-01-07 10:48:07 +01:00
|
|
|
self.on_config_edited(None, path.to_string(), str(default))
|
2012-04-09 19:45:56 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_advanced_close_button_clicked(self, widget):
|
|
|
|
self.window.destroy()
|
|
|
|
|
|
|
|
def fill_model(self, node=None, parent=None):
|
|
|
|
for item, option in gajim.config.get_children(node):
|
|
|
|
name = item[-1]
|
|
|
|
if option is None: # Node
|
|
|
|
newparent = self.model.append(parent, [name, '', ''])
|
|
|
|
self.fill_model(item, newparent)
|
|
|
|
else: # Leaf
|
2012-04-09 19:45:56 +02:00
|
|
|
if len(item) == 1:
|
|
|
|
type_ = self.types[gajim.config.get_type(name)]
|
|
|
|
elif len(item) == 3:
|
|
|
|
type_ = self.types[gajim.config.get_type_per(item[0],
|
|
|
|
item[2])]
|
2010-02-08 15:08:40 +01:00
|
|
|
if name == 'password':
|
|
|
|
value = _('Hidden')
|
|
|
|
else:
|
|
|
|
if type_ == self.types['boolean']:
|
2012-04-09 19:45:56 +02:00
|
|
|
value = self.right_true_dict[option]
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2012-12-31 12:43:53 +01:00
|
|
|
try:
|
|
|
|
value = str(option)
|
|
|
|
except:
|
|
|
|
value = option
|
2010-02-08 15:08:40 +01:00
|
|
|
self.model.append(parent, [name, value, type_])
|
|
|
|
|
2012-12-31 12:43:53 +01:00
|
|
|
def visible_func(self, model, treeiter, data):
|
2013-01-01 19:44:25 +01:00
|
|
|
search_string = self.entry.get_text().lower()
|
2010-02-10 19:24:11 +01:00
|
|
|
for it in tree_model_pre_order(model, treeiter):
|
2017-02-07 21:18:41 +01:00
|
|
|
if model[it][Column.TYPE] != '':
|
2010-02-08 15:08:40 +01:00
|
|
|
opt_path = self.get_option_path(model, it)
|
|
|
|
if len(opt_path) == 3:
|
2017-07-10 21:31:31 +02:00
|
|
|
desc = gajim.config.get_desc_per(opt_path[2], opt_path[0])
|
2010-02-08 15:08:40 +01:00
|
|
|
elif len(opt_path) == 1:
|
|
|
|
desc = gajim.config.get_desc(opt_path[0])
|
2017-02-07 21:18:41 +01:00
|
|
|
if search_string in model[it][Column.PREFERENCE_NAME] or (desc and \
|
2010-02-08 15:08:40 +01:00
|
|
|
search_string in desc.lower()):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@rate_limit(3)
|
|
|
|
def on_advanced_entry_changed(self, widget):
|
|
|
|
self.modelfilter.refilter()
|
|
|
|
if not widget.get_text():
|
|
|
|
# Maybe the expanded rows should be remembered here ...
|
|
|
|
self.treeview.collapse_all()
|
|
|
|
else:
|
|
|
|
# ... and be restored correctly here
|
|
|
|
self.treeview.expand_all()
|