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