]> git.0d.be Git - empathy.git/blob - src/empathy-call.c
Merge branch 'gnome-3-6'
[empathy.git] / src / empathy-call.c
1 /*
2  * Copyright (C) 2007-2011 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program 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  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include <clutter/clutter.h>
30 #include <clutter-gtk/clutter-gtk.h>
31 #include <clutter-gst/clutter-gst.h>
32
33 #ifdef CLUTTER_WINDOWING_X11
34 #include <X11/Xlib.h>
35 #endif
36
37 #include <libempathy/empathy-client-factory.h>
38
39 #include <libempathy-gtk/empathy-ui-utils.h>
40
41 #include "empathy-call-window.h"
42 #include "empathy-call-factory.h"
43
44 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
45 #include <libempathy/empathy-debug.h>
46
47 #include <gst/gst.h>
48
49 /* Exit after $TIMEOUT seconds if not displaying any call window */
50 #define TIMEOUT 60
51
52 #define EMPATHY_CALL_DBUS_NAME "org.gnome.Empathy.Call"
53
54 static GtkApplication *app = NULL;
55 static gboolean activated = FALSE;
56 static gboolean use_timer = TRUE;
57 static guint inhibit_id = 0;
58
59 static EmpathyCallFactory *call_factory = NULL;
60
61 /* An EmpathyContact -> EmpathyCallWindow hash table for all existing
62  * Call windows. We own a ref on the EmpathyContacts. */
63 static GHashTable *call_windows;
64
65 static void
66 call_window_destroyed_cb (GtkWidget *window,
67     EmpathyContact *contact)
68 {
69   g_hash_table_remove (call_windows, contact);
70
71   g_application_release (G_APPLICATION (app));
72 }
73
74 static gboolean
75 find_window_for_handle (gpointer key,
76     gpointer value,
77     gpointer user_data)
78 {
79   EmpathyContact *contact = key;
80   guint handle = GPOINTER_TO_UINT (user_data);
81
82   if (handle == empathy_contact_get_handle (contact))
83     return TRUE;
84
85   return FALSE;
86 }
87
88 static gboolean
89 incoming_call_cb (EmpathyCallFactory *factory,
90     guint handle,
91     TpCallChannel *channel,
92     TpChannelDispatchOperation *dispatch_operation,
93     TpAddDispatchOperationContext *context,
94     gpointer user_data)
95 {
96   EmpathyCallWindow *window = g_hash_table_find (call_windows,
97       find_window_for_handle, GUINT_TO_POINTER (handle));
98
99   if (window != NULL)
100     {
101       /* The window takes care of accepting or rejecting the context. */
102       empathy_call_window_start_ringing (window,
103           channel, dispatch_operation, context);
104       return TRUE;
105     }
106
107   return FALSE;
108 }
109
110 static void
111 call_window_inhibit_cb (EmpathyCallWindow *window,
112     gboolean inhibit,
113     gpointer user_data)
114 {
115   if (inhibit)
116     {
117       if (inhibit_id != 0)
118         return;
119
120       inhibit_id = gtk_application_inhibit (GTK_APPLICATION (app),
121           GTK_WINDOW (window),
122           GTK_APPLICATION_INHIBIT_LOGOUT | GTK_APPLICATION_INHIBIT_SWITCH |
123           GTK_APPLICATION_INHIBIT_SUSPEND | GTK_APPLICATION_INHIBIT_IDLE,
124           _("In a call"));
125     }
126   else
127     {
128       if (inhibit_id == 0)
129         return;
130
131       gtk_application_uninhibit (GTK_APPLICATION (app), inhibit_id);
132       inhibit_id = 0;
133     }
134 }
135
136 static void
137 new_call_handler_cb (EmpathyCallFactory *factory,
138     EmpathyCallHandler *handler,
139     gboolean outgoing,
140     gpointer user_data)
141 {
142   EmpathyCallWindow *window;
143   EmpathyContact *contact;
144
145   DEBUG ("Show the call window");
146
147   contact = empathy_call_handler_get_contact (handler);
148
149   window = g_hash_table_lookup (call_windows, contact);
150
151   if (window != NULL)
152     {
153       empathy_call_window_present (window, handler);
154     }
155   else
156     {
157       window = empathy_call_window_new (handler);
158
159       g_hash_table_insert (call_windows, g_object_ref (contact), window);
160       g_application_hold (G_APPLICATION (app));
161       g_signal_connect (window, "destroy",
162           G_CALLBACK (call_window_destroyed_cb), contact);
163       g_signal_connect (window, "inhibit",
164           G_CALLBACK (call_window_inhibit_cb), NULL);
165
166       gtk_widget_show (GTK_WIDGET (window));
167     }
168 }
169
170 static void
171 activate_cb (GApplication *application)
172 {
173   GError *error = NULL;
174
175   if (activated)
176     return;
177
178   activated = TRUE;
179
180   if (!use_timer)
181     {
182       /* keep a 'ref' to the application */
183       g_application_hold (G_APPLICATION (app));
184     }
185
186   g_assert (call_factory == NULL);
187   call_factory = empathy_call_factory_initialise ();
188
189   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
190       G_CALLBACK (new_call_handler_cb), NULL);
191   g_signal_connect (G_OBJECT (call_factory), "incoming-call",
192       G_CALLBACK (incoming_call_cb), NULL);
193
194   if (!empathy_call_factory_register (call_factory, &error))
195     {
196       g_critical ("Failed to register Handler: %s", error->message);
197       g_error_free (error);
198     }
199 }
200
201 int
202 main (int argc,
203     char *argv[])
204 {
205   GOptionContext *optcontext;
206   GOptionEntry options[] = {
207       { NULL }
208   };
209 #ifdef ENABLE_DEBUG
210   TpDebugSender *debug_sender;
211 #endif
212   GError *error = NULL;
213   gint retval;
214   GtkSettings *gtk_settings;
215
216   g_setenv ("GST_DEBUG_DUMP_DOT_DIR", g_get_tmp_dir (), FALSE);
217
218 #ifdef GDK_WINDOWING_X11
219   /* We can't call clutter_gst_init() before gtk_clutter_init(), so no choice
220    * but to intiialise X11 threading ourself */
221   XInitThreads ();
222 #endif
223
224   optcontext = g_option_context_new (N_("- Empathy Audio/Video Client"));
225   g_option_context_add_group (optcontext, gst_init_get_option_group ());
226   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
227   g_option_context_add_group (optcontext, cogl_get_option_group ());
228   g_option_context_add_group (optcontext,
229       clutter_get_option_group_without_init ());
230   g_option_context_add_group (optcontext, gtk_clutter_get_option_group ());
231   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
232   g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
233
234   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
235     g_print ("%s\nRun '%s --help' to see a full list of available command "
236         "line options.\n",
237         error->message, argv[0]);
238     g_warning ("Error in empathy-call init: %s", error->message);
239     return EXIT_FAILURE;
240   }
241
242   g_option_context_free (optcontext);
243
244   clutter_gst_init (&argc, &argv);
245
246   empathy_gtk_init ();
247   textdomain (GETTEXT_PACKAGE);
248   g_set_application_name (_("Empathy Audio/Video Client"));
249
250   /* Make empathy and empathy-call appear as the same app in gnome-shell */
251   gdk_set_program_class ("Empathy");
252   gtk_window_set_default_icon_name ("empathy");
253
254   gtk_settings = gtk_settings_get_default ();
255   g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme",
256       TRUE, NULL);
257
258   app = gtk_application_new (EMPATHY_CALL_DBUS_NAME, G_APPLICATION_FLAGS_NONE);
259   g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);
260
261 #ifdef ENABLE_DEBUG
262   /* Set up debug sender */
263   debug_sender = tp_debug_sender_dup ();
264   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
265 #endif
266
267   if (g_getenv ("EMPATHY_PERSIST") != NULL)
268     {
269       DEBUG ("Disable timer");
270
271       use_timer = FALSE;
272     }
273
274   call_windows = g_hash_table_new_full (g_direct_hash, g_direct_equal,
275       g_object_unref, NULL);
276
277   /* the inactivity timeout can only be set while the application is held */
278   g_application_hold (G_APPLICATION (app));
279   g_application_set_inactivity_timeout (G_APPLICATION (app), TIMEOUT * 1000);
280   g_application_release (G_APPLICATION (app));
281
282   retval = g_application_run (G_APPLICATION (app), argc, argv);
283
284   g_hash_table_unref (call_windows);
285   g_object_unref (app);
286   tp_clear_object (&call_factory);
287
288 #ifdef ENABLE_DEBUG
289   g_object_unref (debug_sender);
290 #endif
291
292   return retval;
293 }