summaryrefslogtreecommitdiff
path: root/src/tracker/grl-tracker-source-notif.c
blob: beabf6b6846939e9134b2b2c5eb6de3ea9ecf274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * Copyright (C) 2011-2012 Igalia S.L.
 * Copyright (C) 2011 Intel Corporation.
 *
 * Contact: Iago Toral Quiroga <itoral@igalia.com>
 *
 * Authors: Lionel Landwerlin <lionel.g.landwerlin@linux.intel.com>
 *          Juan A. Suarez Romero <jasuarez@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <tracker-sparql.h>

#include "grl-tracker.h"
#include "grl-tracker-source-notif.h"
#include "grl-tracker-source-priv.h"
#include "grl-tracker-utils.h"

/* --------- Logging  -------- */

#define GRL_LOG_DOMAIN_DEFAULT tracker_notif_log_domain
GRL_LOG_DOMAIN_STATIC(tracker_notif_log_domain);

/* ------- Definitions ------- */

#define TRACKER_MEDIA_ITEM                                              \
  "SELECT rdf:type(?urn) tracker:id(?urn) nie:dataSource(?urn) "        \
  "WHERE { ?urn a nfo:FileDataObject . "                                \
  "FILTER (tracker:id(?urn) IN (%s)) }"

/**/

typedef struct {
  /* tables of items for which we know the source */
  GHashTable *inserted_items;
  GHashTable *deleted_items;
  GHashTable *updated_items;

  /* table of items for which we don't know the source */
  GHashTable *orphan_items;

  /* List of new/old sources */
  GList *new_sources;
  GList *old_sources;

  /* Convenient stuff (for tracker/own callbacks...) */
  TrackerSparqlCursor  *cursor;
  gboolean             in_use;
  GrlSourceChangeType  change_type;
} tracker_evt_update_t;

/**/

static guint tracker_dbus_signal_id = 0;

/**/
static tracker_evt_update_t *
tracker_evt_update_new (void)
{
  tracker_evt_update_t *evt = g_slice_new0 (tracker_evt_update_t);

  evt->inserted_items = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
  evt->deleted_items = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
  evt->updated_items = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);

  evt->orphan_items = g_hash_table_new (g_direct_hash, g_direct_equal);

  return evt;
}

static void
tracker_evt_update_free (tracker_evt_update_t *evt)
{
  if (!evt)
    return;

  GRL_DEBUG ("free evt=%p", evt);

  g_hash_table_destroy (evt->inserted_items);
  g_hash_table_destroy (evt->deleted_items);
  g_hash_table_destroy (evt->updated_items);

  g_hash_table_destroy (evt->orphan_items);

  g_list_free (evt->new_sources);
  g_list_free (evt->old_sources);

  g_slice_free (tracker_evt_update_t, evt);
}

static void
tracker_evt_update_source_add (tracker_evt_update_t *evt,
                               const gchar *id,
                               const gchar *source_name)
{
  GrlTrackerSource *source;
  GrlTrackerSourcePriv *priv;

  source = g_hash_table_lookup (grl_tracker_source_sources_modified, id);
  if (!source) {
    source = g_object_new (GRL_TRACKER_SOURCE_TYPE,
			   "source-id", id,
			   "source-name", source_name,
			   "source-desc", GRL_TRACKER_SOURCE_DESC,
			   "tracker-connection", grl_tracker_connection,
                           "tracker-datasource", id,
			   NULL);
    g_hash_table_insert (grl_tracker_source_sources_modified,
                         (gpointer) grl_tracker_source_get_tracker_source (source),
                         g_object_ref (source));
  } else {
    g_object_ref (source);
  }

  priv = GRL_TRACKER_SOURCE_GET_PRIVATE (source);
  priv->state = GRL_TRACKER_SOURCE_STATE_INSERTING;
  priv->notification_ref++;

  evt->new_sources = g_list_append (evt->new_sources, source);

  GRL_DEBUG ("Preadd source p=%p name=%s id=%s count=%u",
             source, source_name, id, priv->notification_ref);

  g_object_unref (source);
}

