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>
33 #include <unique/unique.h>
35 #ifdef HAVE_LIBCHAMPLAIN
36 #include <clutter-gtk/clutter-gtk.h>
39 #include <libnotify/notify.h>
41 #include <telepathy-glib/account-manager.h>
42 #include <telepathy-glib/dbus.h>
43 #include <telepathy-glib/debug-sender.h>
44 #include <telepathy-glib/util.h>
45 #include <telepathy-glib/connection-manager.h>
46 #include <telepathy-glib/interfaces.h>
48 #include <telepathy-logger/log-manager.h>
50 #include <libempathy/empathy-idle.h>
51 #include <libempathy/empathy-utils.h>
52 #include <libempathy/empathy-chatroom-manager.h>
53 #include <libempathy/empathy-account-settings.h>
54 #include <libempathy/empathy-connectivity.h>
55 #include <libempathy/empathy-connection-managers.h>
56 #include <libempathy/empathy-dispatcher.h>
57 #include <libempathy/empathy-dispatch-operation.h>
58 #include <libempathy/empathy-ft-factory.h>
59 #include <libempathy/empathy-gsettings.h>
60 #include <libempathy/empathy-tp-chat.h>
62 #include <libempathy-gtk/empathy-ui-utils.h>
63 #include <libempathy-gtk/empathy-location-manager.h>
65 #include "empathy-main-window.h"
66 #include "empathy-accounts-common.h"
67 #include "empathy-accounts-dialog.h"
68 #include "empathy-chat-manager.h"
69 #include "empathy-status-icon.h"
70 #include "empathy-ft-manager.h"
72 #include "extensions/extensions.h"
74 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
75 #include <libempathy/empathy-debug.h>
77 static gboolean start_hidden = FALSE;
78 static gboolean no_connect = FALSE;
80 static void account_manager_ready_cb (GObject *source_object,
85 dispatch_cb (EmpathyDispatcher *dispatcher,
86 EmpathyDispatchOperation *operation,
91 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
93 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
95 EmpathyFTFactory *factory;
97 factory = empathy_ft_factory_dup_singleton ();
99 /* if the operation is not incoming, don't claim it,
100 * as it might have been triggered by another client, and
101 * we are observing it.
103 if (empathy_dispatch_operation_is_incoming (operation))
104 empathy_ft_factory_claim_channel (factory, operation);
109 use_conn_notify_cb (GSettings *gsettings,
113 EmpathyConnectivity *connectivity = user_data;
115 empathy_connectivity_set_use_conn (connectivity,
116 g_settings_get_boolean (gsettings, key));
120 migrate_config_to_xdg_dir (void)
122 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
124 GFile *xdg_file, *old_file;
125 static const gchar* filenames[] = {
129 "contact-groups.xml",
130 "status-presets.xml",
135 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
136 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
138 /* xdg config dir already exists */
143 old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
145 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
147 /* old config dir didn't exist */
153 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
155 DEBUG ("Failed to create configuration directory; aborting migration");
161 for (i = 0; filenames[i]; i++)
163 old_filename = g_build_filename (old_dir, filenames[i], NULL);
164 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
166 g_free (old_filename);
169 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
170 old_file = g_file_new_for_path (old_filename);
171 xdg_file = g_file_new_for_path (xdg_filename);
173 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
174 NULL, NULL, NULL, NULL))
175 DEBUG ("Failed to migrate %s", filenames[i]);
177 g_free (old_filename);
178 g_free (xdg_filename);
179 g_object_unref (old_file);
180 g_object_unref (xdg_file);
188 show_accounts_ui (GdkScreen *screen,
191 empathy_accounts_dialog_show_application (screen,
192 NULL, if_needed, start_hidden);
195 static UniqueResponse
196 unique_app_message_cb (UniqueApp *unique_app,
198 UniqueMessageData *data,
202 GtkWindow *window = user_data;
203 TpAccountManager *account_manager;
205 DEBUG ("Other instance launched, presenting the main window. "
206 "Command=%d, timestamp %u", command, timestamp);
208 /* XXX: the standalone app somewhat breaks this case, since
209 * communicating it would be a pain */
211 /* We're requested to show stuff again, disable the start hidden global
212 * in case the accounts wizard wants to pop up.
214 start_hidden = FALSE;
216 gtk_window_set_screen (GTK_WINDOW (window),
217 unique_message_data_get_screen (data));
218 gtk_window_set_startup_id (GTK_WINDOW (window),
219 unique_message_data_get_startup_id (data));
220 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
221 gtk_window_set_skip_taskbar_hint (window, FALSE);
223 account_manager = tp_account_manager_dup ();
224 tp_account_manager_prepare_async (account_manager, NULL,
225 account_manager_ready_cb, NULL);
226 g_object_unref (account_manager);
228 return UNIQUE_RESPONSE_OK;
231 static gboolean show_version_cb (const char *option_name,
234 GError **error) G_GNUC_NORETURN;
237 show_version_cb (const char *option_name,
242 g_print ("%s\n", PACKAGE_STRING);
248 new_incoming_transfer_cb (EmpathyFTFactory *factory,
249 EmpathyFTHandler *handler,
254 empathy_ft_manager_display_error (handler, error);
256 empathy_receive_file_with_file_chooser (handler);
260 new_ft_handler_cb (EmpathyFTFactory *factory,
261 EmpathyFTHandler *handler,
266 empathy_ft_manager_display_error (handler, error);
268 empathy_ft_manager_add_handler (handler);
270 g_object_unref (handler);
274 account_manager_ready_cb (GObject *source_object,
275 GAsyncResult *result,
278 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
279 GError *error = NULL;
281 EmpathyConnectivity *connectivity;
282 TpConnectionPresenceType presence;
283 GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
285 if (!tp_account_manager_prepare_finish (manager, result, &error))
287 DEBUG ("Failed to prepare account manager: %s", error->message);
288 g_error_free (error);
293 idle = empathy_idle_dup_singleton ();
294 connectivity = empathy_connectivity_dup_singleton ();
296 presence = tp_account_manager_get_most_available_presence (manager, NULL,
299 if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
301 tp_connection_presence_type_cmp_availability
302 (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
304 /* if current state is Offline, then put it online */
305 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
307 /* Pop up the accounts dialog if we don't have any account */
308 if (!empathy_accounts_has_accounts (manager))
309 show_accounts_ui (gdk_screen_get_default (), TRUE);
311 g_object_unref (idle);
312 g_object_unref (connectivity);
313 g_object_unref (gsettings);
316 static EmpathyDispatcher *
317 setup_dispatcher (void)
319 EmpathyDispatcher *d;
322 const gchar *channeltype;
323 TpHandleType handletype;
325 /* file transfer to contacts */
326 { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT },
331 /* Setup the an extended Client.Handler that matches everything we can do */
332 filters = g_ptr_array_new ();
333 for (i = 0 ; i < G_N_ELEMENTS (types); i++)
336 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
337 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
340 g_ptr_array_add (filters, asv);
343 d = empathy_dispatcher_new (PACKAGE_NAME"MoreThanMeetsTheEye", filters, NULL);
345 g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
346 g_ptr_array_free (filters, TRUE);
352 account_status_changed_cb (TpAccount *account,
356 gchar *dbus_error_name,
358 EmpathyChatroom *room)
362 conn = tp_account_get_connection (account);
366 empathy_dispatcher_join_muc (conn,
367 empathy_chatroom_get_room (room), EMPATHY_DISPATCHER_NON_USER_ACTION);
371 account_manager_chatroom_ready_cb (GObject *source_object,
372 GAsyncResult *result,
375 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
376 EmpathyChatroomManager *chatroom_manager = user_data;
378 GError *error = NULL;
380 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
382 DEBUG ("Failed to prepare account manager: %s", error->message);
383 g_error_free (error);
387 accounts = tp_account_manager_get_valid_accounts (account_manager);
389 for (l = accounts; l != NULL; l = g_list_next (l))
391 TpAccount *account = TP_ACCOUNT (l->data);
393 GList *chatrooms, *p;
395 conn = tp_account_get_connection (account);
397 chatrooms = empathy_chatroom_manager_get_chatrooms (
398 chatroom_manager, account);
400 for (p = chatrooms; p != NULL; p = p->next)
402 EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
404 if (!empathy_chatroom_get_auto_connect (room))
409 g_signal_connect (G_OBJECT (account), "status-changed",
410 G_CALLBACK (account_status_changed_cb), room);
414 empathy_dispatcher_join_muc (conn,
415 empathy_chatroom_get_room (room),
416 EMPATHY_DISPATCHER_NON_USER_ACTION);
420 g_list_free (chatrooms);
423 g_list_free (accounts);
427 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
431 TpAccountManager *account_manager = user_data;
433 tp_account_manager_prepare_async (account_manager, NULL,
434 account_manager_chatroom_ready_cb, chatroom_manager);
438 empathy_idle_set_auto_away_cb (GSettings *gsettings,
442 EmpathyIdle *idle = user_data;
444 empathy_idle_set_auto_away (idle,
445 g_settings_get_boolean (gsettings, key));
449 main (int argc, char *argv[])
452 EmpathyLocationManager *location_manager = NULL;
454 EmpathyStatusIcon *icon;
455 EmpathyDispatcher *dispatcher;
456 TpAccountManager *account_manager;
457 TplLogManager *log_manager;
458 EmpathyChatroomManager *chatroom_manager;
459 EmpathyFTFactory *ft_factory;
462 EmpathyConnectivity *connectivity;
463 EmpathyChatManager *chat_manager;
464 GError *error = NULL;
465 UniqueApp *unique_app;
466 gboolean chatroom_manager_ready;
467 gboolean autoaway = TRUE;
469 TpDebugSender *debug_sender;
471 GSettings *gsettings;
473 GOptionContext *optcontext;
474 GOptionEntry options[] = {
476 0, G_OPTION_ARG_NONE, &no_connect,
477 N_("Don't connect on startup"),
479 { "start-hidden", 'h',
480 0, G_OPTION_ARG_NONE, &start_hidden,
481 N_("Don't display the contact list or any other dialogs on startup"),
484 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
490 g_thread_init (NULL);
493 optcontext = g_option_context_new (N_("- Empathy IM Client"));
494 g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
495 #ifdef HAVE_LIBCHAMPLAIN
496 g_option_context_add_group (optcontext, clutter_get_option_group ());
498 g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
500 if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
501 g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
502 error->message, argv[0]);
503 g_warning ("Error in empathy init: %s", error->message);
507 g_option_context_free (optcontext);
510 g_set_application_name (_(PACKAGE_NAME));
512 gtk_window_set_default_icon_name ("empathy");
513 textdomain (GETTEXT_PACKAGE);
516 /* Set up debug sender */
517 debug_sender = tp_debug_sender_dup ();
518 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
521 unique_app = unique_app_new ("org.gnome."PACKAGE_NAME, NULL);
523 if (unique_app_is_running (unique_app))
525 unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
527 g_object_unref (unique_app);
531 notify_init (_(PACKAGE_NAME));
533 /* Setting up Idle */
534 idle = empathy_idle_dup_singleton ();
536 gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
537 autoaway = g_settings_get_boolean (gsettings, EMPATHY_PREFS_AUTOAWAY);
539 g_signal_connect (gsettings,
540 "changed::" EMPATHY_PREFS_AUTOAWAY,
541 G_CALLBACK (empathy_idle_set_auto_away_cb), idle);
543 empathy_idle_set_auto_away (idle, autoaway);
545 /* Setting up Connectivity */
546 connectivity = empathy_connectivity_dup_singleton ();
547 use_conn_notify_cb (gsettings, EMPATHY_PREFS_USE_CONN,
549 g_signal_connect (gsettings,
550 "changed::" EMPATHY_PREFS_USE_CONN,
551 G_CALLBACK (use_conn_notify_cb), connectivity);
553 /* account management */
554 account_manager = tp_account_manager_dup ();
555 tp_account_manager_prepare_async (account_manager, NULL,
556 account_manager_ready_cb, NULL);
558 /* Handle channels */
559 dispatcher = setup_dispatcher ();
560 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
562 migrate_config_to_xdg_dir ();
565 window = empathy_main_window_dup ();
566 gtk_widget_show (window);
567 icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
570 chat_manager = empathy_chat_manager_dup_singleton ();
572 g_signal_connect (unique_app, "message-received",
573 G_CALLBACK (unique_app_message_cb), window);
576 log_manager = tpl_log_manager_dup_singleton ();
578 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
580 g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
581 if (!chatroom_manager_ready)
583 g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
584 G_CALLBACK (chatroom_manager_ready_cb), account_manager);
588 chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
591 /* Create the FT factory */
592 ft_factory = empathy_ft_factory_dup_singleton ();
593 g_signal_connect (ft_factory, "new-ft-handler",
594 G_CALLBACK (new_ft_handler_cb), NULL);
595 g_signal_connect (ft_factory, "new-incoming-transfer",
596 G_CALLBACK (new_incoming_transfer_cb), NULL);
598 /* Location mananger */
600 location_manager = empathy_location_manager_dup_singleton ();
605 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
608 g_object_unref (debug_sender);
611 g_object_unref (chat_manager);
612 g_object_unref (idle);
613 g_object_unref (connectivity);
614 g_object_unref (icon);
615 g_object_unref (account_manager);
616 g_object_unref (log_manager);
617 g_object_unref (dispatcher);
618 g_object_unref (chatroom_manager);
620 g_object_unref (location_manager);
622 g_object_unref (ft_factory);
623 g_object_unref (unique_app);
624 g_object_unref (gsettings);
625 gtk_widget_destroy (window);