~ubuntu-branches/ubuntu/quantal/gnome-documents/quantal

« back to all changes in this revision

Viewing changes to src/miner/miner-main.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-08-07 22:03:19 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120807220319-cwvu1sb3x9lo9osb
Tags: 0.5.5-0ubuntu1
* New upstream release.
* debian/control.in:
  - Bump minimum libgdata 
  - Drop explicit liboauth-dev from build-depends to match
    configure.ac change

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2011 Red Hat, Inc.
 
3
 *
 
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.
 
8
 *
 
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
 
12
 * for more details.
 
13
 *
 
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
 
17
 *
 
18
 * Author: Cosimo Cecchi <cosimoc@redhat.com>
 
19
 *
 
20
 */
 
21
 
 
22
#ifndef INSIDE_MINER
 
23
#error "miner-main.c is meant to be included, not compiled standalone"
 
24
#endif
 
25
 
 
26
#include <glib-unix.h>
 
27
#include <glib.h>
 
28
 
 
29
#define AUTOQUIT_TIMEOUT 5 /* seconds */
 
30
 
 
31
static const gchar introspection_xml[] =
 
32
  "<node>"
 
33
  "  <interface name='org.gnome.Documents.Miner'>"
 
34
  "    <method name='RefreshDB'>"
 
35
  "    </method>"
 
36
  "  </interface>"
 
37
  "</node>";
 
38
 
 
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;
 
45
 
 
46
static gboolean
 
47
autoquit_timeout_cb (gpointer _unused)
 
48
{
 
49
  g_debug ("Timeout reached, quitting...");
 
50
 
 
51
  autoquit_id = 0;
 
52
  g_main_loop_quit (loop);
 
53
 
 
54
  return FALSE;
 
55
}
 
56
 
 
57
static void
 
58
ensure_autoquit_off (void)
 
59
{
 
60
  if (g_getenv (MINER_NAME "MINER_PERSIST") != NULL)
 
61
    return;
 
62
 
 
63
  if (autoquit_id != 0)
 
64
    {
 
65
      g_source_remove (autoquit_id);
 
66
      autoquit_id = 0;
 
67
    }
 
68
}
 
69
 
 
70
static void
 
71
ensure_autoquit_on (void)
 
72
{
 
73
  if (g_getenv (MINER_NAME "_MINER_PERSIST") != NULL)
 
74
    return;
 
75
 
 
76
  autoquit_id =
 
77
    g_timeout_add_seconds (AUTOQUIT_TIMEOUT,
 
78
                           autoquit_timeout_cb, NULL);
 
79
}
 
80
 
 
81
static gboolean
 
82
signal_handler_cb (gpointer user_data)
 
83
{
 
84
  GMainLoop *loop = user_data;
 
85
 
 
86
  if (cancellable != NULL)
 
87
    g_cancellable_cancel (cancellable);
 
88
 
 
89
  g_main_loop_quit (loop);
 
90
 
 
91
  return FALSE;
 
92
}
 
93
 
 
94
static void
 
95
miner_refresh_db_ready_cb (GObject *source,
 
96
                           GAsyncResult *res,
 
97
                           gpointer user_data)
 
98
{
 
99
  GDBusMethodInvocation *invocation = user_data;
 
100
  GError *error = NULL;
 
101
 
 
102
  gd_miner_refresh_db_finish (GD_MINER (source), res, &error);
 
103
 
 
104
  refreshing = FALSE;
 
105
  ensure_autoquit_on ();
 
106
 
 
107
  if (error != NULL)
 
108
    {
 
109
      g_printerr ("Failed to refresh the DB cache: %s\n", error->message);
 
110
      g_dbus_method_invocation_return_gerror (invocation, error);
 
111
    }
 
112
  else
 
113
    {
 
114
      g_dbus_method_invocation_return_value (invocation, NULL);
 
115
    }
 
116
}
 
117
 
 
118
static void
 
119
handle_refresh_db (GDBusMethodInvocation *invocation)
 