static void
tracker_evt_update_source_del (tracker_evt_update_t *evt,
                               GrlTrackerSource *source)
{
  GrlTrackerSourcePriv *priv = GRL_TRACKER_SOURCE_GET_PRIVATE (source);

  priv->notification_ref++;
  priv->state = GRL_TRACKER_SOURCE_STATE_DELETING;

  evt->old_sources = g_list_append (evt->old_sources, source);

  GRL_DEBUG ("Predel source p=%p name=%s id=%s count=%u", source,
             grl_source_get_name (GRL_SOURCE (source)),
             grl_tracker_source_get_tracker_source (source),
             priv->notification_ref);
}

static void
tracker_evt_postupdate_sources (tracker_evt_update_t *evt)
{
  GList *source;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  source = evt->old_sources;
  while (source != NULL) {
    grl_tracker_del_source (GRL_TRACKER_SOURCE (source->data));
    source = source->next;
  }

  source = evt->new_sources;
  while (source != NULL) {
    grl_tracker_add_source (GRL_TRACKER_SOURCE (source->data));
    source = source->next;
  }

  tracker_evt_update_free (evt);
}

static void
tracker_evt_update_orphan_item_cb (GObject              *object,
                                   GAsyncResult         *result,
                                   tracker_evt_update_t *evt)
{
  guint id;
  const gchar *type, *datasource;
  GrlTrackerSource *source = NULL;
  GError *error = NULL;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  if (!tracker_sparql_cursor_next_finish (evt->cursor, result, &error)) {
    if (error != NULL) {
      GRL_DEBUG ("\terror in parsing : %s", error->message);
      g_error_free (error);
    } else {
      GRL_DEBUG ("\tend of parsing...");
    }

    g_clear_object (&evt->cursor);

    if (grl_tracker_per_device_source) {
      /* Once all items have been processed, add new sources and we're
	 done. */
      tracker_evt_postupdate_sources (evt);
    } else {
      tracker_evt_update_free (evt);
    }

    return;
  }

  type = tracker_sparql_cursor_get_string (evt->cursor, 0, NULL);
  id = tracker_sparql_cursor_get_integer (evt->cursor, 1);
  datasource = tracker_sparql_cursor_get_string (evt->cursor, 2, NULL);

  GRL_DEBUG ("\tOrphan item: id=%u datasource=%s", id, datasource);

  if (!grl_tracker_per_device_source)
    source = grl_tracker_source_find ("");

  if (!source && datasource)
    source = grl_tracker_source_find (datasource);

  if (source && GRL_IS_TRACKER_SOURCE (source)) {
    GrlMedia *media;

    GRL_DEBUG (" \tAdding to cache id=%u", id);
    grl_tracker_source_cache_add_item (grl_tracker_item_cache, id, source);

    if (grl_tracker_source_can_notify (source)) {
      media = grl_tracker_build_grilo_media (type);
      if (media) {
        gchar *str_id = g_strdup_printf ("%i", id);
        gint change_type =
          GPOINTER_TO_INT (g_hash_table_lookup (evt->orphan_items,
                                                GSIZE_TO_POINTER (id)));

        grl_media_set_id (media, str_id);
        g_free (str_id);

        GRL_DEBUG ("\tNotify id=%u source=%s p=%p", id,
                   grl_source_get_name (GRL_SOURCE (source)),
                   source);
        grl_source_notify_change (GRL_SOURCE (source),
                                  media, change_type, FALSE);

        g_object_unref (media);
      }
    }
  }

  tracker_sparql_cursor_next_async (evt->cursor, NULL,
                                    (GAsyncReadyCallback) tracker_evt_update_orphan_item_cb,
                                    (gpointer) evt);
}

static void
tracker_evt_update_orphans_cb (GObject              *object,
                               GAsyncResult         *result,
                               tracker_evt_update_t *evt)
{
  GError *error = NULL;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  evt->cursor = tracker_sparql_connection_query_finish (grl_tracker_connection,
                                                        result, &error);

  if (error != NULL) {
    GRL_WARNING ("Could not execute sparql query: %s", error->message);

    g_error_free (error);
    tracker_evt_postupdate_sources (evt);
    return;
  }

  tracker_sparql_cursor_next_async (evt->cursor, NULL,
                                    (GAsyncReadyCallback) tracker_evt_update_orphan_item_cb,
                                    (gpointer) evt);
}

