2 * Copyright (C) 2007-2009 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include <glib/gstdio.h>
25 #include <glib/gi18n.h>
27 #ifdef HAVE_LIBCHAMPLAIN
28 #include <clutter-gtk/clutter-gtk.h>
31 #include <libnotify/notify.h>
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>
42 #include <libempathy-gtk/empathy-ui-utils.h>
43 #include <libempathy-gtk/empathy-location-manager.h>
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"
52 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
53 #include <libempathy/empathy-debug.h>
55 #define EMPATHY_DBUS_NAME "org.gnome.Empathy"
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))
64 typedef struct _EmpathyApp EmpathyApp;
65 typedef struct _EmpathyAppClass EmpathyAppClass;
73 GType empathy_app_get_type (void);
75 struct _EmpathyAppClass
77 GtkApplicationClass parent_class;
82 GtkApplication parent;
86 gboolean start_hidden;
87 gboolean show_preferences;
88 gchar *preferences_tab;
93 EmpathyStatusIcon *icon;
94 TpAccountManager *account_manager;
95 TplLogManager *log_manager;
96 EmpathyChatroomManager *chatroom_manager;
97 EmpathyFTFactory *ft_factory;
98 EmpathyPresenceManager *presence_mgr;
100 EmpathyNotificationsApprover *notifications_approver;
101 EmpathyConnectionAggregator *conn_aggregator;
103 EmpathyLocationManager *location_manager;
106 TpDebugSender *debug_sender;
109 gboolean shell_running;
113 G_DEFINE_TYPE(EmpathyApp, empathy_app, GTK_TYPE_APPLICATION)
116 empathy_app_dispose (GObject *object)
118 EmpathyApp *self = EMPATHY_APP (object);
119 void (*dispose) (GObject *) =
120 G_OBJECT_CLASS (empathy_app_parent_class)->dispose;
122 /* Only set our presence to offline when exiting if GNOME Shell is not
124 if (self->presence_mgr != NULL &&
125 !self->shell_running)
127 empathy_presence_manager_set_state (self->presence_mgr,
128 TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
132 tp_clear_object (&self->debug_sender);
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);
141 tp_clear_object (&self->location_manager);
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);
153 empathy_app_finalize (GObject *object)
155 EmpathyApp *self = EMPATHY_APP (object);
156 void (*finalize) (GObject *) =
157 G_OBJECT_CLASS (empathy_app_parent_class)->finalize;
159 g_free (self->preferences_tab);
161 if (finalize != NULL)
165 static void account_manager_ready_cb (GObject *source_object,
166 GAsyncResult *result,
170 empathy_app_set_property (GObject *object,
175 EmpathyApp *self = EMPATHY_APP (object);
179 case PROP_NO_CONNECT:
180 self->no_connect = g_value_get_boolean (value);
182 case PROP_START_HIDDEN:
183 self->start_hidden = g_value_get_boolean (value);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 new_incoming_transfer_cb (EmpathyFTFactory *factory,
193 EmpathyFTHandler *handler,
198 empathy_ft_manager_display_error (handler, error);
200 empathy_receive_file_with_file_chooser (handler);
204 new_ft_handler_cb (EmpathyFTFactory *factory,
205 EmpathyFTHandler *handler,
210 empathy_ft_manager_display_error (handler, error);
212 empathy_ft_manager_add_handler (handler);
214 g_object_unref (handler);
218 empathy_app_local_command_line (GApplication *app,
223 empathy_presence_manager_set_auto_away_cb (GSettings *gsettings,
227 EmpathyPresenceManager *presence_mgr = user_data;
229 empathy_presence_manager_set_auto_away (presence_mgr,
230 g_settings_get_boolean (gsettings, key));
233 #define GNOME_SHELL_BUS_NAME "org.gnome.Shell"
236 list_names_cb (TpDBusDaemon *bus_daemon,
237 const gchar * const *names,
240 GObject *weak_object)
242 EmpathyApp *self = (EmpathyApp *) weak_object;
248 for (i = 0; names[i] != NULL; i++)
250 if (!tp_strdiff (names[i], GNOME_SHELL_BUS_NAME))
252 self->shell_running = TRUE;
258 if (self->shell_running)
260 DEBUG ("GNOME Shell is running, don't create status icon");
262 /* Rely on GNOME Shell to watch session state */
263 empathy_presence_manager_set_auto_away (self->presence_mgr, FALSE);
265 empathy_roster_window_set_shell_running (
266 EMPATHY_ROSTER_WINDOW (self->window), TRUE);
272 self->icon = empathy_status_icon_new (GTK_WINDOW (self->window),
275 /* Allow Empathy to watch session state */
276 autoaway = g_settings_get_boolean (self->gsettings,
277 EMPATHY_PREFS_AUTOAWAY);
279 g_signal_connect (self->gsettings,
280 "changed::" EMPATHY_PREFS_AUTOAWAY,
281 G_CALLBACK (empathy_presence_manager_set_auto_away_cb),
284 empathy_presence_manager_set_auto_away (self->presence_mgr, autoaway);
289 empathy_app_command_line (GApplication *app,
290 GApplicationCommandLine *cmdline)
292 EmpathyApp *self = (EmpathyApp *) app;
293 gchar **args, **argv;
294 gint argc, exit_status, i;
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++)
303 if (empathy_app_local_command_line (app, &argv, &exit_status))
304 DEBUG ("failed to parse command line!");
309 if (!self->activated)
311 GError *error = NULL;
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);
321 if (!empathy_ft_factory_register (self->ft_factory, &error))
323 g_warning ("Failed to register FileTransfer handler: %s",
325 g_error_free (error);
328 self->activated = TRUE;
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)));
336 gtk_application_add_window (GTK_APPLICATION (app),
337 GTK_WINDOW (self->window));
338 gtk_application_add_accelerator (GTK_APPLICATION (app),
340 "win." EMPATHY_PREFS_UI_SHOW_OFFLINE,
343 /* check if Shell is running */
344 dbus = tp_dbus_daemon_dup (&error);
345 g_assert_no_error (error);
347 tp_dbus_daemon_list_names (dbus, -1, list_names_cb,
348 self, NULL, G_OBJECT (self));
350 g_object_unref (dbus);
352 self->notifications_approver =
353 empathy_notifications_approver_dup_singleton ();
356 if (self->show_preferences)
358 empathy_roster_window_show_preferences (
359 EMPATHY_ROSTER_WINDOW (self->window), self->preferences_tab);
361 self->show_preferences = FALSE;
365 if (!self->start_hidden)
366 empathy_window_present (GTK_WINDOW (self->window));
369 /* Display the accounts dialog if needed */
370 tp_proxy_prepare_async (self->account_manager, NULL,
371 account_manager_ready_cb, self);
377 preferences_cb (const char *option_name,
382 EmpathyApp *self = data;
384 self->show_preferences = TRUE;
386 g_free (self->preferences_tab);
387 self->preferences_tab = g_strdup (value);
393 show_version_cb (const char *option_name,
399 empathy_app_local_command_line (GApplication *app,
403 EmpathyApp *self = (EmpathyApp *) app;
407 gboolean retval = FALSE;
408 GError *error = NULL;
409 gboolean no_connect = FALSE, start_hidden = FALSE;
411 GOptionContext *optcontext;
413 GOptionEntry options[] = {
415 0, G_OPTION_ARG_NONE, &no_connect,
416 N_("Don't connect on startup"),
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"),
422 { "show-preferences", 'p',
423 G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, &preferences_cb,
426 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
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);
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);
441 argc = g_strv_length (*arguments);
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
446 argv = g_new (gchar*, argc + 1);
447 for (i = 0; i <= argc; i++)
448 argv[i] = (*arguments)[i];
450 if (!g_option_context_parse (optcontext, &argc, &argv, &error))
452 g_print ("%s\nRun '%s --help' to see a full list of available command "
454 error->message, argv[0]);
455 g_warning ("Error in empathy init: %s", error->message);
457 *exit_status = EXIT_FAILURE;
463 g_option_context_free (optcontext);
465 self->no_connect = no_connect;
466 self->start_hidden = start_hidden;
471 static void empathy_app_constructed (GObject *object);
474 empathy_app_class_init (EmpathyAppClass *klass)
476 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
477 GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
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;
485 g_app_class->command_line = empathy_app_command_line;
486 g_app_class->local_command_line = empathy_app_local_command_line;
488 spec = g_param_spec_boolean ("no-connect", "no connect",
489 "Don't connect on startup",
491 G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
492 g_object_class_install_property (gobject_class, PROP_NO_CONNECT, spec);
494 spec = g_param_spec_boolean ("start-hidden", "start hidden",
495 "Don't display the contact list or any other dialogs on startup",
497 G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
498 g_object_class_install_property (gobject_class, PROP_START_HIDDEN, spec);
502 empathy_app_init (EmpathyApp *self)
507 migrate_config_to_xdg_dir (void)
509 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
511 GFile *xdg_file, *old_file;
512 static const gchar* filenames[] = {
516 "contact-groups.xml",
517 "status-presets.xml",
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))
525 /* xdg config dir already exists */
530 old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
532 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
534 /* old config dir didn't exist */
540 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
542 DEBUG ("Failed to create configuration directory; aborting migration");
548 for (i = 0; filenames[i]; i++)
550 old_filename = g_build_filename (old_dir, filenames[i], NULL);
551 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
553 g_free (old_filename);
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);
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]);
564 g_free (old_filename);
565 g_free (xdg_filename);
566 g_object_unref (old_file);
567 g_object_unref (xdg_file);
575 show_accounts_ui (EmpathyApp *self,
579 empathy_accounts_dialog_show_application (screen,
580 NULL, if_needed, self->start_hidden);
584 show_version_cb (const char *option_name,
589 g_print ("%s\n", PACKAGE_STRING);
595 account_manager_ready_cb (GObject *source_object,
596 GAsyncResult *result,
599 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
600 EmpathyApp *self = user_data;
601 GError *error = NULL;
602 TpConnectionPresenceType presence;
604 if (!tp_proxy_prepare_finish (manager, result, &error))
608 DEBUG ("Failed to prepare account manager: %s", error->message);
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"),
618 gtk_dialog_run (GTK_DIALOG (dialog));
619 gtk_widget_destroy (dialog);
621 g_error_free (error);
626 presence = tp_account_manager_get_most_available_presence (manager, NULL,
629 if (g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
631 tp_connection_presence_type_cmp_availability
632 (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
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);
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);
644 account_join_chatrooms (TpAccount *account,
645 EmpathyChatroomManager *chatroom_manager)
648 GList *chatrooms, *p;
650 /* Wait if we are not connected or the TpConnection is not prepared yet */
651 conn = tp_account_get_connection (account);
655 chatrooms = empathy_chatroom_manager_get_chatrooms (
656 chatroom_manager, account);
658 for (p = chatrooms; p != NULL; p = p->next)
660 EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
662 if (!empathy_chatroom_get_auto_connect (room))
665 empathy_join_muc (account, empathy_chatroom_get_room (room),
666 TP_USER_ACTION_TIME_NOT_USER_ACTION);
668 g_list_free (chatrooms);
672 account_connection_changed_cb (TpAccount *account,
674 EmpathyChatroomManager *manager)
676 account_join_chatrooms (account, manager);
680 account_manager_chatroom_ready_cb (GObject *source_object,
681 GAsyncResult *result,
684 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
685 EmpathyChatroomManager *chatroom_manager = user_data;
687 GError *error = NULL;
689 if (!tp_proxy_prepare_finish (account_manager, result, &error))
691 DEBUG ("Failed to prepare account manager: %s", error->message);
692 g_error_free (error);
696 accounts = tp_account_manager_dup_valid_accounts (account_manager);
698 for (l = accounts; l != NULL; l = g_list_next (l))
700 TpAccount *account = TP_ACCOUNT (l->data);
702 /* Try to join all rooms if we're connected */
703 account_join_chatrooms (account, chatroom_manager);
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);
709 g_list_free_full (accounts, g_object_unref);
713 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
717 TpAccountManager *account_manager = user_data;
719 tp_proxy_prepare_async (account_manager, NULL,
720 account_manager_chatroom_ready_cb, chatroom_manager);
724 empathy_app_constructed (GObject *object)
726 EmpathyApp *self = (EmpathyApp *) object;
727 gboolean chatroom_manager_ready;
729 textdomain (GETTEXT_PACKAGE);
730 g_set_application_name (_(PACKAGE_NAME));
732 gtk_window_set_default_icon_name ("empathy");
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);
740 notify_init (_(PACKAGE_NAME));
742 /* Setting up Idle */
743 self->presence_mgr = empathy_presence_manager_dup_singleton ();
745 self->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
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);
752 tp_account_manager_enable_restart (self->account_manager);
754 migrate_config_to_xdg_dir ();
757 self->log_manager = tpl_log_manager_dup_singleton ();
759 self->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
761 g_object_get (self->chatroom_manager, "ready", &chatroom_manager_ready, NULL);
762 if (!chatroom_manager_ready)
764 g_signal_connect (G_OBJECT (self->chatroom_manager), "notify::ready",
765 G_CALLBACK (chatroom_manager_ready_cb), self->account_manager);
769 chatroom_manager_ready_cb (self->chatroom_manager, NULL,
770 self->account_manager);
773 /* Location mananger */
775 self->location_manager = empathy_location_manager_dup_singleton ();
778 self->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
780 self->activated = FALSE;
781 self->ft_factory = NULL;
786 add_empathy_features (void)
788 /* Add 'empathy' specific feature before doing any preparation */
789 EmpathyClientFactory *factory;
791 factory = empathy_client_factory_dup ();
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,
803 g_object_unref (factory);
807 main (int argc, char *argv[])
814 #ifdef HAVE_LIBCHAMPLAIN
815 g_return_val_if_fail (gtk_clutter_init (&argc, &argv) ==
816 CLUTTER_INIT_SUCCESS, 1);
821 gtk_init (&argc, &argv);
824 add_empathy_features ();
826 app = g_object_new (EMPATHY_TYPE_APP,
827 "application-id", EMPATHY_DBUS_NAME,
828 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
831 retval = g_application_run (G_APPLICATION (app), argc, argv);
836 g_object_unref (app);