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