coding utf-8 dialogs py Copyright 2003-2006 Yann Le Boulanger asterix

  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
# -*- coding: utf-8 -*-
## dialogs.py
##
## Copyright (C) 2003-2006 Yann Le Boulanger <asterix@lagaule.org>
## Copyright (C) 2003-2004 Vincent Hanquez <tab@snarc.org>
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com>
## Copyright (C) 2005 Dimitur Kirov <dkirov@gmail.com>
## Copyright (C) 2005-2006 Travis Shirk <travis@pobox.com>
## Copyright (C) 2005 Norman Rasmussen <norman@rasmussen.co.za>
##
## This program 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 2 only.
##
## This program 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.
##
import gtk
import gobject
import os
import gtkgui_helpers
import vcard
import conversation_textview
import message_control
try:
import gtkspell
HAS_GTK_SPELL = True
except:
HAS_GTK_SPELL = False
# those imports are not used in this file, but in files that 'import dialogs'
# so they can do dialog.GajimThemesWindow() for example
from filetransfers_window import FileTransfersWindow
from gajim_themes_window import GajimThemesWindow
from advanced import AdvancedConfigurationWindow
from common import gajim
from common import helpers
from common.exceptions import GajimGeneralException
class EditGroupsDialog:
'''Class for the edit group dialog window'''
def __init__(self, list_):
'''list_ is a list of (contact, account) tuples'''
self.xml = gtkgui_helpers.get_glade('edit_groups_dialog.glade')
self.dialog = self.xml.get_widget('edit_groups_dialog')
self.dialog.set_transient_for(gajim.interface.roster.window)
self.list_ = list_
self.changes_made = False
self.list = self.xml.get_widget('groups_treeview')
if len(list_) == 1:
contact = list_[0][0]
self.xml.get_widget('nickname_label').set_markup(
_("Contact name: <i>%s</i>") % contact.get_shown_name())
self.xml.get_widget('jid_label').set_markup(
_('Jabber ID: <i>%s</i>') % contact.jid)
else:
self.xml.get_widget('nickname_label').set_no_show_all(True)
self.xml.get_widget('nickname_label').hide()
self.xml.get_widget('jid_label').set_no_show_all(True)
self.xml.get_widget('jid_label').hide()
self.xml.signal_autoconnect(self)
self.init_list()
def run(self):
self.dialog.show_all()
if self.changes_made:
for (contact, account) in self.list_:
gajim.connections[account].update_contact(contact.jid, contact.name,
contact.groups)
def on_edit_groups_dialog_response(self, widget, response_id):
if response_id == gtk.RESPONSE_CLOSE:
self.dialog.destroy()
def update_contact(self):
for (contact, account) in self.list_:
tag = gajim.contacts.get_metacontacts_tag(account, contact.jid)
if not tag:
gajim.interface.roster.remove_contact(contact, account)
gajim.interface.roster.add_contact_to_roster(contact.jid, account)
gajim.connections[account].update_contact(contact.jid, contact.name,
contact.groups)
continue
all_jid = gajim.contacts.get_metacontacts_jids(tag)
for _account in all_jid:
if not gajim.interface.roster.regroup and _account != account:
continue
for _jid in all_jid[_account]:
c = gajim.contacts.get_first_contact_from_jid(_account, _jid)
if not c:
continue
gajim.interface.roster.remove_contact(c, _account)
gajim.interface.roster.add_contact_to_roster(_jid, _account)
gajim.connections[_account].update_contact(_jid, c.name,
c.groups)
def remove_group(self, group):
'''remove group group from all contacts and all their brothers'''
for (contact, account) in self.list_:
tag = gajim.contacts.get_metacontacts_tag(account, contact.jid)
if not tag:
if group in contact.groups:
contact.groups.remove(group)
continue
all_jid = gajim.contacts.get_metacontacts_jids(tag)
for _account in all_jid:
if not gajim.interface.roster.regroup and _account != account:
continue
for _jid in all_jid[_account]:
contacts = gajim.contacts.get_contact(_account, _jid)
for c in contacts:
if group in c.groups:
c.groups.remove(group)
def add_group(self, group):
'''add group group to all contacts and all their brothers'''
for (contact, account) in self.list_:
tag = gajim.contacts.get_metacontacts_tag(account, contact.jid)
if not tag:
if group not in contact.groups:
contact.groups.append(group)
continue
all_jid = gajim.contacts.get_metacontacts_jids(tag)
for _account in all_jid:
if not gajim.interface.roster.regroup and _account != account:
continue
for _jid in all_jid[_account]:
contacts = gajim.contacts.get_contact(_account, _jid)
for c in contacts:
if not group in c.groups:
c.groups.append(group)
def on_add_button_clicked(self, widget):
group = self.xml.get_widget('group_entry').get_text().decode('utf-8')
if not group:
return
# Do not allow special groups
if group in helpers.special_groups:
return
# check if it already exists
model = self.list.get_model()
iter = model.get_iter_root()
while iter:
if model.get_value(iter, 0).decode('utf-8') == group:
return
iter = model.iter_next(iter)
self.changes_made = True
model.append((group, True, False))
self.add_group(group)
self.update_contact()
self.init_list() # Re-draw list to sort new item
def group_toggled_cb(self, cell, path):
self.changes_made = True
model = self.list.get_model()
if model[path][2]:
model[path][2] = False
model[path][1] = True
else:
model[path][1] = not model[path][1]
group = model[path][0].decode('utf-8')
if model[path][1]:
self.add_group(group)
else:
self.remove_group(group)
self.update_contact()
def init_list(self):
store = gtk.ListStore(str, bool, bool)
self.list.set_model(store)
for column in self.list.get_columns(): # Clear treeview when re-drawing
self.list.remove_column(column)
accounts = []
# Store groups in a list so we can sort them and the number of contacts in
# it
groups = {}
for (contact, account) in self.list_:
if account not in accounts:
accounts.append(account)
for g in gajim.groups[account].keys():
if g in groups:
continue
groups[g] = 0
for g in contact.groups:
groups[g] += 1
group_list = []
# Remove special groups if they are empty
for group in groups:
if group not in helpers.special_groups or groups[group] > 0:
group_list.append(group)
group_list.sort()
for group in group_list:
iter = store.append()
store.set(iter, 0, group) # Group name
if groups[group] == 0:
store.set(iter, 1, False)
else:
store.set(iter, 1, True)
if groups[group] == len(self.list_):
# all contacts are in this group
store.set(iter, 2, False)
else:
store.set(iter, 2, True)
column = gtk.TreeViewColumn(_('Group'))
column.set_expand(True)
self.list.append_column(column)
renderer = gtk.CellRendererText()
column.pack_start(renderer)
column.set_attributes(renderer, text = 0)
column = gtk.TreeViewColumn(_('In the group'))
column.set_expand(False)
self.list.append_column(column)
renderer = gtk.CellRendererToggle()
column.pack_start(renderer)
renderer.set_property('activatable', True)
renderer.connect('toggled', self.group_toggled_cb)
column.set_attributes(renderer, active = 1, inconsistent = 2)
class PassphraseDialog:
'''Class for Passphrase dialog'''
def run(self):
'''Wait for OK button to be pressed and return passphrase/password'''
rep = self.window.run()
if rep == gtk.RESPONSE_OK:
passphrase = self.passphrase_entry.get_text().decode('utf-8')
else:
passphrase = -1
save_passphrase_checkbutton = self.xml.\
get_widget('save_passphrase_checkbutton')
self.window.destroy()
return passphrase, save_passphrase_checkbutton.get_active()
def __init__(self, titletext, labeltext, checkbuttontext):
self.xml = gtkgui_helpers.get_glade('passphrase_dialog.glade')
self.window = self.xml.get_widget('passphrase_dialog')
self.passphrase_entry = self.xml.get_widget('passphrase_entry')
self.passphrase = -1
self.window.set_title(titletext)
self.xml.get_widget('message_label').set_text(labeltext)
self.xml.get_widget('save_passphrase_checkbutton').set_label(
checkbuttontext)
self.xml.signal_autoconnect(self)
self.window.show_all()
class ChooseGPGKeyDialog:
'''Class for GPG key dialog'''
def __init__(self, title_text, prompt_text, secret_keys, selected = None):
#list : {keyID: userName, ...}
xml = gtkgui_helpers.get_glade('choose_gpg_key_dialog.glade')
self.window = xml.get_widget('choose_gpg_key_dialog')
self.window.set_title(title_text)
self.keys_treeview = xml.get_widget('keys_treeview')
prompt_label = xml.get_widget('prompt_label')
prompt_label.set_text(prompt_text)
model = gtk.ListStore(str, str)
model.set_sort_func(1, self.sort_keys)
model.set_sort_column_id(1, gtk.SORT_ASCENDING)
self.keys_treeview.set_model(model)
#columns
renderer = gtk.CellRendererText()
self.keys_treeview.insert_column_with_attributes(-1, _('KeyID'),
renderer, text = 0)
renderer = gtk.CellRendererText()
self.keys_treeview.insert_column_with_attributes(-1, _('Contact name'),
renderer, text = 1)
self.keys_treeview.set_search_column(1)
self.fill_tree(secret_keys, selected)
self.window.show_all()
def sort_keys(self, model, iter1, iter2):
value1 = model[iter1][1]
value2 = model[iter2][1]
if value1 == _('None'):
return -1
elif value2 == _('None'):
return 1
elif value1 < value2:
return -1
return 1
def run(self):
rep = self.window.run()
if rep == gtk.RESPONSE_OK:
selection = self.keys_treeview.get_selection()
(model, iter) = selection.get_selected()
keyID = [ model[iter][0].decode('utf-8'),
model[iter][1].decode('utf-8') ]
else:
keyID = None
self.window.destroy()
return keyID
def fill_tree(self, list, selected):
model = self.keys_treeview.get_model()
for keyID in list.keys():
iter = model.append((keyID, list[keyID]))
if keyID == selected:
path = model.get_path(iter)
self.keys_treeview.set_cursor(path)