1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
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 #include <libempathy/empathy-account.h>
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include <libempathy/empathy-debug.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include "empathy-new-message-dialog.h"
43 #include "empathy-account-chooser.h"
46 * SECTION:empathy-new-message-dialog
47 * @title: EmpathyNewMessageDialog
48 * @short_description: A dialog to show a new message
49 * @include: libempathy-gtk/empathy-new-message-dialog.h
51 * #EmpathyNewMessageDialog is a dialog which allows a text chat or
52 * call to be started with any contact on any enabled account.
57 GtkWidget *table_contact;
58 GtkWidget *account_chooser;
60 GtkWidget *button_chat;
61 GtkWidget *button_call;
62 EmpathyContactManager *contact_manager;
63 } EmpathyNewMessageDialog;
72 new_message_dialog_account_changed_cb (GtkWidget *widget,
73 EmpathyNewMessageDialog *dialog)
75 EmpathyAccountChooser *chooser;
76 TpConnection *connection;
77 EmpathyTpContactList *contact_list;
80 GtkEntryCompletion *completion;
84 /* Remove completions */
85 completion = gtk_entry_get_completion (GTK_ENTRY (dialog->entry_id));
86 store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
87 gtk_list_store_clear (store);
89 /* Get members of the new account */
90 chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
91 connection = empathy_account_chooser_get_connection (chooser);
95 contact_list = empathy_contact_manager_get_list (dialog->contact_manager,
97 members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (contact_list));
99 /* Add members to the completion */
101 EmpathyContact *contact = members->data;
103 if (empathy_contact_is_online (contact)) {
104 DEBUG ("Adding contact ID %s, Name %s",
105 empathy_contact_get_id (contact),
106 empathy_contact_get_name (contact));
108 tmpstr = g_strdup_printf ("%s (%s)",
109 empathy_contact_get_name (contact),
110 empathy_contact_get_id (contact));
112 gtk_list_store_insert_with_values (store, &iter, -1,
113 COMPLETION_COL_TEXT, tmpstr,
114 COMPLETION_COL_ID, empathy_contact_get_id (contact),
115 COMPLETION_COL_NAME, empathy_contact_get_name (contact),
121 g_object_unref (contact);
122 members = g_list_delete_link (members, members);
127 new_message_dialog_match_selected_cb (GtkEntryCompletion *widget,
130 EmpathyNewMessageDialog *dialog)
134 if (!iter || !model) {
138 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
139 gtk_entry_set_text (GTK_ENTRY (dialog->entry_id), id);
141 DEBUG ("Got selected match **%s**", id);
149 new_message_dialog_match_func (GtkEntryCompletion *completion,
158 model = gtk_entry_completion_get_model (completion);
159 if (!model || !iter) {
163 gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &name, -1);
164 if (strstr (name, key)) {
165 DEBUG ("Key %s is matching name **%s**", key, name);
171 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
172 if (strstr (id, key)) {
173 DEBUG ("Key %s is matching ID **%s**", key, id);
183 new_message_dialog_call_got_contact_cb (EmpathyTpContactFactory *factory,
184 EmpathyContact *contact,
187 GObject *weak_object)
189 EmpathyCallFactory *call_factory;
192 DEBUG ("Error: %s", error->message);
196 call_factory = empathy_call_factory_get ();
197 empathy_call_factory_new_call (call_factory, contact);
201 new_message_dialog_response_cb (GtkWidget *widget,
203 EmpathyNewMessageDialog *dialog)
205 TpConnection *connection;
208 connection = empathy_account_chooser_get_connection (
209 EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
210 id = gtk_entry_get_text (GTK_ENTRY (dialog->entry_id));
211 if (!connection || EMP_STR_EMPTY (id)) {
212 gtk_widget_destroy (widget);
217 EmpathyTpContactFactory *factory;
219 factory = empathy_tp_contact_factory_dup_singleton (connection);
220 empathy_tp_contact_factory_get_from_id (factory, id,
221 new_message_dialog_call_got_contact_cb,
223 g_object_unref (factory);
224 } else if (response == 2) {
225 empathy_dispatcher_chat_with_contact_id (connection, id, NULL, NULL);
228 gtk_widget_destroy (widget);
232 new_message_change_state_button_cb (GtkEditable *editable,
233 EmpathyNewMessageDialog *dialog)
238 id = gtk_entry_get_text (GTK_ENTRY (editable));
239 sensitive = !EMP_STR_EMPTY (id);
241 gtk_widget_set_sensitive (dialog->button_chat, sensitive);
242 gtk_widget_set_sensitive (dialog->button_call, sensitive);
246 new_message_dialog_destroy_cb (GtkWidget *widget,
247 EmpathyNewMessageDialog *dialog)
249 g_object_unref (dialog->contact_manager);
254 * empathy_new_message_dialog_show:
255 * @parent: parent #GtkWindow of the dialog
257 * Create a new #EmpathyNewMessageDialog and show it.
259 * Return value: the new #EmpathyNewMessageDialog
262 empathy_new_message_dialog_show (GtkWindow *parent)
264 static EmpathyNewMessageDialog *dialog = NULL;
267 GtkEntryCompletion *completion;
271 gtk_window_present (GTK_WINDOW (dialog->dialog));
272 return dialog->dialog;
275 dialog = g_new0 (EmpathyNewMessageDialog, 1);
277 /* create a contact manager */
278 dialog->contact_manager = empathy_contact_manager_dup_singleton ();
280 filename = empathy_file_lookup ("empathy-new-message-dialog.ui",
282 gui = empathy_builder_get_file (filename,
283 "new_message_dialog", &dialog->dialog,
284 "table_contact", &dialog->table_contact,
285 "entry_id", &dialog->entry_id,
286 "button_chat", &dialog->button_chat,
287 "button_call",&dialog->button_call,
291 /* text completion */
292 completion = gtk_entry_completion_new ();
293 model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
294 gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
295 gtk_entry_completion_set_match_func (completion,
296 new_message_dialog_match_func,
298 gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
299 gtk_entry_set_completion (GTK_ENTRY (dialog->entry_id), completion);
300 g_signal_connect (completion, "match-selected",
301 G_CALLBACK (new_message_dialog_match_selected_cb),
303 g_object_unref (completion);
304 g_object_unref (model);
306 empathy_builder_connect (gui, dialog,
307 "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
308 "new_message_dialog", "response", new_message_dialog_response_cb,
309 "entry_id", "changed", new_message_change_state_button_cb,
312 g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
314 g_object_unref (gui);
316 /* Create account chooser */
317 dialog->account_chooser = empathy_account_chooser_new ();
318 gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
319 dialog->account_chooser,
321 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
322 empathy_account_chooser_filter_is_connected,
324 gtk_widget_show (dialog->account_chooser);
326 new_message_dialog_account_changed_cb (dialog->account_chooser, dialog);
327 g_signal_connect (dialog->account_chooser, "changed",
328 G_CALLBACK (new_message_dialog_account_changed_cb),
332 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
333 GTK_WINDOW (parent));
336 gtk_widget_set_sensitive (dialog->button_chat, FALSE);
337 gtk_widget_set_sensitive (dialog->button_call, FALSE);
339 gtk_widget_show (dialog->dialog);
341 return dialog->dialog;