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>
29 #include <glib/gstdio.h>
30 #include <glib/gi18n.h>
34 #ifdef HAVE_LIBCHAMPLAIN
35 #include <clutter-gtk/clutter-gtk.h>
38 #include <libnotify/notify.h>
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>
47 #include <telepathy-yell/telepathy-yell.h>
49 #include <telepathy-logger/log-manager.h>
51 #include <libempathy/empathy-client-factory.h>
52 #include <libempathy/empathy-connection-aggregator.h>
53 #include <libempathy/empathy-presence-manager.h>
54 #include <libempathy/empathy-utils.h>
55 #include <libempathy/empathy-chatroom-manager.h>
56 #include <libempathy/empathy-account-settings.h>
57 #include <libempathy/empathy-connectivity.h>
58 #include <libempathy/empathy-connection-managers.h>
59 #include <libempathy/empathy-request-util.h>
60 #include <libempathy/empathy-ft-factory.h>
61 #include <libempathy/empathy-gsettings.h>
62 #include <libempathy/empathy-tp-chat.h>
64 #include <libempathy-gtk/empathy-ui-utils.h>
65 #include <libempathy-gtk/empathy-location-manager.h>
66 #include <libempathy-gtk/empathy-notify-manager.h>
68 #include "empathy-roster-window.h"
69 #include "empathy-accounts-common.h"
70 #include "empathy-accounts-dialog.h"
71 #include "empathy-status-icon.h"
72 #include "empathy-ft-manager.h"
73 #include "empathy-notifications-approver.h"
75 #include "extensions/extensions.h"
77 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
78 #include <libempathy/empathy-debug.h>
80 #define EMPATHY_DBUS_NAME "org.gnome.Empathy"
82 #define EMPATHY_TYPE_APP (empathy_app_get_type ())
83 #define EMPATHY_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_APP, EmpathyApp))
84 #define EMPATHY_APP_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST ((obj), EMPATHY_TYPE_APP, EmpathyAppClass))
85 #define EMPATHY_IS_EMPATHY_APP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_APP))
86 #define EMPATHY_IS_EMPATHY_APP_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((obj), EMPATHY_TYPE_APP))
87 #define EMPATHY_APP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EMPATHY_TYPE_APP, EmpathyAppClass))
89 typedef struct _EmpathyApp EmpathyApp;
90 typedef struct _EmpathyAppClass EmpathyAppClass;
98 GType empathy_app_get_type (void);
100 struct _EmpathyAppClass
102 GtkApplicationClass parent_class;
107 GtkApplication parent;
111 gboolean start_hidden;
112 gboolean show_preferences;
113 gchar *preferences_tab;
118 EmpathyStatusIcon *icon;
119 TpAccountManager *account_manager;
120 TplLogManager *log_manager;
121 EmpathyChatroomManager *chatroom_manager;
122 EmpathyFTFactory *ft_factory;
123 EmpathyPresenceManager *presence_mgr;
124 EmpathyConnectivity *connectivity;
125 GSettings *gsettings;
126 EmpathyNotificationsApprover *notifications_approver;
127 EmpathyConnectionAggregator *conn_aggregator;
129 EmpathyLocationManager *location_manager;
132 TpDebugSender *debug_sender;
135 gboolean shell_running;
139 G_DEFINE_TYPE(EmpathyApp, empathy_app, GTK_TYPE_APPLICATION)
142 empathy_app_dispose (GObject *object)
144 EmpathyApp *self = EMPATHY_APP (object);
145 void (*dispose) (GObject *) =
146 G_OBJECT_CLASS (empathy_app_parent_class)->dispose;
148 /* Only set our presence to offline when exiting if GNOME Shell is not
150 if (self->presence_mgr != NULL &&
151 !self->shell_running)
153 empathy_presence_manager_set_state (self->presence_mgr,
154 TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
158 tp_clear_object (&self->debug_sender);
161 tp_clear_object (&self->presence_mgr);
162 tp_clear_object (&self->connectivity);
163 tp_clear_object (&self->icon);
164 tp_clear_object (&self->account_manager);
165 tp_clear_object (&self->log_manager);
166 tp_clear_object (&self->chatroom_manager);
168 tp_clear_object (&self->location_manager);
170 tp_clear_object (&self->ft_factory);
171 tp_clear_object (&self->gsettings);
172 tp_clear_object (&self->notifications_approver);
173 tp_clear_object (&self->conn_aggregator);
180 empathy_app_finalize (GObject *object)
182 EmpathyApp *self = EMPATHY_APP (object);
183 void (*finalize) (GObject *) =
184 G_OBJECT_CLASS (empathy_app_parent_class)->finalize;
186 g_free (self->preferences_tab);
188 if (finalize != NULL)
192 static void account_manager_ready_cb (GObject *source_object,
193 GAsyncResult *result,
197 empathy_app_set_property (GObject *object,
202 EmpathyApp *self = EMPATHY_APP (object);
206 case PROP_NO_CONNECT:
207 self->no_connect = g_value_get_boolean (value);
209 case PROP_START_HIDDEN:
210 self->start_hidden = g_value_get_boolean (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 new_incoming_transfer_cb (EmpathyFTFactory *factory,
220 EmpathyFTHandler *handler,
225 empathy_ft_manager_display_error (handler, error);
227 empathy_receive_file_with_file_chooser (handler);
231 new_ft_handler_cb (EmpathyFTFactory *factory,
232 EmpathyFTHandler *handler,
237 empathy_ft_manager_display_error (handler, error);
239 empathy_ft_manager_add_handler (handler);
241 g_object_unref (handler);
245 empathy_app_local_command_line (GApplication *app,
250 empathy_presence_manager_set_auto_away_cb (GSettings *gsettings,
254 EmpathyPresenceManager *presence_mgr = user_data;
256 empathy_presence_manager_set_auto_away (presence_mgr,
257 g_settings_get_boolean (gsettings, key));
260 #define GNOME_SHELL_BUS_NAME "org.gnome.Shell"
263 list_names_cb (TpDBusDaemon *bus_daemon,
264 const gchar * const *names,
267 GObject *weak_object)
269 EmpathyApp *self = (EmpathyApp *) weak_object;
275 for (i = 0; names[i] != NULL; i++)
277 if (!tp_strdiff (names[i], GNOME_SHELL_BUS_NAME))
279 self->shell_running = TRUE;
285 if (self->shell_running)
287 DEBUG ("GNOME Shell is running, don't create status icon");
289 /* Rely on GNOME Shell to watch session state */
290 empathy_presence_manager_set_auto_away (self->presence_mgr, FALSE);
292 empathy_roster_window_set_shell_running (
293 EMPATHY_ROSTER_WINDOW (self->window), TRUE);
299 self->icon = empathy_status_icon_new (GTK_WINDOW (self->window),
302 /* Allow Empathy to watch session state */
303 autoaway = g_settings_get_boolean (self->gsettings,
304 EMPATHY_PREFS_AUTOAWAY);
306 g_signal_connect (self->gsettings,
307 "changed::" EMPATHY_PREFS_AUTOAWAY,
308 G_CALLBACK (empathy_presence_manager_set_auto_away_cb),
311 empathy_presence_manager_set_auto_away (self->presence_mgr, autoaway);
316 empathy_app_command_line (GApplication *app,
317 GApplicationCommandLine *cmdline)
319 EmpathyApp *self = (EmpathyApp *) app;
320 gchar **args, **argv;
321 gint argc, exit_status, i;
323 args = g_application_command_line_get_arguments (cmdline, &argc);
324 /* We have to make an extra copy of the array, since g_option_context_parse()
325 * assumes that it can remove strings from the array without freeing them. */
326 argv = g_new (gchar*, argc + 1);
327 for (i = 0; i <= argc; i++)
330 if (empathy_app_local_command_line (app, &argv, &exit_status))
331 DEBUG ("failed to parse command line!");
336 if (!self->activated)
338 GError *error = NULL;
341 /* Create the FT factory */
342 self->ft_factory = empathy_ft_factory_dup_singleton ();
343 g_signal_connect (self->ft_factory, "new-ft-handler",
344 G_CALLBACK (new_ft_handler_cb), NULL);
345 g_signal_connect (self->ft_factory, "new-incoming-transfer",
346 G_CALLBACK (new_incoming_transfer_cb), NULL);
348 if (!empathy_ft_factory_register (self->ft_factory, &error))
350 g_warning ("Failed to register FileTransfer handler: %s",
352 g_error_free (error);
355 self->activated = TRUE;
358 self->window = empathy_roster_window_dup ();
360 gtk_application_add_window (GTK_APPLICATION (app),
361 GTK_WINDOW (self->window));
363 /* check if Shell is running */
364 dbus = tp_dbus_daemon_dup (&error);
365 g_assert_no_error (error);
367 tp_dbus_daemon_list_names (dbus, -1, list_names_cb,
368 self, NULL, G_OBJECT (self));
370 g_object_unref (dbus);
372 self->notifications_approver =
373 empathy_notifications_approver_dup_singleton ();
377 /* We're requested to show stuff again, disable the start hidden global in
378 * case the accounts wizard wants to pop up.
380 self->start_hidden = FALSE;
383 if (self->show_preferences)
384 empathy_roster_window_show_preferences (
385 EMPATHY_ROSTER_WINDOW (self->window), self->preferences_tab);
387 if (!self->start_hidden)
388 empathy_window_present (GTK_WINDOW (self->window));
390 /* Display the accounts dialog if needed */
391 tp_proxy_prepare_async (self->account_manager, NULL,
392 account_manager_ready_cb, self);
398 preferences_cb (const char *option_name,
403 EmpathyApp *self = data;
405 self->show_preferences = TRUE;
407 g_free (self->preferences_tab);
408 self->preferences_tab = g_strdup (value);
414 show_version_cb (const char *option_name,
420 empathy_app_local_command_line (GApplication *app,
424 EmpathyApp *self = (EmpathyApp *) app;
428 gboolean retval = FALSE;
429 GError *error = NULL;
430 gboolean no_connect = FALSE, start_hidden = FALSE;
432 GOptionContext *optcontext;
434 GOptionEntry options[] = {
436 0, G_OPTION_ARG_NONE, &no_connect,
437 N_("Don't connect on startup"),
439 { "start-hidden", 'h',
440 0, G_OPTION_ARG_NONE, &start_hidden,
441 N_("Don't display the contact list or any other dialogs on startup"),
443 { "show-preferences", 'p',
444 G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, &preferences_cb,
447 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
452 /* We create a group so that GOptionArgFuncs get the user data */
453 group = g_option_group_new ("empathy", NULL, NULL, app, NULL);
454 g_option_group_add_entries (group, options);
456 optcontext = g_option_context_new (N_("- Empathy IM Client"));
457 g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
458 g_option_context_set_main_group (optcontext, group);
459 g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
461 argc = g_strv_length (*arguments);
463 /* We dup the args because g_option_context_parse() sets things to NULL,
464 * but we want to parse all the command line to the primary instance
466 argv = g_new (gchar*, argc + 1);
467 for (i = 0; i <= argc; i++)
468 argv[i] = (*arguments)[i];
470 if (!g_option_context_parse (optcontext, &argc, &argv, &error))
472 g_print ("%s\nRun '%s --help' to see a full list of available command "
474 error->message, argv[0]);
475 g_warning ("Error in empathy init: %s", error->message);
477 *exit_status = EXIT_FAILURE;
483 g_option_context_free (optcontext);
485 self->no_connect = no_connect;
486 self->start_hidden = start_hidden;
491 static void empathy_app_constructed (GObject *object);
494 empathy_app_class_init (EmpathyAppClass *klass)
496 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
497 GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
500 gobject_class->set_property = empathy_app_set_property;
501 gobject_class->constructed = empathy_app_constructed;
502 gobject_class->dispose = empathy_app_dispose;
503 gobject_class->finalize = empathy_app_finalize;
505 g_app_class->command_line = empathy_app_command_line;
506 g_app_class->local_command_line = empathy_app_local_command_line;
508 spec = g_param_spec_boolean ("no-connect", "no connect",
509 "Don't connect on startup",
511 G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
512 g_object_class_install_property (gobject_class, PROP_NO_CONNECT, spec);
514 spec = g_param_spec_boolean ("start-hidden", "start hidden",
515 "Don't display the contact list or any other dialogs on startup",
517 G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
518 g_object_class_install_property (gobject_class, PROP_START_HIDDEN, spec);
522 empathy_app_init (EmpathyApp *self)
527 use_conn_notify_cb (GSettings *gsettings,
531 EmpathyConnectivity *connectivity = user_data;
533 empathy_connectivity_set_use_conn (connectivity,
534 g_settings_get_boolean (gsettings, key));
538 migrate_config_to_xdg_dir (void)
540 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
542 GFile *xdg_file, *old_file;
543 static const gchar* filenames[] = {
547 "contact-groups.xml",
548 "status-presets.xml",
553 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
554 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
556 /* xdg config dir already exists */
561 old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
563 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
565 /* old config dir didn't exist */
571 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
573 DEBUG ("Failed to create configuration directory; aborting migration");
579 for (i = 0; filenames[i]; i++)
581 old_filename = g_build_filename (old_dir, filenames[i], NULL);
582 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
584 g_free (old_filename);
587 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
588 old_file = g_file_new_for_path (old_filename);
589 xdg_file = g_file_new_for_path (xdg_filename);
591 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
592 NULL, NULL, NULL, NULL))
593 DEBUG ("Failed to migrate %s", filenames[i]);
595 g_free (old_filename);
596 g_free (xdg_filename);
597 g_object_unref (old_file);
598 g_object_unref (xdg_file);
606 show_accounts_ui (EmpathyApp *self,
610 empathy_accounts_dialog_show_application (screen,
611 NULL, if_needed, self->start_hidden);
615 show_version_cb (const char *option_name,
620 g_print ("%s\n", PACKAGE_STRING);
626 account_manager_ready_cb (GObject *source_object,
627 GAsyncResult *result,
630 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
631 EmpathyApp *self = user_data;
632 GError *error = NULL;
633 TpConnectionPresenceType presence;
635 if (!tp_proxy_prepare_finish (manager, result, &error))
639 DEBUG ("Failed to prepare account manager: %s", error->message);
641 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
642 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
643 _("Error contacting the Account Manager"));
644 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
645 _("There was an error while trying to connect to the Telepathy "
646 "Account Manager. The error was:\n\n%s"),
649 gtk_dialog_run (GTK_DIALOG (dialog));
650 gtk_widget_destroy (dialog);
652 g_error_free (error);
657 presence = tp_account_manager_get_most_available_presence (manager, NULL,
660 if (g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
662 tp_connection_presence_type_cmp_availability
663 (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
665 /* if current state is Offline, then put it online */
666 empathy_presence_manager_set_state (self->presence_mgr,
667 TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
669 /* Pop up the accounts dialog if we don't have any account */
670 if (!empathy_accounts_has_accounts (manager))
671 show_accounts_ui (self, gdk_screen_get_default (), TRUE);
675 account_join_chatrooms (TpAccount *account,
676 EmpathyChatroomManager *chatroom_manager)
679 GList *chatrooms, *p;
681 /* Wait if we are not connected or the TpConnection is not prepared yet */
682 conn = tp_account_get_connection (account);
686 chatrooms = empathy_chatroom_manager_get_chatrooms (
687 chatroom_manager, account);
689 for (p = chatrooms; p != NULL; p = p->next)
691 EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
693 if (!empathy_chatroom_get_auto_connect (room))
696 empathy_join_muc (account, empathy_chatroom_get_room (room),
697 TP_USER_ACTION_TIME_NOT_USER_ACTION);
699 g_list_free (chatrooms);
703 account_connection_changed_cb (TpAccount *account,
705 EmpathyChatroomManager *manager)
707 account_join_chatrooms (account, manager);
711 account_manager_chatroom_ready_cb (GObject *source_object,
712 GAsyncResult *result,
715 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
716 EmpathyChatroomManager *chatroom_manager = user_data;
718 GError *error = NULL;
720 if (!tp_proxy_prepare_finish (account_manager, result, &error))
722 DEBUG ("Failed to prepare account manager: %s", error->message);
723 g_error_free (error);
727 accounts = tp_account_manager_get_valid_accounts (account_manager);
729 for (l = accounts; l != NULL; l = g_list_next (l))
731 TpAccount *account = TP_ACCOUNT (l->data);
733 /* Try to join all rooms if we're connected */
734 account_join_chatrooms (account, chatroom_manager);
736 /* And/or join them on (re)connection */
737 tp_g_signal_connect_object (account, "notify::connection",
738 G_CALLBACK (account_connection_changed_cb), chatroom_manager, 0);
740 g_list_free (accounts);
744 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
748 TpAccountManager *account_manager = user_data;
750 tp_proxy_prepare_async (account_manager, NULL,
751 account_manager_chatroom_ready_cb, chatroom_manager);
755 empathy_app_constructed (GObject *object)
757 EmpathyApp *self = (EmpathyApp *) object;
758 gboolean chatroom_manager_ready;
760 g_set_application_name (_(PACKAGE_NAME));
762 gtk_window_set_default_icon_name ("empathy");
763 textdomain (GETTEXT_PACKAGE);
766 /* Set up debug sender */
767 self->debug_sender = tp_debug_sender_dup ();
768 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
771 notify_init (_(PACKAGE_NAME));
773 /* Setting up Idle */
774 self->presence_mgr = empathy_presence_manager_dup_singleton ();
776 self->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
778 /* Setting up Connectivity */
779 self->connectivity = empathy_connectivity_dup_singleton ();
780 use_conn_notify_cb (self->gsettings, EMPATHY_PREFS_USE_CONN,
782 g_signal_connect (self->gsettings,
783 "changed::" EMPATHY_PREFS_USE_CONN,
784 G_CALLBACK (use_conn_notify_cb), self->connectivity);
786 /* account management */
787 self->account_manager = tp_account_manager_dup ();
788 tp_proxy_prepare_async (self->account_manager, NULL,
789 account_manager_ready_cb, self);
791 migrate_config_to_xdg_dir ();
794 self->log_manager = tpl_log_manager_dup_singleton ();
796 self->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
798 g_object_get (self->chatroom_manager, "ready", &chatroom_manager_ready, NULL);
799 if (!chatroom_manager_ready)
801 g_signal_connect (G_OBJECT (self->chatroom_manager), "notify::ready",
802 G_CALLBACK (chatroom_manager_ready_cb), self->account_manager);
806 chatroom_manager_ready_cb (self->chatroom_manager, NULL,
807 self->account_manager);
810 /* Location mananger */
812 self->location_manager = empathy_location_manager_dup_singleton ();
815 self->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
817 self->activated = FALSE;
818 self->ft_factory = NULL;
823 add_empathy_features (void)
825 /* Add 'empathy' specific feature before doing any preparation */
826 EmpathyClientFactory *factory;
828 factory = empathy_client_factory_dup ();
830 tp_simple_client_factory_add_connection_features_varargs (
831 TP_SIMPLE_CLIENT_FACTORY (factory),
832 /* empathy_connection_aggregator_get_all_groups(), used by
833 * EmpathyGroupsWidget relies on it */
834 TP_CONNECTION_FEATURE_CONTACT_GROUPS,
835 /* empathy_connection_aggregator_dup_all_contacts(), used by
836 * EmpathyEventManager relies on it */
837 TP_CONNECTION_FEATURE_CONTACT_LIST,
840 g_object_unref (factory);
844 main (int argc, char *argv[])
849 g_thread_init (NULL);
852 #ifdef HAVE_LIBCHAMPLAIN
853 gtk_clutter_init (&argc, &argv);
859 gtk_init (&argc, &argv);
862 add_empathy_features ();
864 app = g_object_new (EMPATHY_TYPE_APP,
865 "application-id", EMPATHY_DBUS_NAME,
866 "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
869 retval = g_application_run (G_APPLICATION (app), argc, argv);
874 g_object_unref (app);