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

« back to all changes in this revision

Viewing changes to src/miner/gd-miner-tracker.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-06-29 13:16:35 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120629131635-tkocc51xk4c37a39
Tags: 0.5.3-0ubuntu1
* New upstream release.
* debian/control.in:
  - Bump minimum GTK
  - Build-depend on libzapojit-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2011, 2012 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
#include <glib.h>
 
23
 
 
24
#include "gd-miner-tracker.h"
 
25
 
 
26
static gchar *
 
27
_tracker_utils_format_into_graph (const gchar *graph)
 
28
{
 
29
  return (graph != NULL) ? g_strdup_printf ("INTO <%s> ", graph) : g_strdup ("");
 
30
}
 
31
 
 
32
gchar *
 
33
gd_miner_tracker_sparql_connection_ensure_resource (TrackerSparqlConnection *connection,
 
34
                                                    GCancellable *cancellable,
 
35
                                                    GError **error,
 
36
                                                    const gchar *graph,
 
37
                                                    const gchar *identifier,
 
38
                                                    const gchar *class,
 
39
                                                    ...)
 
40
{
 
41
  GString *select, *insert, *inner;
 
42
  va_list args;
 
43
  const gchar *arg;
 
44
  TrackerSparqlCursor *cursor;
 
45
  gboolean res;
 
46
  gchar *retval = NULL;
 
47
  gchar *graph_str;
 
48
  GVariant *insert_res;
 
49
  GVariantIter *iter;
 
50
  gchar *key = NULL, *val = NULL;
 
51
 
 
52
  /* build the inner query with all the classes */
 
53
  va_start (args, class);
 
54
  inner = g_string_new (NULL);
 
55
 
 
56
  for (arg = class; arg != NULL; arg = va_arg (args, const gchar *))
 
57
    g_string_append_printf (inner, " a %s; ", arg);
 
58
 
 
59
  g_string_append_printf (inner, "nao:identifier \"%s\"", identifier);
 
60
 
 
61
  va_end (args);
 
62
 
 
63
  /* query if such a resource is already in the DB */
 
64
  select = g_string_new (NULL);
 
65
  g_string_append_printf (select,
 
66
                          "SELECT ?urn WHERE { ?urn %s }", inner->str);
 
67
 
 
68
  cursor = tracker_sparql_connection_query (connection,
 
69
                                            select->str,
 
70
                                            cancellable, error);
 
71
 
 
72
  g_string_free (select, TRUE);
 
73
 
 
74
  if (*error != NULL)
 
75
    goto out;
 
76
 
 
77
  res = tracker_sparql_cursor_next (cursor, cancellable, error);
 
78
 
 
79
  if (*error != NULL)
 
80
    goto out;
 
81
 
 
82
  if (res)
 
83
    {
 
84
      /* return the found resource */
 
85
      retval = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
 
86
      g_debug ("Found resource in the store: %s", retval);
 
87
      goto out;
 
88
    }
 
89
 
 
90
  /* not found, create the resource */
 
91
  insert = g_string_new (NULL);
 
92
  graph_str = _tracker_utils_format_into_graph (graph);
 
93
 
 
94
  g_string_append_printf (insert, "INSERT %s { _:res %s }",
 
95
                          graph_str, inner->str);
 
96
  g_free (graph_str);
 
97
  g_string_free (inner, TRUE);
 
98
 
 
99
  insert_res =
 
100
    tracker_sparql_connection_update_blank (connection, insert->str,
 
101
                                            G_PRIORITY_DEFAULT, NULL, error);
 
102
 
 
103
  g_string_free (insert, TRUE);
 
104
 
 
105
  if (*error != NULL)
 
106
    goto out;
 
107
 
 
108
  /* the result is an "aaa{ss}" variant */
 
109
  g_variant_get (insert_res, "aaa{ss}", &iter);
 
110
  g_variant_iter_next (iter, "aa{ss}", &iter);
 
111
  g_variant_iter_next (iter, "a{ss}", &iter);
 
112
  g_variant_iter_next (iter, "{ss}", &key, &val);
 
113
 
 
114
  g_variant_iter_free (iter);
 
115
  g_variant_unref (insert_res);
 
116
 
 
117
  if (g_strcmp0 (key, "res") == 0)
 
118
    {
 
119
      retval = val;
 
120
    }
 
121
  else
 
122
    {
 
123
      g_free (val);
 
124
      goto out;
 
125
    }
 
126
 
 
127
  g_debug ("Created a new resource: %s", retval);
 
128
 
 
129
 out:
 
130
  g_clear_object (&cursor);
 
131
  return retval;
 
132
}
 
133
 
 
134
gboolean
 
135
gd_miner_tracker_sparql_connection_insert_or_replace_triple (TrackerSparqlConnection *connection,
 
136
                                                             GCancellable *cancellable,
 
137
                                                             GError **error,
 
138
                                                             const gchar *graph,
 
139
                                                             const gchar *resource,
 
140
                                                             const gchar *property_name,
 
141
                                                             const gchar *property_value)
 
142
{
 
143
  GString *insert;
 
144
  gchar *graph_str;
 
145
  gboolean retval = TRUE;
 
146
 
 
147
  graph_str = _tracker_utils_format_into_graph (graph);
 
148
 
 
149
  insert = g_string_new (NULL);
 
150
  g_string_append_printf
 
151
    (insert,
 
152
     "INSERT OR REPLACE %s { <%s> a nie:InformationElement ; %s \"%s\" }",
 
153
     graph_str, resource, property_name, property_value);
 
154
 
 
155
  g_debug ("Insert or replace triple: query %s", insert->str);
 
156
 
 
157
  tracker_sparql_connection_update (connection, insert->str,
 
158
                                    G_PRIORITY_DEFAULT, cancellable,
 
159
                                    error);
 
160
 
 
161
  g_string_free (insert, TRUE);
 
162
 
 
163
  if (*error != NULL)
 
164
    retval = FALSE;
 
165
 
 
166
  g_free (graph_str);
 
167
 
 
168
  return retval;
 
169
}
 
170
 
 
171
gboolean
 
172
gd_miner_tracker_sparql_connection_set_triple (TrackerSparqlConnection *connection,
 
173
                                               GCancellable *cancellable,
 
174
                                               GError **error,
 
175
                                               const gchar *graph,
 
176
                                               const gchar *resource,
 
177
                                               const gchar *property_name,
 
178
                                               const gchar *property_value)
 
179
{
 
180
  GString *delete;
 
181
  gboolean retval = TRUE;
 
182
 
 
183
  delete = g_string_new (NULL);
 
184
  g_string_append_printf
 
185
    (delete,
 
186
     "DELETE { <%s> %s ?val } WHERE { <%s> %s ?val }", resource,
 
187
     property_name, resource, property_name);
 
188
 
 
189
  tracker_sparql_connection_update (connection, delete->str,
 
190
                                    G_PRIORITY_DEFAULT, cancellable,
 
191
                                    error);
 
192
 
 
193
  g_string_free (delete, TRUE);
 
194
  if (*error != NULL)
 
195
    {
 
196
      retval = FALSE;
 
197
      goto out;
 
198
    }
 
199
 
 
200
  retval =
 
201
    gd_miner_tracker_sparql_connection_insert_or_replace_triple (connection,
 
202
                                                                 cancellable, error,
 
203
                                                                 graph, resource,
 
204
                                                                 property_name, property_value);
 
205
 
 
206
 out:
 
207
  return retval;
 
208
}