root/branches/gajim_0.11/src/advanced.py

Revision 7841, 8.5 kB (checked in by asterix, 20 months ago)

merge usefull diff from trunk

  • Property svn:eol-style set to LF
Line 
1##      advanced.py
2##
3## Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
4## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com>
5## Copyright (C) 2005 Vincent Hanquez <tab@snarc.org>
6##
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published
9## by the Free Software Foundation; version 2 only.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14## GNU General Public License for more details.
15##
16
17import gtk
18import gtkgui_helpers
19
20from common import gajim
21
22(
23OPT_TYPE,
24OPT_VAL
25) = range(2)
26
27(
28C_PREFNAME,
29C_VALUE,
30C_TYPE
31) = range(3)
32
33GTKGUI_GLADE = 'manage_accounts_window.glade'
34
35class AdvancedConfigurationWindow(object):
36        def __init__(self):
37                self.xml = gtkgui_helpers.get_glade('advanced_configuration_window.glade')
38                self.window = self.xml.get_widget('advanced_configuration_window')
39                self.window.set_transient_for(
40                        gajim.interface.instances['preferences'].window)
41                self.entry = self.xml.get_widget('advanced_entry')
42                self.desc_label = self.xml.get_widget('advanced_desc_label')
43                self.restart_label = self.xml.get_widget('restart_label')
44
45                # Format:
46                # key = option name (root/subopt/opt separated by \n then)
47                # value = array(oldval, newval)
48                self.changed_opts = {}
49               
50                # For i18n
51                self.right_true_dict = {True: _('Activated'), False: _('Deactivated')}
52                self.types = {
53                        'boolean': _('Boolean'),
54                        'integer': _('Integer'),
55                        'string': _('Text'),
56                        'color': _('Color')}
57
58                treeview = self.xml.get_widget('advanced_treeview')
59                self.model = gtk.TreeStore(str, str, str)
60                self.model.set_sort_column_id(0, gtk.SORT_ASCENDING)
61                self.modelfilter = self.model.filter_new()
62                self.modelfilter.set_visible_func(self.visible_func)
63
64                renderer_text = gtk.CellRendererText()
65                col = treeview.insert_column_with_attributes(-1, _('Preference Name'),
66                        renderer_text, text = 0)
67                col.set_resizable(True)
68
69                renderer_text = gtk.CellRendererText()
70                renderer_text.connect('edited', self.on_config_edited)
71                col = treeview.insert_column_with_attributes(-1, _('Value'),
72                        renderer_text, text = 1)
73                col.set_cell_data_func(renderer_text, self.cb_value_column_data)
74
75                if gtk.gtk_version >= (2, 8, 0) and gtk.pygtk_version >= (2, 8, 0):
76                        col.set_resizable(True) # there is a bug in 2.6.x series
77                col.set_max_width(250)
78
79                renderer_text = gtk.CellRendererText()
80                treeview.insert_column_with_attributes(-1, _('Type'),
81                        renderer_text, text = 2)
82
83                # add data to model
84                gajim.config.foreach(self.fill, self.model)
85
86                treeview.set_model(self.modelfilter)
87
88                # connect signal for selection change
89                treeview.get_selection().connect('changed',
90                        self.on_advanced_treeview_selection_changed)
91
92                self.xml.signal_autoconnect(self)
93                self.window.show_all()
94                self.restart_label.hide()
95                gajim.interface.instances['advanced_config'] = self
96
97        def cb_value_column_data(self, col, cell, model, iter):
98                '''check if it's boolen or holds password stuff and if yes
99                make the cellrenderertext not editable else it's editable'''
100                optname = model[iter][C_PREFNAME]
101                opttype = model[iter][C_TYPE]
102                if opttype == self.types['boolean'] or optname in ('password',
103                        'gpgpassword'):
104                        cell.set_property('editable', False)
105                else:
106                        cell.set_property('editable', True)
107
108        def get_option_path(self, model, iter):
109                # It looks like path made from reversed array
110                # path[0] is the true one optname
111                # path[1] is the key name
112                # path[2] is the root of tree
113                # last two is optional
114                path = [model[iter][0].decode('utf-8')]
115                parent = model.iter_parent(iter)
116                while parent:
117                        path.append(model[parent][0].decode('utf-8'))
118                        parent = model.iter_parent(parent)
119                return path
120
121        def on_advanced_treeview_selection_changed(self, treeselection):
122                model, iter = treeselection.get_selected()
123                # Check for GtkTreeIter
124                if iter:
125                        opt_path = self.get_option_path(model, iter)
126                        # Get text from first column in this row
127                        desc = None
128                        if len(opt_path) == 3:
129                                desc = gajim.config.get_desc_per(opt_path[2], opt_path[1],
130                                        opt_path[0])
131                        elif len(opt_path) == 1:
132                                desc = gajim.config.get_desc(opt_path[0])
133                        if desc:
134                                self.desc_label.set_text(desc)
135                        else:
136                                #we talk about option description in advanced configuration editor
137                                self.desc_label.set_text(_('(None)'))
138
139        def remember_option(self, option, oldval, newval):
140                if self.changed_opts.has_key(option):
141                        self.changed_opts[option] = (self.changed_opts[option][0], newval)
142                else:
143                        self.changed_opts[option] = (oldval, newval)
144
145        def on_advanced_treeview_row_activated(self, treeview, path, column):
146                modelpath = self.modelfilter.convert_path_to_child_path(path)
147                modelrow = self.model[modelpath]
148                option = modelrow[0].decode('utf-8')
149                if modelrow[2] == self.types['boolean']:
150                        for key in self.right_true_dict.keys():
151                                if self.right_true_dict[key] == modelrow[1]:
152                                        modelrow[1] = key
153                        newval = {'False': True, 'True': False}[modelrow[1]]
154                        if len(modelpath) > 1:
155                                optnamerow = self.model[modelpath[0]]
156                                optname = optnamerow[0].decode('utf-8')
157                                keyrow = self.model[modelpath[:2]]
158                                key = keyrow[0].decode('utf-8')
159                                gajim.config.get_desc_per(optname, key, option)
160                                self.remember_option(option + '\n' + key + '\n' + optname,
161                                        modelrow[1], newval)
162                                gajim.config.set_per(optname, key, option, newval)
163                        else:
164                                self.remember_option(option, modelrow[1], newval)
165                                gajim.config.set(option, newval)
166                        gajim.interface.save_config()
167                        modelrow[1] = self.right_true_dict[newval]
168                        self.check_for_restart()
169
170        def check_for_restart(self):
171                self.restart_label.hide()
172                for opt in self.changed_opts:
173                        opt_path = opt.split('\n')
174                        if len(opt_path)==3:
175                                restart = gajim.config.get_restart_per(opt_path[2], opt_path[1],
176                                        opt_path[0])
177                        else:
178                                restart = gajim.config.get_restart(opt_path[0])
179                        if restart:
180                                if self.changed_opts[opt][0] != self.changed_opts[opt][1]:
181                                        self.restart_label.show()
182                                        break
183
184        def on_config_edited(self, cell, path, text):
185                # convert modelfilter path to model path
186                modelpath = self.modelfilter.convert_path_to_child_path(path)
187                modelrow = self.model[modelpath]
188                option = modelrow[0].decode('utf-8')
189                text = text.decode('utf-8')
190                if len(modelpath) > 1:
191                        optnamerow = self.model[modelpath[0]]
192                        optname = optnamerow[0].decode('utf-8')
193                        keyrow = self.model[modelpath[:2]]
194                        key = keyrow[0].decode('utf-8')
195                        self.remember_option(option + '\n' + key + '\n' + optname, modelrow[1],
196                                text)
197                        gajim.config.set_per(optname, key, option, text)
198                else:
199                        self.remember_option(option, modelrow[1], text)
200                        gajim.config.set(option, text)
201                gajim.interface.save_config()
202                modelrow[1] = text
203                self.check_for_restart()
204
205        def on_advanced_configuration_window_destroy(self, widget):
206                del gajim.interface.instances['advanced_config']
207
208        def on_advanced_close_button_clicked(self, widget):
209                self.window.destroy()
210
211        def find_iter(self, model, parent_iter, name):
212                if not parent_iter:
213                        iter = model.get_iter_root()
214                else:
215                        iter = model.iter_children(parent_iter)
216                while iter:
217                        if model[iter][C_PREFNAME].decode('utf-8') == name:
218                                break
219                        iter = model.iter_next(iter)
220                return iter
221
222        def fill(self, model, name, parents, val):
223                iter = None
224                if parents:
225                        for p in parents:
226                                iter2 = self.find_iter(model, iter, p)
227                                if iter2:
228                                        iter = iter2
229
230                if not val:
231                        model.append(iter, [name, '', ''])
232                        return
233                type = ''
234                if val[OPT_TYPE]:
235                        type = val[OPT_TYPE][0]
236                        type = self.types[type] # i18n
237                value = val[OPT_VAL]
238                if name in ('password', 'gpgpassword'):
239                        #we talk about password
240                        value = _('Hidden') # override passwords with this string
241                if value in self.right_true_dict:
242                        value = self.right_true_dict[value]
243                model.append(iter, [name, value, type])
244
245        def visible_func(self, model, iter):
246                str = self.entry.get_text().decode('utf-8')
247                if str in (None, ''):
248                        return True # show all
249                name = model[iter][C_PREFNAME].decode('utf-8')
250                # If a child of the iter matches, we return True
251                if model.iter_has_child(iter):
252                        iterC = model.iter_children(iter)
253                        while iterC:
254                                nameC = model[iterC][C_PREFNAME].decode('utf-8')
255                                if model.iter_has_child(iterC):
256                                        iterCC = model.iter_children(iterC)
257                                        while iterCC:
258                                                nameCC = model[iterCC][C_PREFNAME].decode('utf-8')
259                                                if nameCC.find(str) != -1:
260                                                        return True
261                                                iterCC = model.iter_next(iterCC)
262                                elif nameC.find(str) != -1:
263                                        return True
264                                iterC = model.iter_next(iterC)
265                elif name.find(str) != -1:
266                        return True
267                return False
268
269        def on_advanced_entry_changed(self, widget):
270                self.modelfilter.refilter()
Note: See TracBrowser for help on using the browser.