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-request-util.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"
45 static EmpathyNewMessageDialog *dialog_singleton = NULL;
47 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
48 EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
51 * SECTION:empathy-new-message-dialog
52 * @title: EmpathyNewMessageDialog
53 * @short_description: A dialog to show a new message
54 * @include: libempathy-gtk/empathy-new-message-dialog.h
56 * #EmpathyNewMessageDialog is a dialog which allows a text chat
57 * to be started with any contact on any enabled account.
67 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
70 const gchar *contact_id;
72 if (response_id < EMP_NEW_MESSAGE_TEXT) goto out;
74 contact_id = empathy_contact_selector_dialog_get_selected (
75 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
77 if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
81 case EMP_NEW_MESSAGE_TEXT:
82 empathy_chat_with_contact_id (account, contact_id,
83 empathy_get_current_action_time (),
87 case EMP_NEW_MESSAGE_SMS:
88 empathy_sms_contact_id (account, contact_id,
89 empathy_get_current_action_time (),
98 gtk_widget_destroy (GTK_WIDGET (dialog));
102 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
103 EmpathyAccountChooserFilterResultCallback callback,
104 gpointer callback_data,
107 TpConnection *connection;
108 gboolean supported = FALSE;
109 TpCapabilities *caps;
111 /* check if CM supports 1-1 text chat */
112 connection = tp_account_get_connection (account);
113 if (connection == NULL)
116 caps = tp_connection_get_capabilities (connection);
120 supported = tp_capabilities_supports_text_chats (caps);
123 callback (supported, callback_data);
127 empathy_new_message_dialog_update_sms_button_sensitivity (GtkWidget *widget,
131 GtkWidget *self = gtk_widget_get_toplevel (widget);
132 EmpathyContactSelectorDialog *dialog;
135 gboolean sensitive = FALSE;
138 g_return_if_fail (EMPATHY_IS_NEW_MESSAGE_DIALOG (self));
140 dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
142 /* if the Text widget isn't sensitive, don't bother checking the caps */
143 if (!gtk_widget_get_sensitive (dialog->button_action))
146 empathy_contact_selector_dialog_get_selected (dialog, &conn, NULL);
151 /* iterate the rccs to find if SMS channels are supported, this should
153 rccs = tp_capabilities_get_channel_classes (
154 tp_connection_get_capabilities (conn));
156 for (i = 0; i < rccs->len; i++)
161 gboolean sms_channel;
163 tp_value_array_unpack (g_ptr_array_index (rccs, i), 2,
167 /* SMS channels are type:Text and sms-channel:True */
168 type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
169 sms_channel = tp_asv_get_boolean (fixed,
170 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL);
172 sensitive = sms_channel &&
173 !tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_TEXT);
180 gtk_widget_set_sensitive (button, sensitive);
184 empathy_new_message_dialog_constructor (GType type,
186 GObjectConstructParam *props)
190 if (dialog_singleton)
192 retval = G_OBJECT (dialog_singleton);
193 g_object_ref (retval);
197 retval = G_OBJECT_CLASS (
198 empathy_new_message_dialog_parent_class)->constructor (type,
201 dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
202 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
209 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
211 EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
216 /* add an SMS button */
217 button = gtk_button_new_with_mnemonic (_("_SMS"));
218 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
219 GTK_ICON_SIZE_BUTTON);
220 gtk_button_set_image (GTK_BUTTON (button), image);
222 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
223 EMP_NEW_MESSAGE_SMS);
224 gtk_widget_show (button);
226 /* add chat button */
227 parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
228 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
229 GTK_ICON_SIZE_BUTTON);
230 gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
232 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
233 EMP_NEW_MESSAGE_TEXT);
234 gtk_widget_show (parent->button_action);
236 /* the parent class will update the sensitivity of button_action, propagate
238 g_signal_connect (parent->button_action, "notify::sensitive",
239 G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
241 g_signal_connect (dialog, "notify::selected-account",
242 G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
245 /* Tweak the dialog */
246 gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
247 gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
249 gtk_widget_set_sensitive (parent->button_action, FALSE);
253 empathy_new_message_dialog_class_init (
254 EmpathyNewMessageDialogClass *class)
256 GObjectClass *object_class = G_OBJECT_CLASS (class);
257 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
258 EmpathyContactSelectorDialogClass *selector_dialog_class = \
259 EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
261 object_class->constructor = empathy_new_message_dialog_constructor;
263 dialog_class->response = empathy_new_message_dialog_response;
265 selector_dialog_class->account_filter = empathy_new_message_account_filter;
269 * empathy_new_message_dialog_new:
270 * @parent: parent #GtkWindow of the dialog
272 * Create a new #EmpathyNewMessageDialog it.
274 * Return value: the new #EmpathyNewMessageDialog
277 empathy_new_message_dialog_show (GtkWindow *parent)
281 dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
285 gtk_window_set_transient_for (GTK_WINDOW (dialog),
286 GTK_WINDOW (parent));
289 gtk_widget_show (dialog);