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 ();
136 empathy_ft_factory_claim_channel (factory, operation);
141 service_ended_cb (MissionControl *mc,
144 DEBUG ("Mission Control stopped");
148 operation_error_cb (MissionControl *mc,
153 const gchar *message;
155 switch (error_code) {
156 case MC_DISCONNECTED_ERROR:
157 message = "Disconnected";
159 case MC_INVALID_HANDLE_ERROR:
160 message = "Invalid handle";
162 case MC_NO_MATCHING_CONNECTION_ERROR:
163 message = "No matching connection";
165 case MC_INVALID_ACCOUNT_ERROR:
166 message = "Invalid account";
168 case MC_PRESENCE_FAILURE_ERROR:
169 message = "Presence failure";
171 case MC_NO_ACCOUNTS_ERROR:
172 message = "No accounts";
174 case MC_NETWORK_ERROR:
175 message = "Network error";
177 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
178 message = "Contact does not support voice";
180 case MC_LOWMEM_ERROR:
183 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
184 message = "Channel request generic error";
186 case MC_CHANNEL_BANNED_ERROR:
187 message = "Channel banned";
189 case MC_CHANNEL_FULL_ERROR:
190 message = "Channel full";
192 case MC_CHANNEL_INVITE_ONLY_ERROR:
193 message = "Channel invite only";
196 message = "Unknown error code";
199 DEBUG ("Error during operation %d: %s", operation_id, message);
203 use_nm_notify_cb (EmpathyConf *conf,
207 EmpathyIdle *idle = user_data;
210 if (empathy_conf_get_bool (conf, key, &use_nm)) {
211 empathy_idle_set_use_nm (idle, use_nm);
216 create_salut_account (void)
219 McProtocol *protocol;
220 gboolean salut_created = FALSE;
225 gchar *nickname = NULL;
226 gchar *first_name = NULL;
227 gchar *last_name = NULL;
230 GError *error = NULL;
232 /* Check if we already created a salut account */
233 empathy_conf_get_bool (empathy_conf_get (),
234 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
240 DEBUG ("Try to add a salut account...");
242 /* Check if the salut CM is installed */
243 profile = mc_profile_lookup ("salut");
245 DEBUG ("No salut profile");
248 protocol = mc_profile_get_protocol (profile);
250 DEBUG ("Salut not installed");
251 g_object_unref (profile);
254 g_object_unref (protocol);
256 /* Get self EContact from EDS */
257 if (!e_book_get_self (&contact, &book, &error)) {
258 DEBUG ("Failed to get self econtact: %s",
259 error ? error->message : "No error given");
260 g_clear_error (&error);
261 g_object_unref (profile);
265 empathy_conf_set_bool (empathy_conf_get (),
266 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
269 /* Check if there is already a salut account */
270 accounts = mc_accounts_list_by_profile (profile);
272 DEBUG ("There is already a salut account");
273 mc_accounts_list_free (accounts);
274 g_object_unref (profile);
278 account = mc_account_create (profile);
279 mc_account_set_display_name (account, _("People nearby"));
281 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
282 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
283 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
284 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
285 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
287 if (!tp_strdiff (nickname, "nickname")) {
292 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
293 "last-name=%s\nemail=%s\njid=%s\n",
294 nickname, first_name, last_name, email, jid);
296 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
297 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
298 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
299 mc_account_set_param_string (account, "email", email ? email : "");
300 mc_account_set_param_string (account, "jid", jid ? jid : "");
307 g_object_unref (account);
308 g_object_unref (profile);
309 g_object_unref (contact);
310 g_object_unref (book);
313 /* The code that handles single-instance and startup notification is
316 * Copyright (C) 2005 - Paolo Maggi
319 on_bacon_message_received (const char *message,
322 GtkWidget *window = data;
323 guint32 startup_timestamp;
325 g_return_if_fail (message != NULL);
327 DEBUG ("Other instance launched, presenting the main window. message='%s'",
330 if (strcmp (message, "accounts") == 0) {
331 /* accounts dialog requested */
332 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
334 startup_timestamp = atoi (message);
336 /* Set the proper interaction time on the window.
337 * Fall back to roundtripping to the X server when we
338 * don't have the timestamp, e.g. when launched from
339 * terminal. We also need to make sure that the window
340 * has been realized otherwise it will not work. lame. */
341 if (startup_timestamp == 0) {
342 /* Work if launched from the terminal */
343 DEBUG ("Using X server timestamp as a fallback");
345 if (!GTK_WIDGET_REALIZED (window)) {
346 gtk_widget_realize (GTK_WIDGET (window));
349 startup_timestamp = gdk_x11_get_server_time (window->window);
352 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
357 get_startup_timestamp ()
359 const gchar *startup_id_env;
360 gchar *startup_id = NULL;
365 /* we don't unset the env, since startup-notification
366 * may still need it */
367 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
368 if (startup_id_env == NULL) {
372 startup_id = g_strdup (startup_id_env);
374 time_str = g_strrstr (startup_id, "_TIME");
375 if (time_str == NULL) {
381 /* Skip past the "_TIME" part */
384 retval = strtoul (time_str, &end, 0);
385 if (end == time_str || errno != 0)
391 return (retval > 0) ? retval : 0;
395 show_version_cb (const char *option_name,
400 g_print ("%s\n", PACKAGE_STRING);
408 new_incoming_transfer_cb (EmpathyFTFactory *factory,
409 EmpathyFTHandler *handler,
412 empathy_receive_file_with_file_chooser (handler);
416 new_ft_handler_cb (EmpathyFTFactory *factory,
417 EmpathyFTHandler *handler,
420 empathy_ft_manager_add_handler (handler);
422 g_object_unref (handler);
426 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
427 gboolean outgoing, gpointer user_data)
429 EmpathyCallWindow *window;
431 window = empathy_call_window_new (handler);
432 gtk_widget_show (GTK_WIDGET (window));
436 main (int argc, char *argv[])
438 guint32 startup_timestamp;
440 EmpathyLocationManager *location_manager = NULL;
442 EmpathyStatusIcon *icon;
443 EmpathyDispatcher *dispatcher;
444 EmpathyLogManager *log_manager;
445 EmpathyChatroomManager *chatroom_manager;
446 EmpathyCallFactory *call_factory;
447 EmpathyFTFactory *ft_factory;
451 gboolean autoconnect = TRUE;
452 gboolean no_connect = FALSE;
453 gboolean hide_contact_list = FALSE;
454 gboolean accounts_dialog = FALSE;
455 GError *error = NULL;
456 GOptionEntry options[] = {
458 0, G_OPTION_ARG_NONE, &no_connect,
459 N_("Don't connect on startup"),
461 { "hide-contact-list", 'h',
462 0, G_OPTION_ARG_NONE, &hide_contact_list,
463 N_("Don't show the contact list on startup"),
466 0, G_OPTION_ARG_NONE, &accounts_dialog,
467 N_("Show the accounts dialog"),
470 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
475 g_thread_init (NULL);
478 if (!gtk_init_with_args (&argc, &argv,
479 N_("- Empathy Instant Messenger"),
480 options, GETTEXT_PACKAGE, &error)) {
481 g_warning ("Error in empathy init: %s", error->message);
486 g_set_application_name (_(PACKAGE_NAME));
487 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
489 gst_init (&argc, &argv);
491 #if HAVE_LIBCHAMPLAIN
492 gtk_clutter_init (&argc, &argv);
495 gtk_window_set_default_icon_name ("empathy");
496 textdomain (GETTEXT_PACKAGE);
498 /* Setting up the bacon connection */
499 startup_timestamp = get_startup_timestamp ();
500 connection = bacon_message_connection_new ("empathy");
501 if (connection != NULL) {
502 if (!bacon_message_connection_get_is_server (connection)) {
505 if (accounts_dialog) {
506 DEBUG ("Showing accounts dialog from existing Empathy instance");
508 message = g_strdup ("accounts");
512 DEBUG ("Activating existing instance");
514 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
518 bacon_message_connection_send (connection, message);
520 /* We never popup a window, so tell startup-notification
521 * that we are done. */
522 gdk_notify_startup_complete ();
525 bacon_message_connection_free (connection);
530 g_warning ("Cannot create the 'empathy' bacon connection.");
534 mc = empathy_mission_control_dup_singleton ();
535 g_signal_connect (mc, "ServiceEnded",
536 G_CALLBACK (service_ended_cb),
538 g_signal_connect (mc, "Error",
539 G_CALLBACK (operation_error_cb),
542 if (accounts_dialog) {
545 dialog = empathy_accounts_dialog_show (NULL, NULL);
546 g_signal_connect (dialog, "destroy",
547 G_CALLBACK (gtk_main_quit),
554 /* Setting up Idle */
555 idle = empathy_idle_dup_singleton ();
556 empathy_idle_set_auto_away (idle, TRUE);
557 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
558 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
559 use_nm_notify_cb, idle);
562 empathy_conf_get_bool (empathy_conf_get (),
563 EMPATHY_PREFS_AUTOCONNECT,
565 if (autoconnect && ! no_connect &&
566 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
567 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
568 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
571 create_salut_account ();
574 window = empathy_main_window_show ();
575 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
578 /* We se the callback here because we need window */
579 bacon_message_connection_set_callback (connection,
580 on_bacon_message_received,
584 /* Handle channels */
585 dispatcher = empathy_dispatcher_dup_singleton ();
586 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
589 log_manager = empathy_log_manager_dup_singleton ();
590 empathy_log_manager_observe (log_manager, dispatcher);
592 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
593 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
595 notify_init (_(PACKAGE_NAME));
596 /* Create the call factory */
597 call_factory = empathy_call_factory_initialise ();
598 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
599 G_CALLBACK (new_call_handler_cb), NULL);
600 /* Create the FT factory */
601 ft_factory = empathy_ft_factory_dup_singleton ();
602 g_signal_connect (ft_factory, "new-ft-handler",
603 G_CALLBACK (new_ft_handler_cb), NULL);
604 g_signal_connect (ft_factory, "new-incoming-transfer",
605 G_CALLBACK (new_incoming_transfer_cb), NULL);
607 /* Location mananger */
609 location_manager = empathy_location_manager_dup_singleton ();
614 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
617 g_object_unref (idle);
618 g_object_unref (icon);
619 g_object_unref (log_manager);
620 g_object_unref (dispatcher);
621 g_object_unref (chatroom_manager);
622 g_object_unref (ft_manager);
624 g_object_unref (location_manager);
626 g_object_unref (ft_factory);