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