[seahorse/keyservers: 2/2] ServerCategory: Register ServerSource subclasses itself




commit 92a64de09b75da93f805426cfc5b0429f641a627
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Thu Feb 18 14:30:11 2021 +0100

    ServerCategory: Register ServerSource subclasses itself
    
    Rather than trying to let each `SeahorseServerSource` subclass register
    itself in its class_init, do the registration in `ServerCategory`
    itself. Otherwise, what would happen is that new URLs for a certain type
    of category couldn't be added, as the type was never needed until then.
    
    Fixes https://gitlab.gnome.org/GNOME/seahorse/-/issues/262

 common/config.vapi           | 22 +++++++++++++++++++
 common/pgp-settings.vala     |  7 ++++--
 common/server-category.vala  | 42 +++++++++++++++++++++++-------------
 meson.build                  |  5 +++++
 pgp/seahorse-discovery.c     |  2 +-
 pgp/seahorse-hkp-source.c    |  3 ---
 pgp/seahorse-ldap-source.c   |  2 --
 pgp/seahorse-pgp-backend.c   |  2 +-
 pgp/seahorse-server-source.c | 51 +-------------------------------------------
 pgp/seahorse-server-source.h |  2 --
 10 files changed, 62 insertions(+), 76 deletions(-)
---
diff --git a/common/config.vapi b/common/config.vapi
index a04c8534..f408170e 100644
--- a/common/config.vapi
+++ b/common/config.vapi
@@ -51,6 +51,28 @@ namespace Pgp.Backend {
        public void initialize();
 }
 
