[tracker/wip/sam/resource: 7/18] libtracker-extract: Allow using TrackerResource instead of TrackerSparqlBuilder



commit e213301ad7c6db7ea16ffe568f89004f4ce8f89b
Author: Sam Thursfield <ssssam gmail com>
Date:   Sun Mar 27 00:15:33 2016 +0000

    libtracker-extract: Allow using TrackerResource instead of TrackerSparqlBuilder
    
    This won't work yet as the SPARQL generation for TrackerResource isn't
    implemented at all.

 src/libtracker-extract/tracker-data.h         |    2 +
 src/libtracker-extract/tracker-extract-info.c |   45 ++++++++
 src/libtracker-extract/tracker-extract-info.h |    6 +
 src/libtracker-extract/tracker-guarantee.c    |  143 +++++++++++++++++++++++++
 src/libtracker-extract/tracker-guarantee.h    |   10 ++
 5 files changed, 206 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-extract/tracker-data.h b/src/libtracker-extract/tracker-data.h
index bdfe82b..d224507 100644
--- a/src/libtracker-extract/tracker-data.h
+++ b/src/libtracker-extract/tracker-data.h
@@ -24,6 +24,7 @@
 #error "only <libtracker-extract/tracker-extract.h> must be included directly."
 #endif
 
+#include <libtracker-sparql/tracker-resource.h>
 #include <libtracker-sparql/tracker-sparql.h>
 #include "tracker-module-manager.h"
 #include "tracker-extract-info.h"
@@ -50,6 +51,7 @@ G_BEGIN_DECLS
  * <title>Basic extractor example</title>
  * An example of how to write an extractor to retrieve PNG embedded
  * metadata.
+ * FIXME: rewrite with TrackerResource!
  * <programlisting>
  *  G_MODULE_EXPORT gboolean
  *  tracker_extract_get_metadata (TrackerExtractInfo *info)
