]> git.0d.be Git - empathy.git/blob - src/empathy.c
Make the Empathy core program act as a unique app.
[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/gstdio.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkx.h>
33
34 #ifdef HAVE_LIBCHAMPLAIN
35 #include <clutter-gtk/clutter-gtk.h>
36 #endif
37
38 #include <libnotify/notify.h>
39
40 #include <telepathy-glib/account-manager.h>
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/debug-sender.h>
43 #include <telepathy-glib/util.h>
44 #include <telepathy-glib/connection-manager.h>
45 #include <telepathy-glib/interfaces.h>
46
47 #include <telepathy-logger/log-manager.h>
48
49 #include <libempathy/empathy-idle.h>
50 #include <libempathy/empathy-utils.h>
51 #include <libempathy/empathy-chatroom-manager.h>
52 #include <libempathy/empathy-account-settings.h>
53 #include <libempathy/empathy-connectivity.h>
54 #include <libempathy/empathy-connection-managers.h>
55 #include <libempathy/empathy-dispatcher.h>
56 #include <libempathy/empathy-ft-factory.h>
57 #include <libempathy/empathy-gsettings.h>
58 #include <libempathy/empathy-tp-chat.h>
59
60 #include <libempathy-gtk/empathy-ui-utils.h>
61 #include <libempathy-gtk/empathy-location-manager.h>
62
63 #include "empathy-main-window.h"
64 #include "empathy-accounts-common.h"
65 #include "empathy-accounts-dialog.h"
66 #include "empathy-status-icon.h"
67 #include "empathy-ft-manager.h"
68
69 #include "extensions/extensions.h"
70
71 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
72 #include <libempathy/empathy-debug.h>
73
74 #define EMPATHY_DBUS_NAME "org.gnome.Empathy"
75
76 #define EMPATHY_TYPE_APP (empathy_app_get_type ())
77 #define EMPATHY_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_APP, EmpathyApp))
78 #define EMPATHY_APP_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST ((obj), EMPATHY_TYPE_APP, EmpathyAppClass))
79 #define EMPATHY_IS_EMPATHY_APP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_APP))
80 #define EMPATHY_IS_EMPATHY_APP_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((obj), EMPATHY_TYPE_APP))
81 #define EMPATHY_APP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EMPATHY_TYPE_APP, EmpathyAppClass))
82
83 typedef struct _EmpathyApp EmpathyApp;
84 typedef struct _EmpathyAppClass EmpathyAppClass;
85
86 enum
87 {
88   PROP_NO_CONNECT = 1,
89   PROP_START_HIDDEN
90 };
91
92 GType empathy_app_get_type (void);
93
94 struct _EmpathyAppClass
95 {
96   GtkApplicationClass parent_class;
97 };
98
99 struct _EmpathyApp
100 {
101   GtkApplication parent;
102
103   /* Properties */
104   gboolean no_connect;
105   gboolean start_hidden;
106
107   GtkWidget *window;
108   EmpathyStatusIcon *icon;
109   EmpathyDispatcher *dispatcher;
110   TpAccountManager *account_manager;
111   TplLogManager *log_manager;
112   EmpathyChatroomManager *chatroom_manager;
113   EmpathyFTFactory  *ft_factory;
114   EmpathyIdle *idle;
115   EmpathyConnectivity *connectivity;
116   GSettings *gsettings;
117 #ifdef HAVE_GEOCLUE
118   EmpathyLocationManager *location_manager;
119 #endif
120 #ifdef ENABLE_DEBUG
121   TpDebugSender *debug_sender;
122 #endif
123 };
124
125
126 G_DEFINE_TYPE(EmpathyApp, empathy_app, GTK_TYPE_APPLICATION)
127
128 static void
129 empathy_app_dispose (GObject *object)
130 {
131   EmpathyApp *self = EMPATHY_APP (object);
132   void (*dispose) (GObject *) =
133     G_OBJECT_CLASS (empathy_app_parent_class)->dispose;
134
135   if (self->idle != NULL)
136     {
137       empathy_idle_set_state (self->idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
138     }
139
140 #ifdef ENABLE_DEBUG
141   tp_clear_object (&self->debug_sender);
142 #endif
143
144   tp_clear_object (&self->idle);
145   tp_clear_object (&self->connectivity);
146   tp_clear_object (&self->icon);
147   tp_clear_object (&self->account_manager);
148   tp_clear_object (&self->log_manager);
149   tp_clear_object (&self->dispatcher);
150   tp_clear_object (&self->chatroom_manager);
151 #ifdef HAVE_GEOCLUE
152   tp_clear_object (&self->location_manager);
153 #endif
154   tp_clear_object (&self->ft_factory);
155   tp_clear_object (&self->gsettings);
156
157   if (dispose != NULL)
158     dispose (object);
159 }
160
161 static void
162 empathy_app_finalize (GObject *object)
163 {
164   EmpathyApp *self = EMPATHY_APP (object);
165   void (*finalize) (GObject *) =
166     G_OBJECT_CLASS (empathy_app_parent_class)->finalize;
167
168   gtk_widget_destroy (self->window);
169
170   if (finalize != NULL)
171     finalize (object);
172 }
173
174 static void account_manager_ready_cb (GObject *source_object,
175     GAsyncResult *result,
176     gpointer user_data);
177
178 static EmpathyApp *
179 empathy_app_new (guint argc,
180     const gchar * const * argv,
181     gboolean no_connect,
182     gboolean start_hidden)
183 {
184   EmpathyApp *self;
185   GError *error = NULL;
186   GVariant *argv_variant;
187
188   argv_variant = g_variant_new_bytestring_array (argv, argc);
189
190   self = g_object_new (EMPATHY_TYPE_APP,
191       "application-id", EMPATHY_DBUS_NAME,
192       NULL);
193
194   if (self == NULL)
195     {
196       g_critical ("Failed to initiate EmpathyApp: %s", error->message);
197       g_error_free (error);
198     }
199
200   return self;
201 }
202
203 static void
204 empathy_app_set_property (GObject *object,
205     guint prop_id,
206     const GValue *value,
207     GParamSpec *pspec)
208 {
209   EmpathyApp *self = EMPATHY_APP (object);
210
211   switch (prop_id)
212     {
213       case PROP_NO_CONNECT:
214         self->no_connect = g_value_get_boolean (value);
215         break;
216       case PROP_START_HIDDEN:
217         self->start_hidden = g_value_get_boolean (value);
218         break;
219       default:
220         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221         break;
222     }
223 }
224
225 static void
226 empathy_app_activate (GApplication *app)
227 {
228   EmpathyApp *self = (EmpathyApp *) app;
229
230   /* We're requested to show stuff again, disable the start hidden global
231    * in case the accounts wizard wants to pop up.
232    */
233   self->start_hidden = FALSE;
234
235   g_application_hold (G_APPLICATION (app));
236
237   empathy_window_present (GTK_WINDOW (self->window));
238
239   /* Display the accounts dialog if needed */
240   tp_account_manager_prepare_async (self->account_manager, NULL,
241       account_manager_ready_cb, self);
242 }
243
244 static void empathy_app_constructed (GObject *object);
245
246 static void
247 empathy_app_class_init (EmpathyAppClass *klass)
248 {
249   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
250   GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
251   GParamSpec *spec;
252
253   gobject_class->set_property = empathy_app_set_property;
254   gobject_class->constructed = empathy_app_constructed;
255   gobject_class->dispose = empathy_app_dispose;
256   gobject_class->finalize = empathy_app_finalize;
257
258   g_app_class->activate = empathy_app_activate;
259
260   spec = g_param_spec_boolean ("no-connect", "no connect",
261       "Don't connect on startup",
262       FALSE,
263       G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
264   g_object_class_install_property (gobject_class, PROP_NO_CONNECT, spec);
265
266   spec = g_param_spec_boolean ("start-hidden", "start hidden",
267       "Don't display the contact list or any other dialogs on startup",
268       FALSE,
269       G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
270   g_object_class_install_property (gobject_class, PROP_START_HIDDEN, spec);
271 }
272
273 static void
274 empathy_app_init (EmpathyApp *self)
275 {
276 }
277
278 static void
279 use_conn_notify_cb (GSettings *gsettings,
280     const gchar *key,
281     gpointer     user_data)
282 {
283   EmpathyConnectivity *connectivity = user_data;
284
285   empathy_connectivity_set_use_conn (connectivity,
286       g_settings_get_boolean (gsettings, key));
287 }
288
289 static void
290 migrate_config_to_xdg_dir (void)
291 {
292   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
293   int i;
294   GFile *xdg_file, *old_file;
295   static const gchar* filenames[] = {
296     "geometry.ini",
297     "irc-networks.xml",
298     "chatrooms.xml",
299     "contact-groups.xml",
300     "status-presets.xml",
301     "accels.txt",
302     NULL
303   };
304
305   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
306   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
307     {
308       /* xdg config dir already exists */
309       g_free (xdg_dir);
310       return;
311     }
312
313   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
314       PACKAGE_NAME, NULL);
315   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
316     {
317       /* old config dir didn't exist */
318       g_free (xdg_dir);
319       g_free (old_dir);
320       return;
321     }
322
323   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
324     {
325       DEBUG ("Failed to create configuration directory; aborting migration");
326       g_free (xdg_dir);
327       g_free (old_dir);
328       return;
329     }
330
331   for (i = 0; filenames[i]; i++)
332     {
333       old_filename = g_build_filename (old_dir, filenames[i], NULL);
334       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
335         {
336           g_free (old_filename);
337           continue;
338         }
339       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
340       old_file = g_file_new_for_path (old_filename);
341       xdg_file = g_file_new_for_path (xdg_filename);
342
343       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
344           NULL, NULL, NULL, NULL))
345         DEBUG ("Failed to migrate %s", filenames[i]);
346
347       g_free (old_filename);
348       g_free (xdg_filename);
349       g_object_unref (old_file);
350       g_object_unref (xdg_file);
351     }
352
353   g_free (xdg_dir);
354   g_free (old_dir);
355 }
356
357 static void
358 show_accounts_ui (EmpathyApp *self,
359     GdkScreen *screen,
360     gboolean if_needed)
361 {
362   empathy_accounts_dialog_show_application (screen,
363       NULL, if_needed, self->start_hidden);
364 }
365
366 static gboolean
367 show_version_cb (const char *option_name,
368     const char *value,
369     gpointer data,
370     GError **error)
371 {
372   g_print ("%s\n", PACKAGE_STRING);
373
374   exit (EXIT_SUCCESS);
375 }
376
377 static void
378 new_incoming_transfer_cb (EmpathyFTFactory *factory,
379     EmpathyFTHandler *handler,
380     GError *error,
381     gpointer user_data)
382 {
383   if (error)
384     empathy_ft_manager_display_error (handler, error);
385   else
386     empathy_receive_file_with_file_chooser (handler);
387 }
388
389 static void
390 new_ft_handler_cb (EmpathyFTFactory *factory,
391     EmpathyFTHandler *handler,
392     GError *error,
393     gpointer user_data)
394 {
395   if (error)
396     empathy_ft_manager_display_error (handler, error);
397   else
398     empathy_ft_manager_add_handler (handler);
399
400   g_object_unref (handler);
401 }
402
403 static void
404 account_manager_ready_cb (GObject *source_object,
405     GAsyncResult *result,
406     gpointer user_data)
407 {
408   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
409   EmpathyApp *self = user_data;
410   GError *error = NULL;
411   TpConnectionPresenceType presence;
412
413   if (!tp_account_manager_prepare_finish (manager, result, &error))
414     {
415       GtkWidget *dialog;
416
417       DEBUG ("Failed to prepare account manager: %s", error->message);
418
419       dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
420           GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
421           _("Error contacting the Account Manager"));
422       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
423           _("There was an error while trying to connect to the Telepathy "
424             "Account Manager. The error was:\n\n%s"),
425           error->message);
426
427       gtk_dialog_run (GTK_DIALOG (dialog));
428       gtk_widget_destroy (dialog);
429
430       g_error_free (error);
431       return;
432     }
433
434   /* Autoconnect */
435   presence = tp_account_manager_get_most_available_presence (manager, NULL,
436       NULL);
437
438   if (g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
439       !self->no_connect &&
440       tp_connection_presence_type_cmp_availability
441           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
442             <= 0)
443       /* if current state is Offline, then put it online */
444       empathy_idle_set_state (self->idle,
445           TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
446
447   /* Pop up the accounts dialog if we don't have any account */
448   if (!empathy_accounts_has_accounts (manager))
449     show_accounts_ui (self, gdk_screen_get_default (), TRUE);
450 }
451
452 static void
453 account_status_changed_cb (TpAccount *account,
454     guint old_status,
455     guint new_status,
456     guint reason,
457     gchar *dbus_error_name,
458     GHashTable *details,
459     EmpathyChatroom *room)
460 {
461   if (new_status != TP_CONNECTION_STATUS_CONNECTED)
462     return;
463
464   empathy_dispatcher_join_muc (account,
465       empathy_chatroom_get_room (room), TP_USER_ACTION_TIME_NOT_USER_ACTION);
466 }
467
468 static void
469 account_manager_chatroom_ready_cb (GObject *source_object,
470     GAsyncResult *result,
471     gpointer user_data)
472 {
473   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
474   EmpathyChatroomManager *chatroom_manager = user_data;
475   GList *accounts, *l;
476   GError *error = NULL;
477
478   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
479     {
480       DEBUG ("Failed to prepare account manager: %s", error->message);
481       g_error_free (error);
482       return;
483     }
484
485   accounts = tp_account_manager_get_valid_accounts (account_manager);
486
487   for (l = accounts; l != NULL; l = g_list_next (l))
488     {
489       TpAccount *account = TP_ACCOUNT (l->data);
490       TpConnection *conn;
491       GList *chatrooms, *p;
492
493       conn = tp_account_get_connection (account);
494
495       chatrooms = empathy_chatroom_manager_get_chatrooms (
496           chatroom_manager, account);
497
498       for (p = chatrooms; p != NULL; p = p->next)
499         {
500           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
501
502           if (!empathy_chatroom_get_auto_connect (room))
503             continue;
504
505           if (conn == NULL)
506             {
507               g_signal_connect (G_OBJECT (account), "status-changed",
508                   G_CALLBACK (account_status_changed_cb), room);
509             }
510           else
511             {
512               empathy_dispatcher_join_muc (account,
513                   empathy_chatroom_get_room (room),
514                   TP_USER_ACTION_TIME_NOT_USER_ACTION);
515             }
516         }
517
518       g_list_free (chatrooms);
519     }
520
521   g_list_free (accounts);
522 }
523
524 static void
525 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
526     GParamSpec *pspec,
527     gpointer user_data)
528 {
529   TpAccountManager *account_manager = user_data;
530
531   tp_account_manager_prepare_async (account_manager, NULL,
532       account_manager_chatroom_ready_cb, chatroom_manager);
533 }
534
535 static void
536 empathy_idle_set_auto_away_cb (GSettings *gsettings,
537                                 const gchar *key,
538                                 gpointer user_data)
539 {
540         EmpathyIdle *idle = user_data;
541
542         empathy_idle_set_auto_away (idle,
543       g_settings_get_boolean (gsettings, key));
544 }
545
546 static void
547 empathy_app_constructed (GObject *object)
548 {
549   EmpathyApp *self = (EmpathyApp *) object;
550   GError *error = NULL;
551   gboolean chatroom_manager_ready;
552   gboolean autoaway;
553
554   g_set_application_name (_(PACKAGE_NAME));
555
556   gtk_window_set_default_icon_name ("empathy");
557   textdomain (GETTEXT_PACKAGE);
558
559 #ifdef ENABLE_DEBUG
560   /* Set up debug sender */
561   self->debug_sender = tp_debug_sender_dup ();
562   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
563 #endif
564
565   notify_init (_(PACKAGE_NAME));
566
567   /* Setting up Idle */
568   self->idle = empathy_idle_dup_singleton ();
569
570   self->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
571   autoaway = g_settings_get_boolean (self->gsettings, EMPATHY_PREFS_AUTOAWAY);
572
573   g_signal_connect (self->gsettings,
574       "changed::" EMPATHY_PREFS_AUTOAWAY,
575       G_CALLBACK (empathy_idle_set_auto_away_cb), self->idle);
576
577   empathy_idle_set_auto_away (self->idle, autoaway);
578
579   /* Setting up Connectivity */
580   self->connectivity = empathy_connectivity_dup_singleton ();
581   use_conn_notify_cb (self->gsettings, EMPATHY_PREFS_USE_CONN,
582       self->connectivity);
583   g_signal_connect (self->gsettings,
584       "changed::" EMPATHY_PREFS_USE_CONN,
585       G_CALLBACK (use_conn_notify_cb), self->connectivity);
586
587   /* account management */
588   self->account_manager = tp_account_manager_dup ();
589   tp_account_manager_prepare_async (self->account_manager, NULL,
590       account_manager_ready_cb, self);
591
592   /* The EmpathyDispatcher doesn't dispatch anything any more but we have to
593    * keep it around as we still use it to request channels */
594   self->dispatcher = empathy_dispatcher_dup_singleton ();
595
596   migrate_config_to_xdg_dir ();
597
598   /* Setting up UI */
599   self->window = empathy_main_window_dup ();
600   gtk_widget_show (self->window);
601   self->icon = empathy_status_icon_new (GTK_WINDOW (self->window),
602       self->start_hidden);
603
604   /* Logging */
605   self->log_manager = tpl_log_manager_dup_singleton ();
606
607   self->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
608
609   g_object_get (self->chatroom_manager, "ready", &chatroom_manager_ready, NULL);
610   if (!chatroom_manager_ready)
611     {
612       g_signal_connect (G_OBJECT (self->chatroom_manager), "notify::ready",
613           G_CALLBACK (chatroom_manager_ready_cb), self->account_manager);
614     }
615   else
616     {
617       chatroom_manager_ready_cb (self->chatroom_manager, NULL,
618           self->account_manager);
619     }
620
621   /* Create the FT factory */
622   self->ft_factory = empathy_ft_factory_dup_singleton ();
623   g_signal_connect (self->ft_factory, "new-ft-handler",
624       G_CALLBACK (new_ft_handler_cb), NULL);
625   g_signal_connect (self->ft_factory, "new-incoming-transfer",
626       G_CALLBACK (new_incoming_transfer_cb), NULL);
627
628   if (!empathy_ft_factory_register (self->ft_factory, &error))
629     {
630       g_warning ("Failed to register FileTransfer handler: %s", error->message);
631       g_error_free (error);
632     }
633
634   /* Location mananger */
635 #ifdef HAVE_GEOCLUE
636   self->location_manager = empathy_location_manager_dup_singleton ();
637 #endif
638 }
639
640 int
641 main (int argc, char *argv[])
642 {
643   EmpathyApp *app;
644   GError *error = NULL;
645   GOptionContext *optcontext;
646   gboolean no_connect = FALSE, start_hidden = FALSE;
647   GOptionEntry options[] = {
648       { "no-connect", 'n',
649         0, G_OPTION_ARG_NONE, &no_connect,
650         N_("Don't connect on startup"),
651         NULL },
652       { "start-hidden", 'h',
653         0, G_OPTION_ARG_NONE, &start_hidden,
654         N_("Don't display the contact list or any other dialogs on startup"),
655         NULL },
656       { "version", 'v',
657         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
658         NULL, NULL },
659       { NULL }
660   };
661
662   g_thread_init (NULL);
663   g_type_init ();
664
665 #ifdef HAVE_LIBCHAMPLAIN
666   gtk_clutter_init (&argc, &argv);
667 #endif
668
669   empathy_init ();
670
671   optcontext = g_option_context_new (N_("- Empathy IM Client"));
672   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
673   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
674
675   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
676     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
677         error->message, argv[0]);
678     g_warning ("Error in empathy init: %s", error->message);
679     return EXIT_FAILURE;
680   }
681
682   g_option_context_free (optcontext);
683
684   empathy_gtk_init ();
685
686   app = empathy_app_new (argc, (const gchar * const *) argv,
687       no_connect, start_hidden);
688
689   g_application_run (G_APPLICATION (app), argc, argv);
690
691   notify_uninit ();
692   xmlCleanupParser ();
693
694   g_object_unref (app);
695   return EXIT_SUCCESS;
696 }