+[CCode (cheader_filename = "pgp/seahorse-server-source.h")]
+public class ServerSource {
+}
+
+#if WITH_LDAP
+[CCode (cheader_filename = "pgp/seahorse-ldap-source.h")]
+public class LdapSource : ServerSource {
+}
+
+[CCode (cheader_filename = "pgp/seahorse-ldap-source.h")]
+public static bool ldap_is_valid_uri(string uri);
+#endif // WITH_LDAP
+
+#if WITH_HKP
+[CCode (cheader_filename = "pgp/seahorse-hkp-source.h")]
+public class HkpSource : ServerSource {
+}
+
+[CCode (cheader_filename = "pgp/seahorse-hkp-source.h")]
+public static bool hkp_is_valid_uri(string uri);
+#endif // WITH_HKP
+
 [CCode (cheader_filename = "pkcs11/seahorse-pkcs11-backend.h")]
 public class Pkcs11.Backend {
        public static void initialize();
diff --git a/common/pgp-settings.vala b/common/pgp-settings.vala
index 46de1622..ef07a6e7 100644
--- a/common/pgp-settings.vala
+++ b/common/pgp-settings.vala
@@ -53,9 +53,12 @@ public class Seahorse.PgpSettings : GLib.Settings {
         set { set_string("sort-recipients-by", value); }
     }
 
-       public PgpSettings () {
+    public PgpSettings () {
         GLib.Object (schema_id: "org.gnome.crypto.pgp");
-       }
+
+        // Make sure ServerCategories are known before people try to read them
+        ServerCategory.init();
+    }
 
        private static PgpSettings? _instance = null;
        public static PgpSettings instance() {
diff --git a/common/server-category.vala b/common/server-category.vala
index 79ab3579..1d444435 100644
--- a/common/server-category.vala
+++ b/common/server-category.vala
@@ -26,34 +26,46 @@ public class Seahorse.ServerCategory : GLib.Object {
     public delegate bool ValidUriFunc(string uri);
 
     /** The scheme of servers (ie the scheme) */
-    public string scheme { get; construct set; }
+    public GLib.Type gtype { get; construct; }
+
+    /** The scheme of servers (ie the scheme) */
+    public string scheme { get; construct; }
 
     /** User-facing string to describe it */
-    public string description { get; construct set; }
+    public string description { get; construct; }
 
     /** Function to validate a given URI for this category */
     public ValidUriFunc validator;
 
-    private static GenericArray<ServerCategory> categories = null;
+    private static GenericArray<ServerCategory> categories;
 
-    public ServerCategory(string scheme, string description, ValidUriFunc validator) {
-        GLib.Object(scheme: scheme,
+    public ServerCategory(GLib.Type gtype, string scheme, string description, ValidUriFunc validator) {
+        GLib.Object(gtype: gtype,
+                    scheme: scheme,
                     description: description);
         this.validator = validator;
     }
 
-    public static void register(string? scheme, string? description, ValidUriFunc validate) {
-        if (categories == null)
-            categories = new GenericArray<ServerCategory>();
-
-        // Don't register double (if this happens, it's probably a programmer error)
-        if (find_category(scheme) != null) {
-            warning("Scheme '%s' is already registered", scheme);
-            return;
+    public static ServerSource? create_server(string uri) {
+        unowned var cat = find_category (Uri.parse_scheme(uri));
+        if (cat == null) {
+            warning("Unsupported scheme '%s'", Uri.parse_scheme(uri));
+            return null;
         }
 
-        var category = new ServerCategory(scheme, description, validate);
-        categories.add(category);
+        return (ServerSource) GLib.Object.new(cat.gtype, "uri", uri);
+    }
+
+    public static void init() {
+        categories = new GenericArray<ServerCategory>();
+
+#if WITH_LDAP
+        categories.add(new ServerCategory(typeof(LdapSource), "ldap", _("LDAP Key Server"), 
ldap_is_valid_uri));
+#endif // WITH_LDAP
+#if WITH_HKP
+        categories.add(new ServerCategory(typeof(HkpSource), "hkp", _("HTTP Key Server"), hkp_is_valid_uri));
+        categories.add(new ServerCategory(typeof(HkpSource), "hkps", _("HTTPS Key Server"), 
hkp_is_valid_uri));
+#endif // WITH_HKP
     }
 
     /**
diff --git a/meson.build b/meson.build
index 540e933f..a082ea10 100644
--- a/meson.build
+++ b/meson.build
@@ -65,8 +65,13 @@ endif
 if get_option('ldap-support')
   libldap = cc.find_library('ldap')
   liblber = cc.find_library('lber')
+
+  add_project_arguments('-D', 'WITH_LDAP', language: 'vala')
 endif
 
+if get_option('hkp-support')
+  add_project_arguments('-D', 'WITH_HKP', language: 'vala')
+endif
 libsoup = dependency('libsoup-2.4', version: '>= 2.33.92', required: get_option('hkp-support'))
 avahi_client = dependency('avahi-client', required: get_option('key-sharing'))
 avahi_glib = dependency('avahi-glib', version: '>= 0.6', required: get_option('key-sharing'))
diff --git a/pgp/seahorse-discovery.c b/pgp/seahorse-discovery.c
index 5d263c1c..2020442b 100644
--- a/pgp/seahorse-discovery.c
+++ b/pgp/seahorse-discovery.c
@@ -158,7 +158,7 @@ resolve_callback (AvahiServiceResolver *resolver, AvahiIfIndex iface, AvahiProto
 
                /* Add it to the context */
                if (!seahorse_pgp_backend_lookup_remote (NULL, service_uri)) {
-                       SeahorseServerSource *ssrc = seahorse_server_source_new (service_uri);
+                       SeahorseServerSource *ssrc = seahorse_server_category_create_server (service_uri);
                        g_return_if_fail (ssrc != NULL);
                        seahorse_pgp_backend_add_remote (NULL, service_uri, ssrc);
                        g_object_unref (ssrc);
diff --git a/pgp/seahorse-hkp-source.c b/pgp/seahorse-hkp-source.c
index f1711349..4b8a85a2 100644
--- a/pgp/seahorse-hkp-source.c
+++ b/pgp/seahorse-hkp-source.c
@@ -988,9 +988,6 @@ seahorse_hkp_source_class_init (SeahorseHKPSourceClass *klass)
     server_class->export_finish = seahorse_hkp_source_export_finish;
     server_class->import_async = seahorse_hkp_source_import_async;
     server_class->import_finish = seahorse_hkp_source_import_finish;
-
-    seahorse_server_category_register ("hkp", _("HTTP Key Server"), seahorse_hkp_is_valid_uri);
-    seahorse_server_category_register ("hkps", _("HTTPS Key Server"), seahorse_hkp_is_valid_uri);
 }
 
 /**
diff --git a/pgp/seahorse-ldap-source.c b/pgp/seahorse-ldap-source.c
index 70d674c6..4d198241 100644
--- a/pgp/seahorse-ldap-source.c
+++ b/pgp/seahorse-ldap-source.c
@@ -1331,8 +1331,6 @@ seahorse_ldap_source_class_init (SeahorseLDAPSourceClass *klass)
     server_class->export_finish = seahorse_ldap_source_export_finish;
     server_class->import_async = seahorse_ldap_source_import_async;
     server_class->import_finish = seahorse_ldap_source_import_finish;
-
-    seahorse_server_category_register ("ldap", _("LDAP Key Server"), seahorse_ldap_is_valid_uri);
 }
 
 /**
diff --git a/pgp/seahorse-pgp-backend.c b/pgp/seahorse-pgp-backend.c
index b7c88a46..a8825b2e 100644
--- a/pgp/seahorse-pgp-backend.c
+++ b/pgp/seahorse-pgp-backend.c
@@ -107,7 +107,7 @@ on_settings_keyservers_changed (GSettings  *settings,
 
         /* If we don't have a keysource then add it */
         if (!g_hash_table_lookup (self->remotes, uri)) {
-            source = seahorse_server_source_new (uri);
+            source = seahorse_server_category_create_server (uri);
             if (source != NULL) {
                 seahorse_pgp_backend_add_remote (self, uri, source);
                 g_object_unref (source);
diff --git a/pgp/seahorse-server-source.c b/pgp/seahorse-server-source.c
index 1c615a17..cf88aceb 100644
--- a/pgp/seahorse-server-source.c
+++ b/pgp/seahorse-server-source.c
@@ -69,7 +69,7 @@ static void      seahorse_server_source_collection_init    (GcrCollectionIface *
 
 static void      seahorse_server_source_place_iface        (SeahorsePlaceIface *iface);
 
-G_DEFINE_TYPE_WITH_CODE (SeahorseServerSource, seahorse_server_source, G_TYPE_OBJECT,
+G_DEFINE_ABSTRACT_TYPE_WITH_CODE (SeahorseServerSource, seahorse_server_source, G_TYPE_OBJECT,
                          G_ADD_PRIVATE (SeahorseServerSource)
                          G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, seahorse_server_source_collection_init);
                          G_IMPLEMENT_INTERFACE (SEAHORSE_TYPE_PLACE, seahorse_server_source_place_iface);
@@ -323,55 +323,6 @@ seahorse_server_source_collection_init (GcrCollectionIface *iface)
        iface->contains = seahorse_server_source_contains;
 }
 
-/**
- * seahorse_server_source_new:
- * @uri: The server URI to create an object for
- *
- * Creates a #SeahorseServerSource object out of @uri. Depending
- * on the defines at compilation time other sources are supported
- * (ldap, hkp)
- *
- * Returns: A new #SeahorseServerSource or %NULL
- */
-SeahorseServerSource*
-seahorse_server_source_new (const char *uri)
-{
-    g_autofree char *scheme = NULL;
-
-    g_return_val_if_fail (uri && *uri, NULL);
-
-    scheme = g_uri_parse_scheme (uri);
-    if (!scheme) {
-        g_warning ("invalid uri passed (no scheme): %s", uri);
-               return NULL;
-    }
-
-#ifdef WITH_LDAP
-    /* LDAP Uris */
-    if (g_ascii_strcasecmp (scheme, "ldap") == 0)
-        return SEAHORSE_SERVER_SOURCE (seahorse_ldap_source_new (uri));
-#endif /* WITH_LDAP */
-
-#ifdef WITH_HKP
-    /* HKP Uris */
-    if (g_ascii_strcasecmp (scheme, "hkp") == 0 ||
-        g_ascii_strcasecmp (scheme, "hkps") == 0)
-
-        return SEAHORSE_SERVER_SOURCE (seahorse_hkp_source_new (uri));
-
-    /* HTTP Uris */
-    if (g_ascii_strcasecmp (scheme, "http") == 0 ||
-        g_ascii_strcasecmp (scheme, "https") == 0) {
-
-        /* FIXME: if no port given, use port 80/443 */
-        return SEAHORSE_SERVER_SOURCE (seahorse_hkp_source_new (uri));
-    }
-#endif /* WITH_HKP */
-
-    g_warning ("unsupported key server uri scheme: %s", scheme);
-    return NULL;
-}
-
 void
 seahorse_server_source_search_async (SeahorseServerSource *self,
                                      const gchar *match,
diff --git a/pgp/seahorse-server-source.h b/pgp/seahorse-server-source.h
index 47ac232c..9d009b41 100644
--- a/pgp/seahorse-server-source.h
+++ b/pgp/seahorse-server-source.h
@@ -38,8 +38,6 @@
 
 #pragma once
 
-#include "seahorse-pgp-key.h"
-
 #include <gcr/gcr.h>
 
 #define SEAHORSE_TYPE_SERVER_SOURCE (seahorse_server_source_get_type ())


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