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/util.h>
42 #include <libmissioncontrol/mc-account.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-dispatcher.h>
51 #include <libempathy/empathy-dispatch-operation.h>
52 #include <libempathy/empathy-log-manager.h>
53 #include <libempathy/empathy-ft-factory.h>
54 #include <libempathy/empathy-tp-chat.h>
55 #include <libempathy/empathy-tp-call.h>
57 #include <libempathy-gtk/empathy-conf.h>
58 #include <libempathy-gtk/empathy-ui-utils.h>
59 #include <libempathy-gtk/empathy-location-manager.h>
61 #include "empathy-accounts-dialog.h"
62 #include "empathy-main-window.h"
63 #include "empathy-status-icon.h"
64 #include "empathy-call-window.h"
65 #include "empathy-chat-window.h"
66 #include "empathy-ft-manager.h"
67 #include "bacon-message-connection.h"
69 #include "extensions/extensions.h"
71 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
72 #include <libempathy/empathy-debug.h>
76 static BaconMessageConnection *connection = NULL;
79 dispatch_cb (EmpathyDispatcher *dispatcher,
80 EmpathyDispatchOperation *operation,
85 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
87 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
88 EmpathyTpChat *tp_chat;
89 EmpathyChat *chat = NULL;
92 tp_chat = EMPATHY_TP_CHAT (
93 empathy_dispatch_operation_get_channel_wrapper (operation));
95 id = empathy_tp_chat_get_id (tp_chat);
97 EmpathyContact *contact;
99 contact = empathy_tp_chat_get_remote_contact (tp_chat);
101 id = empathy_contact_get_id (contact);
106 EmpathyAccountManager *manager;
107 TpConnection *connection;
110 manager = empathy_account_manager_dup_singleton ();
111 connection = empathy_tp_chat_get_connection (tp_chat);
112 account = empathy_account_manager_get_account (manager,
114 chat = empathy_chat_window_find_chat (account, id);
115 g_object_unref (manager);
119 empathy_chat_set_tp_chat (chat, tp_chat);
121 chat = empathy_chat_new (tp_chat);
124 empathy_chat_window_present_chat (chat);
126 empathy_dispatch_operation_claim (operation);
127 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
128 EmpathyCallFactory *factory;
130 factory = empathy_call_factory_get ();
131 empathy_call_factory_claim_channel (factory, operation);
132 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
133 EmpathyFTFactory *factory;
135 factory = empathy_ft_factory_dup_singleton ();
137 /* if the operation is not incoming, don't claim it,
138 * as it might have been triggered by another client, and
139 * we are observing it.
141 if (empathy_dispatch_operation_is_incoming (operation)) {
142 empathy_ft_factory_claim_channel (factory, operation);
148 service_ended_cb (MissionControl *mc,
151 DEBUG ("Mission Control stopped");
155 operation_error_cb (MissionControl *mc,
160 const gchar *message;
162 switch (error_code) {
163 case MC_DISCONNECTED_ERROR:
164 message = "Disconnected";
166 case MC_INVALID_HANDLE_ERROR:
167 message = "Invalid handle";
169 case MC_NO_MATCHING_CONNECTION_ERROR:
170 message = "No matching connection";
172 case MC_INVALID_ACCOUNT_ERROR:
173 message = "Invalid account";
175 case MC_PRESENCE_FAILURE_ERROR:
176 message = "Presence failure";
178 case MC_NO_ACCOUNTS_ERROR:
179 message = "No accounts";
181 case MC_NETWORK_ERROR:
182 message = "Network error";
184 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
185 message = "Contact does not support voice";
187 case MC_LOWMEM_ERROR:
190 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
191 message = "Channel request generic error";
193 case MC_CHANNEL_BANNED_ERROR:
194 message = "Channel banned";
196 case MC_CHANNEL_FULL_ERROR:
197 message = "Channel full";
199 case MC_CHANNEL_INVITE_ONLY_ERROR:
200 message = "Channel invite only";
203 message = "Unknown error code";
206 DEBUG ("Error during operation %d: %s", operation_id, message);
210 use_nm_notify_cb (EmpathyConf *conf,
214 EmpathyIdle *idle = user_data;
217 if (empathy_conf_get_bool (conf, key, &use_nm)) {
218 empathy_idle_set_use_nm (idle, use_nm);
223 create_salut_account (void)
226 McProtocol *protocol;
227 gboolean salut_created = FALSE;
232 gchar *nickname = NULL;
233 gchar *first_name = NULL;
234 gchar *last_name = NULL;
237 GError *error = NULL;
239 /* Check if we already created a salut account */
240 empathy_conf_get_bool (empathy_conf_get (),
241 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
247 DEBUG ("Try to add a salut account...");
249 /* Check if the salut CM is installed */
250 profile = mc_profile_lookup ("salut");
252 DEBUG ("No salut profile");
255 protocol = mc_profile_get_protocol (profile);
257 DEBUG ("Salut not installed");
258 g_object_unref (profile);
261 g_object_unref (protocol);
263 /* Get self EContact from EDS */
264 if (!e_book_get_self (&contact, &book, &error)) {
265 DEBUG ("Failed to get self econtact: %s",
266 error ? error->message : "No error given");
267 g_clear_error (&error);
268 g_object_unref (profile);
272 empathy_conf_set_bool (empathy_conf_get (),
273 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
276 /* Check if there is already a salut account */
277 accounts = mc_accounts_list_by_profile (profile);
279 DEBUG ("There is already a salut account");
280 mc_accounts_list_free (accounts);
281 g_object_unref (profile);
285 account = mc_account_create (profile);
286 mc_account_set_display_name (account, _("People nearby"));
288 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
289 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
290 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
291 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
292 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
294 if (!tp_strdiff (nickname, "nickname")) {
299 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
300 "last-name=%s\nemail=%s\njid=%s\n",
301 nickname, first_name, last_name, email, jid);
303 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
304 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
305 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
306 mc_account_set_param_string (account, "email", email ? email : "");
307 mc_account_set_param_string (account, "jid", jid ? jid : "");
314 g_object_unref (account);
315 g_object_unref (profile);
316 g_object_unref (contact);
317 g_object_unref (book);
320 /* The code that handles single-instance and startup notification is
323 * Copyright (C) 2005 - Paolo Maggi
326 on_bacon_message_received (const char *message,
329 GtkWidget *window = data;
330 guint32 startup_timestamp;
332 g_return_if_fail (message != NULL);
334 DEBUG ("Other instance launched, presenting the main window. message='%s'",
337 if (strcmp (message, "accounts") == 0) {
338 /* accounts dialog requested */
339 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
341 startup_timestamp = atoi (message);
343 /* Set the proper interaction time on the window.
344 * Fall back to roundtripping to the X server when we
345 * don't have the timestamp, e.g. when launched from
346 * terminal. We also need to make sure that the window
347 * has been realized otherwise it will not work. lame. */
348 if (startup_timestamp == 0) {
349 /* Work if launched from the terminal */
350 DEBUG ("Using X server timestamp as a fallback");
352 if (!GTK_WIDGET_REALIZED (window)) {
353 gtk_widget_realize (GTK_WIDGET (window));
356 startup_timestamp = gdk_x11_get_server_time (window->window);
359 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
364 get_startup_timestamp ()
366 const gchar *startup_id_env;
367 gchar *startup_id = NULL;
372 /* we don't unset the env, since startup-notification
373 * may still need it */
374 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
375 if (startup_id_env == NULL) {
379 startup_id = g_strdup (startup_id_env);
381 time_str = g_strrstr (startup_id, "_TIME");
382 if (time_str == NULL) {
388 /* Skip past the "_TIME" part */
391 retval = strtoul (time_str, &end, 0);
392 if (end == time_str || errno != 0)
398 return (retval > 0) ? retval : 0;
402 show_version_cb (const char *option_name,
407 g_print ("%s\n", PACKAGE_STRING);
415 new_incoming_transfer_cb (EmpathyFTFactory *factory,
416 EmpathyFTHandler *handler,
421 empathy_ft_manager_display_error (handler, error);
423 empathy_receive_file_with_file_chooser (handler);
428 new_ft_handler_cb (EmpathyFTFactory *factory,
429 EmpathyFTHandler *handler,
434 empathy_ft_manager_display_error (handler, error);
436 empathy_ft_manager_add_handler (handler);
439 g_object_unref (handler);
443 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
444 gboolean outgoing, gpointer user_data)
446 EmpathyCallWindow *window;
448 window = empathy_call_window_new (handler);
449 gtk_widget_show (GTK_WIDGET (window));
453 main (int argc, char *argv[])
455 guint32 startup_timestamp;
457 EmpathyLocationManager *location_manager = NULL;
459 EmpathyStatusIcon *icon;
460 EmpathyDispatcher *dispatcher;
461 EmpathyLogManager *log_manager;
462 EmpathyChatroomManager *chatroom_manager;
463 EmpathyCallFactory *call_factory;
464 EmpathyFTFactory *ft_factory;
468 gboolean autoconnect = TRUE;
469 gboolean no_connect = FALSE;
470 gboolean hide_contact_list = FALSE;
471 gboolean accounts_dialog = FALSE;
472 GError *error = NULL;
473 GOptionEntry options[] = {
475 0, G_OPTION_ARG_NONE, &no_connect,
476 N_("Don't connect on startup"),
478 { "hide-contact-list", 'h',
479 0, G_OPTION_ARG_NONE, &hide_contact_list,
480 N_("Don't show the contact list on startup"),
483 0, G_OPTION_ARG_NONE, &accounts_dialog,
484 N_("Show the accounts dialog"),
487 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
492 g_thread_init (NULL);
495 if (!gtk_init_with_args (&argc, &argv,
496 N_("- Empathy Instant Messenger"),
497 options, GETTEXT_PACKAGE, &error)) {
498 g_warning ("Error in empathy init: %s", error->message);
503 g_set_application_name (_(PACKAGE_NAME));
504 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
506 gst_init (&argc, &argv);
508 #if HAVE_LIBCHAMPLAIN
509 gtk_clutter_init (&argc, &argv);
512 gtk_window_set_default_icon_name ("empathy");
513 textdomain (GETTEXT_PACKAGE);
515 /* Setting up the bacon connection */
516 startup_timestamp = get_startup_timestamp ();
517 connection = bacon_message_connection_new ("empathy");
518 if (connection != NULL) {
519 if (!bacon_message_connection_get_is_server (connection)) {
522 if (accounts_dialog) {
523 DEBUG ("Showing accounts dialog from existing Empathy instance");
525 message = g_strdup ("accounts");
529 DEBUG ("Activating existing instance");
531 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
535 bacon_message_connection_send (connection, message);
537 /* We never popup a window, so tell startup-notification
538 * that we are done. */
539 gdk_notify_startup_complete ();
542 bacon_message_connection_free (connection);
547 g_warning ("Cannot create the 'empathy' bacon connection.");
551 mc = empathy_mission_control_dup_singleton ();
552 g_signal_connect (mc, "ServiceEnded",
553 G_CALLBACK (service_ended_cb),
555 g_signal_connect (mc, "Error",
556 G_CALLBACK (operation_error_cb),
559 if (accounts_dialog) {
562 dialog = empathy_accounts_dialog_show (NULL, NULL);
563 g_signal_connect (dialog, "destroy",
564 G_CALLBACK (gtk_main_quit),
571 /* Setting up Idle */
572 idle = empathy_idle_dup_singleton ();
573 empathy_idle_set_auto_away (idle, TRUE);
574 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
575 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
576 use_nm_notify_cb, idle);
579 empathy_conf_get_bool (empathy_conf_get (),
580 EMPATHY_PREFS_AUTOCONNECT,
582 if (autoconnect && ! no_connect &&
583 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
584 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
585 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
588 create_salut_account ();
591 window = empathy_main_window_show ();
592 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
595 /* We se the callback here because we need window */
596 bacon_message_connection_set_callback (connection,
597 on_bacon_message_received,
601 /* Handle channels */
602 dispatcher = empathy_dispatcher_dup_singleton ();
603 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
606 log_manager = empathy_log_manager_dup_singleton ();
607 empathy_log_manager_observe (log_manager, dispatcher);
609 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
610 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
612 notify_init (_(PACKAGE_NAME));
613 /* Create the call factory */
614 call_factory = empathy_call_factory_initialise ();
615 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
616 G_CALLBACK (new_call_handler_cb), NULL);
617 /* Create the FT factory */
618 ft_factory = empathy_ft_factory_dup_singleton ();
619 g_signal_connect (ft_factory, "new-ft-handler",
620 G_CALLBACK (new_ft_handler_cb), NULL);
621 g_signal_connect (ft_factory, "new-incoming-transfer",
622 G_CALLBACK (new_incoming_transfer_cb), NULL);
624 /* Location mananger */
626 location_manager = empathy_location_manager_dup_singleton ();
631 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
634 g_object_unref (idle);
635 g_object_unref (icon);
636 g_object_unref (log_manager);
637 g_object_unref (dispatcher);
638 g_object_unref (chatroom_manager);
640 g_object_unref (location_manager);
642 g_object_unref (ft_factory);