]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
new-message-dialog: port to new tp-glib account API
[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                 if (empathy_contact_is_online (contact)) {
103                         DEBUG ("Adding contact ID %s, Name %s",
104                                empathy_contact_get_id (contact),
105                                empathy_contact_get_name (contact));
106
107                         tmpstr = g_strdup_printf ("%s (%s)",
108                                 empathy_contact_get_name (contact),
109                                 empathy_contact_get_id (contact));
110
111                         gtk_list_store_insert_with_values (store, &iter, -1,
112                                 COMPLETION_COL_TEXT, tmpstr,
113                                 COMPLETION_COL_ID, empathy_contact_get_id (contact),
114                                 COMPLETION_COL_NAME, empathy_contact_get_name (contact),
115                                 -1);
116
117                         g_free (tmpstr);
118                 }
119
120                 g_object_unref (contact);
121                 members = g_list_delete_link (members, members);
122         }
123 }
124
125 static gboolean
126 new_message_dialog_match_selected_cb (GtkEntryCompletion *widget,
127                                       GtkTreeModel       *model,
128                                       GtkTreeIter        *iter,
129                                       EmpathyNewMessageDialog *dialog)
130 {
131         gchar *id;
132
133         if (!iter || !model) {
134                 return FALSE;
135         }
136
137         gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
138         gtk_entry_set_text (GTK_ENTRY (dialog->entry_id), id);
139
140         DEBUG ("Got selected match **%s**", id);
141
142         g_free (id);
143
144         return TRUE;
145 }
146
147 static gboolean
148 new_message_dialog_match_func (GtkEntryCompletion *completion,
149                                const gchar        *key,
150                                GtkTreeIter        *iter,
151                                gpointer            user_data)
152 {
153         GtkTreeModel *model;
154         gchar        *id;
155         gchar        *name;
156
157         model = gtk_entry_completion_get_model (completion);
158         if (!model || !iter) {
159                 return FALSE;
160         }
161
162         gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &name, -1);
163         if (strstr (name, key)) {
164                 DEBUG ("Key %s is matching name **%s**", key, name);
165                 g_free (name);
166                 return TRUE;
167         }
168         g_free (name);
169
170         gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
171         if (strstr (id, key)) {
172                 DEBUG ("Key %s is matching ID **%s**", key, id);
173                 g_free (id);
174                 return TRUE;
175         }
176         g_free (id);
177
178         return FALSE;
179 }
180
181 static void
182 new_message_dialog_call_got_contact_cb (EmpathyTpContactFactory *factory,
183                                         EmpathyContact          *contact,
184                                         const GError            *error,
185                                         gpointer                 user_data,
186                                         GObject                 *weak_object)
187 {
188         EmpathyCallFactory *call_factory;
189
190         if (error != NULL) {
191                 DEBUG ("Error: %s", error->message);
192                 return;
193         }
194
195         call_factory = empathy_call_factory_get ();
196         empathy_call_factory_new_call (call_factory, contact);
197 }
198
199 static void
200 new_message_dialog_response_cb (GtkWidget               *widget,
201                                 gint                    response,
202                                 EmpathyNewMessageDialog *dialog)
203 {
204         TpConnection *connection;
205         const gchar *id;
206
207         connection = empathy_account_chooser_get_connection (
208                 EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
209         id = gtk_entry_get_text (GTK_ENTRY (dialog->entry_id));
210         if (!connection || EMP_STR_EMPTY (id)) {
211                 gtk_widget_destroy (widget);
212                 return;
213         }
214
215         if (response == 1) {
216                 EmpathyTpContactFactory *factory;
217
218                 factory = empathy_tp_contact_factory_dup_singleton (connection);
219                 empathy_tp_contact_factory_get_from_id (factory, id,
220                         new_message_dialog_call_got_contact_cb,
221                         NULL, NULL, NULL);
222                 g_object_unref (factory);
223         } else if (response == 2) {
224                 empathy_dispatcher_chat_with_contact_id (connection, id, NULL, NULL);
225         }
226
227         gtk_widget_destroy (widget);
228 }
229
230 static void
231 new_message_change_state_button_cb  (GtkEditable             *editable,
232                                      EmpathyNewMessageDialog *dialog)
233 {
234         const gchar *id;
235         gboolean     sensitive;
236
237         id = gtk_entry_get_text (GTK_ENTRY (editable));
238         sensitive = !EMP_STR_EMPTY (id);
239
240         gtk_widget_set_sensitive (dialog->button_chat, sensitive);
241         gtk_widget_set_sensitive (dialog->button_call, sensitive);
242 }
243
244 static void
245 new_message_dialog_destroy_cb (GtkWidget               *widget,
246                                EmpathyNewMessageDialog *dialog)
247 {
248         g_object_unref (dialog->contact_manager);
249         g_free (dialog);
250 }
251
252 /**
253  * empathy_new_message_dialog_show:
254  * @parent: parent #GtkWindow of the dialog
255  *
256  * Create a new #EmpathyNewMessageDialog and show it.
257  *
258  * Return value: the new #EmpathyNewMessageDialog
259  */
260 GtkWidget *
261 empathy_new_message_dialog_show (GtkWindow *parent)
262 {
263         static EmpathyNewMessageDialog *dialog = NULL;
264         GtkBuilder                     *gui;
265         gchar                          *filename;
266         GtkEntryCompletion             *completion;
267         GtkListStore                   *model;
268
269         if (dialog) {
270                 gtk_window_present (GTK_WINDOW (dialog->dialog));
271                 return dialog->dialog;
272         }
273
274         dialog = g_new0 (EmpathyNewMessageDialog, 1);
275
276         /* create a contact manager */
277         dialog->contact_manager = empathy_contact_manager_dup_singleton ();
278
279         filename = empathy_file_lookup ("empathy-new-message-dialog.ui",
280                                         "libempathy-gtk");
281         gui = empathy_builder_get_file (filename,
282                                         "new_message_dialog", &dialog->dialog,
283                                         "table_contact", &dialog->table_contact,
284                                         "entry_id", &dialog->entry_id,
285                                         "button_chat", &dialog->button_chat,
286                                         "button_call",&dialog->button_call,
287                                         NULL);
288         g_free (filename);
289
290         /* text completion */
291         completion = gtk_entry_completion_new ();
292         model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
293         gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
294         gtk_entry_completion_set_match_func (completion,
295                                              new_message_dialog_match_func,
296                                              NULL, NULL);
297         gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
298         gtk_entry_set_completion (GTK_ENTRY (dialog->entry_id), completion);
299         g_signal_connect (completion, "match-selected",
300                           G_CALLBACK (new_message_dialog_match_selected_cb),
301                           dialog);
302         g_object_unref (completion);
303         g_object_unref (model);
304
305         empathy_builder_connect (gui, dialog,
306                                "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
307                                "new_message_dialog", "response", new_message_dialog_response_cb,
308                                "entry_id", "changed", new_message_change_state_button_cb,
309                                NULL);
310
311         g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
312
313         g_object_unref (gui);
314
315         /* Create account chooser */
316         dialog->account_chooser = empathy_account_chooser_new ();
317         gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
318                                    dialog->account_chooser,
319                                    1, 2, 0, 1);
320         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
321                                             empathy_account_chooser_filter_is_connected,
322                                             NULL);
323         gtk_widget_show (dialog->account_chooser);
324
325         new_message_dialog_account_changed_cb (dialog->account_chooser, dialog);
326         g_signal_connect (dialog->account_chooser, "changed",
327                           G_CALLBACK (new_message_dialog_account_changed_cb),
328                           dialog);
329
330         if (parent) {
331                 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
332                                               GTK_WINDOW (parent));
333         }
334
335         gtk_widget_set_sensitive (dialog->button_chat, FALSE);
336         gtk_widget_set_sensitive (dialog->button_call, FALSE);
337
338         gtk_widget_show (dialog->dialog);
339
340         return dialog->dialog;
341 }