~noskcaj/ubuntu/trusty/tumbler/0.1.30

« back to all changes in this revision

Viewing changes to tumblerd/tumbler-cache-service.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-11-07 16:34:58 UTC
  • Revision ID: james.westby@ubuntu.com-20101107163458-skwfq34vnuavipne
Tags: upstream-0.1.4
ImportĀ upstreamĀ versionĀ 0.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vi:set et ai sw=2 sts=2 ts=2: */
 
2
/*-
 
3
 * Copyright (c) 2009 Jannis Pohlmann <jannis@xfce.org>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or 
 
6
 * modify it under the terms of the GNU General Public License as
 
7
 * published by the Free Software Foundation; either version 2 of 
 
8
 * the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public 
 
16
 * License along with this program; if not, write to the Free 
 
17
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <glib.h>
 
26
#include <glib/gi18n.h>
 
27
#include <glib-object.h>
 
28
 
 
29
#include <dbus/dbus.h>
 
30
#include <dbus/dbus-glib.h>
 
31
#include <dbus/dbus-glib-lowlevel.h>
 
32
 
 
33
#include <tumbler/tumbler.h>
 
34
 
 
35
#include <tumblerd/tumbler-cache-service.h>
 
36
#include <tumblerd/tumbler-cache-service-dbus-bindings.h>
 
37
#include <tumblerd/tumbler-utils.h>
 
38
 
 
39
 
 
40
 
 
41
typedef struct _MoveRequest    MoveRequest;
 
42
typedef struct _CopyRequest    CopyRequest;
 
43
typedef struct _DeleteRequest  DeleteRequest;
 
44
typedef struct _CleanupRequest CleanupRequest;
 
45
 
 
46
 
 
47
 
 
48
/* Property identifiers */
 
49
enum
 
50
{
 
51
  PROP_0,
 
52
  PROP_CONNECTION,
 
53
};
 
54
 
 
55
 
 
56
 
 
57
static void tumbler_cache_service_constructed    (GObject      *object);
 
58
static void tumbler_cache_service_finalize       (GObject      *object);
 
59
static void tumbler_cache_service_get_property   (GObject      *object,
 
60
                                                  guint         prop_id,
 
61
                                                  GValue       *value,
 
62
                                                  GParamSpec   *pspec);
 
63
static void tumbler_cache_service_set_property   (GObject      *object,
 
64
                                                  guint         prop_id,
 
65
                                                  const GValue *value,
 
66
                                                  GParamSpec   *pspec);
 
67
static void tumbler_cache_service_move_thread    (gpointer      data,
 
68
                                                  gpointer      user_data);
 
69
static void tumbler_cache_service_copy_thread    (gpointer      data,
 
70
                                                  gpointer      user_data);
 
71
static void tumbler_cache_service_delete_thread  (gpointer      data,
 
72
                                                  gpointer      user_data);
 
73
static void tumbler_cache_service_cleanup_thread (gpointer      data,
 
74
                                                  gpointer      user_data);
 
75
 
 
76
 
 
77
 
 
78
struct _TumblerCacheServiceClass
 
79
{
 
80
  GObjectClass __parent__;
 
81
};
 
82
 
 
83
struct _TumblerCacheService
 
84
{
 
85
  GObject __parent__;
 
86
 
 
87
  DBusGConnection *connection;
 
88
 
 
89
  TumblerCache    *cache;
 
90
 
 
91
  GThreadPool     *move_pool;
 
92
  GThreadPool     *copy_pool;
 
93
  GThreadPool     *delete_pool;
 
94
  GThreadPool     *cleanup_pool;
 
95
 
 
96
  GMutex          *mutex;
 
97
};
 
98
 
 
99
struct _MoveRequest
 
100
{
 
101
  GStrv from_uris;
 
102
  GStrv to_uris;
 
103
};
 
104
 
 
105
struct _CopyRequest
 
106
{
 
107
  GStrv from_uris;
 
108
  GStrv to_uris;
 
109
};
 
110
 
 
111
struct _DeleteRequest
 
112
{
 
113
  GStrv uris;
 
114
};
 
115
 
 
116
struct _CleanupRequest
 
117
{
 
118
  guint32 since;
 
119
  gchar  *uri_prefix;
 
120
};
 
121
 
 
122
 
 
123
 
 
124
G_DEFINE_TYPE (TumblerCacheService, tumbler_cache_service, G_TYPE_OBJECT);
 
125
 
 
126
 
 
127
 
 
128
static void
 
129
tumbler_cache_service_class_init (TumblerCacheServiceClass *klass)
 
