[seahorse] Port SeahorseCollection to Vala.



commit 9dcdef9bb87fc7964fc92382e80ff9caead64195
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sat Jan 27 21:04:48 2018 +0100

    Port SeahorseCollection to Vala.

 common/collection.vala                   |  141 ++++++++++++
 common/meson.build                       |    1 +
 common/predicate.vala                    |   22 ++-
 libseahorse/meson.build                  |    1 -
 libseahorse/seahorse-collection.c        |  357 ------------------------------
 libseahorse/seahorse-collection.h        |   60 -----
 libseahorse/seahorse-key-manager-store.h |    2 -
 libseahorse/seahorse-search-provider.c   |    1 -
 pgp/seahorse-pgp-keysets.c               |    1 -
 src/seahorse-key-manager.c               |    1 -
 10 files changed, 157 insertions(+), 430 deletions(-)
---
diff --git a/common/collection.vala b/common/collection.vala
new file mode 100644
index 0000000..ae63e37
--- /dev/null
+++ b/common/collection.vala
@@ -0,0 +1,141 @@
+/*
+ * Seahorse
+ *
+ * Copyright (C) 2005 Stefan Walter
+ * Copyright (C) 2018 Niels De Graef
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+public class Seahorse.Collection : Gcr.Collection, GLib.Object {
+    private GenericSet<GLib.Object?> objects = new GenericSet<GLib.Object?>(direct_hash, direct_equal);
+
+    private DestroyNotify destroy_func;
+
+    /**
+     * Base_collection collection
+     */
+    public Gcr.Collection base_collection { get; construct set; }
+
+    /**
+     * Predicate for matching objects into this set.
+     */
+    public Predicate? predicate { get; private set; }
+
+    public Collection.for_predicate (Gcr.Collection base_collection, Predicate? pred, DestroyNotify 
destroy_func) {
+        GLib.Object (base_collection: base_collection);
+        this.predicate = pred;
+
+        this.destroy_func = destroy_func;
+
+        this.base_collection.added.connect(on_base_collection_added);
+        this.base_collection.removed.connect(on_base_collection_removed);
+
+        refresh ();
+    }
+
+    ~Collection() {
+        SignalHandler.disconnect_by_func((void*) this.base_collection, (void*) on_base_collection_added, 
this);
+        SignalHandler.disconnect_by_func((void*) this.base_collection, (void*) on_base_collection_removed, 
this);
+
+        foreach (GLib.Object obj in this.objects) {
+            SignalHandler.disconnect_by_func((void*) obj, (void*) on_object_changed, this);
+            emit_removed(obj);
+        }
+    }
+
+    public void refresh() {
+        // Make note of all the objects we had prior to refresh
+        GenericSet<GLib.Object?> check = new GenericSet<GLib.Object?>(direct_hash, direct_equal);
+        foreach(GLib.Object? obj in this.objects)
+            check.add(obj);
+
+        foreach (weak GLib.Object object in get_objects()) {
+            // Make note that we've seen this object
+            check.remove(object);
+
+            // This will add to set
+            if (!maybe_remove_object(object))
+                if (maybe_add_object(object))
+                    object.notify.connect(on_object_changed);
+        }
+
+        foreach (GLib.Object obj in check) {
+            SignalHandler.disconnect_by_func ((void*) obj, (void*) on_object_changed, (void*) this);
+            remove_object (obj);
+        }
+    }
+
+    private void on_object_changed (GLib.Object obj, ParamSpec spec) {
+        if (obj in objects)
+            maybe_remove_object(obj);
+        else
+            maybe_add_object(obj);
+    }
+
+    public uint get_length() {
+        return this.objects.length;
+    }
+
+    public List<weak GLib.Object> get_objects() {
+        List<GLib.Object> result = new List<GLib.Object>();
+        foreach (GLib.Object obj in this.objects)
+            result.append(obj);
+        return result;
+    }
+
+    public bool contains(GLib.Object object) {
+        return object in this.objects;
+    }
+
+    private void remove_object(GLib.Object object) {
+        this.objects.remove(object);
+        removed(object);
+    }
+
+    private bool maybe_add_object(GLib.Object obj) {
+        if (obj in objects)
+            return false;
+
+        if (this.predicate == null || !this.predicate.match(obj))
+            return false;
+
+        this.objects.add(obj);
+        emit_added(obj);
+        return true;
+    }
+
+    private bool maybe_remove_object(GLib.Object obj) {
+        if (!(obj in objects))
+            return false;
+
+        if (this.predicate == null || this.predicate.match(obj))
+            return false;
+
+        remove_object(obj);
+        return true;
+    }
+
+    private void on_base_collection_added (Gcr.Collection base_collection, GLib.Object obj) {
+        obj.notify.connect(on_object_changed);
+        maybe_add_object(obj);
+    }
+
+    private void on_base_collection_removed (Gcr.Collection base_collection, GLib.Object object) {
+        SignalHandler.disconnect_by_func (object, (void*) on_object_changed, this);
+
+        if (object in objects)
+            remove_object (object);
+    }
+}
diff --git a/common/meson.build b/common/meson.build
index bf373d3..94c92a8 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -2,6 +2,7 @@ common_sources = [
   'actions.vala',
   'backend.vala',
   'catalog.vala',
+  'collection.vala',
   'deletable.vala',
   'delete-dialog.vala',
   'deleter.vala',
diff --git a/common/predicate.vala b/common/predicate.vala
index dcd6616..6799f05 100644
--- a/common/predicate.vala
+++ b/common/predicate.vala
@@ -21,9 +21,9 @@
  */
 
 [CCode (has_target = false)]
-public delegate bool Seahorse.PredicateFunc(Seahorse.Object obj, void* custom_target);
+public delegate bool Seahorse.PredicateFunc(GLib.Object obj, void* custom_target);
 
-public class Seahorse.Predicate {
+public struct Seahorse.Predicate {
     public Type type;
     public Usage usage;
     public Flags flags;
@@ -38,18 +38,26 @@ public class Seahorse.Predicate {
      *
      * @return false if predicate does not match the Object, true else
      */
-    public bool match(Object obj) {
+    public bool match(GLib.Object obj) {
         // Check all the fields
         if (this.type != 0 && !(obj.get_type().is_a(this.type) && this.type.is_a(obj.get_type())))
             return false;
 
-        if (this.usage != Usage.NONE && this.usage != obj.usage)
-            return false;
+        if (this.usage != Usage.NONE) {
+            Usage obj_usage = Usage.NONE;
+            obj.get("usage", out obj_usage, null);
+
+            if (this.usage != obj_usage)
+                return false;
+        }
 
         if (this.flags != 0 || this.nflags != 0) {
-            if (this.flags != Flags.NONE && (obj.object_flags in this.flags))
+            Flags obj_flags = Flags.NONE;
+            obj.get("object-flags", out obj_flags, null);
+
+            if (this.flags != Flags.NONE && (obj_flags in this.flags))
                 return false;
-            if (this.nflags != Flags.NONE && (obj.object_flags in this.nflags))
+            if (this.nflags != Flags.NONE && (obj_flags in this.nflags))
                 return false;
         }
 
diff --git a/libseahorse/meson.build b/libseahorse/meson.build
index 9313fa8..6a9db39 100644
--- a/libseahorse/meson.build
+++ b/libseahorse/meson.build
@@ -13,7 +13,6 @@ search_provider_src = gnome.gdbus_codegen('seahorse-shell-search-provider-genera
 libseahorse_sources = [
   'seahorse-application.c',
   'seahorse-bind.c',
-  'seahorse-collection.c',
   'seahorse-interaction.c',
   'seahorse-key-manager-store.c',
   'seahorse-object-list.c',
diff --git a/libseahorse/seahorse-key-manager-store.h b/libseahorse/seahorse-key-manager-store.h
index 30f4d92..bcf0dbe 100644
--- a/libseahorse/seahorse-key-manager-store.h
+++ b/libseahorse/seahorse-key-manager-store.h
@@ -21,8 +21,6 @@
 #ifndef __SEAHORSE_KEY_MANAGER_STORE_H__
 #define __SEAHORSE_KEY_MANAGER_STORE_H__
 
-#include "seahorse-collection.h"
-
 #include "seahorse-common.h"
 
 #include <gtk/gtk.h>
diff --git a/libseahorse/seahorse-search-provider.c b/libseahorse/seahorse-search-provider.c
index 22cf633..dddaae1 100644
--- a/libseahorse/seahorse-search-provider.c
+++ b/libseahorse/seahorse-search-provider.c
@@ -22,7 +22,6 @@
 #include "seahorse-search-provider.h"
 
 #include "seahorse-application.h"
-#include "seahorse-collection.h"
 #include "seahorse-widget.h"
 #include "seahorse-shell-search-provider-generated.h"
 
diff --git a/pgp/seahorse-pgp-keysets.c b/pgp/seahorse-pgp-keysets.c
index f567c6f..42a187c 100644
--- a/pgp/seahorse-pgp-keysets.c
+++ b/pgp/seahorse-pgp-keysets.c
@@ -29,7 +29,6 @@
 #include "seahorse-common.h"
 
 #include "libseahorse/seahorse-application.h"
-#include "libseahorse/seahorse-collection.h"
 
 /* -----------------------------------------------------------------------------
  * COMMON KEYSETS 
diff --git a/src/seahorse-key-manager.c b/src/seahorse-key-manager.c
index b46151b..b574e70 100644
--- a/src/seahorse-key-manager.c
+++ b/src/seahorse-key-manager.c
@@ -22,7 +22,6 @@
 #include "config.h"
 
 #include "libseahorse/seahorse-application.h"
-#include "libseahorse/seahorse-collection.h"
 #include "libseahorse/seahorse-key-manager-store.h"
 #include "libseahorse/seahorse-progress.h"
 #include "libseahorse/seahorse-util.h"


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