]> git.0d.be Git - empathy.git/blob - src/empathy.c
move create_salut_account_if_needed and should_create_salut_account to a new file
[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 (managers);
212       g_object_unref (managers);
213       managers = NULL;
214       return TRUE;
215     }
216   return FALSE;
217 }
218
219 static void
220 connection_managers_ready_cb (EmpathyConnectionManagers *managers,
221     GParamSpec *spec,
222     gpointer user_data)
223 {
224   check_connection_managers_ready (managers);
225 }
226
227 static void
228 migrate_config_to_xdg_dir (void)
229 {
230   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
231   int i;
232   GFile *xdg_file, *old_file;
233   static const gchar* filenames[] = {
234     "geometry.ini",
235     "irc-networks.xml",
236     "chatrooms.xml",
237     "contact-groups.xml",
238     "status-presets.xml",
239     "accels.txt",
240     NULL
241   };
242
243   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
244   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
245     {
246       /* xdg config dir already exists */
247       g_free (xdg_dir);
248       return;
249     }
250
251   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
252       PACKAGE_NAME, NULL);
253   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
254     {
255       /* old config dir didn't exist */
256       g_free (xdg_dir);
257       g_free (old_dir);
258       return;
259     }
260
261   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
262     {
263       DEBUG ("Failed to create configuration directory; aborting migration");
264       g_free (xdg_dir);
265       g_free (old_dir);
266       return;
267     }
268
269   for (i = 0; filenames[i]; i++)
270     {
271       old_filename = g_build_filename (old_dir, filenames[i], NULL);
272       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
273         {
274           g_free (old_filename);
275           continue;
276         }
277       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
278       old_file = g_file_new_for_path (old_filename);
279       xdg_file = g_file_new_for_path (xdg_filename);
280
281       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
282           NULL, NULL, NULL, NULL))
283         DEBUG ("Failed to migrate %s", filenames[i]);
284
285       g_free (old_filename);
286       g_free (xdg_filename);
287       g_object_unref (old_file);
288       g_object_unref (xdg_file);
289     }
290
291   g_free (xdg_dir);
292   g_free (old_dir);
293 }
294
295 static void
296 do_show_accounts_ui (GtkWindow *window,
297     TpAccountManager *manager)
298 {
299
300   GtkWidget *ui;
301
302   if (has_non_salut_accounts (manager))
303     ui = empathy_accounts_dialog_show (window, NULL);
304   else
305     ui = empathy_account_assistant_show (window);
306
307   if (account_dialog_only)
308     g_signal_connect (ui, "destroy",
309       G_CALLBACK (gtk_main_quit), NULL);
310 }
311
312 static void
313 account_manager_ready_for_accounts_cb (GObject *source_object,
314     GAsyncResult *result,
315     gpointer user_data)
316 {
317   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
318   GError *error = NULL;
319
320   if (!tp_account_manager_prepare_finish (manager, result, &error))
321     {
322       DEBUG ("Failed to prepare account manager: %s", error->message);
323       g_error_free (error);
324       return;
325     }
326
327   do_show_accounts_ui (user_data, manager);
328 }
329
330 static void
331 show_accounts_ui (GtkWindow *window,
332     gboolean force)
333 {
334   TpAccountManager *manager;
335
336   if (!force)
337     return;
338
339   manager = tp_account_manager_dup ();
340
341   tp_account_manager_prepare_async (manager, NULL,
342       account_manager_ready_for_accounts_cb, window);
343
344   g_object_unref (manager);
345 }
346
347 static UniqueResponse
348 unique_app_message_cb (UniqueApp *unique_app,
349     gint command,
350     UniqueMessageData *data,
351     guint timestamp,
352     gpointer user_data)
353 {
354   GtkWindow *window = user_data;
355
356   DEBUG ("Other instance launched, presenting the main window. "
357       "Command=%d, timestamp %u", command, timestamp);
358
359   if (command == COMMAND_ACCOUNTS_DIALOG)
360     {
361       show_accounts_ui (NULL, TRUE);
362     }
363   else
364     {
365       /* We're requested to show stuff again, disable the start hidden global
366        * in case the accounts wizard wants to pop up.
367        */
368       start_hidden = FALSE;
369
370       show_accounts_ui (window, FALSE);
371
372       gtk_window_set_screen (GTK_WINDOW (window),
373           unique_message_data_get_screen (data));
374       gtk_window_set_startup_id (GTK_WINDOW (window),
375           unique_message_data_get_startup_id (data));
376       gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
377     }
378
379   return UNIQUE_RESPONSE_OK;
380 }
381
382 static gboolean
383 show_version_cb (const char *option_name,
384     const char *value,
385     gpointer data,
386     GError **error)
387 {
388   g_print ("%s\n", PACKAGE_STRING);
389
390   exit (EXIT_SUCCESS);
391
392   return FALSE;
393 }
394
395 static void
396 new_incoming_transfer_cb (EmpathyFTFactory *factory,
397     EmpathyFTHandler *handler,
398     GError *error,
399     gpointer user_data)
400 {
401   if (error)
402     empathy_ft_manager_display_error (handler, error);
403   else
404     empathy_receive_file_with_file_chooser (handler);
405 }
406
407 static void
408 new_ft_handler_cb (EmpathyFTFactory *factory,
409     EmpathyFTHandler *handler,
410     GError *error,
411     gpointer user_data)
412 {
413   if (error)
414     empathy_ft_manager_display_error (handler, error);
415   else
416     empathy_ft_manager_add_handler (handler);
417
418   g_object_unref (handler);
419 }
420
421 static void
422 new_call_handler_cb (EmpathyCallFactory *factory,
423     EmpathyCallHandler *handler,
424     gboolean outgoing,
425     gpointer user_data)
426 {
427   EmpathyCallWindow *window;
428
429   window = empathy_call_window_new (handler);
430   gtk_widget_show (GTK_WIDGET (window));
431 }
432
433 #ifdef ENABLE_DEBUG
434 static void
435 default_log_handler (const gchar *log_domain,
436     GLogLevelFlags log_level,
437     const gchar *message,
438     gpointer user_data)
439 {
440   g_log_default_handler (log_domain, log_level, message, NULL);
441
442   /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
443    * debugger as they already have in empathy_debug. */
444   if (log_level != G_LOG_LEVEL_DEBUG
445       || tp_strdiff (log_domain, G_LOG_DOMAIN))
446     {
447         EmpathyDebugger *dbg;
448         GTimeVal now;
449
450         dbg = empathy_debugger_get_singleton ();
451         g_get_current_time (&now);
452
453         empathy_debugger_add_message (dbg, &now, log_domain,
454                                       log_level, message);
455     }
456 }
457 #endif /* ENABLE_DEBUG */
458
459 static void
460 account_manager_ready_cb (GObject *source_object,
461     GAsyncResult *result,
462     gpointer user_data)
463 {
464   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
465   GError *error = NULL;
466   EmpathyIdle *idle;
467   EmpathyConnectivity *connectivity;
468   gboolean autoconnect = TRUE;
469   TpConnectionPresenceType presence;
470
471   if (!tp_account_manager_prepare_finish (manager, result, &error))
472     {
473       DEBUG ("Failed to prepare account manager: %s", error->message);
474       g_error_free (error);
475       return;
476     }
477
478   /* Autoconnect */
479   idle = empathy_idle_dup_singleton ();
480   connectivity = empathy_connectivity_dup_singleton ();
481
482   presence = tp_account_manager_get_most_available_presence (manager, NULL,
483       NULL);
484
485   empathy_conf_get_bool (empathy_conf_get (),
486       EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
487   if (autoconnect && !no_connect &&
488       tp_connection_presence_type_cmp_availability
489           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
490             <= 0)
491       /* if current state is Offline, then put it online */
492       empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
493
494   if (should_create_salut_account (manager)
495       || !empathy_import_mc4_has_imported ())
496     {
497       EmpathyConnectionManagers *managers;
498       managers = empathy_connection_managers_dup_singleton ();
499
500       if (!check_connection_managers_ready (managers))
501         {
502           g_signal_connect (managers, "notify::ready",
503             G_CALLBACK (connection_managers_ready_cb), NULL);
504         }
505     }
506   else if (!start_hidden)
507     {
508       maybe_show_account_assistant ();
509     }
510
511   g_object_unref (idle);
512   g_object_unref (connectivity);
513 }
514
515 static EmpathyDispatcher *
516 setup_dispatcher (void)
517 {
518   EmpathyDispatcher *d;
519   GPtrArray *filters;
520   struct {
521     const gchar *channeltype;
522     TpHandleType handletype;
523   } types[] = {
524     /* Text channels with handle types none, contact and room */
525     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE  },
526     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT  },
527     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM  },
528     /* file transfer to contacts */
529     { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT  },
530     /* stream media to contacts */
531     { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT  },
532     /* roomlists */
533     { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
534   };
535   gchar *capabilities[] = {
536     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
537     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
538     NULL };
539   GHashTable *asv;
540   guint i;
541
542   /* Setup the basic Client.Handler that matches our client filter */
543   filters = g_ptr_array_new ();
544   asv = tp_asv_new (
545         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
546            TP_IFACE_CHANNEL_TYPE_TEXT,
547         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
548             TP_HANDLE_TYPE_CONTACT,
549         NULL);
550   g_ptr_array_add (filters, asv);
551
552   d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
553
554   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
555   g_ptr_array_free (filters, TRUE);
556
557   /* Setup the an extended Client.Handler that matches everything we can do */
558   filters = g_ptr_array_new ();
559   for (i = 0 ; i < G_N_ELEMENTS (types); i++)
560     {
561       asv = tp_asv_new (
562         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
563         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
564         NULL);
565
566       g_ptr_array_add (filters, asv);
567     }
568
569   asv = tp_asv_new (
570         TP_IFACE_CHANNEL ".ChannelType",
571           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
572         TP_IFACE_CHANNEL ".TargetHandleType",
573           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
574         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
575           G_TYPE_BOOLEAN, TRUE,
576         NULL);
577   g_ptr_array_add (filters, asv);
578
579   asv = tp_asv_new (
580         TP_IFACE_CHANNEL ".ChannelType",
581           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
582         TP_IFACE_CHANNEL ".TargetHandleType",
583           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
584         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
585           G_TYPE_BOOLEAN, TRUE,
586         NULL);
587   g_ptr_array_add (filters, asv);
588
589
590   empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
591     filters, capabilities);
592
593   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
594   g_ptr_array_free (filters, TRUE);
595
596   return d;
597 }
598
599 static void
600 account_status_changed_cb (TpAccount *account,
601     guint old_status,
602     guint new_status,
603     guint reason,
604     gchar *dbus_error_name,
605     GHashTable *details,
606     EmpathyChatroom *room)
607 {
608   TpConnection *conn;
609
610   conn = tp_account_get_connection (account);
611
612   empathy_dispatcher_join_muc (conn,
613       empathy_chatroom_get_room (room), NULL, NULL);
614 }
615
616 static void
617 account_manager_chatroom_ready_cb (GObject *source_object,
618     GAsyncResult *result,
619     gpointer user_data)
620 {
621   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
622   EmpathyChatroomManager *chatroom_manager = user_data;
623   GList *accounts, *l;
624   GError *error = NULL;
625
626   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
627     {
628       DEBUG ("Failed to prepare account manager: %s", error->message);
629       g_error_free (error);
630       return;
631     }
632
633   accounts = tp_account_manager_get_valid_accounts (account_manager);
634
635   for (l = accounts; l != NULL; l = g_list_next (l))
636     {
637       TpAccount *account = TP_ACCOUNT (l->data);
638       TpConnection *conn;
639       GList *chatrooms, *p;
640
641       conn = tp_account_get_connection (account);
642
643       chatrooms = empathy_chatroom_manager_get_chatrooms (
644           chatroom_manager, account);
645
646       for (p = chatrooms; p != NULL; p = p->next)
647         {
648           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
649
650           if (!empathy_chatroom_get_auto_connect (room))
651             continue;
652
653           if (conn == NULL)
654             {
655               g_signal_connect (G_OBJECT (account), "status-changed",
656                   G_CALLBACK (account_status_changed_cb), room);
657             }
658           else
659             {
660               empathy_dispatcher_join_muc (conn,
661                   empathy_chatroom_get_room (room), NULL, NULL);
662             }
663         }
664
665       g_list_free (chatrooms);
666     }
667
668   g_list_free (accounts);
669 }
670
671 static void
672 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
673     GParamSpec *pspec,
674     gpointer user_data)
675 {
676   TpAccountManager *account_manager = user_data;
677
678   tp_account_manager_prepare_async (account_manager, NULL,
679       account_manager_chatroom_ready_cb, chatroom_manager);
680 }
681
682 int
683 main (int argc, char *argv[])
684 {
685 #if HAVE_GEOCLUE
686   EmpathyLocationManager *location_manager = NULL;
687 #endif
688   EmpathyStatusIcon *icon;
689   EmpathyDispatcher *dispatcher;
690   TpAccountManager *account_manager;
691   EmpathyLogManager *log_manager;
692   EmpathyChatroomManager *chatroom_manager;
693   EmpathyCallFactory *call_factory;
694   EmpathyFTFactory  *ft_factory;
695   GtkWidget *window;
696   EmpathyIdle *idle;
697   EmpathyConnectivity *connectivity;
698   GError *error = NULL;
699   TpDBusDaemon *dbus_daemon;
700   UniqueApp *unique_app;
701   gboolean chatroom_manager_ready;
702
703   GOptionContext *optcontext;
704   GOptionEntry options[] = {
705       { "no-connect", 'n',
706         0, G_OPTION_ARG_NONE, &no_connect,
707         N_("Don't connect on startup"),
708         NULL },
709       { "start-hidden", 'h',
710         0, G_OPTION_ARG_NONE, &start_hidden,
711         N_("Don't display the contact list or any other dialogs on startup"),
712         NULL },
713       { "accounts", 'a',
714         0, G_OPTION_ARG_NONE, &account_dialog_only,
715         N_("Show the accounts dialog"),
716         NULL },
717       { "version", 'v',
718         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
719         NULL, NULL },
720       { NULL }
721   };
722
723   /* Init */
724   g_thread_init (NULL);
725   empathy_init ();
726
727   optcontext = g_option_context_new (N_("- Empathy IM Client"));
728   g_option_context_add_group (optcontext, gst_init_get_option_group ());
729   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
730 #if HAVE_LIBCHAMPLAIN
731   g_option_context_add_group (optcontext, clutter_get_option_group ());
732 #endif
733   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
734
735   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
736     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
737         error->message, argv[0]);
738     g_warning ("Error in empathy init: %s", error->message);
739     return EXIT_FAILURE;
740   }
741
742   g_option_context_free (optcontext);
743
744   empathy_gtk_init ();
745   g_set_application_name (_(PACKAGE_NAME));
746   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
747
748   gtk_window_set_default_icon_name ("empathy");
749   textdomain (GETTEXT_PACKAGE);
750
751 #ifdef ENABLE_DEBUG
752   /* Set up debugger */
753   g_log_set_default_handler (default_log_handler, NULL);
754 #endif
755
756   unique_app = unique_app_new_with_commands ("org.gnome.Empathy",
757       NULL, "accounts_dialog", COMMAND_ACCOUNTS_DIALOG, NULL);
758
759   if (unique_app_is_running (unique_app))
760     {
761       unique_app_send_message (unique_app, account_dialog_only ?
762           COMMAND_ACCOUNTS_DIALOG : UNIQUE_ACTIVATE, NULL);
763
764       g_object_unref (unique_app);
765       return EXIT_SUCCESS;
766     }
767
768   /* Take well-known name */
769   dbus_daemon = tp_dbus_daemon_dup (&error);
770   if (error == NULL)
771     {
772       if (!tp_dbus_daemon_request_name (dbus_daemon,
773           "org.gnome.Empathy", TRUE, &error))
774         {
775           DEBUG ("Failed to request well-known name: %s",
776                  error ? error->message : "no message");
777           g_clear_error (&error);
778         }
779       g_object_unref (dbus_daemon);
780     }
781   else
782     {
783       DEBUG ("Failed to dup dbus daemon: %s",
784              error ? error->message : "no message");
785       g_clear_error (&error);
786     }
787
788   if (account_dialog_only)
789     {
790       account_manager = tp_account_manager_dup ();
791       show_accounts_ui (NULL, TRUE);
792
793       gtk_main ();
794
795       g_object_unref (account_manager);
796       return 0;
797     }
798
799   notify_init (_(PACKAGE_NAME));
800
801   /* Setting up Idle */
802   idle = empathy_idle_dup_singleton ();
803   empathy_idle_set_auto_away (idle, TRUE);
804
805   /* Setting up Connectivity */
806   connectivity = empathy_connectivity_dup_singleton ();
807   use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
808       connectivity);
809   empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
810       use_conn_notify_cb, connectivity);
811
812   /* account management */
813   account_manager = tp_account_manager_dup ();
814   tp_account_manager_prepare_async (account_manager, NULL,
815       account_manager_ready_cb, NULL);
816
817   /* Handle channels */
818   dispatcher = setup_dispatcher ();
819   g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
820
821   migrate_config_to_xdg_dir ();
822
823   /* Setting up UI */
824   window = empathy_main_window_show ();
825   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
826
827   g_signal_connect (unique_app, "message-received",
828       G_CALLBACK (unique_app_message_cb), window);
829
830   /* Logging */
831   log_manager = empathy_log_manager_dup_singleton ();
832   empathy_log_manager_observe (log_manager, dispatcher);
833
834   chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
835   empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
836
837   g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
838   if (!chatroom_manager_ready)
839     {
840       g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
841           G_CALLBACK (chatroom_manager_ready_cb), account_manager);
842     }
843   else
844     {
845       chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
846     }
847
848   /* Create the call factory */
849   call_factory = empathy_call_factory_initialise ();
850   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
851       G_CALLBACK (new_call_handler_cb), NULL);
852   /* Create the FT factory */
853   ft_factory = empathy_ft_factory_dup_singleton ();
854   g_signal_connect (ft_factory, "new-ft-handler",
855       G_CALLBACK (new_ft_handler_cb), NULL);
856   g_signal_connect (ft_factory, "new-incoming-transfer",
857       G_CALLBACK (new_incoming_transfer_cb), NULL);
858
859   /* Location mananger */
860 #if HAVE_GEOCLUE
861   location_manager = empathy_location_manager_dup_singleton ();
862 #endif
863
864   gtk_main ();
865
866   empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
867
868   g_object_unref (idle);
869   g_object_unref (connectivity);
870   g_object_unref (icon);
871   g_object_unref (account_manager);
872   g_object_unref (log_manager);
873   g_object_unref (dispatcher);
874   g_object_unref (chatroom_manager);
875 #if HAVE_GEOCLUE
876   g_object_unref (location_manager);
877 #endif
878   g_object_unref (ft_factory);
879   g_object_unref (unique_app);
880
881   notify_uninit ();
882   xmlCleanupParser ();
883
884   return EXIT_SUCCESS;
885 }