130
{
 
131
  GObjectClass *gobject_class;
 
132
 
 
133
  gobject_class = G_OBJECT_CLASS (klass);
 
134
  gobject_class->constructed = tumbler_cache_service_constructed; 
 
135
  gobject_class->finalize = tumbler_cache_service_finalize; 
 
136
  gobject_class->get_property = tumbler_cache_service_get_property;
 
137
  gobject_class->set_property = tumbler_cache_service_set_property;
 
138
 
 
139
  g_object_class_install_property (gobject_class, PROP_CONNECTION,
 
140
                                   g_param_spec_pointer ("connection",
 
141
                                                         "connection",
 
142
                                                         "connection",
 
143
                                                         G_PARAM_READWRITE |
 
144
                                                         G_PARAM_CONSTRUCT_ONLY));
 
145
}
 
146
 
 
147
 
 
148
 
 
149
static void
 
150
tumbler_cache_service_init (TumblerCacheService *service)
 
151
{
 
152
  service->mutex = g_mutex_new ();
 
153
}
 
154
 
 
155
 
 
156
 
 
157
static void
 
158
tumbler_cache_service_constructed (GObject *object)
 
159
{
 
160
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (object);
 
161
 
 
162
  /* chain up to parent classes */
 
163
  if (G_OBJECT_CLASS (tumbler_cache_service_parent_class)->constructed != NULL)
 
164
    (G_OBJECT_CLASS (tumbler_cache_service_parent_class)->constructed) (object);
 
165
 
 
166
  service->cache = tumbler_cache_get_default ();
 
167
 
 
168
  service->move_pool = g_thread_pool_new (tumbler_cache_service_move_thread, 
 
169
                                          service, 1, FALSE, NULL);
 
170
  service->copy_pool = g_thread_pool_new (tumbler_cache_service_copy_thread, 
 
171
                                          service, 1, FALSE, NULL);
 
172
  service->delete_pool = g_thread_pool_new (tumbler_cache_service_delete_thread,
 
173
                                            service, 1, FALSE, NULL);
 
174
  service->cleanup_pool = g_thread_pool_new (tumbler_cache_service_cleanup_thread, 
 
175
                                             service, 1, FALSE, NULL);
 
176
}
 
177
 
 
178
 
 
179
 
 
180
static void
 
181
tumbler_cache_service_finalize (GObject *object)
 
182
{
 
183
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (object);
 
184
 
 
185
  g_thread_pool_free (service->move_pool, TRUE, TRUE);
 
186
  g_thread_pool_free (service->copy_pool, TRUE, TRUE);
 
187
  g_thread_pool_free (service->delete_pool, TRUE, TRUE);
 
188
  g_thread_pool_free (service->cleanup_pool, TRUE, TRUE);
 
189
 
 
190
  if (service->cache != NULL)
 
191
    g_object_unref (service->cache);
 
192
 
 
193
  dbus_g_connection_unref (service->connection);
 
194
 
 
195
  g_mutex_free (service->mutex);
 
196
 
 
197
  (*G_OBJECT_CLASS (tumbler_cache_service_parent_class)->finalize) (object);
 
198
}
 
199
 
 
200
 
 
201
 
 
202
static void
 
203
tumbler_cache_service_get_property (GObject    *object,
 
204
                                    guint       prop_id,
 
205
                                    GValue     *value,
 
206
                                    GParamSpec *pspec)
 
207
{
 
208
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (object);
 
209
 
 
210
  switch (prop_id)
 
211
    {
 
212
    case PROP_CONNECTION:
 
213
      g_value_set_pointer (value, service->connection);
 
214
      break;
 
215
    default:
 
216
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
217
      break;
 
218
    }
 
219
}
 
220
 
 
221
 
 
222
 
 
223
static void
 
224
tumbler_cache_service_set_property (GObject      *object,
 
225
                                    guint         prop_id,
 
226
                                    const GValue *value,
 
227
                                    GParamSpec   *pspec)
 
228
{
 
229
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (object);
 
230
 
 
231
  switch (prop_id)
 
232
    {
 
233
    case PROP_CONNECTION:
 
234
      service->connection = dbus_g_connection_ref (g_value_get_pointer (value));
 
235
      break;
 
236
    default:
 
237
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
238
      break;
 
239
    }
 
240
}
 
241
 
 
242
 
 
243
 
 
244
static void
 
245
tumbler_cache_service_move_thread (gpointer data,
 
246
                                   gpointer user_data)
 
247
{
 
248
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (user_data);
 
249
  MoveRequest         *request = data;
 
250
 
 
251
  g_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service));
 
252
  g_return_if_fail (request != NULL);
 
