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