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/mc-account.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;
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 create_salut_account (void)
228 McProtocol *protocol;
229 gboolean salut_created = FALSE;
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 = mc_account_create (profile);
288 mc_account_set_display_name (account, _("People nearby"));
290 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
291 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
292 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
293 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
294 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
296 if (!tp_strdiff (nickname, "nickname")) {
301 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
302 "last-name=%s\nemail=%s\njid=%s\n",
303 nickname, first_name, last_name, email, jid);
305 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
306 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
307 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
308 mc_account_set_param_string (account, "email", email ? email : "");
309 mc_account_set_param_string (account, "jid", jid ? jid : "");
316 g_object_unref (account);
317 g_object_unref (profile);
318 g_object_unref (contact);
319 g_object_unref (book);
322 /* The code that handles single-instance and startup notification is
325 * Copyright (C) 2005 - Paolo Maggi
328 on_bacon_message_received (const char *message,
331 GtkWidget *window = data;
332 guint32 startup_timestamp;
334 g_return_if_fail (message != NULL);
336 DEBUG ("Other instance launched, presenting the main window. message='%s'",
339 if (strcmp (message, "accounts") == 0) {
340 /* accounts dialog requested */
341 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
343 startup_timestamp = atoi (message);
345 /* Set the proper interaction time on the window.
346 * Fall back to roundtripping to the X server when we
347 * don't have the timestamp, e.g. when launched from
348 * terminal. We also need to make sure that the window
349 * has been realized otherwise it will not work. lame. */
350 if (startup_timestamp == 0) {
351 /* Work if launched from the terminal */
352 DEBUG ("Using X server timestamp as a fallback");
354 if (!GTK_WIDGET_REALIZED (window)) {
355 gtk_widget_realize (GTK_WIDGET (window));
358 startup_timestamp = gdk_x11_get_server_time (window->window);
361 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
366 get_startup_timestamp ()
368 const gchar *startup_id_env;
369 gchar *startup_id = NULL;
374 /* we don't unset the env, since startup-notification
375 * may still need it */
376 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
377 if (startup_id_env == NULL) {
381 startup_id = g_strdup (startup_id_env);
383 time_str = g_strrstr (startup_id, "_TIME");
384 if (time_str == NULL) {
390 /* Skip past the "_TIME" part */
393 retval = strtoul (time_str, &end, 0);
394 if (end == time_str || errno != 0)
400 return (retval > 0) ? retval : 0;
404 show_version_cb (const char *option_name,
409 g_print ("%s\n", PACKAGE_STRING);
417 new_incoming_transfer_cb (EmpathyFTFactory *factory,
418 EmpathyFTHandler *handler,
423 empathy_ft_manager_display_error (handler, error);
425 empathy_receive_file_with_file_chooser (handler);
430 new_ft_handler_cb (EmpathyFTFactory *factory,
431 EmpathyFTHandler *handler,
436 empathy_ft_manager_display_error (handler, error);
438 empathy_ft_manager_add_handler (handler);
441 g_object_unref (handler);
445 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
446 gboolean outgoing, gpointer user_data)
448 EmpathyCallWindow *window;
450 window = empathy_call_window_new (handler);
451 gtk_widget_show (GTK_WIDGET (window));
456 default_log_handler (const gchar *log_domain,
457 GLogLevelFlags log_level,
458 const gchar *message,
461 g_log_default_handler (log_domain, log_level, message, NULL);
463 /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
464 * debugger as they already have in empathy_debug. */
465 if (log_level != G_LOG_LEVEL_DEBUG
466 || tp_strdiff (log_domain, G_LOG_DOMAIN)) {
467 EmpathyDebugger *dbg;
470 dbg = empathy_debugger_get_singleton ();
471 g_get_current_time (&now);
473 empathy_debugger_add_message (dbg, &now, log_domain,
477 #endif /* ENABLE_DEBUG */
480 main (int argc, char *argv[])
482 guint32 startup_timestamp;
484 EmpathyLocationManager *location_manager = NULL;
486 EmpathyStatusIcon *icon;
487 EmpathyDispatcher *dispatcher;
488 EmpathyLogManager *log_manager;
489 EmpathyChatroomManager *chatroom_manager;
490 EmpathyCallFactory *call_factory;
491 EmpathyFTFactory *ft_factory;
495 gboolean autoconnect = TRUE;
496 gboolean no_connect = FALSE;
497 gboolean hide_contact_list = FALSE;
498 gboolean accounts_dialog = FALSE;
499 GError *error = NULL;
500 TpDBusDaemon *dbus_daemon;
501 GOptionEntry options[] = {
503 0, G_OPTION_ARG_NONE, &no_connect,
504 N_("Don't connect on startup"),
506 { "hide-contact-list", 'h',
507 0, G_OPTION_ARG_NONE, &hide_contact_list,
508 N_("Don't show the contact list on startup"),
511 0, G_OPTION_ARG_NONE, &accounts_dialog,
512 N_("Show the accounts dialog"),
515 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
520 g_thread_init (NULL);
523 if (!gtk_init_with_args (&argc, &argv,
524 N_("- Empathy Instant Messenger"),
525 options, GETTEXT_PACKAGE, &error)) {
526 g_warning ("Error in empathy init: %s", error->message);
531 g_set_application_name (_(PACKAGE_NAME));
532 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
534 gst_init (&argc, &argv);
536 #if HAVE_LIBCHAMPLAIN
537 gtk_clutter_init (&argc, &argv);
540 gtk_window_set_default_icon_name ("empathy");
541 textdomain (GETTEXT_PACKAGE);
544 /* Set up debugger */
545 g_log_set_default_handler (default_log_handler, NULL);
548 /* Setting up the bacon connection */
549 startup_timestamp = get_startup_timestamp ();
550 connection = bacon_message_connection_new ("empathy");
551 if (connection != NULL) {
552 if (!bacon_message_connection_get_is_server (connection)) {
555 if (accounts_dialog) {
556 DEBUG ("Showing accounts dialog from existing Empathy instance");
558 message = g_strdup ("accounts");
562 DEBUG ("Activating existing instance");
564 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
568 bacon_message_connection_send (connection, message);
570 /* We never popup a window, so tell startup-notification
571 * that we are done. */
572 gdk_notify_startup_complete ();
575 bacon_message_connection_free (connection);
580 g_warning ("Cannot create the 'empathy' bacon connection.");
583 /* Take well-known name */
584 dbus_daemon = tp_dbus_daemon_dup (&error);
586 if (!tp_dbus_daemon_request_name (dbus_daemon,
589 DEBUG ("Failed to request well-known name: %s",
590 error ? error->message : "no message");
591 g_clear_error (&error);
593 g_object_unref (dbus_daemon);
595 DEBUG ("Failed to dup dbus daemon: %s",
596 error ? error->message : "no message");
597 g_clear_error (&error);
601 mc = empathy_mission_control_dup_singleton ();
602 g_signal_connect (mc, "ServiceEnded",
603 G_CALLBACK (service_ended_cb),
605 g_signal_connect (mc, "Error",
606 G_CALLBACK (operation_error_cb),
609 if (accounts_dialog) {
612 dialog = empathy_accounts_dialog_show (NULL, NULL);
613 g_signal_connect (dialog, "destroy",
614 G_CALLBACK (gtk_main_quit),
621 /* Setting up Idle */
622 idle = empathy_idle_dup_singleton ();
623 empathy_idle_set_auto_away (idle, TRUE);
624 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
625 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
626 use_nm_notify_cb, idle);
629 empathy_conf_get_bool (empathy_conf_get (),
630 EMPATHY_PREFS_AUTOCONNECT,
632 if (autoconnect && ! no_connect &&
633 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
634 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
635 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
638 create_salut_account ();
641 window = empathy_main_window_show ();
642 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
645 /* We se the callback here because we need window */
646 bacon_message_connection_set_callback (connection,
647 on_bacon_message_received,
651 /* Handle channels */
652 dispatcher = empathy_dispatcher_dup_singleton ();
653 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
656 log_manager = empathy_log_manager_dup_singleton ();
657 empathy_log_manager_observe (log_manager, dispatcher);
659 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
660 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
662 notify_init (_(PACKAGE_NAME));
663 /* Create the call factory */
664 call_factory = empathy_call_factory_initialise ();
665 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
666 G_CALLBACK (new_call_handler_cb), NULL);
667 /* Create the FT factory */
668 ft_factory = empathy_ft_factory_dup_singleton ();
669 g_signal_connect (ft_factory, "new-ft-handler",
670 G_CALLBACK (new_ft_handler_cb), NULL);
671 g_signal_connect (ft_factory, "new-incoming-transfer",
672 G_CALLBACK (new_incoming_transfer_cb), NULL);
674 /* Location mananger */
676 location_manager = empathy_location_manager_dup_singleton ();
681 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
684 g_object_unref (idle);
685 g_object_unref (icon);
686 g_object_unref (log_manager);
687 g_object_unref (dispatcher);
688 g_object_unref (chatroom_manager);
690 g_object_unref (location_manager);
692 g_object_unref (ft_factory);