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 <libmissioncontrol/mc-account.h>
31 #include <libmissioncontrol/mission-control.h>
33 #include <libempathy/empathy-call-factory.h>
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>
39 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
40 #include <libempathy/empathy-debug.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include "empathy-new-message-dialog.h"
45 #include "empathy-account-chooser.h"
49 GtkWidget *table_contact;
50 GtkWidget *account_chooser;
52 GtkWidget *button_chat;
53 GtkWidget *button_call;
54 EmpathyContactManager *contact_manager;
55 } EmpathyNewMessageDialog;
64 new_message_dialog_account_changed_cb (GtkWidget *widget,
65 EmpathyNewMessageDialog *dialog)
67 EmpathyAccountChooser *chooser;
69 EmpathyTpContactList *contact_list;
72 GtkEntryCompletion *completion;
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,
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);
85 for (l = members; l; l = l->next) {
86 EmpathyContact *contact = l->data;
88 if (!empathy_contact_is_online (contact)) {
92 DEBUG ("Adding contact ID %s, Name %s",
93 empathy_contact_get_id (contact),
94 empathy_contact_get_name (contact));
96 tmpstr = g_strdup_printf ("%s (%s)",
97 empathy_contact_get_name (contact),
98 empathy_contact_get_id (contact));
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),
109 g_object_unref (account);
113 new_message_dialog_match_selected_cb (GtkEntryCompletion *widget,
116 EmpathyNewMessageDialog *dialog)
120 if (!iter || !model) {
124 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
125 gtk_entry_set_text (GTK_ENTRY (dialog->entry_id), id);
127 DEBUG ("Got selected match **%s**", id);
135 new_message_dialog_match_func (GtkEntryCompletion *completion,
144 model = gtk_entry_completion_get_model (completion);
145 if (!model || !iter) {
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);
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);
169 new_message_dialog_response_cb (GtkWidget *widget,
171 EmpathyNewMessageDialog *dialog)
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 || EMP_STR_EMPTY (id)) {
180 g_object_unref (account);
182 gtk_widget_destroy (widget);
187 EmpathyContactFactory *factory;
188 EmpathyContact *contact;
189 EmpathyCallFactory *call_factory;
191 factory = empathy_contact_factory_dup_singleton ();
192 contact = empathy_contact_factory_get_from_id (factory, account, id);
194 call_factory = empathy_call_factory_get();
195 empathy_call_factory_new_call (call_factory, contact);
197 g_object_unref (contact);
198 g_object_unref (factory);
199 } else if (response == 2) {
200 empathy_dispatcher_chat_with_contact_id (account, id, NULL, NULL);
203 g_object_unref (account);
204 gtk_widget_destroy (widget);
208 new_message_change_state_button_cb (GtkEditable *editable,
209 EmpathyNewMessageDialog *dialog)
214 id = gtk_entry_get_text (GTK_ENTRY (editable));
215 sensitive = !EMP_STR_EMPTY (id);
217 gtk_widget_set_sensitive (dialog->button_chat, sensitive);
218 gtk_widget_set_sensitive (dialog->button_call, sensitive);
222 new_message_dialog_destroy_cb (GtkWidget *widget,
223 EmpathyNewMessageDialog *dialog)
225 g_object_unref (dialog->contact_manager);
230 empathy_new_message_dialog_show (GtkWindow *parent)
232 static EmpathyNewMessageDialog *dialog = NULL;
235 GtkEntryCompletion *completion;
239 gtk_window_present (GTK_WINDOW (dialog->dialog));
240 return dialog->dialog;
243 dialog = g_new0 (EmpathyNewMessageDialog, 1);
245 /* create a contact manager */
246 dialog->contact_manager = empathy_contact_manager_dup_singleton ();
248 filename = empathy_file_lookup ("empathy-new-message-dialog.ui",
250 gui = empathy_builder_get_file (filename,
251 "new_message_dialog", &dialog->dialog,
252 "table_contact", &dialog->table_contact,
253 "entry_id", &dialog->entry_id,
254 "button_chat", &dialog->button_chat,
255 "button_call",&dialog->button_call,
259 /* text completion */
260 completion = gtk_entry_completion_new ();
261 model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
262 gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
263 gtk_entry_completion_set_match_func (completion,
264 new_message_dialog_match_func,
266 gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
267 gtk_entry_set_completion (GTK_ENTRY (dialog->entry_id), completion);
268 g_signal_connect (completion, "match-selected",
269 G_CALLBACK (new_message_dialog_match_selected_cb),
271 g_object_unref(completion);
272 g_object_unref(model);
274 empathy_builder_connect (gui, dialog,
275 "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
276 "new_message_dialog", "response", new_message_dialog_response_cb,
277 "entry_id", "changed", new_message_change_state_button_cb,
280 g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
282 g_object_unref (gui);
284 /* Create account chooser */
285 dialog->account_chooser = empathy_account_chooser_new ();
286 gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
287 dialog->account_chooser,
289 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
290 empathy_account_chooser_filter_is_connected,
292 gtk_widget_show (dialog->account_chooser);
294 new_message_dialog_account_changed_cb (dialog->account_chooser, dialog);
295 g_signal_connect (dialog->account_chooser, "changed",
296 G_CALLBACK (new_message_dialog_account_changed_cb),
300 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
301 GTK_WINDOW (parent));
304 gtk_widget_set_sensitive (dialog->button_chat, FALSE);
305 gtk_widget_set_sensitive (dialog->button_call, FALSE);
307 gtk_widget_show (dialog->dialog);
309 return dialog->dialog;