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 <telepathy-glib/connection-manager.h>
44 #include <libmissioncontrol/mission-control.h>
46 #include <libempathy/empathy-idle.h>
47 #include <libempathy/empathy-utils.h>
48 #include <libempathy/empathy-call-factory.h>
49 #include <libempathy/empathy-chatroom-manager.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_nm_notify_cb (EmpathyConf *conf,
216 EmpathyIdle *idle = user_data;
219 if (empathy_conf_get_bool (conf, key, &use_nm)) {
220 empathy_idle_set_use_nm (idle, use_nm);
225 should_create_salut_account (void)
227 EmpathyAccountManager *manager;
228 gboolean salut_created = FALSE;
231 /* Check if we already created a salut account */
232 empathy_conf_get_bool (empathy_conf_get (),
233 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
236 manager = empathy_account_manager_dup_singleton ();
237 accounts = empathy_account_manager_dup_accounts (manager);
239 for (l = accounts; l != NULL; l = g_list_next (l)) {
240 EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
242 if (!tp_strdiff (empathy_account_get_protocol (account), "local-xmpp"))
243 salut_created = TRUE;
245 g_object_unref (account);
248 g_object_unref (manager);
250 return !salut_created;
254 create_salut_account_if_needed (void)
256 EmpathyAccount *account;
257 EmpathyAccountManager *account_manager;
260 gchar *nickname = NULL;
261 gchar *first_name = NULL;
262 gchar *last_name = NULL;
265 GError *error = NULL;
268 if (!should_create_salut_account())
271 DEBUG ("Trying to add a salut account...");
273 /* Get self EContact from EDS */
274 if (!e_book_get_self (&contact, &book, &error)) {
275 DEBUG ("Failed to get self econtact: %s",
276 error ? error->message : "No error given");
277 g_clear_error (&error);
281 account_manager = empathy_account_manager_dup_singleton ();
282 account = empathy_account_manager_create (account_manager,
283 "salut", "local-xmpp", _("People nearby"));
284 g_object_unref (account_manager);
285 empathy_account_set_enabled (account, TRUE);
287 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
288 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
289 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
290 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
291 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
293 if (!tp_strdiff (nickname, "nickname")) {
298 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
299 "last-name=%s\nemail=%s\njid=%s\n",
300 nickname, first_name, last_name, email, jid);
302 empathy_account_set_param_string (account, "nickname", nickname ? nickname : "");
303 empathy_account_set_param_string (account, "first-name", first_name ? first_name : "");
304 empathy_account_set_param_string (account, "last-name", last_name ? last_name : "");
305 empathy_account_set_param_string (account, "email", email ? email : "");
306 empathy_account_set_param_string (account, "jid", jid ? jid : "");
313 g_object_unref (account);
314 g_object_unref (contact);
315 g_object_unref (book);
317 empathy_conf_set_bool (empathy_conf_get (),
318 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
324 connection_manager_listed (TpConnectionManager * const * cms,
328 GObject *weak_object)
332 DEBUG ("Couldn't get the connection manager list: %s", error->message);
336 for (i = 0; i < n_cms; i++) {
337 if (!tp_strdiff (tp_connection_manager_get_name (cms[i]), "salut")) {
338 /* salut installed, see if we need to create a new account */
339 create_salut_account_if_needed ();
346 create_salut_account (void)
350 if (!should_create_salut_account())
353 d = tp_dbus_daemon_dup (NULL);
355 tp_list_connection_managers (d, connection_manager_listed, NULL,
361 /* The code that handles single-instance and startup notification is
364 * Copyright (C) 2005 - Paolo Maggi
367 on_bacon_message_received (const char *message,
370 GtkWidget *window = data;
371 guint32 startup_timestamp;
373 g_return_if_fail (message != NULL);
375 DEBUG ("Other instance launched, presenting the main window. message='%s'",
378 if (strcmp (message, "accounts") == 0) {
379 /* accounts dialog requested */
380 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
382 startup_timestamp = atoi (message);
384 /* Set the proper interaction time on the window.
385 * Fall back to roundtripping to the X server when we
386 * don't have the timestamp, e.g. when launched from
387 * terminal. We also need to make sure that the window
388 * has been realized otherwise it will not work. lame. */
389 if (startup_timestamp == 0) {
390 /* Work if launched from the terminal */
391 DEBUG ("Using X server timestamp as a fallback");
393 if (!GTK_WIDGET_REALIZED (window)) {
394 gtk_widget_realize (GTK_WIDGET (window));
397 startup_timestamp = gdk_x11_get_server_time (window->window);
400 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
405 get_startup_timestamp ()
407 const gchar *startup_id_env;
408 gchar *startup_id = NULL;
413 /* we don't unset the env, since startup-notification
414 * may still need it */
415 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
416 if (startup_id_env == NULL) {
420 startup_id = g_strdup (startup_id_env);
422 time_str = g_strrstr (startup_id, "_TIME");
423 if (time_str == NULL) {
429 /* Skip past the "_TIME" part */
432 retval = strtoul (time_str, &end, 0);
433 if (end == time_str || errno != 0)
439 return (retval > 0) ? retval : 0;
443 show_version_cb (const char *option_name,
448 g_print ("%s\n", PACKAGE_STRING);
456 new_incoming_transfer_cb (EmpathyFTFactory *factory,
457 EmpathyFTHandler *handler,
462 empathy_ft_manager_display_error (handler, error);
464 empathy_receive_file_with_file_chooser (handler);
469 new_ft_handler_cb (EmpathyFTFactory *factory,
470 EmpathyFTHandler *handler,
475 empathy_ft_manager_display_error (handler, error);
477 empathy_ft_manager_add_handler (handler);
480 g_object_unref (handler);
484 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
485 gboolean outgoing, gpointer user_data)
487 EmpathyCallWindow *window;
489 window = empathy_call_window_new (handler);
490 gtk_widget_show (GTK_WIDGET (window));
495 default_log_handler (const gchar *log_domain,
496 GLogLevelFlags log_level,
497 const gchar *message,
500 g_log_default_handler (log_domain, log_level, message, NULL);
502 /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
503 * debugger as they already have in empathy_debug. */
504 if (log_level != G_LOG_LEVEL_DEBUG
505 || tp_strdiff (log_domain, G_LOG_DOMAIN)) {
506 EmpathyDebugger *dbg;
509 dbg = empathy_debugger_get_singleton ();
510 g_get_current_time (&now);
512 empathy_debugger_add_message (dbg, &now, log_domain,
516 #endif /* ENABLE_DEBUG */
519 main (int argc, char *argv[])
521 guint32 startup_timestamp;
523 EmpathyLocationManager *location_manager = NULL;
525 EmpathyStatusIcon *icon;
526 EmpathyDispatcher *dispatcher;
527 EmpathyAccountManager *account_manager;
528 EmpathyLogManager *log_manager;
529 EmpathyChatroomManager *chatroom_manager;
530 EmpathyCallFactory *call_factory;
531 EmpathyFTFactory *ft_factory;
535 gboolean autoconnect = TRUE;
536 gboolean no_connect = FALSE;
537 gboolean hide_contact_list = FALSE;
538 gboolean accounts_dialog = FALSE;
539 GError *error = NULL;
540 TpDBusDaemon *dbus_daemon;
541 GOptionEntry options[] = {
543 0, G_OPTION_ARG_NONE, &no_connect,
544 N_("Don't connect on startup"),
546 { "hide-contact-list", 'h',
547 0, G_OPTION_ARG_NONE, &hide_contact_list,
548 N_("Don't show the contact list on startup"),
551 0, G_OPTION_ARG_NONE, &accounts_dialog,
552 N_("Show the accounts dialog"),
555 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
560 g_thread_init (NULL);
563 if (!gtk_init_with_args (&argc, &argv,
564 N_("- Empathy Instant Messenger"),
565 options, GETTEXT_PACKAGE, &error)) {
566 g_warning ("Error in empathy init: %s", error->message);
571 g_set_application_name (_(PACKAGE_NAME));
572 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
574 gst_init (&argc, &argv);
576 #if HAVE_LIBCHAMPLAIN
577 gtk_clutter_init (&argc, &argv);
580 gtk_window_set_default_icon_name ("empathy");
581 textdomain (GETTEXT_PACKAGE);
584 /* Set up debugger */
585 g_log_set_default_handler (default_log_handler, NULL);
588 /* Setting up the bacon connection */
589 startup_timestamp = get_startup_timestamp ();
590 connection = bacon_message_connection_new ("empathy");
591 if (connection != NULL) {
592 if (!bacon_message_connection_get_is_server (connection)) {
595 if (accounts_dialog) {
596 DEBUG ("Showing accounts dialog from existing Empathy instance");
598 message = g_strdup ("accounts");
602 DEBUG ("Activating existing instance");
604 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
608 bacon_message_connection_send (connection, message);
610 /* We never popup a window, so tell startup-notification
611 * that we are done. */
612 gdk_notify_startup_complete ();
615 bacon_message_connection_free (connection);
620 g_warning ("Cannot create the 'empathy' bacon connection.");
623 /* Take well-known name */
624 dbus_daemon = tp_dbus_daemon_dup (&error);
626 if (!tp_dbus_daemon_request_name (dbus_daemon,
629 DEBUG ("Failed to request well-known name: %s",
630 error ? error->message : "no message");
631 g_clear_error (&error);
633 g_object_unref (dbus_daemon);
635 DEBUG ("Failed to dup dbus daemon: %s",
636 error ? error->message : "no message");
637 g_clear_error (&error);
641 mc = empathy_mission_control_dup_singleton ();
642 g_signal_connect (mc, "ServiceEnded",
643 G_CALLBACK (service_ended_cb),
645 g_signal_connect (mc, "Error",
646 G_CALLBACK (operation_error_cb),
649 if (accounts_dialog) {
652 dialog = empathy_accounts_dialog_show (NULL, NULL);
653 g_signal_connect (dialog, "destroy",
654 G_CALLBACK (gtk_main_quit),
661 /* Setting up Idle */
662 idle = empathy_idle_dup_singleton ();
663 empathy_idle_set_auto_away (idle, TRUE);
664 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
665 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
666 use_nm_notify_cb, idle);
669 empathy_conf_get_bool (empathy_conf_get (),
670 EMPATHY_PREFS_AUTOCONNECT,
672 if (autoconnect && ! no_connect &&
673 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
674 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
675 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
678 /* account management */
679 account_manager = empathy_account_manager_dup_singleton ();
681 create_salut_account ();
684 window = empathy_main_window_show ();
685 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
688 /* We se the callback here because we need window */
689 bacon_message_connection_set_callback (connection,
690 on_bacon_message_received,
694 /* Handle channels */
695 dispatcher = empathy_dispatcher_dup_singleton ();
696 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
699 log_manager = empathy_log_manager_dup_singleton ();
700 empathy_log_manager_observe (log_manager, dispatcher);
702 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
703 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
705 notify_init (_(PACKAGE_NAME));
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 (idle);
728 g_object_unref (icon);
729 g_object_unref (account_manager);
730 g_object_unref (log_manager);
731 g_object_unref (dispatcher);
732 g_object_unref (chatroom_manager);
734 g_object_unref (location_manager);
736 g_object_unref (ft_factory);