]> git.0d.be Git - empathy.git/blob - src/empathy.c
Merge commit 'maiku/bug_598332'
[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) && !start_hidden)
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,
446     EmpathyAccountManager *manager)
447 {
448
449   GtkWidget *ui;
450
451   if (has_non_salut_accounts (manager))
452     ui = empathy_accounts_dialog_show (window, NULL);
453   else
454     ui = empathy_account_assistant_show (window);
455
456   if (account_dialog_only)
457     g_signal_connect (ui, "destroy",
458       G_CALLBACK (gtk_main_quit), NULL);
459 }
460
461 static void
462 account_manager_ready_for_accounts_cb (EmpathyAccountManager *manager,
463     GParamSpec *spec,
464     gpointer user_data)
465 {
466   if (!empathy_account_manager_is_ready (manager))
467     return;
468
469   do_show_accounts_ui (user_data, manager);
470 }
471
472 static void
473 show_accounts_ui (GtkWindow *window,
474     gboolean force)
475 {
476   EmpathyAccountManager *manager;
477
478   manager = empathy_account_manager_dup_singleton ();
479   if (empathy_account_manager_is_ready (manager))
480     {
481       if (force)
482         do_show_accounts_ui (window, manager);
483       else
484         maybe_show_account_assistant ();
485     }
486   else if (force)
487     {
488       /* Only if we we're forced to show the widget connect to ready, otherwise
489        * the initial readyness will cause the accounts ui to be shown when
490        * needed */
491       g_signal_connect (manager, "notify::ready",
492         G_CALLBACK (account_manager_ready_for_accounts_cb), window);
493     }
494
495   g_object_unref (manager);
496 }
497
498 static UniqueResponse
499 unique_app_message_cb (UniqueApp *unique_app,
500     gint command,
501     UniqueMessageData *data,
502     guint timestamp,
503     gpointer user_data)
504 {
505   GtkWindow *window = user_data;
506
507   DEBUG ("Other instance launched, presenting the main window. "
508       "Command=%d, timestamp %u", command, timestamp);
509
510   if (command == COMMAND_ACCOUNTS_DIALOG)
511     {
512       show_accounts_ui (window, TRUE);
513     }
514   else
515     {
516       /* We're requested to show stuff again, disable the start hidden global
517        * in case the accounts wizard wants to pop up.
518        */
519       start_hidden = FALSE;
520
521       show_accounts_ui (window, FALSE);
522
523       gtk_window_set_screen (GTK_WINDOW (window),
524           unique_message_data_get_screen (data));
525       gtk_window_set_startup_id (GTK_WINDOW (window),
526           unique_message_data_get_startup_id (data));
527       gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
528     }
529
530   return UNIQUE_RESPONSE_OK;
531 }
532
533 static gboolean
534 show_version_cb (const char *option_name,
535     const char *value,
536     gpointer data,
537     GError **error)
538 {
539   g_print ("%s\n", PACKAGE_STRING);
540
541   exit (EXIT_SUCCESS);
542
543   return FALSE;
544 }
545
546 static void
547 new_incoming_transfer_cb (EmpathyFTFactory *factory,
548     EmpathyFTHandler *handler,
549     GError *error,
550     gpointer user_data)
551 {
552   if (error)
553     empathy_ft_manager_display_error (handler, error);
554   else
555     empathy_receive_file_with_file_chooser (handler);
556 }
557
558 static void
559 new_ft_handler_cb (EmpathyFTFactory *factory,
560     EmpathyFTHandler *handler,
561     GError *error,
562     gpointer user_data)
563 {
564   if (error)
565     empathy_ft_manager_display_error (handler, error);
566   else
567     empathy_ft_manager_add_handler (handler);
568
569   g_object_unref (handler);
570 }
571
572 static void
573 new_call_handler_cb (EmpathyCallFactory *factory,
574     EmpathyCallHandler *handler,
575     gboolean outgoing,
576     gpointer user_data)
577 {
578   EmpathyCallWindow *window;
579
580   window = empathy_call_window_new (handler);
581   gtk_widget_show (GTK_WIDGET (window));
582 }
583
584 #ifdef ENABLE_DEBUG
585 static void
586 default_log_handler (const gchar *log_domain,
587     GLogLevelFlags log_level,
588     const gchar *message,
589     gpointer user_data)
590 {
591   g_log_default_handler (log_domain, log_level, message, NULL);
592
593   /* G_LOG_DOMAIN = "empathy". No need to send empathy messages to the
594    * debugger as they already have in empathy_debug. */
595   if (log_level != G_LOG_LEVEL_DEBUG
596       || tp_strdiff (log_domain, G_LOG_DOMAIN))
597     {
598         EmpathyDebugger *dbg;
599         GTimeVal now;
600
601         dbg = empathy_debugger_get_singleton ();
602         g_get_current_time (&now);
603
604         empathy_debugger_add_message (dbg, &now, log_domain,
605                                       log_level, message);
606     }
607 }
608 #endif /* ENABLE_DEBUG */
609
610 static void
611 account_manager_ready_cb (EmpathyAccountManager *manager,
612     GParamSpec *spec,
613     gpointer user_data)
614 {
615   if (!empathy_account_manager_is_ready (manager))
616     return;
617
618   if (should_create_salut_account () || !empathy_import_mc4_has_imported ())
619     {
620       EmpathyConnectionManagers *managers;
621       managers = empathy_connection_managers_dup_singleton ();
622
623       if (!check_connection_managers_ready (managers))
624         {
625           g_signal_connect (managers, "notify::ready",
626             G_CALLBACK (connection_managers_ready_cb), NULL);
627         }
628     }
629   else if (!start_hidden)
630     {
631       maybe_show_account_assistant ();
632     }
633 }
634
635 static EmpathyDispatcher *
636 setup_dispatcher (void)
637 {
638   EmpathyDispatcher *d;
639   GPtrArray *filters;
640   struct {
641     const gchar *channeltype;
642     TpHandleType handletype;
643   } types[] = {
644     /* Text channels with handle types none, contact and room */
645     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_NONE  },
646     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT  },
647     { TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM  },
648     /* file transfer to contacts */
649     { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT  },
650     /* stream media to contacts */
651     { TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT  },
652     /* stream tubes to contacts and rooms */
653     { TP_IFACE_CHANNEL_TYPE_STREAM_TUBE, TP_HANDLE_TYPE_CONTACT  },
654     { TP_IFACE_CHANNEL_TYPE_STREAM_TUBE, TP_HANDLE_TYPE_ROOM  },
655     /* d-bus tubes to contacts and rooms */
656     { TP_IFACE_CHANNEL_TYPE_DBUS_TUBE, TP_HANDLE_TYPE_CONTACT },
657     { TP_IFACE_CHANNEL_TYPE_DBUS_TUBE, TP_HANDLE_TYPE_ROOM  },
658     /* roomlists */
659     { TP_IFACE_CHANNEL_TYPE_ROOM_LIST, TP_HANDLE_TYPE_NONE },
660   };
661   gchar *capabilities[] = {
662     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
663     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
664     NULL };
665   GHashTable *asv;
666   int i;
667
668   /* Setup the basic Client.Handler that matches our client filter */
669   filters = g_ptr_array_new ();
670   asv = tp_asv_new (
671         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
672            TP_IFACE_CHANNEL_TYPE_TEXT,
673         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT,
674             TP_HANDLE_TYPE_CONTACT,
675         NULL);
676   g_ptr_array_add (filters, asv);
677
678   d = empathy_dispatcher_new (PACKAGE_NAME, filters, NULL);
679
680   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
681   g_ptr_array_free (filters, TRUE);
682
683   /* Setup the an extended Client.Handler that matches everything we can do */
684   filters = g_ptr_array_new ();
685   for (i = 0 ; i < G_N_ELEMENTS (types); i++)
686     {
687       asv = tp_asv_new (
688         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
689         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
690         NULL);
691
692       g_ptr_array_add (filters, asv);
693     }
694
695   asv = tp_asv_new (
696         TP_IFACE_CHANNEL ".ChannelType",
697           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
698         TP_IFACE_CHANNEL ".TargetHandleType",
699           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
700         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio",
701           G_TYPE_BOOLEAN, TRUE,
702         NULL);
703   g_ptr_array_add (filters, asv);
704
705   asv = tp_asv_new (
706         TP_IFACE_CHANNEL ".ChannelType",
707           G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
708         TP_IFACE_CHANNEL ".TargetHandleType",
709           G_TYPE_INT, TP_HANDLE_TYPE_CONTACT,
710         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo",
711           G_TYPE_BOOLEAN, TRUE,
712         NULL);
713   g_ptr_array_add (filters, asv);
714
715
716   empathy_dispatcher_add_handler (d, PACKAGE_NAME"MoreThanMeetsTheEye",
717     filters, capabilities);
718
719   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
720   g_ptr_array_free (filters, TRUE);
721
722   return d;
723 }
724
725 static void
726 account_connection_notify_cb (EmpathyAccount *account,
727     GParamSpec *pspec,
728     EmpathyChatroom *room)
729 {
730   TpConnection *conn;
731
732   conn = empathy_account_get_connection (account);
733
734   if (conn == NULL)
735     return;
736
737   empathy_dispatcher_join_muc (conn,
738       empathy_chatroom_get_room (room), NULL, NULL);
739 }
740
741 static void
742 account_manager_chatroom_ready_cb (EmpathyAccountManager *account_manager,
743     GParamSpec *pspec,
744     EmpathyChatroomManager *chatroom_manager)
745 {
746   GList *accounts, *l;
747
748   accounts = empathy_account_manager_dup_accounts (account_manager);
749
750   for (l = accounts; l != NULL; l = g_list_next (l))
751     {
752       EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
753       TpConnection *conn;
754       GList *chatrooms, *p;
755
756       conn = empathy_account_get_connection (account);
757
758       chatrooms = empathy_chatroom_manager_get_chatrooms (
759           chatroom_manager, account);
760
761       for (p = chatrooms; p != NULL; p = p->next)
762         {
763           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
764
765           if (!empathy_chatroom_get_auto_connect (room))
766             continue;
767
768           if (conn == NULL)
769             {
770               g_signal_connect (G_OBJECT (account), "notify::connection",
771                   G_CALLBACK (account_connection_notify_cb), room);
772             }
773           else
774             {
775               empathy_dispatcher_join_muc (conn,
776                   empathy_chatroom_get_room (room), NULL, NULL);
777             }
778         }
779
780       g_list_free (chatrooms);
781     }
782
783   g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
784   g_list_free (accounts);
785 }
786
787 static void
788 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
789     GParamSpec *pspec,
790     EmpathyAccountManager *account_manager)
791 {
792   gboolean ready;
793
794   g_object_get (G_OBJECT (account_manager), "ready", &ready, NULL);
795
796   if (ready)
797     {
798       account_manager_chatroom_ready_cb (account_manager, NULL,
799           chatroom_manager);
800     }
801   else
802     {
803       g_signal_connect (account_manager, "notify::ready",
804           G_CALLBACK (account_manager_chatroom_ready_cb), chatroom_manager);
805     }
806 }
807
808 int
809 main (int argc, char *argv[])
810 {
811 #if HAVE_GEOCLUE
812   EmpathyLocationManager *location_manager = NULL;
813 #endif
814   EmpathyStatusIcon *icon;
815   EmpathyDispatcher *dispatcher;
816   EmpathyAccountManager *account_manager;
817   EmpathyLogManager *log_manager;
818   EmpathyChatroomManager *chatroom_manager;
819   EmpathyCallFactory *call_factory;
820   EmpathyFTFactory  *ft_factory;
821   GtkWidget *window;
822   EmpathyIdle *idle;
823   EmpathyConnectivity *connectivity;
824   gboolean autoconnect = TRUE;
825   gboolean no_connect = FALSE;
826   GError *error = NULL;
827   TpDBusDaemon *dbus_daemon;
828   UniqueApp *unique_app;
829   gboolean chatroom_manager_ready;
830
831   GOptionContext *optcontext;
832   GOptionEntry options[] = {
833       { "no-connect", 'n',
834         0, G_OPTION_ARG_NONE, &no_connect,
835         N_("Don't connect on startup"),
836         NULL },
837       { "start-hidden", 'h',
838         0, G_OPTION_ARG_NONE, &start_hidden,
839         N_("Don't display the contact list or any other dialogs on startup"),
840         NULL },
841       { "accounts", 'a',
842         0, G_OPTION_ARG_NONE, &account_dialog_only,
843         N_("Show the accounts dialog"),
844         NULL },
845       { "version", 'v',
846         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
847         NULL, NULL },
848       { NULL }
849   };
850
851   /* Init */
852   g_thread_init (NULL);
853   empathy_init ();
854
855   optcontext = g_option_context_new (N_("- Empathy IM Client"));
856   g_option_context_add_group (optcontext, gst_init_get_option_group ());
857   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
858   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
859
860   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
861     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
862         error->message, argv[0]);
863     g_warning ("Error in empathy init: %s", error->message);
864     return EXIT_FAILURE;
865   }
866
867   g_option_context_free (optcontext);
868
869   empathy_gtk_init ();
870   g_set_application_name (_(PACKAGE_NAME));
871   g_setenv ("PULSE_PROP_media.role", "phone", TRUE);
872
873 #if HAVE_LIBCHAMPLAIN
874   gtk_clutter_init (&argc, &argv);
875 #endif
876
877   gtk_window_set_default_icon_name ("empathy");
878   textdomain (GETTEXT_PACKAGE);
879
880 #ifdef ENABLE_DEBUG
881   /* Set up debugger */
882   g_log_set_default_handler (default_log_handler, NULL);
883 #endif
884
885   unique_app = unique_app_new_with_commands ("org.gnome.Empathy",
886       NULL, "accounts_dialog", COMMAND_ACCOUNTS_DIALOG, NULL);
887
888   if (unique_app_is_running (unique_app))
889     {
890       unique_app_send_message (unique_app, account_dialog_only ?
891           COMMAND_ACCOUNTS_DIALOG : UNIQUE_ACTIVATE, NULL);
892
893       g_object_unref (unique_app);
894       return EXIT_SUCCESS;
895     }
896
897   /* Take well-known name */
898   dbus_daemon = tp_dbus_daemon_dup (&error);
899   if (error == NULL)
900     {
901       if (!tp_dbus_daemon_request_name (dbus_daemon,
902           "org.gnome.Empathy", TRUE, &error))
903         {
904           DEBUG ("Failed to request well-known name: %s",
905                  error ? error->message : "no message");
906           g_clear_error (&error);
907         }
908       g_object_unref (dbus_daemon);
909     }
910   else
911     {
912       DEBUG ("Failed to dup dbus daemon: %s",
913              error ? error->message : "no message");
914       g_clear_error (&error);
915     }
916
917   if (account_dialog_only)
918     {
919       account_manager = empathy_account_manager_dup_singleton ();
920       show_accounts_ui (NULL, TRUE);
921
922       gtk_main ();
923
924       g_object_unref (account_manager);
925       return 0;
926     }
927
928   notify_init (_(PACKAGE_NAME));
929
930   /* Setting up Idle */
931   idle = empathy_idle_dup_singleton ();
932   empathy_idle_set_auto_away (idle, TRUE);
933
934   /* Setting up Connectivity */
935   connectivity = empathy_connectivity_dup_singleton ();
936   use_conn_notify_cb (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
937       connectivity);
938   empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_USE_CONN,
939       use_conn_notify_cb, connectivity);
940
941   /* Autoconnect */
942   empathy_conf_get_bool (empathy_conf_get (),
943       EMPATHY_PREFS_AUTOCONNECT, &autoconnect);
944   if (autoconnect && !no_connect &&
945       tp_connection_presence_type_cmp_availability
946           (empathy_idle_get_state (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
947             <= 0)
948       empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
949
950   /* account management */
951   account_manager = empathy_account_manager_dup_singleton ();
952   g_signal_connect (account_manager, "notify::ready",
953       G_CALLBACK (account_manager_ready_cb), NULL);
954
955   /* Handle channels */
956   dispatcher = setup_dispatcher ();
957   g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
958
959   migrate_config_to_xdg_dir ();
960
961   /* Setting up UI */
962   window = empathy_main_window_show ();
963   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
964
965   g_signal_connect (unique_app, "message-received",
966       G_CALLBACK (unique_app_message_cb), window);
967
968   /* Logging */
969   log_manager = empathy_log_manager_dup_singleton ();
970   empathy_log_manager_observe (log_manager, dispatcher);
971
972   chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
973   empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
974
975   g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
976   if (!chatroom_manager_ready)
977     {
978       g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
979           G_CALLBACK (chatroom_manager_ready_cb), account_manager);
980     }
981   else
982     {
983       chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
984     }
985
986   /* Create the call factory */
987   call_factory = empathy_call_factory_initialise ();
988   g_signal_connect (G_OBJECT (call_factory), "new-call-handler",
989       G_CALLBACK (new_call_handler_cb), NULL);
990   /* Create the FT factory */
991   ft_factory = empathy_ft_factory_dup_singleton ();
992   g_signal_connect (ft_factory, "new-ft-handler",
993       G_CALLBACK (new_ft_handler_cb), NULL);
994   g_signal_connect (ft_factory, "new-incoming-transfer",
995       G_CALLBACK (new_incoming_transfer_cb), NULL);
996
997   /* Location mananger */
998 #if HAVE_GEOCLUE
999   location_manager = empathy_location_manager_dup_singleton ();
1000 #endif
1001
1002   gtk_main ();
1003
1004   empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1005
1006   g_object_unref (idle);
1007   g_object_unref (connectivity);
1008   g_object_unref (icon);
1009   g_object_unref (account_manager);
1010   g_object_unref (log_manager);
1011   g_object_unref (dispatcher);
1012   g_object_unref (chatroom_manager);
1013 #if HAVE_GEOCLUE
1014   g_object_unref (location_manager);
1015 #endif
1016   g_object_unref (ft_factory);
1017   g_object_unref (unique_app);
1018
1019   notify_uninit ();
1020
1021   return EXIT_SUCCESS;
1022 }