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