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