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