static void
tracker_evt_update_orphans (tracker_evt_update_t *evt)
{
  gboolean first = TRUE;
  GString *request_str;
  GHashTableIter iter;
  gpointer key, value;
  GList *source, *sources;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  if (g_hash_table_size (evt->orphan_items) < 1) {
    tracker_evt_postupdate_sources (evt);
    return;
  }

  sources = grl_registry_get_sources (grl_registry_get_default (),
                                      FALSE);

  request_str = g_string_new ("");

  g_hash_table_iter_init (&iter, evt->orphan_items);
  while (g_hash_table_iter_next (&iter, &key, &value)) {
    guint id = GPOINTER_TO_UINT (key);
    GrlSourceChangeType change_type = GPOINTER_TO_INT (value);

    if (change_type != GRL_CONTENT_REMOVED) {
      if (first) {
        g_string_append_printf (request_str, "%u", id);
        first = FALSE;
      }
      else {
        g_string_append_printf (request_str, ", %u", id);
      }
    } else {
      /* Notify all sources that a media been removed */
      source = sources;
      while (source != NULL) {
        if (GRL_IS_TRACKER_SOURCE (source->data)) {
          GRL_DEBUG ("\tNotify id=%u source=%s p=%p", id,
                     grl_source_get_name (GRL_SOURCE (source->data)),
                     source->data);
          if (grl_tracker_source_can_notify (GRL_TRACKER_SOURCE (source->data))) {
            GrlMedia *media = grl_media_new ();
            gchar *str_id = g_strdup_printf ("%u", id);

            grl_media_set_id (media, str_id);
            g_free (str_id);

            grl_source_notify_change (GRL_SOURCE (source->data),
                                      media,
                                      GRL_CONTENT_REMOVED,
                                      FALSE);
            g_object_unref (media);
          }
        }
        source = source->next;
      }
    }
  }

  if (request_str->len > 0) {
    gchar *sparql_final = g_strdup_printf (TRACKER_MEDIA_ITEM, request_str->str);

    GRL_DEBUG ("\trequest : '%s'", sparql_final);

    tracker_sparql_connection_query_async (grl_tracker_connection,
                                           sparql_final,
                                           NULL,
                                           (GAsyncReadyCallback) tracker_evt_update_orphans_cb,
                                           evt);

    g_free (sparql_final);
  } else {
    tracker_evt_postupdate_sources (evt);
  }

  g_string_free (request_str, TRUE);
}

static void
tracker_evt_update_items_cb (gpointer              key,
                             gpointer              value,
                             tracker_evt_update_t *evt)
{
  guint id = GPOINTER_TO_INT (key);
  gchar *str_id;
  GrlTrackerSource *source = (GrlTrackerSource *) value;
  GrlMedia *media;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  g_assert (source != NULL);

  if (!grl_tracker_source_can_notify (source)) {
    GRL_DEBUG ("\tno notification for source %s...",
	       grl_source_get_name (GRL_SOURCE (source)));
    return;
  }

  media = grl_media_new ();
  str_id = g_strdup_printf ("%i", id);
  grl_media_set_id (media, str_id);
  g_free (str_id);

  GRL_DEBUG ("\tNotify id=%u source=%s", id,
             grl_source_get_name (GRL_SOURCE (source)));
  grl_source_notify_change (GRL_SOURCE (source), media, evt->change_type, FALSE);

  g_object_unref (media);
}

static void
tracker_evt_update_items (tracker_evt_update_t *evt)
{
  evt->change_type = GRL_CONTENT_REMOVED;
  g_hash_table_foreach (evt->deleted_items,
                        (GHFunc) tracker_evt_update_items_cb, evt);
  evt->change_type = GRL_CONTENT_ADDED;
  g_hash_table_foreach (evt->inserted_items,
                        (GHFunc) tracker_evt_update_items_cb, evt);
  evt->change_type = GRL_CONTENT_CHANGED;
  g_hash_table_foreach (evt->updated_items,
                        (GHFunc) tracker_evt_update_items_cb, evt);
}

