search_window.py 7 KB
Newer Older
Yann Leboulanger's avatar
Yann Leboulanger committed
1
# -*- coding: utf-8 -*-
2
##	search_window.py
Yann Leboulanger's avatar
Yann Leboulanger committed
3
##
Yann Leboulanger's avatar
Yann Leboulanger committed
4
## Copyright (C) 2007 Yann Leboulanger <asterix@lagaule.org>
Yann Leboulanger's avatar
Yann Leboulanger committed
5
##
6 7 8
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
Yann Leboulanger's avatar
Yann Leboulanger committed
9
## it under the terms of the GNU General Public License as published
10
## by the Free Software Foundation; version 3 only.
Yann Leboulanger's avatar
Yann Leboulanger committed
11
##
12
## Gajim is distributed in the hope that it will be useful,
Yann Leboulanger's avatar
Yann Leboulanger committed
13 14 15
## 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.
16 17 18
##
## You should have received a copy of the GNU General Public License
## along with Gajim.  If not, see <http://www.gnu.org/licenses/>.
Yann Leboulanger's avatar
Yann Leboulanger committed
19 20 21 22

import gobject
import gtk

23
from common import gajim, dataforms
Yann Leboulanger's avatar
Yann Leboulanger committed
24 25 26

import gtkgui_helpers
import dialogs
27
import vcard
28
import config
Yann Leboulanger's avatar
Yann Leboulanger committed
29 30 31 32 33 34 35 36 37 38 39 40 41
import dataforms_widget

class SearchWindow:
	def __init__(self, account, jid):
		'''Create new window.'''

		# an account object
		self.account = account
		self.jid = jid

		# retrieving widgets from xml
		self.xml = gtkgui_helpers.get_glade('search_window.glade')
		self.window = self.xml.get_widget('search_window')
42 43
		for name in ('label', 'progressbar', 'search_vbox', 'search_button',
		'add_contact_button', 'information_button'):
Yann Leboulanger's avatar
Yann Leboulanger committed
44 45 46 47 48 49 50 51 52 53
			self.__dict__[name] = self.xml.get_widget(name)

		# displaying the window
		self.xml.signal_autoconnect(self)
		self.window.show_all()
		self.request_form()
		self.pulse_id = gobject.timeout_add(80, self.pulse_callback)

		self.is_form = None

54 55 56
		# Is there a jid column in results ? if -1: no, else column number
		self.jid_column = -1

Yann Leboulanger's avatar
Yann Leboulanger committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	def request_form(self):
		gajim.connections[self.account].request_search_fields(self.jid)
	
	def pulse_callback(self):
		self.progressbar.pulse()
		return True

	def on_search_window_key_press_event(self, widget, event):
		if event.keyval == gtk.keysyms.Escape:
			self.window.destroy()

	def on_search_window_destroy(self, widget):
		if self.pulse_id:
			gobject.source_remove(self.pulse_id)
		del gajim.interface.instances[self.account]['search'][self.jid]

	def on_close_button_clicked(self, button):
		self.window.destroy()

	def on_search_button_clicked(self, button):
		if self.is_form:
			self.data_form_widget.data_form.type = 'submit'
			gajim.connections[self.account].send_search_form(self.jid,
				self.data_form_widget.data_form, True)
81
		else:
82 83 84 85
			infos = self.data_form_widget.get_infos()
			if infos.has_key('instructions'):
				del infos['instructions']
			gajim.connections[self.account].send_search_form(self.jid, infos,
86
				False)
87 88

		self.search_vbox.remove(self.data_form_widget)
89

Yann Leboulanger's avatar
Yann Leboulanger committed
90 91 92 93 94 95
		self.progressbar.show()
		self.label.set_text(_('Waiting for results'))
		self.label.show()
		self.pulse_id = gobject.timeout_add(80, self.pulse_callback)
		self.search_button.hide()

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	def on_add_contact_button_clicked(self, widget):
		(model, iter) = self.result_treeview.get_selection().get_selected()
		if not iter:
			return
		jid = model[iter][self.jid_column]
		dialogs.AddNewContactWindow(self.account, jid)
	
	def on_information_button_clicked(self, widget):
		(model, iter) = self.result_treeview.get_selection().get_selected()
		if not iter:
			return
		jid = model[iter][self.jid_column]
		if gajim.interface.instances[self.account]['infos'].has_key(jid):
			gajim.interface.instances[self.account]['infos'][jid].window.present()
		else:
			contact = gajim.contacts.create_contact(jid = jid, name='', groups=[],
				show='', status='', sub='', ask='', resource='', priority=0,
				keyID='', our_chatstate=None, chatstate=None)
			gajim.interface.instances[self.account]['infos'][jid] = \
				vcard.VcardWindow(contact, self.account)

