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