static void
tracker_evt_preupdate_sources_item_cb (GObject              *object,
                                       GAsyncResult         *result,
                                       tracker_evt_update_t *evt)
{
  const gchar *type, *datasource, *uri, *datasource_name;
  gboolean source_available = FALSE;
  GrlTrackerSource *source;
  GError *error = NULL;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  if (!tracker_sparql_cursor_next_finish (evt->cursor, result, &error)) {
    if (error != NULL) {
      GRL_DEBUG ("\terror in parsing : %s", error->message);
      g_error_free (error);
    } else {
      GRL_DEBUG ("\tend of parsing... start notifying sources");
    }

    g_clear_object (&evt->cursor);

    /* Once all sources have been preupdated, start items
       updates. */
    tracker_evt_update_items (evt);
    tracker_evt_update_orphans (evt);

    return;
  }

  type = tracker_sparql_cursor_get_string (evt->cursor, 0, NULL);
  datasource = tracker_sparql_cursor_get_string (evt->cursor, 1, NULL);
  datasource_name = tracker_sparql_cursor_get_string (evt->cursor, 2, NULL);
  uri = tracker_sparql_cursor_get_string (evt->cursor, 3, NULL);
  if (tracker_sparql_cursor_is_bound (evt->cursor, 4))
    source_available = tracker_sparql_cursor_get_boolean (evt->cursor, 4);

  source = grl_tracker_source_find (datasource);

  GRL_DEBUG ("\tdatasource=%s uri=%s available=%i source=%p",
             datasource, uri, source_available, source);

  if (source_available) {
    if (source == NULL) {
      gchar *source_name = grl_tracker_get_source_name (type, uri, datasource,
                                                        datasource_name);
      /* Defer source creation until we have processed all sources */
      if (source_name) {
        tracker_evt_update_source_add (evt, datasource, source_name);
        g_free (source_name);
      }
    } else {
      GRL_DEBUG ("\tChanges on source %p / %s", source, datasource);
    }
  } else if (!source_available && source != NULL) {
    tracker_evt_update_source_del (evt, GRL_TRACKER_SOURCE (source));
  }

  tracker_sparql_cursor_next_async (evt->cursor, NULL,
                                    (GAsyncReadyCallback) tracker_evt_preupdate_sources_item_cb,
                                    (gpointer) evt);
}

static void
tracker_evt_preupdate_sources_cb (GObject              *object,
                                  GAsyncResult         *result,
                                  tracker_evt_update_t *evt)
{
  GError *error = NULL;

  GRL_DEBUG ("%s: evt=%p", __FUNCTION__, evt);

  evt->cursor = tracker_sparql_connection_query_finish (grl_tracker_connection,
                                                        result, &error);

  if (error != NULL) {
    GRL_WARNING ("\tCannot handle datasource request : %s", error->message);

    g_error_free (error);

    tracker_evt_update_items (evt);
    tracker_evt_update_orphans (evt);
    return;
  }

  tracker_sparql_cursor_next_async (evt->cursor, NULL,
                                    (GAsyncReadyCallback) tracker_evt_preupdate_sources_item_cb,
                                    (gpointer) evt);
}

static void
tracker_evt_preupdate_sources (tracker_evt_update_t *evt)
{
  tracker_sparql_connection_query_async (grl_tracker_connection,
                                         TRACKER_DATASOURCES_REQUEST,
                                         NULL,
                                         (GAsyncReadyCallback) tracker_evt_preupdate_sources_cb,
                                         evt);
}

static void
tracker_dbus_signal_cb (GDBusConnection *connection,
                        const gchar     *sender_name,
                        const gchar     *object_path,
                        const gchar     *interface_name,
                        const gchar     *signal_name,
                        GVariant        *parameters,
                        gpointer         user_data)

