# -*- 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)