Display a context menu when clicking on audio/video icons (#590051)
[empathy-mirror.git] / libempathy-gtk / empathy-new-call-dialog.c
blob1a171af5a10d93b15fb4132ce393afe9a46ee9e2
1 /*
2 * Copyright (C) 2009 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: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
21 #include <config.h>
23 #include <string.h>
24 #include <stdlib.h>
26 #include <gtk/gtk.h>
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-call-factory.h>
34 #include <libempathy/empathy-dispatcher.h>
35 #include <libempathy/empathy-utils.h>
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include <libempathy/empathy-debug.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <libempathy-gtk/empathy-images.h>
43 #include "empathy-new-call-dialog.h"
44 #include "empathy-account-chooser.h"
46 static EmpathyNewCallDialog *dialog_singleton = NULL;
48 G_DEFINE_TYPE(EmpathyNewCallDialog, empathy_new_call_dialog,
49 EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
51 typedef struct _EmpathyNewCallDialogPriv EmpathyNewCallDialogPriv;
53 struct _EmpathyNewCallDialogPriv {
54 GtkWidget *check_video;
57 #define GET_PRIV(o) \
58 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_NEW_CALL_DIALOG, \
59 EmpathyNewCallDialogPriv))
61 /**
62 * SECTION:empathy-new-call-dialog
63 * @title: EmpathyNewCallDialog
64 * @short_description: A dialog to show a new call
65 * @include: libempathy-gtk/empathy-new-call-dialog.h
67 * #EmpathyNewCallDialog is a dialog which allows a call
68 * to be started with any contact on any enabled account.
71 static void
72 got_contact_cb (EmpathyTpContactFactory *factory,
73 EmpathyContact *contact,
74 const GError *error,
75 gpointer user_data,
76 GObject *object)
78 EmpathyCallFactory *call_factory;
79 gboolean video = GPOINTER_TO_UINT (user_data);
81 if (error != NULL)
83 DEBUG ("Failed: %s", error->message);
84 return;
87 call_factory = empathy_call_factory_get ();
88 empathy_call_factory_new_call_with_streams (call_factory, contact, TRUE,
89 video);
92 static void
93 empathy_new_call_dialog_response (GtkDialog *dialog, int response_id)
95 EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
96 EmpathyTpContactFactory *factory;
97 gboolean video;
98 TpConnection *connection;
99 const gchar *contact_id;
101 if (response_id != GTK_RESPONSE_ACCEPT) goto out;
103 contact_id = empathy_contact_selector_dialog_get_selected (
104 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), &connection);
106 if (EMP_STR_EMPTY (contact_id) || connection == NULL) goto out;
108 /* check if video is enabled now because the dialog will be destroyed once
109 * we return from this function. */
110 video = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->check_video));
112 factory = empathy_tp_contact_factory_dup_singleton (connection);
113 empathy_tp_contact_factory_get_from_id (factory, contact_id,
114 got_contact_cb, GUINT_TO_POINTER (video), NULL, NULL);
116 g_object_unref (factory);
118 out:
119 gtk_widget_destroy (GTK_WIDGET (dialog));
122 static gboolean
123 empathy_new_call_dialog_account_filter (EmpathyContactSelectorDialog *dialog,
124 TpAccount *account)
126 TpConnection *connection;
127 EmpathyDispatcher *dispatcher;
128 GList *classes;
130 if (tp_account_get_connection_status (account, NULL) !=
131 TP_CONNECTION_STATUS_CONNECTED)
132 return FALSE;
134 /* check if CM supports calls */
135 connection = tp_account_get_connection (account);
136 if (connection == NULL)
137 return FALSE;
139 dispatcher = empathy_dispatcher_dup_singleton ();
141 classes = empathy_dispatcher_find_requestable_channel_classes
142 (dispatcher, connection, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
143 TP_HANDLE_TYPE_CONTACT, NULL);
145 g_object_unref (dispatcher);
147 if (classes == NULL)
148 return FALSE;
150 g_list_free (classes);
151 return TRUE;
154 static GObject *
155 empathy_new_call_dialog_constructor (GType type,
156 guint n_props,
157 GObjectConstructParam *props)
159 GObject *retval;
161 if (dialog_singleton)
163 retval = G_OBJECT (dialog_singleton);
164 g_object_ref (retval);
166 else
168 retval = G_OBJECT_CLASS (
169 empathy_new_call_dialog_parent_class)->constructor (type,
170 n_props, props);
172 dialog_singleton = EMPATHY_NEW_CALL_DIALOG (retval);
173 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
176 return retval;
179 static void
180 empathy_new_call_dialog_init (EmpathyNewCallDialog *dialog)
182 EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
183 dialog);
184 EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
185 GtkWidget *image;
187 /* add video toggle */
188 priv->check_video = gtk_check_button_new_with_mnemonic (_("Send _Video"));
190 gtk_box_pack_end (GTK_BOX (parent->vbox), priv->check_video,
191 FALSE, TRUE, 0);
193 gtk_widget_show (priv->check_video);
195 /* add chat button */
196 parent->button_action = gtk_button_new_with_mnemonic (_("_Call"));
197 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
198 GTK_ICON_SIZE_BUTTON);
199 gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
201 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
202 GTK_RESPONSE_ACCEPT);
203 gtk_widget_show (parent->button_action);
205 /* Tweak the dialog */
206 gtk_window_set_title (GTK_WINDOW (dialog), _("New Call"));
207 gtk_window_set_role (GTK_WINDOW (dialog), "new_call");
209 gtk_widget_set_sensitive (parent->button_action, FALSE);
212 static void
213 empathy_new_call_dialog_class_init (
214 EmpathyNewCallDialogClass *class)
216 GObjectClass *object_class = G_OBJECT_CLASS (class);
217 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
218 EmpathyContactSelectorDialogClass *selector_dialog_class = \
219 EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
221 g_type_class_add_private (class, sizeof (EmpathyNewCallDialogPriv));
223 object_class->constructor = empathy_new_call_dialog_constructor;
225 dialog_class->response = empathy_new_call_dialog_response;
227 selector_dialog_class->account_filter = empathy_new_call_dialog_account_filter;
231 * empathy_new_call_dialog_new:
232 * @parent: parent #GtkWindow of the dialog
234 * Create a new #EmpathyNewCallDialog it.
236 * Return value: the new #EmpathyNewCallDialog
238 GtkWidget *
239 empathy_new_call_dialog_show (GtkWindow *parent)
241 GtkWidget *dialog;
243 dialog = g_object_new (EMPATHY_TYPE_NEW_CALL_DIALOG, NULL);
245 if (parent)
247 gtk_window_set_transient_for (GTK_WINDOW (dialog),
248 GTK_WINDOW (parent));
251 gtk_widget_show (dialog);
252 return dialog;