]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
Merge branch 'trivia'
[empathy.git] / libempathy-gtk / empathy-new-message-dialog.c
1 /*
2  * Copyright (C) 2007-2008 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <gtk/gtk.h>
27 #include <glib/gi18n-lib.h>
28
29 #include <telepathy-glib/interfaces.h>
30
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>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
38
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-images.h>
41
42 #include "empathy-new-message-dialog.h"
43 #include "empathy-account-chooser.h"
44
45 static EmpathyNewMessageDialog *dialog_singleton = NULL;
46
47 G_DEFINE_TYPE(EmpathyNewMessageDialog, empathy_new_message_dialog,
48                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
49
50 /**
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
55  *
56  * #EmpathyNewMessageDialog is a dialog which allows a text chat
57  * to be started with any contact on any enabled account.
58  */
59
60 enum
61 {
62   EMP_NEW_MESSAGE_TEXT,
63   EMP_NEW_MESSAGE_SMS,
64 };
65
66 static void
67 empathy_new_message_dialog_response (GtkDialog *dialog, int response_id)
68 {
69   TpAccount *account;
70   const gchar *contact_id;
71
72   if (response_id < EMP_NEW_MESSAGE_TEXT) goto out;
73
74   contact_id = empathy_contact_selector_dialog_get_selected (
75       EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
76
77   if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
78
79   switch (response_id)
80     {
81       case EMP_NEW_MESSAGE_TEXT:
82         empathy_chat_with_contact_id (account, contact_id,
83             empathy_get_current_action_time ());
84         break;
85
86       case EMP_NEW_MESSAGE_SMS:
87         empathy_sms_contact_id (account, contact_id,
88             empathy_get_current_action_time ());
89         break;
90
91       default:
92         g_warn_if_reached ();
93     }
94
95 out:
96   gtk_widget_destroy (GTK_WIDGET (dialog));
97 }
98
99 static void
100 empathy_new_message_account_filter (EmpathyContactSelectorDialog *dialog,
101     EmpathyAccountChooserFilterResultCallback callback,
102     gpointer callback_data,
103     TpAccount *account)
104 {
105   TpConnection *connection;
106   gboolean supported = FALSE;
107   TpCapabilities *caps;
108
109   /* check if CM supports 1-1 text chat */
110   connection = tp_account_get_connection (account);
111   if (connection == NULL)
112       goto out;
113
114   caps = tp_connection_get_capabilities (connection);
115   if (caps == NULL)
116       goto out;
117
118   supported = tp_capabilities_supports_text_chats (caps);
119
120 out:
121   callback (supported, callback_data);
122 }
123
124 static void
125 empathy_new_message_dialog_update_sms_button_sensitivity (GtkWidget *widget,
126     GParamSpec *pspec,
127     GtkWidget *button)
128 {
129   GtkWidget *self = gtk_widget_get_toplevel (widget);
130   EmpathyContactSelectorDialog *dialog;
131   TpConnection *conn;
132   GPtrArray *rccs;
133   gboolean sensitive = FALSE;
134   guint i;
135
136   g_return_if_fail (EMPATHY_IS_NEW_MESSAGE_DIALOG (self));
137
138   dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
139
140   /* if the Text widget isn't sensitive, don't bother checking the caps */
141   if (!gtk_widget_get_sensitive (dialog->button_action))
142     goto finally;
143
144   empathy_contact_selector_dialog_get_selected (dialog, &conn, NULL);
145
146   if (conn == NULL)
147     goto finally;
148
149   /* iterate the rccs to find if SMS channels are supported, this should
150    * be in tp-glib */
151   rccs = tp_capabilities_get_channel_classes (
152       tp_connection_get_capabilities (conn));
153
154   for (i = 0; i < rccs->len; i++)
155     {
156       GHashTable *fixed;
157       GStrv allowed;
158       const char *type;
159       gboolean sms_channel;
160
161       tp_value_array_unpack (g_ptr_array_index (rccs, i), 2,
162           &fixed,
163           &allowed);
164
165       /* SMS channels are type:Text and sms-channel:True */
166       type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
167       sms_channel = tp_asv_get_boolean (fixed,
168           TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL);
169
170       sensitive = sms_channel &&
171         !tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_TEXT);
172
173       if (sensitive)
174         break;
175     }
176
177 finally:
178   gtk_widget_set_sensitive (button, sensitive);
179 }
180
181 static GObject *
182 empathy_new_message_dialog_constructor (GType type,
183     guint n_props,
184     GObjectConstructParam *props)
185 {
186   GObject *retval;
187
188   if (dialog_singleton)
189     {
190       retval = G_OBJECT (dialog_singleton);
191       g_object_ref (retval);
192     }
193   else
194     {
195       retval = G_OBJECT_CLASS (
196       empathy_new_message_dialog_parent_class)->constructor (type,
197         n_props, props);
198
199       dialog_singleton = EMPATHY_NEW_MESSAGE_DIALOG (retval);
200       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
201     }
202
203   return retval;
204 }
205
206 static void
207 empathy_new_message_dialog_init (EmpathyNewMessageDialog *dialog)
208 {
209   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
210         dialog);
211   GtkWidget *button;
212   GtkWidget *image;
213
214   /* add an SMS button */
215   button = gtk_button_new_with_mnemonic (_("_SMS"));
216   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
217       GTK_ICON_SIZE_BUTTON);
218   gtk_button_set_image (GTK_BUTTON (button), image);
219
220   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
221       EMP_NEW_MESSAGE_SMS);
222   gtk_widget_show (button);
223
224   /* add chat button */
225   parent->button_action = gtk_button_new_with_mnemonic (_("C_hat"));
226   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
227       GTK_ICON_SIZE_BUTTON);
228   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
229
230   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
231       EMP_NEW_MESSAGE_TEXT);
232   gtk_widget_show (parent->button_action);
233
234   /* the parent class will update the sensitivity of button_action, propagate
235    * it */
236   g_signal_connect (parent->button_action, "notify::sensitive",
237       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
238       button);
239   g_signal_connect (dialog, "notify::selected-account",
240       G_CALLBACK (empathy_new_message_dialog_update_sms_button_sensitivity),
241       button);
242
243   /* Tweak the dialog */
244   gtk_window_set_title (GTK_WINDOW (dialog), _("New Conversation"));
245   gtk_window_set_role (GTK_WINDOW (dialog), "new_message");
246
247   gtk_widget_set_sensitive (parent->button_action, FALSE);
248 }
249
250 static void
251 empathy_new_message_dialog_class_init (
252   EmpathyNewMessageDialogClass *class)
253 {
254   GObjectClass *object_class = G_OBJECT_CLASS (class);
255   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
256   EmpathyContactSelectorDialogClass *selector_dialog_class = \
257     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
258
259   object_class->constructor = empathy_new_message_dialog_constructor;
260
261   dialog_class->response = empathy_new_message_dialog_response;
262
263   selector_dialog_class->account_filter = empathy_new_message_account_filter;
264 }
265
266 /**
267  * empathy_new_message_dialog_new:
268  * @parent: parent #GtkWindow of the dialog
269  *
270  * Create a new #EmpathyNewMessageDialog it.
271  *
272  * Return value: the new #EmpathyNewMessageDialog
273  */
274 GtkWidget *
275 empathy_new_message_dialog_show (GtkWindow *parent)
276 {
277   GtkWidget *dialog;
278
279   dialog = g_object_new (EMPATHY_TYPE_NEW_MESSAGE_DIALOG, NULL);
280
281   if (parent)
282     {
283       gtk_window_set_transient_for (GTK_WINDOW (dialog),
284           GTK_WINDOW (parent));
285     }
286
287   gtk_widget_show (dialog);
288   return dialog;
289 }