120
{
 
121
  GdMiner *miner;
 
122
 
 
123
  ensure_autoquit_off ();
 
124
 
 
125
  /* if we're refreshing already, compress with the current request */
 
126
  if (refreshing)
 
127
    return;
 
128
 
 
129
  refreshing = TRUE;
 
130
  cancellable = g_cancellable_new ();
 
131
  miner = g_object_new (MINER_TYPE, NULL);
 
132
 
 
133
  gd_miner_refresh_db_async (miner, cancellable,
 
134
                             miner_refresh_db_ready_cb, invocation);
 
135
 
 
136
  g_object_unref (miner);
 
137
}
 
138
 
 
139
static void
 
140
handle_method_call (GDBusConnection       *connection,
 
141
                    const gchar           *sender,
 
142
                    const gchar           *object_path,
 
143
                    const gchar           *interface_name,
 
144
                    const gchar           *method_name,
 
145
                    GVariant              *parameters,
 
146
                    GDBusMethodInvocation *invocation,
 
147
                    gpointer               user_data)
 
148
{
 
149
  if (g_strcmp0 (method_name, "RefreshDB") == 0)
 
150
    handle_refresh_db (g_object_ref (invocation));
 
151
  else
 
152
    g_assert_not_reached ();
 
153
}
 
154
 
 
155
static const GDBusInterfaceVTable interface_vtable =
 
156
{
 
157
  handle_method_call,
 
158
  NULL, /* get_property */
 
159
  NULL, /* set_property */
 
160
};
 
161
 
 
162
static void
 
163
on_bus_acquired (GDBusConnection *connection,
 
164
                 const gchar *name,
 
165
                 gpointer user_data)
 
166
{
 
167
  GError *error = NULL;
 
168
 
 
169
  g_debug ("Connected to the session bus: %s", name);
 
170
 
 
171
  g_dbus_connection_register_object (connection,
 
172
                                     MINER_OBJECT_PATH,
 
173
                                     introspection_data->interfaces[0],
 
174
                                     &interface_vtable,
 
175
                                     NULL,
 
176
                                     NULL,
 
177
                                     &error);
 
178
 
 
179
  if (error != NULL)
 
180
    {
 
181
      g_printerr ("Error exporting object on the session bus: %s",
 
182
                  error->message);
 
183
      g_error_free (error);
 
184
 
 
185
      _exit (1);
 
186
    }
 
187
 
 
188
  g_debug ("Object exported on the session bus");
 
189
}
 
190
 
 
191
static void
 
192
on_name_lost (GDBusConnection *connection,
 
193
              const gchar *name,
 
194
              gpointer user_data)
 
195
{
 
196
  g_debug ("Lost bus name: %s, exiting", name);
 
197
 
 
198
  if (cancellable != NULL)
 
199
    g_cancellable_cancel (cancellable);
 
200
 
 
201
  name_owner_id = 0;
 
202
}
 
203
 
 
204
static void
 
205
on_name_acquired (GDBusConnection *connection,
 
206
                  const gchar *name,
 
207
                  gpointer user_data)
 
208
{
 
209
  g_debug ("Acquired bus name: %s", name);
 
210
}
 
211
 
 
212
int
 
213
main (int argc,
 
214
      char **argv)
 
215
{
 
216
  g_type_init ();
 
217
 
 
218
  ensure_autoquit_on ();
 
219
  loop = g_main_loop_new (NULL, FALSE);
 
220
 
 
221
  g_unix_signal_add_full (G_PRIORITY_DEFAULT,
 
222
                          SIGTERM,
 
223
                          signal_handler_cb,
 
224
                          loop, NULL);
 
225
  g_unix_signal_add_full (G_PRIORITY_DEFAULT,
 
226
                          SIGINT,
 
227
                          signal_handler_cb,
 
228
                          loop, NULL);
 
229
 
 
230
  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
 
231
  g_assert (introspection_data != NULL);
 
232
 
 
233
  name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
 
234
                                  MINER_BUS_NAME,
 
235
                                  G_BUS_NAME_OWNER_FLAGS_NONE,
 
236
                                  on_bus_acquired,
 
237
                                  on_name_acquired,
 
238
                                  on_name_lost,
 
239
                                  NULL, NULL);
 
240
 
 
241
  g_main_loop_run (loop);
 
242
  g_main_loop_unref (loop);
 
243
 
 
244
  if (name_owner_id != 0)
 
245
    g_bus_unown_name (name_owner_id);
 
246
 
 
247
  return 0;
 
248
}