[gnome-games] core: Add PluginRegisterIterator



commit bd88258d01a5fb709c21157367ec083af27fc583
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Apr 13 10:28:34 2017 +0200

    core: Add PluginRegisterIterator
    
    Allow to loop through the content of PluginRegister in a synchronous or
    asynchronous way.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781334

 src/Makefile.am                        |    1 +
 src/core/plugin-register-iterator.vala |   45 ++++++++++++++++++++++++++++++++
 src/core/plugin-register.vala          |    4 +++
 3 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index d012718..edb04e4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -52,6 +52,7 @@ gnome_games_SOURCES = \
        core/media-set/media-set-error.vala \
        core/plugin.vala \
        core/plugin-register.vala \
+       core/plugin-register-iterator.vala \
        core/plugin-registrar.vala \
        core/runner.vala \
        core/title.vala \
diff --git a/src/core/plugin-register-iterator.vala b/src/core/plugin-register-iterator.vala
new file mode 100644
index 0000000..5448336
--- /dev/null
+++ b/src/core/plugin-register-iterator.vala
@@ -0,0 +1,45 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.PluginRegisterIterator {
+       private PluginRegister plugin_register;
+       private PluginRegistrar? plugins_registrar;
+       private FileEnumerator enumerator;
+
+       internal PluginRegisterIterator (PluginRegister plugin_register) {
+               this.plugin_register = plugin_register;
+               enumerator = null;
+       }
+
+       public new PluginRegistrar? get () {
+               return plugins_registrar;
+       }
+
+       public bool next () {
+               try {
+                       var directory = File.new_for_path (PLUGINS_DIR);
+                       if (enumerator == null) {
+                               enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
+                       }
+
+                       FileInfo info;
+                       while ((info = enumerator.next_file ()) != null) {
+                               var name = info.get_name ();
+                               if (!name.has_suffix (".plugin"))
+                                       continue;
+
+                               var descriptor = directory.get_child (name);
+                               var descriptor_path = descriptor.get_path ();
+                               plugins_registrar = plugin_register.get_plugin_registrar (descriptor_path);
+
+                               return true;
+                       }
+               }
+               catch (Error e) {
+                       debug (e.message);
+               }
+
+               plugins_registrar = null;
+
+               return false;
+       }
+}
diff --git a/src/core/plugin-register.vala b/src/core/plugin-register.vala
index 7bdf023..3a1fd6c 100644
--- a/src/core/plugin-register.vala
+++ b/src/core/plugin-register.vala
@@ -17,6 +17,10 @@ private class Games.PluginRegister : Object {
                return instance;
        }
 
+       public PluginRegisterIterator iterator () {
+               return new PluginRegisterIterator (this);
+       }
+
        public void foreach_plugin_registrar (PluginRegistrarFunc func) {
                var directory = File.new_for_path (PLUGINS_DIR);
                try {


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