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/gtk-clutter-embed.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-account-manager.h>
50 #include <libempathy/empathy-debugger.h>
51 #include <libempathy/empathy-dispatcher.h>
52 #include <libempathy/empathy-dispatch-operation.h>
53 #include <libempathy/empathy-log-manager.h>
54 #include <libempathy/empathy-ft-factory.h>
55 #include <libempathy/empathy-tp-chat.h>
56 #include <libempathy/empathy-tp-call.h>
58 #include <libempathy-gtk/empathy-conf.h>
59 #include <libempathy-gtk/empathy-ui-utils.h>
60 #include <libempathy-gtk/empathy-location-manager.h>
62 #include "empathy-accounts-dialog.h"
63 #include "empathy-main-window.h"
64 #include "empathy-status-icon.h"
65 #include "empathy-call-window.h"
66 #include "empathy-chat-window.h"
67 #include "empathy-ft-manager.h"
68 #include "bacon-message-connection.h"
70 #include "extensions/extensions.h"
72 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
73 #include <libempathy/empathy-debug.h>
77 static BaconMessageConnection *connection = NULL;
80 dispatch_cb (EmpathyDispatcher *dispatcher,
81 EmpathyDispatchOperation *operation,
86 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
88 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
89 EmpathyTpChat *tp_chat;
90 EmpathyChat *chat = NULL;
93 tp_chat = EMPATHY_TP_CHAT (
94 empathy_dispatch_operation_get_channel_wrapper (operation));
96 id = empathy_tp_chat_get_id (tp_chat);
98 EmpathyContact *contact;
100 contact = empathy_tp_chat_get_remote_contact (tp_chat);
102 id = empathy_contact_get_id (contact);
107 EmpathyAccountManager *manager;
108 TpConnection *connection;
109 EmpathyAccount *account;
111 manager = empathy_account_manager_dup_singleton ();
112 connection = empathy_tp_chat_get_connection (tp_chat);
113 account = empathy_account_manager_get_account (manager,
115 chat = empathy_chat_window_find_chat (account, id);
116 g_object_unref (manager);
120 empathy_chat_set_tp_chat (chat, tp_chat);
122 chat = empathy_chat_new (tp_chat);
125 empathy_chat_window_present_chat (chat);
127 empathy_dispatch_operation_claim (operation);
128 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
129 EmpathyCallFactory *factory;
131 factory = empathy_call_factory_get ();
132 empathy_call_factory_claim_channel (factory, operation);
133 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
134 EmpathyFTFactory *factory;
136 factory = empathy_ft_factory_dup_singleton ();
138 /* if the operation is not incoming, don't claim it,
139 * as it might have been triggered by another client, and
140 * we are observing it.
142 if (empathy_dispatch_operation_is_incoming (operation)) {
143 empathy_ft_factory_claim_channel (factory, operation);
149 service_ended_cb (MissionControl *mc,
152 DEBUG ("Mission Control stopped");
156 operation_error_cb (MissionControl *mc,
161 const gchar *message;
163 switch (error_code) {
164 case MC_DISCONNECTED_ERROR:
165 message = "Disconnected";
167 case MC_INVALID_HANDLE_ERROR:
168 message = "Invalid handle";
170 case MC_NO_MATCHING_CONNECTION_ERROR:
171 message = "No matching connection";
173 case MC_INVALID_ACCOUNT_ERROR:
174 message = "Invalid account";
176 case MC_PRESENCE_FAILURE_ERROR:
177 message = "Presence failure";
179 case MC_NO_ACCOUNTS_ERROR:
180 message = "No accounts";
182 case MC_NETWORK_ERROR:
183 message = "Network error";
185 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
186 message = "Contact does not support voice";
188 case MC_LOWMEM_ERROR:
191 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
192 message = "Channel request generic error";
194 case MC_CHANNEL_BANNED_ERROR:
195 message = "Channel banned";
197 case MC_CHANNEL_FULL_ERROR:
198 message = "Channel full";
200 case MC_CHANNEL_INVITE_ONLY_ERROR:
201 message = "Channel invite only";
204 message = "Unknown error code";
207 DEBUG ("Error during operation %d: %s", operation_id, message);
211 use_nm_notify_cb (EmpathyConf *conf,
215 EmpathyIdle *idle = user_data;
218 if (empathy_conf_get_bool (conf, key, &use_nm)) {
219 empathy_idle_set_use_nm (idle, use_nm);
224 create_salut_account (void)
227 McProtocol *protocol;
228 gboolean salut_created = FALSE;
229 EmpathyAccount *account;
230 EmpathyAccountManager *account_manager;
234 gchar *nickname = NULL;
235 gchar *first_name = NULL;
236 gchar *last_name = NULL;
239 GError *error = NULL;
241 /* Check if we already created a salut account */
242 empathy_conf_get_bool (empathy_conf_get (),
243 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
249 DEBUG ("Try to add a salut account...");
251 /* Check if the salut CM is installed */
252 profile = mc_profile_lookup ("salut");
254 DEBUG ("No salut profile");
257 protocol = mc_profile_get_protocol (profile);
259 DEBUG ("Salut not installed");
260 g_object_unref (profile);
263 g_object_unref (protocol);
265 /* Get self EContact from EDS */
266 if (!e_book_get_self (&contact, &book, &error)) {
267 DEBUG ("Failed to get self econtact: %s",
268 error ? error->message : "No error given");
269 g_clear_error (&error);
270 g_object_unref (profile);
274 empathy_conf_set_bool (empathy_conf_get (),
275 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
278 /* Check if there is already a salut account */
279 accounts = mc_accounts_list_by_profile (profile);
281 DEBUG ("There is already a salut account");
282 mc_accounts_list_free (accounts);
283 g_object_unref (profile);
287 account_manager = empathy_account_manager_dup_singleton ();
288 account = empathy_account_manager_create (account_manager, profile);
289 empathy_account_set_display_name (account, _("People nearby"));
290 g_object_unref (account_manager);
292 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
293 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
294 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
295 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
296 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
298 if (!tp_strdiff (nickname, "nickname")) {
303 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
304 "last-name=%s\nemail=%s\njid=%s\n",
305 nickname, first_name, last_name, email, jid);
307 empathy_account_set_param_string (account, "nickname", nickname ? nickname : "");
308 empathy_account_set_param_string (account, "first-name", first_name ? first_name : "");
309 empathy_account_set_param_string (account, "last-name", last_name ? last_name : "");
310 empathy_account_set_param_string (account, "email", email ? email : "");
311 empathy_account_set_param_string (account, "jid", jid ? jid : "");
318 g_object_unref (account);
319 g_object_unref (profile);
320 g_object_unref (contact);
321 g_object_unref (book);
325 migrate_config_to_xdg_dir (void)
327 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
329 GFile *xdg_file, *old_file;
330 static const gchar* filenames[] = {
334 "contact-groups.xml",
335 "status-presets.xml",
340 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
341 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
342 /* xdg config dir already exists */
347 old_dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
348 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
349 /* old config dir didn't exist */
355 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1) {
356 DEBUG ("Failed to create configuration directory; aborting migration");
362 for (i = 0; filenames[i]; i++) {
363 old_filename = g_build_filename (old_dir, filenames[i], NULL);
364 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS)) {
365 g_free (old_filename);
368 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
369 old_file = g_file_new_for_path (old_filename);
370 xdg_file = g_file_new_for_path (xdg_filename);
371 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
372 NULL, NULL, NULL, NULL)) {
373 DEBUG ("Failed to migrate %s", filenames[i]);
375 g_free (old_filename);
376 g_free (xdg_filename);
377 g_object_unref (old_file);
378 g_object_unref (xdg_file);
385 /* The code that handles single-instance and startup notification is
388 * Copyright (C) 2005 - Paolo Maggi
391 on_bacon_message_received (const char *message,
394 GtkWidget *window = data;
395 guint32 startup_timestamp;
397 g_return_if_fail (message != NULL);
399 DEBUG ("Other instance launched, presenting the main window. message='%s'",
402 if (strcmp (message, "accounts") == 0) {
403 /* accounts dialog requested */
404 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
406 startup_timestamp = atoi (message);
408 /* Set the proper interaction time on the window.
409 * Fall back to roundtripping to the X server when we
410 * don't have the timestamp, e.g. when launched from
411 * terminal. We also need to make sure that the window
412 * has been realized otherwise it will not work. lame. */
413 if (startup_timestamp == 0) {
414 /* Work if launched from the terminal */
415 DEBUG ("Using X server timestamp as a fallback");
417 if (!GTK_WIDGET_REALIZED (window)) {
418 gtk_widget_realize (GTK_WIDGET (window));
421 startup_timestamp = gdk_x11_get_server_time (gtk_widget_get_window (window));
424 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
429 get_startup_timestamp ()
431 const gchar *startup_id_env;
432 gchar *startup_id = NULL;
437 /* we don't unset the env, since startup-notification
438 * may still need it */
439 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
440 if (startup_id_env == NULL) {
444 startup_id = g_strdup (startup_id_env);
446 time_str = g_strrstr (startup_id, "_TIME");
447 if (time_str == NULL) {
453 /* Skip past the "_TIME" part */
456 retval = strtoul (time_str, &end, 0);
457 if (end == time_str || errno != 0)
463 return (retval > 0) ? retval : 0;
467 show_version_cb (const char *option_name,
472 g_print ("%s\n", PACKAGE_STRING);
480 new_incoming_transfer_cb (EmpathyFTFactory *factory,
481 EmpathyFTHandler *handler,
486 empathy_ft_manager_display_error (handler, error);
488 empathy_receive_file_with_file_chooser (handler);
493 new_ft_handler_cb (EmpathyFTFactory *factory,
494 EmpathyFTHandler *handler,
499 empathy_ft_manager_display_error (handler, error);
501 empathy_ft_manager_add_handler (handler);
504 g_object_unref (handler);
508 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
509 gboolean outgoing, gpointer user_data)
511 EmpathyCallWindow *window;
513 window = empathy_call_window_new (handler);
514 gtk_widget_show (GTK_WIDGET (window));
519 default_log_handler (const gchar *log_domain,
520 GLogLevelFlags log_level,
521 const gchar *message,
524 g_log_default_handler (log_domain, log_level, message, NULL);
526 /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
527 * debugger as they already have in empathy_debug. */
528 if (log_level != G_LOG_LEVEL_DEBUG
529 || tp_strdiff (log_domain, G_LOG_DOMAIN)) {
530 EmpathyDebugger *dbg;
533 dbg = empathy_debugger_get_singleton ();
534 g_get_current_time (&now);
536 empathy_debugger_add_message (dbg, &now, log_domain,
540 #endif /* ENABLE_DEBUG */
543 main (int argc, char *argv[])
545 guint32 startup_timestamp;
547 EmpathyLocationManager *location_manager = NULL;
549 EmpathyStatusIcon *icon;
550 EmpathyDispatcher *dispatcher;
551 EmpathyLogManager *log_manager;
552 EmpathyChatroomManager *chatroom_manager;
553 EmpathyCallFactory *call_factory;
554 EmpathyFTFactory *ft_factory;
558 gboolean autoconnect = TRUE;
559 gboolean no_connect = FALSE;
560 gboolean hide_contact_list = FALSE;
561 gboolean accounts_dialog = FALSE;
562 GError *error = NULL;
563 TpDBusDaemon *dbus_daemon;
564 GOptionEntry options[] = {
566 0, G_OPTION_ARG_NONE, &no_connect,
567 N_("Don't connect on startup"),
569 { "hide-contact-list", 'h',
570 0, G_OPTION_ARG_NONE, &hide_contact_list,
571 N_("Don't show the contact list on startup"),
574 0, G_OPTION_ARG_NONE, &accounts_dialog,
575 N_("Show the accounts dialog"),
578 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
583 g_thread_init (NULL);
586 if (!gtk_init_with_args (&argc, &argv,
587 N_("- Empathy Instant Messenger"),
588 options, GETTEXT_PACKAGE, &error)) {
589 g_warning ("Error in empathy init: %s", error->message);
594 g_set_application_name (_(PACKAGE_NAME));
595 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
597 gst_init (&argc, &argv);
599 #if HAVE_LIBCHAMPLAIN
600 gtk_clutter_init (&argc, &argv);
603 gtk_window_set_default_icon_name ("empathy");
604 textdomain (GETTEXT_PACKAGE);
607 /* Set up debugger */
608 g_log_set_default_handler (default_log_handler, NULL);
611 /* Setting up the bacon connection */
612 startup_timestamp = get_startup_timestamp ();
613 connection = bacon_message_connection_new ("empathy");
614 if (connection != NULL) {
615 if (!bacon_message_connection_get_is_server (connection)) {
618 if (accounts_dialog) {
619 DEBUG ("Showing accounts dialog from existing Empathy instance");
621 message = g_strdup ("accounts");
625 DEBUG ("Activating existing instance");
627 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
631 bacon_message_connection_send (connection, message);
633 /* We never popup a window, so tell startup-notification
634 * that we are done. */
635 gdk_notify_startup_complete ();
638 bacon_message_connection_free (connection);
643 g_warning ("Cannot create the 'empathy' bacon connection.");
646 /* Take well-known name */
647 dbus_daemon = tp_dbus_daemon_dup (&error);
649 if (!tp_dbus_daemon_request_name (dbus_daemon,
652 DEBUG ("Failed to request well-known name: %s",
653 error ? error->message : "no message");
654 g_clear_error (&error);
656 g_object_unref (dbus_daemon);
658 DEBUG ("Failed to dup dbus daemon: %s",
659 error ? error->message : "no message");
660 g_clear_error (&error);
664 mc = empathy_mission_control_dup_singleton ();
665 g_signal_connect (mc, "ServiceEnded",
666 G_CALLBACK (service_ended_cb),
668 g_signal_connect (mc, "Error",
669 G_CALLBACK (operation_error_cb),
672 if (accounts_dialog) {
675 dialog = empathy_accounts_dialog_show (NULL, NULL);
676 g_signal_connect (dialog, "destroy",
677 G_CALLBACK (gtk_main_quit),
684 /* Setting up Idle */
685 idle = empathy_idle_dup_singleton ();
686 empathy_idle_set_auto_away (idle, TRUE);
687 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
688 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
689 use_nm_notify_cb, idle);
692 empathy_conf_get_bool (empathy_conf_get (),
693 EMPATHY_PREFS_AUTOCONNECT,
695 if (autoconnect && ! no_connect &&
696 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
697 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
698 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
702 migrate_config_to_xdg_dir ();
703 create_salut_account ();
706 window = empathy_main_window_show ();
707 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
710 /* We se the callback here because we need window */
711 bacon_message_connection_set_callback (connection,
712 on_bacon_message_received,
716 /* Handle channels */
717 dispatcher = empathy_dispatcher_dup_singleton ();
718 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
721 log_manager = empathy_log_manager_dup_singleton ();
722 empathy_log_manager_observe (log_manager, dispatcher);
724 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
725 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
727 notify_init (_(PACKAGE_NAME));
728 /* Create the call factory */
729 call_factory = empathy_call_factory_initialise ();
730 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
731 G_CALLBACK (new_call_handler_cb), NULL);
732 /* Create the FT factory */
733 ft_factory = empathy_ft_factory_dup_singleton ();
734 g_signal_connect (ft_factory, "new-ft-handler",
735 G_CALLBACK (new_ft_handler_cb), NULL);
736 g_signal_connect (ft_factory, "new-incoming-transfer",
737 G_CALLBACK (new_incoming_transfer_cb), NULL);
739 /* Location mananger */
741 location_manager = empathy_location_manager_dup_singleton ();
746 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
749 g_object_unref (idle);
750 g_object_unref (icon);
751 g_object_unref (log_manager);
752 g_object_unref (dispatcher);
753 g_object_unref (chatroom_manager);
755 g_object_unref (location_manager);
757 g_object_unref (ft_factory);