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