[gnome-photos] Add PhotosCameraCache
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-photos] Add PhotosCameraCache
- Date: Mon, 12 Aug 2013 10:55:36 +0000 (UTC)
commit bce044f715b23e22f7a282317276770fc15a88b3
Author: Debarshi Ray <debarshir gnome org>
Date: Mon Aug 12 11:57:33 2013 +0200
Add PhotosCameraCache
src/Makefile.am | 2 +
src/photos-camera-cache.c | 224 ++++++++++++++++++++++++++++++++++++++++++++
src/photos-camera-cache.h | 82 ++++++++++++++++
src/photos-query-builder.c | 12 +++
src/photos-query-builder.h | 2 +
5 files changed, 322 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 5bff585..0e95e5d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -68,6 +68,8 @@ gnome_photos_SOURCES = \
photos-base-manager.h \
photos-base-item.c \
photos-base-item.h \
+ photos-camera-cache.c \
+ photos-camera-cache.h \
photos-collection-icon-watcher.c \
photos-collection-icon-watcher.h \
photos-collection-manager.c \
diff --git a/src/photos-camera-cache.c b/src/photos-camera-cache.c
new file mode 100644
index 0000000..931136a
--- /dev/null
+++ b/src/photos-camera-cache.c
@@ -0,0 +1,224 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2013 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <tracker-sparql.h>
+
+#include "photos-camera-cache.h"
+#include "photos-query-builder.h"
+#include "photos-tracker-queue.h"
+
+
+struct _PhotosCameraCachePrivate
+{
+ GHashTable *cache;
+ PhotosTrackerQueue *queue;
+};
+
+
+G_DEFINE_TYPE_WITH_PRIVATE (PhotosCameraCache, photos_camera_cache, G_TYPE_OBJECT);
+
+
+static void
+photos_camera_cache_cursor_next (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ GTask *task = G_TASK (user_data);
+ PhotosCameraCache *self;
+ PhotosCameraCachePrivate *priv;
+ TrackerSparqlCursor *cursor = TRACKER_SPARQL_CURSOR (source_object);
+ GError *error;
+ const gchar *manufacturer;
+ const gchar *model;
+ gchar *camera;
+ gpointer key;
+
+ self = PHOTOS_CAMERA_CACHE (g_task_get_source_object (task));
+ priv = self->priv;
+
+ error = NULL;
+ tracker_sparql_cursor_next_finish (cursor, res, &error);
+ if (error != NULL)
+ {
+ g_task_return_error (task, error);
+ goto out;
+ }
+
+ manufacturer = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+ model = tracker_sparql_cursor_get_string (cursor, 1, NULL);
+
+ if (g_str_has_prefix (model, manufacturer))
+ camera = g_strdup (model);
+ else
+ camera = g_strconcat (manufacturer, " ", model, NULL);
+
+ key = g_task_get_task_data (task);
+ g_hash_table_insert (priv->cache, key, camera);
+
+ g_task_return_pointer (task, g_strdup (camera), g_free);
+
+ out:
+ tracker_sparql_cursor_close (cursor);
+ g_object_unref (task);
+}
+
+
+static void
+photos_camera_cache_equipment_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ GTask *task = G_TASK (user_data);
+ TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
+ TrackerSparqlCursor *cursor;
+ GError *error;
+
+ error = NULL;
+ cursor = tracker_sparql_connection_query_finish (connection, res, &error);
+ if (error != NULL)
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ tracker_sparql_cursor_next_async (cursor, NULL, photos_camera_cache_cursor_next, g_object_ref (task));
+ g_object_unref (cursor);
+}
+
+
+static GObject *
+photos_camera_cache_constructor (GType type, guint n_construct_params, GObjectConstructParam
*construct_params)
+{
+ static GObject *self = NULL;
+
+ if (self == NULL)
+ {
+ self = G_OBJECT_CLASS (photos_camera_cache_parent_class)->constructor (type,
+ n_construct_params,
+ construct_params);
+ g_object_add_weak_pointer (self, (gpointer) &self);
+ return self;
+ }
+
+ return g_object_ref (self);
+}
+
+
+static void
+photos_camera_cache_dispose (GObject *object)
+{
+ PhotosCameraCache *self = PHOTOS_CAMERA_CACHE (object);
+
+ g_clear_object (&self->priv->queue);
+
+ G_OBJECT_CLASS (photos_camera_cache_parent_class)->dispose (object);
+}
+
+
+static void
+photos_camera_cache_finalize (GObject *object)
+{
+ PhotosCameraCache *self = PHOTOS_CAMERA_CACHE (object);
+
+ g_hash_table_unref (self->priv->cache);
+
+ G_OBJECT_CLASS (photos_camera_cache_parent_class)->finalize (object);
+}
+
+
+static void
+photos_camera_cache_init (PhotosCameraCache *self)
+{
+ PhotosCameraCachePrivate *priv;
+
+ self->priv = photos_camera_cache_get_instance_private (self);
+ priv = self->priv;
+
+ priv->cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
+ priv->queue = photos_tracker_queue_new ();
+}
+
+
+static void
+photos_camera_cache_class_init (PhotosCameraCacheClass *class)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+ object_class->constructor = photos_camera_cache_constructor;
+ object_class->dispose = photos_camera_cache_dispose;
+ object_class->finalize = photos_camera_cache_finalize;
+}
+
+
+PhotosCameraCache *
+photos_camera_cache_new (void)
+{
+ return g_object_new (PHOTOS_TYPE_CAMERA_CACHE, NULL);
+}
+
+
+void
+photos_camera_cache_get_camera_async (PhotosCameraCache *self,
+ GQuark id,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ PhotosCameraCachePrivate *priv = self->priv;
+ GTask *task;
+ PhotosQuery *query;
+ const gchar *camera;
+
+ task = g_task_new (self, cancellable, callback, user_data);
+ g_task_set_check_cancellable (task, TRUE);
+ g_task_set_source_tag (task, photos_camera_cache_get_camera_async);
+ g_task_set_task_data (task, GUINT_TO_POINTER (id), NULL);
+
+ camera = g_hash_table_lookup (priv->cache, GUINT_TO_POINTER (id));
+ if (camera != NULL)
+ {
+ g_task_return_pointer (task, g_strdup (camera), g_free);
+ goto out;
+ }
+
+ query = photos_query_builder_equipment_query (id);
+ photos_tracker_queue_select (priv->queue,
+ query->sparql,
+ cancellable,
+ photos_camera_cache_equipment_query_executed,
+ g_object_ref (task),
+ g_object_unref);
+ photos_query_free (query);
+
+ out:
+ g_object_unref (task);
+}
+
+
+gchar *
+photos_camera_cache_get_camera_finish (PhotosCameraCache *self, GAsyncResult *res, GError **error)
+{
+ GTask *task = G_TASK (res);
+
+ g_return_val_if_fail (g_task_is_valid (res, self), NULL);
+ g_return_val_if_fail (g_task_get_source_tag (task) == photos_camera_cache_get_camera_async, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+ return g_task_propagate_pointer (task, error);
+}
diff --git a/src/photos-camera-cache.h b/src/photos-camera-cache.h
new file mode 100644
index 0000000..f3cc6d7
--- /dev/null
+++ b/src/photos-camera-cache.h
@@ -0,0 +1,82 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2013 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef PHOTOS_CAMERA_CACHE_H
+#define PHOTOS_CAMERA_CACHE_H
+
+#include <gio/gio.h>
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_CAMERA_CACHE (photos_camera_cache_get_type ())
+
+#define PHOTOS_CAMERA_CACHE(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ PHOTOS_TYPE_CAMERA_CACHE, PhotosCameraCache))
+
+#define PHOTOS_CAMERA_CACHE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST ((klass), \
+ PHOTOS_TYPE_CAMERA_CACHE, PhotosCameraCacheClass))
+
+#define PHOTOS_IS_CAMERA_CACHE(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ PHOTOS_TYPE_CAMERA_CACHE))
+
+#define PHOTOS_IS_CAMERA_CACHE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+ PHOTOS_TYPE_CAMERA_CACHE))
+
+#define PHOTOS_CAMERA_CACHE_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ PHOTOS_TYPE_CAMERA_CACHE, PhotosCameraCacheClass))
+
+typedef struct _PhotosCameraCache PhotosCameraCache;
+typedef struct _PhotosCameraCacheClass PhotosCameraCacheClass;
+typedef struct _PhotosCameraCachePrivate PhotosCameraCachePrivate;
+
+struct _PhotosCameraCache
+{
+ GObject parent_instance;
+ PhotosCameraCachePrivate *priv;
+};
+
+struct _PhotosCameraCacheClass
+{
+ GObjectClass parent_class;
+};
+
+GType photos_camera_cache_get_type (void) G_GNUC_CONST;
+
+PhotosCameraCache *photos_camera_cache_new (void);
+
+void photos_camera_cache_get_camera_async (PhotosCameraCache *self,
+ GQuark id,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+gchar *photos_camera_cache_get_camera_finish (PhotosCameraCache *self,
+ GAsyncResult *res,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* PHOTOS_CAMERA_CACHE_H */
diff --git a/src/photos-query-builder.c b/src/photos-query-builder.c
index 6b0db9d..bcf1ab5 100644
--- a/src/photos-query-builder.c
+++ b/src/photos-query-builder.c
@@ -276,6 +276,18 @@ photos_query_builder_delete_resource_query (const gchar *resource)
PhotosQuery *
+photos_query_builder_equipment_query (GQuark equipment)
+{
+ const gchar *resource;
+ gchar *sparql;
+
+ resource = g_quark_to_string (equipment);
+ sparql = g_strdup_printf ("SELECT nfo:manufacturer (<%s>) nfo:model (<%s>) WHERE {}", resource, resource);
+ return photos_query_new (sparql);
+}
+
+
+PhotosQuery *
photos_query_builder_fetch_collections_query (const gchar *resource)
{
gchar *sparql;
diff --git a/src/photos-query-builder.h b/src/photos-query-builder.h
index 92a1353..d2cf5c5 100644
--- a/src/photos-query-builder.h
+++ b/src/photos-query-builder.h
@@ -39,6 +39,8 @@ PhotosQuery *photos_query_builder_count_query (gint flags);
PhotosQuery *photos_query_builder_delete_resource_query (const gchar *resource);
+PhotosQuery *photos_query_builder_equipment_query (GQuark equipment);
+
PhotosQuery *photos_query_builder_fetch_collections_query (const gchar *resource);
PhotosQuery *photos_query_builder_global_query (gint flags);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]