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