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



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

    libtracker-extract: Allow using TrackerResource instead of TrackerSparqlBuilder
    
    May not fully work.

 src/libtracker-extract/tracker-data.h         |    2 +
 src/libtracker-extract/tracker-extract-info.c |   68 +++++++++++-
 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, 223 insertions(+), 6 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..56c957f 100644
--- a/src/libtracker-extract/tracker-extract-info.c
+++ b/src/libtracker-extract/tracker-extract-info.c
@@ -39,6 +39,8 @@
 
 struct _TrackerExtractInfo
 {
+       TrackerResource *resource;
+
        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 = NULL;
+
        info->preupdate = tracker_sparql_builder_new_update ();
        info->postupdate = tracker_sparql_builder_new_update ();
        info->metadata = tracker_sparql_builder_new_embedded_insert ();
@@ -139,13 +143,9 @@ tracker_extract_info_unref (TrackerExtractInfo *info)
        if (g_atomic_int_dec_and_test (&info->ref_count)) {
                g_object_unref (info->file);
                g_free (info->mimetype);
-               g_free (info->graph);
-               g_free (info->urn);
 
-               g_object_unref (info->preupdate);
-               g_object_unref (info->postupdate);
-               g_object_unref (info->metadata);
-               g_free (info->where_clause);
+               if (info->resource)
+                       g_object_unref (info->resource);
 
 #ifdef HAVE_LIBMEDIAART
                if (info->media_art_process)
@@ -322,6 +322,62 @@ tracker_extract_info_get_urn (TrackerExtractInfo *info)
        return info->urn;
 }
 
+
+/**
+ * tracker_extract_info_get_resource:
+ * @info: a #TrackerExtractInfo
+ *
+ * Returns the #TrackerResource representing metadata about the file
+ * associated with this #TrackerExtractInfo, or %NULL if
+ * tracker_extract_info_set_metadata() was not yet called.
+ *
+ * Returns: (transfer none): a #TrackerResource instance
+ *
+ * Since: 1.10
+ */
+TrackerResource *
+tracker_extract_info_get_resource (TrackerExtractInfo *info)
+{
+       return info->resource;
+}
+
+/**
+ * tracker_extract_info_set_resource:
+ * @info: a #TrackerExtractInfo
+ * @resource: a #TrackerResource
+ *
+ * Adds the #TrackerResource with results from the extraction to this
+ * #TrackerExtractInfo.
+ *
+ * Information about the file itself should be represented by properties of
+ * @resource itself. It's expected this resource will have nfo:FileDataObject
+ * as one of its types. This @resource can have related resources attached to
+ * it.
+ *
+ * In most cases, a file contains a single logical resource. Most MP3 files
+ * contain one song, for example. In this case you set all properties on the
+ * one @resource.
+ *
+ * In more complex cases, a single physical resource (i.e. a file) contains multiple
+ * logical resources: for example, an MBOX file holding multiple emails, or
+ * an audio file containing an entire CD. In this case you should treat each
+ * logical resource as its own #TrackerResource. Only properties of the file
+ * itself should be set on @resource. You then relate each logical
+ * #TrackerResource to the main @resource using the nie:isStoredAs property.
+ *
+ * FIXME: you need a way to delete the logical resources when re-extracting a
+ * file -- still need to decide on API for that.
+ *
+ * Since: 1.10
+ **/
+void
+tracker_extract_info_set_resource (TrackerExtractInfo *info,
+                                   TrackerResource    *resource)
+{
+       g_object_ref (resource);
+       info->resource = resource;
+}
+
 #ifdef HAVE_LIBMEDIAART
 
 /**
diff --git a/src/libtracker-extract/tracker-extract-info.h b/src/libtracker-extract/tracker-extract-info.h
index 857956f..a75031d 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);
+
+TrackerResource *     tracker_extract_info_get_resource           (TrackerExtractInfo *info);
+void                  tracker_extract_info_set_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]