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-status-icon.h"
76 #include "empathy-call-window.h"
77 #include "empathy-chat-window.h"
78 #include "empathy-ft-manager.h"
80 #include "extensions/extensions.h"
82 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
83 #include <libempathy/empathy-debug.h>
87 static gboolean start_hidden = FALSE;
88 static gboolean no_connect = FALSE;
90 static void account_manager_ready_cb (GObject *source_object,
95 dispatch_cb (EmpathyDispatcher *dispatcher,
96 EmpathyDispatchOperation *operation,
101 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
103 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
105 EmpathyTpChat *tp_chat;
106 EmpathyChat *chat = NULL;
109 tp_chat = EMPATHY_TP_CHAT
110 (empathy_dispatch_operation_get_channel_wrapper (operation));
112 id = empathy_tp_chat_get_id (tp_chat);
113 if (!EMP_STR_EMPTY (id))
115 TpConnection *connection;
118 connection = empathy_tp_chat_get_connection (tp_chat);
119 account = empathy_get_account_for_connection (connection);
120 chat = empathy_chat_window_find_chat (account, id);
125 empathy_chat_set_tp_chat (chat, tp_chat);
129 chat = empathy_chat_new (tp_chat);
130 /* empathy_chat_new returns a floating reference as EmpathyChat is
131 * a GtkWidget. This reference will be taken by a container
132 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
135 empathy_chat_window_present_chat (chat);
137 empathy_dispatch_operation_claim (operation);
139 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
141 EmpathyCallFactory *factory;
143 factory = empathy_call_factory_get ();
144 empathy_call_factory_claim_channel (factory, operation);
146 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
148 EmpathyFTFactory *factory;
150 factory = empathy_ft_factory_dup_singleton ();
152 /* if the operation is not incoming, don't claim it,
153 * as it might have been triggered by another client, and
154 * we are observing it.
156 if (empathy_dispatch_operation_is_incoming (operation))
157 empathy_ft_factory_claim_channel (factory, operation);
162 use_conn_notify_cb (EmpathyConf *conf,
166 EmpathyConnectivity *connectivity = user_data;
169 if (empathy_conf_get_bool (conf, key, &use_conn))
171 empathy_connectivity_set_use_conn (connectivity, use_conn);
176 migrate_config_to_xdg_dir (void)
178 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
180 GFile *xdg_file, *old_file;
181 static const gchar* filenames[] = {
185 "contact-groups.xml",
186 "status-presets.xml",
191 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
192 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
194 /* xdg config dir already exists */
199 old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
201 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
203 /* old config dir didn't exist */
209 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
211 DEBUG ("Failed to create configuration directory; aborting migration");
217 for (i = 0; filenames[i]; i++)
219 old_filename = g_build_filename (old_dir, filenames[i], NULL);
220 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
222 g_free (old_filename);
225 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
226 old_file = g_file_new_for_path (old_filename);
227 xdg_file = g_file_new_for_path (xdg_filename);
229 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
230 NULL, NULL, NULL, NULL))
231 DEBUG ("Failed to migrate %s", filenames[i]);
233 g_free (old_filename);
234 g_free (xdg_filename);
235 g_object_unref (old_file);
236 g_object_unref (xdg_file);
244 accounts_application_exited_cb (GPid pid,
250 g_warning ("accounts application exited with status %d: '%s'",
251 status, g_strerror (status));
256 show_accounts_ui (GdkScreen *screen,
259 g_debug ("launching accounts UI");
260 empathy_accounts_dialog_show_application (screen,
261 accounts_application_exited_cb, NULL, NULL, if_needed, start_hidden);
264 static UniqueResponse
265 unique_app_message_cb (UniqueApp *unique_app,
267 UniqueMessageData *data,
271 GtkWindow *window = user_data;
272 TpAccountManager *account_manager;
274 DEBUG ("Other instance launched, presenting the main window. "
275 "Command=%d, timestamp %u", command, timestamp);
277 /* XXX: the standalone app somewhat breaks this case, since
278 * communicating it would be a pain */
280 /* We're requested to show stuff again, disable the start hidden global
281 * in case the accounts wizard wants to pop up.
283 start_hidden = FALSE;
285 gtk_window_set_screen (GTK_WINDOW (window),
286 unique_message_data_get_screen (data));
287 gtk_window_set_startup_id (GTK_WINDOW (window),
288 unique_message_data_get_startup_id (data));
289 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
290 gtk_window_set_skip_taskbar_hint (window, FALSE);
292 account_manager = tp_account_manager_dup ();
293 tp_account_manager_prepare_async (account_manager, NULL,
294 account_manager_ready_cb, NULL);
295 g_object_unref (account_manager);
297 return UNIQUE_RESPONSE_OK;
301 show_version_cb (const char *option_name,
306 g_print ("%s\n", PACKAGE_STRING);
314 new_incoming_transfer_cb (EmpathyFTFactory *factory,
315 EmpathyFTHandler *handler,
320 empathy_ft_manager_display_error (handler, error);
322 empathy_receive_file_with_file_chooser (handler);
326 new_ft_handler_cb (EmpathyFTFactory *factory,
327 EmpathyFTHandler *handler,
332 empathy_ft_manager_display_error (handler, error);
334 empathy_ft_manager_add_handler (handler);
336 g_object_unref (handler);
340 new_call_handler_cb (EmpathyCallFactory *factory,
341 EmpathyCallHandler *handler,
345 EmpathyCallWindow *window;
347 window = empathy_call_window_new (handler);
348 gtk_widget_show (GTK_WIDGET (window));
352 account_manager_ready_cb (GObject *source_object,
353 GAsyncResult *result,
356 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
357 GError *error = NULL;
359 EmpathyConnectivity *connectivity;
360 gboolean autoconnect = TRUE;
361 TpConnectionPresenceType presence;
363 if (!tp_account_manager_prepare_finish (manager, result, &error))
365 DEBUG ("Failed to prepare account manager: %s", error->message);
366 g_error_free (error);
371 idle = empathy_idle_dup_singleton ();
372 connectivity = empathy_connectivity_dup_singleton ();
374 presence = tp_account_manager_get_most_available_presence (manager, NULL,
377 empathy_conf_get_bool (empathy_conf_get (),
378 EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
379 if (autoconnect && !no_connect &&
380 tp_connection_presence_type_cmp_availability
381 (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
383 /* if current state is Offline, then put it online */
384 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
386 /* Pop up the accounts dialog if it's needed (either when we don't have any
387 * non-salut accounts yet or when we haven't imported mc4 accounts yet */
388 if (!empathy_accounts_has_non_salut_accounts (manager)
389 || !empathy_import_mc4_has_imported ())
390 show_accounts_ui (gdk_screen_get_default (), TRUE);
392 g_object_unref (idle);
393 g_object_unref (connectivity);
396 static EmpathyDispatcher *
397 setup_dispatcher (void)
399 EmpathyDispatcher *d;
402 const gchar *channeltype;
403 TpHandleType handletype;
405 /* Text channels with handle types none, contact and room */
406 { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE },
407 { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT },
408 { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM },
409 /* file transfer to contacts */
410 { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT },
411 /* stream media to contacts */
412 { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT },
414 { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
416 gchar *capabilities[] = {
417 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
418 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
423 /* Setup the basic Client.Handler that matches our client filter */
424 filters = g_ptr_array_new ();
426 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
427 TP_IFACE_CHANNEL_TYPE_TEXT,
428 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
429 TP_HANDLE_TYPE_CONTACT,
431 g_ptr_array_add (filters, asv);
433 d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
435 g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
436 g_ptr_array_free (filters, TRUE);
438 /* Setup the an extended Client.Handler that matches everything we can do */
439 filters = g_ptr_array_new ();
440 for (i = 0 ; i < G_N_ELEMENTS (types); i++)
443 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
444 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
447 g_ptr_array_add (filters, asv);
451 TP_IFACE_CHANNEL ".ChannelType",
452 G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
453 TP_IFACE_CHANNEL ".TargetHandleType",
454 G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
455 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
456 G_TYPE_BOOLEAN, TRUE,
458 g_ptr_array_add (filters, asv);
461 TP_IFACE_CHANNEL ".ChannelType",
462 G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
463 TP_IFACE_CHANNEL ".TargetHandleType",
464 G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
465 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
466 G_TYPE_BOOLEAN, TRUE,
468 g_ptr_array_add (filters, asv);
471 empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
472 filters, capabilities);
474 g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
475 g_ptr_array_free (filters, TRUE);
481 account_status_changed_cb (TpAccount *account,
485 gchar *dbus_error_name,
487 EmpathyChatroom *room)
491 conn = tp_account_get_connection (account);
495 empathy_dispatcher_join_muc (conn,
496 empathy_chatroom_get_room (room), NULL, NULL);
500 account_manager_chatroom_ready_cb (GObject *source_object,
501 GAsyncResult *result,
504 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
505 EmpathyChatroomManager *chatroom_manager = user_data;
507 GError *error = NULL;
509 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
511 DEBUG ("Failed to prepare account manager: %s", error->message);
512 g_error_free (error);
516 accounts = tp_account_manager_get_valid_accounts (account_manager);
518 for (l = accounts; l != NULL; l = g_list_next (l))
520 TpAccount *account = TP_ACCOUNT (l->data);
522 GList *chatrooms, *p;
524 conn = tp_account_get_connection (account);
526 chatrooms = empathy_chatroom_manager_get_chatrooms (
527 chatroom_manager, account);
529 for (p = chatrooms; p != NULL; p = p->next)
531 EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
533 if (!empathy_chatroom_get_auto_connect (room))
538 g_signal_connect (G_OBJECT (account), "status-changed",
539 G_CALLBACK (account_status_changed_cb), room);
543 empathy_dispatcher_join_muc (conn,
544 empathy_chatroom_get_room (room), NULL, NULL);
548 g_list_free (chatrooms);
551 g_list_free (accounts);
555 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
559 TpAccountManager *account_manager = user_data;
561 tp_account_manager_prepare_async (account_manager, NULL,
562 account_manager_chatroom_ready_cb, chatroom_manager);
566 main (int argc, char *argv[])
569 EmpathyLocationManager *location_manager = NULL;
571 EmpathyStatusIcon *icon;
572 EmpathyDispatcher *dispatcher;
573 TpAccountManager *account_manager;
575 TplLogManager *log_manager;
577 EmpathyLogManager *log_manager;
578 #endif /* ENABLE_TPL */
579 EmpathyChatroomManager *chatroom_manager;
580 EmpathyCallFactory *call_factory;
581 EmpathyFTFactory *ft_factory;
584 EmpathyConnectivity *connectivity;
585 GError *error = NULL;
586 UniqueApp *unique_app;
587 gboolean chatroom_manager_ready;
590 TpDebugSender *debug_sender;
591 #endif /* ENABLE_TPL */
593 GOptionContext *optcontext;
594 GOptionEntry options[] = {
596 0, G_OPTION_ARG_NONE, &no_connect,
597 N_("Don't connect on startup"),
599 { "start-hidden", 'h',
600 0, G_OPTION_ARG_NONE, &start_hidden,
601 N_("Don't display the contact list or any other dialogs on startup"),
604 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
610 g_thread_init (NULL);
613 optcontext = g_option_context_new (N_("- Empathy IM Client"));
614 g_option_context_add_group (optcontext, gst_init_get_option_group ());
615 g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
616 #if HAVE_LIBCHAMPLAIN
617 g_option_context_add_group (optcontext, clutter_get_option_group ());
619 g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
621 if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
622 g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
623 error->message, argv[0]);
624 g_warning ("Error in empathy init: %s", error->message);
628 g_option_context_free (optcontext);
631 g_set_application_name (_(PACKAGE_NAME));
632 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
634 gtk_window_set_default_icon_name ("empathy");
635 textdomain (GETTEXT_PACKAGE);
638 /* Set up debug sender */
639 debug_sender = tp_debug_sender_dup ();
640 g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
643 unique_app = unique_app_new ("org.gnome."PACKAGE_NAME, NULL);
645 if (unique_app_is_running (unique_app))
647 unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
649 g_object_unref (unique_app);
653 notify_init (_(PACKAGE_NAME));
655 /* Setting up Idle */
656 idle = empathy_idle_dup_singleton ();
657 empathy_idle_set_auto_away (idle, TRUE);
659 /* Setting up Connectivity */
660 connectivity = empathy_connectivity_dup_singleton ();
661 use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
663 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
664 use_conn_notify_cb, connectivity);
666 /* account management */
667 account_manager = tp_account_manager_dup ();
668 tp_account_manager_prepare_async (account_manager, NULL,
669 account_manager_ready_cb, NULL);
671 /* Handle channels */
672 dispatcher = setup_dispatcher ();
673 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
675 migrate_config_to_xdg_dir ();
678 window = empathy_main_window_show ();
679 icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
681 g_signal_connect (unique_app, "message-received",
682 G_CALLBACK (unique_app_message_cb), window);
686 log_manager = tpl_log_manager_dup_singleton ();
688 log_manager = empathy_log_manager_dup_singleton ();
689 empathy_log_manager_observe (log_manager, dispatcher);
692 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
693 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
695 g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
696 if (!chatroom_manager_ready)
698 g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
699 G_CALLBACK (chatroom_manager_ready_cb), account_manager);
703 chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
706 /* Create the call factory */
707 call_factory = empathy_call_factory_initialise ();
708 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
709 G_CALLBACK (new_call_handler_cb), NULL);
710 /* Create the FT factory */
711 ft_factory = empathy_ft_factory_dup_singleton ();
712 g_signal_connect (ft_factory, "new-ft-handler",
713 G_CALLBACK (new_ft_handler_cb), NULL);
714 g_signal_connect (ft_factory, "new-incoming-transfer",
715 G_CALLBACK (new_incoming_transfer_cb), NULL);
717 /* Location mananger */
719 location_manager = empathy_location_manager_dup_singleton ();
724 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
727 g_object_unref (debug_sender);
730 g_object_unref (idle);
731 g_object_unref (connectivity);
732 g_object_unref (icon);
733 g_object_unref (account_manager);
734 g_object_unref (log_manager);
735 g_object_unref (dispatcher);
736 g_object_unref (chatroom_manager);
738 g_object_unref (location_manager);
740 g_object_unref (ft_factory);
741 g_object_unref (unique_app);