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