[libmediaart/api-cleanup: 14/14] WIP



commit 281f726b38488b8746a61d75318ee119a5c4834b
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Feb 25 18:35:42 2014 +0000

    WIP

 libmediaart/extract.c |  168 ++++++++++++++++++++++++++++++++++++++++++++-----
 libmediaart/extract.h |   12 +++-
 2 files changed, 161 insertions(+), 19 deletions(-)
---
diff --git a/libmediaart/extract.c b/libmediaart/extract.c
index 8991d7a..b130744 100644
--- a/libmediaart/extract.c
+++ b/libmediaart/extract.c
@@ -1499,7 +1499,7 @@ get_heuristic_for_parent_path (GFile        *file,
 }
 
 /**
- * media_art_process_file:
+ * media_art_process_buffer:
  * @process: Media art process object
  * @file: File to be processed
  * @buffer: (array length=len)(allow-none): a buffer containing @file data, or %NULL
@@ -1529,15 +1529,15 @@ get_heuristic_for_parent_path (GFile        *file,
  * Since: 0.3.0
  */
 gboolean
-media_art_process_file (MediaArtProcess  *process,
-                        GFile            *file,
-                        const guchar     *buffer,
-                        gsize             len,
-                        const gchar      *mime,
-                        MediaArtType      type,
-                        const gchar      *artist,
-                        const gchar      *title,
-                        GError          **error)
+media_art_process_buffer (MediaArtProcess  *process,
+                          GFile            *file,
+                          const guchar     *buffer,
+                          gsize             len,
+                          const gchar      *mime,
+                          MediaArtType      type,
+                          const gchar      *artist,
+                          const gchar      *title,
+                          GError          **error)
 {
        MediaArtProcessPrivate *private;
        GFile *cache_art_file, *local_art_file;
@@ -1549,6 +1549,8 @@ media_art_process_file (MediaArtProcess  *process,
        g_return_val_if_fail (MEDIA_ART_IS_PROCESS (process), FALSE);
        g_return_val_if_fail (G_IS_FILE (file), FALSE);
        g_return_val_if_fail (type > MEDIA_ART_NONE && type < MEDIA_ART_TYPE_COUNT, FALSE);
+       g_return_val_if_fail (buffer != NULL, FALSE);
+       g_return_val_if_fail (len < 0, FALSE);
 
        private = media_art_process_get_instance_private (process);
 
@@ -1617,15 +1619,150 @@ media_art_process_file (MediaArtProcess  *process,
        }
 
        cache_art_path = g_file_get_path (cache_art_file);
-       no_cache_or_old = cache_mtime == 0 || mtime > cache_mtime;
 
-       if ((buffer && len > 0) && no_cache_or_old) {
+       if (cache_mtime == 0 || mtime > cache_mtime) {
                processed = media_art_set (buffer, len, mime, type, artist, title, error);
                set_mtime (cache_art_path, mtime);
-               created = TRUE;
+       } else {
+               g_debug ("Album art already exists for uri:'%s' as '%s'",
+                        uri,
+                        cache_art_path);
+       }
+
+       if (local_art_file && !g_file_query_exists (local_art_file, NULL)) {
+               /* We can't reuse art_exists here because the
+                * situation might have changed
+                */
+               if (g_file_query_exists (cache_art_file, NULL)) {
+                       gchar *local_art_uri;
+
+                       local_art_uri = g_file_get_uri (local_art_file);
+                       media_art_copy_to_local (process, cache_art_path, local_art_uri);
+                       g_free (local_art_uri);
+               }
+       }
+
+       if (cache_art_file) {
+               g_object_unref (cache_art_file);
        }
 
-       if (!created && no_cache_or_old) {
+       if (local_art_file) {
+               g_object_unref (local_art_file);
+       }
+
+       g_free (cache_art_path);
+       g_free (uri);
+
+       return processed;
+}
+
+/**
+ * media_art_process_file:
+ * @process: Media art process object
+ * @file: File to be processed
+ * @type: The type of media
+ * @artist: The media file artist name, or %NULL
+ * @title: The media file title, or %NULL
+ * @error: Pointer to potential GLib / MediaArt error, or %NULL
+ *
+ * Processes a media file. 
+ *
+ * FIXME: Finish...
+ *
+ * If @file is on a removable filesystem, the media art file will be saved in a
+ * cache on the removable file system rather than on the host machine.
+ *
+ * Returns: %TRUE if @file could be processed or %FALSE if @error is set.
+ *
+ * Since: 0.3.0
+ */
+gboolean
+media_art_process_file (MediaArtProcess  *process,
+                        GFile            *file,
+                        MediaArtType      type,
+                        const gchar      *artist,
+                        const gchar      *title,
+                        GError          **error)
+{
+       MediaArtProcessPrivate *private;
+       GFile *cache_art_file, *local_art_file;
+       GError *local_error = NULL;
+       gchar *cache_art_path, *uri;
+       gboolean processed, no_cache_or_old;
+       guint64 mtime, cache_mtime = 0;
+
+       g_return_val_if_fail (MEDIA_ART_IS_PROCESS (process), FALSE);
+       g_return_val_if_fail (G_IS_FILE (file), FALSE);
+       g_return_val_if_fail (type > MEDIA_ART_NONE && type < MEDIA_ART_TYPE_COUNT, FALSE);
+
+       private = media_art_process_get_instance_private (process);
+
+       processed = FALSE;
+
+       uri = g_file_get_uri (file);
+       g_debug ("Processing media art: artist:'%s', title:'%s', type:'%s', uri:'%s'",
+                artist ? artist : "",
+                title ? title : "",
+                media_art_type_name[type],
+                uri)
+
+       mtime = get_mtime (file, &local_error);
+       if (local_error != NULL) {
+               g_debug ("Could not get mtime for file '%s': %s",
+                        uri,
+                        local_error->message);
+               g_propagate_error (error, local_error);
+               g_free (uri);
+
+               return FALSE;
+       }
+
+       media_art_get_file (artist,
+                           title,
+                           media_art_type_name[type],
+                           file,
+                           &cache_art_file,
+                           &local_art_file);
+
+       cache_mtime = get_mtime (cache_art_file, &local_error);
+
+       if (local_error &&
+           local_error->domain == g_io_error_quark () &&
+           local_error->code == G_IO_ERROR_NOT_FOUND) {
+               /* cache_art_file not existing is the only error we
+                * accept here, anything else and we return.
+                */
+               gchar *path;
+
+               path = g_file_get_uri (cache_art_file);
+               g_debug ("Cache for media art didn't exist (%s)",
+                        path);
+               g_free (path);
+               g_clear_error (&local_error);
+       }
+
+       if (local_error) {
+               g_free (uri);
+
+               uri = g_file_get_uri (cache_art_file);
+               g_debug ("Could not get mtime for cache '%s': %s",
+                        uri,
+                        local_error->message);
+               g_free (uri);
+
+               if (local_art_file) {
+                       g_object_unref (local_art_file);
+               }
+
+               g_propagate_error (error, local_error);
+
+               return FALSE;
+       }
+
+       cache_art_path = g_file_get_path (cache_art_file);
+       no_cache_or_old = cache_mtime == 0 || mtime > cache_mtime;
+
+       if (no_cache_or_old) {
                /* If not, we perform a heuristic on the dir */
                gchar *key;
 
@@ -1659,7 +1796,7 @@ media_art_process_file (MediaArtProcess  *process,
                } else {
                        g_free (key);
                }
-       } else if (!created) {
+       } else {
                g_debug ("Album art already exists for uri:'%s' as '%s'",
                         uri,
                         cache_art_path);
@@ -1692,7 +1829,6 @@ media_art_process_file (MediaArtProcess  *process,
        return processed;
 }
 
-
 /**
  * media_art_process_uri:
  * @process: Media art process object
diff --git a/libmediaart/extract.h b/libmediaart/extract.h
index a17c8c9..b944105 100644
--- a/libmediaart/extract.h
+++ b/libmediaart/extract.h
@@ -120,14 +120,20 @@ gboolean media_art_process_uri  (MediaArtProcess      *process,
 
 gboolean media_art_process_file (MediaArtProcess      *process,
                                  GFile                *file,
-                                 const guchar         *buffer,
-                                gsize                 len,
-                                const gchar          *mime,
                                 MediaArtType          type,
                                 const gchar          *artist,
                                  const gchar          *title,
                                  GError              **error);
 
+gboolean media_art_process_buffer (MediaArtProcess      *process,
+                                   GFile                *file,
+                                   const guchar         *buffer,
+                                   gsize                 len,
+                                   const gchar          *mime,
+                                   MediaArtType          type,
+                                   const gchar          *artist,
+                                   const gchar          *title,
+                                   GError              **error);
 G_END_DECLS
 
 #endif /* __LIBMEDIAART_UTILS_H__ */


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