]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-call-dialog.c
Updated Persian translation
[empathy.git] / libempathy-gtk / empathy-new-call-dialog.c
1 /*
2  * Copyright (C) 2009 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: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
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 <telepathy-yell/telepathy-yell.h>
32
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-camera-monitor.h>
35 #include <libempathy/empathy-contact-manager.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-request-util.h>
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
40 #include <libempathy/empathy-debug.h>
41
42 #include <libempathy-gtk/empathy-ui-utils.h>
43 #include <libempathy-gtk/empathy-images.h>
44
45 #include "empathy-new-call-dialog.h"
46 #include "empathy-account-chooser.h"
47 #include "empathy-call-utils.h"
48
49 static EmpathyNewCallDialog *dialog_singleton = NULL;
50
51 G_DEFINE_TYPE(EmpathyNewCallDialog, empathy_new_call_dialog,
52                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
53
54 typedef struct _EmpathyNewCallDialogPriv EmpathyNewCallDialogPriv;
55
56 typedef struct {
57   EmpathyAccountChooserFilterResultCallback callback;
58   gpointer                                  user_data;
59 } FilterCallbackData;
60
61 struct _EmpathyNewCallDialogPriv {
62   GtkWidget *check_video;
63
64   EmpathyCameraMonitor *monitor;
65 };
66
67 #define GET_PRIV(o) \
68   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_NEW_CALL_DIALOG, \
69     EmpathyNewCallDialogPriv))
70
71 /**
72  * SECTION:empathy-new-call-dialog
73  * @title: EmpathyNewCallDialog
74  * @short_description: A dialog to show a new call
75  * @include: libempathy-gtk/empathy-new-call-dialog.h
76  *
77  * #EmpathyNewCallDialog is a dialog which allows a call
78  * to be started with any contact on any enabled account.
79  */
80
81 static void
82 empathy_new_call_dialog_response (GtkDialog *dialog, int response_id)
83 {
84   EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
85   gboolean video;
86   TpAccount *account;
87   const gchar *contact_id;
88
89   if (response_id != GTK_RESPONSE_ACCEPT) goto out;
90
91   contact_id = empathy_contact_selector_dialog_get_selected (
92       EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
93
94   if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
95
96   /* check if video is enabled now because the dialog will be destroyed once
97    * we return from this function. */
98   video = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->check_video));
99
100   empathy_call_new_with_streams (contact_id,
101       account, TRUE, video,
102       empathy_get_current_action_time ());
103
104 out:
105   gtk_widget_destroy (GTK_WIDGET (dialog));
106 }
107
108 static void
109 empathy_new_call_dialog_account_filter (EmpathyContactSelectorDialog *dialog,
110     EmpathyAccountChooserFilterResultCallback callback,
111     gpointer callback_data,
112     TpAccount *account)
113 {
114   TpConnection *connection;
115   gboolean supported = FALSE;
116   guint i;
117   TpCapabilities *caps;
118   GPtrArray *classes;
119
120   /* check if CM supports calls */
121   connection = tp_account_get_connection (account);
122   if (connection == NULL)
123       goto out;
124
125   caps = tp_connection_get_capabilities (connection);
126   if (caps == NULL)
127       goto out;
128
129   classes = tp_capabilities_get_channel_classes (caps);
130
131   for (i = 0; i < classes->len; i++)
132     {
133       GHashTable *fixed;
134       GStrv allowed;
135       const gchar *chan_type;
136
137       tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
138           &fixed, &allowed);
139
140       chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
141
142       if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)
143           && tp_strdiff (chan_type, TPY_IFACE_CHANNEL_TYPE_CALL))
144         continue;
145
146       if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
147           TP_HANDLE_TYPE_CONTACT)
148         continue;
149
150       supported = TRUE;
151       break;
152     }
153
154 out:
155   callback (supported, callback_data);
156 }
157
158 static void
159 empathy_new_call_dialog_dispose (GObject *object)
160 {
161   EmpathyNewCallDialogPriv *priv = GET_PRIV (object);
162
163   tp_clear_object (&priv->monitor);
164
165   G_OBJECT_CLASS (empathy_new_call_dialog_parent_class)->dispose (object);
166 }
167
168 static GObject *
169 empathy_new_call_dialog_constructor (GType type,
170     guint n_props,
171     GObjectConstructParam *props)
172 {
173   GObject *retval;
174
175   if (dialog_singleton)
176     {
177       retval = G_OBJECT (dialog_singleton);
178       g_object_ref (retval);
179     }
180   else
181     {
182       retval = G_OBJECT_CLASS (
183       empathy_new_call_dialog_parent_class)->constructor (type,
184         n_props, props);
185
186       dialog_singleton = EMPATHY_NEW_CALL_DIALOG (retval);
187       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
188     }
189
190   return retval;
191 }
192
193 static void
194 empathy_new_call_dialog_init (EmpathyNewCallDialog *dialog)
195 {
196   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
197         dialog);
198   EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
199   GtkWidget *image;
200
201   priv->monitor = empathy_camera_monitor_dup_singleton ();
202
203   /* add video toggle */
204   priv->check_video = gtk_check_button_new_with_mnemonic (_("Send _Video"));
205   g_object_bind_property (priv->monitor, "available",
206       priv->check_video, "sensitive",
207       G_BINDING_SYNC_CREATE);
208
209   gtk_box_pack_end (GTK_BOX (parent->vbox), priv->check_video,
210       FALSE, TRUE, 0);
211
212   gtk_widget_show (priv->check_video);
213
214   /* add chat button */
215   parent->button_action = gtk_button_new_with_mnemonic (_("C_all"));
216   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
217       GTK_ICON_SIZE_BUTTON);
218   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
219
220   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
221       GTK_RESPONSE_ACCEPT);
222   gtk_widget_show (parent->button_action);
223
224   /* Tweak the dialog */
225   gtk_window_set_title (GTK_WINDOW (dialog), _("New Call"));
226   gtk_window_set_role (GTK_WINDOW (dialog), "new_call");
227
228   gtk_widget_set_sensitive (parent->button_action, FALSE);
229 }
230
231 static void
232 empathy_new_call_dialog_class_init (
233   EmpathyNewCallDialogClass *class)
234 {
235   GObjectClass *object_class = G_OBJECT_CLASS (class);
236   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
237   EmpathyContactSelectorDialogClass *selector_dialog_class = \
238     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
239
240   g_type_class_add_private (class, sizeof (EmpathyNewCallDialogPriv));
241
242   object_class->constructor = empathy_new_call_dialog_constructor;
243   object_class->dispose = empathy_new_call_dialog_dispose;
244
245   dialog_class->response = empathy_new_call_dialog_response;
246
247   selector_dialog_class->account_filter = empathy_new_call_dialog_account_filter;
248 }
249
250 /**
251  * empathy_new_call_dialog_new:
252  * @parent: parent #GtkWindow of the dialog
253  *
254  * Create a new #EmpathyNewCallDialog it.
255  *
256  * Return value: the new #EmpathyNewCallDialog
257  */
258 GtkWidget *
259 empathy_new_call_dialog_show (GtkWindow *parent)
260 {
261   GtkWidget *dialog;
262
263   dialog = g_object_new (EMPATHY_TYPE_NEW_CALL_DIALOG, NULL);
264
265   if (parent)
266     {
267       gtk_window_set_transient_for (GTK_WINDOW (dialog),
268                   GTK_WINDOW (parent));
269     }
270
271   gtk_widget_show (dialog);
272   return dialog;
273 }