]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
Merge branch 'master' into mc5
[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 #include <libempathy/empathy-account.h>
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include <libempathy/empathy-debug.h>
39
40 #include <libempathy-gtk/empathy-ui-utils.h>
41
42 #include "empathy-new-message-dialog.h"
43 #include "empathy-account-chooser.h"
44
45 /**
46  * SECTION:empathy-new-message-dialog
47  * @title: EmpathyNewMessageDialog
48  * @short_description: A dialog to show a new message
49  * @include: libempathy-gtk/empathy-new-message-dialog.h
50  *
51  * #EmpathyNewMessageDialog is a dialog which allows a text chat or
52  * call to be started with any contact on any enabled account.
53  */
54
55 typedef struct {
56         GtkWidget *dialog;
57         GtkWidget *table_contact;
58         GtkWidget *account_chooser;
59         GtkWidget *entry_id;
60         GtkWidget *button_chat;
61         GtkWidget *button_call;
62         EmpathyContactManager *contact_manager;
63 } EmpathyNewMessageDialog;
64
65 enum {
66         COMPLETION_COL_TEXT,
67         COMPLETION_COL_ID,
68         COMPLETION_COL_NAME,
69 } CompletionCol;
70
71 static void
72 new_message_dialog_account_changed_cb (GtkWidget               *widget,
73                                        EmpathyNewMessageDialog *dialog)
74 {
75         EmpathyAccountChooser *chooser;
76         TpConnection          *connection;
77         EmpathyTpContactList *contact_list;
78         GList                *members;
79         GtkListStore         *store;
80         GtkEntryCompletion   *completion;
81         GtkTreeIter           iter;
82         gchar                *tmpstr;
83
84         /* Remove completions */
85         completion = gtk_entry_get_completion (GTK_ENTRY (dialog->entry_id));
86         store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
87         gtk_list_store_clear (store);
88
89         /* Get members of the new account */
90         chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
91         connection = empathy_account_chooser_get_connection (chooser);
92         if (!connection) {
93                 return;
94         }
95         contact_list = empathy_contact_manager_get_list (dialog->contact_manager,
96                                                          connection);
97         members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (contact_list));
98
99         /* Add members to the completion */
100         while (members) {
101                 EmpathyContact *contact = members->data;
102
103                 if (empathy_contact_is_online (contact)) {
104                         DEBUG ("Adding contact ID %s, Name %s",
105                                empathy_contact_get_id (contact),
106                                empathy_contact_get_name (contact));
107
108                         tmpstr = g_strdup_printf ("%s (%s)",
109                                 empathy_contact_get_name (contact),
110                                 empathy_contact_get_id (contact));
111
112                         gtk_list_store_insert_with_values (store, &iter, -1,
113                                 COMPLETION_COL_TEXT, tmpstr,
114                                 COMPLETION_COL_ID, empathy_contact_get_id (contact),
115                                 COMPLETION_COL_NAME, empathy_contact_get_name (contact),
116                                 -1);
117
118                         g_free (tmpstr);
119                 }
120
121                 g_object_unref (contact);
122                 members = g_list_delete_link (members, members);
123         }
124 }
125
126 static gboolean
127 new_message_dialog_match_selected_cb (GtkEntryCompletion *widget,
128                                       GtkTreeModel       *model,
129                                       GtkTreeIter        *iter,
130                                       EmpathyNewMessageDialog *dialog)
131 {
132         gchar *id;
133
134         if (!iter || !model) {
135                 return FALSE;
136         }
137
138         gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
139         gtk_entry_set_text (GTK_ENTRY (dialog->entry_id), id);
140
141         DEBUG ("Got selected match **%s**", id);
142
143         g_free (id);
144
145         return TRUE;
146 }
147
148 static gboolean
149 new_message_dialog_match_func (GtkEntryCompletion *completion,
150                                const gchar        *key,
151                                GtkTreeIter        *iter,
152                                gpointer            user_data)
153 {
154         GtkTreeModel *model;
155         gchar        *id;
156         gchar        *name;
157
158         model = gtk_entry_completion_get_model (completion);
159         if (!model || !iter) {
160                 return FALSE;
161         }
162
163         gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &name, -1);
164         if (strstr (name, key)) {
165                 DEBUG ("Key %s is matching name **%s**", key, name);
166                 g_free (name);
167                 return TRUE;
168         }
169         g_free (name);
170
171         gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
172         if (strstr (id, key)) {
173                 DEBUG ("Key %s is matching ID **%s**", key, id);
174                 g_free (id);
175                 return TRUE;
176         }
177         g_free (id);
178
179         return FALSE;
180 }
181
182 static void
183 new_message_dialog_call_got_contact_cb (EmpathyTpContactFactory *factory,
184                                         EmpathyContact          *contact,
185                                         const GError            *error,
186                                         gpointer                 user_data,
187                                         GObject                 *weak_object)
188 {
189         EmpathyCallFactory *call_factory;
190
191         if (error != NULL) {
192                 DEBUG ("Error: %s", error->message);
193                 return;
194         }
195
196         call_factory = empathy_call_factory_get ();
197         empathy_call_factory_new_call (call_factory, contact);
198 }
199
200 static void
201 new_message_dialog_response_cb (GtkWidget               *widget,
202                                 gint                    response,
203                                 EmpathyNewMessageDialog *dialog)
204 {
205         TpConnection *connection;
206         const gchar *id;
207
208         connection = empathy_account_chooser_get_connection (
209                 EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
210         id = gtk_entry_get_text (GTK_ENTRY (dialog->entry_id));
211         if (!connection || EMP_STR_EMPTY (id)) {
212                 gtk_widget_destroy (widget);
213                 return;
214         }
215
216         if (response == 1) {
217                 EmpathyTpContactFactory *factory;
218
219                 factory = empathy_tp_contact_factory_dup_singleton (connection);
220                 empathy_tp_contact_factory_get_from_id (factory, id,
221                         new_message_dialog_call_got_contact_cb,
222                         NULL, NULL, NULL);
223                 g_object_unref (factory);
224         } else if (response == 2) {
225                 empathy_dispatcher_chat_with_contact_id (connection, id, NULL, NULL);
226         }
227
228         gtk_widget_destroy (widget);
229 }
230
231 static void
232 new_message_change_state_button_cb  (GtkEditable             *editable,
233                                      EmpathyNewMessageDialog *dialog)
234 {
235         const gchar *id;
236         gboolean     sensitive;
237
238         id = gtk_entry_get_text (GTK_ENTRY (editable));
239         sensitive = !EMP_STR_EMPTY (id);
240
241         gtk_widget_set_sensitive (dialog->button_chat, sensitive);
242         gtk_widget_set_sensitive (dialog->button_call, sensitive);
243 }
244
245 static void
246 new_message_dialog_destroy_cb (GtkWidget               *widget,
247                                EmpathyNewMessageDialog *dialog)
248 {
249         g_object_unref (dialog->contact_manager);
250         g_free (dialog);
251 }
252
253 /**
254  * empathy_new_message_dialog_show:
255  * @parent: parent #GtkWindow of the dialog
256  *
257  * Create a new #EmpathyNewMessageDialog and show it.
258  *
259  * Return value: the new #EmpathyNewMessageDialog
260  */
261 GtkWidget *
262 empathy_new_message_dialog_show (GtkWindow *parent)
263 {
264         static EmpathyNewMessageDialog *dialog = NULL;
265         GtkBuilder                     *gui;
266         gchar                          *filename;
267         GtkEntryCompletion             *completion;
268         GtkListStore                   *model;
269
270         if (dialog) {
271                 gtk_window_present (GTK_WINDOW (dialog->dialog));
272                 return dialog->dialog;
273         }
274
275         dialog = g_new0 (EmpathyNewMessageDialog, 1);
276
277         /* create a contact manager */
278         dialog->contact_manager = empathy_contact_manager_dup_singleton ();
279
280         filename = empathy_file_lookup ("empathy-new-message-dialog.ui",
281                                         "libempathy-gtk");
282         gui = empathy_builder_get_file (filename,
283                                         "new_message_dialog", &dialog->dialog,
284                                         "table_contact", &dialog->table_contact,
285                                         "entry_id", &dialog->entry_id,
286                                         "button_chat", &dialog->button_chat,
287                                         "button_call",&dialog->button_call,
288                                         NULL);
289         g_free (filename);
290
291         /* text completion */
292         completion = gtk_entry_completion_new ();
293         model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
294         gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
295         gtk_entry_completion_set_match_func (completion,
296                                              new_message_dialog_match_func,
297                                              NULL, NULL);
298         gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
299         gtk_entry_set_completion (GTK_ENTRY (dialog->entry_id), completion);
300         g_signal_connect (completion, "match-selected",
301                           G_CALLBACK (new_message_dialog_match_selected_cb),
302                           dialog);
303         g_object_unref (completion);
304         g_object_unref (model);
305
306         empathy_builder_connect (gui, dialog,
307                                "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
308                                "new_message_dialog", "response", new_message_dialog_response_cb,
309                                "entry_id", "changed", new_message_change_state_button_cb,
310                                NULL);
311
312         g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
313
314         g_object_unref (gui);
315
316         /* Create account chooser */
317         dialog->account_chooser = empathy_account_chooser_new ();
318         gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
319                                    dialog->account_chooser,
320                                    1, 2, 0, 1);
321         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
322                                             empathy_account_chooser_filter_is_connected,
323                                             NULL);
324         gtk_widget_show (dialog->account_chooser);
325
326         new_message_dialog_account_changed_cb (dialog->account_chooser, dialog);
327         g_signal_connect (dialog->account_chooser, "changed",
328                           G_CALLBACK (new_message_dialog_account_changed_cb),
329                           dialog);
330
331         if (parent) {
332                 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
333                                               GTK_WINDOW (parent));
334         }
335
336         gtk_widget_set_sensitive (dialog->button_chat, FALSE);
337         gtk_widget_set_sensitive (dialog->button_call, FALSE);
338
339         gtk_widget_show (dialog->dialog);
340
341         return dialog->dialog;
342 }