2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2007 Stephan Erb <steve-e AT h3c.de>
|
|
|
|
# Copyright (C) 2007-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim 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 3 only.
|
|
|
|
#
|
|
|
|
# Gajim 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-04-19 16:26:21 +02:00
|
|
|
|
2013-07-28 20:50:30 +02:00
|
|
|
from gi.repository import GLib
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Gtk
|
2015-01-19 13:39:11 +01:00
|
|
|
from gi.repository import Gdk
|
2007-04-19 16:26:21 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2018-07-21 14:35:28 +02:00
|
|
|
from gajim.common.modules import dataforms
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import ged
|
2007-04-19 16:26:21 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim import gtkgui_helpers
|
|
|
|
from gajim import vcard
|
|
|
|
from gajim import config
|
|
|
|
from gajim import dataforms_widget
|
2018-09-26 19:06:47 +02:00
|
|
|
from gajim.gtk.add_contact import AddNewContactWindow
|
2018-07-16 23:22:33 +02:00
|
|
|
|
2007-04-19 16:26:21 +02:00
|
|
|
|
|
|
|
class SearchWindow:
|
2010-02-08 15:08:40 +01:00
|
|
|
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_gtk_builder('search_window.ui')
|
|
|
|
self.window = self.xml.get_object('search_window')
|
|
|
|
for name in ('label', 'progressbar', 'search_vbox', 'search_button',
|
|
|
|
'add_contact_button', 'information_button'):
|
|
|
|
self.__dict__[name] = self.xml.get_object(name)
|
|
|
|
|
2014-01-26 08:59:24 +01:00
|
|
|
self.search_button.set_sensitive(False)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# displaying the window
|
|
|
|
self.xml.connect_signals(self)
|
|
|
|
self.window.show_all()
|
|
|
|
self.request_form()
|
2013-07-28 20:50:30 +02:00
|
|
|
self.pulse_id = GLib.timeout_add(80, self.pulse_callback)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.is_form = None
|
|
|
|
|
|
|
|
# Is there a jid column in results ? if -1: no, else column number
|
|
|
|
self.jid_column = -1
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('search-form-received', ged.GUI1,
|
2010-08-30 21:42:36 +02:00
|
|
|
self._nec_search_form_received)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('search-result-received', ged.GUI1,
|
2010-08-30 21:42:36 +02:00
|
|
|
self._nec_search_result_received)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def request_form(self):
|
2018-06-25 19:34:10 +02:00
|
|
|
con = app.connections[self.account]
|
|
|
|
con.get_module('Search').request_search_fields(self.jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def pulse_callback(self):
|
|
|
|
self.progressbar.pulse()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def on_search_window_key_press_event(self, widget, event):
|
2012-12-23 16:23:43 +01:00
|
|
|
if event.keyval == Gdk.KEY_Escape:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.window.destroy()
|
|
|
|
|
|
|
|
def on_search_window_destroy(self, widget):
|
|
|
|
if self.pulse_id:
|
2013-07-28 20:50:30 +02:00
|
|
|
GLib.source_remove(self.pulse_id)
|
2017-08-13 13:18:56 +02:00
|
|
|
del app.interface.instances[self.account]['search'][self.jid]
|
|
|
|
app.ged.remove_event_handler('search-form-received', ged.GUI1,
|
2010-08-30 21:42:36 +02:00
|
|
|
self._nec_search_form_received)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('search-result-received', ged.GUI1,
|
2010-08-30 21:42:36 +02:00
|
|
|
self._nec_search_result_received)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def on_close_button_clicked(self, button):
|
|
|
|
self.window.destroy()
|
|
|
|
|
|
|
|
def on_search_button_clicked(self, button):
|
2018-06-25 19:34:10 +02:00
|
|
|
con = app.connections[self.account]
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.is_form:
|
2012-08-22 12:55:57 +02:00
|
|
|
self.data_form_widget.data_form.type_ = 'submit'
|
2018-06-25 19:34:10 +02:00
|
|
|
con.get_module('Search').send_search_form(
|
|
|
|
self.jid, self.data_form_widget.data_form.get_purged(), True)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
infos = self.data_form_widget.get_infos()
|
|
|
|
if 'instructions' in infos:
|
|
|
|
del infos['instructions']
|
2018-06-25 19:34:10 +02:00
|
|
|
con.get_module('Search').send_search_form(self.jid, infos, False)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.search_vbox.remove(self.data_form_widget)
|
|
|
|
|
|
|
|
self.progressbar.show()
|
|
|
|
self.label.set_text(_('Waiting for results'))
|
|
|
|
self.label.show()
|
2013-07-28 20:50:30 +02:00
|
|
|
self.pulse_id = GLib.timeout_add(80, self.pulse_callback)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.search_button.hide()
|
|
|
|
|
|
|
|
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]
|
2018-07-16 23:22:33 +02:00
|
|
|
AddNewContactWindow(self.account, jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
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]
|
2017-08-13 13:18:56 +02:00
|
|
|
if jid in app.interface.instances[self.account]['infos']:
|
|
|
|
app.interface.instances[self.account]['infos'][jid].window.present()
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
contact = app.contacts.create_contact(jid=jid, account=self.account)
|
|
|
|
app.interface.instances[self.account]['infos'][jid] = \
|
2010-02-08 15:08:40 +01:00
|
|
|
vcard.VcardWindow(contact, self.account)
|
|
|
|
|
2010-08-30 21:42:36 +02:00
|
|
|
def _nec_search_form_received(self, obj):
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.pulse_id:
|
2013-07-28 20:50:30 +02:00
|
|
|
GLib.source_remove(self.pulse_id)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.progressbar.hide()
|
|
|
|
self.label.hide()
|
|
|
|
|
2010-08-30 21:42:36 +02:00
|
|
|
if obj.is_dataform:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.is_form = True
|
|
|
|
self.data_form_widget = dataforms_widget.DataFormWidget()
|
2018-09-12 00:01:54 +02:00
|
|
|
self.dataform = dataforms.extend_form(node=obj.data)
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
if self.data_form_widget.title:
|
|
|
|
self.window.set_title('%s - Search - Gajim' % \
|
|
|
|
self.data_form_widget.title)
|
|
|
|
else:
|
|
|
|
self.is_form = False
|
2010-08-30 21:42:36 +02:00
|
|
|
self.data_form_widget = config.FakeDataForm(obj.data)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.data_form_widget.show_all()
|
2012-12-23 16:23:43 +01:00
|
|
|
self.search_vbox.pack_start(self.data_form_widget, True, True, 0)
|
2014-01-26 08:59:24 +01:00
|
|
|
self.search_button.set_sensitive(True)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2010-08-30 21:42:36 +02:00
|
|
|
def _nec_search_result_received(self, obj):
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.pulse_id:
|
2013-07-28 20:50:30 +02:00
|
|
|
GLib.source_remove(self.pulse_id)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.progressbar.hide()
|
|
|
|
self.label.hide()
|
|
|
|
|
2010-08-30 21:42:36 +02:00
|
|
|
if not obj.is_dataform:
|
|
|
|
if not obj.data:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.label.set_text(_('No result'))
|
|
|
|
self.label.show()
|
|
|
|
return
|
|
|
|
# We suppose all items have the same fields
|
2012-12-23 16:23:43 +01:00
|
|
|
sw = Gtk.ScrolledWindow()
|
|
|
|
sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
|
|
|
|
self.result_treeview = Gtk.TreeView()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.result_treeview.connect('cursor-changed',
|
2010-08-30 21:42:36 +02:00
|
|
|
self.on_result_treeview_cursor_changed)
|
2010-02-08 15:08:40 +01:00
|
|
|
sw.add(self.result_treeview)
|
|
|
|
# Create model
|
2010-08-30 21:42:36 +02:00
|
|
|
fieldtypes = [str]*len(obj.data[0])
|
2012-12-23 16:23:43 +01:00
|
|
|
model = Gtk.ListStore(*fieldtypes)
|
2010-02-08 15:08:40 +01:00
|
|
|
# Copy data to model
|
2010-08-30 21:42:36 +02:00
|
|
|
for item in obj.data:
|
2010-02-08 15:08:40 +01:00
|
|
|
model.append(item.values())
|
|
|
|
# Create columns
|
|
|
|
counter = 0
|
2010-08-30 21:42:36 +02:00
|
|
|
for field in obj.data[0].keys():
|
2012-12-23 16:23:43 +01:00
|
|
|
self.result_treeview.append_column(Gtk.TreeViewColumn(field,
|
|
|
|
Gtk.CellRendererText(), text=counter))
|
2010-02-08 15:08:40 +01:00
|
|
|
if field == 'jid':
|
|
|
|
self.jid_column = counter
|
|
|
|
counter += 1
|
|
|
|
self.result_treeview.set_model(model)
|
|
|
|
sw.show_all()
|
2012-12-23 16:23:43 +01:00
|
|
|
self.search_vbox.pack_start(sw, True, True, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.jid_column > -1:
|
|
|
|
self.add_contact_button.show()
|
|
|
|
self.information_button.show()
|
|
|
|
return
|
|
|
|
|
2018-09-12 00:01:54 +02:00
|
|
|
self.dataform = dataforms.extend_form(node=obj.data)
|
2018-09-16 01:10:04 +02:00
|
|
|
if not self.dataform.items:
|
2010-02-08 15:08:40 +01:00
|
|
|
# No result
|
|
|
|
self.label.set_text(_('No result'))
|
|
|
|
self.label.show()
|
|
|
|
return
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self.result_treeview = self.data_form_widget.records_treeview
|
|
|
|
selection = self.result_treeview.get_selection()
|
2012-12-23 16:23:43 +01:00
|
|
|
selection.set_mode(Gtk.SelectionMode.SINGLE)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.result_treeview.connect('cursor-changed',
|
2010-08-30 21:42:36 +02:00
|
|
|
self.on_result_treeview_cursor_changed)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
counter = 0
|
2013-07-22 20:36:03 +02:00
|
|
|
for field in self.dataform.reported.iter_fields():
|
2010-02-08 15:08:40 +01:00
|
|
|
if field.var == 'jid':
|
|
|
|
self.jid_column = counter
|
|
|
|
break
|
|
|
|
counter += 1
|
2012-12-23 16:23:43 +01:00
|
|
|
self.search_vbox.pack_start(self.data_form_widget, True, True, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.data_form_widget.show()
|
|
|
|
if self.jid_column > -1:
|
|
|
|
self.add_contact_button.show()
|
|
|
|
self.information_button.show()
|
|
|
|
if self.data_form_widget.title:
|
|
|
|
self.window.set_title('%s - Search - Gajim' % \
|
2010-08-30 21:42:36 +02:00
|
|
|
self.data_form_widget.title)
|