[gnome-games/wip/theawless/local-cover-speed: 1/2] local-cover: Check only for possible cover files



commit 9267fa0245b5cca52ec1d8896851ec66a067ed60
Author: theawless <theawless gmail com>
Date:   Sun Oct 7 04:07:48 2018 +0530

    local-cover: Check only for possible cover files
    
    Vastly reduce the number of files that need to be investigated for being
    a cover by searching only for possible extensions and prefixes.

 src/utils/local-cover.vala | 98 +++++++++++++++-------------------------------
 1 file changed, 31 insertions(+), 67 deletions(-)
---
diff --git a/src/utils/local-cover.vala b/src/utils/local-cover.vala
index f2ac42a4..53e922d8 100644
--- a/src/utils/local-cover.vala
+++ b/src/utils/local-cover.vala
@@ -3,6 +3,16 @@
 public class Games.LocalCover : Object, Cover {
        private Uri uri;
        private GLib.Icon? icon;
+       private GenericSet<string> extensions;
+
+       construct {
+               extensions = new GenericSet<string> (str_hash, str_equal);
+
+               extensions.add ("cover");
+               foreach (var format in Gdk.Pixbuf.get_formats ())
+                       foreach (var extension in format.get_extensions ())
+                               extensions.add (extension);
+       }
 
        public LocalCover (Uri uri) {
                this.uri = uri;
@@ -12,90 +22,44 @@ public class Games.LocalCover : Object, Cover {
                if (icon != null)
                        return icon;
 
-               try {
-                       var cover_path = yield get_cover_path ();
-                       if (cover_path != null)
-                               load_cover (cover_path);
-               }
-               catch (Error e) {
-                       warning (e.message);
-               }
+               load_cover ();
 
                return icon;
        }
 
-       private async string? get_cover_path () throws Error {
-               var cover_path = yield get_sibbling_cover_path ();
+       private string get_cover_path () {
+               var basename = uri.to_file ().get_basename ();
+               var prefix = basename.substring (0, basename.last_index_of ("."));
+
+               var cover_path = get_cover_path_for (prefix);
                if (cover_path == null)
-                       cover_path = yield get_directory_cover_path ();
+                       cover_path = get_cover_path_for ("folder");
+               if (cover_path == null)
+                       cover_path = get_cover_path_for ("cover");
 
                return cover_path;
        }
 
-       private async string? get_sibbling_cover_path () throws Error {
-               var file = uri.to_file ();
-               var parent = file.get_parent ();
+       private string? get_cover_path_for (string prefix) {
+               var parent = uri.to_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);
-               var attributes = string.join (",", FileAttribute.STANDARD_NAME, 
FileAttribute.STANDARD_FAST_CONTENT_TYPE);
-               yield directory.foreach_async (attributes, (sibbling) => {
-                       var sibbling_basename = sibbling.get_name ();
-                       if (sibbling_basename == basename)
-                               return false;
-
-                       if (!sibbling_basename.has_prefix (prefix))
-                               return false;
+               foreach (var extension in extensions.get_values ()) {
+                       var cover_basename = @"$prefix.$extension";
+                       var cover_path = Path.build_filename (parent.get_path (), cover_basename);
 
-                       var type = sibbling.get_attribute_string (FileAttribute.STANDARD_FAST_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;
-               });
+                       if (FileUtils.test (cover_path, FileTest.EXISTS))
+                               return cover_path;
+               }
 
-               return cover_path;
+               return null;
        }
 
-       private async string? get_directory_cover_path () throws Error {
-               var file = uri.to_file ();
-               var parent = file.get_parent ();
-               if (parent == null)
-                       return null;
-
-               string cover_path = null;
-               var directory = new Directory (parent);
-               var attributes = string.join (",", FileAttribute.STANDARD_NAME, 
FileAttribute.STANDARD_FAST_CONTENT_TYPE);
-               yield directory.foreach_async (attributes, (sibbling) => {
-                       var sibbling_basename = sibbling.get_name ();
-                       if (!sibbling_basename.has_prefix ("cover.") &&
-                           !sibbling_basename.has_prefix ("folder."))
-                               return false;
-
-                       var type = sibbling.get_attribute_string (FileAttribute.STANDARD_FAST_CONTENT_TYPE);
-                       if (!type.has_prefix ("image"))
-                               return false;
-
-                       var sibbling_file = parent.get_child (sibbling_basename);
-                       cover_path = sibbling_file.get_path ();
+       private void load_cover () {
+               var cover_path = get_cover_path ();
 
-                       return true;
-               });
-
-               return cover_path;
-       }
-
-       private void load_cover (string cover_path) {
-               if (!FileUtils.test (cover_path, FileTest.EXISTS))
+               if (cover_path == null)
                        return;
 
                var file = File.new_for_path (cover_path);


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