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>
33 #include <unique/unique.h>
36 #include <clutter-gtk/clutter-gtk.h>
39 #include <libebook/e-book.h>
40 #include <libnotify/notify.h>
42 #include <telepathy-glib/dbus.h>
43 #include <telepathy-glib/util.h>
44 #include <telepathy-glib/connection-manager.h>
45 #include <telepathy-glib/interfaces.h>
47 #include <libempathy/empathy-idle.h>
48 #include <libempathy/empathy-utils.h>
49 #include <libempathy/empathy-call-factory.h>
50 #include <libempathy/empathy-chatroom-manager.h>
51 #include <libempathy/empathy-account-settings.h>
52 #include <libempathy/empathy-connectivity.h>
53 #include <libempathy/empathy-account-manager.h>
54 #include <libempathy/empathy-connection-managers.h>
55 #include <libempathy/empathy-debugger.h>
56 #include <libempathy/empathy-dispatcher.h>
57 #include <libempathy/empathy-dispatch-operation.h>
58 #include <libempathy/empathy-log-manager.h>
59 #include <libempathy/empathy-ft-factory.h>
60 #include <libempathy/empathy-tp-chat.h>
61 #include <libempathy/empathy-tp-call.h>
63 #include <libempathy-gtk/empathy-conf.h>
64 #include <libempathy-gtk/empathy-ui-utils.h>
65 #include <libempathy-gtk/empathy-location-manager.h>
67 #include "empathy-accounts-dialog.h"
68 #include "empathy-main-window.h"
69 #include "empathy-status-icon.h"
70 #include "empathy-call-window.h"
71 #include "empathy-chat-window.h"
72 #include "empathy-ft-manager.h"
74 #include "extensions/extensions.h"
76 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
77 #include <libempathy/empathy-debug.h>
81 #define COMMAND_ACCOUNTS_DIALOG 1
84 dispatch_cb (EmpathyDispatcher *dispatcher,
85 EmpathyDispatchOperation *operation,
90 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
92 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT) {
93 EmpathyTpChat *tp_chat;
94 EmpathyChat *chat = NULL;
97 tp_chat = EMPATHY_TP_CHAT (
98 empathy_dispatch_operation_get_channel_wrapper (operation));
100 id = empathy_tp_chat_get_id (tp_chat);
102 EmpathyContact *contact;
104 contact = empathy_tp_chat_get_remote_contact (tp_chat);
106 id = empathy_contact_get_id (contact);
111 EmpathyAccountManager *manager;
112 TpConnection *connection;
113 EmpathyAccount *account;
115 manager = empathy_account_manager_dup_singleton ();
116 connection = empathy_tp_chat_get_connection (tp_chat);
117 account = empathy_account_manager_get_account_for_connection (manager,
119 chat = empathy_chat_window_find_chat (account, id);
120 g_object_unref (manager);
124 empathy_chat_set_tp_chat (chat, tp_chat);
126 chat = empathy_chat_new (tp_chat);
129 empathy_chat_window_present_chat (chat);
131 empathy_dispatch_operation_claim (operation);
132 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA) {
133 EmpathyCallFactory *factory;
135 factory = empathy_call_factory_get ();
136 empathy_call_factory_claim_channel (factory, operation);
137 } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) {
138 EmpathyFTFactory *factory;
140 factory = empathy_ft_factory_dup_singleton ();
142 /* if the operation is not incoming, don't claim it,
143 * as it might have been triggered by another client, and
144 * we are observing it.
146 if (empathy_dispatch_operation_is_incoming (operation)) {
147 empathy_ft_factory_claim_channel (factory, operation);
152 /* Salut account creation */
154 should_create_salut_account (void)
156 EmpathyAccountManager *manager;
157 gboolean salut_created = FALSE;
160 /* Check if we already created a salut account */
161 empathy_conf_get_bool (empathy_conf_get (),
162 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
167 DEBUG ("Gconf says we already created a salut account once");
171 manager = empathy_account_manager_dup_singleton ();
172 accounts = empathy_account_manager_dup_accounts (manager);
174 for (l = accounts; l != NULL; l = g_list_next (l)) {
175 EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
177 if (!tp_strdiff (empathy_account_get_protocol (account), "local-xmpp"))
178 salut_created = TRUE;
180 g_object_unref (account);
183 g_object_unref (manager);
187 DEBUG ("Existing salut account already exists, flagging so in gconf");
188 empathy_conf_set_bool (empathy_conf_get (),
189 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
193 return !salut_created;
197 salut_account_created (GObject *source,
198 GAsyncResult *result, gpointer user_data)
200 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
201 EmpathyAccount *account;
202 GError *error = NULL;
204 if (!empathy_account_settings_apply_finish (settings, result, &error))
206 DEBUG ("Failed to create salut account: %s", error->message);
207 g_error_free (error);
211 account = empathy_account_settings_get_account (settings);
213 empathy_account_set_enabled (account, TRUE);
214 empathy_conf_set_bool (empathy_conf_get (),
215 EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
220 use_conn_notify_cb (EmpathyConf *conf,
224 EmpathyConnectivity *connectivity = user_data;
227 if (empathy_conf_get_bool (conf, key, &use_conn)) {
228 empathy_connectivity_set_use_conn (connectivity, use_conn);
233 create_salut_account_if_needed (EmpathyConnectionManagers *managers)
235 EmpathyAccountSettings *settings;
236 TpConnectionManager *manager;
237 const TpConnectionManagerProtocol *protocol;
240 gchar *nickname = NULL;
241 gchar *first_name = NULL;
242 gchar *last_name = NULL;
245 GError *error = NULL;
248 if (!should_create_salut_account ())
251 manager = empathy_connection_managers_get_cm (managers, "salut");
254 DEBUG ("Salut not installed, not making a salut account");
258 protocol = tp_connection_manager_get_protocol (manager, "local-xmpp");
259 if (protocol == NULL)
261 DEBUG ("Salut doesn't support local-xmpp!!");
265 DEBUG ("Trying to add a salut account...");
267 /* Get self EContact from EDS */
268 if (!e_book_get_self (&contact, &book, &error)) {
269 DEBUG ("Failed to get self econtact: %s",
270 error ? error->message : "No error given");
271 g_clear_error (&error);
275 settings = empathy_account_settings_new ("salut", "local-xmpp",
278 nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
279 first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
280 last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
281 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
282 jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
284 if (!tp_strdiff (nickname, "nickname")) {
289 DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
290 "last-name=%s\nemail=%s\njid=%s\n",
291 nickname, first_name, last_name, email, jid);
293 empathy_account_settings_set_string (settings, "nickname", nickname ? nickname : "");
294 empathy_account_settings_set_string (settings, "first-name", first_name ? first_name : "");
295 empathy_account_settings_set_string (settings, "last-name", last_name ? last_name : "");
296 empathy_account_settings_set_string (settings, "email", email ? email : "");
297 empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
299 empathy_account_settings_apply_async (settings,
300 salut_account_created, NULL);
307 g_object_unref (settings);
308 g_object_unref (contact);
309 g_object_unref (book);
313 connection_managers_ready_cb (EmpathyConnectionManagers *managers,
314 GParamSpec *spec, gpointer user_data)
316 if (empathy_connection_managers_is_ready (managers))
318 create_salut_account_if_needed (managers);
319 g_object_unref (managers);
325 create_salut_account (void)
327 EmpathyConnectionManagers *managers;
329 if (!should_create_salut_account ())
332 managers = empathy_connection_managers_dup_singleton ();
334 if (empathy_connection_managers_is_ready (managers))
336 create_salut_account_if_needed (managers);
337 g_object_unref (managers);
341 g_signal_connect (managers, "notify::ready",
342 G_CALLBACK (connection_managers_ready_cb), NULL);
347 migrate_config_to_xdg_dir (void)
349 gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
351 GFile *xdg_file, *old_file;
352 static const gchar* filenames[] = {
356 "contact-groups.xml",
357 "status-presets.xml",
362 xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
363 if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
364 /* xdg config dir already exists */
369 old_dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
370 if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
371 /* old config dir didn't exist */
377 if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1) {
378 DEBUG ("Failed to create configuration directory; aborting migration");
384 for (i = 0; filenames[i]; i++) {
385 old_filename = g_build_filename (old_dir, filenames[i], NULL);
386 if (!g_file_test (old_filename, G_FILE_TEST_EXISTS)) {
387 g_free (old_filename);
390 xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
391 old_file = g_file_new_for_path (old_filename);
392 xdg_file = g_file_new_for_path (xdg_filename);
393 if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
394 NULL, NULL, NULL, NULL)) {
395 DEBUG ("Failed to migrate %s", filenames[i]);
397 g_free (old_filename);
398 g_free (xdg_filename);
399 g_object_unref (old_file);
400 g_object_unref (xdg_file);
407 static UniqueResponse
408 unique_app_message_cb (UniqueApp *unique_app,
410 UniqueMessageData *data,
414 GtkWidget *window = user_data;
416 DEBUG ("Other instance launched, presenting the main window. "
417 "Command=%d, timestamp %u", command, timestamp);
419 if (command == COMMAND_ACCOUNTS_DIALOG) {
420 empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
422 gtk_window_set_screen (GTK_WINDOW (window),
423 unique_message_data_get_screen (data));
424 gtk_window_set_startup_id (GTK_WINDOW (window),
425 unique_message_data_get_startup_id (data));
426 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
429 return UNIQUE_RESPONSE_OK;
433 show_version_cb (const char *option_name,
438 g_print ("%s\n", PACKAGE_STRING);
446 new_incoming_transfer_cb (EmpathyFTFactory *factory,
447 EmpathyFTHandler *handler,
452 empathy_ft_manager_display_error (handler, error);
454 empathy_receive_file_with_file_chooser (handler);
459 new_ft_handler_cb (EmpathyFTFactory *factory,
460 EmpathyFTHandler *handler,
465 empathy_ft_manager_display_error (handler, error);
467 empathy_ft_manager_add_handler (handler);
470 g_object_unref (handler);
474 new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler,
475 gboolean outgoing, gpointer user_data)
477 EmpathyCallWindow *window;
479 window = empathy_call_window_new (handler);
480 gtk_widget_show (GTK_WIDGET (window));
485 default_log_handler (const gchar *log_domain,
486 GLogLevelFlags log_level,
487 const gchar *message,
490 g_log_default_handler (log_domain, log_level, message, NULL);
492 /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
493 * debugger as they already have in empathy_debug. */
494 if (log_level != G_LOG_LEVEL_DEBUG
495 || tp_strdiff (log_domain, G_LOG_DOMAIN)) {
496 EmpathyDebugger *dbg;
499 dbg = empathy_debugger_get_singleton ();
500 g_get_current_time (&now);
502 empathy_debugger_add_message (dbg, &now, log_domain,
506 #endif /* ENABLE_DEBUG */
509 account_manager_ready_cb (EmpathyAccountManager *manager,
513 if (!empathy_account_manager_is_ready (manager))
516 if (empathy_account_manager_get_count (manager) == 0)
518 empathy_accounts_dialog_show (GTK_WINDOW (empathy_main_window_get ()),
521 create_salut_account ();
525 main (int argc, char *argv[])
528 EmpathyLocationManager *location_manager = NULL;
530 EmpathyStatusIcon *icon;
531 EmpathyDispatcher *dispatcher;
532 EmpathyAccountManager *account_manager;
533 EmpathyLogManager *log_manager;
534 EmpathyChatroomManager *chatroom_manager;
535 EmpathyCallFactory *call_factory;
536 EmpathyFTFactory *ft_factory;
539 EmpathyConnectivity *connectivity;
540 gboolean autoconnect = TRUE;
541 gboolean no_connect = FALSE;
542 gboolean hide_contact_list = FALSE;
543 gboolean accounts_dialog = FALSE;
544 GError *error = NULL;
545 TpDBusDaemon *dbus_daemon;
546 UniqueApp *unique_app;
547 GOptionEntry options[] = {
549 0, G_OPTION_ARG_NONE, &no_connect,
550 N_("Don't connect on startup"),
552 { "hide-contact-list", 'h',
553 0, G_OPTION_ARG_NONE, &hide_contact_list,
554 N_("Don't show the contact list on startup"),
557 0, G_OPTION_ARG_NONE, &accounts_dialog,
558 N_("Show the accounts dialog"),
561 G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb, NULL, NULL },
566 g_thread_init (NULL);
569 if (!gtk_init_with_args (&argc, &argv,
570 N_("- Empathy IM Client"),
571 options, GETTEXT_PACKAGE, &error)) {
572 g_warning ("Error in empathy init: %s", error->message);
577 g_set_application_name (_(PACKAGE_NAME));
578 g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
580 gst_init (&argc, &argv);
582 #if HAVE_LIBCHAMPLAIN
583 gtk_clutter_init (&argc, &argv);
586 gtk_window_set_default_icon_name ("empathy");
587 textdomain (GETTEXT_PACKAGE);
590 /* Set up debugger */
591 g_log_set_default_handler (default_log_handler, NULL);
594 unique_app = unique_app_new_with_commands ("org.gnome.Empathy",
597 COMMAND_ACCOUNTS_DIALOG,
600 if (unique_app_is_running (unique_app)) {
601 unique_app_send_message (unique_app,
603 COMMAND_ACCOUNTS_DIALOG :
606 g_object_unref (unique_app);
610 /* Take well-known name */
611 dbus_daemon = tp_dbus_daemon_dup (&error);
613 if (!tp_dbus_daemon_request_name (dbus_daemon,
616 DEBUG ("Failed to request well-known name: %s",
617 error ? error->message : "no message");
618 g_clear_error (&error);
620 g_object_unref (dbus_daemon);
622 DEBUG ("Failed to dup dbus daemon: %s",
623 error ? error->message : "no message");
624 g_clear_error (&error);
627 if (accounts_dialog) {
630 dialog = empathy_accounts_dialog_show (NULL, NULL);
631 g_signal_connect (dialog, "destroy",
632 G_CALLBACK (gtk_main_quit),
639 /* Setting up Idle */
640 idle = empathy_idle_dup_singleton ();
641 empathy_idle_set_auto_away (idle, TRUE);
643 /* Setting up Connectivity */
644 connectivity = empathy_connectivity_dup_singleton ();
645 use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
647 empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
648 use_conn_notify_cb, connectivity);
651 empathy_conf_get_bool (empathy_conf_get (),
652 EMPATHY_PREFS_AUTOCONNECT,
654 if (autoconnect && ! no_connect &&
655 tp_connection_presence_type_cmp_availability (empathy_idle_get_state
656 (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
657 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
660 /* account management */
661 account_manager = empathy_account_manager_dup_singleton ();
662 g_signal_connect (account_manager, "notify::ready",
663 G_CALLBACK (account_manager_ready_cb), NULL);
665 migrate_config_to_xdg_dir ();
668 window = empathy_main_window_show ();
669 icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
671 g_signal_connect (unique_app, "message-received",
672 G_CALLBACK (unique_app_message_cb),
675 /* Handle channels */
676 dispatcher = empathy_dispatcher_dup_singleton ();
677 g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
680 log_manager = empathy_log_manager_dup_singleton ();
681 empathy_log_manager_observe (log_manager, dispatcher);
683 chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
684 empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
686 notify_init (_(PACKAGE_NAME));
687 /* Create the call factory */
688 call_factory = empathy_call_factory_initialise ();
689 g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
690 G_CALLBACK (new_call_handler_cb), NULL);
691 /* Create the FT factory */
692 ft_factory = empathy_ft_factory_dup_singleton ();
693 g_signal_connect (ft_factory, "new-ft-handler",
694 G_CALLBACK (new_ft_handler_cb), NULL);
695 g_signal_connect (ft_factory, "new-incoming-transfer",
696 G_CALLBACK (new_incoming_transfer_cb), NULL);
698 /* Location mananger */
700 location_manager = empathy_location_manager_dup_singleton ();
705 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
707 g_object_unref (idle);
708 g_object_unref (connectivity);
709 g_object_unref (icon);
710 g_object_unref (account_manager);
711 g_object_unref (log_manager);
712 g_object_unref (dispatcher);
713 g_object_unref (chatroom_manager);
715 g_object_unref (location_manager);
717 g_object_unref (ft_factory);
718 g_object_unref (unique_app);