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