]> git.0d.be Git - empathy.git/blob - src/empathy.c
Don't pass managers to create_salut_account_if_needed
[empathy.git] / src / empathy.c
1 /*
2  * Copyright (C) 2007-2009 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32 #include <unique/unique.h>
33
34 #if HAVE_LIBCHAMPLAIN
35 #include <clutter-gtk/clutter-gtk.h>
36 #endif
37
38 #include <libnotify/notify.h>
39
40 #include <telepathy-glib/account-manager.h>
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/util.h>
43 #include <telepathy-glib/connection-manager.h>
44 #include <telepathy-glib/interfaces.h>
45
46 #include <libempathy/empathy-idle.h>
47 #include <libempathy/empathy-utils.h>
48 #include <libempathy/empathy-call-factory.h>
49 #include <libempathy/empathy-chatroom-manager.h>
50 #include <libempathy/empathy-account-settings.h>
51 #include <libempathy/empathy-connectivity.h>
52 #include <libempathy/empathy-connection-managers.h>
53 #include <libempathy/empathy-debugger.h>
54 #include <libempathy/empathy-dispatcher.h>
55 #include <libempathy/empathy-dispatch-operation.h>
56 #include <libempathy/empathy-log-manager.h>
57 #include <libempathy/empathy-ft-factory.h>
58 #include <libempathy/empathy-tp-chat.h>
59 #include <libempathy/empathy-tp-call.h>
60
61 #include <libempathy-gtk/empathy-conf.h>
62 #include <libempathy-gtk/empathy-ui-utils.h>
63 #include <libempathy-gtk/empathy-location-manager.h>
64
65 #include "empathy-account-assistant.h"
66 #include "empathy-accounts-dialog.h"
67 #include "empathy-main-window.h"
68 #include "empathy-status-icon.h"
69 #include "empathy-call-window.h"
70 #include "empathy-chat-window.h"
71 #include "empathy-ft-manager.h"
72 #include "empathy-import-mc4-accounts.h"
73 #include "empathy-auto-salut-account-helper.h"
74
75 #include "extensions/extensions.h"
76
77 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
78 #include <libempathy/empathy-debug.h>
79
80 #include <gst/gst.h>
81
82 #define COMMAND_ACCOUNTS_DIALOG 1
83
84 static gboolean account_dialog_only = FALSE;
85 static gboolean start_hidden = FALSE;
86 static gboolean no_connect = FALSE;
87
88 static void
89 dispatch_cb (EmpathyDispatcher *dispatcher,
90     EmpathyDispatchOperation *operation,
91     gpointer user_data)
92 {
93   GQuark channel_type;
94
95   channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
96
97   if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
98     {
99       EmpathyTpChat *tp_chat;
100       EmpathyChat   *chat = NULL;
101       const gchar   *id;
102
103       tp_chat = EMPATHY_TP_CHAT
104         (empathy_dispatch_operation_get_channel_wrapper (operation));
105
106       id = empathy_tp_chat_get_id (tp_chat);
107       if (!EMP_STR_EMPTY (id))
108         {
109           TpConnection *connection;
110           TpAccount *account;
111
112           connection = empathy_tp_chat_get_connection (tp_chat);
113           account = empathy_get_account_for_connection (connection);
114           chat = empathy_chat_window_find_chat (account, id);
115         }
116
117       if (chat)
118         {
119           empathy_chat_set_tp_chat (chat, tp_chat);
120         }
121       else
122         {
123           chat = empathy_chat_new (tp_chat);
124           /* empathy_chat_new returns a floating reference as EmpathyChat is
125            * a GtkWidget. This reference will be taken by a container
126            * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
127         }
128
129       empathy_chat_window_present_chat (chat);
130
131       empathy_dispatch_operation_claim (operation);
132     }
133   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
134     {
135       EmpathyCallFactory *factory;
136
137       factory = empathy_call_factory_get ();
138       empathy_call_factory_claim_channel (factory, operation);
139     }
140   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
141     {
142       EmpathyFTFactory *factory;
143
144       factory = empathy_ft_factory_dup_singleton ();
145
146       /* if the operation is not incoming, don't claim it,
147        * as it might have been triggered by another client, and
148        * we are observing it.
149        */
150       if (empathy_dispatch_operation_is_incoming (operation))
151         empathy_ft_factory_claim_channel (factory, operation);
152     }
153 }
154
155 static void
156 use_conn_notify_cb (EmpathyConf *conf,
157     const gchar *key,
158     gpointer     user_data)
159 {
160   EmpathyConnectivity *connectivity = user_data;
161   gboolean     use_conn;
162
163   if (empathy_conf_get_bool (conf, key, &use_conn))
164     {
165       empathy_connectivity_set_use_conn (connectivity, use_conn);
166     }
167 }
168
169 static gboolean
170 has_non_salut_accounts (TpAccountManager *manager)
171 {
172   gboolean ret = FALSE;
173   GList *accounts, *l;
174
175   accounts = tp_account_manager_get_valid_accounts (manager);
176
177   for (l = accounts ; l != NULL; l = g_list_next (l))
178     {
179       if (tp_strdiff (tp_account_get_protocol (l->data), "local-xmpp"))
180         {
181           ret = TRUE;
182           break;
183         }
184     }
185
186   g_list_free (accounts);
187
188   return ret;
189 }
190
191 static void
192 maybe_show_account_assistant (void)
193 {
194   TpAccountManager *manager;
195   manager = tp_account_manager_dup ();
196
197   if (!has_non_salut_accounts (manager))
198     empathy_account_assistant_show (GTK_WINDOW (empathy_main_window_get ()));
199
200   g_object_unref (manager);
201 }
202
203 static gboolean
204 check_connection_managers_ready (EmpathyConnectionManagers *managers)
205 {
206   if (empathy_connection_managers_is_ready (managers))
207     {
208       if (!empathy_import_mc4_accounts (managers) && !start_hidden)
209         maybe_show_account_assistant ();
210
211       create_salut_account_if_needed ();
212       g_object_unref (managers);
213       return TRUE;
214     }
215   return FALSE;
216 }
217
218 static void
219 connection_managers_ready_cb (EmpathyConnectionManagers *managers,
220     GParamSpec *spec,
221     gpointer user_data)
222 {
223   check_connection_managers_ready (managers);
224 }
225
226 static void
227 migrate_config_to_xdg_dir (void)
228 {
229   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
230   int i;
231   GFile *xdg_file, *old_file;
232   static const gchar* filenames[] = {
233     "geometry.ini",
234     "irc-networks.xml",
235     "chatrooms.xml",
236     "contact-groups.xml",
237     "status-presets.xml",
238     "accels.txt",
239     NULL
240   };
241
242   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
243   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
244     {
245       /* xdg config dir already exists */
246       g_free (xdg_dir);
247       return;
248     }
249
250   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
251       PACKAGE_NAME, NULL);
252   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
253     {
254       /* old config dir didn't exist */
255       g_free (xdg_dir);
256       g_free (old_dir);
257       return;
258     }
259
260   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
261     {
262       DEBUG ("Failed to create configuration directory; aborting migration");
263       g_free (xdg_dir);
264       g_free (old_dir);
265       return;
266     }
267
268   for (i = 0; filenames[i]; i++)
269     {
270       old_filename = g_build_filename (old_dir, filenames[i], NULL);
271       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
272         {
273           g_free (old_filename);
274           continue;
275         }
276       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
277       old_file = g_file_new_for_path (old_filename);
278       xdg_file = g_file_new_for_path (xdg_filename);
279
280       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
281           NULL, NULL, NULL, NULL))
282         DEBUG ("Failed to migrate %s", filenames[i]);
283
284       g_free (old_filename);
285       g_free (xdg_filename);
286       g_object_unref (old_file);
287       g_object_unref (xdg_file);
288     }
289
290   g_free (xdg_dir);
291   g_free (old_dir);
292 }
293
294 static void
295 do_show_accounts_ui (GtkWindow *window,
296     TpAccountManager *manager)
297 {
298
299   GtkWidget *ui;
300
301   if (has_non_salut_accounts (manager))
302     ui = empathy_accounts_dialog_show (window, NULL);
303   else
304     ui = empathy_account_assistant_show (window);
305
306   if (account_dialog_only)
307     g_signal_connect (ui, "destroy",
308       G_CALLBACK (gtk_main_quit), NULL);
309 }
310
311 static void
312 account_manager_ready_for_accounts_cb (GObject *source_object,
313     GAsyncResult *result,
314     gpointer user_data)
315 {
316   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
317   GError *error = NULL;
318
319   if (!tp_account_manager_prepare_finish (manager, result, &error))
320     {
321       DEBUG ("Failed to prepare account manager: %s", error->message);
322       g_error_free (error);
323       return;
324     }
325
326   do_show_accounts_ui (user_data, manager);
327 }
328
329 static void
330 show_accounts_ui (GtkWindow *window,
331     gboolean force)
332 {
333   TpAccountManager *manager;
334
335   if (!force)
336     return;
337
338   manager = tp_account_manager_dup ();
339
340   tp_account_manager_prepare_async (manager, NULL,
341       account_manager_ready_for_accounts_cb, window);
342
343   g_object_unref (manager);
344 }
345
346 static UniqueResponse
347 unique_app_message_cb (UniqueApp *unique_app,
348     gint command,
349     UniqueMessageData *data,
350     guint timestamp,
351     gpointer user_data)
352 {
353   GtkWindow *window = user_data;
354
355   DEBUG ("Other instance launched, presenting the main window. "
356       "Command=%d, timestamp %u", command, timestamp);
357
358   if (command == COMMAND_ACCOUNTS_DIALOG)
359     {
360       show_accounts_ui (NULL, TRUE);
361     }
362   else
363     {
364       /* We're requested to show stuff again, disable the start hidden global
365        * in case the accounts wizard wants to pop up.
366        */
367       start_hidden = FALSE;
368
369       show_accounts_ui (window, FALSE);
370
371       gtk_window_set_screen (GTK_WINDOW (window),
372           unique_message_data_get_screen (data));
373       gtk_window_set_startup_id (GTK_WINDOW (window),
374           unique_message_data_get_startup_id (data));
375       gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
376     }
377
378   return UNIQUE_RESPONSE_OK;
379 }
380
381 static gboolean
382 show_version_cb (const char *option_name,
383     const char *value,
384     gpointer data,
385     GError **error)
386 {
387   g_print ("%s\n", PACKAGE_STRING);
388
389   exit (EXIT_SUCCESS);
390
391   return FALSE;
392 }
393
394 static void
395 new_incoming_transfer_cb (EmpathyFTFactory *factory,
396     EmpathyFTHandler *handler,
397     GError *error,
398     gpointer user_data)
399 {
400   if (error)
401     empathy_ft_manager_display_error (handler, error);
402   else
403     empathy_receive_file_with_file_chooser (handler);
404 }
405
406 static void
407 new_ft_handler_cb (EmpathyFTFactory *factory,
408     EmpathyFTHandler *handler,
409     GError *error,
410     gpointer user_data)
411 {
412   if (error)
413     empathy_ft_manager_display_error (handler, error);
414   else
415     empathy_ft_manager_add_handler (handler);
416
417   g_object_unref (handler);
418 }
419
420 static void
421 new_call_handler_cb (EmpathyCallFactory *factory,
422     EmpathyCallHandler *handler,
423     gboolean outgoing,
424     gpointer user_data)
425 {
426   EmpathyCallWindow *window;
427
428   window = empathy_call_window_new (handler);
429   gtk_widget_show (GTK_WIDGET (window));
430 }
431
432 #ifdef ENABLE_DEBUG
433 static void
434 default_log_handler (const gchar *log_domain,
435     GLogLevelFlags log_level,
436     const gchar *message,
437     gpointer user_data)
438 {
439   g_log_default_handler (log_domain, log_level, message, NULL);
440
441   /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
442    * debugger as they already have in empathy_debug. */
443   if (log_level != G_LOG_LEVEL_DEBUG
444       || tp_strdiff (log_domain, G_LOG_DOMAIN))
445     {
446         EmpathyDebugger *dbg;
447         GTimeVal now;
448
449         dbg = empathy_debugger_get_singleton ();
450         g_get_current_time (&now);
451
452         empathy_debugger_add_message (dbg, &now, log_domain,
453                                       log_level, message);
454     }
455 }
456 #endif /* ENABLE_DEBUG */
457
458 static void
459 account_manager_ready_cb (GObject *source_object,
460     GAsyncResult *result,
461     gpointer user_data)
462 {
463   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
464   GError *error = NULL;
465   EmpathyIdle *idle;
466   EmpathyConnectivity *connectivity;
467   gboolean autoconnect = TRUE;
468   TpConnectionPresenceType presence;
469
470   if (!tp_account_manager_prepare_finish (manager, result, &error))
471     {
472       DEBUG ("Failed to prepare account manager: %s", error->message);
473       g_error_free (error);
474       return;
475     }
476
477   /* Autoconnect */
478   idle = empathy_idle_dup_singleton ();
479   connectivity = empathy_connectivity_dup_singleton ();
480
481   presence = tp_account_manager_get_most_available_presence (manager, NULL,
482       NULL);
483
484   empathy_conf_get_bool (empathy_conf_get (),
485       EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
486   if (autoconnect && !no_connect &&
487       tp_connection_presence_type_cmp_availability
488           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
489             <= 0)
490       /* if current state is Offline, then put it online */
491       empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
492
493   if (should_create_salut_account (manager)
494       || !empathy_import_mc4_has_imported ())
495     {
496       EmpathyConnectionManagers *managers;
497       managers = empathy_connection_managers_dup_singleton ();
498
499       if (!check_connection_managers_ready (managers))
500         {
501           g_signal_connect (managers, "notify::ready",
502             G_CALLBACK (connection_managers_ready_cb), NULL);
503         }
504     }
505   else if (!start_hidden)
506     {
507       maybe_show_account_assistant ();
508     }
509
510   g_object_unref (idle);
511   g_object_unref (connectivity);
512 }
513
514 static EmpathyDispatcher *
515 setup_dispatcher (void)
516 {
517   EmpathyDispatcher *d;
518   GPtrArray *filters;
519   struct {
520     const gchar *channeltype;
521     TpHandleType handletype;
522   } types[] = {
523     /* Text channels with handle types none, contact and room */
524     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE  },
525     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT  },
526     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM  },
527     /* file transfer to contacts */
528     { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT  },
529     /* stream media to contacts */
530     { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT  },
531     /* roomlists */
532     { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
533   };
534   gchar *capabilities[] = {
535     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
536     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
537     NULL };
538   GHashTable *asv;
539   guint i;
540
541   /* Setup the basic Client.Handler that matches our client filter */
542   filters = g_ptr_array_new ();
543   asv = tp_asv_new (
544         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
545            TP_IFACE_CHANNEL_TYPE_TEXT,
546         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
547             TP_HANDLE_TYPE_CONTACT,
548         NULL);
549   g_ptr_array_add (filters, asv);
550
551   d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
552
553   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
554   g_ptr_array_free (filters, TRUE);
555
556   /* Setup the an extended Client.Handler that matches everything we can do */
557   filters = g_ptr_array_new ();
558   for (i = 0 ; i < G_N_ELEMENTS (types); i++)
559     {
560       asv = tp_asv_new (
561         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
562         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
563         NULL);
564
565       g_ptr_array_add (filters, asv);
566     }
567
568   asv = tp_asv_new (
569         TP_IFACE_CHANNEL ".ChannelType",
570           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
571         TP_IFACE_CHANNEL ".TargetHandleType",
572           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
573         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
574           G_TYPE_BOOLEAN, TRUE,
575         NULL);
576   g_ptr_array_add (filters, asv);
577
578   asv = tp_asv_new (
579         TP_IFACE_CHANNEL ".ChannelType",
580           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
581         TP_IFACE_CHANNEL ".TargetHandleType",
582           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
583         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
584           G_TYPE_BOOLEAN, TRUE,
585         NULL);
586   g_ptr_array_add (filters, asv);
587
588
589   empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
590     filters, capabilities);
591
592   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
593   g_ptr_array_free (filters, TRUE);
594
595   return d;
596 }
597
598 static void
599 account_status_changed_cb (TpAccount *account,
600     guint old_status,
601     guint new_status,
602     guint reason,
603     gchar *dbus_error_name,
604     GHashTable *details,
605     EmpathyChatroom *room)
606 {
607   TpConnection *conn;
608
609   conn = tp_account_get_connection (account);
610
611   empathy_dispatcher_join_muc (conn,
612       empathy_chatroom_get_room (room), NULL, NULL);
613 }
614
615 static void
616 account_manager_chatroom_ready_cb (GObject *source_object,
617     GAsyncResult *result,
618     gpointer user_data)
619 {
620   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
621   EmpathyChatroomManager *chatroom_manager = user_data;
622   GList *accounts, *l;
623   GError *error = NULL;
624
625   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
626     {
627       DEBUG ("Failed to prepare account manager: %s", error->message);
628       g_error_free (error);
629       return;
630     }
631
632   accounts = tp_account_manager_get_valid_accounts (account_manager);
633
634   for (l = accounts; l != NULL; l = g_list_next (l))
635     {
636       TpAccount *account = TP_ACCOUNT (l->data);
637       TpConnection *conn;
638       GList *chatrooms, *p;
639
640       conn = tp_account_get_connection (account);
641
642       chatrooms = empathy_chatroom_manager_get_chatrooms (
643           chatroom_manager, account);
644
645       for (p = chatrooms; p != NULL; p = p->next)
646         {
647           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
648
649           if (!empathy_chatroom_get_auto_connect (room))
650             continue;
651
652           if (conn == NULL)
653             {
654               g_signal_connect (G_OBJECT (account), "status-changed",
655                   G_CALLBACK (account_status_changed_cb), room);
656             }
657           else
658             {
659               empathy_dispatcher_join_muc (conn,
660                   empathy_chatroom_get_room (room), NULL, NULL);
661             }
662         }
663
664       g_list_free (chatrooms);
665     }
666
667   g_list_free (accounts);
668 }
669
670 static void
671 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
672     GParamSpec *pspec,
673     gpointer user_data)
674 {
675   TpAccountManager *account_manager = user_data;
676
677   tp_account_manager_prepare_async (account_manager, NULL,
678       account_manager_chatroom_ready_cb, chatroom_manager);
679 }
680
681 int
682 main (int argc, char *argv[])
683 {
684 #if HAVE_GEOCLUE
685   EmpathyLocationManager *location_manager = NULL;
686 #endif
687   EmpathyStatusIcon *icon;
688   EmpathyDispatcher *dispatcher;
689   TpAccountManager *account_manager;
690   EmpathyLogManager *log_manager;
691   EmpathyChatroomManager *chatroom_manager;
692   EmpathyCallFactory *call_factory;
693   EmpathyFTFactory  *ft_factory;
694   GtkWidget *window;
695   EmpathyIdle *idle;
696   EmpathyConnectivity *connectivity;
697   GError *error = NULL;
698   TpDBusDaemon *dbus_daemon;
699   UniqueApp *unique_app;
700   gboolean chatroom_manager_ready;
701
702   GOptionContext *optcontext;
703   GOptionEntry options[] = {
704       { "no-connect", 'n',
705         0, G_OPTION_ARG_NONE, &no_connect,
706         N_("Don't connect on startup"),
707         NULL },
708       { "start-hidden", 'h',
709         0, G_OPTION_ARG_NONE, &start_hidden,
710         N_("Don't display the contact list or any other dialogs on startup"),
711         NULL },
712       { "accounts", 'a',
713         0, G_OPTION_ARG_NONE, &account_dialog_only,
714         N_("Show the accounts dialog"),
715         NULL },
716       { "version", 'v',
717         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
718         NULL, NULL },
719       { NULL }
720   };
721
722   /* Init */
723   g_thread_init (NULL);
724   empathy_init ();
725
726   optcontext = g_option_context_new (N_("- Empathy IM Client"));
727   g_option_context_add_group (optcontext, gst_init_get_option_group ());
728   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
729 #if HAVE_LIBCHAMPLAIN
730   g_option_context_add_group (optcontext, clutter_get_option_group ());
731 #endif
732   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
733
734   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
735     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
736         error->message, argv[0]);
737     g_warning ("Error in empathy init: %s", error->message);
738     return EXIT_FAILURE;
739   }
740
741   g_option_context_free (optcontext);
742
743   empathy_gtk_init ();
744   g_set_application_name (_(PACKAGE_NAME));
745   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
746
747   gtk_window_set_default_icon_name ("empathy");
748   textdomain (GETTEXT_PACKAGE);
749
750 #ifdef ENABLE_DEBUG
751   /* Set up debugger */
752   g_log_set_default_handler (default_log_handler, NULL);
753 #endif
754
755   unique_app = unique_app_new_with_commands ("org.gnome.Empathy",
756       NULL, "accounts_dialog", COMMAND_ACCOUNTS_DIALOG, NULL);
757
758   if (unique_app_is_running (unique_app))
759     {
760       unique_app_send_message (unique_app, account_dialog_only ?
761           COMMAND_ACCOUNTS_DIALOG : UNIQUE_ACTIVATE, NULL);
762
763       g_object_unref (unique_app);
764       return EXIT_SUCCESS;
765     }
766
767   /* Take well-known name */
768   dbus_daemon = tp_dbus_daemon_dup (&error);
769   if (error == NULL)
770     {
771       if (!tp_dbus_daemon_request_name (dbus_daemon,
772           "org.gnome.Empathy", TRUE, &error))
773         {
774           DEBUG ("Failed to request well-known name: %s",
775                  error ? error->message : "no message");
776           g_clear_error (&error);
777         }
778       g_object_unref (dbus_daemon);
779     }
780   else
781     {
782       DEBUG ("Failed to dup dbus daemon: %s",
783              error ? error->message : "no message");
784       g_clear_error (&error);
785     }
786
787   if (account_dialog_only)
788     {
789       account_manager = tp_account_manager_dup ();
790       show_accounts_ui (NULL, TRUE);
791
792       gtk_main ();
793
794       g_object_unref (account_manager);
795       return 0;
796     }
797
798   notify_init (_(PACKAGE_NAME));
799
800   /* Setting up Idle */
801   idle = empathy_idle_dup_singleton ();
802   empathy_idle_set_auto_away (idle, TRUE);
803
804   /* Setting up Connectivity */
805   connectivity = empathy_connectivity_dup_singleton ();
806   use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
807       connectivity);
808   empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
809       use_conn_notify_cb, connectivity);
810
811   /* account management */
812   account_manager = tp_account_manager_dup ();
813   tp_account_manager_prepare_async (account_manager, NULL,
814       account_manager_ready_cb, NULL);
815
816   /* Handle channels */
817   dispatcher = setup_dispatcher ();
818   g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
819
820   migrate_config_to_xdg_dir ();
821
822   /* Setting up UI */
823   window = empathy_main_window_show ();
824   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
825
826   g_signal_connect (unique_app, "message-received",
827       G_CALLBACK (unique_app_message_cb), window);
828
829   /* Logging */
830   log_manager = empathy_log_manager_dup_singleton ();
831   empathy_log_manager_observe (log_manager, dispatcher);
832
833   chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
834   empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
835
836   g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
837   if (!chatroom_manager_ready)
838     {
839       g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
840           G_CALLBACK (chatroom_manager_ready_cb), account_manager);
841     }
842   else
843     {
844       chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
845     }
846
847   /* Create the call factory */
848   call_factory = empathy_call_factory_initialise ();
849   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
850       G_CALLBACK (new_call_handler_cb), NULL);
851   /* Create the FT factory */
852   ft_factory = empathy_ft_factory_dup_singleton ();
853   g_signal_connect (ft_factory, "new-ft-handler",
854       G_CALLBACK (new_ft_handler_cb), NULL);
855   g_signal_connect (ft_factory, "new-incoming-transfer",
856       G_CALLBACK (new_incoming_transfer_cb), NULL);
857
858   /* Location mananger */
859 #if HAVE_GEOCLUE
860   location_manager = empathy_location_manager_dup_singleton ();
861 #endif
862
863   gtk_main ();
864
865   empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
866
867   g_object_unref (idle);
868   g_object_unref (connectivity);
869   g_object_unref (icon);
870   g_object_unref (account_manager);
871   g_object_unref (log_manager);
872   g_object_unref (dispatcher);
873   g_object_unref (chatroom_manager);
874 #if HAVE_GEOCLUE
875   g_object_unref (location_manager);
876 #endif
877   g_object_unref (ft_factory);
878   g_object_unref (unique_app);
879
880   notify_uninit ();
881   xmlCleanupParser ();
882
883   return EXIT_SUCCESS;
884 }