Welcome to mirror list, hosted at ThFree Co, Russian Federation.

advanced_configuration_window.py « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16ef488e98ab77b9f370eafca20ba466b8902c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# -*- coding:utf-8 -*-
## src/advanced.py
##
## Copyright (C) 2005 Travis Shirk <travis AT pobox.com>
##                    Vincent Hanquez <tab AT snarc.org>
## Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
## Copyright (C) 2006-2007 Jean-Marie Traissard <jim AT lapin.org>
##
## 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 Gtk
import gtkgui_helpers
from gi.repository import GLib

from common import gajim
from common import helpers

(
OPT_TYPE,
OPT_VAL
) = range(2)

(
C_PREFNAME,
C_VALUE,
C_TYPE
) = range(3)

GTKGUI_GLADE = 'manage_accounts_window.ui'

def rate_limit(rate):
    """
    Call func at most *rate* times per second
    """
    def decorator(func):
        timeout = [None]
        def f(*args, **kwargs):
            if timeout[0] is not None:
                GLib.source_remove(timeout[0])
                timeout[0] = None
            def timeout_func():
                func(*args, **kwargs)
                timeout[0] = None
            timeout[0] = GLib.timeout_add(int(1000.0 / rate), timeout_func)
        return f
    return decorator

def tree_model_iter_children(model, treeiter):
    it = model.iter_children(treeiter)
    while it:
        yield it
        it = model.iter_next(it)

def tree_model_pre_order(model, treeiter):
    yield treeiter
    for childiter in tree_model_iter_children(model, treeiter):
        for it in tree_model_pre_order(model, childiter):
            yield it


class AdvancedConfigurationWindow(object):
    def __init__(self):
        self.xml = gtkgui_helpers.get_gtk_builder('advanced_configuration_window.ui')
        self.window = self.xml.get_object('advanced_configuration_window')
        self.window.set_transient_for(
                gajim.interface.instances['preferences'].window)
        self.entry = self.xml.get_object('advanced_entry')
        self.desc_label = self.xml.get_object('advanced_desc_label')
        self.restart_box = self.xml.get_object('restart_box')
        self.reset_button = self.xml.get_object('reset_button')

        # 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
        self.model = Gtk.TreeStore(str, str, str)
        self.fill_model()
        self.model.set_sort_column_id(0, Gtk.SortType.ASCENDING)
        self.modelfilter = self.model.filter_new()
        self.modelfilter.set_visible_func(self.visible_func)

        renderer_text = Gtk.CellRendererText()
        col = Gtk.TreeViewColumn(_('Preference Name'), renderer_text, text = 0)
        treeview.insert_column(col, -1)
        col.set_resizable(True)

        renderer_text = Gtk.CellRendererText()
        renderer_text.connect('edited', self.on_config_edited)
        col = Gtk.TreeViewColumn(_('Value'),renderer_text, text = 1)
        treeview.insert_column(col, -1)
        col.set_cell_data_func(renderer_text, self.cb_value_column_data)

        col.props.resizable = True
        col.props.expand = True
        col.props.sizing = Gtk.TreeViewColumnSizing.FIXED

        renderer_text = Gtk.CellRendererText()
        col = Gtk.TreeViewColumn(_('Type'), renderer_text, text = 2)
        treeview.insert_column(col, -1)
        col.props.expand = True
        col.props.sizing = Gtk.TreeViewColumnSizing.FIXED

        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)
        self.restart_box.set_no_show_all(True)
        self.window.show_all()
        gajim.interface.instances['advanced_config'] = self

    def cb_value_column_data(self, col, cell, model, iter_, data):
        """
        Check if it's boolen or holds password stuff and if yes  make the
        cellrenderertext not editable, else - it's editable
        """
        optname = model[iter_][C_PREFNAME]
        opttype = model[iter_][C_TYPE]
        if opttype == self.types['boolean'] or optname == 'password':
            cell.set_property('editable', False)
        else:
            cell.set_property('editable', True)

    def get_option_path(self, model, iter_):
        # 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
        path = [model[iter_][0]]
        parent = model.iter_parent(iter_)
        while parent:
            path.append(model[parent][0])
            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:
                desc = gajim.config.get_desc_per(opt_path[2], opt_path[1],
                        opt_path[0])
            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)'))
            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)

    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]
        option = modelrow[0]
        if modelrow[2] == self.types['boolean']:
            for key in self.right_true_dict.keys():
                if self.right_true_dict[key] == modelrow[1]:
                    modelrow[1] = str(key)
            newval = {'False': True, 'True': False}[modelrow[1]]
            if len(modelpath.get_indices()) > 1:
                optnamerow = self.model[modelpath.get_indices()[0]]
                optname = optnamerow[0]
                modelpath.up()
                keyrow = self.model[modelpath]
                key = keyrow[0]
                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):
        self.restart_box.hide()
        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]:
                    self.restart_box.set_no_show_all(False)
                    self.restart_box.show_all()
                    break

    def on_config_edited(self, cell, path, text):
        # convert modelfilter path to model path
        path=Gtk.TreePath.new_from_string(path)
        modelpath = self.modelfilter.convert_path_to_child_path(path)
        modelrow = self.model[modelpath]
        option = modelrow[0]
        if modelpath.get_depth() > 2:
            modelpath.up() # Get parent
            keyrow = self.model[modelpath]
            key = keyrow[0]
            modelpath.up() # Get parent
            optnamerow = self.model[modelpath]
            optname = optnamerow[0]
            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()

    def on_advanced_configuration_window_destroy(self, widget):
        del gajim.interface.instances['advanced_config']

    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])

            if model[iter_][C_TYPE] == self.types['boolean']:
                if self.right_true_dict[default] == model[iter_][C_VALUE]:
                    return
                modelpath = self.modelfilter.convert_path_to_child_path(path)
                modelrow = self.model[modelpath]
                option = modelrow[0]
                if len(modelpath) > 1:
                    optnamerow = self.model[modelpath[0]]
                    optname = optnamerow[0]
                    keyrow = self.model[modelpath[:2]]
                    key = keyrow[0]
                    self.remember_option(option + '\n' + key + '\n' + optname,
                        modelrow[C_VALUE], default)
                    gajim.config.set_per(optname, key, option, default)
                else:
                    self.remember_option(option, modelrow[C_VALUE], default)
                    gajim.config.set(option, default)
                modelrow[C_VALUE] = self.right_true_dict[default]
                self.check_for_restart()
            else:
                if str(default) == model[iter_][C_VALUE]:
                    return
                self.on_config_edited(None, path.to_string(), str(default))

    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
                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])]
                if name == 'password':
                    value = _('Hidden')
                else:
                    if type_ == self.types['boolean']:
                        value = self.right_true_dict[option]
                    else:
                        try:
                            value = str(option)
                        except:
                            value = option
                self.model.append(parent, [name, value, type_])

    def visible_func(self, model, treeiter, data):
        search_string  = self.entry.get_text().lower()
        for it in tree_model_pre_order(model, treeiter):
            if model[it][C_TYPE] != '':
                opt_path = self.get_option_path(model, it)
                if len(opt_path) == 3:
                    desc = gajim.config.get_desc_per(opt_path[2], opt_path[1],
                            opt_path[0])
                elif len(opt_path) == 1:
                    desc = gajim.config.get_desc(opt_path[0])
                if search_string in model[it][C_PREFNAME] or (desc and \
                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()