]> git.0d.be Git - empathy.git/blob - src/empathy.c
Merge branch 'debugger'
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  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 #if HAVE_LIBCHAMPLAIN
35 #include <clutter-gtk/gtk-clutter-embed.h>
36 #endif
37
38 #include <libebook/e-book.h>
39 #include <libnotify/notify.h>
40
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/util.h>
43 #include <libmissioncontrol/mc-account.h>
44 #include <libmissioncontrol/mission-control.h>
45
46 #include <libempathy/empathy-idle.h>
47 #include <libempathy/empathy-utils.h>
48 #include <libempathy/empathy-call-factory.h>
49 #include <libempathy/empathy-chatroom-manager.h>
50 #include <libempathy/empathy-account-manager.h>
51 #include <libempathy/empathy-debugger.h>
52 #include <libempathy/empathy-dispatcher.h>
53 #include <libempathy/empathy-dispatch-operation.h>
54 #include <libempathy/empathy-log-manager.h>
55 #include <libempathy/empathy-ft-factory.h>
56 #include <libempathy/empathy-tp-chat.h>
57 #include <libempathy/empathy-tp-call.h>
58
59 #include <libempathy-gtk/empathy-conf.h>
60 #include <libempathy-gtk/empathy-ui-utils.h>
61 #include <libempathy-gtk/empathy-location-manager.h>
62
63 #include "empathy-accounts-dialog.h"
64 #include "empathy-main-window.h"
65 #include "empathy-status-icon.h"
66 #include "empathy-call-window.h"
67 #include "empathy-chat-window.h"
68 #include "empathy-ft-manager.h"
69 #include "bacon-message-connection.h"
70
71 #include "extensions/extensions.h"
72
73 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
74 #include <libempathy/empathy-debug.h>
75
76 #include <gst/gst.h>
77
78 static BaconMessageConnection *connection = NULL;
79
80 static void
81 dispatch_cb (EmpathyDispatcher *dispatcher,
82              EmpathyDispatchOperation *operation,
83              gpointer           user_data)
84 {
85         GQuark channel_type;
86
87         channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
88
89         if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
90                 EmpathyTpChat *tp_chat;
91                 EmpathyChat   *chat = NULL;
92                 const gchar   *id;
93
94                 tp_chat = EMPATHY_TP_CHAT (
95                         empathy_dispatch_operation_get_channel_wrapper (operation));
96
97                 id = empathy_tp_chat_get_id (tp_chat);
98                 if (!id) {
99                         EmpathyContact *contact;
100
101                         contact = empathy_tp_chat_get_remote_contact (tp_chat);
102                         if (contact) {
103                                 id = empathy_contact_get_id (contact);
104                         }
105                 }
106
107                 if (id) {
108                         EmpathyAccountManager *manager;
109                         TpConnection *connection;
110                         McAccount *account;
111
112                         manager = empathy_account_manager_dup_singleton ();
113                         connection = empathy_tp_chat_get_connection (tp_chat);
114                         account = empathy_account_manager_get_account (manager,
115                                                                        connection);
116                         chat = empathy_chat_window_find_chat (account, id);
117                         g_object_unref (manager);
118                 }
119
120                 if (chat) {
121                         empathy_chat_set_tp_chat (chat, tp_chat);
122                 } else {
123                         chat = empathy_chat_new (tp_chat);
124                 }
125
126                 empathy_chat_window_present_chat (chat);
127
128                 empathy_dispatch_operation_claim (operation);
129         } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
130                 EmpathyCallFactory *factory;
131
132                 factory = empathy_call_factory_get ();
133                 empathy_call_factory_claim_channel (factory, operation);
134         } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
135                 EmpathyFTFactory *factory;
136
137                 factory = empathy_ft_factory_dup_singleton ();
138
139                 /* if the operation is not incoming, don't claim it,
140                  * as it might have been triggered by another client, and
141                  * we are observing it.
142                  */
143                 if (empathy_dispatch_operation_is_incoming (operation)) {
144                         empathy_ft_factory_claim_channel (factory, operation);
145                 }
146         }
147 }
148
149 static void
150 service_ended_cb (MissionControl *mc,
151                   gpointer        user_data)
152 {
153         DEBUG ("Mission Control stopped");
154 }
155
156 static void
157 operation_error_cb (MissionControl *mc,
158                     guint           operation_id,
159                     guint           error_code,
160                     gpointer        user_data)
161 {
162         const gchar *message;
163
164         switch (error_code) {
165         case MC_DISCONNECTED_ERROR:
166                 message = "Disconnected";
167                 break;
168         case MC_INVALID_HANDLE_ERROR:
169                 message = "Invalid handle";
170                 break;
171         case MC_NO_MATCHING_CONNECTION_ERROR:
172                 message = "No matching connection";
173                 break;
174         case MC_INVALID_ACCOUNT_ERROR:
175                 message = "Invalid account";
176                 break;
177         case MC_PRESENCE_FAILURE_ERROR:
178                 message = "Presence failure";
179                 break;
180         case MC_NO_ACCOUNTS_ERROR:
181                 message = "No accounts";
182                 break;
183         case MC_NETWORK_ERROR:
184                 message = "Network error";
185                 break;
186         case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
187                 message = "Contact does not support voice";
188                 break;
189         case MC_LOWMEM_ERROR:
190                 message = "Lowmem";
191                 break;
192         case MC_CHANNEL_REQUEST_GENERIC_ERROR:
193                 message = "Channel request generic error";
194                 break;
195         case MC_CHANNEL_BANNED_ERROR:
196                 message = "Channel banned";
197                 break;
198         case MC_CHANNEL_FULL_ERROR:
199                 message = "Channel full";
200                 break;
201         case MC_CHANNEL_INVITE_ONLY_ERROR:
202                 message = "Channel invite only";
203                 break;
204         default:
205                 message = "Unknown error code";
206         }
207
208         DEBUG ("Error during operation %d: %s", operation_id, message);
209 }
210
211 static void
212 use_nm_notify_cb (EmpathyConf *conf,
213                   const gchar *key,
214                   gpointer     user_data)
215 {
216         EmpathyIdle *idle = user_data;
217         gboolean     use_nm;
218
219         if (empathy_conf_get_bool (conf, key, &use_nm)) {
220                 empathy_idle_set_use_nm (idle, use_nm);
221         }
222 }
223
224 static void
225 create_salut_account (void)
226 {
227         McProfile  *profile;
228         McProtocol *protocol;
229         gboolean    salut_created = FALSE;
230         McAccount  *account;
231         GList      *accounts;
232         EBook      *book;
233         EContact   *contact;
234         gchar      *nickname = NULL;
235         gchar      *first_name = NULL;
236         gchar      *last_name = NULL;
237         gchar      *email = NULL;
238         gchar      *jid = NULL;
239         GError     *error = NULL;
240
241         /* Check if we already created a salut account */
242         empathy_conf_get_bool (empathy_conf_get (),
243                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
244                                &salut_created);
245         if (salut_created) {
246                 return;
247         }
248
249         DEBUG ("Try to add a salut account...");
250
251         /* Check if the salut CM is installed */
252         profile = mc_profile_lookup ("salut");
253         if (!profile) {
254                 DEBUG ("No salut profile");
255                 return;
256         }
257         protocol = mc_profile_get_protocol (profile);
258         if (!protocol) {
259                 DEBUG ("Salut not installed");
260                 g_object_unref (profile);
261                 return;
262         }
263         g_object_unref (protocol);
264
265         /* Get self EContact from EDS */
266         if (!e_book_get_self (&contact, &book, &error)) {
267                 DEBUG ("Failed to get self econtact: %s",
268                         error ? error->message : "No error given");
269                 g_clear_error (&error);
270                 g_object_unref (profile);
271                 return;
272         }
273
274         empathy_conf_set_bool (empathy_conf_get (),
275                                EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
276                                TRUE);
277
278         /* Check if there is already a salut account */
279         accounts = mc_accounts_list_by_profile (profile);
280         if (accounts) {
281                 DEBUG ("There is already a salut account");
282                 mc_accounts_list_free (accounts);
283                 g_object_unref (profile);
284                 return;
285         }
286
287         account = mc_account_create (profile);
288         mc_account_set_display_name (account, _("People nearby"));
289
290         nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
291         first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
292         last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
293         email = e_contact_get (contact, E_CONTACT_EMAIL_1);
294         jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
295
296         if (!tp_strdiff (nickname, "nickname")) {
297                 g_free (nickname);
298                 nickname = NULL;
299         }
300
301         DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
302                 "last-name=%s\nemail=%s\njid=%s\n",
303                 nickname, first_name, last_name, email, jid);
304
305         mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
306         mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
307         mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
308         mc_account_set_param_string (account, "email", email ? email : "");
309         mc_account_set_param_string (account, "jid", jid ? jid : "");
310
311         g_free (nickname);
312         g_free (first_name);
313         g_free (last_name);
314         g_free (email);
315         g_free (jid);
316         g_object_unref (account);
317         g_object_unref (profile);
318         g_object_unref (contact);
319         g_object_unref (book);
320 }
321
322 /* The code that handles single-instance and startup notification is
323  * copied from gedit.
324  *
325  * Copyright (C) 2005 - Paolo Maggi
326  */
327 static void
328 on_bacon_message_received (const char *message,
329                            gpointer    data)
330 {
331         GtkWidget *window = data;
332         guint32    startup_timestamp;
333
334         g_return_if_fail (message != NULL);
335
336         DEBUG ("Other instance launched, presenting the main window. message='%s'",
337                 message);
338
339         if (strcmp (message, "accounts") == 0) {
340                 /* accounts dialog requested */
341                 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
342         } else {
343                 startup_timestamp = atoi (message);
344
345                 /* Set the proper interaction time on the window.
346                  * Fall back to roundtripping to the X server when we
347                  * don't have the timestamp, e.g. when launched from
348                  * terminal. We also need to make sure that the window
349                  * has been realized otherwise it will not work. lame. */
350                 if (startup_timestamp == 0) {
351                         /* Work if launched from the terminal */
352                         DEBUG ("Using X server timestamp as a fallback");
353
354                         if (!GTK_WIDGET_REALIZED (window)) {
355                                 gtk_widget_realize (GTK_WIDGET (window));
356                         }
357
358                         startup_timestamp = gdk_x11_get_server_time (window->window);
359                 }
360
361                 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
362         }
363 }
364
365 static guint32
366 get_startup_timestamp ()
367 {
368         const gchar *startup_id_env;
369         gchar       *startup_id = NULL;
370         gchar       *time_str;
371         gchar       *end;
372         gulong       retval = 0;
373
374         /* we don't unset the env, since startup-notification
375          * may still need it */
376         startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
377         if (startup_id_env == NULL) {
378                 goto out;
379         }
380
381         startup_id = g_strdup (startup_id_env);
382
383         time_str = g_strrstr (startup_id, "_TIME");
384         if (time_str == NULL) {
385                 goto out;
386         }
387
388         errno = 0;
389
390         /* Skip past the "_TIME" part */
391         time_str += 5;
392
393         retval = strtoul (time_str, &end, 0);
394         if (end == time_str || errno != 0)
395                 retval = 0;
396
397  out:
398         g_free (startup_id);
399
400         return (retval > 0) ? retval : 0;
401 }
402
403 static gboolean
404 show_version_cb (const char *option_name,
405                  const char *value,
406                  gpointer data,
407                  GError **error)
408 {
409         g_print ("%s\n", PACKAGE_STRING);
410
411         exit (EXIT_SUCCESS);
412
413         return FALSE;
414 }
415
416 static void
417 new_incoming_transfer_cb (EmpathyFTFactory *factory,
418                           EmpathyFTHandler *handler,
419                           GError *error,
420                           gpointer user_data)
421 {
422         if (error) {
423                 empathy_ft_manager_display_error (handler, error);
424         } else {
425                 empathy_receive_file_with_file_chooser (handler);
426         }
427 }
428
429 static void
430 new_ft_handler_cb (EmpathyFTFactory *factory,
431                    EmpathyFTHandler *handler,
432                    GError *error,
433                    gpointer user_data)
434 {
435         if (error) {
436                 empathy_ft_manager_display_error (handler, error);
437         } else {
438                 empathy_ft_manager_add_handler (handler);
439         }
440
441         g_object_unref (handler);
442 }
443
444 static void
445 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
446         gboolean outgoing, gpointer user_data)
447 {
448         EmpathyCallWindow *window;
449
450         window = empathy_call_window_new (handler);
451         gtk_widget_show (GTK_WIDGET (window));
452 }
453
454 #ifdef ENABLE_DEBUG
455 static void
456 default_log_handler (const gchar *log_domain,
457     GLogLevelFlags log_level,
458     const gchar *message,
459     gpointer user_data)
460 {
461         g_log_default_handler (log_domain, log_level, message, NULL);
462
463         /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
464          * debugger as they already have in empathy_debug. */
465         if (log_level != G_LOG_LEVEL_DEBUG
466             || tp_strdiff (log_domain, G_LOG_DOMAIN)) {
467                 EmpathyDebugger *dbg;
468                 GTimeVal now;
469
470                 dbg = empathy_debugger_get_singleton ();
471                 g_get_current_time (&now);
472
473                 empathy_debugger_add_message (dbg, &now, log_domain,
474                                               log_level, message);
475         }
476 }
477 #endif /* ENABLE_DEBUG */
478
479 int
480 main (int argc, char *argv[])
481 {
482         guint32            startup_timestamp;
483 #if HAVE_GEOCLUE
484         EmpathyLocationManager *location_manager = NULL;
485 #endif
486         EmpathyStatusIcon *icon;
487         EmpathyDispatcher *dispatcher;
488         EmpathyLogManager *log_manager;
489         EmpathyChatroomManager *chatroom_manager;
490         EmpathyCallFactory *call_factory;
491         EmpathyFTFactory  *ft_factory;
492         GtkWidget         *window;
493         MissionControl    *mc;
494         EmpathyIdle       *idle;
495         gboolean           autoconnect = TRUE;
496         gboolean           no_connect = FALSE;
497         gboolean           hide_contact_list = FALSE;
498         gboolean           accounts_dialog = FALSE;
499         GError            *error = NULL;
500         TpDBusDaemon      *dbus_daemon;
501         GOptionEntry       options[] = {
502                 { "no-connect", 'n',
503                   0, G_OPTION_ARG_NONE, &no_connect,
504                   N_("Don't connect on startup"),
505                   NULL },
506                 { "hide-contact-list", 'h',
507                   0, G_OPTION_ARG_NONE, &hide_contact_list,
508                   N_("Don't show the contact list on startup"),
509                   NULL },
510                 { "accounts", 'a',
511                   0, G_OPTION_ARG_NONE, &accounts_dialog,
512                   N_("Show the accounts dialog"),
513                   NULL },
514                 { "version", 'v',
515                   G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
516                 { NULL }
517         };
518
519         /* Init */
520         g_thread_init (NULL);
521         empathy_init ();
522
523         if (!gtk_init_with_args (&argc, &argv,
524                                  N_("- Empathy Instant Messenger"),
525                                  options, GETTEXT_PACKAGE, &error)) {
526                 g_warning ("Error in empathy init: %s", error->message);
527                 return EXIT_FAILURE;
528         }
529
530         empathy_gtk_init ();
531         g_set_application_name (_(PACKAGE_NAME));
532         g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
533
534         gst_init (&argc, &argv);
535
536 #if HAVE_LIBCHAMPLAIN
537         gtk_clutter_init (&argc, &argv);
538 #endif
539
540         gtk_window_set_default_icon_name ("empathy");
541         textdomain (GETTEXT_PACKAGE);
542
543 #ifdef ENABLE_DEBUG
544         /* Set up debugger */
545         g_log_set_default_handler (default_log_handler, NULL);
546 #endif
547
548         /* Setting up the bacon connection */
549         startup_timestamp = get_startup_timestamp ();
550         connection = bacon_message_connection_new ("empathy");
551         if (connection != NULL) {
552                 if (!bacon_message_connection_get_is_server (connection)) {
553                         gchar *message;
554
555                         if (accounts_dialog) {
556                                 DEBUG ("Showing accounts dialog from existing Empathy instance");
557
558                                 message = g_strdup ("accounts");
559
560                         } else {
561
562                                 DEBUG ("Activating existing instance");
563
564                                 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
565                                                            startup_timestamp);
566                         }
567
568                         bacon_message_connection_send (connection, message);
569
570                         /* We never popup a window, so tell startup-notification
571                          * that we are done. */
572                         gdk_notify_startup_complete ();
573
574                         g_free (message);
575                         bacon_message_connection_free (connection);
576
577                         return EXIT_SUCCESS;
578                 }
579         } else {
580                 g_warning ("Cannot create the 'empathy' bacon connection.");
581         }
582
583         /* Take well-known name */
584         dbus_daemon = tp_dbus_daemon_dup (&error);
585         if (error == NULL) {
586                 if (!tp_dbus_daemon_request_name (dbus_daemon,
587                                                   "org.gnome.Empathy",
588                                                   TRUE, &error)) {
589                         DEBUG ("Failed to request well-known name: %s",
590                                error ? error->message : "no message");
591                         g_clear_error (&error);
592                 }
593                 g_object_unref (dbus_daemon);
594         } else {
595                 DEBUG ("Failed to dup dbus daemon: %s",
596                        error ? error->message : "no message");
597                 g_clear_error (&error);
598         }
599
600         /* Setting up MC */
601         mc = empathy_mission_control_dup_singleton ();
602         g_signal_connect (mc, "ServiceEnded",
603                           G_CALLBACK (service_ended_cb),
604                           NULL);
605         g_signal_connect (mc, "Error",
606                           G_CALLBACK (operation_error_cb),
607                           NULL);
608
609         if (accounts_dialog) {
610                 GtkWidget *dialog;
611
612                 dialog = empathy_accounts_dialog_show (NULL, NULL);
613                 g_signal_connect (dialog, "destroy",
614                                   G_CALLBACK (gtk_main_quit),
615                                   NULL);
616
617                 gtk_main ();
618                 return 0;
619         }
620
621         /* Setting up Idle */
622         idle = empathy_idle_dup_singleton ();
623         empathy_idle_set_auto_away (idle, TRUE);
624         use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
625         empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
626                                  use_nm_notify_cb, idle);
627
628         /* Autoconnect */
629         empathy_conf_get_bool (empathy_conf_get (),
630                                EMPATHY_PREFS_AUTOCONNECT,
631                                &autoconnect);
632         if (autoconnect && ! no_connect &&
633                 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
634                         (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
635                 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
636         }
637
638         create_salut_account ();
639
640         /* Setting up UI */
641         window = empathy_main_window_show ();
642         icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
643
644         if (connection) {
645                 /* We se the callback here because we need window */
646                 bacon_message_connection_set_callback (connection,
647                                                        on_bacon_message_received,
648                                                        window);
649         }
650
651         /* Handle channels */
652         dispatcher = empathy_dispatcher_dup_singleton ();
653         g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
654
655         /* Logging */
656         log_manager = empathy_log_manager_dup_singleton ();
657         empathy_log_manager_observe (log_manager, dispatcher);
658
659         chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
660         empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
661
662         notify_init (_(PACKAGE_NAME));
663         /* Create the call factory */
664         call_factory = empathy_call_factory_initialise ();
665         g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
666                 G_CALLBACK (new_call_handler_cb), NULL);
667         /* Create the FT factory */
668         ft_factory = empathy_ft_factory_dup_singleton ();
669         g_signal_connect (ft_factory, "new-ft-handler",
670                 G_CALLBACK (new_ft_handler_cb), NULL);
671         g_signal_connect (ft_factory, "new-incoming-transfer",
672                 G_CALLBACK (new_incoming_transfer_cb), NULL);
673
674         /* Location mananger */
675 #if HAVE_GEOCLUE
676         location_manager = empathy_location_manager_dup_singleton ();
677 #endif
678
679         gtk_main ();
680
681         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
682
683         g_object_unref (mc);
684         g_object_unref (idle);
685         g_object_unref (icon);
686         g_object_unref (log_manager);
687         g_object_unref (dispatcher);
688         g_object_unref (chatroom_manager);
689 #if HAVE_GEOCLUE
690         g_object_unref (location_manager);
691 #endif
692         g_object_unref (ft_factory);
693
694         notify_uninit ();
695
696         return EXIT_SUCCESS;
697 }
698