]> git.0d.be Git - empathy.git/blob - src/empathy.c
Toss out the old EmpathyCallWindow initialise the CallFactory and hook into its signals
[empathy.git] / src / empathy.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkx.h>
33
34 #include <libebook/e-book.h>
35 #include <libnotify/notify.h>
36
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mc-account.h>
39 #include <libmissioncontrol/mission-control.h>
40
41 #include <libempathy/empathy-idle.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-call-factory.h>
44 #include <libempathy/empathy-chatroom-manager.h>
45 #include <libempathy/empathy-dispatcher.h>
46 #include <libempathy/empathy-dispatch-operation.h>
47 #include <libempathy/empathy-tp-chat.h>
48 #include <libempathy/empathy-tp-call.h>
49
50 #include <libempathy-gtk/empathy-conf.h>
51 #include <libempathy-gtk/empathy-ui-utils.h>
52
53 #include "empathy-accounts-dialog.h"
54 #include "empathy-main-window.h"
55 #include "empathy-status-icon.h"
56 #include "empathy-call-window.h"
57 #include "empathy-chat-window.h"
58 #include "empathy-ft-manager.h"
59 #include "bacon-message-connection.h"
60
61 #include "extensions/extensions.h"
62
63 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
64 #include <libempathy/empathy-debug.h>
65
66 #include <gst/gst.h>
67
68 static BaconMessageConnection *connection = NULL;
69
70 static void
71 dispatch_cb (EmpathyDispatcher *dispatcher,
72              EmpathyDispatchOperation *operation,
73              gpointer           user_data)
74 {
75         GQuark channel_type;
76
77         channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
78
79         if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
80                 EmpathyTpChat *tp_chat;
81                 EmpathyChat   *chat = NULL;
82                 const gchar   *id;
83
84                 tp_chat = EMPATHY_TP_CHAT (
85                         empathy_dispatch_operation_get_channel_wrapper (operation));
86
87                 id = empathy_tp_chat_get_id (tp_chat);
88                 if (!id) {
89                         EmpathyContact *contact;
90
91                         contact = empathy_tp_chat_get_remote_contact (tp_chat);
92                         if (contact) {
93                                 id = empathy_contact_get_id (contact);
94                         }
95                 }
96
97                 if (id) {
98                         McAccount *account;
99
100                         account = empathy_tp_chat_get_account (tp_chat);
101                         chat = empathy_chat_window_find_chat (account, id);
102                 }
103
104                 if (chat) {
105                         empathy_chat_set_tp_chat (chat, tp_chat);
106                 } else {
107                         chat = empathy_chat_new (tp_chat);
108                 }
109
110                 empathy_chat_window_present_chat (chat);
111
112                 empathy_dispatch_operation_claim (operation);
113         } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
114                 EmpathyCallFactory *factory;
115
116                 factory = empathy_call_factory_get ();
117                 empathy_call_factory_claim_channel (factory, operation);
118         } else if (channel_type == EMP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
119                 EmpathyFTManager *ft_manager;
120                 EmpathyTpFile    *tp_file;
121
122                 ft_manager = empathy_ft_manager_dup_singleton ();
123                 tp_file = EMPATHY_TP_FILE (
124                         empathy_dispatch_operation_get_channel_wrapper (operation));
125                 empathy_ft_manager_add_tp_file (ft_manager, tp_file);
126                 empathy_dispatch_operation_claim (operation);
127         }
128 }
129
130 static void
131 service_ended_cb (MissionControl *mc,
132                   gpointer        user_data)
133 {
134         DEBUG ("Mission Control stopped");
135 }
136
137 static void
138 operation_error_cb (MissionControl *mc,
139                     guint           operation_id,
140                     guint           error_code,
141                     gpointer        user_data)
142 {
143         const gchar *message;
144
145         switch (error_code) {
146         case MC_DISCONNECTED_ERROR:
147                 message = "Disconnected";
148                 break;
149         case MC_INVALID_HANDLE_ERROR:
150                 message = "Invalid handle";
151                 break;
152         case MC_NO_MATCHING_CONNECTION_ERROR:
153                 message = "No matching connection";
154                 break;
155         case MC_INVALID_ACCOUNT_ERROR:
156                 message = "Invalid account";
157                 break;
158         case MC_PRESENCE_FAILURE_ERROR:
159                 message = "Presence failure";
160                 break;
161         case MC_NO_ACCOUNTS_ERROR:
162                 message = "No accounts";
163                 break;
164         case MC_NETWORK_ERROR:
165                 message = "Network error";
166                 break;
167         case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
168                 message = "Contact does not support voice";
169                 break;
170         case MC_LOWMEM_ERROR:
171                 message = "Lowmem";
172                 break;
173         case MC_CHANNEL_REQUEST_GENERIC_ERROR:
174                 message = "Channel request generic error";
175                 break;
176         case MC_CHANNEL_BANNED_ERROR:
177                 message = "Channel banned";
178                 break;
179         case MC_CHANNEL_FULL_ERROR:
180                 message = "Channel full";
181                 break;
182         case MC_CHANNEL_INVITE_ONLY_ERROR:
183                 message = "Channel invite only";
184                 break;
185         default:
186                 message = "Unknown error code";
187         }
188
189         DEBUG ("Error during operation %d: %s", operation_id, message);
190 }
191
192 static void
193 use_nm_notify_cb (EmpathyConf *conf,
194                   const gchar *key,
195                   gpointer     user_data)
196 {
197         EmpathyIdle *idle = user_data;
198         gboolean     use_nm;
199
200         if (empathy_conf_get_bool (conf, key, &use_nm)) {
201                 empathy_idle_set_use_nm (idle, use_nm);
202         }
203 }
204
205 static void
206 create_salut_account (void)
207 {
208         McProfile  *profile;
209         McProtocol *protocol;
210         gboolean    salut_created = FALSE;
211         McAccount  *account;
212         GList      *accounts;
213         EBook      *book;
214         EContact   *contact;
215         gchar      *nickname = NULL;
216         gchar      *first_name = NULL;
217         gchar      *last_name = NULL;
218         gchar      *email = NULL;
219         gchar      *jid = NULL;
220         GError     *error = NULL;
221
222         /* Check if we already created a salut account */
223         empathy_conf_get_bool (empathy_conf_get(),
224                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
225                                &salut_created);
226         if (salut_created) {
227                 return;
228         }
229
230         DEBUG ("Try to add a salut account...");
231
232         /* Check if the salut CM is installed */
233         profile = mc_profile_lookup ("salut");
234         if (!profile) {
235                 DEBUG ("No salut profile");
236                 return;
237         }
238         protocol = mc_profile_get_protocol (profile);
239         if (!protocol) {
240                 DEBUG ("Salut not installed");
241                 g_object_unref (profile);
242                 return;
243         }
244         g_object_unref (protocol);
245
246         /* Get self EContact from EDS */
247         if (!e_book_get_self (&contact, &book, &error)) {
248                 DEBUG ("Failed to get self econtact: %s",
249                         error ? error->message : "No error given");
250                 g_clear_error (&error);
251                 g_object_unref (profile);
252                 return;
253         }
254
255         empathy_conf_set_bool (empathy_conf_get (),
256                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
257                                TRUE);
258
259         /* Check if there is already a salut account */
260         accounts = mc_accounts_list_by_profile (profile);
261         if (accounts) {
262                 DEBUG ("There is already a salut account");
263                 mc_accounts_list_free (accounts);
264                 g_object_unref (profile);
265                 return;
266         }
267
268         account = mc_account_create (profile);
269         mc_account_set_display_name (account, _("People nearby"));
270         
271         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
272         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
273         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
274         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
275         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
276         
277         if (!tp_strdiff (nickname, "nickname")) {
278                 g_free (nickname);
279                 nickname = NULL;
280         }
281
282         DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
283                 "last-name=%s\nemail=%s\njid=%s\n",
284                 nickname, first_name, last_name, email, jid);
285
286         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
287         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
288         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
289         mc_account_set_param_string (account, "email", email ? email : "");
290         mc_account_set_param_string (account, "jid", jid ? jid : "");
291
292         g_free (nickname);
293         g_free (first_name);
294         g_free (last_name);
295         g_free (email);
296         g_free (jid);
297         g_object_unref (account);
298         g_object_unref (profile);
299         g_object_unref (contact);
300         g_object_unref (book);
301 }
302
303 /* The code that handles single-instance and startup notification is
304  * copied from gedit.
305  *
306  * Copyright (C) 2005 - Paolo Maggi 
307  */
308 static void
309 on_bacon_message_received (const char *message,
310                            gpointer    data)
311 {
312         GtkWidget *window = data;
313         guint32    startup_timestamp;
314
315         g_return_if_fail (message != NULL);
316
317         DEBUG ("Other instance launched, presenting the main window. message='%s'",
318                 message);
319
320         if (strcmp (message, "accounts") == 0) {
321                 /* accounts dialog requested */
322                 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
323         } else {
324                 startup_timestamp = atoi (message);
325
326                 /* Set the proper interaction time on the window.
327                  * Fall back to roundtripping to the X server when we
328                  * don't have the timestamp, e.g. when launched from
329                  * terminal. We also need to make sure that the window
330                  * has been realized otherwise it will not work. lame. */
331                 if (startup_timestamp == 0) {
332                         /* Work if launched from the terminal */
333                         DEBUG ("Using X server timestamp as a fallback");
334
335                         if (!GTK_WIDGET_REALIZED (window)) {
336                                 gtk_widget_realize (GTK_WIDGET (window));
337                         }
338
339                         startup_timestamp = gdk_x11_get_server_time (window->window);
340                 }
341
342                 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
343         }
344 }
345
346 static guint32
347 get_startup_timestamp ()
348 {
349         const gchar *startup_id_env;
350         gchar       *startup_id = NULL;
351         gchar       *time_str;
352         gchar       *end;
353         gulong       retval = 0;
354
355         /* we don't unset the env, since startup-notification
356          * may still need it */
357         startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
358         if (startup_id_env == NULL) {
359                 goto out;
360         }
361
362         startup_id = g_strdup (startup_id_env);
363
364         time_str = g_strrstr (startup_id, "_TIME");
365         if (time_str == NULL) {
366                 goto out;
367         }
368
369         errno = 0;
370
371         /* Skip past the "_TIME" part */
372         time_str += 5;
373
374         retval = strtoul (time_str, &end, 0);
375         if (end == time_str || errno != 0)
376                 retval = 0;
377
378  out:
379         g_free (startup_id);
380
381         return (retval > 0) ? retval : 0;
382 }
383
384 static gboolean
385 show_version_cb (const char *option_name,
386                  const char *value,
387                  gpointer data,
388                  GError **error)
389 {
390         g_print ("%s\n", PACKAGE_STRING);
391
392         exit (EXIT_SUCCESS);
393
394         return FALSE;
395 }
396
397 static void
398 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
399         gboolean outgoing, gpointer user_data)
400 {
401                 EmpathyCallWindow *window;
402
403                 window = empathy_call_window_new (handler);
404                 gtk_widget_show (GTK_WIDGET (window));
405 }
406
407 int
408 main (int argc, char *argv[])
409 {
410         guint32            startup_timestamp;
411         EmpathyStatusIcon *icon;
412         EmpathyDispatcher *dispatcher;
413         EmpathyChatroomManager *chatroom_manager;
414         EmpathyCallFactory *call_factory;
415         GtkWidget         *window;
416         MissionControl    *mc;
417         EmpathyIdle       *idle;
418         gboolean           autoconnect = TRUE;
419         gboolean           no_connect = FALSE; 
420         gboolean           hide_contact_list = FALSE;
421         gboolean           accounts_dialog = FALSE;
422         GError            *error = NULL;
423         GOptionEntry       options[] = {
424                 { "no-connect", 'n',
425                   0, G_OPTION_ARG_NONE, &no_connect,
426                   N_("Don't connect on startup"),
427                   NULL },
428                 { "hide-contact-list", 'h',
429                   0, G_OPTION_ARG_NONE, &hide_contact_list,
430                   N_("Don't show the contact list on startup"),
431                   NULL },
432                 { "accounts", 'a',
433                   0, G_OPTION_ARG_NONE, &accounts_dialog,
434                   N_("Show the accounts dialog"),
435                   NULL },
436                 { "version", 'v',
437                   G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
438                 { NULL }
439         };
440
441         /* Init */
442         g_thread_init (NULL);
443         empathy_init ();
444
445         if (!gtk_init_with_args (&argc, &argv,
446                                  N_("- Empathy Instant Messenger"),
447                                  options, GETTEXT_PACKAGE, &error)) {
448                 g_warning ("Error in empathy init: %s", error->message);
449                 return EXIT_FAILURE;
450         }
451
452         empathy_gtk_init ();
453         g_set_application_name (_(PACKAGE_NAME));
454
455         gst_init (&argc, &argv);
456
457         g_set_application_name (PACKAGE_NAME);
458         gtk_window_set_default_icon_name ("empathy");
459         textdomain (GETTEXT_PACKAGE);
460
461         /* Setting up the bacon connection */
462         startup_timestamp = get_startup_timestamp ();
463         connection = bacon_message_connection_new ("empathy");
464         if (connection != NULL) {
465                 if (!bacon_message_connection_get_is_server (connection)) {
466                         gchar *message;
467
468                         if (accounts_dialog) {
469                                 DEBUG ("Showing accounts dialog from existing Empathy instance");
470
471                                 message = g_strdup ("accounts");
472
473                         } else {
474
475                                 DEBUG ("Activating existing instance");
476
477                                 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
478                                                            startup_timestamp);
479                         }
480
481                         bacon_message_connection_send (connection, message);
482
483                         /* We never popup a window, so tell startup-notification
484                          * that we are done. */
485                         gdk_notify_startup_complete ();
486
487                         g_free (message);
488                         bacon_message_connection_free (connection);
489
490                         return EXIT_SUCCESS;
491                 }
492         } else {
493                 g_warning ("Cannot create the 'empathy' bacon connection.");
494         }
495
496         /* Setting up MC */
497         mc = empathy_mission_control_dup_singleton ();
498         g_signal_connect (mc, "ServiceEnded",
499                           G_CALLBACK (service_ended_cb),
500                           NULL);
501         g_signal_connect (mc, "Error",
502                           G_CALLBACK (operation_error_cb),
503                           NULL);
504
505         if (accounts_dialog) {
506                 GtkWidget *dialog;
507
508                 dialog = empathy_accounts_dialog_show (NULL, NULL);
509                 g_signal_connect (dialog, "destroy",
510                                   G_CALLBACK (gtk_main_quit),
511                                   NULL);
512
513                 gtk_main ();
514                 return 0;
515         }
516
517         /* Setting up Idle */
518         idle = empathy_idle_dup_singleton ();
519         empathy_idle_set_auto_away (idle, TRUE);
520         use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
521         empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
522                                  use_nm_notify_cb, idle);
523
524         /* Autoconnect */
525         empathy_conf_get_bool (empathy_conf_get(),
526                                EMPATHY_PREFS_AUTOCONNECT,
527                                &autoconnect);
528         if (autoconnect && ! no_connect &&
529             empathy_idle_get_state (idle) <= MC_PRESENCE_OFFLINE) {
530                 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
531         }
532         
533         create_salut_account ();
534
535         /* Setting up UI */
536         window = empathy_main_window_show ();
537         icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
538
539         if (connection) {
540                 /* We se the callback here because we need window */
541                 bacon_message_connection_set_callback (connection,
542                                                        on_bacon_message_received,
543                                                        window);
544         }
545
546         /* Handle channels */
547         dispatcher = empathy_dispatcher_dup_singleton ();
548         g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
549
550         chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
551         empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
552
553         notify_init (_(PACKAGE_NAME));
554         /* Create the call factory */
555         call_factory = empathy_call_factory_initialise ();
556         g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
557                 G_CALLBACK (new_call_handler_cb), NULL);
558
559         gtk_main ();
560
561         empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
562
563         g_object_unref (mc);
564         g_object_unref (idle);
565         g_object_unref (icon);
566         g_object_unref (dispatcher);
567         g_object_unref (chatroom_manager);
568
569         notify_uninit ();
570
571         return EXIT_SUCCESS;
572 }
573