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