]> git.0d.be Git - empathy.git/blob - src/empathy.c
Add noreturn annotation to version callback function
[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 #include <unique/unique.h>
34
35 #ifdef HAVE_LIBCHAMPLAIN
36 #include <clutter-gtk/clutter-gtk.h>
37 #endif
38
39 #include <libnotify/notify.h>
40
41 #include <telepathy-glib/account-manager.h>
42 #include <telepathy-glib/dbus.h>
43 #include <telepathy-glib/debug-sender.h>
44 #include <telepathy-glib/util.h>
45 #include <telepathy-glib/connection-manager.h>
46 #include <telepathy-glib/interfaces.h>
47
48 #include <telepathy-logger/log-manager.h>
49
50 #include <libempathy/empathy-idle.h>
51 #include <libempathy/empathy-utils.h>
52 #include <libempathy/empathy-chatroom-manager.h>
53 #include <libempathy/empathy-account-settings.h>
54 #include <libempathy/empathy-connectivity.h>
55 #include <libempathy/empathy-connection-managers.h>
56 #include <libempathy/empathy-dispatcher.h>
57 #include <libempathy/empathy-dispatch-operation.h>
58 #include <libempathy/empathy-ft-factory.h>
59 #include <libempathy/empathy-gsettings.h>
60 #include <libempathy/empathy-tp-chat.h>
61
62 #include <libempathy-gtk/empathy-ui-utils.h>
63 #include <libempathy-gtk/empathy-location-manager.h>
64
65 #include "empathy-main-window.h"
66 #include "empathy-accounts-common.h"
67 #include "empathy-accounts-dialog.h"
68 #include "empathy-chat-manager.h"
69 #include "empathy-status-icon.h"
70 #include "empathy-ft-manager.h"
71
72 #include "extensions/extensions.h"
73
74 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
75 #include <libempathy/empathy-debug.h>
76
77 static gboolean start_hidden = FALSE;
78 static gboolean no_connect = FALSE;
79
80 static void account_manager_ready_cb (GObject *source_object,
81     GAsyncResult *result,
82     gpointer user_data);
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_FILE_TRANSFER)
94     {
95       EmpathyFTFactory *factory;
96
97       factory = empathy_ft_factory_dup_singleton ();
98
99       /* if the operation is not incoming, don't claim it,
100        * as it might have been triggered by another client, and
101        * we are observing it.
102        */
103       if (empathy_dispatch_operation_is_incoming (operation))
104         empathy_ft_factory_claim_channel (factory, operation);
105     }
106 }
107
108 static void
109 use_conn_notify_cb (GSettings *gsettings,
110     const gchar *key,
111     gpointer     user_data)
112 {
113   EmpathyConnectivity *connectivity = user_data;
114
115   empathy_connectivity_set_use_conn (connectivity,
116       g_settings_get_boolean (gsettings, key));
117 }
118
119 static void
120 migrate_config_to_xdg_dir (void)
121 {
122   gchar *xdg_dir, *old_dir, *xdg_filename, *old_filename;
123   int i;
124   GFile *xdg_file, *old_file;
125   static const gchar* filenames[] = {
126     "geometry.ini",
127     "irc-networks.xml",
128     "chatrooms.xml",
129     "contact-groups.xml",
130     "status-presets.xml",
131     "accels.txt",
132     NULL
133   };
134
135   xdg_dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
136   if (g_file_test (xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
137     {
138       /* xdg config dir already exists */
139       g_free (xdg_dir);
140       return;
141     }
142
143   old_dir = g_build_filename (g_get_home_dir (), ".gnome2",
144       PACKAGE_NAME, NULL);
145   if (!g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
146     {
147       /* old config dir didn't exist */
148       g_free (xdg_dir);
149       g_free (old_dir);
150       return;
151     }
152
153   if (g_mkdir_with_parents (xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR)) == -1)
154     {
155       DEBUG ("Failed to create configuration directory; aborting migration");
156       g_free (xdg_dir);
157       g_free (old_dir);
158       return;
159     }
160
161   for (i = 0; filenames[i]; i++)
162     {
163       old_filename = g_build_filename (old_dir, filenames[i], NULL);
164       if (!g_file_test (old_filename, G_FILE_TEST_EXISTS))
165         {
166           g_free (old_filename);
167           continue;
168         }
169       xdg_filename = g_build_filename (xdg_dir, filenames[i], NULL);
170       old_file = g_file_new_for_path (old_filename);
171       xdg_file = g_file_new_for_path (xdg_filename);
172
173       if (!g_file_move (old_file, xdg_file, G_FILE_COPY_NONE,
174           NULL, NULL, NULL, NULL))
175         DEBUG ("Failed to migrate %s", filenames[i]);
176
177       g_free (old_filename);
178       g_free (xdg_filename);
179       g_object_unref (old_file);
180       g_object_unref (xdg_file);
181     }
182
183   g_free (xdg_dir);
184   g_free (old_dir);
185 }
186
187 static void
188 show_accounts_ui (GdkScreen *screen,
189     gboolean if_needed)
190 {
191   empathy_accounts_dialog_show_application (screen,
192       NULL, if_needed, start_hidden);
193 }
194
195 static UniqueResponse
196 unique_app_message_cb (UniqueApp *unique_app,
197     gint command,
198     UniqueMessageData *data,
199     guint timestamp,
200     gpointer user_data)
201 {
202   GtkWindow *window = user_data;
203   TpAccountManager *account_manager;
204
205   DEBUG ("Other instance launched, presenting the main window. "
206       "Command=%d, timestamp %u", command, timestamp);
207
208   /* XXX: the standalone app somewhat breaks this case, since
209    * communicating it would be a pain */
210
211   /* We're requested to show stuff again, disable the start hidden global
212    * in case the accounts wizard wants to pop up.
213    */
214   start_hidden = FALSE;
215
216   gtk_window_set_screen (GTK_WINDOW (window),
217       unique_message_data_get_screen (data));
218   gtk_window_set_startup_id (GTK_WINDOW (window),
219       unique_message_data_get_startup_id (data));
220   gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
221   gtk_window_set_skip_taskbar_hint (window, FALSE);
222
223   account_manager = tp_account_manager_dup ();
224   tp_account_manager_prepare_async (account_manager, NULL,
225       account_manager_ready_cb, NULL);
226   g_object_unref (account_manager);
227
228   return UNIQUE_RESPONSE_OK;
229 }
230
231 static gboolean show_version_cb (const char *option_name,
232     const char *value,
233     gpointer data,
234     GError **error) G_GNUC_NORETURN;
235
236 static gboolean
237 show_version_cb (const char *option_name,
238     const char *value,
239     gpointer data,
240     GError **error)
241 {
242   g_print ("%s\n", PACKAGE_STRING);
243
244   exit (EXIT_SUCCESS);
245 }
246
247 static void
248 new_incoming_transfer_cb (EmpathyFTFactory *factory,
249     EmpathyFTHandler *handler,
250     GError *error,
251     gpointer user_data)
252 {
253   if (error)
254     empathy_ft_manager_display_error (handler, error);
255   else
256     empathy_receive_file_with_file_chooser (handler);
257 }
258
259 static void
260 new_ft_handler_cb (EmpathyFTFactory *factory,
261     EmpathyFTHandler *handler,
262     GError *error,
263     gpointer user_data)
264 {
265   if (error)
266     empathy_ft_manager_display_error (handler, error);
267   else
268     empathy_ft_manager_add_handler (handler);
269
270   g_object_unref (handler);
271 }
272
273 static void
274 account_manager_ready_cb (GObject *source_object,
275     GAsyncResult *result,
276     gpointer user_data)
277 {
278   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
279   GError *error = NULL;
280   EmpathyIdle *idle;
281   EmpathyConnectivity *connectivity;
282   TpConnectionPresenceType presence;
283   GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
284
285   if (!tp_account_manager_prepare_finish (manager, result, &error))
286     {
287       DEBUG ("Failed to prepare account manager: %s", error->message);
288       g_error_free (error);
289       return;
290     }
291
292   /* Autoconnect */
293   idle = empathy_idle_dup_singleton ();
294   connectivity = empathy_connectivity_dup_singleton ();
295
296   presence = tp_account_manager_get_most_available_presence (manager, NULL,
297       NULL);
298
299   if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_AUTOCONNECT) &&
300       !no_connect &&
301       tp_connection_presence_type_cmp_availability
302           (presence, TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
303             <= 0)
304       /* if current state is Offline, then put it online */
305       empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
306
307   /* Pop up the accounts dialog if we don't have any account */
308   if (!empathy_accounts_has_accounts (manager))
309     show_accounts_ui (gdk_screen_get_default (), TRUE);
310
311   g_object_unref (idle);
312   g_object_unref (connectivity);
313   g_object_unref (gsettings);
314 }
315
316 static EmpathyDispatcher *
317 setup_dispatcher (void)
318 {
319   EmpathyDispatcher *d;
320   GPtrArray *filters;
321   struct {
322     const gchar *channeltype;
323     TpHandleType handletype;
324   } types[] = {
325     /* file transfer to contacts */
326     { TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT  },
327   };
328   GHashTable *asv;
329   guint i;
330
331   /* Setup the an extended Client.Handler that matches everything we can do */
332   filters = g_ptr_array_new ();
333   for (i = 0 ; i < G_N_ELEMENTS (types); i++)
334     {
335       asv = tp_asv_new (
336         TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, types[i].channeltype,
337         TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_INT, types[i].handletype,
338         NULL);
339
340       g_ptr_array_add (filters, asv);
341     }
342
343   d = empathy_dispatcher_new (PACKAGE_NAME"MoreThanMeetsTheEye", filters, NULL);
344
345   g_ptr_array_foreach (filters, (GFunc) g_hash_table_destroy, NULL);
346   g_ptr_array_free (filters, TRUE);
347
348   return d;
349 }
350
351 static void
352 account_status_changed_cb (TpAccount *account,
353     guint old_status,
354     guint new_status,
355     guint reason,
356     gchar *dbus_error_name,
357     GHashTable *details,
358     EmpathyChatroom *room)
359 {
360   TpConnection *conn;
361
362   conn = tp_account_get_connection (account);
363   if (conn == NULL)
364     return;
365
366   empathy_dispatcher_join_muc (conn,
367       empathy_chatroom_get_room (room), EMPATHY_DISPATCHER_NON_USER_ACTION);
368 }
369
370 static void
371 account_manager_chatroom_ready_cb (GObject *source_object,
372     GAsyncResult *result,
373     gpointer user_data)
374 {
375   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
376   EmpathyChatroomManager *chatroom_manager = user_data;
377   GList *accounts, *l;
378   GError *error = NULL;
379
380   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
381     {
382       DEBUG ("Failed to prepare account manager: %s", error->message);
383       g_error_free (error);
384       return;
385     }
386
387   accounts = tp_account_manager_get_valid_accounts (account_manager);
388
389   for (l = accounts; l != NULL; l = g_list_next (l))
390     {
391       TpAccount *account = TP_ACCOUNT (l->data);
392       TpConnection *conn;
393       GList *chatrooms, *p;
394
395       conn = tp_account_get_connection (account);
396
397       chatrooms = empathy_chatroom_manager_get_chatrooms (
398           chatroom_manager, account);
399
400       for (p = chatrooms; p != NULL; p = p->next)
401         {
402           EmpathyChatroom *room = EMPATHY_CHATROOM (p->data);
403
404           if (!empathy_chatroom_get_auto_connect (room))
405             continue;
406
407           if (conn == NULL)
408             {
409               g_signal_connect (G_OBJECT (account), "status-changed",
410                   G_CALLBACK (account_status_changed_cb), room);
411             }
412           else
413             {
414               empathy_dispatcher_join_muc (conn,
415                   empathy_chatroom_get_room (room),
416                   EMPATHY_DISPATCHER_NON_USER_ACTION);
417             }
418         }
419
420       g_list_free (chatrooms);
421     }
422
423   g_list_free (accounts);
424 }
425
426 static void
427 chatroom_manager_ready_cb (EmpathyChatroomManager *chatroom_manager,
428     GParamSpec *pspec,
429     gpointer user_data)
430 {
431   TpAccountManager *account_manager = user_data;
432
433   tp_account_manager_prepare_async (account_manager, NULL,
434       account_manager_chatroom_ready_cb, chatroom_manager);
435 }
436
437 static void
438 empathy_idle_set_auto_away_cb (GSettings *gsettings,
439                                 const gchar *key,
440                                 gpointer user_data)
441 {
442         EmpathyIdle *idle = user_data;
443
444         empathy_idle_set_auto_away (idle,
445       g_settings_get_boolean (gsettings, key));
446 }
447
448 int
449 main (int argc, char *argv[])
450 {
451 #ifdef HAVE_GEOCLUE
452   EmpathyLocationManager *location_manager = NULL;
453 #endif
454   EmpathyStatusIcon *icon;
455   EmpathyDispatcher *dispatcher;
456   TpAccountManager *account_manager;
457   TplLogManager *log_manager;
458   EmpathyChatroomManager *chatroom_manager;
459   EmpathyFTFactory  *ft_factory;
460   GtkWidget *window;
461   EmpathyIdle *idle;
462   EmpathyConnectivity *connectivity;
463   EmpathyChatManager *chat_manager;
464   GError *error = NULL;
465   UniqueApp *unique_app;
466   gboolean chatroom_manager_ready;
467   gboolean autoaway = TRUE;
468 #ifdef ENABLE_DEBUG
469   TpDebugSender *debug_sender;
470 #endif
471   GSettings *gsettings;
472
473   GOptionContext *optcontext;
474   GOptionEntry options[] = {
475       { "no-connect", 'n',
476         0, G_OPTION_ARG_NONE, &no_connect,
477         N_("Don't connect on startup"),
478         NULL },
479       { "start-hidden", 'h',
480         0, G_OPTION_ARG_NONE, &start_hidden,
481         N_("Don't display the contact list or any other dialogs on startup"),
482         NULL },
483       { "version", 'v',
484         G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, show_version_cb,
485         NULL, NULL },
486       { NULL }
487   };
488
489   /* Init */
490   g_thread_init (NULL);
491   empathy_init ();
492
493   optcontext = g_option_context_new (N_("- Empathy IM Client"));
494   g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
495 #ifdef HAVE_LIBCHAMPLAIN
496   g_option_context_add_group (optcontext, clutter_get_option_group ());
497 #endif
498   g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
499
500   if (!g_option_context_parse (optcontext, &argc, &argv, &error)) {
501     g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
502         error->message, argv[0]);
503     g_warning ("Error in empathy init: %s", error->message);
504     return EXIT_FAILURE;
505   }
506
507   g_option_context_free (optcontext);
508
509   empathy_gtk_init ();
510   g_set_application_name (_(PACKAGE_NAME));
511
512   gtk_window_set_default_icon_name ("empathy");
513   textdomain (GETTEXT_PACKAGE);
514
515 #ifdef ENABLE_DEBUG
516   /* Set up debug sender */
517   debug_sender = tp_debug_sender_dup ();
518   g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
519 #endif
520
521   unique_app = unique_app_new ("org.gnome."PACKAGE_NAME, NULL);
522
523   if (unique_app_is_running (unique_app))
524     {
525       unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
526
527       g_object_unref (unique_app);
528       return EXIT_SUCCESS;
529     }
530
531   notify_init (_(PACKAGE_NAME));
532
533   /* Setting up Idle */
534   idle = empathy_idle_dup_singleton ();
535
536   gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
537   autoaway = g_settings_get_boolean (gsettings, EMPATHY_PREFS_AUTOAWAY);
538
539   g_signal_connect (gsettings,
540       "changed::" EMPATHY_PREFS_AUTOAWAY,
541       G_CALLBACK (empathy_idle_set_auto_away_cb), idle);
542
543   empathy_idle_set_auto_away (idle, autoaway);
544
545   /* Setting up Connectivity */
546   connectivity = empathy_connectivity_dup_singleton ();
547   use_conn_notify_cb (gsettings, EMPATHY_PREFS_USE_CONN,
548       connectivity);
549   g_signal_connect (gsettings,
550       "changed::" EMPATHY_PREFS_USE_CONN,
551       G_CALLBACK (use_conn_notify_cb), connectivity);
552
553   /* account management */
554   account_manager = tp_account_manager_dup ();
555   tp_account_manager_prepare_async (account_manager, NULL,
556       account_manager_ready_cb, NULL);
557
558   /* Handle channels */
559   dispatcher = setup_dispatcher ();
560   g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
561
562   migrate_config_to_xdg_dir ();
563
564   /* Setting up UI */
565   window = empathy_main_window_dup ();
566   gtk_widget_show (window);
567   icon = empathy_status_icon_new (GTK_WINDOW (window), start_hidden);
568
569   /* Chat manager */
570   chat_manager = empathy_chat_manager_dup_singleton ();
571
572   g_signal_connect (unique_app, "message-received",
573       G_CALLBACK (unique_app_message_cb), window);
574
575   /* Logging */
576   log_manager = tpl_log_manager_dup_singleton ();
577
578   chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
579
580   g_object_get (chatroom_manager, "ready", &chatroom_manager_ready, NULL);
581   if (!chatroom_manager_ready)
582     {
583       g_signal_connect (G_OBJECT (chatroom_manager), "notify::ready",
584           G_CALLBACK (chatroom_manager_ready_cb), account_manager);
585     }
586   else
587     {
588       chatroom_manager_ready_cb (chatroom_manager, NULL, account_manager);
589     }
590
591   /* Create the FT factory */
592   ft_factory = empathy_ft_factory_dup_singleton ();
593   g_signal_connect (ft_factory, "new-ft-handler",
594       G_CALLBACK (new_ft_handler_cb), NULL);
595   g_signal_connect (ft_factory, "new-incoming-transfer",
596       G_CALLBACK (new_incoming_transfer_cb), NULL);
597
598   /* Location mananger */
599 #ifdef HAVE_GEOCLUE
600   location_manager = empathy_location_manager_dup_singleton ();
601 #endif
602
603   gtk_main ();
604
605   empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
606
607 #ifdef ENABLE_DEBUG
608   g_object_unref (debug_sender);
609 #endif
610
611   g_object_unref (chat_manager);
612   g_object_unref (idle);
613   g_object_unref (connectivity);
614   g_object_unref (icon);
615   g_object_unref (account_manager);
616   g_object_unref (log_manager);
617   g_object_unref (dispatcher);
618   g_object_unref (chatroom_manager);
619 #ifdef HAVE_GEOCLUE
620   g_object_unref (location_manager);
621 #endif
622   g_object_unref (ft_factory);
623   g_object_unref (unique_app);
624   g_object_unref (gsettings);
625   gtk_widget_destroy (window);
626
627   notify_uninit ();
628   xmlCleanupParser ();
629
630   return EXIT_SUCCESS;
631 }