Yann Leboulanger's avatar
Yann Leboulanger committed
117 118 119 120 121 122
	def on_form_arrived(self, form, is_form):
		if self.pulse_id:
			gobject.source_remove(self.pulse_id)
		self.progressbar.hide()
		self.label.hide()

123 124 125 126 127 128 129 130 131
		if is_form:
			self.is_form = True
			self.data_form_widget = dataforms_widget.DataFormWidget()
			self.dataform = dataforms.ExtendForm(node = form)
			self.data_form_widget.set_sensitive(True)
			try:
				self.data_form_widget.data_form = self.dataform
			except dataforms.Error:
				self.label.set_text(_('Error in received dataform'))
132
				self.label.show()
133 134 135 136 137 138 139
				return
			if self.data_form_widget.title:
				self.window.set_title('%s - Search - Gajim' % \
					self.data_form_widget.title)
		else:
			self.is_form = False
			self.data_form_widget = config.FakeDataForm(form)
Yann Leboulanger's avatar
Yann Leboulanger committed
140

141
		self.data_form_widget.show_all()
142 143
		self.search_vbox.pack_start(self.data_form_widget)

144 145 146 147 148 149 150 151 152 153 154 155 156
	def on_result_treeview_cursor_changed(self, treeview):
		if self.jid_column == -1:
			return
		(model, iter) = treeview.get_selection().get_selected()
		if not iter:
			return
		if model[iter][self.jid_column]:
			self.add_contact_button.set_sensitive(True)
			self.information_button.set_sensitive(True)
		else:
			self.add_contact_button.set_sensitive(False)
			self.information_button.set_sensitive(False)

157 158 159 160 161 162 163 164 165 166 167 168 169 170
	def on_result_arrived(self, form, is_form):
		if self.pulse_id:
			gobject.source_remove(self.pulse_id)
		self.progressbar.hide()
		self.label.hide()

		if not is_form:
			if not form:
				self.label.set_text(_('No result'))
				self.label.show()
				return
			# We suppose all items have the same fields
			sw = gtk.ScrolledWindow()
			sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
171 172 173 174
			self.result_treeview = gtk.TreeView()
			self.result_treeview.connect('cursor-changed',
				self.on_result_treeview_cursor_changed)
			sw.add(self.result_treeview)
175 176 177 178 179 180 181 182 183
			# Create model
			fieldtypes = [str]*len(form[0])
			model = gtk.ListStore(*fieldtypes)
			# Copy data to model
			for item in form:
				model.append(item.values())
			# Create columns
			counter = 0
			for field in form[0].keys():
184
				self.result_treeview.append_column(
185 186
					gtk.TreeViewColumn(field, gtk.CellRendererText(),
					text = counter))
187 188
				if field == 'jid':
					self.jid_column = counter
189
				counter += 1
190
			self.result_treeview.set_model(model)
191 192
			sw.show_all()
			self.search_vbox.pack_start(sw)
193 194 195
			if self.jid_column > -1:
				self.add_contact_button.show()
				self.information_button.show()
196 197 198
			return

		self.dataform = dataforms.ExtendForm(node = form)
199 200 201 202 203
		if len(self.dataform.items) == 0:
			# No result
			self.label.set_text(_('No result'))
			self.label.show()
			return
204 205 206 207 208 209 210 211 212

		self.data_form_widget.set_sensitive(True)
		try:
			self.data_form_widget.data_form = self.dataform
		except dataforms.Error:
			self.label.set_text(_('Error in received dataform'))
			self.label.show()
			return

213 214 215 216 217 218 219 220 221 222 223 224
		self.result_treeview = self.data_form_widget.records_treeview
		selection = self.result_treeview.get_selection()
		selection.set_mode(gtk.SELECTION_SINGLE)
		self.result_treeview.connect('cursor-changed',
			self.on_result_treeview_cursor_changed)

		counter = 0
		for field in self.dataform.items[0].fields:
			if field.var == 'jid':
				self.jid_column = counter
				break
			counter += 1
225 226
		self.search_vbox.pack_start(self.data_form_widget)
		self.data_form_widget.show()
227 228 229
		if self.jid_column > -1:
			self.add_contact_button.show()
			self.information_button.show()
230 231 232 233
		if self.data_form_widget.title:
			self.window.set_title('%s - Search - Gajim' % \
				self.data_form_widget.title)