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 get_error_display_message (GError *error)
69 if (error->domain != TP_ERROR)
74 case TP_ERROR_NETWORK_ERROR:
75 return _("Network error");
76 case TP_ERROR_OFFLINE:
77 return _("The contact is offline");
78 case TP_ERROR_INVALID_HANDLE:
79 return _("The specified contact is either invalid or unknown");
80 case TP_ERROR_NOT_CAPABLE:
81 return _("The contact does not support this kind of conversation");
82 case TP_ERROR_NOT_IMPLEMENTED:
83 return _("The requested functionality is not implemented "
85 case TP_ERROR_INVALID_ARGUMENT:
86 /* Not very user friendly to say 'invalid arguments' */
88 case TP_ERROR_NOT_AVAILABLE:
89 return _("Could not start a conversation with the given contact");
90 case TP_ERROR_CHANNEL_BANNED:
91 return _("You are banned from this channel");
92 case TP_ERROR_CHANNEL_FULL:
93 return _("This channel is full");
94 case TP_ERROR_CHANNEL_INVITE_ONLY:
95 return _("You must be invited to join this channel");
96 case TP_ERROR_DISCONNECTED:
97 return _("Can't proceed while disconnected");
98 case TP_ERROR_PERMISSION_DENIED:
99 return _("Permission denied");
101 DEBUG ("Unhandled error code: %d", error->code);
105 return _("There was an error starting the conversation");
109 show_chat_error (GError *error)
113 dialog = gtk_message_dialog_new (NULL, 0,
114 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
116 get_error_display_message (error));
118 g_signal_connect_swapped (dialog, "response",
119 G_CALLBACK (gtk_widget_destroy),
122 gtk_widget_show (dialog);
126 ensure_text_channel_cb (GObject *source,
127 GAsyncResult *result,
130 GError *error = NULL;
132 if (!tp_account_channel_request_ensure_channel_finish (
133 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
135 DEBUG ("Failed to ensure text channel: %s", error->message);
136 show_chat_error (error);
137 g_error_free (error);
142 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
145 const gchar *contact_id;
147 if (response_id < EMP_NEW_MESSAGE_TEXT) goto out;
149 contact_id = empathy_contact_selector_dialog_get_selected (
150 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
152 if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
156 case EMP_NEW_MESSAGE_TEXT:
157 empathy_chat_with_contact_id (account, contact_id,
158 empathy_get_current_action_time (),
159 ensure_text_channel_cb, NULL);
162 case EMP_NEW_MESSAGE_SMS:
163 empathy_sms_contact_id (account, contact_id,
164 empathy_get_current_action_time (),
165 ensure_text_channel_cb, NULL);
169 g_warn_if_reached ();
173 gtk_widget_destroy (GTK_WIDGET (dialog));
177 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
178 EmpathyAccountChooserFilterResultCallback callback,
179 gpointer callback_data,
182 TpConnection *connection;
183 gboolean supported = FALSE;
184 TpCapabilities *caps;
186 /* check if CM supports 1-1 text chat */
187 connection = tp_account_get_connection (account);
188 if (connection == NULL)
191 caps = tp_connection_get_capabilities (connection);
195 supported = tp_capabilities_supports_text_chats (caps);
198 callback (supported, callback_data);
202 empathy_new_message_dialog_update_sms_button_sensitivity (GtkWidget *widget,
206 GtkWidget *self = gtk_widget_get_toplevel (widget);
207 EmpathyContactSelectorDialog *dialog;
210 gboolean sensitive = FALSE;
213 g_return_if_fail (EMPATHY_IS_NEW_MESSAGE_DIALOG (self));
215 dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
217 /* if the Text widget isn't sensitive, don't bother checking the caps */
218 if (!gtk_widget_get_sensitive (dialog->button_action))
221 empathy_contact_selector_dialog_get_selected (dialog, &conn, NULL);
226 /* iterate the rccs to find if SMS channels are supported, this should
228 rccs = tp_capabilities_get_channel_classes (
229 tp_connection_get_capabilities (conn));
231 for (i = 0; i < rccs->len; i++)
236 gboolean sms_channel;
238 tp_value_array_unpack (g_ptr_array_index (rccs, i), 2,
242 /* SMS channels are type:Text and sms-channel:True */
243 type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
244 sms_channel = tp_asv_get_boolean (fixed,
245 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL);
247 sensitive = sms_channel &&
248 !tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_TEXT);
255 gtk_widget_set_sensitive (button, sensitive);
259 empathy_new_message_dialog_constructor (GType type,
261 GObjectConstructParam *props)
265 if (dialog_singleton)
267 retval = G_OBJECT (dialog_singleton);
268 g_object_ref (retval);
272 retval = G_OBJECT_CLASS (
273 empathy_new_message_dialog_parent_class)->constructor (type,
276 dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
277 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
284 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
286 EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
291 /* add an SMS button */
292 button = gtk_button_new_with_mnemonic (_("_SMS"));
293 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
294 GTK_ICON_SIZE_BUTTON);
295 gtk_button_set_image (GTK_BUTTON (button), image);
297 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
298 EMP_NEW_MESSAGE_SMS);
299 gtk_widget_show (button);
301 /* add chat button */
302 parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
303 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
304 GTK_ICON_SIZE_BUTTON);
305 gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
307 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
308 EMP_NEW_MESSAGE_TEXT);
309 gtk_widget_show (parent->button_action);
311 /* the parent class will update the sensitivity of button_action, propagate
313 g_signal_connect (parent->button_action, "notify::sensitive",
314 G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
316 g_signal_connect (dialog, "notify::selected-account",
317 G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
320 /* Tweak the dialog */
321 gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
322 gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
324 gtk_widget_set_sensitive (parent->button_action, FALSE);
328 empathy_new_message_dialog_class_init (
329 EmpathyNewMessageDialogClass *class)
331 GObjectClass *object_class = G_OBJECT_CLASS (class);
332 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
333 EmpathyContactSelectorDialogClass *selector_dialog_class = \
334 EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
336 object_class->constructor = empathy_new_message_dialog_constructor;
338 dialog_class->response = empathy_new_message_dialog_response;
340 selector_dialog_class->account_filter = empathy_new_message_account_filter;
344 * empathy_new_message_dialog_new:
345 * @parent: parent #GtkWindow of the dialog
347 * Create a new #EmpathyNewMessageDialog it.
349 * Return value: the new #EmpathyNewMessageDialog
352 empathy_new_message_dialog_show (GtkWindow *parent)
356 dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
360 gtk_window_set_transient_for (GTK_WINDOW (dialog),
361 GTK_WINDOW (parent));
364 gtk_widget_show (dialog);