]> git.0d.be Git - empathy.git/blob - src/empathy.c
Move should_create_salut_account to local-xmpp-assistant-widget
[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/gstdio.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkx.h>
33
34 #ifdef 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 #include <telepathy-yell/telepathy-yell.h>
48
49 #include <telepathy-logger/log-manager.h>
50
51 #include <libempathy/empathy-client-factory.h>
52 #include <libempathy/empathy-connection-aggregator.h>
53 #include <libempathy/empathy-presence-manager.h>
54 #include <libempathy/empathy-utils.h>
55 #include <libempathy/empathy-chatroom-manager.h>
56 #include <libempathy/empathy-account-settings.h>
57 #include <libempathy/empathy-connectivity.h>
58 #include <libempathy/empathy-connection-managers.h>
59 #include <libempathy/empathy-request-util.h>
60 #include <libempathy/empathy-ft-factory.h>
61 #include <libempathy/empathy-gsettings.h>
62 #include <libempathy/empathy-tp-chat.h>
63
64 #include <libempathy-gtk/empathy-ui-utils.h>
65 #include <libempathy-gtk/empathy-location-manager.h>
66 #include <libempathy-gtk/empathy-notify-manager.h>
67
68 #include "empathy-roster-window.h"
69 #include "empathy-accounts-common.h"
70 #include "empathy-accounts-dialog.h"
71 #include "empathy-status-icon.h"
72 #include "empathy-ft-manager.h"
73 #include "empathy-notifications-approver.h"
74
75 #include "extensions/extensions.h"
76
77 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
78 #include <libempathy/empathy-debug.h>
79
80 #define EMPATHY_DBUS_NAME "org.gnome.Empathy"
81
82 #define EMPATHY_TYPE_APP (empathy_app_get_type ())
83 #define EMPATHY_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_APP, EmpathyApp))
84 #define EMPATHY_APP_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST ((obj), EMPATHY_TYPE_APP, EmpathyAppClass))
85 #define EMPATHY_IS_EMPATHY_APP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_APP))
86 #define EMPATHY_IS_EMPATHY_APP_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((obj), EMPATHY_TYPE_APP))
87 #define EMPATHY_APP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EMPATHY_TYPE_APP, EmpathyAppClass))
88
89 typedef struct _EmpathyApp EmpathyApp;
90 typedef struct _EmpathyAppClass EmpathyAppClass;
91
92 enum
93 {
94   PROP_NO_CONNECT = 1,
95   PROP_START_HIDDEN
96 };
97
98 GType empathy_app_get_type (void);
99
100 struct _EmpathyAppClass
101 {
102   GtkApplicationClass parent_class;
103 };
104
105 struct _EmpathyApp
106 {
107   GtkApplication parent;
108
109   /* Properties */
110   gboolean no_connect;
111   gboolean start_hidden;
112   gboolean show_preferences;
113   gchar *preferences_tab;
114
115   gboolean activated;
116
117   GtkWidget *window;
118   EmpathyStatusIcon *icon;
119   TpAccountManager *account_manager;
120   TplLogManager *log_manager;
121   EmpathyChatroomManager *chatroom_manager;
122   EmpathyFTFactory  *ft_factory;
123   EmpathyPresenceManager *presence_mgr;
124   EmpathyConnectivity *connectivity;
125   GSettings *gsettings;
126   EmpathyNotificationsApprover *notifications_approver;
127   EmpathyConnectionAggregator *conn_aggregator;
128 #ifdef HAVE_GEOCLUE
129   EmpathyLocationManager *location_manager;
130 #endif
131 #ifdef ENABLE_DEBUG
132   TpDebugSender *debug_sender;
133 #endif
134
135   gboolean shell_running;
136 };
137
138
139 G_DEFINE_TYPE(EmpathyApp, empathy_app, GTK_TYPE_APPLICATION)
140
141 static void
142 empathy_app_dispose (GObject *object)
143 {
144   EmpathyApp *self = EMPATHY_APP (object);
145   void (*dispose) (GObject *) =
146     G_OBJECT_CLASS (empathy_app_parent_class)->dispose;
147
148   /* Only set our presence to offline when exiting if GNOME Shell is not
149    * running */
150   if (self->presence_mgr != NULL &&
151       !self->shell_running)
152     {
153       empathy_presence_manager_set_state (self->presence_mgr,
154           TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
155     }
156
157 #ifdef ENABLE_DEBUG
158   tp_clear_object (&self->debug_sender);
159 #endif
160
161   tp_clear_object (&self->presence_mgr);
162   tp_clear_object (&self->connectivity);
163   tp_clear_object (&self->icon);
164   tp_clear_object (&self->account_manager);
165   tp_clear_object (&self->log_manager);
166   tp_clear_object (&self->chatroom_manager);
167 #ifdef HAVE_GEOCLUE
168   tp_clear_object (&self->location_manager);
169 #endif
170   tp_clear_object (&self->ft_factory);
171   tp_clear_object (&self->gsettings);
172   tp_clear_object (&self->notifications_approver);
173   tp_clear_object (&self->conn_aggregator);
174
175   if (dispose != NULL)
176     dispose (object);
177 }
178
179 static void
180 empathy_app_finalize (GObject *object)
181 {
182   EmpathyApp *self = EMPATHY_APP (object);
183   void (*finalize) (GObject *) =
184     G_OBJECT_CLASS (empathy_app_parent_class)->finalize;
185
186   g_free (self->preferences_tab);
187
188   if (finalize != NULL)
189     finalize (object);
190 }
191
192 static void account_manager_ready_cb (GObject *source_object,
193     GAsyncResult *result,
194     gpointer user_data);
195
196 static void
197 empathy_app_set_property (GObject *object,
198     guint prop_id,
199     const GValue *value,
200     GParamSpec *pspec)
201 {
202   EmpathyApp *self = EMPATHY_APP (object);
203
204   switch (prop_id)
205     {
206       case PROP_NO_CONNECT:
207         self->no_connect = g_value_get_boolean (value);
208         break;
209       case PROP_START_HIDDEN:
210         self->start_hidden = g_value_get_boolean (value);
211         break;
212       default:
213         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214         break;
215     }
216 }
217
218 static void
219 new_incoming_transfer_cb (EmpathyFTFactory *factory,
220     EmpathyFTHandler *handler,
221     GError *error,
222     gpointer user_data)
223 {
224   if (error)
225     empathy_ft_manager_display_error (handler, error);
226   else
227     empathy_receive_file_with_file_chooser (handler);
228 }
229
230 static void
231 new_ft_handler_cb (EmpathyFTFactory *factory,
232     EmpathyFTHandler *handler,
233     GError *error,
234     gpointer user_data)
235 {
236   if (error)
237     empathy_ft_manager_display_error (handler, error);
238   else
239     empathy_ft_manager_add_handler (handler);
240
241   g_object_unref (handler);
242 }
243
244 static gboolean
245 empathy_app_local_command_line (GApplication *app,
246     gchar ***arguments,
247     gint *exit_status);
248
249 static void
250 empathy_presence_manager_set_auto_away_cb (GSettings *gsettings,
251     const gchar *key,
252     gpointer user_data)
253 {
254   EmpathyPresenceManager *presence_mgr = user_data;
255
256   empathy_presence_manager_set_auto_away (presence_mgr,
257       g_settings_get_boolean (gsettings, key));
258 }
259
260 #define GNOME_SHELL_BUS_NAME "org.gnome.Shell"
261
262 static void
263 list_names_cb (TpDBusDaemon *bus_daemon,
264         const gchar * const *names,
265         const GError *error,
266         gpointer user_data,
267         GObject *weak_object)
268 {
269   EmpathyApp *self = (EmpathyApp *) weak_object;
270   guint i;
271
272   if (error != NULL)
273       goto out;
274
275   for (i = 0; names[i] != NULL; i++)
276     {
277       if (!tp_strdiff (names[i], GNOME_SHELL_BUS_NAME))
278         {
279           self->shell_running = TRUE;
280           break;
281         }
282     }
283
284 out:
285   if (self->shell_running)
286     {
287       DEBUG ("GNOME Shell is running, don't create status icon");
288
289       /* Rely on GNOME Shell to watch session state */
290       empathy_presence_manager_set_auto_away (self->presence_mgr, FALSE);
291
292       empathy_roster_window_set_shell_running (
293           EMPATHY_ROSTER_WINDOW (self->window), TRUE);
294     }
295   else
296     {
297       gboolean autoaway;
298
299       self->icon = empathy_status_icon_new (GTK_WINDOW (self->window),
300           self->start_hidden);
301
302       /* Allow Empathy to watch session state */
303       autoaway = g_settings_get_boolean (self->gsettings,
304           EMPATHY_PREFS_AUTOAWAY);
305
306       g_signal_connect (self->gsettings,
307           "changed::" EMPATHY_PREFS_AUTOAWAY,
308           G_CALLBACK (empathy_presence_manager_set_auto_away_cb),
309           self->presence_mgr);
310
311       empathy_presence_manager_set_auto_away (self->presence_mgr, autoaway);
312     }
313 }
314
315 static int
316 empathy_app_command_line (GApplication *app,
317     GApplicationCommandLine *cmdline)
318 {
319   EmpathyApp *self = (EmpathyApp *) app;
320   gchar **args, **argv;
321   gint argc, exit_status, i;
322
323   args = g_application_command_line_get_arguments (cmdline, &argc);
324   /* We have to make an extra copy of the array, since g_option_context_parse()
325    * assumes that it can remove strings from the array without freeing them. */
326   argv = g_new (gchar*, argc + 1);
327   for (i = 0; i <= argc; i++)
328     argv[i] = args[i];
329
330   if (empathy_app_local_command_line (app, &argv, &exit_status))
331     DEBUG ("failed to parse command line!");
332
333   g_free (argv);
334   g_strfreev (args);
335
336   if (!self->activated)
337     {
338       GError *error = NULL;
339       TpDBusDaemon *dbus;
340
341       /* Create the FT factory */
342       self->ft_factory = empathy_ft_factory_dup_singleton ();
343       g_signal_connect (self->ft_factory, "new-ft-handler",
344           G_CALLBACK (new_ft_handler_cb), NULL);
345       g_signal_connect (self->ft_factory, "new-incoming-transfer",
346           G_CALLBACK (new_incoming_transfer_cb), NULL);
347
348       if (!empathy_ft_factory_register (self->ft_factory, &error))
349         {
350           g_warning ("Failed to register FileTransfer handler: %s",
351               error->message);
352           g_error_free (error);
353         }
354
355       self->activated = TRUE;
356
357       /* Setting up UI */
358       self->window = empathy_roster_window_dup ();
359
360       gtk_application_add_window (GTK_APPLICATION (app),
361           GTK_WINDOW (self->window));
362
363       /* check if Shell is running */
364       dbus = tp_dbus_daemon_dup (&error);
365       g_assert_no_error (error);
366
367       tp_dbus_daemon_list_names (dbus, -1, list_names_cb,
368               self, NULL, G_OBJECT (self));
369
370       g_object_unref (dbus);
371
372       self->notifications_approver =
373         empathy_notifications_approver_dup_singleton ();
374     }
375   else
376     {
377       /* We're requested to show stuff again, disable the start hidden global in
378        * case the accounts wizard wants to pop up.
379        */
380       self->start_hidden = FALSE;
381     }
382
383   if (self->show_preferences)
384     empathy_roster_window_show_preferences (
385         EMPATHY_ROSTER_WINDOW (self->window), self->preferences_tab);
386
387   if (!self->start_hidden)
388     empathy_window_present (GTK_WINDOW (self->window));
389
390   /* Display the accounts dialog if needed */
391   tp_proxy_prepare_async (self->account_manager, NULL,
392       account_manager_ready_cb, self);
393
394   return 0;
395 }
396
397 static gboolean
398 preferences_cb (const char *option_name,
399     const char *value,
400     gpointer data,
401     GError **error)
402 {
403   EmpathyApp *self = data;
404
405   self->show_preferences = TRUE;
406
407   g_free (self->preferences_tab);
408   self->preferences_tab = g_strdup (value);
409
410   return TRUE;
411 }
412
413 static gboolean
414 show_version_cb (const char *option_name,
415     const char *value,
416     gpointer data,
417     GError **error);
418
419 static gboolean
420 empathy_app_local_command_line (GApplication *app,
421     gchar ***arguments,
422     gint *exit_status)
423 {
424   EmpathyApp *self = (EmpathyApp *) app;
425   gint i;
426   gchar **argv;
427   gint argc = 0;
428   gboolean retval = FALSE;
429   GError *error = NULL;
430   gboolean no_connect = FALSE, start_hidden = FALSE;
431
432   GOptionContext *optcontext;
433   GOptionGroup *group;
434   GOptionEntry options[] = {
435       { "no-connect", 'n',
436         0, G_OPTION_ARG_NONE, &no_connect,
437         N_("Don't connect on startup"),
438         NULL },
439       { "start-hidden", 'h',
440         0, G_OPTION_ARG_NONE, &start_hidden,
441         N_("Don't display the contact list or any other dialogs on startup"),
442         NULL },
443       { "show-preferences", 'p',
444         G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, &preferences_cb,
445         NULL, NULL },
446       { "version", 'v',
447         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
448         NULL, NULL },
449       { NULL }
450   };
451
452   /* We create a group so that GOptionArgFuncs get the user data */
453   group = g_option_group_new ("empathy", NULL, NULL, app, NULL);
454   g_option_group_add_entries (group, options);
455
456   optcontext = g_option_context_new (N_("- Empathy IM Client"));
457   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
458   g_option_context_set_main_group (optcontext, group);
459   g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
460
461   argc = g_strv_length (*arguments);
462
463   /* We dup the args because g_option_context_parse() sets things to NULL,
464    * but we want to parse all the command line to the primary instance
465    * if necessary. */
466   argv = g_new (gchar*, argc + 1);
467   for (i = 0; i <= argc; i++)
468     argv[i] = (*arguments)[i];
469
470   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
471     {
472       g_print ("%s\nRun '%s --help' to see a full list of available command "
473           "line options.\n",
474           error->message, argv[0]);
475       g_warning ("Error in empathy init: %s", error->message);
476
477       *exit_status = EXIT_FAILURE;
478       retval = TRUE;
479     }
480
481   g_free (argv);
482
483   g_option_context_free (optcontext);
484
485   self->no_connect = no_connect;
486   self->start_hidden = start_hidden;
487
488   return retval;
489 }
490
491 static void empathy_app_constructed (GObject *object);
492
493 static void
494 empathy_app_class_init (EmpathyAppClass *klass)
495 {
496   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
497   GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
498   GParamSpec *spec;
499
500   gobject_class->set_property = empathy_app_set_property;
501   gobject_class->constructed = empathy_app_constructed;
502   gobject_class->dispose = empathy_app_dispose;
503   gobject_class->finalize = empathy_app_finalize;
504
505   g_app_class->command_line = empathy_app_command_line;
506   g_app_class->local_command_line = empathy_app_local_command_line;
507
508   spec = g_param_spec_boolean ("no-connect", "no connect",
509       "Don't connect on startup",
510       FALSE,
511       G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
512   g_object_class_install_property (gobject_class, PROP_NO_CONNECT, spec);
513
514   spec = g_param_spec_boolean ("start-hidden", "start hidden",
515       "Don't display the contact list or any other dialogs on startup",
516       FALSE,
517       G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
518   g_object_class_install_property (gobject_class, PROP_START_HIDDEN, spec);
519 }
520
521 static void
522 empathy_app_init (EmpathyApp *self)
523 {
524 }
525
526 static void
527 use_conn_notify_cb (GSettings *gsettings,
528     const gchar *key,
529     gpointer     user_data)
530 {
531   EmpathyConnectivity *connectivity = user_data;
532
533   empathy_connectivity_set_use_conn (connectivity,
534       g_settings_get_boolean (gsettings, key));
535 }
536
537 static void
538 migrate_config_to_xdg_dir (void)
539 {
540   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
541   int i;
542   GFile *xdg_file, *old_file;
543   static const gchar* filenames[] = {
544     "geometry.ini",
545     "irc-networks.xml",
546     "chatrooms.xml",
547     "contact-groups.xml",
548     "status-presets.xml",
549     "accels.txt",
550     NULL
551   };
552
553   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
554   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
555     {
556       /* xdg config dir already exists */
557       g_free (xdg_dir);
558       return;
559     }
560
561   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
562       PACKAGE_NAME, NULL);
563   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
564     {
565       /* old config dir didn't exist */
566       g_free (xdg_dir);
567       g_free (old_dir);
568       return;
569     }
570
571   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
572     {
573       DEBUG ("Failed to create configuration directory; aborting migration");
574       g_free (xdg_dir);
575       g_free (old_dir);
576       return;
577     }
578
579   for (i = 0; filenames[i]; i++)
580     {
581       old_filename = g_build_filename (old_dir, filenames[i], NULL);
582       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
583         {
584           g_free (old_filename);
585           continue;
586         }
587       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
588       old_file = g_file_new_for_path (old_filename);
589       xdg_file = g_file_new_for_path (xdg_filename);
590
591       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
592           NULL, NULL, NULL, NULL))
593         DEBUG ("Failed to migrate %s", filenames[i]);
594
595       g_free (old_filename);
596       g_free (xdg_filename);
597       g_object_unref (old_file);
598       g_object_unref (xdg_file);
599     }
600
601   g_free (xdg_dir);
602   g_free (old_dir);
603 }
604
605 static void
606 show_accounts_ui (EmpathyApp *self,
607     GdkScreen *screen,
608     gboolean if_needed)
609 {
610   empathy_accounts_dialog_show_application (screen,
611       NULL, if_needed, self->start_hidden);
612 }
613
614 static gboolean
615 show_version_cb (const char *option_name,
616     const char *value,
617     gpointer data,
618     GError **error)
619 {
620   g_print ("%s\n", PACKAGE_STRING);
621
622   exit (EXIT_SUCCESS);
623 }
624
625 static void
626 account_manager_ready_cb (GObject *source_object,
627     GAsyncResult *result,
628     gpointer user_data)
629 {
630   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
631   EmpathyApp *self = user_data;
632   GError *error = NULL;
633   TpConnectionPresenceType presence;
634
635   if (!tp_proxy_prepare_finish (manager, result, &error))
636     {
637       GtkWidget *dialog;
638
639       DEBUG ("Failed to prepare account manager: %s", error->message);
640
641       dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
642           GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
643           _("Error contacting the Account Manager"));
644       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
645           _("There was an error while trying to connect to the Telepathy "
646             "Account Manager. The error was:\n\n%s"),
647           error->message);
648
649       gtk_dialog_run (GTK_DIALOG (dialog));
650       gtk_widget_destroy (dialog);
651
652       g_error_free (error);
653       return;
654     }
655
656   /* Autoconnect */
657   presence = tp_account_manager_get_most_available_presence (manager, NULL,
658       NULL);
659
660   if (g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
661       !self->no_connect &&
662       tp_connection_presence_type_cmp_availability
663           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
664             <= 0)
665       /* if current state is Offline, then put it online */
666       empathy_presence_manager_set_state (self->presence_mgr,
667           TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
668
669   /* Pop up the accounts dialog if we don't have any account */
670   if (!empathy_accounts_has_accounts (manager))
671     show_accounts_ui (self, gdk_screen_get_default (), TRUE);
672 }
673
674 static void
675 account_join_chatrooms (TpAccount *account,
676   EmpathyChatroomManager *chatroom_manager)
677 {
678   TpConnection *conn;
679   GList *chatrooms, *p;
680
681   /* Wait if we are not connected or the TpConnection is not prepared yet */
682   conn = tp_account_get_connection (account);
683   if (conn == NULL)
684     return;
685
686   chatrooms = empathy_chatroom_manager_get_chatrooms (
687           chatroom_manager, account);
688
689   for (p = chatrooms; p != NULL; p = p->next)
690     {
691       EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
692
693       if (!empathy_chatroom_get_auto_connect (room))
694         continue;
695
696       empathy_join_muc (account, empathy_chatroom_get_room (room),
697         TP_USER_ACTION_TIME_NOT_USER_ACTION);
698     }
699   g_list_free (chatrooms);
700 }
701
702 static void
703 account_connection_changed_cb (TpAccount *account,
704     GParamSpec *spec,
705     EmpathyChatroomManager *manager)
706 {
707   account_join_chatrooms (account, manager);
708 }
709
710 static void
711 account_manager_chatroom_ready_cb (GObject *source_object,
712     GAsyncResult *result,
713     gpointer user_data)
714 {
715   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
716   EmpathyChatroomManager *chatroom_manager = user_data;
717   GList *accounts, *l;
718   GError *error = NULL;
719
720   if (!tp_proxy_prepare_finish (account_manager, result, &error))
721     {
722       DEBUG ("Failed to prepare account manager: %s", error->message);
723       g_error_free (error);
724       return;
725     }
726
727   accounts = tp_account_manager_get_valid_accounts (account_manager);
728
729   for (l = accounts; l != NULL; l = g_list_next (l))
730     {
731       TpAccount *account = TP_ACCOUNT (l->data);
732
733       /* Try to join all rooms if we're connected */
734       account_join_chatrooms (account, chatroom_manager);
735
736       /* And/or join them on (re)connection */
737       tp_g_signal_connect_object (account, "notify::connection",
738         G_CALLBACK (account_connection_changed_cb), chatroom_manager, 0);
739     }
740   g_list_free (accounts);
741 }
742
743 static void
744 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
745     GParamSpec *pspec,
746     gpointer user_data)
747 {
748   TpAccountManager *account_manager = user_data;
749
750   tp_proxy_prepare_async (account_manager, NULL,
751       account_manager_chatroom_ready_cb, chatroom_manager);
752 }
753
754 static void
755 empathy_app_constructed (GObject *object)
756 {
757   EmpathyApp *self = (EmpathyApp *) object;
758   gboolean chatroom_manager_ready;
759
760   g_set_application_name (_(PACKAGE_NAME));
761
762   gtk_window_set_default_icon_name ("empathy");
763   textdomain (GETTEXT_PACKAGE);
764
765 #ifdef ENABLE_DEBUG
766   /* Set up debug sender */
767   self->debug_sender = tp_debug_sender_dup ();
768   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
769 #endif
770
771   notify_init (_(PACKAGE_NAME));
772
773   /* Setting up Idle */
774   self->presence_mgr = empathy_presence_manager_dup_singleton ();
775
776   self->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
777
778   /* Setting up Connectivity */
779   self->connectivity = empathy_connectivity_dup_singleton ();
780   use_conn_notify_cb (self->gsettings, EMPATHY_PREFS_USE_CONN,
781       self->connectivity);
782   g_signal_connect (self->gsettings,
783       "changed::" EMPATHY_PREFS_USE_CONN,
784       G_CALLBACK (use_conn_notify_cb), self->connectivity);
785
786   /* account management */
787   self->account_manager = tp_account_manager_dup ();
788   tp_proxy_prepare_async (self->account_manager, NULL,
789       account_manager_ready_cb, self);
790
791   migrate_config_to_xdg_dir ();
792
793   /* Logging */
794   self->log_manager = tpl_log_manager_dup_singleton ();
795
796   self->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
797
798   g_object_get (self->chatroom_manager, "ready", &chatroom_manager_ready, NULL);
799   if (!chatroom_manager_ready)
800     {
801       g_signal_connect (G_OBJECT (self->chatroom_manager), "notify::ready",
802           G_CALLBACK (chatroom_manager_ready_cb), self->account_manager);
803     }
804   else
805     {
806       chatroom_manager_ready_cb (self->chatroom_manager, NULL,
807           self->account_manager);
808     }
809
810   /* Location mananger */
811 #ifdef HAVE_GEOCLUE
812   self->location_manager = empathy_location_manager_dup_singleton ();
813 #endif
814
815   self->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
816
817   self->activated = FALSE;
818   self->ft_factory = NULL;
819   self->window = NULL;
820 }
821
822 static void
823 add_empathy_features (void)
824 {
825   /* Add 'empathy' specific feature before doing any preparation */
826   EmpathyClientFactory *factory;
827
828   factory = empathy_client_factory_dup ();
829
830   tp_simple_client_factory_add_connection_features_varargs (
831       TP_SIMPLE_CLIENT_FACTORY (factory),
832       /* empathy_connection_aggregator_get_all_groups(), used by
833        * EmpathyGroupsWidget relies on it */
834       TP_CONNECTION_FEATURE_CONTACT_GROUPS,
835       /* empathy_connection_aggregator_dup_all_contacts(), used by
836        * EmpathyEventManager relies on it */
837       TP_CONNECTION_FEATURE_CONTACT_LIST,
838       NULL);
839
840   g_object_unref (factory);
841 }
842
843 int
844 main (int argc, char *argv[])
845 {
846   EmpathyApp *app;
847   gint retval;
848
849   g_thread_init (NULL);
850   g_type_init ();
851
852 #ifdef HAVE_LIBCHAMPLAIN
853   gtk_clutter_init (&argc, &argv);
854 #endif
855
856   g_type_init ();
857   tpy_cli_init ();
858   empathy_init ();
859   gtk_init (&argc, &argv);
860   empathy_gtk_init ();
861
862   add_empathy_features ();
863
864   app = g_object_new (EMPATHY_TYPE_APP,
865       "application-id", EMPATHY_DBUS_NAME,
866       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
867       NULL);
868
869   retval = g_application_run (G_APPLICATION (app), argc, argv);
870
871   notify_uninit ();
872   xmlCleanupParser ();
873
874   g_object_unref (app);
875
876   return retval;
877 }