]> git.0d.be Git - empathy.git/blob - src/empathy-invite-participant-dialog.c
invite-participant-dialog: add an entry searching the contact list
[empathy.git] / src / empathy-invite-participant-dialog.c
1 /*
2  * empathy-invite-participant-dialog.c
3  *
4  * EmpathyInviteParticipantDialog
5  *
6  * (c) 2009, Collabora Ltd.
7  *
8  * Authors:
9  *    Danielle Madeley <danielle.madeley@collabora.co.uk>
10  */
11
12 #include <glib/gi18n.h>
13 #include <folks/folks-telepathy.h>
14
15 #include "empathy-invite-participant-dialog.h"
16
17 #include <libempathy-gtk/empathy-individual-view.h>
18 #include <libempathy-gtk/empathy-ui-utils.h>
19
20 G_DEFINE_TYPE (EmpathyInviteParticipantDialog,
21     empathy_invite_participant_dialog, GTK_TYPE_DIALOG);
22
23 enum
24 {
25   PROP_TP_CHAT = 1
26 };
27
28 struct _EmpathyInviteParticipantDialogPrivate
29 {
30   EmpathyTpChat *tp_chat;
31
32   EmpathyIndividualStore *store;
33   EmpathyIndividualView *view;
34
35   GtkWidget *invite_button;
36
37   GPtrArray *search_words;
38 };
39
40 static void
41 invite_participant_dialog_get_property (GObject *object,
42     guint param_id,
43     GValue *value,
44     GParamSpec *pspec)
45 {
46   EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
47     object;
48
49   switch (param_id)
50     {
51     case PROP_TP_CHAT:
52       g_value_set_object (value, self->priv->tp_chat);
53       break;
54     default:
55       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
56       break;
57     };
58 }
59
60 static void
61 invite_participant_dialog_set_property (GObject *object,
62     guint param_id,
63     const GValue *value,
64     GParamSpec *pspec)
65 {
66   EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
67     object;
68
69   switch (param_id)
70     {
71     case PROP_TP_CHAT:
72       g_assert (self->priv->tp_chat == NULL); /* construct-only */
73       self->priv->tp_chat = g_value_dup_object (value);
74       break;
75     default:
76       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
77       break;
78     };
79 }
80
81 static void
82 invite_participant_dialog_dispose (GObject *object)
83 {
84   EmpathyInviteParticipantDialog *self = (EmpathyInviteParticipantDialog *)
85     object;
86
87   tp_clear_object (&self->priv->tp_chat);
88   tp_clear_object (&self->priv->store);
89   tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
90
91   G_OBJECT_CLASS (empathy_invite_participant_dialog_parent_class)->dispose (
92       object);
93 }
94
95 static void
96 empathy_invite_participant_dialog_class_init (
97     EmpathyInviteParticipantDialogClass *klass)
98 {
99   GObjectClass *object_class = G_OBJECT_CLASS (klass);
100
101   object_class->get_property = invite_participant_dialog_get_property;
102   object_class->set_property = invite_participant_dialog_set_property;
103   object_class->dispose = invite_participant_dialog_dispose;
104
105   g_type_class_add_private (object_class,
106       sizeof (EmpathyInviteParticipantDialogPrivate));
107
108   g_object_class_install_property (object_class,
109       PROP_TP_CHAT,
110       g_param_spec_object ("tp-chat", "EmpathyTpChat", "EmpathyTpChat",
111           EMPATHY_TYPE_TP_CHAT,
112           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
113 }
114
115 static void
116 view_selection_changed_cb (GtkWidget *treeview,
117     EmpathyInviteParticipantDialog *self)
118 {
119   FolksIndividual *individual;
120
121   individual = empathy_individual_view_dup_selected (self->priv->view);
122
123   gtk_widget_set_sensitive (self->priv->invite_button, individual != NULL);
124
125   tp_clear_object (&individual);
126 }
127
128 /* Return the TpContact of @individual which is on the same connection as the
129  * EmpathyTpChat */
130 static TpContact *
131 get_tp_contact_for_chat (EmpathyInviteParticipantDialog *self,
132     FolksIndividual *individual)
133 {
134   GList *personas, *l;
135   TpConnection *chat_conn;
136
137   chat_conn = tp_channel_borrow_connection ((TpChannel *) self->priv->tp_chat);
138
139   personas = folks_individual_get_personas (individual);
140
141   for (l = personas; l != NULL; l = g_list_next (l))
142     {
143       TpfPersona *persona = l->data;
144       TpContact *contact;
145       TpConnection *contact_conn;
146
147       if (!TPF_IS_PERSONA (persona))
148         continue;
149
150       contact = tpf_persona_get_contact (persona);
151       if (contact == NULL)
152         continue;
153
154       contact_conn = tp_contact_get_connection (contact);
155
156       if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
157             tp_proxy_get_object_path (chat_conn)))
158         return contact;
159     }
160
161   return NULL;
162 }
163
164 static gboolean
165 filter_func (GtkTreeModel *model,
166     GtkTreeIter *iter,
167     gpointer user_data)
168 {
169   EmpathyInviteParticipantDialog *self = user_data;
170   FolksIndividual *individual;
171   TpContact *contact;
172   gboolean is_online;
173   GList *members, *l;
174   gboolean display = FALSE;
175
176   gtk_tree_model_get (model, iter,
177       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
178       EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
179       -1);
180
181   if (individual == NULL)
182     goto out;
183
184   if (self->priv->search_words == NULL)
185     {
186       /* Not searching, display online contacts */
187       if (!is_online)
188         goto out;
189     }
190   else
191     {
192       if (!empathy_individual_match_words (individual,
193             self->priv->search_words))
194         goto out;
195     }
196
197   /* Filter out individuals not having a persona on the same connection as the
198    * EmpathyTpChat. */
199   contact = get_tp_contact_for_chat (self, individual);
200
201   if (contact == NULL)
202     goto out;
203
204   /* Filter out contacts which are already in the chat */
205   members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (
206         self->priv->tp_chat));
207
208   display = TRUE;
209
210   for (l = members; l != NULL; l = g_list_next (l))
211     {
212       EmpathyContact *member = l->data;
213       TpHandle handle;
214
215       /* Try to get the non-channel specific handle. */
216       handle = tp_channel_group_get_handle_owner (
217           TP_CHANNEL (self->priv->tp_chat),
218           empathy_contact_get_handle (member));
219       if (handle == 0)
220         handle = empathy_contact_get_handle (member);
221
222       if (handle == tp_contact_get_handle (contact))
223         {
224           display = FALSE;
225           break;
226         }
227     }
228
229   g_list_free_full (members, g_object_unref);
230
231 out:
232   tp_clear_object (&individual);
233   return display;
234 }
235
236 static void
237 search_text_changed (GtkEntry *entry,
238     EmpathyInviteParticipantDialog *self)
239 {
240   tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
241
242   self->priv->search_words = empathy_live_search_strip_utf8_string (
243       gtk_entry_get_text (entry));
244
245   empathy_individual_view_refilter (self->priv->view);
246 }
247
248 static void
249 empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
250 {
251   GtkDialog *dialog = GTK_DIALOG (self);
252   GtkWidget *label;
253   char *str;
254   GtkWidget *content;
255   EmpathyIndividualManager *mgr;
256   GtkTreeSelection *selection;
257   GtkWidget *scroll;
258   GtkWidget *search_entry;
259
260   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
261       self, EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
262       EmpathyInviteParticipantDialogPrivate);
263
264   content = gtk_dialog_get_content_area (dialog);
265
266   label = gtk_label_new (NULL);
267   str = g_strdup_printf (
268       "<span size=\"x-large\" weight=\"bold\">%s</span>\n\n%s",
269       _("Invite Participant"),
270       _("Choose a contact to invite into the conversation:"));
271   gtk_label_set_markup (GTK_LABEL (label), str);
272   g_free (str);
273
274   gtk_box_pack_start (GTK_BOX (content), label, FALSE, TRUE, 6);
275   gtk_widget_show (label);
276
277   gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
278
279   /* Search entry */
280   search_entry = gtk_entry_new ();
281   gtk_box_pack_start (GTK_BOX (content), search_entry, FALSE, TRUE, 6);
282   gtk_widget_show (search_entry);
283
284   g_signal_connect (search_entry, "changed",
285       G_CALLBACK (search_text_changed), self);
286
287   /* Add the treeview */
288   mgr = empathy_individual_manager_dup_singleton ();
289   self->priv->store = empathy_individual_store_new (mgr);
290   g_object_unref (mgr);
291
292   empathy_individual_store_set_show_groups (self->priv->store, FALSE);
293
294   self->priv->view = empathy_individual_view_new (self->priv->store,
295       EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
296
297   empathy_individual_view_set_custom_filter (self->priv->view,
298       filter_func, self);
299
300   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
301
302   g_signal_connect (selection, "changed",
303       G_CALLBACK (view_selection_changed_cb), self);
304
305   scroll = gtk_scrolled_window_new (NULL, NULL);
306
307   gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (self->priv->view));
308
309   gtk_box_pack_start (GTK_BOX (content), scroll, TRUE, TRUE, 6);
310   gtk_widget_show (GTK_WIDGET (self->priv->view));
311   gtk_widget_show (scroll);
312
313   self->priv->invite_button = gtk_dialog_add_button (dialog, _("Invite"),
314       GTK_RESPONSE_ACCEPT);
315   gtk_widget_set_sensitive (self->priv->invite_button, FALSE);
316
317   gtk_window_set_title (GTK_WINDOW (self), _("Invite Participant"));
318   gtk_window_set_role (GTK_WINDOW (self), "invite_participant");
319
320   /* Set a default height so a few contacts are displayed */
321   gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
322 }
323
324 GtkWidget *
325 empathy_invite_participant_dialog_new (GtkWindow *parent,
326     EmpathyTpChat *tp_chat)
327 {
328   GtkWidget *self = g_object_new (EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
329       "tp-chat", tp_chat,
330       NULL);
331
332   if (parent != NULL)
333     {
334       gtk_window_set_transient_for (GTK_WINDOW (self), parent);
335     }
336
337   return self;
338 }
339
340 TpContact *
341 empathy_invite_participant_dialog_get_selected (
342     EmpathyInviteParticipantDialog *self)
343 {
344   FolksIndividual *individual;
345   TpContact *contact;
346
347   individual = empathy_individual_view_dup_selected (self->priv->view);
348   if (individual == NULL)
349     return NULL;
350
351   contact = get_tp_contact_for_chat (self, individual);
352
353   g_object_unref (individual);
354   return contact;
355 }