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