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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
30 #include <glib/gi18n.h>
34 #include <libebook/e-book.h>
35 #include <libnotify/notify.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mc-account.h>
39 #include <libmissioncontrol/mission-control.h>
41 #include <libempathy/empathy-idle.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-call-factory.h>
44 #include <libempathy/empathy-chatroom-manager.h>
45 #include <libempathy/empathy-dispatcher.h>
46 #include <libempathy/empathy-dispatch-operation.h>
47 #include <libempathy/empathy-log-manager.h>
48 #include <libempathy/empathy-tp-chat.h>
49 #include <libempathy/empathy-tp-call.h>
51 #include <libempathy-gtk/empathy-conf.h>
52 #include <libempathy-gtk/empathy-ui-utils.h>
54 #include "empathy-accounts-dialog.h"
55 #include "empathy-main-window.h"
56 #include "empathy-status-icon.h"
57 #include "empathy-call-window.h"
58 #include "empathy-chat-window.h"
59 #include "empathy-ft-manager.h"
60 #include "bacon-message-connection.h"
62 #include "extensions/extensions.h"
64 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
65 #include <libempathy/empathy-debug.h>
69 static BaconMessageConnection *connection = NULL;
72 dispatch_cb (EmpathyDispatcher *dispatcher,
73 EmpathyDispatchOperation *operation,
78 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
80 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
81 EmpathyTpChat *tp_chat;
82 EmpathyChat *chat = NULL;
85 tp_chat = EMPATHY_TP_CHAT (
86 empathy_dispatch_operation_get_channel_wrapper (operation));
88 id = empathy_tp_chat_get_id (tp_chat);
90 EmpathyContact *contact;
92 contact = empathy_tp_chat_get_remote_contact (tp_chat);
94 id = empathy_contact_get_id (contact);
101 account = empathy_tp_chat_get_account (tp_chat);
102 chat = empathy_chat_window_find_chat (account, id);
106 empathy_chat_set_tp_chat (chat, tp_chat);
108 chat = empathy_chat_new (tp_chat);
111 empathy_chat_window_present_chat (chat);
113 empathy_dispatch_operation_claim (operation);
114 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
115 EmpathyCallFactory *factory;
117 factory = empathy_call_factory_get ();
118 empathy_call_factory_claim_channel (factory, operation);
119 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
120 EmpathyFTManager *ft_manager;
121 EmpathyTpFile *tp_file;
123 ft_manager = empathy_ft_manager_dup_singleton ();
124 tp_file = EMPATHY_TP_FILE (
125 empathy_dispatch_operation_get_channel_wrapper (operation));
126 empathy_ft_manager_add_tp_file (ft_manager, tp_file);
127 empathy_dispatch_operation_claim (operation);
128 g_object_unref (ft_manager);
133 service_ended_cb (MissionControl *mc,
136 DEBUG ("Mission Control stopped");
140 operation_error_cb (MissionControl *mc,
145 const gchar *message;
147 switch (error_code) {
148 case MC_DISCONNECTED_ERROR:
149 message = "Disconnected";
151 case MC_INVALID_HANDLE_ERROR:
152 message = "Invalid handle";
154 case MC_NO_MATCHING_CONNECTION_ERROR:
155 message = "No matching connection";
157 case MC_INVALID_ACCOUNT_ERROR:
158 message = "Invalid account";
160 case MC_PRESENCE_FAILURE_ERROR:
161 message = "Presence failure";
163 case MC_NO_ACCOUNTS_ERROR:
164 message = "No accounts";
166 case MC_NETWORK_ERROR:
167 message = "Network error";
169 case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
170 message = "Contact does not support voice";
172 case MC_LOWMEM_ERROR:
175 case MC_CHANNEL_REQUEST_GENERIC_ERROR:
176 message = "Channel request generic error";
178 case MC_CHANNEL_BANNED_ERROR:
179 message = "Channel banned";
181 case MC_CHANNEL_FULL_ERROR:
182 message = "Channel full";
184 case MC_CHANNEL_INVITE_ONLY_ERROR:
185 message = "Channel invite only";
188 message = "Unknown error code";
191 DEBUG ("Error during operation %d: %s", operation_id, message);
195 use_nm_notify_cb (EmpathyConf *conf,
199 EmpathyIdle *idle = user_data;
202 if (empathy_conf_get_bool (conf, key, &use_nm)) {
203 empathy_idle_set_use_nm (idle, use_nm);
208 create_salut_account (void)
211 McProtocol *protocol;
212 gboolean salut_created = FALSE;
217 gchar *nickname = NULL;
218 gchar *first_name = NULL;
219 gchar *last_name = NULL;
222 GError *error = NULL;
224 /* Check if we already created a salut account */
225 empathy_conf_get_bool (empathy_conf_get(),
226 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
232 DEBUG ("Try to add a salut account...");
234 /* Check if the salut CM is installed */
235 profile = mc_profile_lookup ("salut");
237 DEBUG ("No salut profile");
240 protocol = mc_profile_get_protocol (profile);
242 DEBUG ("Salut not installed");
243 g_object_unref (profile);
246 g_object_unref (protocol);
248 /* Get self EContact from EDS */
249 if (!e_book_get_self (&contact, &book, &error)) {
250 DEBUG ("Failed to get self econtact: %s",
251 error ? error->message : "No error given");
252 g_clear_error (&error);
253 g_object_unref (profile);
257 empathy_conf_set_bool (empathy_conf_get (),
258 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
261 /* Check if there is already a salut account */
262 accounts = mc_accounts_list_by_profile (profile);
264 DEBUG ("There is already a salut account");
265 mc_accounts_list_free (accounts);
266 g_object_unref (profile);
270 account = mc_account_create (profile);
271 mc_account_set_display_name (account, _("People nearby"));
273 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
274 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
275 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
276 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
277 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
279 if (!tp_strdiff (nickname, "nickname")) {
284 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
285 "last-name=%s\nemail=%s\njid=%s\n",
286 nickname, first_name, last_name, email, jid);
288 mc_account_set_param_string (account, "nickname", nickname ? nickname : "");
289 mc_account_set_param_string (account, "first-name", first_name ? first_name : "");
290 mc_account_set_param_string (account, "last-name", last_name ? last_name : "");
291 mc_account_set_param_string (account, "email", email ? email : "");
292 mc_account_set_param_string (account, "jid", jid ? jid : "");
299 g_object_unref (account);
300 g_object_unref (profile);
301 g_object_unref (contact);
302 g_object_unref (book);
305 /* The code that handles single-instance and startup notification is
308 * Copyright (C) 2005 - Paolo Maggi
311 on_bacon_message_received (const char *message,
314 GtkWidget *window = data;
315 guint32 startup_timestamp;
317 g_return_if_fail (message != NULL);
319 DEBUG ("Other instance launched, presenting the main window. message='%s'",
322 if (strcmp (message, "accounts") == 0) {
323 /* accounts dialog requested */
324 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
326 startup_timestamp = atoi (message);
328 /* Set the proper interaction time on the window.
329 * Fall back to roundtripping to the X server when we
330 * don't have the timestamp, e.g. when launched from
331 * terminal. We also need to make sure that the window
332 * has been realized otherwise it will not work. lame. */
333 if (startup_timestamp == 0) {
334 /* Work if launched from the terminal */
335 DEBUG ("Using X server timestamp as a fallback");
337 if (!GTK_WIDGET_REALIZED (window)) {
338 gtk_widget_realize (GTK_WIDGET (window));
341 startup_timestamp = gdk_x11_get_server_time (window->window);
344 gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
349 get_startup_timestamp ()
351 const gchar *startup_id_env;
352 gchar *startup_id = NULL;
357 /* we don't unset the env, since startup-notification
358 * may still need it */
359 startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
360 if (startup_id_env == NULL) {
364 startup_id = g_strdup (startup_id_env);
366 time_str = g_strrstr (startup_id, "_TIME");
367 if (time_str == NULL) {
373 /* Skip past the "_TIME" part */
376 retval = strtoul (time_str, &end, 0);
377 if (end == time_str || errno != 0)
383 return (retval > 0) ? retval : 0;
387 show_version_cb (const char *option_name,
392 g_print ("%s\n", PACKAGE_STRING);
400 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
401 gboolean outgoing, gpointer user_data)
403 EmpathyCallWindow *window;
405 window = empathy_call_window_new (handler);
406 gtk_widget_show (GTK_WIDGET (window));
410 main (int argc, char *argv[])
412 guint32 startup_timestamp;
413 EmpathyStatusIcon *icon;
414 EmpathyDispatcher *dispatcher;
415 EmpathyLogManager *log_manager;
416 EmpathyChatroomManager *chatroom_manager;
417 EmpathyFTManager *ft_manager;
418 EmpathyCallFactory *call_factory;
422 gboolean autoconnect = TRUE;
423 gboolean no_connect = FALSE;
424 gboolean hide_contact_list = FALSE;
425 gboolean accounts_dialog = FALSE;
426 GError *error = NULL;
427 GOptionEntry options[] = {
429 0, G_OPTION_ARG_NONE, &no_connect,
430 N_("Don't connect on startup"),
432 { "hide-contact-list", 'h',
433 0, G_OPTION_ARG_NONE, &hide_contact_list,
434 N_("Don't show the contact list on startup"),
437 0, G_OPTION_ARG_NONE, &accounts_dialog,
438 N_("Show the accounts dialog"),
441 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
446 g_thread_init (NULL);
449 if (!gtk_init_with_args (&argc, &argv,
450 N_("- Empathy Instant Messenger"),
451 options, GETTEXT_PACKAGE, &error)) {
452 g_warning ("Error in empathy init: %s", error->message);
457 g_set_application_name (_(PACKAGE_NAME));
459 gst_init (&argc, &argv);
461 gtk_window_set_default_icon_name ("empathy");
462 textdomain (GETTEXT_PACKAGE);
464 /* Setting up the bacon connection */
465 startup_timestamp = get_startup_timestamp ();
466 connection = bacon_message_connection_new ("empathy");
467 if (connection != NULL) {
468 if (!bacon_message_connection_get_is_server (connection)) {
471 if (accounts_dialog) {
472 DEBUG ("Showing accounts dialog from existing Empathy instance");
474 message = g_strdup ("accounts");
478 DEBUG ("Activating existing instance");
480 message = g_strdup_printf ("%" G_GUINT32_FORMAT,
484 bacon_message_connection_send (connection, message);
486 /* We never popup a window, so tell startup-notification
487 * that we are done. */
488 gdk_notify_startup_complete ();
491 bacon_message_connection_free (connection);
496 g_warning ("Cannot create the 'empathy' bacon connection.");
500 mc = empathy_mission_control_dup_singleton ();
501 g_signal_connect (mc, "ServiceEnded",
502 G_CALLBACK (service_ended_cb),
504 g_signal_connect (mc, "Error",
505 G_CALLBACK (operation_error_cb),
508 if (accounts_dialog) {
511 dialog = empathy_accounts_dialog_show (NULL, NULL);
512 g_signal_connect (dialog, "destroy",
513 G_CALLBACK (gtk_main_quit),
520 /* Setting up Idle */
521 idle = empathy_idle_dup_singleton ();
522 empathy_idle_set_auto_away (idle, TRUE);
523 use_nm_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_NM, idle);
524 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_NM,
525 use_nm_notify_cb, idle);
528 empathy_conf_get_bool (empathy_conf_get(),
529 EMPATHY_PREFS_AUTOCONNECT,
531 if (autoconnect && ! no_connect &&
532 empathy_idle_get_state (idle) <= MC_PRESENCE_OFFLINE) {
533 empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
536 create_salut_account ();
539 window = empathy_main_window_show ();
540 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
543 /* We se the callback here because we need window */
544 bacon_message_connection_set_callback (connection,
545 on_bacon_message_received,
549 /* Handle channels */
550 dispatcher = empathy_dispatcher_dup_singleton ();
551 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
554 log_manager = empathy_log_manager_dup_singleton ();
555 empathy_log_manager_observe (log_manager, dispatcher);
557 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
558 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
560 ft_manager = empathy_ft_manager_dup_singleton ();
562 notify_init (_(PACKAGE_NAME));
563 /* Create the call factory */
564 call_factory = empathy_call_factory_initialise ();
565 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
566 G_CALLBACK (new_call_handler_cb), NULL);
570 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
573 g_object_unref (idle);
574 g_object_unref (icon);
575 g_object_unref (log_manager);
576 g_object_unref (dispatcher);
577 g_object_unref (chatroom_manager);
578 g_object_unref (ft_manager);