253
 
 
254
  g_mutex_lock (service->mutex);
 
255
 
 
256
  if (service->cache != NULL)
 
257
    tumbler_cache_move (service->cache, request->from_uris, request->to_uris);
 
258
 
 
259
  g_strfreev (request->from_uris);
 
260
  g_strfreev (request->to_uris);
 
261
  g_slice_free (MoveRequest, request);
 
262
 
 
263
  g_mutex_unlock (service->mutex);
 
264
}
 
265
 
 
266
 
 
267
 
 
268
static void
 
269
tumbler_cache_service_copy_thread (gpointer data,
 
270
                                   gpointer user_data)
 
271
{
 
272
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (user_data);
 
273
  CopyRequest         *request = data;
 
274
 
 
275
  g_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service));
 
276
  g_return_if_fail (request != NULL);
 
277
 
 
278
  g_mutex_lock (service->mutex);
 
279
 
 
280
  if (service->cache != NULL)
 
281
    tumbler_cache_copy (service->cache, request->from_uris, request->to_uris);
 
282
 
 
283
  g_strfreev (request->from_uris);
 
284
  g_strfreev (request->to_uris);
 
285
  g_slice_free (CopyRequest, request);
 
286
 
 
287
  g_mutex_unlock (service->mutex);
 
288
}
 
289
 
 
290
 
 
291
 
 
292
static void
 
293
tumbler_cache_service_delete_thread (gpointer data,
 
294
                                     gpointer user_data)
 
295
{
 
296
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (user_data);
 
297
  DeleteRequest       *request = data;
 
298
 
 
299
  g_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service));
 
300
  g_return_if_fail (request != NULL);
 
301
 
 
302
  g_mutex_lock (service->mutex);
 
303
 
 
304
  if (service->cache != NULL)
 
305
    tumbler_cache_delete (service->cache, request->uris);
 
306
 
 
307
  g_strfreev (request->uris);
 
308
  g_slice_free (DeleteRequest, request);
 
309
 
 
310
  g_mutex_unlock (service->mutex);
 
311
}
 
312
 
 
313
 
 
314
 
 
315
static void
 
316
tumbler_cache_service_cleanup_thread (gpointer data,
 
317
                                      gpointer user_data)
 
318
{
 
319
  TumblerCacheService *service = TUMBLER_CACHE_SERVICE (user_data);
 
320
  CleanupRequest      *request = data;
 
321
 
 
322
  g_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service));
 
323
  g_return_if_fail (request != NULL);
 
324
 
 
325
  g_mutex_lock (service->mutex);
 
326
 
 
327
  if (service->cache != NULL)
 
328
    tumbler_cache_cleanup (service->cache, request->uri_prefix, request->since);
 
329
 
 
330
  g_free (request->uri_prefix);
 
331
  g_slice_free (CleanupRequest, request);
 
332
 
 
333
  g_mutex_unlock (service->mutex);
 
334
}
 
335
 
 
336
 
 
337
 
 
338
TumblerCacheService *
 
339
tumbler_cache_service_new (DBusGConnection *connection)
 
340
{
 
341
  return g_object_new (TUMBLER_TYPE_CACHE_SERVICE, "connection", connection, NULL);
 
342
}
 
343
 
 
344
 
 
345
 
 
346
gboolean
 
347
tumbler_cache_service_start (TumblerCacheService *service,
 
348
                             GError             **error)
 
349
{
 
350
  DBusConnection *connection;
 
351
  DBusError       dbus_error;
 
352
  gint            result;
 
353
 
 
354
  g_return_val_if_fail (TUMBLER_IS_CACHE_SERVICE (service), FALSE);
 
355
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
356
 
 
357
  g_mutex_lock (service->mutex);
 
358
 
 
359
  /* initialize the D-Bus error */
 
360
  dbus_error_init (&dbus_error);
 
361
 
 
362
  /* get the native D-Bus connection */
 
363
  connection = dbus_g_connection_get_connection (service->connection);
 
364
 
 
365
  /* request ownership for the cache interface */
 
366
  result = dbus_bus_request_name (connection, "org.freedesktop.thumbnails.Cache1", 
 
367
                                  DBUS_NAME_FLAG_DO_NOT_QUEUE, &dbus_error);
 
368
 
 
369
  /* check if that failed */
 
370
  if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
 
371
    {
 
372
      /* propagate the D-Bus error */
 
373
      if (dbus_error_is_set (&dbus_error))
 
374
        {
 
375
          if (error != NULL)
 
376
            dbus_set_g_error (error, &dbus_error);
 
377
 
 
378
          dbus_error_free (&dbus_error);
 
379
        }
 
380
      else if (error != NULL)
 
381
        {
 
382
          g_set_error (error, DBUS_GERROR, DBUS_GERROR_FAILED,
 
383
                       _("Another thumbnail cache service is already running"));
 
384
        }
 
385
 
 
386
      g_mutex_unlock (service->mutex);
 
387
 
 
388
      return FALSE;
 
389
    }
 
390
  
 
391
  /* everything's fine, install the cache type D-Bus info */
 
392
  dbus_g_object_type_install_info (G_OBJECT_TYPE (service),
 
393
                                   &dbus_glib_tumbler_cache_service_object_info);
 
394
 
 
395
  /* register the cache instance as a handler of the cache interface */
 
396
  dbus_g_connection_register_g_object (service->connection, 
 
397
                                       "/org/freedesktop/thumbnails/Cache1",
 
398
                                       G_OBJECT (service));
 
399
 
 
400
  g_mutex_unlock (service->mutex);
 
401
 
 
402
  return TRUE;
 
403
}
 
