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/gi18n.h>
32 #include <unique/unique.h>
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>
48 #include <telepathy-logger/log-manager.h>
49 #include <telepathy-logger/log-store-empathy.h>
52 #include <libempathy/empathy-log-manager.h>
53 #endif /* ENABLE_TPL */
54 #include <libempathy/empathy-idle.h>
55 #include <libempathy/empathy-utils.h>
56 #include <libempathy/empathy-call-factory.h>
57 #include <libempathy/empathy-chatroom-manager.h>
58 #include <libempathy/empathy-account-settings.h>
59 #include <libempathy/empathy-connectivity.h>
60 #include <libempathy/empathy-connection-managers.h>
61 #include <libempathy/empathy-dispatcher.h>
62 #include <libempathy/empathy-dispatch-operation.h>
63 #include <libempathy/empathy-ft-factory.h>
64 #include <libempathy/empathy-tp-chat.h>
65 #include <libempathy/empathy-tp-call.h>
67 #include <libempathy-gtk/empathy-conf.h>
68 #include <libempathy-gtk/empathy-ui-utils.h>
69 #include <libempathy-gtk/empathy-location-manager.h>
71 #include "empathy-main-window.h"
72 #include "empathy-import-mc4-accounts.h"
73 #include "empathy-accounts-common.h"
74 #include "empathy-accounts-dialog.h"
75 #include "empathy-chat-manager.h"
76 #include "empathy-status-icon.h"
77 #include "empathy-call-window.h"
78 #include "empathy-chat-window.h"
79 #include "empathy-ft-manager.h"
81 #include "extensions/extensions.h"
83 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
84 #include <libempathy/empathy-debug.h>
88 static gboolean start_hidden = FALSE;
89 static gboolean no_connect = FALSE;
91 static void account_manager_ready_cb (GObject *source_object,
96 dispatch_cb (EmpathyDispatcher *dispatcher,
97 EmpathyDispatchOperation *operation,
102 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
104 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
106 EmpathyTpChat *tp_chat;
107 EmpathyChat *chat = NULL;
110 tp_chat = EMPATHY_TP_CHAT
111 (empathy_dispatch_operation_get_channel_wrapper (operation));
113 id = empathy_tp_chat_get_id (tp_chat);
114 if (!EMP_STR_EMPTY (id))
116 TpConnection *connection;
119 connection = empathy_tp_chat_get_connection (tp_chat);
120 account = empathy_get_account_for_connection (connection);
121 chat = empathy_chat_window_find_chat (account, id);
126 empathy_chat_set_tp_chat (chat, tp_chat);
130 chat = empathy_chat_new (tp_chat);
131 /* empathy_chat_new returns a floating reference as EmpathyChat is
132 * a GtkWidget. This reference will be taken by a container
133 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
136 empathy_chat_window_present_chat (chat,
137 empathy_dispatch_operation_get_user_action_time (operation));
139 if (empathy_tp_chat_is_invited (tp_chat))
141 /* We have been invited to the room. Add ourself as member as this
142 * channel has been approved. */
143 empathy_tp_chat_join (tp_chat);
146 empathy_dispatch_operation_claim (operation);
148 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
150 EmpathyCallFactory *factory;
152 factory = empathy_call_factory_get ();
153 empathy_call_factory_claim_channel (factory, operation);
155 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
157 EmpathyFTFactory *factory;
159 factory = empathy_ft_factory_dup_singleton ();
161 /* if the operation is not incoming, don't claim it,
162 * as it might have been triggered by another client, and
163 * we are observing it.
165 if (empathy_dispatch_operation_is_incoming (operation))
166 empathy_ft_factory_claim_channel (factory, operation);
171 use_conn_notify_cb (EmpathyConf *conf,
175 EmpathyConnectivity *connectivity = user_data;
178 if (empathy_conf_get_bool (conf, key, &use_conn))
180 empathy_connectivity_set_use_conn (connectivity, use_conn);
185 migrate_config_to_xdg_dir (void)
187 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
189 GFile *xdg_file, *old_file;
190 static const gchar* filenames[] = {
194 "contact-groups.xml",
195 "status-presets.xml",
200 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
201 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
203 /* xdg config dir already exists */
208 old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
210 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
212 /* old config dir didn't exist */
218 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
220 DEBUG ("Failed to create configuration directory; aborting migration");
226 for (i = 0; filenames[i]; i++)
228 old_filename = g_build_filename (old_dir, filenames[i], NULL);
229 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
231 g_free (old_filename);
234 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
235 old_file = g_file_new_for_path (old_filename);
236 xdg_file = g_file_new_for_path (xdg_filename);
238 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
239 NULL, NULL, NULL, NULL))
240 DEBUG ("Failed to migrate %s", filenames[i]);
242 g_free (old_filename);
243 g_free (xdg_filename);
244 g_object_unref (old_file);
245 g_object_unref (xdg_file);
253 show_accounts_ui (GdkScreen *screen,
256 empathy_accounts_dialog_show_application (screen,
257 NULL, if_needed, start_hidden);
260 static UniqueResponse
261 unique_app_message_cb (UniqueApp *unique_app,
263 UniqueMessageData *data,
267 GtkWindow *window = user_data;
268 TpAccountManager *account_manager;
270 DEBUG ("Other instance launched, presenting the main window. "
271 "Command=%d, timestamp %u", command, timestamp);
273 /* XXX: the standalone app somewhat breaks this case, since
274 * communicating it would be a pain */
276 /* We're requested to show stuff again, disable the start hidden global
277 * in case the accounts wizard wants to pop up.
279 start_hidden = FALSE;
281 gtk_window_set_screen (GTK_WINDOW (window),
282 unique_message_data_get_screen (data));
283 gtk_window_set_startup_id (GTK_WINDOW (window),
284 unique_message_data_get_startup_id (data));
285 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
286 gtk_window_set_skip_taskbar_hint (window, FALSE);
288 account_manager = tp_account_manager_dup ();
289 tp_account_manager_prepare_async (account_manager, NULL,
290 account_manager_ready_cb, NULL);
291 g_object_unref (account_manager);
293 return UNIQUE_RESPONSE_OK;
297 show_version_cb (const char *option_name,
302 g_print ("%s\n", PACKAGE_STRING);
310 new_incoming_transfer_cb (EmpathyFTFactory *factory,
311 EmpathyFTHandler *handler,
316 empathy_ft_manager_display_error (handler, error);
318 empathy_receive_file_with_file_chooser (handler);
322 new_ft_handler_cb (EmpathyFTFactory *factory,
323 EmpathyFTHandler *handler,
328 empathy_ft_manager_display_error (handler, error);
330 empathy_ft_manager_add_handler (handler);
332 g_object_unref (handler);
336 new_call_handler_cb (EmpathyCallFactory *factory,
337 EmpathyCallHandler *handler,
341 EmpathyCallWindow *window;
343 window = empathy_call_window_new (handler);
344 gtk_widget_show (GTK_WIDGET (window));
348 account_manager_ready_cb (GObject *source_object,
349 GAsyncResult *result,
352 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
353 GError *error = NULL;
355 EmpathyConnectivity *connectivity;
356 gboolean autoconnect = TRUE;
357 TpConnectionPresenceType presence;
359 if (!tp_account_manager_prepare_finish (manager, result, &error))
361 DEBUG ("Failed to prepare account manager: %s", error->message);
362 g_error_free (error);
367 idle = empathy_idle_dup_singleton ();
368 connectivity = empathy_connectivity_dup_singleton ();
370 presence = tp_account_manager_get_most_available_presence (manager, NULL,
373 empathy_conf_get_bool (empathy_conf_get (),
374 EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
375 if (autoconnect && !no_connect &&
376 tp_connection_presence_type_cmp_availability
377 (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
379 /* if current state is Offline, then put it online */
380 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
382 /* Pop up the accounts dialog if it's needed (either when we don't have any
383 * accounts yet or when we haven't imported mc4 accounts yet */
384 if (!empathy_accounts_has_accounts (manager)
385 || !empathy_import_mc4_has_imported ())
386 show_accounts_ui (gdk_screen_get_default (), TRUE);
388 g_object_unref (idle);
389 g_object_unref (connectivity);
392 static EmpathyDispatcher *
393 setup_dispatcher (void)
395 EmpathyDispatcher *d;
398 const gchar *channeltype;
399 TpHandleType handletype;
401 /* Text channels with handle types none, contact and room */
402 { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE },
403 { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT },
404 { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM },
405 /* file transfer to contacts */
406 { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT },
407 /* stream media to contacts */
408 { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT },
410 { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
412 gchar *capabilities[] = {
413 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
414 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
419 /* Setup the basic Client.Handler that matches our client filter */
420 filters = g_ptr_array_new ();
422 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
423 TP_IFACE_CHANNEL_TYPE_TEXT,
424 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
425 TP_HANDLE_TYPE_CONTACT,
427 g_ptr_array_add (filters, asv);
429 d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
431 g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
432 g_ptr_array_free (filters, TRUE);
434 /* Setup the an extended Client.Handler that matches everything we can do */
435 filters = g_ptr_array_new ();
436 for (i = 0 ; i < G_N_ELEMENTS (types); i++)
439 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
440 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
443 g_ptr_array_add (filters, asv);
447 TP_IFACE_CHANNEL ".ChannelType",
448 G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
449 TP_IFACE_CHANNEL ".TargetHandleType",
450 G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
451 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
452 G_TYPE_BOOLEAN, TRUE,
454 g_ptr_array_add (filters, asv);
457 TP_IFACE_CHANNEL ".ChannelType",
458 G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
459 TP_IFACE_CHANNEL ".TargetHandleType",
460 G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
461 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
462 G_TYPE_BOOLEAN, TRUE,
464 g_ptr_array_add (filters, asv);
467 empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
468 filters, capabilities);
470 g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
471 g_ptr_array_free (filters, TRUE);
477 account_status_changed_cb (TpAccount *account,
481 gchar *dbus_error_name,
483 EmpathyChatroom *room)
487 conn = tp_account_get_connection (account);
491 empathy_dispatcher_join_muc (conn,
492 empathy_chatroom_get_room (room), EMPATHY_DISPATCHER_NON_USER_ACTION,
497 account_manager_chatroom_ready_cb (GObject *source_object,
498 GAsyncResult *result,
501 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
502 EmpathyChatroomManager *chatroom_manager = user_data;
504 GError *error = NULL;
506 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
508 DEBUG ("Failed to prepare account manager: %s", error->message);
509 g_error_free (error);
513 accounts = tp_account_manager_get_valid_accounts (account_manager);
515 for (l = accounts; l != NULL; l = g_list_next (l))
517 TpAccount *account = TP_ACCOUNT (l->data);
519 GList *chatrooms, *p;
521 conn = tp_account_get_connection (account);
523 chatrooms = empathy_chatroom_manager_get_chatrooms (
524 chatroom_manager, account);
526 for (p = chatrooms; p != NULL; p = p->next)
528 EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
530 if (!empathy_chatroom_get_auto_connect (room))
535 g_signal_connect (G_OBJECT (account), "status-changed",
536 G_CALLBACK (account_status_changed_cb), room);
540 empathy_dispatcher_join_muc (conn,
541 empathy_chatroom_get_room (room),
542 EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
546 g_list_free (chatrooms);
549 g_list_free (accounts);
553 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
557 TpAccountManager *account_manager = user_data;
559 tp_account_manager_prepare_async (account_manager, NULL,
560 account_manager_chatroom_ready_cb, chatroom_manager);
564 empathy_idle_set_auto_away_cb (EmpathyConf *conf,
569 EmpathyIdle *idle = user_data;
571 empathy_conf_get_bool (conf, key, &autoaway);
572 empathy_idle_set_auto_away (idle, autoaway);
576 main (int argc, char *argv[])
579 EmpathyLocationManager *location_manager = NULL;
581 EmpathyStatusIcon *icon;
582 EmpathyDispatcher *dispatcher;
583 TpAccountManager *account_manager;
585 TplLogManager *log_manager;
587 EmpathyLogManager *log_manager;
588 #endif /* ENABLE_TPL */
589 EmpathyChatroomManager *chatroom_manager;
590 EmpathyCallFactory *call_factory;
591 EmpathyFTFactory *ft_factory;
594 EmpathyConnectivity *connectivity;
595 EmpathyChatManager *chat_manager;
596 GError *error = NULL;
597 UniqueApp *unique_app;
598 gboolean chatroom_manager_ready;
599 gboolean autoaway = TRUE;
601 TpDebugSender *debug_sender;
604 GOptionContext *optcontext;
605 GOptionEntry options[] = {
607 0, G_OPTION_ARG_NONE, &no_connect,
608 N_("Don't connect on startup"),
610 { "start-hidden", 'h',
611 0, G_OPTION_ARG_NONE, &start_hidden,
612 N_("Don't display the contact list or any other dialogs on startup"),
615 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
621 g_thread_init (NULL);
624 optcontext = g_option_context_new (N_("- Empathy IM Client"));
625 g_option_context_add_group (optcontext, gst_init_get_option_group ());
626 g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
627 #if HAVE_LIBCHAMPLAIN
628 g_option_context_add_group (optcontext, clutter_get_option_group ());
630 g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
632 if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
633 g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
634 error->message, argv[0]);
635 g_warning ("Error in empathy init: %s", error->message);
639 g_option_context_free (optcontext);
642 g_set_application_name (_(PACKAGE_NAME));
643 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
645 gtk_window_set_default_icon_name ("empathy");
646 textdomain (GETTEXT_PACKAGE);
649 /* Set up debug sender */
650 debug_sender = tp_debug_sender_dup ();
651 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
654 unique_app = unique_app_new ("org.gnome."PACKAGE_NAME, NULL);
656 if (unique_app_is_running (unique_app))
658 unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
660 g_object_unref (unique_app);
664 notify_init (_(PACKAGE_NAME));
666 /* Setting up Idle */
667 idle = empathy_idle_dup_singleton ();
669 empathy_conf_get_bool (empathy_conf_get (),
670 EMPATHY_PREFS_AUTOAWAY, &autoaway);
672 empathy_conf_notify_add (empathy_conf_get (),
673 EMPATHY_PREFS_AUTOAWAY,
674 empathy_idle_set_auto_away_cb,
677 empathy_idle_set_auto_away (idle, autoaway);
679 /* Setting up Connectivity */
680 connectivity = empathy_connectivity_dup_singleton ();
681 use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
683 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
684 use_conn_notify_cb, connectivity);
686 /* account management */
687 account_manager = tp_account_manager_dup ();
688 tp_account_manager_prepare_async (account_manager, NULL,
689 account_manager_ready_cb, NULL);
691 /* Handle channels */
692 dispatcher = setup_dispatcher ();
693 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
695 migrate_config_to_xdg_dir ();
698 window = empathy_main_window_show ();
699 icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
702 chat_manager = empathy_chat_manager_dup_singleton ();
704 g_signal_connect (unique_app, "message-received",
705 G_CALLBACK (unique_app_message_cb), window);
709 log_manager = tpl_log_manager_dup_singleton ();
711 log_manager = empathy_log_manager_dup_singleton ();
712 empathy_log_manager_observe (log_manager, dispatcher);
715 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
716 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
718 g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
719 if (!chatroom_manager_ready)
721 g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
722 G_CALLBACK (chatroom_manager_ready_cb), account_manager);
726 chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
729 /* Create the call factory */
730 call_factory = empathy_call_factory_initialise ();
731 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
732 G_CALLBACK (new_call_handler_cb), NULL);
733 /* Create the FT factory */
734 ft_factory = empathy_ft_factory_dup_singleton ();
735 g_signal_connect (ft_factory, "new-ft-handler",
736 G_CALLBACK (new_ft_handler_cb), NULL);
737 g_signal_connect (ft_factory, "new-incoming-transfer",
738 G_CALLBACK (new_incoming_transfer_cb), NULL);
740 /* Location mananger */
742 location_manager = empathy_location_manager_dup_singleton ();
747 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
750 g_object_unref (debug_sender);
753 g_object_unref (chat_manager);
754 g_object_unref (idle);
755 g_object_unref (connectivity);
756 g_object_unref (icon);
757 g_object_unref (account_manager);
758 g_object_unref (log_manager);
759 g_object_unref (dispatcher);
760 g_object_unref (chatroom_manager);
762 g_object_unref (location_manager);
764 g_object_unref (ft_factory);
765 g_object_unref (unique_app);