[gnome-games] utils: Add LocalCover



commit 6b3307990c6c5160eff6197a073c93b9d92bdc34
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed Aug 10 16:55:03 2016 +0200

    utils: Add LocalCover
    
    This will be used in the next commits to look for image files named
    similarly to a game file or named as cover files typically are.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769676

 src/Makefile.am            |    1 +
 src/utils/local-cover.vala |  108 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index be548d6..25412ad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -116,6 +116,7 @@ gnome_games_SOURCES = \
        utils/filename-title.vala \
        utils/fingerprint.vala \
        utils/grep.vala \
+       utils/local-cover.vala \
        utils/string-input-stream.vala \
        \
        config.vala \
diff --git a/src/utils/local-cover.vala b/src/utils/local-cover.vala
new file mode 100644
index 0000000..a265432
--- /dev/null
+++ b/src/utils/local-cover.vala
@@ -0,0 +1,108 @@
+// This file is part of GNOME Games. License: GPLv3
+
+public class Games.LocalCover : Object, Cover {
+       private string uri;
+       private bool resolved;
+       private GLib.Icon? icon;
+
+       public LocalCover (string uri) {
+               this.uri = uri;
+       }
+
+       public GLib.Icon? get_cover () {
+               if (resolved)
+                       return icon;
+
+               resolved = true;
+
+               string? cover_path;
+               try {
+                       cover_path = get_cover_path ();
+               }
+               catch (Error e) {
+                       debug (e.message);
+
+                       return null;
+               }
+
+               if (cover_path == null)
+                       return null;
+
+               var file = File.new_for_path (cover_path);
+               icon = new FileIcon (file);
+
+               return icon;
+       }
+
+       private string? get_cover_path () throws Error {
+               var cover_path = get_sibbling_cover_path ();
+               if (FileUtils.test (cover_path, FileTest.EXISTS))
+                       return cover_path;
+
+               cover_path = get_directory_cover_path ();
+               if (FileUtils.test (cover_path, FileTest.EXISTS))
+                       return cover_path;
+
+               return null;
+       }
+
+       private string? get_sibbling_cover_path () throws Error {
+               var file = File.new_for_uri (uri);
+               var parent = file.get_parent ();
+               if (parent == null)
+                       return null;
+
+               var basename = file.get_basename ();
+               var splitted_basename = basename.split (".");
+               var prefix = splitted_basename.length == 1 ? basename : string.joinv (".", 
splitted_basename[0:splitted_basename.length - 1]);
+
+               string cover_path = null;
+               var directory = new Directory (parent);
+               directory.foreach ("*", (sibbling) => {
+                       var sibbling_basename = sibbling.get_name ();
+                       if (sibbling_basename == basename)
+                               return false;
+
+                       if (!sibbling_basename.has_prefix (prefix))
+                               return false;
+
+                       var type = sibbling.get_content_type ();
+                       if (!type.has_prefix ("image"))
+                               return false;
+
+                       var sibbling_file = parent.get_child (sibbling_basename);
+                       cover_path = sibbling_file.get_path ();
+
+                       return true;
+               });
+
+               return cover_path;
+       }
+
+       private string? get_directory_cover_path () throws Error {
+               var file = File.new_for_uri (uri);
+               var parent = file.get_parent ();
+               if (parent == null)
+                       return null;
+
+               string cover_path = null;
+               var directory = new Directory (parent);
+               directory.foreach ("*", (sibbling) => {
+                       var sibbling_basename = sibbling.get_name ();
+                       if (!sibbling_basename.has_prefix ("cover.") &&
+                           !sibbling_basename.has_prefix ("folder."))
+                               return false;
+
+                       var type = sibbling.get_content_type ();
+                       if (!type.has_prefix ("image"))
+                               return false;
+
+                       var sibbling_file = parent.get_child (sibbling_basename);
+                       cover_path = sibbling_file.get_path ();
+
+                       return true;
+               });
+
+               return cover_path;
+       }
+}


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