2 * Copyright (C) 2007-2008 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
27 #include <glib/gi18n-lib.h>
29 #include <telepathy-glib/interfaces.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>
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-images.h>
42 #include "empathy-new-message-dialog.h"
43 #include "empathy-account-chooser.h"
46 EmpathyAccountChooserFilterResultCallback callback;
50 static EmpathyNewMessageDialog *dialog_singleton = NULL;
52 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
53 EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
56 * SECTION:empathy-new-message-dialog
57 * @title: EmpathyNewMessageDialog
58 * @short_description: A dialog to show a new message
59 * @include: libempathy-gtk/empathy-new-message-dialog.h
61 * #EmpathyNewMessageDialog is a dialog which allows a text chat
62 * to be started with any contact on any enabled account.
66 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
69 const gchar *contact_id;
71 if (response_id != GTK_RESPONSE_ACCEPT) goto out;
73 contact_id = empathy_contact_selector_dialog_get_selected (
74 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
76 if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
78 empathy_dispatcher_chat_with_contact_id (account, contact_id,
79 gtk_get_current_event_time ());
82 gtk_widget_destroy (GTK_WIDGET (dialog));
86 conn_prepared_cb (GObject *conn,
90 FilterCallbackData *data = user_data;
94 if (!tp_proxy_prepare_finish (conn, result, &myerr))
96 data->callback (FALSE, data->user_data);
97 g_slice_free (FilterCallbackData, data);
100 caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
101 data->callback (tp_capabilities_supports_text_chats (caps),
104 g_slice_free (FilterCallbackData, data);
108 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
109 EmpathyAccountChooserFilterResultCallback callback,
110 gpointer callback_data,
113 TpConnection *connection;
114 FilterCallbackData *cb_data;
115 GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
117 if (tp_account_get_connection_status (account, NULL) !=
118 TP_CONNECTION_STATUS_CONNECTED)
120 callback (FALSE, callback_data);
124 /* check if CM supports 1-1 text chat */
125 connection = tp_account_get_connection (account);
126 if (connection == NULL)
128 callback (FALSE, callback_data);
132 cb_data = g_slice_new0 (FilterCallbackData);
133 cb_data->callback = callback;
134 cb_data->user_data = callback_data;
135 tp_proxy_prepare_async (connection, features, conn_prepared_cb, cb_data);
139 empathy_new_message_dialog_constructor (GType type,
141 GObjectConstructParam *props)
145 if (dialog_singleton)
147 retval = G_OBJECT (dialog_singleton);
148 g_object_ref (retval);
152 retval = G_OBJECT_CLASS (
153 empathy_new_message_dialog_parent_class)->constructor (type,
156 dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
157 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
164 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
166 EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
170 /* add chat button */
171 parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
172 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
173 GTK_ICON_SIZE_BUTTON);
174 gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
176 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
177 GTK_RESPONSE_ACCEPT);
178 gtk_widget_show (parent->button_action);
180 /* Tweak the dialog */
181 gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
182 gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
184 gtk_widget_set_sensitive (parent->button_action, FALSE);
188 empathy_new_message_dialog_class_init (
189 EmpathyNewMessageDialogClass *class)
191 GObjectClass *object_class = G_OBJECT_CLASS (class);
192 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
193 EmpathyContactSelectorDialogClass *selector_dialog_class = \
194 EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
196 object_class->constructor = empathy_new_message_dialog_constructor;
198 dialog_class->response = empathy_new_message_dialog_response;
200 selector_dialog_class->account_filter = empathy_new_message_account_filter;
204 * empathy_new_message_dialog_new:
205 * @parent: parent #GtkWindow of the dialog
207 * Create a new #EmpathyNewMessageDialog it.
209 * Return value: the new #EmpathyNewMessageDialog
212 empathy_new_message_dialog_show (GtkWindow *parent)
216 dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
220 gtk_window_set_transient_for (GTK_WINDOW (dialog),
221 GTK_WINDOW (parent));
224 gtk_widget_show (dialog);