1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
30 #include <glib/gi18n.h>
35 #include <clutter-gtk/clutter-gtk.h>
38 #include <libebook/e-book.h>
39 #include <libnotify/notify.h>
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/util.h>
43 #include <libmissioncontrol/mission-control.h>
45 #include <libempathy/empathy-idle.h>
46 #include <libempathy/empathy-utils.h>
47 #include <libempathy/empathy-call-factory.h>
48 #include <libempathy/empathy-chatroom-manager.h>
49 #include <libempathy/empathy-connectivity.h>
50 #include <libempathy/empathy-account-manager.h>
51 #include <libempathy/empathy-debugger.h>
52 #include <libempathy/empathy-dispatcher.h>
53 #include <libempathy/empathy-dispatch-operation.h>
54 #include <libempathy/empathy-log-manager.h>
55 #include <libempathy/empathy-ft-factory.h>
56 #include <libempathy/empathy-tp-chat.h>
57 #include <libempathy/empathy-tp-call.h>
59 #include <libempathy-gtk/empathy-conf.h>
60 #include <libempathy-gtk/empathy-ui-utils.h>
61 #include <libempathy-gtk/empathy-location-manager.h>
63 #include "empathy-accounts-dialog.h"
64 #include "empathy-main-window.h"
65 #include "empathy-status-icon.h"
66 #include "empathy-call-window.h"
67 #include "empathy-chat-window.h"
68 #include "empathy-ft-manager.h"
69 #include "bacon-message-connection.h"
71 #include "extensions/extensions.h"
73 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
74 #include <libempathy/empathy-debug.h>
78 static BaconMessageConnection *connection = NULL;
81 dispatch_cb (EmpathyDispatcher *dispatcher,
82 EmpathyDispatchOperation *operation,
87 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
89 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
90 EmpathyTpChat *tp_chat;
91 EmpathyChat *chat = NULL;
94 tp_chat = EMPATHY_TP_CHAT (
95 empathy_dispatch_operation_get_channel_wrapper (operation));
97 id = empathy_tp_chat_get_id (tp_chat);
99 EmpathyContact *contact;
101 contact = empathy_tp_chat_get_remote_contact (tp_chat);
103 id = empathy_contact_get_id (contact);
108 EmpathyAccountManager *manager;
109 TpConnection *connection;
110 EmpathyAccount *account;
112 manager = empathy_account_manager_dup_singleton ();
113 connection = empathy_tp_chat_get_connection (tp_chat);
114 account = empathy_account_manager_get_account (manager,
116 chat = empathy_chat_window_find_chat (account, id);
117 g_object_unref (manager);
121 empathy_chat_set_tp_chat (chat, tp_chat);
123 chat = empathy_chat_new (tp_chat);
126 empathy_chat_window_present_chat (chat);
128 empathy_dispatch_operation_claim (operation);
129 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
130 EmpathyCallFactory *factory;
132 factory = empathy_call_factory_get ();
133 empathy_call_factory_claim_channel (factory, operation);
134 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
135 EmpathyFTFactory *factory;
137 factory = empathy_ft_factory_dup_singleton ();
139 /* if the operation is not incoming, don't claim it,
140 * as it might have been triggered by another client, and
141 * we are observing it.
143 if (empathy_dispatch_operation_is_incoming (operation)) {
144 empathy_ft_factory_claim_channel (factory, operation);
150 service_ended_cb (MissionControl *mc,
153 DEBUG ("Mission Control stopped");
157 operation_error_cb (MissionControl *mc,
162 const gchar *message;
164 switch (error_code) {
165 case MC_DISCONNECTED_ERROR:
166 message = "Disconnected";
168 case MC_INVALID_HANDLE_ERROR:
169 message = "Invalid handle";
171 case MC_NO_MATCHING_CONNECTION_ERROR:
172 message = "No matching connection";
174 case MC_INVALID_ACCOUNT_ERROR:
175 message = "Invalid account";
177 case MC_PRESENCE_FAILURE_ERROR:
178 message = "Presence failure";
180 case MC_NO_ACCOUNTS_ERROR:
181 message = "No accounts";
183 case MC_NETWORK_ERROR:
184 message = "Network error";
186 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
187 message = "Contact does not support voice";
189 case MC_LOWMEM_ERROR:
192 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
193 message = "Channel request generic error";
195 case MC_CHANNEL_BANNED_ERROR:
196 message = "Channel banned";
198 case MC_CHANNEL_FULL_ERROR:
199 message = "Channel full";
201 case MC_CHANNEL_INVITE_ONLY_ERROR:
202 message = "Channel invite only";
205 message = "Unknown error code";
208 DEBUG ("Error during operation %d: %s", operation_id, message);
212 use_conn_notify_cb (EmpathyConf *conf,
216 EmpathyConnectivity *connectivity = user_data;
219 if (empathy_conf_get_bool (conf, key, &use_conn)) {
220 empathy_connectivity_set_use_conn (connectivity, use_conn);
225 create_salut_account (void)
228 McProtocol *protocol;
229 gboolean salut_created = FALSE;
230 EmpathyAccount *account;
231 EmpathyAccountManager *account_manager;
235 gchar *nickname = NULL;
236 gchar *first_name = NULL;
237 gchar *last_name = NULL;
240 GError *error = NULL;
242 /* Check if we already created a salut account */
243 empathy_conf_get_bool (empathy_conf_get (),
244 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
250 DEBUG ("Try to add a salut account...");
252 /* Check if the salut CM is installed */
253 profile = mc_profile_lookup ("salut");
255 DEBUG ("No salut profile");
258 protocol = mc_profile_get_protocol (profile);
260 DEBUG ("Salut not installed");
261 g_object_unref (profile);
264 g_object_unref (protocol);
266 /* Get self EContact from EDS */
267 if (!e_book_get_self (&contact, &book, &error)) {
268 DEBUG ("Failed to get self econtact: %s",
269 error ? error->message : "No error given");
270 g_clear_error (&error);
271 g_object_unref (profile);
275 empathy_conf_set_bool (empathy_conf_get (),
276 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
279 /* Check if there is already a salut account */
280 accounts = mc_accounts_list_by_profile (profile);
282 DEBUG ("There is already a salut account");
283 mc_accounts_list_free (accounts);
284 g_object_unref (profile);
288 account_manager = empathy_account_manager_dup_singleton ();
289 account = empathy_account_manager_create (account_manager, profile);
290 empathy_account_set_display_name (account, _("People nearby"));
291 g_object_unref (account_manager);
293 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
294 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
295 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
296 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
297 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
299 if (!tp_strdiff (nickname, "nickname")) {
304 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
305 "last-name=%s\nemail=%s\njid=%s\n",
306 nickname, first_name, last_name, email, jid);
308 empathy_account_set_param_string (account, "nickname", nickname ? nickname : "");
309 empathy_account_set_param_string (account, "first-name", first_name ? first_name : "");
310 empathy_account_set_param_string (account, "last-name", last_name ? last_name : "");
311 empathy_account_set_param_string (account, "email", email ? email : "");
312 empathy_account_set_param_string (account, "jid", jid ? jid : "");
319 g_object_unref (account);
320 g_object_unref (profile);
321 g_object_unref (contact);
322 g_object_unref (book);
326 migrate_config_to_xdg_dir (void)
328 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
330 GFile *xdg_file, *old_file;
331 static const gchar* filenames[] = {
335 "contact-groups.xml",
336 "status-presets.xml",
341 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
342 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
343 /* xdg config dir already exists */
348 old_dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
349 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
350 /* old config dir didn't exist */
356 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1) {
357 DEBUG ("Failed to create configuration directory; aborting migration");
363 for (i = 0; filenames[i]; i++) {
364 old_filename = g_build_filename (old_dir, filenames[i], NULL);
365 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS)) {
366 g_free (old_filename);
369 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
370 old_file = g_file_new_for_path (old_filename);
371 xdg_file = g_file_new_for_path (xdg_filename);
372 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
373 NULL, NULL, NULL, NULL)) {
374 DEBUG ("Failed to migrate %s", filenames[i]);
376 g_free (old_filename);
377 g_free (xdg_filename);
378 g_object_unref (old_file);
379 g_object_unref (xdg_file);
386 /* The code that handles single-instance and startup notification is
389 * Copyright (C) 2005 - Paolo Maggi
392 on_bacon_message_received (const char *message,
395 GtkWidget *window = data;
396 guint32 startup_timestamp;
398 g_return_if_fail (message != NULL);
400 DEBUG ("Other instance launched, presenting the main window. message='%s'",
403 if (strcmp (message, "accounts") == 0) {
404 /* accounts dialog requested */
405 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
407 startup_timestamp = atoi (message);
409 /* Set the proper interaction time on the window.
410 * Fall back to roundtripping to the X server when we
411 * don't have the timestamp, e.g. when launched from
412 * terminal. We also need to make sure that the window
413 * has been realized otherwise it will not work. lame. */
414 if (startup_timestamp == 0) {
415 /* Work if launched from the terminal */
416 DEBUG ("Using X server timestamp as a fallback");
418 if (!GTK_WIDGET_REALIZED (window)) {
419 gtk_widget_realize (GTK_WIDGET (window));
422 startup_timestamp = gdk_x11_get_server_time (gtk_widget_get_window (window));
425 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
430 get_startup_timestamp ()
432 const gchar *startup_id_env;
433 gchar *startup_id = NULL;
438 /* we don't unset the env, since startup-notification
439 * may still need it */
440 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
441 if (startup_id_env == NULL) {
445 startup_id = g_strdup (startup_id_env);
447 time_str = g_strrstr (startup_id, "_TIME");
448 if (time_str == NULL) {
454 /* Skip past the "_TIME" part */
457 retval = strtoul (time_str, &end, 0);
458 if (end == time_str || errno != 0)
464 return (retval > 0) ? retval : 0;
468 show_version_cb (const char *option_name,
473 g_print ("%s\n", PACKAGE_STRING);
481 new_incoming_transfer_cb (EmpathyFTFactory *factory,
482 EmpathyFTHandler *handler,
487 empathy_ft_manager_display_error (handler, error);
489 empathy_receive_file_with_file_chooser (handler);
494 new_ft_handler_cb (EmpathyFTFactory *factory,
495 EmpathyFTHandler *handler,
500 empathy_ft_manager_display_error (handler, error);
502 empathy_ft_manager_add_handler (handler);
505 g_object_unref (handler);
509 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
510 gboolean outgoing, gpointer user_data)
512 EmpathyCallWindow *window;
514 window = empathy_call_window_new (handler);
515 gtk_widget_show (GTK_WIDGET (window));
520 default_log_handler (const gchar *log_domain,
521 GLogLevelFlags log_level,
522 const gchar *message,
525 g_log_default_handler (log_domain, log_level, message, NULL);
527 /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
528 * debugger as they already have in empathy_debug. */
529 if (log_level != G_LOG_LEVEL_DEBUG
530 || tp_strdiff (log_domain, G_LOG_DOMAIN)) {
531 EmpathyDebugger *dbg;
534 dbg = empathy_debugger_get_singleton ();
535 g_get_current_time (&now);
537 empathy_debugger_add_message (dbg, &now, log_domain,
541 #endif /* ENABLE_DEBUG */
544 main (int argc, char *argv[])
546 guint32 startup_timestamp;
548 EmpathyLocationManager *location_manager = NULL;
550 EmpathyStatusIcon *icon;
551 EmpathyDispatcher *dispatcher;
552 EmpathyLogManager *log_manager;
553 EmpathyChatroomManager *chatroom_manager;
554 EmpathyCallFactory *call_factory;
555 EmpathyFTFactory *ft_factory;
559 EmpathyConnectivity *connectivity;
560 gboolean autoconnect = TRUE;
561 gboolean no_connect = FALSE;
562 gboolean hide_contact_list = FALSE;
563 gboolean accounts_dialog = FALSE;
564 GError *error = NULL;
565 TpDBusDaemon *dbus_daemon;
566 GOptionEntry options[] = {
568 0, G_OPTION_ARG_NONE, &no_connect,
569 N_("Don't connect on startup"),
571 { "hide-contact-list", 'h',
572 0, G_OPTION_ARG_NONE, &hide_contact_list,
573 N_("Don't show the contact list on startup"),
576 0, G_OPTION_ARG_NONE, &accounts_dialog,
577 N_("Show the accounts dialog"),
580 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
585 g_thread_init (NULL);
588 if (!gtk_init_with_args (&argc, &argv,
589 N_("- Empathy IM Client"),
590 options, GETTEXT_PACKAGE, &error)) {
591 g_warning ("Error in empathy init: %s", error->message);
596 g_set_application_name (_(PACKAGE_NAME));
597 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
599 gst_init (&argc, &argv);
601 #if HAVE_LIBCHAMPLAIN
602 gtk_clutter_init (&argc, &argv);
605 gtk_window_set_default_icon_name ("empathy");
606 textdomain (GETTEXT_PACKAGE);
609 /* Set up debugger */
610 g_log_set_default_handler (default_log_handler, NULL);
613 /* Setting up the bacon connection */
614 startup_timestamp = get_startup_timestamp ();
615 connection = bacon_message_connection_new ("empathy");
616 if (connection != NULL) {
617 if (!bacon_message_connection_get_is_server (connection)) {
620 if (accounts_dialog) {
621 DEBUG ("Showing accounts dialog from existing Empathy instance");
623 message = g_strdup ("accounts");
627 DEBUG ("Activating existing instance");
629 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
633 bacon_message_connection_send (connection, message);
635 /* We never popup a window, so tell startup-notification
636 * that we are done. */
637 gdk_notify_startup_complete ();
640 bacon_message_connection_free (connection);
645 g_warning ("Cannot create the 'empathy' bacon connection.");
648 /* Take well-known name */
649 dbus_daemon = tp_dbus_daemon_dup (&error);
651 if (!tp_dbus_daemon_request_name (dbus_daemon,
654 DEBUG ("Failed to request well-known name: %s",
655 error ? error->message : "no message");
656 g_clear_error (&error);
658 g_object_unref (dbus_daemon);
660 DEBUG ("Failed to dup dbus daemon: %s",
661 error ? error->message : "no message");
662 g_clear_error (&error);
666 mc = empathy_mission_control_dup_singleton ();
667 g_signal_connect (mc, "ServiceEnded",
668 G_CALLBACK (service_ended_cb),
670 g_signal_connect (mc, "Error",
671 G_CALLBACK (operation_error_cb),
674 if (accounts_dialog) {
677 dialog = empathy_accounts_dialog_show (NULL, NULL);
678 g_signal_connect (dialog, "destroy",
679 G_CALLBACK (gtk_main_quit),
686 /* Setting up Idle */
687 idle = empathy_idle_dup_singleton ();
688 empathy_idle_set_auto_away (idle, TRUE);
690 /* Setting up Connectivity */
691 connectivity = empathy_connectivity_dup_singleton ();
692 use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
694 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
695 use_conn_notify_cb, connectivity);
698 empathy_conf_get_bool (empathy_conf_get (),
699 EMPATHY_PREFS_AUTOCONNECT,
701 if (autoconnect && ! no_connect &&
702 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
703 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
704 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
708 migrate_config_to_xdg_dir ();
709 create_salut_account ();
712 window = empathy_main_window_show ();
713 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
716 /* We se the callback here because we need window */
717 bacon_message_connection_set_callback (connection,
718 on_bacon_message_received,
722 /* Handle channels */
723 dispatcher = empathy_dispatcher_dup_singleton ();
724 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
727 log_manager = empathy_log_manager_dup_singleton ();
728 empathy_log_manager_observe (log_manager, dispatcher);
730 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
731 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
733 notify_init (_(PACKAGE_NAME));
734 /* Create the call factory */
735 call_factory = empathy_call_factory_initialise ();
736 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
737 G_CALLBACK (new_call_handler_cb), NULL);
738 /* Create the FT factory */
739 ft_factory = empathy_ft_factory_dup_singleton ();
740 g_signal_connect (ft_factory, "new-ft-handler",
741 G_CALLBACK (new_ft_handler_cb), NULL);
742 g_signal_connect (ft_factory, "new-incoming-transfer",
743 G_CALLBACK (new_incoming_transfer_cb), NULL);
745 /* Location mananger */
747 location_manager = empathy_location_manager_dup_singleton ();
752 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
755 g_object_unref (idle);
756 g_object_unref (connectivity);
757 g_object_unref (icon);
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);