diff --git a/src/libtracker-extract/tracker-extract-info.c b/src/libtracker-extract/tracker-extract-info.c
index c534695..b96e5b6 100644
--- a/src/libtracker-extract/tracker-extract-info.c
+++ b/src/libtracker-extract/tracker-extract-info.c
@@ -39,6 +39,8 @@
 
 struct _TrackerExtractInfo
 {
+       GList *resource_list;
+
        TrackerSparqlBuilder *preupdate;
        TrackerSparqlBuilder *postupdate;
        TrackerSparqlBuilder *metadata;
@@ -87,6 +89,8 @@ tracker_extract_info_new (GFile       *file,
        info->graph = g_strdup (graph);
        info->urn = g_strdup (urn);
 
+       info->resource_list = NULL;
+
        info->preupdate = tracker_sparql_builder_new_update ();
        info->postupdate = tracker_sparql_builder_new_update ();
        info->metadata = tracker_sparql_builder_new_embedded_insert ();
@@ -322,6 +326,47 @@ tracker_extract_info_get_urn (TrackerExtractInfo *info)
        return info->urn;
 }
 
+
+/**
+ * tracker_extract_info_get_resources:
+ * @info: a #TrackerExtractInfo
+ *
+ * Return all resources that were found during extraction and added to the
+ * result with tracker_extract_info_add_resource().
+ *
+ * Returns: (transfer none): a #GList of #TrackerResource instances, which is
+ *     owned by the #TrackerExtractInfo object and should not be modified directly
+ *
+ * Since: 1.10
+ */
+GList *
+tracker_extract_info_get_resources (TrackerExtractInfo *info)
+{
+       return info->resource_list;
+}
+
+/**
+ * tracker_extract_info_add_resource:
+ * @info: a #TrackerExtractInfo
+ * @resource: a #TrackerResource
+ *
+ * Adds a #TrackerResource with results from the extraction.
+ *
+ * If the resource links to other resources (added with
+ * tracker_resource_add_relation() or tracker_resource_set_relation()), those
+ * will be found during serialization without you needing to explicitly
+ * add them to the #TrackerExtractInfo.
+ *
+ * Since: 1.10
+ **/
+void
+tracker_extract_info_add_resource (TrackerExtractInfo *info,
+                                   TrackerResource    *resource)
+{
+       g_object_ref (resource);
+       info->resource_list = g_list_prepend (info->resource_list, resource);
+}
+
 #ifdef HAVE_LIBMEDIAART
 
 /**
diff --git a/src/libtracker-extract/tracker-extract-info.h b/src/libtracker-extract/tracker-extract-info.h
index 857956f..e151e0c 100644
--- a/src/libtracker-extract/tracker-extract-info.h
+++ b/src/libtracker-extract/tracker-extract-info.h
@@ -47,6 +47,12 @@ TrackerExtractInfo *  tracker_extract_info_ref                    (TrackerExtrac
 void                  tracker_extract_info_unref                  (TrackerExtractInfo *info);
 GFile *               tracker_extract_info_get_file               (TrackerExtractInfo *info);
 const gchar *         tracker_extract_info_get_mimetype           (TrackerExtractInfo *info);
+
+GList *               tracker_extract_info_get_resources          (TrackerExtractInfo *info);
+void                  tracker_extract_info_add_resource           (TrackerExtractInfo *info,
+                                                                   TrackerResource    *resource);
+
+/* All these are deprecated since TrackerResource support was added */
 const gchar *         tracker_extract_info_get_graph              (TrackerExtractInfo *info);
 const gchar *         tracker_extract_info_get_urn                (TrackerExtractInfo *info);
 TrackerSparqlBuilder *tracker_extract_info_get_preupdate_builder  (TrackerExtractInfo *info);
diff --git a/src/libtracker-extract/tracker-guarantee.c b/src/libtracker-extract/tracker-guarantee.c
index f43a4e6..9b8460a 100644
--- a/src/libtracker-extract/tracker-guarantee.c
+++ b/src/libtracker-extract/tracker-guarantee.c
@@ -87,6 +87,9 @@ get_date_from_file_mtime (const gchar *uri)
  * (before the "." and extension of the file) as the title. If the
  * title has any "_" characters, they are also converted into spaces.
  *
+ * This function only operates if Tracker was compiled with
+ * --enable-guarantee-metadata enabled at configure-time.
+ *
  * Returns: %TRUE on success and content was added to @metadata, otherwise %FALSE.
  *
  * Since: 0.10
@@ -160,6 +163,9 @@ tracker_guarantee_title_from_file (TrackerSparqlBuilder  *metadata,
  * When parsing @uri, stat() is called on the file to create a
  * date based on the file's mtime.
  *
+ * This function only operates if Tracker was compiled with
+ * --enable-guarantee-metadata enabled at configure-time.
+ *
  * Returns: %TRUE on success and content was added to @metadata, otherwise %FALSE.
  *
  * Since: 0.10
@@ -205,3 +211,140 @@ tracker_guarantee_date_from_file_mtime (TrackerSparqlBuilder *metadata,
 
        return success;
 }
+
+
+/**
+ * tracker_guarantee_resource_title_from_file:
+ * @resource: the relevant #TrackerResource
+ * @key: the property URI to set
+ * @current_value: the current data to check before looking at @uri.
+ * @uri: a string representing a URI to use
+ * @p_new_value: pointer to a string which receives the new title, or
+ *             %NULL
+ *
+ * Checks @current_value to make sure it is usable (i.e. not %NULL or an
+ * empty string). If it is not usable, then @uri is parsed to guarantee a
+ * metadata value for @key.
+ *
+ * Parses the file pointed to by @uri and uses the basename
+ * (before the "." and extension of the file) as the title. If the
+ * title has any "_" characters, they are also converted into spaces.
+ *
+ * This function only operates if Tracker was compiled with
+ * --enable-guarantee-metadata enabled at configure-time.
+ *
+ * Returns: %TRUE on success and content was added to @metadata, otherwise %FALSE.
+ *
+ * Since: 1.10
+ **/
+gboolean
+tracker_guarantee_resource_title_from_file (TrackerResource  *resource,
+                                            const gchar      *key,
+                                            const gchar      *current_value,
+                                            const gchar      *uri,
+                                            gchar           **p_new_value)
+{
+       gboolean success = TRUE;
+
+#ifdef GUARANTEE_METADATA
+       g_return_val_if_fail (metadata != NULL, FALSE);
+       g_return_val_if_fail (key != NULL, FALSE);
+       g_return_val_if_fail (uri != NULL, FALSE);
+
+       if (current_value && *current_value != '\0') {
+               tracker_resource_set_string (resource, key, current_value);
+
+               if (p_new_value != NULL) {
+                       *p_new_value = g_strdup (current_value);
+               }
+       } else {
+               gchar *value;
+
+               value = get_title_from_file (uri);
+
+               if (value && value[0] != '\0') {
+                       tracker_resource_set_string (resource, key, value);
+               } else {
+                       success = FALSE;
+               }
+
+               if (p_new_value != NULL) {
+                       *p_new_value = value;
+               } else {
+                       g_free (value);
+               }
+       }
+#else  /* GUARANTEE_METADATA */
+       if (current_value && *current_value != '\0') {
+               tracker_resource_set_string (resource, key, current_value);
+
+               if (p_new_value != NULL) {
+                       *p_new_value = g_strdup (current_value);
+               }
+       } else {
+               success = FALSE;
+       }
+#endif /* GUARANTEE_METADATA */
+
+       return success;
+}
+
+/**
+ * tracker_guarantee_resource_date_from_file_mtime:
+ * @resource: the relevant #TrackerResource
+ * @key: the property URI to set
+ * @current_value: the current data to check before looking at @uri
+ * @uri: a string representing a URI to use
+ *
+ * Checks @current_value to make sure it is sane (i.e. not %NULL or an
+ * empty string). If it is, then @uri is parsed to guarantee a
+ * metadata value for @key.
+ *
+ * When parsing @uri, stat() is called on the file to create a
+ * date based on the file's mtime.
+ *
+ * This function only operates if Tracker was compiled with
+ * --enable-guarantee-metadata enabled at configure-time.
+ *
+ * Returns: %TRUE on success and content was added to @metadata, otherwise %FALSE.
+ *
+ * Since: 1.10
+ **/
+gboolean
+tracker_guarantee_resource_date_from_file_mtime (TrackerResource *resource,
+                                                 const gchar     *key,
+                                                 const gchar     *current_value,
+                                                 const gchar     *uri)
+{
+       gboolean success = TRUE;
+
+#ifdef GUARANTEE_METADATA
+       g_return_val_if_fail (resource != NULL, FALSE);
+       g_return_val_if_fail (key != NULL, FALSE);
+       g_return_val_if_fail (uri != NULL, FALSE);
+
+       if (current_value && *current_value != '\0') {
+               tracker_resource_set_string (resource, key, current_value);
+       } else {
+               gchar *value;
+
+               value = get_date_from_file_mtime (uri);
+
+               if (value && *value != '\0') {
+                       tracker_resource_set_string (resource, key, value);
+               } else {
+                       success = FALSE;
+               }
+
+               g_free (value);
+       }
+#else  /* GUARANTEE_METADATA */
+       if (current_value && *current_value != '\0') {
+               tracker_resource_set_string (resource, key, current_value);
+       } else {
+               success = FALSE;
+       }
+#endif /* GUARANTEE_METADATA */
+
+       return success;
+}
diff --git a/src/libtracker-extract/tracker-guarantee.h b/src/libtracker-extract/tracker-guarantee.h
index e14fd8e..9b58a63 100644
--- a/src/libtracker-extract/tracker-guarantee.h
+++ b/src/libtracker-extract/tracker-guarantee.h
@@ -40,6 +40,16 @@ gboolean tracker_guarantee_date_from_file_mtime (TrackerSparqlBuilder *metadata,
                                                  const gchar          *current_value,
                                                  const gchar          *uri);
 
+gboolean tracker_guarantee_resource_title_from_file      (TrackerResource  *resource,
+                                                          const gchar      *key,
+                                                          const gchar      *current_value,
+                                                          const gchar      *uri,
+                                                          gchar           **p_new_value);
+gboolean tracker_guarantee_resource_date_from_file_mtime (TrackerResource  *resource,
+                                                          const gchar      *key,
+                                                          const gchar      *current_value,
+                                                          const gchar      *uri);
+
 G_END_DECLS
 
 #endif /*  __LIBTRACKER_EXTRACT_GUARANTEE_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]