{
  gchar *class_name;
  gint graph = 0, subject = 0, predicate = 0, object = 0;
  GVariantIter *iter1, *iter2;
  tracker_evt_update_t *evt = tracker_evt_update_new ();

  g_variant_get (parameters, "(&sa(iiii)a(iiii))", &class_name, &iter1, &iter2);

  GRL_DEBUG ("Tracker update event for class=%s ins=%lu del=%lu evt=%p",
             class_name,
             (unsigned long) g_variant_iter_n_children (iter1),
             (unsigned long) g_variant_iter_n_children (iter2),
	     evt);

  /* Process deleted items */
  while (g_variant_iter_loop (iter1, "(iiii)", &graph,
                              &subject, &predicate, &object)) {
    gpointer psubject = GSIZE_TO_POINTER (subject);
    GrlTrackerSource *source =
      grl_tracker_source_cache_get_source (grl_tracker_item_cache, subject);

    /* GRL_DEBUG ("\tdelete=> subject=%i", subject); */

    if (source) {
      g_hash_table_insert (evt->deleted_items, psubject,
                           g_object_ref (source));
    } else {
        g_hash_table_insert (evt->orphan_items, psubject,
                             GSIZE_TO_POINTER (GRL_CONTENT_REMOVED));
    }
  }

  while (g_variant_iter_loop (iter2, "(iiii)", &graph,
                              &subject, &predicate, &object)) {
    gpointer psubject = GSIZE_TO_POINTER (subject);
    GrlTrackerSource *source =
      grl_tracker_source_cache_get_source (grl_tracker_item_cache, subject);

    /* GRL_DEBUG ("\tinsert=> subject=%i", subject); */

    if (source) {
      /* Removed & inserted items are probably just renamed items... */
      if (g_hash_table_lookup (evt->deleted_items, psubject)) {
        g_hash_table_remove (evt->deleted_items, psubject);
        g_hash_table_insert (evt->updated_items, psubject,
                             g_object_ref (source));
      } else if (!g_hash_table_lookup (evt->updated_items, psubject)) {
        g_hash_table_insert (evt->inserted_items, psubject,
                             g_object_ref (source));
      }
    } else {
      gpointer state;

      if (g_hash_table_lookup_extended (evt->orphan_items, psubject,
                                        NULL, &state) &&
            (GPOINTER_TO_INT (state) == GRL_CONTENT_REMOVED)) {
        g_hash_table_insert (evt->orphan_items, psubject,
                             GSIZE_TO_POINTER (GRL_CONTENT_CHANGED));
      } else {
        g_hash_table_insert (evt->orphan_items, psubject,
                             GSIZE_TO_POINTER (GRL_CONTENT_ADDED));
      }
    }
  }

  g_variant_iter_free (iter1);
  g_variant_iter_free (iter2);

  GRL_DEBUG ("\tinserted=%i deleted=%i updated=%i orphan=%i",
             g_hash_table_size (evt->inserted_items),
             g_hash_table_size (evt->deleted_items),
             g_hash_table_size (evt->updated_items),
	     g_hash_table_size (evt->orphan_items));

  if (grl_tracker_per_device_source) {
    tracker_evt_preupdate_sources (evt);
  } else {
    tracker_evt_update_items (evt);
    tracker_evt_update_orphans (evt);
  }
}

void
grl_tracker_source_dbus_start_watch (void)
{
  GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);

  tracker_dbus_signal_id = g_dbus_connection_signal_subscribe (connection,
                                                               TRACKER_DBUS_SERVICE,
                                                               TRACKER_DBUS_INTERFACE_RESOURCES,
                                                               "GraphUpdated",
                                                               TRACKER_DBUS_OBJECT_RESOURCES,
                                                               NULL,
                                                               G_DBUS_SIGNAL_FLAGS_NONE,
                                                               tracker_dbus_signal_cb,
                                                               NULL,
                                                               NULL);
}

void
grl_tracker_source_init_notifs (void)
{
  GRL_LOG_DOMAIN_INIT (tracker_notif_log_domain, "tracker-notif");
}