]> git.0d.be Git - empathy.git/blob - src/empathy.c
Make sure the account manager is completely unreffed when exiting
[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 <libebook/e-book.h>
39 #include <libnotify/notify.h>
40
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-account-manager.h>
53 #include <libempathy/empathy-connection-managers.h>
54 #include <libempathy/empathy-debugger.h>
55 #include <libempathy/empathy-dispatcher.h>
56 #include <libempathy/empathy-dispatch-operation.h>
57 #include <libempathy/empathy-log-manager.h>
58 #include <libempathy/empathy-ft-factory.h>
59 #include <libempathy/empathy-tp-chat.h>
60 #include <libempathy/empathy-tp-call.h>
61
62 #include <libempathy-gtk/empathy-conf.h>
63 #include <libempathy-gtk/empathy-ui-utils.h>
64 #include <libempathy-gtk/empathy-location-manager.h>
65
66 #include "empathy-account-assistant.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"
73 #include "empathy-import-mc4-accounts.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
87 static void
88 dispatch_cb (EmpathyDispatcher *dispatcher,
89     EmpathyDispatchOperation *operation,
90     gpointer user_data)
91 {
92   GQuark channel_type;
93
94   channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
95
96   if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
97     {
98       EmpathyTpChat *tp_chat;
99       EmpathyChat   *chat = NULL;
100       const gchar   *id;
101
102       tp_chat = EMPATHY_TP_CHAT
103         (empathy_dispatch_operation_get_channel_wrapper (operation));
104
105       id = empathy_tp_chat_get_id (tp_chat);
106       if (!EMP_STR_EMPTY (id))
107         {
108           EmpathyAccountManager *manager;
109           TpConnection *connection;
110           EmpathyAccount *account;
111
112           manager = empathy_account_manager_dup_singleton ();
113           connection = empathy_tp_chat_get_connection (tp_chat);
114           account = empathy_account_manager_get_account_for_connection (
115               manager, connection);
116           chat = empathy_chat_window_find_chat (account, id);
117           g_object_unref (manager);
118         }
119
120       if (chat)
121         empathy_chat_set_tp_chat (chat, tp_chat);
122       else
123         chat = empathy_chat_new (tp_chat);
124
125       empathy_chat_window_present_chat (chat);
126
127       empathy_dispatch_operation_claim (operation);
128     }
129   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
130     {
131       EmpathyCallFactory *factory;
132
133       factory = empathy_call_factory_get ();
134       empathy_call_factory_claim_channel (factory, operation);
135     }
136   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
137     {
138       EmpathyFTFactory *factory;
139
140       factory = empathy_ft_factory_dup_singleton ();
141
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.
145        */
146       if (empathy_dispatch_operation_is_incoming (operation))
147         empathy_ft_factory_claim_channel (factory, operation);
148     }
149 }
150
151 /* Salut account creation */
152 static gboolean
153 should_create_salut_account (void)
154 {
155   EmpathyAccountManager *manager;
156   gboolean salut_created = FALSE;
157   GList *accounts, *l;
158
159   /* Check if we already created a salut account */
160   empathy_conf_get_bool (empathy_conf_get (),
161       EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
162       &salut_created);
163
164   if (salut_created)
165     {
166       DEBUG ("Gconf says we already created a salut account once");
167       return FALSE;
168     }
169
170   manager = empathy_account_manager_dup_singleton ();
171   accounts = empathy_account_manager_dup_accounts (manager);
172
173   for (l = accounts; l != NULL;  l = g_list_next (l))
174     {
175       EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
176
177       if (!tp_strdiff (empathy_account_get_protocol (account), "local-xmpp"))
178         salut_created = TRUE;
179
180       g_object_unref (account);
181     }
182
183   g_object_unref (manager);
184
185   if (salut_created)
186     {
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,
190           TRUE);
191     }
192
193   return !salut_created;
194 }
195
196 static void
197 salut_account_created (GObject *source,
198     GAsyncResult *result,
199     gpointer user_data)
200 {
201   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
202   EmpathyAccount *account;
203   GError *error = NULL;
204
205   if (!empathy_account_settings_apply_finish (settings, result, &error))
206     {
207       DEBUG ("Failed to create salut account: %s", error->message);
208       g_error_free (error);
209       return;
210     }
211
212   account = empathy_account_settings_get_account (settings);
213
214   empathy_account_set_enabled_async (account, TRUE, NULL, NULL);
215   empathy_conf_set_bool (empathy_conf_get (),
216       EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
217       TRUE);
218 }
219
220 static void
221 use_conn_notify_cb (EmpathyConf *conf,
222     const gchar *key,
223     gpointer     user_data)
224 {
225   EmpathyConnectivity *connectivity = user_data;
226   gboolean     use_conn;
227
228   if (empathy_conf_get_bool (conf, key, &use_conn))
229     {
230       empathy_connectivity_set_use_conn (connectivity, use_conn);
231     }
232 }
233
234 static void
235 create_salut_account_if_needed (EmpathyConnectionManagers *managers)
236 {
237   EmpathyAccountSettings  *settings;
238   TpConnectionManager *manager;
239   const TpConnectionManagerProtocol *protocol;
240   EBook      *book;
241   EContact   *contact;
242   gchar      *nickname = NULL;
243   gchar      *first_name = NULL;
244   gchar      *last_name = NULL;
245   gchar      *email = NULL;
246   gchar      *jid = NULL;
247   GError     *error = NULL;
248
249
250   if (!should_create_salut_account ())
251     return;
252
253   manager = empathy_connection_managers_get_cm (managers, "salut");
254   if (manager == NULL)
255     {
256       DEBUG ("Salut not installed, not making a salut account");
257       return;
258     }
259
260   protocol = tp_connection_manager_get_protocol (manager, "local-xmpp");
261   if (protocol == NULL)
262     {
263       DEBUG ("Salut doesn't support local-xmpp!!");
264       return;
265     }
266
267   DEBUG ("Trying to add a salut account...");
268
269   /* Get self EContact from EDS */
270   if (!e_book_get_self (&contact, &book, &error))
271     {
272       DEBUG ("Failed to get self econtact: %s",
273           error ? error->message : "No error given");
274       g_clear_error (&error);
275       return;
276     }
277
278   settings = empathy_account_settings_new ("salut", "local-xmpp",
279       _("People nearby"));
280
281   nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
282   first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
283   last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
284   email = e_contact_get (contact, E_CONTACT_EMAIL_1);
285   jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
286
287   if (!tp_strdiff (nickname, "nickname"))
288     {
289       g_free (nickname);
290       nickname = NULL;
291     }
292
293   DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
294      "last-name=%s\nemail=%s\njid=%s\n",
295      nickname, first_name, last_name, email, jid);
296
297   empathy_account_settings_set_string (settings,
298       "nickname", nickname ? nickname : "");
299   empathy_account_settings_set_string (settings,
300       "first-name", first_name ? first_name : "");
301   empathy_account_settings_set_string (settings,
302       "last-name", last_name ? last_name : "");
303   empathy_account_settings_set_string (settings, "email", email ? email : "");
304   empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
305
306   empathy_account_settings_apply_async (settings,
307       salut_account_created, NULL);
308
309   g_free (nickname);
310   g_free (first_name);
311   g_free (last_name);
312   g_free (email);
313   g_free (jid);
314   g_object_unref (settings);
315   g_object_unref (contact);
316   g_object_unref (book);
317 }
318
319 static gboolean
320 has_non_salut_accounts (EmpathyAccountManager *manager)
321 {
322   gboolean ret = FALSE;
323   GList *accounts, *l;
324
325   accounts = empathy_account_manager_dup_accounts (manager);
326
327   for (l = accounts ; l != NULL; l = g_list_next (l))
328     {
329       EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
330
331       if (tp_strdiff (empathy_account_get_protocol (l->data), "local-xmpp"))
332         ret = TRUE;
333
334       g_object_unref (account);
335     }
336
337   g_list_free (accounts);
338
339   return ret;
340 }
341
342 static void
343 maybe_show_account_assistant (void)
344 {
345   EmpathyAccountManager *manager;
346   manager = empathy_account_manager_dup_singleton ();
347
348   if (!has_non_salut_accounts (manager))
349     empathy_account_assistant_show (GTK_WINDOW (empathy_main_window_get ()));
350 }
351
352 static gboolean
353 check_connection_managers_ready (EmpathyConnectionManagers *managers)
354 {
355   if (empathy_connection_managers_is_ready (managers))
356     {
357       if (!empathy_import_mc4_accounts (managers))
358         maybe_show_account_assistant ();
359
360       create_salut_account_if_needed (managers);
361       g_object_unref (managers);
362       managers = NULL;
363       return TRUE;
364     }
365   return FALSE;
366 }
367
368 static void
369 connection_managers_ready_cb (EmpathyConnectionManagers *managers,
370     GParamSpec *spec,
371     gpointer user_data)
372 {
373   check_connection_managers_ready (managers);
374 }
375
376 static void
377 migrate_config_to_xdg_dir (void)
378 {
379   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
380   int i;
381   GFile *xdg_file, *old_file;
382   static const gchar* filenames[] = {
383     "geometry.ini",
384     "irc-networks.xml",
385     "chatrooms.xml",
386     "contact-groups.xml",
387     "status-presets.xml",
388     "accels.txt",
389     NULL
390   };
391
392   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
393   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
394     {
395       /* xdg config dir already exists */
396       g_free (xdg_dir);
397       return;
398     }
399
400   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
401       PACKAGE_NAME, NULL);
402   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
403     {
404       /* old config dir didn't exist */
405       g_free (xdg_dir);
406       g_free (old_dir);
407       return;
408     }
409
410   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
411     {
412       DEBUG ("Failed to create configuration directory; aborting migration");
413       g_free (xdg_dir);
414       g_free (old_dir);
415       return;
416     }
417
418   for (i = 0; filenames[i]; i++)
419     {
420       old_filename = g_build_filename (old_dir, filenames[i], NULL);
421       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
422         {
423           g_free (old_filename);
424           continue;
425         }
426       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
427       old_file = g_file_new_for_path (old_filename);
428       xdg_file = g_file_new_for_path (xdg_filename);
429
430       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
431           NULL, NULL, NULL, NULL))
432         DEBUG ("Failed to migrate %s", filenames[i]);
433
434       g_free (old_filename);
435       g_free (xdg_filename);
436       g_object_unref (old_file);
437       g_object_unref (xdg_file);
438     }
439
440   g_free (xdg_dir);
441   g_free (old_dir);
442 }
443
444 static void
445 do_show_accounts_ui (GtkWindow *window, EmpathyAccountManager *manager)
446 {
447
448   GtkWidget *ui;
449
450   if (has_non_salut_accounts (manager))
451     ui = empathy_accounts_dialog_show (window, NULL);
452   else
453     ui = empathy_account_assistant_show (window);
454
455   if (account_dialog_only)
456     g_signal_connect (ui, "destroy",
457       G_CALLBACK (gtk_main_quit), NULL);
458 }
459
460 static void
461 account_manager_ready_for_accounts_cb (EmpathyAccountManager *manager,
462     GParamSpec *spec,
463     gpointer user_data)
464 {
465   if (!empathy_account_manager_is_ready (manager))
466     return;
467
468   do_show_accounts_ui (user_data, manager);
469 }
470
471 static void
472 show_accounts_ui (GtkWindow *window)
473 {
474   EmpathyAccountManager *manager;
475
476   manager = empathy_account_manager_dup_singleton ();
477   if (empathy_account_manager_is_ready (manager))
478     {
479       do_show_accounts_ui (window, manager);
480     }
481   else
482     {
483       g_signal_connect (manager, "notify::ready",
484         G_CALLBACK (account_manager_ready_for_accounts_cb), window);
485     }
486
487   g_object_unref (manager);
488 }
489
490 static UniqueResponse
491 unique_app_message_cb (UniqueApp *unique_app,
492     gint command,
493     UniqueMessageData *data,
494     guint timestamp,
495     gpointer user_data)
496 {
497   GtkWindow *window = user_data;
498
499   DEBUG ("Other instance launched, presenting the main window. "
500       "Command=%d, timestamp %u", command, timestamp);
501
502   if (command == COMMAND_ACCOUNTS_DIALOG)
503     {
504       show_accounts_ui (window);
505     }
506   else
507     {
508       gtk_window_set_screen (GTK_WINDOW (window),
509           unique_message_data_get_screen (data));
510       gtk_window_set_startup_id (GTK_WINDOW (window),
511           unique_message_data_get_startup_id (data));
512       gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
513     }
514
515   return UNIQUE_RESPONSE_OK;
516 }
517
518 static gboolean
519 show_version_cb (const char *option_name,
520     const char *value,
521     gpointer data,
522     GError **error)
523 {
524   g_print ("%s\n", PACKAGE_STRING);
525
526   exit (EXIT_SUCCESS);
527
528   return FALSE;
529 }
530
531 static void
532 new_incoming_transfer_cb (EmpathyFTFactory *factory,
533     EmpathyFTHandler *handler,
534     GError *error,
535     gpointer user_data)
536 {
537   if (error)
538     empathy_ft_manager_display_error (handler, error);
539   else
540     empathy_receive_file_with_file_chooser (handler);
541 }
542
543 static void
544 new_ft_handler_cb (EmpathyFTFactory *factory,
545     EmpathyFTHandler *handler,
546     GError *error,
547     gpointer user_data)
548 {
549   if (error)
550     empathy_ft_manager_display_error (handler, error);
551   else
552     empathy_ft_manager_add_handler (handler);
553
554   g_object_unref (handler);
555 }
556
557 static void
558 new_call_handler_cb (EmpathyCallFactory *factory,
559     EmpathyCallHandler *handler,
560     gboolean outgoing,
561     gpointer user_data)
562 {
563   EmpathyCallWindow *window;
564
565   window = empathy_call_window_new (handler);
566   gtk_widget_show (GTK_WIDGET (window));
567 }
568
569 #ifdef ENABLE_DEBUG
570 static void
571 default_log_handler (const gchar *log_domain,
572     GLogLevelFlags log_level,
573     const gchar *message,
574     gpointer user_data)
575 {
576   g_log_default_handler (log_domain, log_level, message, NULL);
577
578   /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
579    * debugger as they already have in empathy_debug. */
580   if (log_level != G_LOG_LEVEL_DEBUG
581       || tp_strdiff (log_domain, G_LOG_DOMAIN))
582     {
583         EmpathyDebugger *dbg;
584         GTimeVal now;
585
586         dbg = empathy_debugger_get_singleton ();
587         g_get_current_time (&now);
588
589         empathy_debugger_add_message (dbg, &now, log_domain,
590                                       log_level, message);
591     }
592 }
593 #endif /* ENABLE_DEBUG */
594
595 static void
596 account_manager_ready_cb (EmpathyAccountManager *manager,
597     GParamSpec *spec,
598     gpointer user_data)
599 {
600   if (!empathy_account_manager_is_ready (manager))
601     return;
602
603   if (should_create_salut_account () || !empathy_import_mc4_has_imported ())
604     {
605       EmpathyConnectionManagers *managers;
606       managers = empathy_connection_managers_dup_singleton ();
607
608       if (!check_connection_managers_ready (managers))
609         {
610           g_signal_connect (managers, "notify::ready",
611             G_CALLBACK (connection_managers_ready_cb), NULL);
612         }
613     }
614   else if (!start_hidden)
615     {
616       maybe_show_account_assistant ();
617     }
618 }
619
620 static EmpathyDispatcher *
621 setup_dispatcher (void)
622 {
623   EmpathyDispatcher *d;
624   GPtrArray *filters;
625   struct {
626     const gchar *channeltype;
627     TpHandleType handletype;
628   } types[] = {
629     /* Text channels with handle types none, contact and room */
630     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE  },
631     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT  },
632     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM  },
633     /* file transfer to contacts */
634     { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT  },
635     /* stream media to contacts */
636     { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT  },
637     /* stream tubes to contacts and rooms */
638     { TP_IFACE_CHANNEL_TYPE_STREAM_TUBE, TP_HANDLE_TYPE_CONTACT  },
639     { TP_IFACE_CHANNEL_TYPE_STREAM_TUBE, TP_HANDLE_TYPE_ROOM  },
640     /* d-bus tubes to contacts and rooms */
641     { TP_IFACE_CHANNEL_TYPE_DBUS_TUBE, TP_HANDLE_TYPE_CONTACT },
642     { TP_IFACE_CHANNEL_TYPE_DBUS_TUBE, TP_HANDLE_TYPE_ROOM  },
643     /* roomlists */
644     { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
645   };
646   gchar *capabilities[] = {
647     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
648     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
649     NULL };
650   GHashTable *asv;
651   int i;
652
653   /* Setup the basic Client.Handler that matches our client filter */
654   filters = g_ptr_array_new ();
655   asv = tp_asv_new (
656         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
657            TP_IFACE_CHANNEL_TYPE_TEXT,
658         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
659             TP_HANDLE_TYPE_CONTACT,
660         NULL);
661   g_ptr_array_add (filters, asv);
662
663   d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
664
665   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
666   g_ptr_array_free (filters, TRUE);
667
668   /* Setup the an extended Client.Handler that matches everything we can do */
669   filters = g_ptr_array_new ();
670   for (i = 0 ; i < G_N_ELEMENTS (types); i++)
671     {
672       asv = tp_asv_new (
673         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
674         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
675         NULL);
676
677       g_ptr_array_add (filters, asv);
678     }
679
680   asv = tp_asv_new (
681         TP_IFACE_CHANNEL ".ChannelType",
682           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
683         TP_IFACE_CHANNEL ".TargetHandleType",
684           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
685         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
686           G_TYPE_BOOLEAN, TRUE,
687         NULL);
688   g_ptr_array_add (filters, asv);
689
690   asv = tp_asv_new (
691         TP_IFACE_CHANNEL ".ChannelType",
692           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
693         TP_IFACE_CHANNEL ".TargetHandleType",
694           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
695         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
696           G_TYPE_BOOLEAN, TRUE,
697         NULL);
698   g_ptr_array_add (filters, asv);
699
700
701   empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
702     filters, capabilities);
703
704   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
705   g_ptr_array_free (filters, TRUE);
706
707   return d;
708 }
709
710 static void
711 account_connection_notify_cb (EmpathyAccount *account,
712     GParamSpec *pspec,
713     EmpathyChatroom *room)
714 {
715   TpConnection *conn;
716
717   conn = empathy_account_get_connection (account);
718
719   if (conn == NULL)
720     return;
721
722   empathy_dispatcher_join_muc (conn,
723       empathy_chatroom_get_room (room), NULL, NULL);
724 }
725
726 static void
727 account_manager_chatroom_ready_cb (EmpathyAccountManager *account_manager,
728     GParamSpec *pspec,
729     EmpathyChatroomManager *chatroom_manager)
730 {
731   GList *accounts, *l;
732
733   accounts = empathy_account_manager_dup_accounts (account_manager);
734
735   for (l = accounts; l != NULL; l = g_list_next (l))
736     {
737       EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
738       TpConnection *conn;
739       GList *chatrooms, *p;
740
741       conn = empathy_account_get_connection (account);
742
743       chatrooms = empathy_chatroom_manager_get_chatrooms (
744           chatroom_manager, account);
745
746       for (p = chatrooms; p != NULL; p = p->next)
747         {
748           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
749
750           if (!empathy_chatroom_get_auto_connect (room))
751             continue;
752
753           if (conn == NULL)
754             {
755               g_signal_connect (G_OBJECT (account), "notify::connection",
756                   G_CALLBACK (account_connection_notify_cb), room);
757             }
758           else
759             {
760               empathy_dispatcher_join_muc (conn,
761                   empathy_chatroom_get_room (room), NULL, NULL);
762             }
763         }
764
765       g_list_free (chatrooms);
766     }
767
768   g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
769   g_list_free (accounts);
770 }
771
772 static void
773 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
774     GParamSpec *pspec,
775     EmpathyAccountManager *account_manager)
776 {
777   gboolean ready;
778
779   g_object_get (G_OBJECT (account_manager), "ready", &ready, NULL);
780
781   if (ready)
782     {
783       account_manager_chatroom_ready_cb (account_manager, NULL,
784           chatroom_manager);
785     }
786   else
787     {
788       g_signal_connect (account_manager, "notify::ready",
789           G_CALLBACK (account_manager_chatroom_ready_cb), chatroom_manager);
790     }
791 }
792
793 int
794 main (int argc, char *argv[])
795 {
796 #if HAVE_GEOCLUE
797   EmpathyLocationManager *location_manager = NULL;
798 #endif
799   EmpathyStatusIcon *icon;
800   EmpathyDispatcher *dispatcher;
801   EmpathyAccountManager *account_manager;
802   EmpathyLogManager *log_manager;
803   EmpathyChatroomManager *chatroom_manager;
804   EmpathyCallFactory *call_factory;
805   EmpathyFTFactory  *ft_factory;
806   GtkWidget *window;
807   EmpathyIdle *idle;
808   EmpathyConnectivity *connectivity;
809   gboolean autoconnect = TRUE;
810   gboolean no_connect = FALSE;
811   GError *error = NULL;
812   TpDBusDaemon *dbus_daemon;
813   UniqueApp *unique_app;
814   gboolean chatroom_manager_ready;
815
816   GOptionContext *optcontext;
817   GOptionEntry options[] = {
818       { "no-connect", 'n',
819         0, G_OPTION_ARG_NONE, &no_connect,
820         N_("Don't connect on startup"),
821         NULL },
822       { "hide-contact-list", 'h',
823         0, G_OPTION_ARG_NONE, &start_hidden,
824         N_("Don't show the contact list on startup"),
825         NULL },
826       { "accounts", 'a',
827         0, G_OPTION_ARG_NONE, &account_dialog_only,
828         N_("Show the accounts dialog"),
829         NULL },
830       { "version", 'v',
831         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
832         NULL, NULL },
833       { NULL }
834   };
835
836   /* Init */
837   g_thread_init (NULL);
838   empathy_init ();
839
840   optcontext = g_option_context_new (N_("- Empathy IM Client"));
841   g_option_context_add_group (optcontext, gst_init_get_option_group ());
842   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
843   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
844
845   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
846     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
847         error->message, argv[0]);
848     g_warning ("Error in empathy init: %s", error->message);
849     return EXIT_FAILURE;
850   }
851
852   g_option_context_free (optcontext);
853
854   empathy_gtk_init ();
855   g_set_application_name (_(PACKAGE_NAME));
856   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
857
858 #if HAVE_LIBCHAMPLAIN
859   gtk_clutter_init (&argc, &argv);
860 #endif
861
862   gtk_window_set_default_icon_name ("empathy");
863   textdomain (GETTEXT_PACKAGE);
864
865 #ifdef ENABLE_DEBUG
866   /* Set up debugger */
867   g_log_set_default_handler (default_log_handler, NULL);
868 #endif
869
870   unique_app = unique_app_new_with_commands ("org.gnome.Empathy",
871       NULL, "accounts_dialog", COMMAND_ACCOUNTS_DIALOG, NULL);
872
873   if (unique_app_is_running (unique_app))
874     {
875       unique_app_send_message (unique_app, account_dialog_only ?
876           COMMAND_ACCOUNTS_DIALOG : UNIQUE_ACTIVATE, NULL);
877
878       g_object_unref (unique_app);
879       return EXIT_SUCCESS;
880     }
881
882   /* Take well-known name */
883   dbus_daemon = tp_dbus_daemon_dup (&error);
884   if (error == NULL)
885     {
886       if (!tp_dbus_daemon_request_name (dbus_daemon,
887           "org.gnome.Empathy", TRUE, &error))
888         {
889           DEBUG ("Failed to request well-known name: %s",
890                  error ? error->message : "no message");
891           g_clear_error (&error);
892         }
893       g_object_unref (dbus_daemon);
894     }
895   else
896     {
897       DEBUG ("Failed to dup dbus daemon: %s",
898              error ? error->message : "no message");
899       g_clear_error (&error);
900     }
901
902   if (account_dialog_only)
903     {
904       account_manager = empathy_account_manager_dup_singleton ();
905       show_accounts_ui (NULL);
906
907       gtk_main ();
908
909       g_object_unref (account_manager);
910       return 0;
911     }
912
913   notify_init (_(PACKAGE_NAME));
914
915   /* Setting up Idle */
916   idle = empathy_idle_dup_singleton ();
917   empathy_idle_set_auto_away (idle, TRUE);
918
919   /* Setting up Connectivity */
920   connectivity = empathy_connectivity_dup_singleton ();
921   use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
922       connectivity);
923   empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
924       use_conn_notify_cb, connectivity);
925
926   /* Autoconnect */
927   empathy_conf_get_bool (empathy_conf_get (),
928       EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
929   if (autoconnect && !no_connect &&
930       tp_connection_presence_type_cmp_availability
931           (empathy_idle_get_state (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
932             <= 0)
933       empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
934
935   /* account management */
936   account_manager = empathy_account_manager_dup_singleton ();
937   g_signal_connect (account_manager, "notify::ready",
938       G_CALLBACK (account_manager_ready_cb), NULL);
939
940   /* Handle channels */
941   dispatcher = setup_dispatcher ();
942   g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
943
944   migrate_config_to_xdg_dir ();
945
946   /* Setting up UI */
947   window = empathy_main_window_show ();
948   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
949
950   g_signal_connect (unique_app, "message-received",
951       G_CALLBACK (unique_app_message_cb), window);
952
953   /* Logging */
954   log_manager = empathy_log_manager_dup_singleton ();
955   empathy_log_manager_observe (log_manager, dispatcher);
956
957   chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
958   empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
959
960   g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
961   if (!chatroom_manager_ready)
962     {
963       g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
964           G_CALLBACK (chatroom_manager_ready_cb), account_manager);
965     }
966   else
967     {
968       chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
969     }
970
971   /* Create the call factory */
972   call_factory = empathy_call_factory_initialise ();
973   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
974       G_CALLBACK (new_call_handler_cb), NULL);
975   /* Create the FT factory */
976   ft_factory = empathy_ft_factory_dup_singleton ();
977   g_signal_connect (ft_factory, "new-ft-handler",
978       G_CALLBACK (new_ft_handler_cb), NULL);
979   g_signal_connect (ft_factory, "new-incoming-transfer",
980       G_CALLBACK (new_incoming_transfer_cb), NULL);
981
982   /* Location mananger */
983 #if HAVE_GEOCLUE
984   location_manager = empathy_location_manager_dup_singleton ();
985 #endif
986
987   gtk_main ();
988
989   empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
990
991   g_object_unref (idle);
992   g_object_unref (connectivity);
993   g_object_unref (icon);
994   g_object_unref (account_manager);
995   g_object_unref (log_manager);
996   g_object_unref (dispatcher);
997   g_object_unref (chatroom_manager);
998 #if HAVE_GEOCLUE
999   g_object_unref (location_manager);
1000 #endif
1001   g_object_unref (ft_factory);
1002   g_object_unref (unique_app);
1003
1004   notify_uninit ();
1005
1006   return EXIT_SUCCESS;
1007 }