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-tp-chat.h>
54 #include <libempathy/empathy-tp-call.h>
56 #include <libempathy-gtk/empathy-conf.h>
57 #include <libempathy-gtk/empathy-ui-utils.h>
58 #include <libempathy-gtk/empathy-location-manager.h>
60 #include "empathy-accounts-dialog.h"
61 #include "empathy-main-window.h"
62 #include "empathy-status-icon.h"
63 #include "empathy-call-window.h"
64 #include "empathy-chat-window.h"
65 #include "empathy-ft-manager.h"
66 #include "bacon-message-connection.h"
68 #include "extensions/extensions.h"
70 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
71 #include <libempathy/empathy-debug.h>
75 static BaconMessageConnection *connection = NULL;
78 dispatch_cb (EmpathyDispatcher *dispatcher,
79 EmpathyDispatchOperation *operation,
84 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
86 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
87 EmpathyTpChat *tp_chat;
88 EmpathyChat *chat = NULL;
91 tp_chat = EMPATHY_TP_CHAT (
92 empathy_dispatch_operation_get_channel_wrapper (operation));
94 id = empathy_tp_chat_get_id (tp_chat);
96 EmpathyContact *contact;
98 contact = empathy_tp_chat_get_remote_contact (tp_chat);
100 id = empathy_contact_get_id (contact);
105 EmpathyAccountManager *manager;
106 TpConnection *connection;
109 manager = empathy_account_manager_dup_singleton ();
110 connection = empathy_tp_chat_get_connection (tp_chat);
111 account = empathy_account_manager_get_account (manager,
113 chat = empathy_chat_window_find_chat (account, id);
114 g_object_unref (manager);
118 empathy_chat_set_tp_chat (chat, tp_chat);
120 chat = empathy_chat_new (tp_chat);
123 empathy_chat_window_present_chat (chat);
125 empathy_dispatch_operation_claim (operation);
126 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
127 EmpathyCallFactory *factory;
129 factory = empathy_call_factory_get ();
130 empathy_call_factory_claim_channel (factory, operation);
131 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
132 EmpathyFTManager *ft_manager;
133 EmpathyTpFile *tp_file;
135 ft_manager = empathy_ft_manager_dup_singleton ();
136 tp_file = EMPATHY_TP_FILE (
137 empathy_dispatch_operation_get_channel_wrapper (operation));
138 empathy_ft_manager_add_tp_file (ft_manager, tp_file);
139 empathy_dispatch_operation_claim (operation);
140 g_object_unref (ft_manager);
145 service_ended_cb (MissionControl *mc,
148 DEBUG ("Mission Control stopped");
152 operation_error_cb (MissionControl *mc,
157 const gchar *message;
159 switch (error_code) {
160 case MC_DISCONNECTED_ERROR:
161 message = "Disconnected";
163 case MC_INVALID_HANDLE_ERROR:
164 message = "Invalid handle";
166 case MC_NO_MATCHING_CONNECTION_ERROR:
167 message = "No matching connection";
169 case MC_INVALID_ACCOUNT_ERROR:
170 message = "Invalid account";
172 case MC_PRESENCE_FAILURE_ERROR:
173 message = "Presence failure";
175 case MC_NO_ACCOUNTS_ERROR:
176 message = "No accounts";
178 case MC_NETWORK_ERROR:
179 message = "Network error";
181 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
182 message = "Contact does not support voice";
184 case MC_LOWMEM_ERROR:
187 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
188 message = "Channel request generic error";
190 case MC_CHANNEL_BANNED_ERROR:
191 message = "Channel banned";
193 case MC_CHANNEL_FULL_ERROR:
194 message = "Channel full";
196 case MC_CHANNEL_INVITE_ONLY_ERROR:
197 message = "Channel invite only";
200 message = "Unknown error code";
203 DEBUG ("Error during operation %d: %s", operation_id, message);
207 use_nm_notify_cb (EmpathyConf *conf,
211 EmpathyIdle *idle = user_data;
214 if (empathy_conf_get_bool (conf, key, &use_nm)) {
215 empathy_idle_set_use_nm (idle, use_nm);
220 create_salut_account (void)
223 McProtocol *protocol;
224 gboolean salut_created = FALSE;
229 gchar *nickname = NULL;
230 gchar *first_name = NULL;
231 gchar *last_name = NULL;
234 GError *error = NULL;
236 /* Check if we already created a salut account */
237 empathy_conf_get_bool (empathy_conf_get (),
238 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
244 DEBUG ("Try to add a salut account...");
246 /* Check if the salut CM is installed */
247 profile = mc_profile_lookup ("salut");
249 DEBUG ("No salut profile");
252 protocol = mc_profile_get_protocol (profile);
254 DEBUG ("Salut not installed");
255 g_object_unref (profile);
258 g_object_unref (protocol);
260 /* Get self EContact from EDS */
261 if (!e_book_get_self (&contact, &book, &error)) {
262 DEBUG ("Failed to get self econtact: %s",
263 error ? error->message : "No error given");
264 g_clear_error (&error);
265 g_object_unref (profile);
269 empathy_conf_set_bool (empathy_conf_get (),
270 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
273 /* Check if there is already a salut account */
274 accounts = mc_accounts_list_by_profile (profile);
276 DEBUG ("There is already a salut account");
277 mc_accounts_list_free (accounts);
278 g_object_unref (profile);
282 account = mc_account_create (profile);
283 mc_account_set_display_name (account, _("People nearby"));
285 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
286 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
287 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
288 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
289 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
291 if (!tp_strdiff (nickname, "nickname")) {
296 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
297 "last-name=%s\nemail=%s\njid=%s\n",
298 nickname, first_name, last_name, email, jid);
300 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
301 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
302 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
303 mc_account_set_param_string (account, "email", email ? email : "");
304 mc_account_set_param_string (account, "jid", jid ? jid : "");
311 g_object_unref (account);
312 g_object_unref (profile);
313 g_object_unref (contact);
314 g_object_unref (book);
317 /* The code that handles single-instance and startup notification is
320 * Copyright (C) 2005 - Paolo Maggi
323 on_bacon_message_received (const char *message,
326 GtkWidget *window = data;
327 guint32 startup_timestamp;
329 g_return_if_fail (message != NULL);
331 DEBUG ("Other instance launched, presenting the main window. message='%s'",
334 if (strcmp (message, "accounts") == 0) {
335 /* accounts dialog requested */
336 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
338 startup_timestamp = atoi (message);
340 /* Set the proper interaction time on the window.
341 * Fall back to roundtripping to the X server when we
342 * don't have the timestamp, e.g. when launched from
343 * terminal. We also need to make sure that the window
344 * has been realized otherwise it will not work. lame. */
345 if (startup_timestamp == 0) {
346 /* Work if launched from the terminal */
347 DEBUG ("Using X server timestamp as a fallback");
349 if (!GTK_WIDGET_REALIZED (window)) {
350 gtk_widget_realize (GTK_WIDGET (window));
353 startup_timestamp = gdk_x11_get_server_time (window->window);
356 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
361 get_startup_timestamp ()
363 const gchar *startup_id_env;
364 gchar *startup_id = NULL;
369 /* we don't unset the env, since startup-notification
370 * may still need it */
371 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
372 if (startup_id_env == NULL) {
376 startup_id = g_strdup (startup_id_env);
378 time_str = g_strrstr (startup_id, "_TIME");
379 if (time_str == NULL) {
385 /* Skip past the "_TIME" part */
388 retval = strtoul (time_str, &end, 0);
389 if (end == time_str || errno != 0)
395 return (retval > 0) ? retval : 0;
399 show_version_cb (const char *option_name,
404 g_print ("%s\n", PACKAGE_STRING);
412 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
413 gboolean outgoing, gpointer user_data)
415 EmpathyCallWindow *window;
417 window = empathy_call_window_new (handler);
418 gtk_widget_show (GTK_WIDGET (window));
422 main (int argc, char *argv[])
424 guint32 startup_timestamp;
426 EmpathyLocationManager *location_manager = NULL;
428 EmpathyStatusIcon *icon;
429 EmpathyDispatcher *dispatcher;
430 EmpathyLogManager *log_manager;
431 EmpathyChatroomManager *chatroom_manager;
432 EmpathyFTManager *ft_manager;
433 EmpathyCallFactory *call_factory;
437 gboolean autoconnect = TRUE;
438 gboolean no_connect = FALSE;
439 gboolean hide_contact_list = FALSE;
440 gboolean accounts_dialog = FALSE;
441 GError *error = NULL;
442 GOptionEntry options[] = {
444 0, G_OPTION_ARG_NONE, &no_connect,
445 N_("Don't connect on startup"),
447 { "hide-contact-list", 'h',
448 0, G_OPTION_ARG_NONE, &hide_contact_list,
449 N_("Don't show the contact list on startup"),
452 0, G_OPTION_ARG_NONE, &accounts_dialog,
453 N_("Show the accounts dialog"),
456 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
461 g_thread_init (NULL);
464 if (!gtk_init_with_args (&argc, &argv,
465 N_("- Empathy Instant Messenger"),
466 options, GETTEXT_PACKAGE, &error)) {
467 g_warning ("Error in empathy init: %s", error->message);
472 g_set_application_name (_(PACKAGE_NAME));
473 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
475 gst_init (&argc, &argv);
477 #if HAVE_LIBCHAMPLAIN
478 gtk_clutter_init (&argc, &argv);
481 gtk_window_set_default_icon_name ("empathy");
482 textdomain (GETTEXT_PACKAGE);
484 /* Setting up the bacon connection */
485 startup_timestamp = get_startup_timestamp ();
486 connection = bacon_message_connection_new ("empathy");
487 if (connection != NULL) {
488 if (!bacon_message_connection_get_is_server (connection)) {
491 if (accounts_dialog) {
492 DEBUG ("Showing accounts dialog from existing Empathy instance");
494 message = g_strdup ("accounts");
498 DEBUG ("Activating existing instance");
500 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
504 bacon_message_connection_send (connection, message);
506 /* We never popup a window, so tell startup-notification
507 * that we are done. */
508 gdk_notify_startup_complete ();
511 bacon_message_connection_free (connection);
516 g_warning ("Cannot create the 'empathy' bacon connection.");
520 mc = empathy_mission_control_dup_singleton ();
521 g_signal_connect (mc, "ServiceEnded",
522 G_CALLBACK (service_ended_cb),
524 g_signal_connect (mc, "Error",
525 G_CALLBACK (operation_error_cb),
528 if (accounts_dialog) {
531 dialog = empathy_accounts_dialog_show (NULL, NULL);
532 g_signal_connect (dialog, "destroy",
533 G_CALLBACK (gtk_main_quit),
540 /* Setting up Idle */
541 idle = empathy_idle_dup_singleton ();
542 empathy_idle_set_auto_away (idle, TRUE);
543 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
544 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
545 use_nm_notify_cb, idle);
548 empathy_conf_get_bool (empathy_conf_get (),
549 EMPATHY_PREFS_AUTOCONNECT,
551 if (autoconnect && ! no_connect &&
552 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
553 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
554 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
557 create_salut_account ();
560 window = empathy_main_window_show ();
561 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
564 /* We se the callback here because we need window */
565 bacon_message_connection_set_callback (connection,
566 on_bacon_message_received,
570 /* Handle channels */
571 dispatcher = empathy_dispatcher_dup_singleton ();
572 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
575 log_manager = empathy_log_manager_dup_singleton ();
576 empathy_log_manager_observe (log_manager, dispatcher);
578 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
579 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
581 ft_manager = empathy_ft_manager_dup_singleton ();
583 notify_init (_(PACKAGE_NAME));
584 /* Create the call factory */
585 call_factory = empathy_call_factory_initialise ();
586 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
587 G_CALLBACK (new_call_handler_cb), NULL);
589 /* Location mananger */
591 location_manager = empathy_location_manager_get_default ();
596 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
599 g_object_unref (idle);
600 g_object_unref (icon);
601 g_object_unref (log_manager);
602 g_object_unref (dispatcher);
603 g_object_unref (chatroom_manager);
604 g_object_unref (ft_manager);
606 g_object_unref (location_manager);