From 4447c442f5ef5fcb7f89ad371add1fd7be0ca308 Mon Sep 17 00:00:00 2001 From: "Zeeshan Ali (Khattak)" Date: Thu, 15 Jul 2010 20:35:18 +0300 Subject: [PATCH] core: Clean-up MediaArtStore code --- src/rygel/rygel-media-art-store.vala | 181 ++++++++++++++++++----------------- 1 file changed, 91 insertions(+), 90 deletions(-) diff --git a/src/rygel/rygel-media-art-store.vala b/src/rygel/rygel-media-art-store.vala index ab2b3b0..18f2365 100644 --- a/src/rygel/rygel-media-art-store.vala +++ b/src/rygel/rygel-media-art-store.vala @@ -40,44 +40,12 @@ public class Rygel.MediaArtStore : GLib.Object { private string directory; - private MediaArtStore () throws MediaArtStoreError { - var dir = Path.build_filename (Environment.get_user_cache_dir (), - "media-art"); - var file = File.new_for_path (dir); - - if (!file.query_exists (null)) { - var message = _("Failed to find media-art directory"); - - throw new MediaArtStoreError.NO_DIR (message); - } - - this.directory = dir; - try { - var regex_string = Regex.escape_string (invalid_chars); - char_remove_regex = new Regex ("[%s]".printf (regex_string)); - regex_string = Regex.escape_string (convert_chars); - char_convert_regex = new Regex ("[%s]".printf (regex_string)); - space_compress_regex = new Regex ("\\s+"); - block_regexes = new Regex[0]; - - foreach (var block in blocks) { - var block_re = block_pattern.printf ( - Regex.escape_string ("%C".printf (block[0])), - Regex.escape_string ("%C".printf (block[1])), - Regex.escape_string ("%C".printf (block[1]))); - block_regexes += new Regex (block_re); - } - } catch (RegexError error) { - assert_not_reached (); - } - } - public static MediaArtStore? get_default () { if (first_time) { try { media_art_store = new MediaArtStore (); } catch (MediaArtStoreError error) { - warning (_("No media art available: %s"), error.message); + warning ("No media art available: %s", error.message); } } @@ -86,10 +54,43 @@ public class Rygel.MediaArtStore : GLib.Object { return media_art_store; } + public Thumbnail? find_media_art (MediaItem item, + bool simple = false) throws Error { + string[] types = { "track", "album", "artist", "podcast", "radio" }; + File file = null; + + foreach (var type in types) { + file = this.get_media_art_file (type, item, simple); + if (file.query_exists (null)) { + break; + } else { + file = null; + } + } + + if (file == null) { + return null; + } + + var info = file.query_info (FILE_ATTRIBUTE_ACCESS_CAN_READ + "," + + FILE_ATTRIBUTE_STANDARD_SIZE, + FileQueryInfoFlags.NONE, + null); + if (!info.get_attribute_boolean (FILE_ATTRIBUTE_ACCESS_CAN_READ)) { + return null; + } + + var thumb = new AlbumArt (); + thumb.uri = file.get_uri (); + thumb.size = (long) info.get_size (); + + return thumb; + } + public Thumbnail? find_media_art_any (MediaItem item) throws Error { - var thumb = find_media_art (item); + var thumb = this.find_media_art (item); if (thumb == null) { - thumb = find_media_art (item, true); + thumb = this.find_media_art (item, true); } return thumb; @@ -99,13 +100,14 @@ public class Rygel.MediaArtStore : GLib.Object { MediaItem item, bool simple = false) { string hash; - string suffix = "jpeg"; + string suffix; if (simple) { - hash = get_simple_hash (type, item); + hash = this.get_simple_hash (type, item); suffix = "jpg"; } else { - hash = get_hash (type, item); + hash = this.get_hash (type, item); + suffix = "jpeg"; } var file_path = "%s-%s.%s".printf (type, hash, suffix); @@ -114,19 +116,36 @@ public class Rygel.MediaArtStore : GLib.Object { return File.new_for_path (path); } - private string normalize_and_hash (string? input, bool utf8_only = true) { - string normalized = " "; - if (input != null && input != "") { - if (utf8_only) { - normalized = input; - } else { - normalized = albumart_strip_invalid_entities (input); - normalized = normalized.down (); - } - normalized = normalized.normalize (-1, NormalizeMode.ALL); + private MediaArtStore () throws MediaArtStoreError { + var dir = Path.build_filename (Environment.get_user_cache_dir (), + "media-art"); + var file = File.new_for_path (dir); + + if (!file.query_exists (null)) { + var message = "Failed to find media-art directory"; + + throw new MediaArtStoreError.NO_DIR (message); } - return Checksum.compute_for_string (ChecksumType.MD5, normalized); + this.directory = dir; + try { + var regex_string = Regex.escape_string (invalid_chars); + char_remove_regex = new Regex ("[%s]".printf (regex_string)); + regex_string = Regex.escape_string (convert_chars); + char_convert_regex = new Regex ("[%s]".printf (regex_string)); + space_compress_regex = new Regex ("\\s+"); + block_regexes = new Regex[0]; + + foreach (var block in blocks) { + var block_re = block_pattern.printf ( + Regex.escape_string ("%C".printf (block[0])), + Regex.escape_string ("%C".printf (block[1])), + Regex.escape_string ("%C".printf (block[1]))); + block_regexes += new Regex (block_re); + } + } catch (RegexError error) { + assert_not_reached (); + } } private string get_simple_hash (string type, MediaItem item) { @@ -134,19 +153,19 @@ public class Rygel.MediaArtStore : GLib.Object { switch (type) { case "artist": case "radio": - hash = normalize_and_hash (item.author); + hash = this.normalize_and_hash (item.author); break; case "podcast": - hash = normalize_and_hash (item.title); + hash = this.normalize_and_hash (item.title); break; case "album": - hash = normalize_and_hash (item.author + "\t" + - item.album); + hash = this.normalize_and_hash (item.author + "\t" + + item.album); break; case "track": - hash = normalize_and_hash (item.author + "\t" + - item.album + "\t" + - item.title); + hash = this.normalize_and_hash (item.author + "\t" + + item.album + "\t" + + item.title); break; default: assert_not_reached (); @@ -159,18 +178,18 @@ public class Rygel.MediaArtStore : GLib.Object { string b = null, c = null; switch (type) { case "track": - b = normalize_and_hash (item.author, false) + "-" + - normalize_and_hash (item.album, false); - c = normalize_and_hash (item.title, false); + b = this.normalize_and_hash (item.author, false) + "-" + + this.normalize_and_hash (item.album, false); + c = this.normalize_and_hash (item.title, false); break; case "album": case "artist": - b = normalize_and_hash (item.author, false); - c = normalize_and_hash (item.album, false); + b = this.normalize_and_hash (item.author, false); + c = this.normalize_and_hash (item.album, false); break; case "radio": case "podcast": - b = normalize_and_hash (item.title, false); + b = this.normalize_and_hash (item.title, false); c = PLACEHOLDER_HASH; break; } @@ -178,40 +197,22 @@ public class Rygel.MediaArtStore : GLib.Object { return "%s-%s".printf (b, c); } - public Thumbnail? find_media_art (MediaItem item, - bool simple = false) throws Error { - string[] types = { "track", "album", "artist", "podcast", "radio" }; - File file = null; - - foreach (var type in types) { - file = get_media_art_file (type, item, simple); - if (file.query_exists (null)) { - break; + private string normalize_and_hash (string? input, bool utf8_only = true) { + string normalized = " "; + if (input != null && input != "") { + if (utf8_only) { + normalized = input; } else { - file = null; + normalized = this.strip_invalid_entities (input); + normalized = normalized.down (); } + normalized = normalized.normalize (-1, NormalizeMode.ALL); } - if (file == null) { - return null; - } - - var info = file.query_info (FILE_ATTRIBUTE_ACCESS_CAN_READ + "," + - FILE_ATTRIBUTE_STANDARD_SIZE, - FileQueryInfoFlags.NONE, - null); - if (!info.get_attribute_boolean (FILE_ATTRIBUTE_ACCESS_CAN_READ)) { - return null; - } - - var thumb = new AlbumArt (); - thumb.uri = file.get_uri (); - thumb.size = (long) info.get_size (); - - return thumb; + return Checksum.compute_for_string (ChecksumType.MD5, normalized); } - string albumart_strip_invalid_entities (string original) { + string strip_invalid_entities (string original) { string p; p = original; -- 2.7.4