]> git.0d.be Git - empathy.git/blob - src/empathy.c
Merge branch 'trivia'
[empathy.git] / src / empathy.c
1 /*
2  * Copyright (C) 2007-2009 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  */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32 #include <unique/unique.h>
33
34 #if HAVE_LIBCHAMPLAIN
35 #include <clutter-gtk/clutter-gtk.h>
36 #endif
37
38 #include <libnotify/notify.h>
39
40 #include <telepathy-glib/account-manager.h>
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/debug-sender.h>
43 #include <telepathy-glib/util.h>
44 #include <telepathy-glib/connection-manager.h>
45 #include <telepathy-glib/interfaces.h>
46
47 #ifdef ENABLE_TPL
48 #include <telepathy-logger/log-manager.h>
49 #include <telepathy-logger/log-store-empathy.h>
50 #else
51
52 #include <libempathy/empathy-log-manager.h>
53 #endif /* ENABLE_TPL */
54 #include <libempathy/empathy-idle.h>
55 #include <libempathy/empathy-utils.h>
56 #include <libempathy/empathy-call-factory.h>
57 #include <libempathy/empathy-chatroom-manager.h>
58 #include <libempathy/empathy-account-settings.h>
59 #include <libempathy/empathy-connectivity.h>
60 #include <libempathy/empathy-connection-managers.h>
61 #include <libempathy/empathy-dispatcher.h>
62 #include <libempathy/empathy-dispatch-operation.h>
63 #include <libempathy/empathy-ft-factory.h>
64 #include <libempathy/empathy-tp-chat.h>
65 #include <libempathy/empathy-tp-call.h>
66
67 #include <libempathy-gtk/empathy-conf.h>
68 #include <libempathy-gtk/empathy-ui-utils.h>
69 #include <libempathy-gtk/empathy-location-manager.h>
70
71 #include "empathy-main-window.h"
72 #include "empathy-import-mc4-accounts.h"
73 #include "empathy-accounts-common.h"
74 #include "empathy-accounts-dialog.h"
75 #include "empathy-chat-manager.h"
76 #include "empathy-status-icon.h"
77 #include "empathy-call-window.h"
78 #include "empathy-chat-window.h"
79 #include "empathy-ft-manager.h"
80
81 #include "extensions/extensions.h"
82
83 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
84 #include <libempathy/empathy-debug.h>
85
86 #include <gst/gst.h>
87
88 static gboolean start_hidden = FALSE;
89 static gboolean no_connect = FALSE;
90
91 static void account_manager_ready_cb (GObject *source_object,
92     GAsyncResult *result,
93     gpointer user_data);
94
95 static void
96 dispatch_cb (EmpathyDispatcher *dispatcher,
97     EmpathyDispatchOperation *operation,
98     gpointer user_data)
99 {
100   GQuark channel_type;
101
102   channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
103
104   if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
105     {
106       EmpathyTpChat *tp_chat;
107       EmpathyChat   *chat = NULL;
108       const gchar   *id;
109
110       tp_chat = EMPATHY_TP_CHAT
111         (empathy_dispatch_operation_get_channel_wrapper (operation));
112
113       id = empathy_tp_chat_get_id (tp_chat);
114       if (!EMP_STR_EMPTY (id))
115         {
116           TpConnection *connection;
117           TpAccount *account;
118
119           connection = empathy_tp_chat_get_connection (tp_chat);
120           account = empathy_get_account_for_connection (connection);
121           chat = empathy_chat_window_find_chat (account, id);
122         }
123
124       if (chat)
125         {
126           empathy_chat_set_tp_chat (chat, tp_chat);
127         }
128       else
129         {
130           chat = empathy_chat_new (tp_chat);
131           /* empathy_chat_new returns a floating reference as EmpathyChat is
132            * a GtkWidget. This reference will be taken by a container
133            * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
134         }
135
136       empathy_chat_window_present_chat (chat,
137           empathy_dispatch_operation_get_user_action_time (operation));
138
139       if (empathy_tp_chat_is_invited (tp_chat))
140         {
141           /* We have been invited to the room. Add ourself as member as this
142            * channel has been approved. */
143           empathy_tp_chat_join (tp_chat);
144         }
145
146       empathy_dispatch_operation_claim (operation);
147     }
148   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
149     {
150       EmpathyCallFactory *factory;
151
152       factory = empathy_call_factory_get ();
153       empathy_call_factory_claim_channel (factory, operation);
154     }
155   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
156     {
157       EmpathyFTFactory *factory;
158
159       factory = empathy_ft_factory_dup_singleton ();
160
161       /* if the operation is not incoming, don't claim it,
162        * as it might have been triggered by another client, and
163        * we are observing it.
164        */
165       if (empathy_dispatch_operation_is_incoming (operation))
166         empathy_ft_factory_claim_channel (factory, operation);
167     }
168 }
169
170 static void
171 use_conn_notify_cb (EmpathyConf *conf,
172     const gchar *key,
173     gpointer     user_data)
174 {
175   EmpathyConnectivity *connectivity = user_data;
176   gboolean     use_conn;
177
178   if (empathy_conf_get_bool (conf, key, &use_conn))
179     {
180       empathy_connectivity_set_use_conn (connectivity, use_conn);
181     }
182 }
183
184 static void
185 migrate_config_to_xdg_dir (void)
186 {
187   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
188   int i;
189   GFile *xdg_file, *old_file;
190   static const gchar* filenames[] = {
191     "geometry.ini",
192     "irc-networks.xml",
193     "chatrooms.xml",
194     "contact-groups.xml",
195     "status-presets.xml",
196     "accels.txt",
197     NULL
198   };
199
200   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
201   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
202     {
203       /* xdg config dir already exists */
204       g_free (xdg_dir);
205       return;
206     }
207
208   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
209       PACKAGE_NAME, NULL);
210   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
211     {
212       /* old config dir didn't exist */
213       g_free (xdg_dir);
214       g_free (old_dir);
215       return;
216     }
217
218   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
219     {
220       DEBUG ("Failed to create configuration directory; aborting migration");
221       g_free (xdg_dir);
222       g_free (old_dir);
223       return;
224     }
225
226   for (i = 0; filenames[i]; i++)
227     {
228       old_filename = g_build_filename (old_dir, filenames[i], NULL);
229       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
230         {
231           g_free (old_filename);
232           continue;
233         }
234       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
235       old_file = g_file_new_for_path (old_filename);
236       xdg_file = g_file_new_for_path (xdg_filename);
237
238       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
239           NULL, NULL, NULL, NULL))
240         DEBUG ("Failed to migrate %s", filenames[i]);
241
242       g_free (old_filename);
243       g_free (xdg_filename);
244       g_object_unref (old_file);
245       g_object_unref (xdg_file);
246     }
247
248   g_free (xdg_dir);
249   g_free (old_dir);
250 }
251
252 static void
253 show_accounts_ui (GdkScreen *screen,
254     gboolean if_needed)
255 {
256   empathy_accounts_dialog_show_application (screen,
257       NULL, if_needed, start_hidden);
258 }
259
260 static UniqueResponse
261 unique_app_message_cb (UniqueApp *unique_app,
262     gint command,
263     UniqueMessageData *data,
264     guint timestamp,
265     gpointer user_data)
266 {
267   GtkWindow *window = user_data;
268   TpAccountManager *account_manager;
269
270   DEBUG ("Other instance launched, presenting the main window. "
271       "Command=%d, timestamp %u", command, timestamp);
272
273   /* XXX: the standalone app somewhat breaks this case, since
274    * communicating it would be a pain */
275
276   /* We're requested to show stuff again, disable the start hidden global
277    * in case the accounts wizard wants to pop up.
278    */
279   start_hidden = FALSE;
280
281   gtk_window_set_screen (GTK_WINDOW (window),
282       unique_message_data_get_screen (data));
283   gtk_window_set_startup_id (GTK_WINDOW (window),
284       unique_message_data_get_startup_id (data));
285   gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
286   gtk_window_set_skip_taskbar_hint (window, FALSE);
287
288   account_manager = tp_account_manager_dup ();
289   tp_account_manager_prepare_async (account_manager, NULL,
290       account_manager_ready_cb, NULL);
291   g_object_unref (account_manager);
292
293   return UNIQUE_RESPONSE_OK;
294 }
295
296 static gboolean
297 show_version_cb (const char *option_name,
298     const char *value,
299     gpointer data,
300     GError **error)
301 {
302   g_print ("%s\n", PACKAGE_STRING);
303
304   exit (EXIT_SUCCESS);
305
306   return FALSE;
307 }
308
309 static void
310 new_incoming_transfer_cb (EmpathyFTFactory *factory,
311     EmpathyFTHandler *handler,
312     GError *error,
313     gpointer user_data)
314 {
315   if (error)
316     empathy_ft_manager_display_error (handler, error);
317   else
318     empathy_receive_file_with_file_chooser (handler);
319 }
320
321 static void
322 new_ft_handler_cb (EmpathyFTFactory *factory,
323     EmpathyFTHandler *handler,
324     GError *error,
325     gpointer user_data)
326 {
327   if (error)
328     empathy_ft_manager_display_error (handler, error);
329   else
330     empathy_ft_manager_add_handler (handler);
331
332   g_object_unref (handler);
333 }
334
335 static void
336 new_call_handler_cb (EmpathyCallFactory *factory,
337     EmpathyCallHandler *handler,
338     gboolean outgoing,
339     gpointer user_data)
340 {
341   EmpathyCallWindow *window;
342
343   window = empathy_call_window_new (handler);
344   gtk_widget_show (GTK_WIDGET (window));
345 }
346
347 static void
348 account_manager_ready_cb (GObject *source_object,
349     GAsyncResult *result,
350     gpointer user_data)
351 {
352   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
353   GError *error = NULL;
354   EmpathyIdle *idle;
355   EmpathyConnectivity *connectivity;
356   gboolean autoconnect = TRUE;
357   TpConnectionPresenceType presence;
358
359   if (!tp_account_manager_prepare_finish (manager, result, &error))
360     {
361       DEBUG ("Failed to prepare account manager: %s", error->message);
362       g_error_free (error);
363       return;
364     }
365
366   /* Autoconnect */
367   idle = empathy_idle_dup_singleton ();
368   connectivity = empathy_connectivity_dup_singleton ();
369
370   presence = tp_account_manager_get_most_available_presence (manager, NULL,
371       NULL);
372
373   empathy_conf_get_bool (empathy_conf_get (),
374       EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
375   if (autoconnect && !no_connect &&
376       tp_connection_presence_type_cmp_availability
377           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
378             <= 0)
379       /* if current state is Offline, then put it online */
380       empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
381
382   /* Pop up the accounts dialog if it's needed (either when we don't have any
383    * accounts yet or when we haven't imported mc4 accounts yet */
384   if (!empathy_accounts_has_accounts (manager)
385       || !empathy_import_mc4_has_imported ())
386     show_accounts_ui (gdk_screen_get_default (), TRUE);
387
388   g_object_unref (idle);
389   g_object_unref (connectivity);
390 }
391
392 static EmpathyDispatcher *
393 setup_dispatcher (void)
394 {
395   EmpathyDispatcher *d;
396   GPtrArray *filters;
397   struct {
398     const gchar *channeltype;
399     TpHandleType handletype;
400   } types[] = {
401     /* Text channels with handle types none, contact and room */
402     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE  },
403     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT  },
404     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM  },
405     /* file transfer to contacts */
406     { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT  },
407     /* stream media to contacts */
408     { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT  },
409     /* roomlists */
410     { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
411   };
412   gchar *capabilities[] = {
413     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
414     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
415     NULL };
416   GHashTable *asv;
417   guint i;
418
419   /* Setup the basic Client.Handler that matches our client filter */
420   filters = g_ptr_array_new ();
421   asv = tp_asv_new (
422         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
423            TP_IFACE_CHANNEL_TYPE_TEXT,
424         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
425             TP_HANDLE_TYPE_CONTACT,
426         NULL);
427   g_ptr_array_add (filters, asv);
428
429   d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
430
431   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
432   g_ptr_array_free (filters, TRUE);
433
434   /* Setup the an extended Client.Handler that matches everything we can do */
435   filters = g_ptr_array_new ();
436   for (i = 0 ; i < G_N_ELEMENTS (types); i++)
437     {
438       asv = tp_asv_new (
439         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
440         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
441         NULL);
442
443       g_ptr_array_add (filters, asv);
444     }
445
446   asv = tp_asv_new (
447         TP_IFACE_CHANNEL ".ChannelType",
448           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
449         TP_IFACE_CHANNEL ".TargetHandleType",
450           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
451         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
452           G_TYPE_BOOLEAN, TRUE,
453         NULL);
454   g_ptr_array_add (filters, asv);
455
456   asv = tp_asv_new (
457         TP_IFACE_CHANNEL ".ChannelType",
458           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
459         TP_IFACE_CHANNEL ".TargetHandleType",
460           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
461         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
462           G_TYPE_BOOLEAN, TRUE,
463         NULL);
464   g_ptr_array_add (filters, asv);
465
466
467   empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
468     filters, capabilities);
469
470   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
471   g_ptr_array_free (filters, TRUE);
472
473   return d;
474 }
475
476 static void
477 account_status_changed_cb (TpAccount *account,
478     guint old_status,
479     guint new_status,
480     guint reason,
481     gchar *dbus_error_name,
482     GHashTable *details,
483     EmpathyChatroom *room)
484 {
485   TpConnection *conn;
486
487   conn = tp_account_get_connection (account);
488   if (conn == NULL)
489     return;
490
491   empathy_dispatcher_join_muc (conn,
492       empathy_chatroom_get_room (room), EMPATHY_DISPATCHER_NON_USER_ACTION,
493       NULL, NULL);
494 }
495
496 static void
497 account_manager_chatroom_ready_cb (GObject *source_object,
498     GAsyncResult *result,
499     gpointer user_data)
500 {
501   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
502   EmpathyChatroomManager *chatroom_manager = user_data;
503   GList *accounts, *l;
504   GError *error = NULL;
505
506   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
507     {
508       DEBUG ("Failed to prepare account manager: %s", error->message);
509       g_error_free (error);
510       return;
511     }
512
513   accounts = tp_account_manager_get_valid_accounts (account_manager);
514
515   for (l = accounts; l != NULL; l = g_list_next (l))
516     {
517       TpAccount *account = TP_ACCOUNT (l->data);
518       TpConnection *conn;
519       GList *chatrooms, *p;
520
521       conn = tp_account_get_connection (account);
522
523       chatrooms = empathy_chatroom_manager_get_chatrooms (
524           chatroom_manager, account);
525
526       for (p = chatrooms; p != NULL; p = p->next)
527         {
528           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
529
530           if (!empathy_chatroom_get_auto_connect (room))
531             continue;
532
533           if (conn == NULL)
534             {
535               g_signal_connect (G_OBJECT (account), "status-changed",
536                   G_CALLBACK (account_status_changed_cb), room);
537             }
538           else
539             {
540               empathy_dispatcher_join_muc (conn,
541                   empathy_chatroom_get_room (room),
542                   EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
543             }
544         }
545
546       g_list_free (chatrooms);
547     }
548
549   g_list_free (accounts);
550 }
551
552 static void
553 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
554     GParamSpec *pspec,
555     gpointer user_data)
556 {
557   TpAccountManager *account_manager = user_data;
558
559   tp_account_manager_prepare_async (account_manager, NULL,
560       account_manager_chatroom_ready_cb, chatroom_manager);
561 }
562
563 static void
564 empathy_idle_set_auto_away_cb (EmpathyConf *conf,
565                                 const gchar *key,
566                                 gpointer user_data)
567 {
568         gboolean autoaway;
569         EmpathyIdle *idle = user_data;
570
571         empathy_conf_get_bool (conf, key, &autoaway);
572         empathy_idle_set_auto_away (idle, autoaway);
573 }
574
575 int
576 main (int argc, char *argv[])
577 {
578 #if HAVE_GEOCLUE
579   EmpathyLocationManager *location_manager = NULL;
580 #endif
581   EmpathyStatusIcon *icon;
582   EmpathyDispatcher *dispatcher;
583   TpAccountManager *account_manager;
584 #ifdef ENABLE_TPL
585   TplLogManager *log_manager;
586 #else
587   EmpathyLogManager *log_manager;
588 #endif /* ENABLE_TPL */
589   EmpathyChatroomManager *chatroom_manager;
590   EmpathyCallFactory *call_factory;
591   EmpathyFTFactory  *ft_factory;
592   GtkWidget *window;
593   EmpathyIdle *idle;
594   EmpathyConnectivity *connectivity;
595   EmpathyChatManager *chat_manager;
596   GError *error = NULL;
597   UniqueApp *unique_app;
598   gboolean chatroom_manager_ready;
599   gboolean autoaway = TRUE;
600 #ifdef ENABLE_DEBUG
601   TpDebugSender *debug_sender;
602 #endif
603
604   GOptionContext *optcontext;
605   GOptionEntry options[] = {
606       { "no-connect", 'n',
607         0, G_OPTION_ARG_NONE, &no_connect,
608         N_("Don't connect on startup"),
609         NULL },
610       { "start-hidden", 'h',
611         0, G_OPTION_ARG_NONE, &start_hidden,
612         N_("Don't display the contact list or any other dialogs on startup"),
613         NULL },
614       { "version", 'v',
615         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
616         NULL, NULL },
617       { NULL }
618   };
619
620   /* Init */
621   g_thread_init (NULL);
622   empathy_init ();
623
624   optcontext = g_option_context_new (N_("- Empathy IM Client"));
625   g_option_context_add_group (optcontext, gst_init_get_option_group ());
626   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
627 #if HAVE_LIBCHAMPLAIN
628   g_option_context_add_group (optcontext, clutter_get_option_group ());
629 #endif
630   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
631
632   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
633     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
634         error->message, argv[0]);
635     g_warning ("Error in empathy init: %s", error->message);
636     return EXIT_FAILURE;
637   }
638
639   g_option_context_free (optcontext);
640
641   empathy_gtk_init ();
642   g_set_application_name (_(PACKAGE_NAME));
643   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
644
645   gtk_window_set_default_icon_name ("empathy");
646   textdomain (GETTEXT_PACKAGE);
647
648 #ifdef ENABLE_DEBUG
649   /* Set up debug sender */
650   debug_sender = tp_debug_sender_dup ();
651   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
652 #endif
653
654   unique_app = unique_app_new ("org.gnome."PACKAGE_NAME, NULL);
655
656   if (unique_app_is_running (unique_app))
657     {
658       unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
659
660       g_object_unref (unique_app);
661       return EXIT_SUCCESS;
662     }
663
664   notify_init (_(PACKAGE_NAME));
665
666   /* Setting up Idle */
667   idle = empathy_idle_dup_singleton ();
668
669   empathy_conf_get_bool (empathy_conf_get (),
670       EMPATHY_PREFS_AUTOAWAY, &autoaway);
671
672   empathy_conf_notify_add (empathy_conf_get (),
673                            EMPATHY_PREFS_AUTOAWAY,
674                            empathy_idle_set_auto_away_cb,
675                            idle);
676
677   empathy_idle_set_auto_away (idle, autoaway);
678
679   /* Setting up Connectivity */
680   connectivity = empathy_connectivity_dup_singleton ();
681   use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
682       connectivity);
683   empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
684       use_conn_notify_cb, connectivity);
685
686   /* account management */
687   account_manager = tp_account_manager_dup ();
688   tp_account_manager_prepare_async (account_manager, NULL,
689       account_manager_ready_cb, NULL);
690
691   /* Handle channels */
692   dispatcher = setup_dispatcher ();
693   g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
694
695   migrate_config_to_xdg_dir ();
696
697   /* Setting up UI */
698   window = empathy_main_window_show ();
699   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
700
701   /* Chat manager */
702   chat_manager = empathy_chat_manager_dup_singleton ();
703
704   g_signal_connect (unique_app, "message-received",
705       G_CALLBACK (unique_app_message_cb), window);
706
707   /* Logging */
708 #ifdef ENABLE_TPL
709   log_manager = tpl_log_manager_dup_singleton ();
710 #else
711   log_manager = empathy_log_manager_dup_singleton ();
712   empathy_log_manager_observe (log_manager, dispatcher);
713 #endif
714
715   chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
716   empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
717
718   g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
719   if (!chatroom_manager_ready)
720     {
721       g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
722           G_CALLBACK (chatroom_manager_ready_cb), account_manager);
723     }
724   else
725     {
726       chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
727     }
728
729   /* Create the call factory */
730   call_factory = empathy_call_factory_initialise ();
731   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
732       G_CALLBACK (new_call_handler_cb), NULL);
733   /* Create the FT factory */
734   ft_factory = empathy_ft_factory_dup_singleton ();
735   g_signal_connect (ft_factory, "new-ft-handler",
736       G_CALLBACK (new_ft_handler_cb), NULL);
737   g_signal_connect (ft_factory, "new-incoming-transfer",
738       G_CALLBACK (new_incoming_transfer_cb), NULL);
739
740   /* Location mananger */
741 #if HAVE_GEOCLUE
742   location_manager = empathy_location_manager_dup_singleton ();
743 #endif
744
745   gtk_main ();
746
747   empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
748
749 #ifdef ENABLE_DEBUG
750   g_object_unref (debug_sender);
751 #endif
752
753   g_object_unref (chat_manager);
754   g_object_unref (idle);
755   g_object_unref (connectivity);
756   g_object_unref (icon);
757   g_object_unref (account_manager);
758   g_object_unref (log_manager);
759   g_object_unref (dispatcher);
760   g_object_unref (chatroom_manager);
761 #if HAVE_GEOCLUE
762   g_object_unref (location_manager);
763 #endif
764   g_object_unref (ft_factory);
765   g_object_unref (unique_app);
766
767   notify_uninit ();
768   xmlCleanupParser ();
769
770   return EXIT_SUCCESS;
771 }