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