2
* Copyright (c) 2011 Red Hat, Inc.
4
* Gnome Documents is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by the
6
* Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
9
* Gnome Documents is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with Gnome Documents; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Author: Cosimo Cecchi <cosimoc@redhat.com>
23
#error "miner-main.c is meant to be included, not compiled standalone"
26
#include <glib-unix.h>
29
#define AUTOQUIT_TIMEOUT 5 /* seconds */
31
static const gchar introspection_xml[] =
33
" <interface name='org.gnome.Documents.Miner'>"
34
" <method name='RefreshDB'>"
39
static GDBusNodeInfo *introspection_data = NULL;
40
static GCancellable *cancellable = NULL;
41
static GMainLoop *loop = NULL;
42
static guint name_owner_id = 0;
43
static guint autoquit_id = 0;
44
static gboolean refreshing = FALSE;
47
autoquit_timeout_cb (gpointer _unused)
49
g_debug ("Timeout reached, quitting...");
52
g_main_loop_quit (loop);
58
ensure_autoquit_off (void)
60
if (g_getenv (MINER_NAME "MINER_PERSIST") != NULL)
65
g_source_remove (autoquit_id);
71
ensure_autoquit_on (void)
73
if (g_getenv (MINER_NAME "_MINER_PERSIST") != NULL)
77
g_timeout_add_seconds (AUTOQUIT_TIMEOUT,
78
autoquit_timeout_cb, NULL);
82
signal_handler_cb (gpointer user_data)
84
GMainLoop *loop = user_data;
86
if (cancellable != NULL)
87
g_cancellable_cancel (cancellable);
89
g_main_loop_quit (loop);
95
miner_refresh_db_ready_cb (GObject *source,
99
GDBusMethodInvocation *invocation = user_data;
100
GError *error = NULL;
102
gd_miner_refresh_db_finish (GD_MINER (source), res, &error);
105
ensure_autoquit_on ();
109
g_printerr ("Failed to refresh the DB cache: %s\n", error->message);
110
g_dbus_method_invocation_return_gerror (invocation, error);
114
g_dbus_method_invocation_return_value (invocation, NULL);
119
handle_refresh_db (GDBusMethodInvocation *invocation)
123
ensure_autoquit_off ();
125
/* if we're refreshing already, compress with the current request */
130
cancellable = g_cancellable_new ();
131
miner = g_object_new (MINER_TYPE, NULL);
133
gd_miner_refresh_db_async (miner, cancellable,
134
miner_refresh_db_ready_cb, invocation);
136
g_object_unref (miner);
140
handle_method_call (GDBusConnection *connection,
142
const gchar *object_path,
143
const gchar *interface_name,
144
const gchar *method_name,
145
GVariant *parameters,
146
GDBusMethodInvocation *invocation,
149
if (g_strcmp0 (method_name, "RefreshDB") == 0)
150
handle_refresh_db (g_object_ref (invocation));
152
g_assert_not_reached ();
155
static const GDBusInterfaceVTable interface_vtable =
158
NULL, /* get_property */
159
NULL, /* set_property */
163
on_bus_acquired (GDBusConnection *connection,
167
GError *error = NULL;
169
g_debug ("Connected to the session bus: %s", name);
171
g_dbus_connection_register_object (connection,
173
introspection_data->interfaces[0],
181
g_printerr ("Error exporting object on the session bus: %s",
183
g_error_free (error);
188
g_debug ("Object exported on the session bus");
192
on_name_lost (GDBusConnection *connection,
196
g_debug ("Lost bus name: %s, exiting", name);
198
if (cancellable != NULL)
199
g_cancellable_cancel (cancellable);
205
on_name_acquired (GDBusConnection *connection,
209
g_debug ("Acquired bus name: %s", name);
218
ensure_autoquit_on ();
219
loop = g_main_loop_new (NULL, FALSE);
221
g_unix_signal_add_full (G_PRIORITY_DEFAULT,
225
g_unix_signal_add_full (G_PRIORITY_DEFAULT,
230
introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
231
g_assert (introspection_data != NULL);
233
name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
235
G_BUS_NAME_OWNER_FLAGS_NONE,
241
g_main_loop_run (loop);
242
g_main_loop_unref (loop);
244
if (name_owner_id != 0)
245
g_bus_unown_name (name_owner_id);