]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
Autocomplete for offline contacts. Closes bug #583919.
[empathy.git] / libempathy-gtk / empathy-new-message-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <libempathy/empathy-call-factory.h>
31 #include <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-dispatcher.h>
34 #include <libempathy/empathy-utils.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
38
39 #include <libempathy-gtk/empathy-ui-utils.h>
40
41 #include "empathy-new-message-dialog.h"
42 #include "empathy-account-chooser.h"
43
44 /**
45  * SECTION:empathy-new-message-dialog
46  * @title: EmpathyNewMessageDialog
47  * @short_description: A dialog to show a new message
48  * @include: libempathy-gtk/empathy-new-message-dialog.h
49  *
50  * #EmpathyNewMessageDialog is a dialog which allows a text chat or
51  * call to be started with any contact on any enabled account.
52  */
53
54 typedef struct {
55         GtkWidget *dialog;
56         GtkWidget *table_contact;
57         GtkWidget *account_chooser;
58         GtkWidget *entry_id;
59         GtkWidget *button_chat;
60         GtkWidget *button_call;
61         EmpathyContactManager *contact_manager;
62 } EmpathyNewMessageDialog;
63
64 enum {
65         COMPLETION_COL_TEXT,
66         COMPLETION_COL_ID,
67         COMPLETION_COL_NAME,
68 } CompletionCol;
69
70 static void
71 new_message_dialog_account_changed_cb (GtkWidget               *widget,
72                                        EmpathyNewMessageDialog *dialog)
73 {
74         EmpathyAccountChooser *chooser;
75         TpConnection          *connection;
76         EmpathyTpContactList *contact_list;
77         GList                *members;
78         GtkListStore         *store;
79         GtkEntryCompletion   *completion;
80         GtkTreeIter           iter;
81         gchar                *tmpstr;
82
83         /* Remove completions */
84         completion = gtk_entry_get_completion (GTK_ENTRY (dialog->entry_id));
85         store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
86         gtk_list_store_clear (store);
87
88         /* Get members of the new account */
89         chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
90         connection = empathy_account_chooser_get_connection (chooser);
91         if (!connection) {
92                 return;
93         }
94         contact_list = empathy_contact_manager_get_list (dialog->contact_manager,
95                                                          connection);
96         members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (contact_list));
97
98         /* Add members to the completion */
99         while (members) {
100                 EmpathyContact *contact = members->data;
101
102                 DEBUG ("Adding contact ID %s, Name %s",
103                        empathy_contact_get_id (contact),
104                        empathy_contact_get_name (contact));
105
106                 tmpstr = g_strdup_printf ("%s (%s)",
107                         empathy_contact_get_name (contact),
108                         empathy_contact_get_id (contact));
109
110                 gtk_list_store_insert_with_values (store, &iter, -1,
111                         COMPLETION_COL_TEXT, tmpstr,
112                         COMPLETION_COL_ID, empathy_contact_get_id (contact),
113                         COMPLETION_COL_NAME, empathy_contact_get_name (contact),
114                         -1);
115
116                 g_free (tmpstr);
117
118                 g_object_unref (contact);
119                 members = g_list_delete_link (members, members);
120         }
121 }
122
123 static gboolean
124 new_message_dialog_match_selected_cb (GtkEntryCompletion *widget,
125                                       GtkTreeModel       *model,
126                                       GtkTreeIter        *iter,
127                                       EmpathyNewMessageDialog *dialog)
128 {
129         gchar *id;
130
131         if (!iter || !model) {
132                 return FALSE;
133         }
134
135         gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
136         gtk_entry_set_text (GTK_ENTRY (dialog->entry_id), id);
137
138         DEBUG ("Got selected match **%s**", id);
139
140         g_free (id);
141
142         return TRUE;
143 }
144
145 static gboolean
146 new_message_dialog_match_func (GtkEntryCompletion *completion,
147                                const gchar        *key,
148                                GtkTreeIter        *iter,
149                                gpointer            user_data)
150 {
151         GtkTreeModel *model;
152         gchar        *id;
153         gchar        *name;
154
155         model = gtk_entry_completion_get_model (completion);
156         if (!model || !iter) {
157                 return FALSE;
158         }
159
160         gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &name, -1);
161         if (strstr (name, key)) {
162                 DEBUG ("Key %s is matching name **%s**", key, name);
163                 g_free (name);
164                 return TRUE;
165         }
166         g_free (name);
167
168         gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
169         if (strstr (id, key)) {
170                 DEBUG ("Key %s is matching ID **%s**", key, id);
171                 g_free (id);
172                 return TRUE;
173         }
174         g_free (id);
175
176         return FALSE;
177 }
178
179 static void
180 new_message_dialog_call_got_contact_cb (EmpathyTpContactFactory *factory,
181                                         EmpathyContact          *contact,
182                                         const GError            *error,
183                                         gpointer                 user_data,
184                                         GObject                 *weak_object)
185 {
186         EmpathyCallFactory *call_factory;
187
188         if (error != NULL) {
189                 DEBUG ("Error: %s", error->message);
190                 return;
191         }
192
193         call_factory = empathy_call_factory_get ();
194         empathy_call_factory_new_call (call_factory, contact);
195 }
196
197 static void
198 new_message_dialog_response_cb (GtkWidget               *widget,
199                                 gint                    response,
200                                 EmpathyNewMessageDialog *dialog)
201 {
202         TpConnection *connection;
203         const gchar *id;
204
205         connection = empathy_account_chooser_get_connection (
206                 EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
207         id = gtk_entry_get_text (GTK_ENTRY (dialog->entry_id));
208         if (!connection || EMP_STR_EMPTY (id)) {
209                 gtk_widget_destroy (widget);
210                 return;
211         }
212
213         if (response == 1) {
214                 EmpathyTpContactFactory *factory;
215
216                 factory = empathy_tp_contact_factory_dup_singleton (connection);
217                 empathy_tp_contact_factory_get_from_id (factory, id,
218                         new_message_dialog_call_got_contact_cb,
219                         NULL, NULL, NULL);
220                 g_object_unref (factory);
221         } else if (response == 2) {
222                 empathy_dispatcher_chat_with_contact_id (connection, id, NULL, NULL);
223         }
224
225         gtk_widget_destroy (widget);
226 }
227
228 static void
229 new_message_change_state_button_cb  (GtkEditable             *editable,
230                                      EmpathyNewMessageDialog *dialog)
231 {
232         const gchar *id;
233         gboolean     sensitive;
234
235         id = gtk_entry_get_text (GTK_ENTRY (editable));
236         sensitive = !EMP_STR_EMPTY (id);
237
238         gtk_widget_set_sensitive (dialog->button_chat, sensitive);
239         gtk_widget_set_sensitive (dialog->button_call, sensitive);
240 }
241
242 static void
243 new_message_dialog_destroy_cb (GtkWidget               *widget,
244                                EmpathyNewMessageDialog *dialog)
245 {
246         g_object_unref (dialog->contact_manager);
247         g_free (dialog);
248 }
249
250 /**
251  * empathy_new_message_dialog_show:
252  * @parent: parent #GtkWindow of the dialog
253  *
254  * Create a new #EmpathyNewMessageDialog and show it.
255  *
256  * Return value: the new #EmpathyNewMessageDialog
257  */
258 GtkWidget *
259 empathy_new_message_dialog_show (GtkWindow *parent)
260 {
261         static EmpathyNewMessageDialog *dialog = NULL;
262         GtkBuilder                     *gui;
263         gchar                          *filename;
264         GtkEntryCompletion             *completion;
265         GtkListStore                   *model;
266
267         if (dialog) {
268                 gtk_window_present (GTK_WINDOW (dialog->dialog));
269                 return dialog->dialog;
270         }
271
272         dialog = g_new0 (EmpathyNewMessageDialog, 1);
273
274         /* create a contact manager */
275         dialog->contact_manager = empathy_contact_manager_dup_singleton ();
276
277         filename = empathy_file_lookup ("empathy-new-message-dialog.ui",
278                                         "libempathy-gtk");
279         gui = empathy_builder_get_file (filename,
280                                         "new_message_dialog", &dialog->dialog,
281                                         "table_contact", &dialog->table_contact,
282                                         "entry_id", &dialog->entry_id,
283                                         "button_chat", &dialog->button_chat,
284                                         "button_call",&dialog->button_call,
285                                         NULL);
286         g_free (filename);
287
288         /* text completion */
289         completion = gtk_entry_completion_new ();
290         model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
291         gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
292         gtk_entry_completion_set_match_func (completion,
293                                              new_message_dialog_match_func,
294                                              NULL, NULL);
295         gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
296         gtk_entry_set_completion (GTK_ENTRY (dialog->entry_id), completion);
297         g_signal_connect (completion, "match-selected",
298                           G_CALLBACK (new_message_dialog_match_selected_cb),
299                           dialog);
300         g_object_unref (completion);
301         g_object_unref (model);
302
303         empathy_builder_connect (gui, dialog,
304                                "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
305                                "new_message_dialog", "response", new_message_dialog_response_cb,
306                                "entry_id", "changed", new_message_change_state_button_cb,
307                                NULL);
308
309         g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
310
311         g_object_unref (gui);
312
313         /* Create account chooser */
314         dialog->account_chooser = empathy_account_chooser_new ();
315         gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
316                                    dialog->account_chooser,
317                                    1, 2, 0, 1);
318         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
319                                             empathy_account_chooser_filter_is_connected,
320                                             NULL);
321         gtk_widget_show (dialog->account_chooser);
322
323         new_message_dialog_account_changed_cb (dialog->account_chooser, dialog);
324         g_signal_connect (dialog->account_chooser, "changed",
325                           G_CALLBACK (new_message_dialog_account_changed_cb),
326                           dialog);
327
328         if (parent) {
329                 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
330                                               GTK_WINDOW (parent));
331         }
332
333         gtk_widget_set_sensitive (dialog->button_chat, FALSE);
334         gtk_widget_set_sensitive (dialog->button_call, FALSE);
335
336         gtk_widget_show (dialog->dialog);
337
338         return dialog->dialog;
339 }