diff options
author | Martyn Russell <martyn@lanedo.com> | 2014-07-28 09:25:22 +0100 |
---|---|---|
committer | Martyn Russell <martyn@lanedo.com> | 2014-07-28 09:25:22 +0100 |
commit | f868869ff93c56b4d5df0bcd00393adcb03af7a0 (patch) | |
tree | 8b503c859accd2e85f08374be51b7676041198f2 /libmediaart | |
parent | f7317f8fdc21e357393e355e937d98a1b1a769c1 (diff) | |
parent | 23f3c73c3c3153b670c36b4f74c2e57665f7f075 (diff) | |
download | libmediaart-f868869ff93c56b4d5df0bcd00393adcb03af7a0.tar.gz |
Merge branch 'api-cleanup'
Diffstat (limited to 'libmediaart')
-rw-r--r-- | libmediaart/cache.c | 43 | ||||
-rw-r--r-- | libmediaart/extract.c | 1200 | ||||
-rw-r--r-- | libmediaart/extract.h | 124 | ||||
-rw-r--r-- | libmediaart/extractdummy.c | 80 | ||||
-rw-r--r-- | libmediaart/extractgeneric.h | 17 | ||||
-rw-r--r-- | libmediaart/extractpixbuf.c | 75 | ||||
-rw-r--r-- | libmediaart/extractqt.cpp | 23 | ||||
-rw-r--r-- | libmediaart/storage.c | 22 |
8 files changed, 1032 insertions, 552 deletions
diff --git a/libmediaart/cache.c b/libmediaart/cache.c index 7249b14..82286a4 100644 --- a/libmediaart/cache.c +++ b/libmediaart/cache.c @@ -29,22 +29,25 @@ /** * SECTION:cache - * @title: Caching and Management - * @short_description: Caching and management of stored media art. + * @title: Cache + * @short_description: Caching and lookup of stored media art. * @include: libmediaart/mediaart.h * - * These functions give you access to the media art that has been extracted - * and saved in the user's XDG_CACHE_HOME directory. + * These functions give you access to the media art that has been + * extracted and saved in the user's XDG_CACHE_HOME directory (usually + * ~/.cache/media-art/). * * To find the media art for a given media file, use the function - * media_art_get_file() (you can also use media_art_get_path(), which does the - * same thing but for path strings instead of #GFile objects). + * media_art_get_file() (you can also use media_art_get_path(), which + * does the same thing but for path strings instead of #GFile + * objects). * - * If media art for the file is not found in the cache, these functions will - * return %NULL. You may find some embedded media art upon loading the file, - * and you can use media_art_process() to convert it to the correct format and - * save it in the cache for next time. The media_art_process() function also - * supports searching for external media art images using a basic heuristic. + * If media art for the file is not found in the cache, these + * functions will return %NULL. You may find some embedded media art + * upon loading the file, and you can use media_art_process_buffer() + * to convert it to the correct format and save it in the cache for + * next time. The media_art_process_file() function also supports + * searching for external media art images using a basic heuristic. **/ static gboolean @@ -271,9 +274,7 @@ media_art_get_file (const gchar *artist, *local_file = NULL; } - if (!artist && !title) { - return; - } + g_return_if_fail (artist != NULL || title != NULL); if (artist) { artist_stripped = media_art_strip_invalid_entities (artist); @@ -359,8 +360,8 @@ media_art_get_file (const gchar *artist, * @local_uri: (out) (transfer full) (allow-none): the location to store the * local uri or %NULL * - * Get the path to media art for a given resource. Newly allocated data in - * @path and @local_uri must be freed with g_free(). + * Get the path to media art for a given resource. Newly allocated + * data returned in @path and @local_uri must be freed with g_free(). * * Since: 0.2.0 */ @@ -417,7 +418,7 @@ media_art_remove_foreach (gpointer data, * @artist: artist the media art belongs to * @album: (allow-none): album the media art belongs or %NULL * - * Removes media art for given album/artist/etc provided. + * Removes media art for given album/artist provided. * * Returns: #TRUE on success, otherwise #FALSE. * @@ -470,9 +471,11 @@ media_art_remove (const gchar *artist, } /* Add the album path also (to which the symlinks are made) */ - media_art_get_path (NULL, album, "album", NULL, &target, NULL); - if (target) { - g_hash_table_replace (table, target, target); + if (album) { + media_art_get_path (NULL, album, "album", NULL, &target, NULL); + if (target) { + g_hash_table_replace (table, target, target); + } } /* Perhaps we should have an internal list of media art files that we made, diff --git a/libmediaart/extract.c b/libmediaart/extract.c index e9ff970..7091aac 100644 --- a/libmediaart/extract.c +++ b/libmediaart/extract.c @@ -46,7 +46,7 @@ * from a media file and saving it into the media art cache, so that future * applications can display the media art without having to extract the image * again. This is done using the media_art_process_file() or - * media_art_process() functions. + * media_art_process_buffer() functions. * * Extracting new media art from a file needs to be done by your application. * Usually, when an application loads a media file any embedded images will be @@ -56,21 +56,26 @@ * * The media art cache requires that all images are saved as 'application/jpeg' * files. Embedded images can be in several formats, and - * media_art_process_file() and media_art_process() functions will + * media_art_process_file() and media_art_process_buffer() functions will * convert the supplied image data into the correct format if * necessary. There are multiple backends that can be used for this, * and you can choose which is used at build time using the library's * 'configure' script. * * If there is no embedded media art in a file, - * media_art_process_file() and media_art_process() functions will + * media_art_process_file() and media_art_process_buffer() functions will * look in the directory that contains the media file for likely media * art using a simple heuristic. - * - * You must call media_art_init() before using the functions in libmediaart, - * and call media_art_shutdown() to free the resources it uses. **/ +typedef struct { + gboolean disable_requests; + + GHashTable *media_art_cache; + GDBusConnection *connection; + Storage *storage; +} MediaArtProcessPrivate; + static const gchar *media_art_type_name[MEDIA_ART_TYPE_COUNT] = { "invalid", "album", @@ -78,6 +83,7 @@ static const gchar *media_art_type_name[MEDIA_ART_TYPE_COUNT] = { }; typedef struct { + MediaArtProcess *process; gchar *art_path; gchar *local_uri; } FileInfo; @@ -96,16 +102,129 @@ typedef enum { IMAGE_MATCH_TYPE_COUNT } ImageMatchType; -static gboolean initialized = FALSE; -static gboolean disable_requests; +static void media_art_queue_cb (GObject *source_object, + GAsyncResult *res, + gpointer user_data); +static void media_art_process_initable_iface_init (GInitableIface *iface); + +G_DEFINE_TYPE_WITH_CODE (MediaArtProcess, media_art_process, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, + media_art_process_initable_iface_init) + G_ADD_PRIVATE (MediaArtProcess)) + +static void +media_art_process_finalize (GObject *object) +{ + MediaArtProcessPrivate *private; + MediaArtProcess *process; + + process = MEDIA_ART_PROCESS (object); + private = media_art_process_get_instance_private (process); + + if (private->storage) { + g_object_unref (private->storage); + } + + if (private->connection) { + g_object_unref (private->connection); + } + + if (private->media_art_cache) { + g_hash_table_unref (private->media_art_cache); + } + + media_art_plugin_shutdown (); + + G_OBJECT_CLASS (media_art_process_parent_class)->finalize (object); +} + +static gboolean +media_art_process_initable_init (GInitable *initable, + GCancellable *cancellable, + GError **error) +{ + MediaArtProcessPrivate *private; + MediaArtProcess *process; + GError *local_error = NULL; + + process = MEDIA_ART_PROCESS (initable); + private = media_art_process_get_instance_private (process); + + g_debug ("Initializing media art processing requirements..."); -static GHashTable *media_art_cache; -static GDBusConnection *connection; -static Storage *storage; + media_art_plugin_init (0); -static void media_art_queue_cb (GObject *source_object, - GAsyncResult *res, - gpointer user_data); + /* Cache to know if we have already handled uris */ + private->media_art_cache = g_hash_table_new_full (g_str_hash, + g_str_equal, + (GDestroyNotify) g_free, + NULL); + + /* Signal handler for new album art from the extractor */ + private->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &local_error); + + if (!private->connection) { + g_critical ("Could not connect to the D-Bus session bus, %s", + local_error ? local_error->message : "no error given."); + g_propagate_error (error, local_error); + + return FALSE; + } + + private->storage = storage_new (); + if (!private->storage) { + g_critical ("Could not start storage module for removable media detection"); + + if (error) { + *error = g_error_new (media_art_error_quark (), + MEDIA_ART_ERROR_NO_STORAGE, + "Could not initialize storage module"); + } + + return FALSE; + } + + return TRUE; +} + +static void +media_art_process_initable_iface_init (GInitableIface *iface) +{ + iface->init = media_art_process_initable_init; +} + +static void +media_art_process_class_init (MediaArtProcessClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = media_art_process_finalize; +} + +static void +media_art_process_init (MediaArtProcess *thumbnailer) +{ +} + +/** + * media_art_process_new: + * @error: Pointer to potential GLib / MediaArt error, or %NULL + * + * Initialize a GObject for processing and extracting media art. + * + * This function initializes cache hash tables, backend plugins, + * storage modules used for removable devices and a connection to D-Bus. + * + * Returns: A new #MediaArtProcess object on success or %NULL if + * @error is set. This object must be freed using g_object_unref(). + * + * Since: 0.5.0 + */ +MediaArtProcess * +media_art_process_new (GError **error) +{ + return g_initable_new (MEDIA_ART_TYPE_PROCESS, NULL, error, NULL); +} static GDir * get_parent_g_dir (const gchar *uri, @@ -142,7 +261,6 @@ get_parent_g_dir (const gchar *uri, return dir; } - static gchar * checksum_for_data (GChecksumType checksum_type, const guchar *data, @@ -255,10 +373,11 @@ end: } static gboolean -convert_from_other_format (const gchar *found, - const gchar *target, - const gchar *album_path, - const gchar *artist) +convert_from_other_format (const gchar *found, + const gchar *target, + const gchar *album_path, + const gchar *artist, + GError **error) { gboolean retval; gchar *sum1 = NULL; @@ -266,18 +385,28 @@ convert_from_other_format (const gchar *found, target_temp = g_strdup_printf ("%s-tmp", target); - if (!media_art_file_to_jpeg (found, target_temp)) { + if (!media_art_file_to_jpeg (found, target_temp, error)) { g_free (target_temp); return FALSE; } if (artist == NULL || g_strcmp0 (artist, " ") == 0) { - if (g_rename (target_temp, album_path) == -1) { - g_debug ("rename(%s, %s) error: %s", - target_temp, - album_path, - g_strerror (errno)); + retval = g_rename (target_temp, album_path) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_RENAME_FAILED, + "Could not rename '%s' to '%s': %s", + target_temp, + album_path, + g_strerror (errno)); } + + g_debug ("rename(%s, %s) error: %s", + target_temp, + album_path, + g_strerror (errno)); } else if (file_get_checksum_if_exists (G_CHECKSUM_MD5, target_temp, &sum1, @@ -294,45 +423,77 @@ convert_from_other_format (const gchar *found, /* If album-space-md5.jpg is the same as found, * make a symlink */ - - if (symlink (album_path, target) != 0) { - g_debug ("symlink(%s, %s) error: %s", - album_path, - target, - g_strerror (errno)); - retval = FALSE; - } else { - retval = TRUE; + retval = symlink (album_path, target) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_RENAME_FAILED, + "Could not rename '%s' to '%s': %s", + album_path, + target, + g_strerror (errno)); } + g_debug ("symlink(%s, %s) error: %s", + album_path, + target, + g_strerror (errno)); + g_unlink (target_temp); } else { /* If album-space-md5.jpg isn't the same as found, * make a new album-md5-md5.jpg (found -> target) */ - - if (g_rename (target_temp, album_path) == -1) { - g_debug ("rename(%s, %s) error: %s", - target_temp, - album_path, - g_strerror (errno)); + retval = g_rename (target_temp, album_path) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_RENAME_FAILED, + "Could not rename '%s' to '%s': %s", + target_temp, + album_path, + g_strerror (errno)); } + + g_debug ("rename(%s, %s) error: %s", + target_temp, + album_path, + g_strerror (errno)); } g_free (sum2); } else { /* If there's not yet a album-space-md5.jpg, make one, * and symlink album-md5-md5.jpg to it */ - g_rename (target_temp, album_path); + retval = g_rename (target_temp, album_path) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_RENAME_FAILED, + "Could not rename '%s' to '%s': %s", + album_path, + target, + g_strerror (errno)); + } else { + retval = symlink (album_path, target) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_SYMLINK_FAILED, + "Could not rename '%s' to '%s': %s", + album_path, + target, + g_strerror (errno)); + } - if (symlink (album_path, target) != 0) { g_debug ("symlink(%s,%s) error: %s", - album_path, - target, - g_strerror (errno)); - retval = FALSE; - } else { - retval = TRUE; + album_path, + target, + g_strerror (errno)); } } @@ -530,11 +691,12 @@ media_art_find_by_artist_and_title (const gchar *uri, } static gboolean -media_art_heuristic (const gchar *artist, - const gchar *title, - MediaArtType type, - const gchar *filename_uri, - const gchar *local_uri) +get_heuristic (MediaArtType type, + const gchar *filename_uri, + const gchar *local_uri, + const gchar *artist, + const gchar *title, + GError **error) { gchar *art_file_path = NULL; gchar *album_art_file_path = NULL; @@ -544,7 +706,10 @@ media_art_heuristic (const gchar *artist, gboolean retval = FALSE; if (title == NULL || title[0] == '\0') { - g_debug ("Unable to fetch media art, no title specified"); + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_NO_TITLE, + "Title is required, but was not provided, or was empty"); return FALSE; } @@ -600,171 +765,186 @@ media_art_heuristic (const gchar *artist, artist, title); - if (art_file_path != NULL) { - if (g_str_has_suffix (art_file_path, "jpeg") || - g_str_has_suffix (art_file_path, "jpg")) { + if (!art_file_path) { + // FIXME: Do we GError here? - gboolean is_jpeg = FALSE; - gchar *sum1 = NULL; - - if (type != MEDIA_ART_ALBUM || - (artist == NULL || g_strcmp0 (artist, " ") == 0)) { - GFile *art_file; - GFile *target_file; - GError *err = NULL; - - g_debug ("Album art (JPEG) found in same directory being used:'%s'", - art_file_path); - - target_file = g_file_new_for_path (target); - art_file = g_file_new_for_path (art_file_path); + g_free (art_file_path); + g_free (album_art_file_path); - g_file_copy (art_file, - target_file, - 0, - NULL, - NULL, - NULL, - &err); + g_free (target); + g_free (artist_stripped); + g_free (title_stripped); - if (err) { - g_debug ("%s", err->message); - g_clear_error (&err); - } + return FALSE; + } - g_object_unref (art_file); - g_object_unref (target_file); - } else if (file_get_checksum_if_exists (G_CHECKSUM_MD5, - art_file_path, - &sum1, - TRUE, - &is_jpeg)) { - /* Avoid duplicate artwork for each track in an album */ - media_art_get_path (NULL, - title_stripped, - media_art_type_name [type], - NULL, - &album_art_file_path, - NULL); + if (g_str_has_suffix (art_file_path, "jpeg") || + g_str_has_suffix (art_file_path, "jpg")) { + gboolean is_jpeg = FALSE; + gchar *sum1 = NULL; + + if (type != MEDIA_ART_ALBUM || + (artist == NULL || g_strcmp0 (artist, " ") == 0)) { + GFile *art_file; + GFile *target_file; + GError *local_error = NULL; + + g_debug ("Album art (JPEG) found in same directory being used:'%s'", + art_file_path); + + target_file = g_file_new_for_path (target); + art_file = g_file_new_for_path (art_file_path); + + g_file_copy (art_file, + target_file, + 0, + NULL, + NULL, + NULL, + &local_error); + + if (local_error) { + g_debug ("%s", local_error->message); + g_propagate_error (error, local_error); + } - if (is_jpeg) { - gchar *sum2 = NULL; - - g_debug ("Album art (JPEG) found in same directory being used:'%s'", art_file_path); - - if (file_get_checksum_if_exists (G_CHECKSUM_MD5, - album_art_file_path, - &sum2, - FALSE, - NULL)) { - if (g_strcmp0 (sum1, sum2) == 0) { - /* If album-space-md5.jpg is the same as found, - * make a symlink */ - - if (symlink (album_art_file_path, target) != 0) { - g_debug ("symlink(%s, %s) error: %s", - album_art_file_path, - target, - g_strerror (errno)); - retval = FALSE; - } else { - retval = TRUE; - } - } else { - GFile *art_file; - GFile *target_file; - GError *err = NULL; - - /* If album-space-md5.jpg isn't the same as found, - * make a new album-md5-md5.jpg (found -> target) */ - - target_file = g_file_new_for_path (target); - art_file = g_file_new_for_path (art_file_path); - retval = g_file_copy (art_file, - target_file, - 0, - NULL, - NULL, - NULL, - &err); - if (err) { - g_debug ("%s", err->message); - g_clear_error (&err); - } - g_object_unref (art_file); - g_object_unref (target_file); + g_object_unref (art_file); + g_object_unref (target_file); + } else if (file_get_checksum_if_exists (G_CHECKSUM_MD5, + art_file_path, + &sum1, + TRUE, + &is_jpeg)) { + /* Avoid duplicate artwork for each track in an album */ + media_art_get_path (NULL, + title_stripped, + media_art_type_name [type], + NULL, + &album_art_file_path, + NULL); + + if (is_jpeg) { + gchar *sum2 = NULL; + + g_debug ("Album art (JPEG) found in same directory being used:'%s'", art_file_path); + + if (file_get_checksum_if_exists (G_CHECKSUM_MD5, + album_art_file_path, + &sum2, + FALSE, + NULL)) { + if (g_strcmp0 (sum1, sum2) == 0) { + /* If album-space-md5.jpg is the same as found, + * make a symlink */ + retval = symlink (album_art_file_path, target) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_SYMLINK_FAILED, + "Could not link '%s' to '%s': %s", + album_art_file_path, + target, + g_strerror (errno)); } - g_free (sum2); + + g_debug ("symlink(%s, %s) error: %s", + album_art_file_path, + target, + g_strerror (errno)); } else { GFile *art_file; - GFile *album_art_file; - GError *err = NULL; - - /* If there's not yet a album-space-md5.jpg, make one, - * and symlink album-md5-md5.jpg to it */ + GFile *target_file; - album_art_file = g_file_new_for_path (album_art_file_path); + /* If album-space-md5.jpg isn't the same as found, + * make a new album-md5-md5.jpg (found -> target) */ + target_file = g_file_new_for_path (target); art_file = g_file_new_for_path (art_file_path); retval = g_file_copy (art_file, - album_art_file, + target_file, 0, NULL, NULL, NULL, - &err); - - if (err == NULL) { - if (symlink (album_art_file_path, target) != 0) { - g_debug ("symlink(%s, %s) error: %s", - album_art_file_path, target, - g_strerror (errno)); - retval = FALSE; - } else { - retval = TRUE; - } - } else { - g_debug ("%s", err->message); - g_clear_error (&err); - retval = FALSE; - } + error); - g_object_unref (album_art_file); g_object_unref (art_file); + g_object_unref (target_file); } + + g_free (sum2); } else { - g_debug ("Album art found in same directory but not a real JPEG file (trying to convert): '%s'", art_file_path); - retval = convert_from_other_format (art_file_path, - target, - album_art_file_path, - artist); - } + GFile *art_file; + GFile *album_art_file; + + /* If there's not yet a album-space-md5.jpg, make one, + * and symlink album-md5-md5.jpg to it */ + album_art_file = g_file_new_for_path (album_art_file_path); + art_file = g_file_new_for_path (art_file_path); + retval = g_file_copy (art_file, + album_art_file, + 0, + NULL, + NULL, + NULL, + error); + + if (retval) { + retval = symlink (album_art_file_path, target) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_SYMLINK_FAILED, + "Could not link '%s' to '%s': %s", + album_art_file_path, + target, + g_strerror (errno)); + } - g_free (sum1); + g_debug ("symlink(%s, %s) error: %s", + album_art_file_path, target, + g_strerror (errno)); + } + + g_object_unref (album_art_file); + g_object_unref (art_file); + } } else { - /* Can't read contents of the cover.jpg file ... */ - retval = FALSE; - } - } else if (g_str_has_suffix (art_file_path, "png")) { - if (!album_art_file_path) { - media_art_get_path (NULL, - title_stripped, - media_art_type_name[type], - NULL, - &album_art_file_path, - NULL); + g_debug ("Album art found in same directory but not a real JPEG file (trying to convert): '%s'", art_file_path); + retval = convert_from_other_format (art_file_path, + target, + album_art_file_path, + artist, + error); } - g_debug ("Album art (PNG) found in same directory being used:'%s'", art_file_path); - retval = convert_from_other_format (art_file_path, - target, - album_art_file_path, - artist); + g_free (sum1); + } else { + /* Can't read contents of the cover.jpg file ... */ + retval = FALSE; + } + } else if (g_str_has_suffix (art_file_path, "png")) { + if (!album_art_file_path) { + media_art_get_path (NULL, + title_stripped, + media_art_type_name[type], + NULL, + &album_art_file_path, + NULL); } - g_free (art_file_path); - g_free (album_art_file_path); + g_debug ("Album art (PNG) found in same directory being used:'%s'", art_file_path); + retval = convert_from_other_format (art_file_path, + target, + album_art_file_path, + artist, + error); } + g_free (art_file_path); + g_free (album_art_file_path); + g_free (target); g_free (artist_stripped); g_free (title_stripped); @@ -777,8 +957,6 @@ is_buffer_jpeg (const gchar *mime, const unsigned char *buffer, size_t buffer_len) { - gboolean is_jpeg; - if (buffer == NULL || buffer_len < 3) { return FALSE; } @@ -798,21 +976,19 @@ is_buffer_jpeg (const gchar *mime, } static gboolean -media_art_set (const unsigned char *buffer, - size_t len, - const gchar *mime, - MediaArtType type, - const gchar *artist, - const gchar *title) +media_art_set (const unsigned char *buffer, + size_t len, + const gchar *mime, + MediaArtType type, + const gchar *artist, + const gchar *title, + GError **error) { gchar *local_path; gchar *album_path; gchar *md5_album = NULL; gchar *md5_tmp = NULL; gchar *temp; - const gchar *save_path = NULL; - gboolean save_buffer = FALSE; - gboolean need_symlink = FALSE; gboolean retval = FALSE; g_return_val_if_fail (type > MEDIA_ART_NONE && type < MEDIA_ART_TYPE_COUNT, FALSE); @@ -854,7 +1030,7 @@ media_art_set (const unsigned char *buffer, * i) save buffer to jpeg only. */ if (type != MEDIA_ART_ALBUM || (artist == NULL || g_strcmp0 (artist, " ") == 0)) { - retval = media_art_buffer_to_jpeg (buffer, len, mime, local_path); + retval = media_art_buffer_to_jpeg (buffer, len, mime, local_path, error); g_debug ("Saving buffer to jpeg (%ld bytes) --> '%s'", len, local_path); g_free (local_path); @@ -875,21 +1051,29 @@ media_art_set (const unsigned char *buffer, if (!g_file_test (album_path, G_FILE_TEST_EXISTS)) { retval = TRUE; - if (media_art_buffer_to_jpeg (buffer, len, mime, album_path)) { + if (media_art_buffer_to_jpeg (buffer, len, mime, album_path, error)) { g_debug ("Saving buffer to jpeg (%ld bytes) --> '%s'", len, album_path); /* If album-space-md5.jpg doesn't * exist, make one and make a symlink * to album-md5-md5.jpg */ - if (symlink (album_path, local_path) != 0) { - g_debug ("Creating symlink '%s' --> '%s', %s", - album_path, - local_path, - retval ? g_strerror (errno) : "no error given"); + retval = symlink (album_path, local_path) == 0; - retval = FALSE; + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_SYMLINK_FAILED, + "Could not link '%s' to '%s': %s", + album_path, + local_path, + g_strerror (errno)); } + + g_debug ("Creating symlink '%s' --> '%s', %s", + album_path, + local_path, + retval ? g_strerror (errno) : "no error given"); } g_free (album_path); @@ -926,6 +1110,17 @@ media_art_set (const unsigned char *buffer, */ if (g_strcmp0 (md5_data, md5_album) == 0) { retval = symlink (album_path, local_path) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_SYMLINK_FAILED, + "Could not link '%s' to '%s': %s", + album_path, + local_path, + g_strerror (errno)); + } + g_debug ("Creating symlink '%s' --> '%s', %s", album_path, local_path, @@ -934,7 +1129,7 @@ media_art_set (const unsigned char *buffer, /* If album-space-md5.jpg isn't the same as * buffer, make a new album-md5-md5.jpg */ - retval = media_art_buffer_to_jpeg (buffer, len, mime, local_path); + retval = media_art_buffer_to_jpeg (buffer, len, mime, local_path, error); g_debug ("Saving buffer to jpeg (%ld bytes) --> '%s'", len, local_path); } @@ -955,7 +1150,7 @@ media_art_set (const unsigned char *buffer, temp = g_strdup_printf ("%s-tmp", album_path); /* If buffer isn't a JPEG */ - if (!media_art_buffer_to_jpeg (buffer, len, mime, temp)) { + if (!media_art_buffer_to_jpeg (buffer, len, mime, temp, error)) { /* Can't read temp file ... */ g_unlink (temp); @@ -977,6 +1172,17 @@ media_art_set (const unsigned char *buffer, * buffer, make a symlink to album-md5-md5.jpg */ retval = symlink (album_path, local_path) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_SYMLINK_FAILED, + "Could not link '%s' to '%s': %s", + album_path, + local_path, + g_strerror (errno)); + } + g_debug ("Creating symlink '%s' --> '%s', %s", album_path, local_path, @@ -986,6 +1192,17 @@ media_art_set (const unsigned char *buffer, * buffer, make a new album-md5-md5.jpg */ retval = g_rename (temp, local_path) == 0; + + if (!retval) { + g_set_error (error, + media_art_error_quark (), + MEDIA_ART_ERROR_RENAME_FAILED, + "Could not rename '%s' to '%s': %s", + temp, + local_path, + g_strerror (errno)); + } + g_debug ("Renaming temp file '%s' --> '%s', %s", temp, local_path, @@ -1007,13 +1224,15 @@ media_art_set (const unsigned char *buffer, } static FileInfo * -file_info_new (const gchar *local_uri, - const gchar *art_path) +file_info_new (MediaArtProcess *process, + const gchar *local_uri, + const gchar *art_path) { FileInfo *fi; fi = g_slice_new (FileInfo); + fi->process = g_object_ref (process); fi->local_uri = g_strdup (local_uri); fi->art_path = g_strdup (art_path); @@ -1023,23 +1242,33 @@ file_info_new (const gchar *local_uri, static void file_info_free (FileInfo *fi) { + if (!fi) { + return; + } + g_free (fi->local_uri); g_free (fi->art_path); + g_object_unref (fi->process); g_slice_free (FileInfo, fi); } static void -media_art_request_download (MediaArtType type, - const gchar *album, - const gchar *artist, - const gchar *local_uri, - const gchar *art_path) +media_art_request_download (MediaArtProcess *process, + MediaArtType type, + const gchar *album, + const gchar *artist, + const gchar *local_uri, + const gchar *art_path) { - if (connection) { + MediaArtProcessPrivate *private; + + private = media_art_process_get_instance_private (process); + + if (private->connection) { FileInfo *info; - if (disable_requests) { + if (private->disable_requests) { return; } @@ -1047,9 +1276,9 @@ media_art_request_download (MediaArtType type, return; } - info = file_info_new (local_uri, art_path); + info = file_info_new (process, local_uri, art_path); - g_dbus_connection_call (connection, + g_dbus_connection_call (private->connection, ALBUMARTER_SERVICE, ALBUMARTER_PATH, ALBUMARTER_INTERFACE, @@ -1069,14 +1298,18 @@ media_art_request_download (MediaArtType type, } static void -media_art_copy_to_local (const gchar *filename, - const gchar *local_uri) +media_art_copy_to_local (MediaArtProcess *process, + const gchar *filename, + const gchar *local_uri) { + MediaArtProcessPrivate *private; GSList *roots, *l; gboolean on_removable_device = FALSE; guint flen; - roots = storage_get_device_roots (storage, STORAGE_REMOVABLE, FALSE); + private = media_art_process_get_instance_private (process); + + roots = storage_get_device_roots (private->storage, STORAGE_REMOVABLE, FALSE); flen = strlen (filename); for (l = roots; l; l = l->next) { @@ -1133,17 +1366,20 @@ media_art_queue_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { + MediaArtProcessPrivate *private; GError *error = NULL; FileInfo *fi; GVariant *v; fi = user_data; + private = media_art_process_get_instance_private (fi->process); + v = g_dbus_connection_call_finish ((GDBusConnection *) source_object, res, &error); if (error) { if (error->code == G_DBUS_ERROR_SERVICE_UNKNOWN) { - disable_requests = TRUE; + private->disable_requests = TRUE; } else { g_warning ("%s", error->message); } @@ -1154,10 +1390,11 @@ media_art_queue_cb (GObject *source_object, g_variant_unref (v); } - if (storage && + if (private->storage && fi->art_path && g_file_test (fi->art_path, G_FILE_TEST_EXISTS)) { - media_art_copy_to_local (fi->art_path, + media_art_copy_to_local (fi->process, + fi->art_path, fi->local_uri); } @@ -1165,80 +1402,23 @@ media_art_queue_cb (GObject *source_object, } /** - * media_art_init: - * - * Initialize libmediaart. + * media_art_error_quark: * - * This function initializes cache hash tables, backend plugins, - * storage modules used for removable devices and connections to D-Bus. - * - * Returns: %TRUE if initialisation was successful, %FALSE otherwise. + * Returns: the #GQuark used to identify media art errors in + * GError structures. * * Since: 0.2.0 - */ -gboolean -media_art_init (void) -{ - GError *error = NULL; - - g_return_val_if_fail (initialized == FALSE, FALSE); - - media_art_plugin_init (0); - - /* Cache to know if we have already handled uris */ - media_art_cache = g_hash_table_new_full (g_str_hash, - g_str_equal, - (GDestroyNotify) g_free, - NULL); - - /* Signal handler for new album art from the extractor */ - connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); - - if (!connection) { - g_critical ("Could not connect to the D-Bus session bus, %s", - error ? error->message : "no error given."); - g_clear_error (&error); - return FALSE; - } - - storage = storage_new (); - if (!storage) { - g_critical ("Could not start storage module for removable media detection"); - return FALSE; - } - - initialized = TRUE; - - return TRUE; -} - -/** - * media_art_shutdown: - * - * Clean up and free the resources created and mentioned in media_art_init(). - * - * Since: 0.2.0 - */ -void -media_art_shutdown (void) + **/ +GQuark +media_art_error_quark (void) { - g_return_if_fail (initialized == TRUE); - - if (storage) { - g_object_unref (storage); - } + static GQuark error_quark = 0; - if (connection) { - g_object_unref (connection); + if (G_UNLIKELY (error_quark == 0)) { + error_quark = g_quark_from_static_string ("media-art-error-quark"); } - if (media_art_cache) { - g_hash_table_unref (media_art_cache); - } - - media_art_plugin_shutdown (); - - initialized = FALSE; + return error_quark; } static void @@ -1253,27 +1433,21 @@ set_mtime (const gchar *filename, static guint64 -get_mtime (GFile *file) +get_mtime (GFile *file, + GError **error) { GFileInfo *info; - GError *error = NULL; - guint64 mtime; + GError *local_error = NULL; + guint64 mtime; info = g_file_query_info (file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, - &error); + &local_error); - if (G_UNLIKELY (error)) { - gchar *uri; - - uri = g_file_get_uri (file); - g_message ("Could not get mtime for '%s': %s", - uri, - error->message); - g_free (uri); - g_error_free (error); + if (G_UNLIKELY (local_error != NULL)) { + g_propagate_error (error, local_error); mtime = 0; } else { mtime = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED); @@ -1283,191 +1457,327 @@ get_mtime (GFile *file) return mtime; } -guint64 -get_mtime_by_path (const gchar *path) -{ - GFile *file; - guint64 mtime; - - g_return_val_if_fail (path != NULL, 0); - - file = g_file_new_for_path (path); - - mtime = get_mtime (file); - - g_object_unref (file); - - return mtime; -} - - -guint64 -get_mtime_by_uri (const gchar *uri) +static gchar * +get_heuristic_for_parent_path (GFile *file, + MediaArtType type, + const gchar *artist, + const gchar *title) { - GFile *file; - guint64 mtime; + gchar *key; + gchar *parent_path = NULL; + GFile *parent; - g_return_val_if_fail (uri != NULL, 0); + if (!file) { + return NULL; + } - file = g_file_new_for_uri (uri); + parent = g_file_get_parent (file); + if (parent) { + parent_path = g_file_get_path (parent); + g_object_unref (parent); + } - mtime = get_mtime (file); + /* Just used for caching in our hash table */ + key = g_strdup_printf ("%s:%s:%s:%s", + parent_path ? parent_path : "", + media_art_type_name[type], + artist ? artist : "", + title ? title : ""); - g_object_unref (file); + g_free (parent_path); - return mtime; + return key; } - /** - * media_art_process_file: + * media_art_process_buffer: + * @process: Media art process object + * @type: The type of media + * @flags: The options given for how to process the media art + * @related_file: File related to the media art * @buffer: (array length=len)(allow-none): a buffer containing @file data, or %NULL * @len: length of @buffer, or 0 * @mime: MIME type of @buffer, or %NULL - * @type: The type of media - * @artist: The media file artist name, or %NULL - * @title: The media file title, or %NULL - * @file: File to be processed + * @artist: (allow-none): The artist name @file or %NULL + * @title: (allow-none): The title for @file or %NULL + * @error: Pointer to potential GLib / MediaArt error, or %NULL * - * Processes a media file. If you have extracted any embedded media art and - * passed this in as @buffer, the image data will be converted to the correct - * format and saved in the media art cache. - * - * If @buffer is %NULL, libmediaart will search the parent directory of @file - * for image files that are likely to be media art for @file, and if one is - * found it will be saved in the media art cache. + * Processes a memory buffer represented by @buffer and @len. If you + * have extracted any embedded media art and passed this in as + * @buffer, the image data will be converted to the correct format and + * saved in the media art cache. * * 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 the file could be processed. + * Returns: %TRUE if @file could be processed or %FALSE if @error is set. * - * Since: 0.2.0 + * Since: 0.5.0 */ gboolean -media_art_process_file (const guchar *buffer, - gsize len, - const gchar *mime, - MediaArtType type, - const gchar *artist, - const gchar *title, - GFile *file) +media_art_process_buffer (MediaArtProcess *process, + MediaArtType type, + MediaArtProcessFlags flags, + GFile *related_file, + const guchar *buffer, + gsize len, + const gchar *mime, + const gchar *artist, + const gchar *title, + GError **error) { GFile *cache_art_file, *local_art_file; - gchar *art_path, *uri; - gchar *local_art_uri = NULL; - gboolean processed = TRUE, a_exists, created = FALSE; - guint64 mtime, a_mtime = 0; + GError *local_error = NULL; + gchar *cache_art_path, *uri; + gboolean processed, created; + guint64 mtime, cache_mtime = 0; + g_return_val_if_fail (MEDIA_ART_IS_PROCESS (process), FALSE); g_return_val_if_fail (type > MEDIA_ART_NONE && type < MEDIA_ART_TYPE_COUNT, FALSE); - g_return_val_if_fail (G_IS_FILE (file), FALSE); + g_return_val_if_fail (G_IS_FILE (related_file), FALSE); + g_return_val_if_fail (buffer != NULL, FALSE); + g_return_val_if_fail (len < 0, FALSE); - uri = g_file_get_uri (file); - g_debug ("Processing media art: artist:'%s', title:'%s', type:'%s', uri:'%s'. Buffer is %ld bytes, mime:'%s'", + processed = created = FALSE; + + uri = g_file_get_uri (related_file); + g_debug ("Processing media art: artist:'%s', title:'%s', type:'%s', uri:'%s', flags:0x%.8x. Buffer is %ld bytes, mime:'%s'", artist ? artist : "", title ? title : "", media_art_type_name[type], uri, + flags, (long int) len, mime); - mtime = get_mtime (file); + mtime = get_mtime (related_file, &local_error); + if (local_error != NULL) { + g_debug ("Could not get mtime for related 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, + related_file, &cache_art_file, &local_art_file); - if (!cache_art_file) { - g_debug ("Album art path could not be obtained, not processing any further"); + 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 did not exist (%s)", + path); + g_free (path); + g_clear_error (&local_error); + } else 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_free (uri); + g_propagate_error (error, local_error); return FALSE; } - a_exists = g_file_query_exists (cache_art_file, NULL); - if (a_exists) { - a_mtime = get_mtime (cache_art_file); + cache_art_path = g_file_get_path (cache_art_file); + + if (flags & MEDIA_ART_PROCESS_FLAGS_FORCE || + cache_mtime == 0 || mtime > cache_mtime) { + processed = media_art_set (buffer, len, mime, type, artist, title, error); + set_mtime (cache_art_path, mtime); + } else { + g_debug ("Album art already exists for uri:'%s' as '%s'", + uri, + cache_art_path); + processed = TRUE; } - art_path = g_file_get_path (cache_art_file); - local_art_uri = g_file_get_uri (local_art_file); + 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 (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 + * @type: The type of media + * @flags: The options given for how to process the media art + * @file: File to be processed + * @artist: (allow-none): The artist name @file or %NULL + * @title: (allow-none): The title for @file or %NULL + * @error: Pointer to potential GLib / MediaArt error, or %NULL + * + * Process @file and check if media art exists and if it is up to date + * with @artist and @title provided. Either @artist OR @title can be + * %NULL, but they can not both be %NULL. + * + * NOTE: This function MAY retrieve media art for + * @artist and @title combinations. It is not guaranteed and depends + * on download services available over DBus at the time. + * + * In cases where download is unavailable, media_art_process_file() + * will only try to procure a cache for possible media art found in + * directories surrounding the location of @file. If a buffer or + * memory chunk needs to be saved to disk which has been retrieved + * from an MP3 (for example), you should use + * media_art_process_buffer(). + * + * The modification time (mtime) of @file is checked against the + * cached stored for @artist and @title. If the cache is old or + * doesn't exist, it will be updated. What this actually does is + * update the mtime of the cache (a symlink) on the disk. + * + * If there is no actual media art stored locally (for example, it's + * stored in a directory on a removable device), it is copied locally + * (usually to an XDG cache directory). + * + * 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, + MediaArtType type, + MediaArtProcessFlags flags, + GFile *file, + 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 no_cache_or_old; + guint64 mtime, cache_mtime; + + g_return_val_if_fail (MEDIA_ART_IS_PROCESS (process), FALSE); + g_return_val_if_fail (type > MEDIA_ART_NONE && type < MEDIA_ART_TYPE_COUNT, FALSE); + g_return_val_if_fail (G_IS_FILE (file), FALSE); + g_return_val_if_fail (artist != NULL || title != NULL, FALSE); + + private = media_art_process_get_instance_private (process); + + uri = g_file_get_uri (file); + g_debug ("Processing media art: artist:'%s', title:'%s', type:'%s', uri:'%s', flags:0x%.8x", + artist ? artist : "", + title ? title : "", + media_art_type_name[type], + uri, + flags); + + 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); - if ((buffer && len > 0) && ((!a_exists) || (a_exists && mtime > a_mtime))) { - processed = media_art_set (buffer, len, mime, type, artist, title); - set_mtime (art_path, mtime); - created = TRUE; + return FALSE; } - if ((!created) && ((!a_exists) || (a_exists && mtime > a_mtime))) { + media_art_get_file (artist, + title, + media_art_type_name[type], + file, + &cache_art_file, + &local_art_file); + + cache_art_path = g_file_get_path (cache_art_file); + + cache_mtime = get_mtime (cache_art_file, &local_error); + no_cache_or_old = cache_mtime == -1 || cache_mtime < mtime; + + if (no_cache_or_old) { /* If not, we perform a heuristic on the dir */ gchar *key; - gchar *dirname = NULL; - GFile *dirf; - dirf = g_file_get_parent (file); - if (dirf) { - dirname = g_file_get_path (dirf); - g_object_unref (dirf); - } + key = get_heuristic_for_parent_path (file, type, artist, title); - key = g_strdup_printf ("%i-%s-%s-%s", - type, - artist ? artist : "", - title ? title : "", - dirname ? dirname : ""); + if (!g_hash_table_lookup (private->media_art_cache, key)) { + gchar *local_art_uri; - g_free (dirname); + local_art_uri = g_file_get_uri (local_art_file); - if (!g_hash_table_lookup (media_art_cache, key)) { - if (!media_art_heuristic (artist, - title, - type, - uri, - local_art_uri)) { + if (!get_heuristic (type, uri, local_art_uri, artist, title, error)) { /* If the heuristic failed, we * request the download the * media-art to the media-art * downloaders */ - media_art_request_download (type, + media_art_request_download (process, + type, artist, title, local_art_uri, - art_path); + cache_art_path); + + /* FIXME: Should return TRUE or FALSE? */ } - set_mtime (art_path, mtime); + set_mtime (cache_art_path, mtime); - g_hash_table_insert (media_art_cache, + g_hash_table_insert (private->media_art_cache, key, GINT_TO_POINTER(TRUE)); + g_free (local_art_uri); } else { g_free (key); } } else { - if (!created) { - g_debug ("Album art already exists for uri:'%s' as '%s'", - uri, - 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)) { - media_art_copy_to_local (art_path, local_art_uri); - } + g_debug ("Album art already exists for uri:'%s' as '%s'", + uri, + cache_art_path); } if (cache_art_file) { @@ -1478,54 +1788,56 @@ media_art_process_file (const guchar *buffer, g_object_unref (local_art_file); } - g_free (art_path); - g_free (local_art_uri); + g_free (cache_art_path); g_free (uri); - return processed; + return TRUE; } - /** - * media_art_process: - * @buffer: (array length=len): A buffer of binary image data - * @len: The length of @buffer, in bytes - * @mime: The MIME type of the data stored in @buffer + * media_art_process_uri: + * @process: Media art process object * @type: The type of media that contained the image data - * @artist: (allow-none): Artist name of the media - * @title: (allow-none): Title of the media - * @uri: URI of the media file that contained the image data + * @flags: How the media art is processed + * @uri: URI of the media file that contained the data + * @artist: (allow-none): The artist name @uri or %NULL + * @title: (allow-none): The title for @uri or %NULL + * @error: Pointer to potential GLib / MediaArt error, or %NULL * - * This function is the same as media_art_process_file(), but takes the URI as - * a string rather than a #GFile object. + * This function calls media_art_process_file(), but takes the @uri as + * a string rather than a #GFile object. Either @artist OR @title can be + * %NULL, but they can not both be %NULL. * - * Returns: %TRUE in case of success, %FALSE otherwise. + * Returns: %TRUE if @uri could be processed or %FALSE if @error is set. * - * Since: 0.2.0 + * Since: 0.5.0 */ gboolean -media_art_process (const unsigned char *buffer, - size_t len, - const gchar *mime, - MediaArtType type, - const gchar *artist, - const gchar *title, - const gchar *uri) +media_art_process_uri (MediaArtProcess *process, + MediaArtType type, + MediaArtProcessFlags flags, + const gchar *uri, + const gchar *artist, + const gchar *title, + GError **error) { GFile *file; gboolean result; + g_return_val_if_fail (MEDIA_ART_IS_PROCESS (process), FALSE); + g_return_val_if_fail (type > MEDIA_ART_NONE && type < MEDIA_ART_TYPE_COUNT, FALSE); g_return_val_if_fail (uri != NULL, FALSE); + g_return_val_if_fail (artist != NULL || title != NULL, FALSE); file = g_file_new_for_uri (uri); - result = media_art_process_file (buffer, - len, - mime, + result = media_art_process_file (process, type, + flags, + file, artist, title, - file); + error); g_object_unref (file); diff --git a/libmediaart/extract.h b/libmediaart/extract.h index 3744923..e5ab8fa 100644 --- a/libmediaart/extract.h +++ b/libmediaart/extract.h @@ -46,25 +46,111 @@ typedef enum { MEDIA_ART_TYPE_COUNT } MediaArtType; -gboolean media_art_init (void); -void media_art_shutdown (void); - -gboolean media_art_process (const unsigned char *buffer, - size_t len, - const gchar *mime, - MediaArtType type, - const gchar *artist, - const gchar *title, - const gchar *uri); - -gboolean media_art_process_file (const guchar *buffer, - gsize len, - const gchar *mime, - MediaArtType type, - const gchar *artist, - const gchar *title, - GFile *file); +/** + * MediaArtProcessFlags: + * @MEDIA_ART_PROCESS_FLAGS_NONE: Normal operation. + * @MEDIA_ART_PROCESS_FLAGS_FORCE: Force media art to be re-saved to disk even if it already exists and the related file or URI has the same modified time (mtime). + * + * This type categorized the flags used when processing media art. + * + * Since: 0.3.0 + */ +typedef enum { + MEDIA_ART_PROCESS_FLAGS_NONE = 0, + MEDIA_ART_PROCESS_FLAGS_FORCE = 1 << 0, +} MediaArtProcessFlags; + +/** + * MediaArtError: + * @MEDIA_ART_ERROR_NO_STORAGE: Storage information is unknown, we + * have no knowledge about removable media. + * @MEDIA_ART_ERROR_NO_TITLE: Title is required, but was not provided, + * or was empty. + * @MEDIA_ART_ERROR_SYMLINK_FAILED: A call to symlink() failed + * resulting in the incorrect storage of media art. + * @MEDIA_ART_ERROR_RENAME_FAILED: File could not be renamed. + * + * Enumeration values used in errors returned by the + * #MediaArtError API. + * + * Since: 0.2.0 + **/ +typedef enum { + MEDIA_ART_ERROR_NO_STORAGE, + MEDIA_ART_ERROR_NO_TITLE, + MEDIA_ART_ERROR_SYMLINK_FAILED, + MEDIA_ART_ERROR_RENAME_FAILED +} MediaArtError; + + +/** + * media_art_error_quark: + * + * A #GQuark representing the type of #GError for #MediaArtProcess failures. + * + * Since: 0.2.0 + **/ +GQuark media_art_error_quark (void) G_GNUC_CONST; + + +#define MEDIA_ART_TYPE_PROCESS (media_art_process_get_type()) +#define MEDIA_ART_PROCESS(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MEDIA_ART_TYPE_PROCESS, MediaArtProcess)) +#define MEDIA_ART_PROCESS_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MEDIA_ART_TYPE_PROCESS, MediaArtProcessClass)) +#define MEDIA_ART_IS_PROCESS(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MEDIA_ART_TYPE_PROCESS)) +#define MEDIA_ART_IS_PROCESS_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MEDIA_ART_TYPE_PROCESS)) +#define MEDIA_ART_PROCESS_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MEDIA_ART_TYPE_PROCESS, MediaArtProcessClass)) + +typedef struct _MediaArtProcess MediaArtProcess; +typedef struct _MediaArtProcessClass MediaArtProcessClass; + +/** + * MediaArtProcess: + * @parent: parent object + * + * A class implementation for processing and extracting media art. + **/ +struct _MediaArtProcess { + GObject parent; +}; + +/** + * MediaArtProcessClass: + * @parent: parent object class + * + * Prototype for the class. + **/ +struct _MediaArtProcessClass { + GObjectClass parent; +}; + + +GType media_art_process_get_type (void) G_GNUC_CONST; +MediaArtProcess *media_art_process_new (GError **error); +gboolean media_art_process_uri (MediaArtProcess *process, + MediaArtType type, + MediaArtProcessFlags flags, + const gchar *uri, + const gchar *artist, + const gchar *title, + GError **error); +gboolean media_art_process_file (MediaArtProcess *process, + MediaArtType type, + MediaArtProcessFlags flags, + GFile *file, + const gchar *artist, + const gchar *title, + GError **error); +gboolean media_art_process_buffer (MediaArtProcess *process, + MediaArtType type, + MediaArtProcessFlags flags, + GFile *related_file, + const guchar *buffer, + gsize len, + const gchar *mime, + const gchar *artist, + const gchar *title, + GError **error); G_END_DECLS -#endif /* __LIBMEDIAART_UTILS_H__ */ +#endif /* __LIBMEDIAART_EXTRACT_H__ */ diff --git a/libmediaart/extractdummy.c b/libmediaart/extractdummy.c index 195de31..f80ccd2 100644 --- a/libmediaart/extractdummy.c +++ b/libmediaart/extractdummy.c @@ -22,30 +22,98 @@ #include "extractgeneric.h" +/** + * SECTION:plugins + * @title: Plugins + * @short_description: Plugins for cache conversion. + * @include: libmediaart/mediaart.h + * + * Plugins are provided to allow different systems to make use of + * existing file format conversion APIs. By default, a GdkPixbuf and + * Qt implementation are provided. This API allows new implementations + * to be provided. + * + **/ + +/** + * media_art_plugin_init: + * @max_width: The maximum width that an image is allowed to be + * + * This function facilitates a plugin's need to create any + * internal caches before anything else is done. This function must + * exist in each plugin and is called immediately after the plugin is + * loaded. Conversely, media_art_plugin_shutdown() is called before + * tear down of the plugin system or plugin itself. + * + * Since: 0.1.0 + */ void media_art_plugin_init (gint max_width) { /* Initialize something */ } +/** + * media_art_plugin_shutdown: + * + * This function facilitates a plugin's need to clean up any + * internal caches. This function must exist in each plugin and is + * called immediately after the plugin is loaded. Conversely, + * media_art_plugin_init() is called after the plugin is loaded. + * + * Since: 0.1.0 + */ void media_art_plugin_shutdown (void) { /* Shutdown something */ } +/** + * media_art_file_to_jpeg: + * @filename: Original file name (not URI) to convert + * @target: Output file name (not URI) to save converted content to + * @error: A #GError to use upon failure, or %NULL + * + * Save @filename to @target as JPEG format. The @filename may not be + * a JPEG in the first place. + * + * Returns: %TRUE if conversion was successful, otherwise %FALSE is + * returned if @error is set. + * + * Since: 0.1.0 + */ gboolean -media_art_file_to_jpeg (const gchar *filename, - const gchar *target) +media_art_file_to_jpeg (const gchar *filename, + const gchar *target, + GError **error) { return FALSE; } +/** + * media_art_buffer_to_jpeg: + * @buffer: Raw buffer representing content to save + * @len: Length of @buffer to use + * @buffer_mime: MIME type for @buffer + * @target: Output file name (not URI) to save converted content to + * @error: A #GError to use upon failure, or %NULL + * + * This function performs the same operation as + * media_art_file_to_jpeg() with the exception that a raw @buffer can + * be used instead providing @len and the @buffer_mime too. + * + * Returns: %TRUE if conversion was successful, otherwise %FALSE is + * returned if @error is set. + * + * Since: 0.1.0 + */ gboolean -media_art_buffer_to_jpeg (const unsigned char *buffer, - size_t len, - const gchar *buffer_mime, - const gchar *target) +media_art_buffer_to_jpeg (const unsigned char *buffer, + size_t len, + const gchar *buffer_mime, + const gchar *target, + GError **error) { return FALSE; } diff --git a/libmediaart/extractgeneric.h b/libmediaart/extractgeneric.h index b48d2be..8fa114b 100644 --- a/libmediaart/extractgeneric.h +++ b/libmediaart/extractgeneric.h @@ -20,7 +20,6 @@ * Philip Van Hoof <philip@codeminded.be> */ - #ifndef __LIBMEDIAART_EXTRACTGENERIC_H__ #define __LIBMEDIAART_EXTRACTGENERIC_H__ @@ -32,15 +31,17 @@ G_BEGIN_DECLS -void media_art_plugin_init (gint max_width); +void media_art_plugin_init (gint max_width); void media_art_plugin_shutdown (void); -gboolean media_art_file_to_jpeg (const gchar *filename, - const gchar *target); -gboolean media_art_buffer_to_jpeg (const unsigned char *buffer, - size_t len, - const gchar *buffer_mime, - const gchar *target); +gboolean media_art_file_to_jpeg (const gchar *filename, + const gchar *target, + GError **error); +gboolean media_art_buffer_to_jpeg (const unsigned char *buffer, + size_t len, + const gchar *buffer_mime, + const gchar *target, + GError **error); G_END_DECLS diff --git a/libmediaart/extractpixbuf.c b/libmediaart/extractpixbuf.c index 2f823fc..3b2854b 100644 --- a/libmediaart/extractpixbuf.c +++ b/libmediaart/extractpixbuf.c @@ -40,28 +40,28 @@ media_art_plugin_shutdown (void) } gboolean -media_art_file_to_jpeg (const gchar *filename, - const gchar *target) +media_art_file_to_jpeg (const gchar *filename, + const gchar *target, + GError **error) { GdkPixbuf *pixbuf; - GError *error = NULL; + GError *local_error = NULL; /* TODO: Add resizing support */ - pixbuf = gdk_pixbuf_new_from_file (filename, &error); - - if (error) { - g_clear_error (&error); + pixbuf = gdk_pixbuf_new_from_file (filename, &local_error); + if (local_error) { + g_propagate_error (error, local_error); return FALSE; - } else { - gdk_pixbuf_save (pixbuf, target, "jpeg", &error, NULL); - g_object_unref (pixbuf); + } - if (error) { - g_clear_error (&error); - return FALSE; - } + gdk_pixbuf_save (pixbuf, target, "jpeg", &local_error, NULL); + g_object_unref (pixbuf); + + if (local_error) { + g_propagate_error (error, local_error); + return FALSE; } return TRUE; @@ -87,11 +87,14 @@ size_prepared_cb (GdkPixbufLoader *loader, } gboolean -media_art_buffer_to_jpeg (const unsigned char *buffer, - size_t len, - const gchar *buffer_mime, - const gchar *target) +media_art_buffer_to_jpeg (const unsigned char *buffer, + size_t len, + const gchar *buffer_mime, + const gchar *target, + GError **error) { + GError *local_error = NULL; + if (max_width_in_bytes < 0) { g_debug ("Not saving album art from buffer, disabled in config"); return TRUE; @@ -103,11 +106,12 @@ media_art_buffer_to_jpeg (const unsigned char *buffer, g_strcmp0 (buffer_mime, "JPG") == 0) && (buffer && len > 2 && buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)) { g_debug ("Saving album art using raw data as uri:'%s'", target); - g_file_set_contents (target, (const gchar *) buffer, (gssize) len, NULL); + if (!g_file_set_contents (target, (const gchar *) buffer, (gssize) len, error)) { + return FALSE; + } } else { GdkPixbuf *pixbuf; GdkPixbufLoader *loader; - GError *error = NULL; g_debug ("Saving album art using GdkPixbufLoader for uri:'%s' (max width:%d)", target, @@ -121,11 +125,11 @@ media_art_buffer_to_jpeg (const unsigned char *buffer, NULL); } - if (!gdk_pixbuf_loader_write (loader, buffer, len, &error)) { - g_warning ("Could not write with GdkPixbufLoader when setting album art, %s", - error ? error->message : "no error given"); + if (!gdk_pixbuf_loader_write (loader, buffer, len, &local_error)) { + g_warning ("Could not write with GdkPixbufLoader when setting media art, %s", + local_error ? local_error->message : "no error given"); - g_clear_error (&error); + g_propagate_error (error, local_error); gdk_pixbuf_loader_close (loader, NULL); g_object_unref (loader); @@ -135,7 +139,9 @@ media_art_buffer_to_jpeg (const unsigned char *buffer, pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); if (pixbuf == NULL) { - g_warning ("Could not get pixbuf from GdkPixbufLoader when setting album art"); + g_warning ("Could not get pixbuf from GdkPixbufLoader when setting media art"); + + /* FIXME: Set error here */ gdk_pixbuf_loader_close (loader, NULL); g_object_unref (loader); @@ -143,21 +149,24 @@ media_art_buffer_to_jpeg (const unsigned char *buffer, return FALSE; } - if (!gdk_pixbuf_save (pixbuf, target, "jpeg", &error, NULL)) { - g_warning ("Could not save GdkPixbuf when setting album art, %s", - error ? error->message : "no error given"); + if (!gdk_pixbuf_save (pixbuf, target, "jpeg", &local_error, NULL)) { + g_warning ("Could not save GdkPixbuf when setting media art, %s", + local_error ? local_error->message : "no error given"); - g_clear_error (&error); + g_propagate_error (error, local_error); gdk_pixbuf_loader_close (loader, NULL); g_object_unref (loader); return FALSE; } - if (!gdk_pixbuf_loader_close (loader, &error)) { - g_warning ("Could not close GdkPixbufLoader when setting album art, %s", - error ? error->message : "no error given"); - g_clear_error (&error); + if (!gdk_pixbuf_loader_close (loader, &local_error)) { + g_warning ("Could not close GdkPixbufLoader when setting media art, %s", + local_error ? local_error->message : "no error given"); + + g_propagate_error (error, local_error); + + return FALSE; } g_object_unref (loader); diff --git a/libmediaart/extractqt.cpp b/libmediaart/extractqt.cpp index d39ddd7..dda324f 100644 --- a/libmediaart/extractqt.cpp +++ b/libmediaart/extractqt.cpp @@ -82,8 +82,9 @@ media_art_plugin_shutdown (void) } gboolean -media_art_file_to_jpeg (const gchar *filename, - const gchar *target) +media_art_file_to_jpeg (const gchar *filename, + const gchar *target, + GError **error) { if (max_width_in_bytes < 0) { g_debug ("Not saving album art from file, disabled in config"); @@ -91,6 +92,7 @@ media_art_file_to_jpeg (const gchar *filename, } /* TODO: Add resizing support */ + /* TODO: Add error reporting */ QFile file (filename); @@ -129,10 +131,11 @@ media_art_file_to_jpeg (const gchar *filename, } gboolean -media_art_buffer_to_jpeg (const unsigned char *buffer, - size_t len, - const gchar *buffer_mime, - const gchar *target) +media_art_buffer_to_jpeg (const unsigned char *buffer, + size_t len, + const gchar *buffer_mime, + const gchar *target, + GError **error) { if (max_width_in_bytes < 0) { g_debug ("Not saving album art from buffer, disabled in config"); @@ -144,14 +147,16 @@ media_art_buffer_to_jpeg (const unsigned char *buffer, (g_strcmp0 (buffer_mime, "image/jpeg") == 0 || g_strcmp0 (buffer_mime, "JPG") == 0) && (buffer && len > 2 && buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)) { - g_debug ("Saving album art using raw data as uri:'%s'", - target); - g_file_set_contents (target, (const gchar*) buffer, (gssize) len, NULL); + g_debug ("Saving album art using raw data as uri:'%s'", target); + if (!g_file_set_contents (target, (const gchar*) buffer, (gssize) len, error)) { + return FALSE; + } } else { QImageReader *reader = NULL; QByteArray array; /* TODO: Add resizing support */ + /* TODO: Add error reporting */ array = QByteArray ((const char *) buffer, (int) len); diff --git a/libmediaart/storage.c b/libmediaart/storage.c index feb22f6..36213d7 100644 --- a/libmediaart/storage.c +++ b/libmediaart/storage.c @@ -28,7 +28,7 @@ #include "marshal.h" /** - * SECTION:-storage + * SECTION:storage * @short_description: Removable storage and mount point convenience API * @include: libmediaart/mediaart.h * @@ -140,8 +140,6 @@ storage_init (Storage *storage) { StoragePrivate *priv; - g_message ("Initializing Storage..."); - priv = STORAGE_GET_PRIVATE (storage); priv->mounts = g_node_new (NULL); @@ -163,8 +161,6 @@ storage_init (Storage *storage) g_signal_connect_object (priv->volume_monitor, "mount-added", G_CALLBACK (mount_added_cb), storage, 0); - g_message ("Mount monitors set up for to watch for added, removed and pre-unmounts..."); - /* Get all mounts and set them up */ if (!mounts_setup (storage)) { return; @@ -702,7 +698,7 @@ mounts_setup (Storage *storage) mounts = g_volume_monitor_get_mounts (priv->volume_monitor); if (!mounts) { - g_message ("No mounts found to iterate"); + g_debug ("No mounts found to iterate"); return TRUE; } @@ -752,19 +748,19 @@ mount_remove (Storage *storage, if (node) { info = node->data; - g_message ("Mount:'%s' with UUID:'%s' now unmounted from:'%s'", - name, - info->uuid, - mount_point); + g_debug ("Mount:'%s' with UUID:'%s' now unmounted from:'%s'", + name, + info->uuid, + mount_point); g_signal_emit (storage, signals[MOUNT_POINT_REMOVED], 0, info->uuid, mount_point, NULL); g_hash_table_remove (priv->mounts_by_uuid, info->uuid); mount_node_free (node); } else { - g_message ("Mount:'%s' now unmounted from:'%s' (was not tracked)", - name, - mount_point); + g_debug ("Mount:'%s' now unmounted from:'%s' (was not tracked)", + name, + mount_point); } g_free (name); |