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 "empathy-client-factory.h"
34 #include "empathy-connection-aggregator.h"
35 #include "empathy-presence-manager.h"
36 #include "empathy-utils.h"
37 #include "empathy-chatroom-manager.h"
38 #include "empathy-request-util.h"
39 #include "empathy-ft-factory.h"
40 #include "empathy-gsettings.h"
42 #include "empathy-ui-utils.h"
43 #include "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 "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)
357 empathy_roster_window_show_preferences (
358 EMPATHY_ROSTER_WINDOW (self->window), self->preferences_tab);
360 if (!self->start_hidden)
361 empathy_window_present (GTK_WINDOW (self->window));
363 /* Display the accounts dialog if needed */
364 tp_proxy_prepare_async (self->account_manager, NULL,
365 account_manager_ready_cb, self);
371 preferences_cb (const char *option_name,
376 EmpathyApp *self = data;
378 self->show_preferences = TRUE;
380 g_free (self->preferences_tab);
381 self->preferences_tab = g_strdup (value);
387 show_version_cb (const char *option_name,
393 empathy_app_local_command_line (GApplication *app,
397 EmpathyApp *self = (EmpathyApp *) app;
401 gboolean retval = FALSE;
402 GError *error = NULL;
403 gboolean no_connect = FALSE, start_hidden = FALSE;
405 GOptionContext *optcontext;
407 GOptionEntry options[] = {
409 0, G_OPTION_ARG_NONE, &no_connect,
410 N_("Don't connect on startup"),
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"),
416 { "show-preferences", 'p',
417 G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, &preferences_cb,
420 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
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);
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);
435 argc = g_strv_length (*arguments);
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
440 argv = g_new (gchar*, argc + 1);
441 for (i = 0; i <= argc; i++)
442 argv[i] = (*arguments)[i];
444 if (!g_option_context_parse (optcontext, &argc, &argv, &error))
446 g_print ("%s\nRun '%s --help' to see a full list of available command "
448 error->message, argv[0]);
449 g_warning ("Error in empathy init: %s", error->message);
451 *exit_status = EXIT_FAILURE;
457 g_option_context_free (optcontext);
459 self->no_connect = no_connect;
460 self->start_hidden = start_hidden;
465 static void empathy_app_constructed (GObject *object);
468 empathy_app_class_init (EmpathyAppClass *klass)
470 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
471 GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
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;
479 g_app_class->command_line = empathy_app_command_line;
480 g_app_class->local_command_line = empathy_app_local_command_line;
482 spec = g_param_spec_boolean ("no-connect", "no connect",
483 "Don't connect on startup",
485 G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
486 g_object_class_install_property (gobject_class, PROP_NO_CONNECT, spec);
488 spec = g_param_spec_boolean ("start-hidden", "start hidden",
489 "Don't display the contact list or any other dialogs on startup",
491 G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
492 g_object_class_install_property (gobject_class, PROP_START_HIDDEN, spec);
496 empathy_app_init (EmpathyApp *self)
501 migrate_config_to_xdg_dir (void)
503 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
505 GFile *xdg_file, *old_file;
506 static const gchar* filenames[] = {
510 "contact-groups.xml",
511 "status-presets.xml",
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))
519 /* xdg config dir already exists */
524 old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
526 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
528 /* old config dir didn't exist */
534 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
536 DEBUG ("Failed to create configuration directory; aborting migration");
542 for (i = 0; filenames[i]; i++)
544 old_filename = g_build_filename (old_dir, filenames[i], NULL);
545 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
547 g_free (old_filename);
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);
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]);
558 g_free (old_filename);
559 g_free (xdg_filename);
560 g_object_unref (old_file);
561 g_object_unref (xdg_file);
569 show_accounts_ui (EmpathyApp *self,
573 empathy_accounts_dialog_show_application (screen,
574 NULL, if_needed, self->start_hidden);
578 show_version_cb (const char *option_name,
583 g_print ("%s\n", PACKAGE_STRING);
589 account_manager_ready_cb (GObject *source_object,
590 GAsyncResult *result,
593 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
594 EmpathyApp *self = user_data;
595 GError *error = NULL;
596 TpConnectionPresenceType presence;
598 if (!tp_proxy_prepare_finish (manager, result, &error))
602 DEBUG ("Failed to prepare account manager: %s", error->message);
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"),
612 gtk_dialog_run (GTK_DIALOG (dialog));
613 gtk_widget_destroy (dialog);
615 g_error_free (error);
620 presence = tp_account_manager_get_most_available_presence (manager, NULL,
623 if (g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
625 tp_connection_presence_type_cmp_availability
626 (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
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);
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);
638 account_join_chatrooms (TpAccount *account,
639 EmpathyChatroomManager *chatroom_manager)
642 GList *chatrooms, *p;
644 /* Wait if we are not connected or the TpConnection is not prepared yet */
645 conn = tp_account_get_connection (account);
649 chatrooms = empathy_chatroom_manager_get_chatrooms (
650 chatroom_manager, account);
652 for (p = chatrooms; p != NULL; p = p->next)
654 EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
656 if (!empathy_chatroom_get_auto_connect (room))
659 empathy_join_muc (account, empathy_chatroom_get_room (room),
660 TP_USER_ACTION_TIME_NOT_USER_ACTION);
662 g_list_free (chatrooms);
666 account_connection_changed_cb (TpAccount *account,
668 EmpathyChatroomManager *manager)
670 account_join_chatrooms (account, manager);
674 account_manager_chatroom_ready_cb (GObject *source_object,
675 GAsyncResult *result,
678 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
679 EmpathyChatroomManager *chatroom_manager = user_data;
681 GError *error = NULL;
683 if (!tp_proxy_prepare_finish (account_manager, result, &error))
685 DEBUG ("Failed to prepare account manager: %s", error->message);
686 g_error_free (error);
690 accounts = tp_account_manager_dup_valid_accounts (account_manager);
692 for (l = accounts; l != NULL; l = g_list_next (l))
694 TpAccount *account = TP_ACCOUNT (l->data);
696 /* Try to join all rooms if we're connected */
697 account_join_chatrooms (account, chatroom_manager);
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);
703 g_list_free_full (accounts, g_object_unref);
707 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
711 TpAccountManager *account_manager = user_data;
713 tp_proxy_prepare_async (account_manager, NULL,
714 account_manager_chatroom_ready_cb, chatroom_manager);
718 empathy_app_constructed (GObject *object)
720 EmpathyApp *self = (EmpathyApp *) object;
721 gboolean chatroom_manager_ready;
723 textdomain (GETTEXT_PACKAGE);
724 g_set_application_name (_(PACKAGE_NAME));
726 gtk_window_set_default_icon_name ("empathy");
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);
734 notify_init (_(PACKAGE_NAME));
736 /* Setting up Idle */
737 self->presence_mgr = empathy_presence_manager_dup_singleton ();
739 self->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
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);
746 tp_account_manager_enable_restart (self->account_manager);
748 migrate_config_to_xdg_dir ();
751 self->log_manager = tpl_log_manager_dup_singleton ();
753 self->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
755 g_object_get (self->chatroom_manager, "ready", &chatroom_manager_ready, NULL);
756 if (!chatroom_manager_ready)
758 g_signal_connect (G_OBJECT (self->chatroom_manager), "notify::ready",
759 G_CALLBACK (chatroom_manager_ready_cb), self->account_manager);
763 chatroom_manager_ready_cb (self->chatroom_manager, NULL,
764 self->account_manager);
767 /* Location mananger */
769 self->location_manager = empathy_location_manager_dup_singleton ();
772 self->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
774 self->activated = FALSE;
775 self->ft_factory = NULL;
780 add_empathy_features (void)
782 /* Add 'empathy' specific feature before doing any preparation */
783 EmpathyClientFactory *factory;
785 factory = empathy_client_factory_dup ();
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,
797 g_object_unref (factory);
801 main (int argc, char *argv[])
808 #ifdef HAVE_LIBCHAMPLAIN
809 g_return_val_if_fail (gtk_clutter_init (&argc, &argv) ==
810 CLUTTER_INIT_SUCCESS, 1);
815 gtk_init (&argc, &argv);
818 add_empathy_features ();
820 app = g_object_new (EMPATHY_TYPE_APP,
821 "application-id", EMPATHY_DBUS_NAME,
822 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
825 retval = g_application_run (G_APPLICATION (app), argc, argv);
830 g_object_unref (app);