404
 
 
405
 
 
406
 
 
407
void
 
408
tumbler_cache_service_move (TumblerCacheService   *service,
 
409
                            const GStrv            from_uris,
 
410
                            const GStrv            to_uris,
 
411
                            DBusGMethodInvocation *context)
 
412
{
 
413
  MoveRequest *request;
 
414
 
 
415
  dbus_async_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service), context);
 
416
  dbus_async_return_if_fail (from_uris != NULL, context);
 
417
  dbus_async_return_if_fail (to_uris != NULL, context);
 
418
  dbus_async_return_if_fail (g_strv_length (from_uris) == g_strv_length (to_uris), context);
 
419
 
 
420
  request = g_slice_new0 (MoveRequest);
 
421
  request->from_uris = g_strdupv (from_uris);
 
422
  request->to_uris = g_strdupv (to_uris);
 
423
 
 
424
  g_thread_pool_push (service->move_pool, request, NULL);
 
425
 
 
426
  dbus_g_method_return (context);
 
427
}
 
428
 
 
429
 
 
430
 
 
431
void
 
432
tumbler_cache_service_copy (TumblerCacheService   *service,
 
433
                            const GStrv            from_uris,
 
434
                            const GStrv            to_uris,
 
435
                            DBusGMethodInvocation *context)
 
436
{
 
437
  CopyRequest *request;
 
438
 
 
439
  dbus_async_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service), context);
 
440
  dbus_async_return_if_fail (from_uris != NULL, context);
 
441
  dbus_async_return_if_fail (to_uris != NULL, context);
 
442
  dbus_async_return_if_fail (g_strv_length (from_uris) == g_strv_length (to_uris), context);
 
443
 
 
444
  request = g_slice_new0 (CopyRequest);
 
445
  request->from_uris = g_strdupv (from_uris);
 
446
  request->to_uris = g_strdupv (to_uris);
 
447
 
 
448
  g_thread_pool_push (service->copy_pool, request, NULL);
 
449
 
 
450
  dbus_g_method_return (context);
 
451
}
 
452
 
 
453
 
 
454
 
 
455
void
 
456
tumbler_cache_service_delete (TumblerCacheService   *service,
 
457
                              const GStrv            uris,
 
458
                              DBusGMethodInvocation *context)
 
459
{
 
460
  DeleteRequest *request;
 
461
 
 
462
  dbus_async_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service), context);
 
463
  dbus_async_return_if_fail (uris != NULL, context);
 
464
 
 
465
  request = g_slice_new0 (DeleteRequest);
 
466
  request->uris = g_strdupv (uris);
 
467
 
 
468
  g_thread_pool_push (service->delete_pool, request, NULL);
 
469
 
 
470
  dbus_g_method_return (context);
 
471
}
 
472
 
 
473
 
 
474
 
 
475
void
 
476
tumbler_cache_service_cleanup (TumblerCacheService   *service,
 
477
                               const gchar           *uri_prefix,
 
478
                               guint32                since,
 
479
                               DBusGMethodInvocation *context)
 
480
{
 
481
  CleanupRequest *request;
 
482
 
 
483
  dbus_async_return_if_fail (TUMBLER_IS_CACHE_SERVICE (service), context);
 
484
 
 
485
  request = g_slice_new0 (CleanupRequest);
 
486
  request->uri_prefix = g_strdup (uri_prefix);
 
487
  request->since = since;
 
488
 
 
489
  g_thread_pool_push (service->cleanup_pool, request, NULL);
 
490
 
 
491
  dbus_g_method_return (context);
 
492
}