| 1 | ## gajim_themes_window.py |
|---|
| 2 | ## |
|---|
| 3 | ## Contributors for this file: |
|---|
| 4 | ## - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 5 | ## - Nikos Kouremenos <kourem@gmail.com> |
|---|
| 6 | ## - Dimitur Kirov <dkirov@gmail.com> |
|---|
| 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 pango |
|---|
| 30 | import dialogs |
|---|
| 31 | import gtkgui_helpers |
|---|
| 32 | |
|---|
| 33 | from common import gajim |
|---|
| 34 | from common import i18n |
|---|
| 35 | _ = i18n._ |
|---|
| 36 | APP = i18n.APP |
|---|
| 37 | gtk.glade.bindtextdomain (APP, i18n.DIR) |
|---|
| 38 | gtk.glade.textdomain (APP) |
|---|
| 39 | |
|---|
| 40 | class GajimThemesWindow: |
|---|
| 41 | |
|---|
| 42 | def __init__(self): |
|---|
| 43 | self.xml = gtkgui_helpers.get_glade('gajim_themes_window.glade') |
|---|
| 44 | self.window = self.xml.get_widget('gajim_themes_window') |
|---|
| 45 | |
|---|
| 46 | self.options = ['account', 'group', 'contact', 'banner'] |
|---|
| 47 | self.options_combobox = self.xml.get_widget('options_combobox') |
|---|
| 48 | self.textcolor_checkbutton = self.xml.get_widget('textcolor_checkbutton') |
|---|
| 49 | self.background_checkbutton = self.xml.get_widget('background_checkbutton') |
|---|
| 50 | self.textfont_checkbutton = self.xml.get_widget('textfont_checkbutton') |
|---|
| 51 | self.text_colorbutton = self.xml.get_widget('text_colorbutton') |
|---|
| 52 | self.background_colorbutton = self.xml.get_widget('background_colorbutton') |
|---|
| 53 | self.text_fontbutton = self.xml.get_widget('text_fontbutton') |
|---|
| 54 | self.bold_togglebutton = self.xml.get_widget('bold_togglebutton') |
|---|
| 55 | self.italic_togglebutton = self.xml.get_widget('italic_togglebutton') |
|---|
| 56 | self.themes_tree = self.xml.get_widget('themes_treeview') |
|---|
| 57 | self.theme_options_vbox = self.xml.get_widget('theme_options_vbox') |
|---|
| 58 | self.colorbuttons = {} |
|---|
| 59 | for chatstate in ('active', 'inactive', 'composing', 'paused', 'gone', |
|---|
| 60 | 'muc_msg', 'muc_directed_msg'): |
|---|
| 61 | self.colorbuttons[chatstate] = self.xml.get_widget(chatstate + \ |
|---|
| 62 | '_colorbutton') |
|---|
| 63 | model = gtk.ListStore(str) |
|---|
| 64 | self.themes_tree.set_model(model) |
|---|
| 65 | col = gtk.TreeViewColumn(_('Theme')) |
|---|
| 66 | self.themes_tree.append_column(col) |
|---|
| 67 | renderer = gtk.CellRendererText() |
|---|
| 68 | col.pack_start(renderer, True) |
|---|
| 69 | col.set_attributes(renderer, text = 0) |
|---|
| 70 | renderer.connect('edited', self.on_theme_cell_edited) |
|---|
| 71 | renderer.set_property('editable', True) |
|---|
| 72 | self.current_theme = gajim.config.get('roster_theme') |
|---|
| 73 | self.no_update = False |
|---|
| 74 | self.fill_themes_treeview() |
|---|
| 75 | self.current_option = self.options[0] |
|---|
| 76 | self.set_theme_options(self.current_theme, self.current_option) |
|---|
| 77 | |
|---|
| 78 | self.xml.signal_autoconnect(self) |
|---|
| 79 | self.window.connect('delete-event', self.on_themese_window_delete_event) |
|---|
| 80 | self.themes_tree.get_selection().connect('changed', |
|---|
| 81 | self.selection_changed) |
|---|
| 82 | self.window.show_all() |
|---|
| 83 | |
|---|
| 84 | def on_themese_window_delete_event(self, widget, event): |
|---|
| 85 | self.window.hide() |
|---|
| 86 | return True # do NOT destroy the window |
|---|
| 87 | |
|---|
| 88 | def on_close_button_clicked(self, widget): |
|---|
| 89 | self.window.hide() |
|---|
| 90 | |
|---|
| 91 | def on_theme_cell_edited(self, cell, row, new_name): |
|---|
| 92 | model = self.themes_tree.get_model() |
|---|
| 93 | iter = model.get_iter_from_string(row) |
|---|
| 94 | old_name = model.get_value(iter, 0).decode('utf-8') |
|---|
| 95 | new_name = new_name.decode('utf-8') |
|---|
| 96 | if old_name == new_name: |
|---|
| 97 | return |
|---|
| 98 | new_config_name = new_name.replace(' ', '_') |
|---|
| 99 | if new_config_name in gajim.config.get_per('themes'): |
|---|
| 100 | return |
|---|
| 101 | gajim.config.add_per('themes', new_config_name) |
|---|
| 102 | # Copy old theme values |
|---|
| 103 | old_config_name = old_name.replace(' ', '_') |
|---|
| 104 | properties = ['textcolor', 'bgcolor', 'font', 'fontattrs'] |
|---|
| 105 | gajim.config.add_per('themes', new_config_name) |
|---|
| 106 | for option in self.options: |
|---|
| 107 | for property in properties: |
|---|
| 108 | option_name = option + property |
|---|
| 109 | gajim.config.set_per('themes', new_config_name, option_name, |
|---|
| 110 | gajim.config.get_per('themes', old_config_name, option_name)) |
|---|
| 111 | gajim.config.del_per('themes', old_config_name) |
|---|
| 112 | if old_config_name == gajim.config.get('roster_theme'): |
|---|
| 113 | gajim.config.set('roster_theme', new_config_name) |
|---|
| 114 | model.set_value(iter, 0, new_name) |
|---|
| 115 | self.current_theme = new_name |
|---|
| 116 | |
|---|
| 117 | def fill_themes_treeview(self): |
|---|
| 118 | self.xml.get_widget('remove_button').set_sensitive(False) |
|---|
| 119 | self.theme_options_vbox.set_sensitive(False) |
|---|
| 120 | model = self.themes_tree.get_model() |
|---|
| 121 | model.clear() |
|---|
| 122 | for config_theme in gajim.config.get_per('themes'): |
|---|
| 123 | theme = config_theme.replace('_', ' ') |
|---|
| 124 | iter = model.append([theme]) |
|---|
| 125 | if gajim.config.get('roster_theme') == config_theme: |
|---|
| 126 | self.themes_tree.get_selection().select_iter(iter) |
|---|
| 127 | self.xml.get_widget('remove_button').set_sensitive(True) |
|---|
| 128 | self.theme_options_vbox.set_sensitive(True) |
|---|
| 129 | |
|---|
| 130 | def selection_changed(self, widget = None): |
|---|
| 131 | (model, iter) = self.themes_tree.get_selection().get_selected() |
|---|
| 132 | selected = self.themes_tree.get_selection().get_selected_rows() |
|---|
| 133 | if not iter or selected[1] == []: |
|---|
| 134 | self.theme_options_vbox.set_sensitive(False) |
|---|
| 135 | return |
|---|
| 136 | self.xml.get_widget('remove_button').set_sensitive(True) |
|---|
| 137 | self.theme_options_vbox.set_sensitive(True) |
|---|
| 138 | self.current_theme = model.get_value(iter, 0).decode('utf-8') |
|---|
| 139 | self.current_theme = self.current_theme.replace(' ', '_') |
|---|
| 140 | self.set_theme_options(self.current_theme) |
|---|
| 141 | |
|---|
| 142 | def on_add_button_clicked(self, widget): |
|---|
| 143 | model = self.themes_tree.get_model() |
|---|
| 144 | iter = model.append() |
|---|
| 145 | i = 0 |
|---|
| 146 | # don't confuse translators |
|---|
| 147 | theme_name = _('theme name') |
|---|
| 148 | theme_name_ns = theme_name.replace(' ', '_') |
|---|
| 149 | while theme_name_ns + unicode(i) in gajim.config.get_per('themes'): |
|---|
| 150 | i += 1 |
|---|
| 151 | model.set_value(iter, 0, theme_name + unicode(i)) |
|---|
| 152 | gajim.config.add_per('themes', theme_name_ns + unicode(i)) |
|---|
| 153 | self.themes_tree.get_selection().select_iter(iter) |
|---|
| 154 | col = self.themes_tree.get_column(0) |
|---|
| 155 | path = model.get_path(iter) |
|---|
| 156 | self.themes_tree.set_cursor(path, col, True) |
|---|
| 157 | |
|---|
| 158 | def on_remove_button_clicked(self, widget): |
|---|
| 159 | (model, iter) = self.themes_tree.get_selection().get_selected() |
|---|
| 160 | if not iter: |
|---|
| 161 | return |
|---|
| 162 | if self.current_theme == gajim.config.get('roster_theme'): |
|---|
| 163 | dialogs.ErrorDialog( |
|---|
| 164 | _('You cannot delete your current theme'), |
|---|
| 165 | _('Please first choose another for your current theme.')) |
|---|
| 166 | return |
|---|
| 167 | self.theme_options_vbox.set_sensitive(False) |
|---|
| 168 | gajim.config.del_per('themes', self.current_theme) |
|---|
| 169 | model.remove(iter) |
|---|
| 170 | |
|---|
| 171 | def set_theme_options(self, theme, option = 'account'): |
|---|
| 172 | self.no_update = True |
|---|
| 173 | self.options_combobox.set_active(self.options.index(option)) |
|---|
| 174 | textcolor = gajim.config.get_per('themes', theme, option + 'textcolor') |
|---|
| 175 | if textcolor: |
|---|
| 176 | state = True |
|---|
| 177 | self.text_colorbutton.set_color(gtk.gdk.color_parse(textcolor)) |
|---|
| 178 | else: |
|---|
| 179 | state = False |
|---|
| 180 | self.textcolor_checkbutton.set_active(state) |
|---|
| 181 | self.text_colorbutton.set_sensitive(state) |
|---|
| 182 | bgcolor = gajim.config.get_per('themes', theme, option + 'bgcolor') |
|---|
| 183 | if bgcolor: |
|---|
| 184 | state = True |
|---|
| 185 | self.background_colorbutton.set_color(gtk.gdk.color_parse( |
|---|
| 186 | bgcolor)) |
|---|
| 187 | else: |
|---|
| 188 | state = False |
|---|
| 189 | self.background_checkbutton.set_active(state) |
|---|
| 190 | self.background_colorbutton.set_sensitive(state) |
|---|
| 191 | |
|---|
| 192 | # get the font name before we set widgets and it will not be overriden |
|---|
| 193 | font_name = gajim.config.get_per('themes', theme, option + 'font') |
|---|
| 194 | font_attrs = gajim.config.get_per('themes', theme, option + 'fontattrs') |
|---|
| 195 | self._set_font_widgets(font_attrs) |
|---|
| 196 | if font_name: |
|---|
| 197 | state = True |
|---|
| 198 | self.text_fontbutton.set_font_name(font_name) |
|---|
| 199 | else: |
|---|
| 200 | state = False |
|---|
| 201 | self.textfont_checkbutton.set_active(state) |
|---|
| 202 | self.text_fontbutton.set_sensitive(state) |
|---|
| 203 | self.no_update = False |
|---|
| 204 | gajim.interface.roster.change_roster_style(None) |
|---|
| 205 | |
|---|
| 206 | for chatstate in ('active', 'inactive', 'composing', 'paused', 'gone', |
|---|
| 207 | 'muc_msg', 'muc_directed_msg'): |
|---|
| 208 | color = gajim.config.get_per('themes', theme, 'state_' + chatstate + \ |
|---|
| 209 | '_color') |
|---|
| 210 | self.colorbuttons[chatstate].set_color(gtk.gdk.color_parse(color)) |
|---|
| 211 | |
|---|
| 212 | def on_textcolor_checkbutton_toggled(self, widget): |
|---|
| 213 | state = widget.get_active() |
|---|
| 214 | self.text_colorbutton.set_sensitive(state) |
|---|
| 215 | self._set_color(state, self.text_colorbutton, |
|---|
| 216 | 'textcolor') |
|---|
| 217 | |
|---|
| 218 | def on_background_checkbutton_toggled(self, widget): |
|---|
| 219 | state = widget.get_active() |
|---|
| 220 | self.background_colorbutton.set_sensitive(state) |
|---|
| 221 | self._set_color(state, self.background_colorbutton, |
|---|
| 222 | 'bgcolor') |
|---|
| 223 | |
|---|
| 224 | def on_textfont_checkbutton_toggled(self, widget): |
|---|
| 225 | self.text_fontbutton.set_sensitive(widget.get_active()) |
|---|
| 226 | self._set_font() |
|---|
| 227 | |
|---|
| 228 | def on_text_colorbutton_color_set(self, widget): |
|---|
| 229 | self._set_color(True, widget, 'textcolor') |
|---|
| 230 | |
|---|
| 231 | def on_background_colorbutton_color_set(self, widget): |
|---|
| 232 | self._set_color(True, widget, 'bgcolor') |
|---|
| 233 | |
|---|
| 234 | def on_text_fontbutton_font_set(self, widget): |
|---|
| 235 | self._set_font() |
|---|
| 236 | |
|---|
| 237 | def on_options_combobox_changed(self, widget): |
|---|
| 238 | index = self.options_combobox.get_active() |
|---|
| 239 | if index == -1: |
|---|
| 240 | return |
|---|
| 241 | self.current_option = self.options[index] |
|---|
| 242 | self.set_theme_options(self.current_theme, |
|---|
| 243 | self.current_option) |
|---|
| 244 | |
|---|
| 245 | def on_bold_togglebutton_toggled(self, widget): |
|---|
| 246 | if not self.no_update: |
|---|
| 247 | self._set_font() |
|---|
| 248 | |
|---|
| 249 | def on_italic_togglebutton_toggled(self, widget): |
|---|
| 250 | if not self.no_update: |
|---|
| 251 | self._set_font() |
|---|
| 252 | |
|---|
| 253 | def _set_color(self, state, widget, option): |
|---|
| 254 | ''' set color value in prefs and update the UI ''' |
|---|
| 255 | if state: |
|---|
| 256 | color = widget.get_color() |
|---|
| 257 | color_string = gtkgui_helpers.make_color_string(color) |
|---|
| 258 | else: |
|---|
| 259 | color_string = '' |
|---|
| 260 | begin_option = '' |
|---|
| 261 | if not option.startswith('state'): |
|---|
| 262 | begin_option = self.current_option |
|---|
| 263 | gajim.config.set_per('themes', self.current_theme, |
|---|
| 264 | begin_option + option, color_string) |
|---|
| 265 | # use faster functions for this |
|---|
| 266 | if self.current_option == 'banner': |
|---|
| 267 | gajim.interface.roster.repaint_themed_widgets() |
|---|
| 268 | gajim.interface.save_config() |
|---|
| 269 | return |
|---|
| 270 | if self.no_update: |
|---|
| 271 | return |
|---|
| 272 | gajim.interface.roster.change_roster_style(self.current_option) |
|---|
| 273 | gajim.interface.save_config() |
|---|
| 274 | |
|---|
| 275 | def _set_font(self): |
|---|
| 276 | ''' set font value in prefs and update the UI ''' |
|---|
| 277 | state = self.textfont_checkbutton.get_active() |
|---|
| 278 | if state: |
|---|
| 279 | font_string = self.text_fontbutton.get_font_name() |
|---|
| 280 | else: |
|---|
| 281 | font_string = '' |
|---|
| 282 | gajim.config.set_per('themes', self.current_theme, |
|---|
| 283 | self.current_option + 'font', font_string) |
|---|
| 284 | font_attrs = self._get_font_attrs() |
|---|
| 285 | gajim.config.set_per('themes', self.current_theme, |
|---|
| 286 | self.current_option + 'fontattrs', font_attrs) |
|---|
| 287 | # use faster functions for this |
|---|
| 288 | if self.current_option == 'banner': |
|---|
| 289 | gajim.interface.roster.repaint_themed_widgets() |
|---|
| 290 | if self.no_update: |
|---|
| 291 | return |
|---|
| 292 | gajim.interface.roster.change_roster_style(self.current_option) |
|---|
| 293 | gajim.interface.save_config() |
|---|
| 294 | |
|---|
| 295 | def _toggle_font_widgets(self, font_props): |
|---|
| 296 | ''' toggle font buttons with the bool values of font_props tuple''' |
|---|
| 297 | self.bold_togglebutton.set_active(font_props[0]) |
|---|
| 298 | self.italic_togglebutton.set_active(font_props[1]) |
|---|
| 299 | |
|---|
| 300 | def _get_font_description(self): |
|---|
| 301 | ''' return a FontDescription from togglebuttons |
|---|
| 302 | states''' |
|---|
| 303 | fd = pango.FontDescription() |
|---|
| 304 | if self.bold_togglebutton.get_active(): |
|---|
| 305 | fd.set_weight(pango.WEIGHT_BOLD) |
|---|
| 306 | if self.italic_togglebutton.get_active(): |
|---|
| 307 | fd.set_style(pango.STYLE_ITALIC) |
|---|
| 308 | return fd |
|---|
| 309 | |
|---|
| 310 | def _set_font_widgets(self, font_attrs): |
|---|
| 311 | ''' set the correct toggle state of font style buttons by |
|---|
| 312 | a font string of type 'BI' ''' |
|---|
| 313 | font_props = [False, False, False] |
|---|
| 314 | if font_attrs: |
|---|
| 315 | if font_attrs.find('B') != -1: |
|---|
| 316 | font_props[0] = True |
|---|
| 317 | if font_attrs.find('I') != -1: |
|---|
| 318 | font_props[1] = True |
|---|
| 319 | self._toggle_font_widgets(font_props) |
|---|
| 320 | |
|---|
| 321 | def _get_font_attrs(self): |
|---|
| 322 | ''' get a string with letters of font attribures: 'BI' ''' |
|---|
| 323 | attrs = '' |
|---|
| 324 | if self.bold_togglebutton.get_active(): |
|---|
| 325 | attrs += 'B' |
|---|
| 326 | if self.italic_togglebutton.get_active(): |
|---|
| 327 | attrs += 'I' |
|---|
| 328 | return attrs |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | def _get_font_props(self, font_name): |
|---|
| 332 | ''' get tuple of font properties: Weight, Style ''' |
|---|
| 333 | font_props = [False, False, False] |
|---|
| 334 | font_description = pango.FontDescription(font_name) |
|---|
| 335 | if font_description.get_weight() != pango.WEIGHT_NORMAL: |
|---|
| 336 | font_props[0] = True |
|---|
| 337 | if font_description.get_style() != pango.STYLE_ITALIC: |
|---|
| 338 | font_props[1] = True |
|---|
| 339 | return font_props |
|---|
| 340 | |
|---|
| 341 | def on_active_colorbutton_color_set(self, widget): |
|---|
| 342 | self.no_update = True |
|---|
| 343 | self._set_color(True, widget, 'state_active_color') |
|---|
| 344 | self.no_update = False |
|---|
| 345 | |
|---|
| 346 | def on_inactive_colorbutton_color_set(self, widget): |
|---|
| 347 | self.no_update = True |
|---|
| 348 | self._set_color(True, widget, 'state_inactive_color') |
|---|
| 349 | self.no_update = False |
|---|
| 350 | |
|---|
| 351 | def on_composing_colorbutton_color_set(self, widget): |
|---|
| 352 | self.no_update = True |
|---|
| 353 | self._set_color(True, widget, 'state_composing_color') |
|---|
| 354 | self.no_update = False |
|---|
| 355 | |
|---|
| 356 | def on_paused_colorbutton_color_set(self, widget): |
|---|
| 357 | self.no_update = True |
|---|
| 358 | self._set_color(True, widget, 'state_paused_color') |
|---|
| 359 | self.no_update = False |
|---|
| 360 | |
|---|
| 361 | def on_gone_colorbutton_color_set(self, widget): |
|---|
| 362 | self.no_update = True |
|---|
| 363 | self._set_color(True, widget, 'state_gone_color') |
|---|
| 364 | self.no_update = False |
|---|
| 365 | |
|---|
| 366 | def on_muc_msg_colorbutton_color_set(self, widget): |
|---|
| 367 | self.no_update = True |
|---|
| 368 | self._set_color(True, widget, 'state_muc_msg_color') |
|---|
| 369 | self.no_update = False |
|---|
| 370 | |
|---|
| 371 | def on_muc_directed_msg_colorbutton_color_set(self, widget): |
|---|
| 372 | self.no_update = True |
|---|
| 373 | self._set_color(True, widget, 'state_muc_directed_msg_color') |
|---|
| 374 | self.no_update = False |
|---|