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