]> git.0d.be Git - empathy.git/blob - src/empathy.c
debug-window: fix messages array leak
[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     {
358       empathy_roster_window_show_preferences (
359           EMPATHY_ROSTER_WINDOW (self->window), self->preferences_tab);
360
361       self->show_preferences = FALSE;
362     }
363   else
364     {
365       if (!self->start_hidden)
366         empathy_window_present (GTK_WINDOW (self->window));
367     }
368
369   /* Display the accounts dialog if needed */
370   tp_proxy_prepare_async (self->account_manager, NULL,
371       account_manager_ready_cb, self);
372
373   return 0;
374 }
375
376 static gboolean
377 preferences_cb (const char *option_name,
378     const char *value,
379     gpointer data,
380     GError **error)
381 {
382   EmpathyApp *self = data;
383
384   self->show_preferences = TRUE;
385
386   g_free (self->preferences_tab);
387   self->preferences_tab = g_strdup (value);
388
389   return TRUE;
390 }
391
392 static gboolean
393 show_version_cb (const char *option_name,
394     const char *value,
395     gpointer data,
396     GError **error);
397
398 static gboolean
399 empathy_app_local_command_line (GApplication *app,
400     gchar ***arguments,
401     gint *exit_status)
402 {
403   EmpathyApp *self = (EmpathyApp *) app;
404   gint i;
405   gchar **argv;
406   gint argc = 0;
407   gboolean retval = FALSE;
408   GError *error = NULL;
409   gboolean no_connect = FALSE, start_hidden = FALSE;
410
411   GOptionContext *optcontext;
412   GOptionGroup *group;
413   GOptionEntry options[] = {
414       { "no-connect", 'n',
415         0, G_OPTION_ARG_NONE, &no_connect,
416         N_("Don't connect on startup"),
417         NULL },
418       { "start-hidden", 'h',
419         0, G_OPTION_ARG_NONE, &start_hidden,
420         N_("Don't display the contact list or any other dialogs on startup"),
421         NULL },
422       { "show-preferences", 'p',
423         G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, &preferences_cb,
424         NULL, NULL },
425       { "version", 'v',
426         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
427         NULL, NULL },
428       { NULL }
429   };
430
431   /* We create a group so that GOptionArgFuncs get the user data */
432   group = g_option_group_new ("empathy", NULL, NULL, app, NULL);
433   g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
434   g_option_group_add_entries (group, options);
435
436   optcontext = g_option_context_new (N_("- Empathy IM Client"));
437   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
438   g_option_context_set_main_group (optcontext, group);
439   g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
440
441   argc = g_strv_length (*arguments);
442
443   /* We dup the args because g_option_context_parse() sets things to NULL,
444    * but we want to parse all the command line to the primary instance
445    * if necessary. */
446   argv = g_new (gchar*, argc + 1);
447   for (i = 0; i <= argc; i++)
448     argv[i] = (*arguments)[i];
449
450   if (!g_option_context_parse (optcontext, &argc, &argv, &error))
451     {
452       g_print ("%s\nRun '%s --help' to see a full list of available command "
453           "line options.\n",
454           error->message, argv[0]);
455       g_warning ("Error in empathy init: %s", error->message);
456
457       *exit_status = EXIT_FAILURE;
458       retval = TRUE;
459     }
460
461   g_free (argv);
462
463   g_option_context_free (optcontext);
464
465   self->no_connect = no_connect;
466   self->start_hidden = start_hidden;
467
468   return retval;
469 }
470
471 static void empathy_app_constructed (GObject *object);
472
473 static void
474 empathy_app_class_init (EmpathyAppClass *klass)
475 {
476   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
477   GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
478   GParamSpec *spec;
479
480   gobject_class->set_property = empathy_app_set_property;
481   gobject_class->constructed = empathy_app_constructed;
482   gobject_class->dispose = empathy_app_dispose;
483   gobject_class->finalize = empathy_app_finalize;
484
485   g_app_class->command_line = empathy_app_command_line;
486   g_app_class->local_command_line = empathy_app_local_command_line;
487
488   spec = g_param_spec_boolean ("no-connect", "no connect",
489       "Don't connect 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_NO_CONNECT, spec);
493
494   spec = g_param_spec_boolean ("start-hidden", "start hidden",
495       "Don't display the contact list or any other dialogs on startup",
496       FALSE,
497       G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
498   g_object_class_install_property (gobject_class, PROP_START_HIDDEN, spec);
499 }
500
501 static void
502 empathy_app_init (EmpathyApp *self)
503 {
504 }
505
506 static void
507 migrate_config_to_xdg_dir (void)
508 {
509   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
510   int i;
511   GFile *xdg_file, *old_file;
512   static const gchar* filenames[] = {
513     "geometry.ini",
514     "irc-networks.xml",
515     "chatrooms.xml",
516     "contact-groups.xml",
517     "status-presets.xml",
518     "accels.txt",
519     NULL
520   };
521
522   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
523   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
524     {
525       /* xdg config dir already exists */
526       g_free (xdg_dir);
527       return;
528     }
529
530   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
531       PACKAGE_NAME, NULL);
532   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
533     {
534       /* old config dir didn't exist */
535       g_free (xdg_dir);
536       g_free (old_dir);
537       return;
538     }
539
540   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
541     {
542       DEBUG ("Failed to create configuration directory; aborting migration");
543       g_free (xdg_dir);
544       g_free (old_dir);
545       return;
546     }
547
548   for (i = 0; filenames[i]; i++)
549     {
550       old_filename = g_build_filename (old_dir, filenames[i], NULL);
551       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
552         {
553           g_free (old_filename);
554           continue;
555         }
556       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
557       old_file = g_file_new_for_path (old_filename);
558       xdg_file = g_file_new_for_path (xdg_filename);
559
560       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
561           NULL, NULL, NULL, NULL))
562         DEBUG ("Failed to migrate %s", filenames[i]);
563
564       g_free (old_filename);
565       g_free (xdg_filename);
566       g_object_unref (old_file);
567       g_object_unref (xdg_file);
568     }
569
570   g_free (xdg_dir);
571   g_free (old_dir);
572 }
573
574 static void
575 show_accounts_ui (EmpathyApp *self,
576     GdkScreen *screen,
577     gboolean if_needed)
578 {
579   empathy_accounts_dialog_show_application (screen,
580       NULL, if_needed, self->start_hidden);
581 }
582
583 static gboolean
584 show_version_cb (const char *option_name,
585     const char *value,
586     gpointer data,
587     GError **error)
588 {
589   g_print ("%s\n", PACKAGE_STRING);
590
591   exit (EXIT_SUCCESS);
592 }
593
594 static void
595 account_manager_ready_cb (GObject *source_object,
596     GAsyncResult *result,
597     gpointer user_data)
598 {
599   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
600   EmpathyApp *self = user_data;
601   GError *error = NULL;
602   TpConnectionPresenceType presence;
603
604   if (!tp_proxy_prepare_finish (manager, result, &error))
605     {
606       GtkWidget *dialog;
607
608       DEBUG ("Failed to prepare account manager: %s", error->message);
609
610       dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
611           GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
612           _("Error contacting the Account Manager"));
613       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
614           _("There was an error while trying to connect to the Telepathy "
615             "Account Manager. The error was:\n\n%s"),
616           error->message);
617
618       gtk_dialog_run (GTK_DIALOG (dialog));
619       gtk_widget_destroy (dialog);
620
621       g_error_free (error);
622       return;
623     }
624
625   /* Autoconnect */
626   presence = tp_account_manager_get_most_available_presence (manager, NULL,
627       NULL);
628
629   if (g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
630       !self->no_connect &&
631       tp_connection_presence_type_cmp_availability
632           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
633             <= 0)
634       /* if current state is Offline, then put it online */
635       empathy_presence_manager_set_state (self->presence_mgr,
636           TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
637
638   /* Pop up the accounts dialog if we don't have any account */
639   if (!empathy_accounts_has_accounts (manager))
640     show_accounts_ui (self, gdk_screen_get_default (), TRUE);
641 }
642
643 static void
644 account_join_chatrooms (TpAccount *account,
645   EmpathyChatroomManager *chatroom_manager)
646 {
647   TpConnection *conn;
648   GList *chatrooms, *p;
649
650   /* Wait if we are not connected or the TpConnection is not prepared yet */
651   conn = tp_account_get_connection (account);
652   if (conn == NULL)
653     return;
654
655   chatrooms = empathy_chatroom_manager_get_chatrooms (
656           chatroom_manager, account);
657
658   for (p = chatrooms; p != NULL; p = p->next)
659     {
660       EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
661
662       if (!empathy_chatroom_get_auto_connect (room))
663         continue;
664
665       empathy_join_muc (account, empathy_chatroom_get_room (room),
666         TP_USER_ACTION_TIME_NOT_USER_ACTION);
667     }
668   g_list_free (chatrooms);
669 }
670
671 static void
672 account_connection_changed_cb (TpAccount *account,
673     GParamSpec *spec,
674     EmpathyChatroomManager *manager)
675 {
676   account_join_chatrooms (account, manager);
677 }
678
679 static void
680 account_manager_chatroom_ready_cb (GObject *source_object,
681     GAsyncResult *result,
682     gpointer user_data)
683 {
684   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
685   EmpathyChatroomManager *chatroom_manager = user_data;
686   GList *accounts, *l;
687   GError *error = NULL;
688
689   if (!tp_proxy_prepare_finish (account_manager, result, &error))
690     {
691       DEBUG ("Failed to prepare account manager: %s", error->message);
692       g_error_free (error);
693       return;
694     }
695
696   accounts = tp_account_manager_dup_valid_accounts (account_manager);
697
698   for (l = accounts; l != NULL; l = g_list_next (l))
699     {
700       TpAccount *account = TP_ACCOUNT (l->data);
701
702       /* Try to join all rooms if we're connected */
703       account_join_chatrooms (account, chatroom_manager);
704
705       /* And/or join them on (re)connection */
706       tp_g_signal_connect_object (account, "notify::connection",
707         G_CALLBACK (account_connection_changed_cb), chatroom_manager, 0);
708     }
709   g_list_free_full (accounts, g_object_unref);
710 }
711
712 static void
713 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
714     GParamSpec *pspec,
715     gpointer user_data)
716 {
717   TpAccountManager *account_manager = user_data;
718
719   tp_proxy_prepare_async (account_manager, NULL,
720       account_manager_chatroom_ready_cb, chatroom_manager);
721 }
722
723 static void
724 empathy_app_constructed (GObject *object)
725 {
726   EmpathyApp *self = (EmpathyApp *) object;
727   gboolean chatroom_manager_ready;
728
729   textdomain (GETTEXT_PACKAGE);
730   g_set_application_name (_(PACKAGE_NAME));
731
732   gtk_window_set_default_icon_name ("empathy");
733
734 #ifdef ENABLE_DEBUG
735   /* Set up debug sender */
736   self->debug_sender = tp_debug_sender_dup ();
737   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
738 #endif
739
740   notify_init (_(PACKAGE_NAME));
741
742   /* Setting up Idle */
743   self->presence_mgr = empathy_presence_manager_dup_singleton ();
744
745   self->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
746
747   /* account management */
748   self->account_manager = tp_account_manager_dup ();
749   tp_proxy_prepare_async (self->account_manager, NULL,
750       account_manager_ready_cb, self);
751
752   tp_account_manager_enable_restart (self->account_manager);
753
754   migrate_config_to_xdg_dir ();
755
756   /* Logging */
757   self->log_manager = tpl_log_manager_dup_singleton ();
758
759   self->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
760
761   g_object_get (self->chatroom_manager, "ready", &chatroom_manager_ready, NULL);
762   if (!chatroom_manager_ready)
763     {
764       g_signal_connect (G_OBJECT (self->chatroom_manager), "notify::ready",
765           G_CALLBACK (chatroom_manager_ready_cb), self->account_manager);
766     }
767   else
768     {
769       chatroom_manager_ready_cb (self->chatroom_manager, NULL,
770           self->account_manager);
771     }
772
773   /* Location mananger */
774 #ifdef HAVE_GEOCLUE
775   self->location_manager = empathy_location_manager_dup_singleton ();
776 #endif
777
778   self->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
779
780   self->activated = FALSE;
781   self->ft_factory = NULL;
782   self->window = NULL;
783 }
784
785 static void
786 add_empathy_features (void)
787 {
788   /* Add 'empathy' specific feature before doing any preparation */
789   EmpathyClientFactory *factory;
790
791   factory = empathy_client_factory_dup ();
792
793   tp_simple_client_factory_add_connection_features_varargs (
794       TP_SIMPLE_CLIENT_FACTORY (factory),
795       /* empathy_connection_aggregator_get_all_groups(), used by
796        * EmpathyGroupsWidget relies on it */
797       TP_CONNECTION_FEATURE_CONTACT_GROUPS,
798       /* empathy_connection_aggregator_dup_all_contacts(), used by
799        * EmpathyEventManager relies on it */
800       TP_CONNECTION_FEATURE_CONTACT_LIST,
801       NULL);
802
803   g_object_unref (factory);
804 }
805
806 int
807 main (int argc, char *argv[])
808 {
809   EmpathyApp *app;
810   gint retval;
811
812   g_type_init ();
813
814 #ifdef HAVE_LIBCHAMPLAIN
815   g_return_val_if_fail (gtk_clutter_init (&argc, &argv) ==
816       CLUTTER_INIT_SUCCESS, 1);
817 #endif
818
819   g_type_init ();
820   empathy_init ();
821   gtk_init (&argc, &argv);
822   empathy_gtk_init ();
823
824   add_empathy_features ();
825
826   app = g_object_new (EMPATHY_TYPE_APP,
827       "application-id", EMPATHY_DBUS_NAME,
828       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
829       NULL);
830
831   retval = g_application_run (G_APPLICATION (app), argc, argv);
832
833   notify_uninit ();
834   xmlCleanupParser ();
835
836   g_object_unref (app);
837
838   return retval;
839 }