[seahorse/keyservers: 1/2] common/servers: Create ServerCategory class
- From: Niels De Graef <nielsdg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [seahorse/keyservers: 1/2] common/servers: Create ServerCategory class
- Date: Thu, 18 Feb 2021 13:45:19 +0000 (UTC)
commit b2dee3a37591368355be89883d620a4681458e46
Author: Niels De Graef <nielsdegraef gmail com>
Date: Wed Feb 17 13:57:40 2021 +0100
common/servers: Create ServerCategory class
And put `Servers.get_uris()` and `Servers.get_names()` into the
PgpSettings class, where they really make much more sense.
While we're at it, do some cleanups in the related code.
common/add-keyserver-dialog.vala | 12 +++--
common/keyserver-control.vala | 2 +-
common/meson.build | 2 +-
common/pgp-settings.vala | 22 ++++++++
common/prefs.vala | 4 +-
common/server-category.vala | 96 +++++++++++++++++++++++++++++++++
common/servers.vala | 114 ---------------------------------------
pgp/seahorse-hkp-source.c | 4 +-
pgp/seahorse-keyserver-search.c | 27 +++++-----
pgp/seahorse-keyserver-sync.c | 108 ++++++++++++++++++-------------------
pgp/seahorse-ldap-source.c | 2 +-
pgp/seahorse-pgp-backend.c | 93 ++++++++++++++++----------------
src/main.vala | 1 -
13 files changed, 244 insertions(+), 243 deletions(-)
---
diff --git a/common/add-keyserver-dialog.vala b/common/add-keyserver-dialog.vala
index f37bcb55..3437d285 100644
--- a/common/add-keyserver-dialog.vala
+++ b/common/add-keyserver-dialog.vala
@@ -36,7 +36,7 @@ public class Seahorse.AddKeyserverDialog : Gtk.Dialog {
default_width: 400,
use_header_bar: 1
);
- this.keyserver_types = Seahorse.Servers.get_types();
+ this.keyserver_types = ServerCategory.get_types();
// Load ui
Gtk.Builder builder = new Gtk.Builder();
@@ -58,8 +58,10 @@ public class Seahorse.AddKeyserverDialog : Gtk.Dialog {
this.keyserver_port.changed.connect(() => on_prefs_add_keyserver_uri_changed());
// The description for the key server types, plus custom
- foreach (string type in this.keyserver_types)
- this.keyserver_type.append_text(Seahorse.Servers.get_description(type));
+ foreach (string type in this.keyserver_types) {
+ unowned var category = ServerCategory.find_category(type);
+ this.keyserver_type.append_text(category.description);
+ }
this.keyserver_type.append_text(_("Custom"));
this.keyserver_type.set_active(0);
@@ -90,7 +92,7 @@ public class Seahorse.AddKeyserverDialog : Gtk.Dialog {
return null;
if (scheme == null) // Custom URI?
- return Servers.is_valid_uri(host)? host : null;
+ return ServerCategory.is_valid_uri(host)? host : null;
string? port = this.keyserver_port.text;
if (port == "")
@@ -102,7 +104,7 @@ public class Seahorse.AddKeyserverDialog : Gtk.Dialog {
uri += ":%s".printf(port);
// And check if it's valid
- if (!Servers.is_valid_uri(uri))
+ if (!ServerCategory.is_valid_uri(uri))
uri = null;
return uri;
diff --git a/common/keyserver-control.vala b/common/keyserver-control.vala
index 74865633..b623e83e 100644
--- a/common/keyserver-control.vala
+++ b/common/keyserver-control.vala
@@ -152,7 +152,7 @@ public class Seahorse.KeyserverControl : Gtk.ComboBox {
}
bool chosen_iter_set = false;
- foreach (weak string keyserver in Servers.get_uris()) {
+ foreach (unowned string keyserver in PgpSettings.instance().get_uris()) {
assert (keyserver != null);
iter = append_entry(store, keyserver, Option.KEYSERVER);
if (chosen != null && chosen == keyserver) {
diff --git a/common/meson.build b/common/meson.build
index 39b743e7..849af26b 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -20,7 +20,7 @@ common_sources = files(
'predicate.vala',
'prefs.vala',
'registry.vala',
- 'servers.vala',
+ 'server-category.vala',
'types.vala',
'util.vala',
'validity.vala',
diff --git a/common/pgp-settings.vala b/common/pgp-settings.vala
index 67c70fe0..46de1622 100644
--- a/common/pgp-settings.vala
+++ b/common/pgp-settings.vala
@@ -63,4 +63,26 @@ public class Seahorse.PgpSettings : GLib.Settings {
_instance = new PgpSettings();
return _instance;
}
+
+ [CCode (array_null_terminated = true, array_length = false)]
+ public string[] get_uris() {
+ // The values are 'uri name', remove the name part
+ string[] uris = {};
+ foreach (string server in this.keyservers)
+ uris += server.strip().split(" ", 2)[0];
+
+ return uris;
+ }
+
+ [CCode (array_null_terminated = true, array_length = false)]
+ public string[] get_names() {
+ string[] names = {};
+ foreach (string server in this.keyservers) {
+ // The values are 'uri' or 'uri name', remove the name part if there
+ string[] split = server._strip().split(" ", 2);
+ names += (split.length == 1)? split[0] : split[1];
+ }
+
+ return names;
+ }
}
diff --git a/common/prefs.vala b/common/prefs.vala
index aa332823..bab3a714 100644
--- a/common/prefs.vala
+++ b/common/prefs.vala
@@ -131,7 +131,7 @@ public class Seahorse.Prefs : Gtk.Dialog {
// Perform keyserver page initialization
private void setup_keyservers () {
- string[] keyservers = Seahorse.Servers.get_uris();
+ string[] keyservers = PgpSettings.instance().get_uris();
populate_keyservers(keyservers);
Gtk.TreeModel model = this.keyservers.get_model();
@@ -161,7 +161,7 @@ public class Seahorse.Prefs : Gtk.Dialog {
// Called when a cell has completed being edited
private void keyserver_cell_edited (Gtk.CellRendererText cell, string? path, string? text, Gtk.TreeStore
store) {
- if (!Servers.is_valid_uri(text)) {
+ if (!ServerCategory.is_valid_uri(text)) {
Util.show_error (null,
_("Not a valid Key Server address."),
_("For help contact your system administrator or the administrator of the key
server." ));
diff --git a/common/server-category.vala b/common/server-category.vala
new file mode 100644
index 00000000..79ab3579
--- /dev/null
+++ b/common/server-category.vala
@@ -0,0 +1,96 @@
+/*
+ * Seahorse
+ *
+ * Copyright (C) 2008 Stefan Walter
+ * Copyright (C) 2017 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/>.
+ */
+
+/**
+ * Describes a category of servers
+ */
+public class Seahorse.ServerCategory : GLib.Object {
+ [CCode (cname = "SeahorseValidUriFunc", has_target = false)]
+ public delegate bool ValidUriFunc(string uri);
+
+ /** The scheme of servers (ie the scheme) */
+ public string scheme { get; construct set; }
+
+ /** User-facing string to describe it */
+ public string description { get; construct set; }
+
+ /** Function to validate a given URI for this category */
+ public ValidUriFunc validator;
+
+ private static GenericArray<ServerCategory> categories = null;
+
+ public ServerCategory(string scheme, string description, ValidUriFunc validator) {
+ GLib.Object(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;
+ }
+
+ var category = new ServerCategory(scheme, description, validate);
+ categories.add(category);
+ }
+
+ /**
+ * Returns a null-terminated string array of supported schemes
+ */
+ [CCode (array_null_terminated = true, array_length = false)]
+ public static string[] get_types() {
+ string[] types = {};
+
+ for (uint i = 0; i < categories.length; i++)
+ types += categories[i].scheme;
+
+ return types;
+ }
+
+ public static unowned ServerCategory? find_category(string? scheme) {
+ for (uint i = 0; i < categories.length; i++) {
+ if (scheme == categories[i].scheme)
+ return categories[i];
+ }
+
+ return null;
+ }
+
+ /**
+ * Checks to see if the passed uri is valid against registered validators
+ */
+ public static bool is_valid_uri(string uri) {
+ string[] parts = uri.split(":", 2);
+ if (parts.length <= 0)
+ return false;
+
+ for (uint i = 0; i < categories.length; i++) {
+ if ((parts[0] == categories[i].scheme) && categories[i].validator(uri))
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/pgp/seahorse-hkp-source.c b/pgp/seahorse-hkp-source.c
index 43547325..f1711349 100644
--- a/pgp/seahorse-hkp-source.c
+++ b/pgp/seahorse-hkp-source.c
@@ -989,8 +989,8 @@ seahorse_hkp_source_class_init (SeahorseHKPSourceClass *klass)
server_class->import_async = seahorse_hkp_source_import_async;
server_class->import_finish = seahorse_hkp_source_import_finish;
- seahorse_servers_register_type ("hkp", _("HTTP Key Server"), seahorse_hkp_is_valid_uri);
- seahorse_servers_register_type ("hkps", _("HTTPS Key Server"), seahorse_hkp_is_valid_uri);
+ 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-keyserver-search.c b/pgp/seahorse-keyserver-search.c
index ecdedcd6..68fb2e88 100644
--- a/pgp/seahorse-keyserver-search.c
+++ b/pgp/seahorse-keyserver-search.c
@@ -303,15 +303,18 @@ populate_keyserver_list (SeahorseKeyserverSearch *self, GtkWidget *box, gchar **
}
static void
-on_settings_keyservers_changed (GSettings *settings, const gchar *key, gpointer user_data)
+on_settings_keyservers_changed (GSettings *settings,
+ const char *key,
+ gpointer user_data)
{
- SeahorseKeyserverSearch *self = SEAHORSE_KEYSERVER_SEARCH (user_data);
- g_auto(GStrv) keyservers = NULL;
- g_auto(GStrv) names = NULL;
+ SeahorseKeyserverSearch *self = SEAHORSE_KEYSERVER_SEARCH (user_data);
+ SeahorsePgpSettings *pgp_settings = SEAHORSE_PGP_SETTINGS (settings);
+ g_auto(GStrv) keyservers = NULL;
+ g_auto(GStrv) names = NULL;
- keyservers = seahorse_servers_get_uris ();
- names = seahorse_servers_get_names ();
- populate_keyserver_list (self, self->key_server_list, keyservers, names);
+ keyservers = seahorse_pgp_settings_get_uris (pgp_settings);
+ names = seahorse_pgp_settings_get_names (pgp_settings);
+ populate_keyserver_list (self, self->key_server_list, keyservers, names);
}
/**
@@ -385,11 +388,11 @@ seahorse_keyserver_search_init (SeahorseKeyserverSearch *self)
gtk_editable_select_region (GTK_EDITABLE (self->search_entry), 0, -1);
}
- /* The key servers to list */
- settings = seahorse_pgp_settings_instance ();
- on_settings_keyservers_changed (G_SETTINGS (settings), "keyservers", self);
- g_signal_connect_object (settings, "changed::keyservers",
- G_CALLBACK (on_settings_keyservers_changed), self, 0);
+ /* The key servers to list */
+ settings = seahorse_pgp_settings_instance ();
+ on_settings_keyservers_changed (G_SETTINGS (settings), "keyservers", self);
+ g_signal_connect_object (settings, "changed::keyservers",
+ G_CALLBACK (on_settings_keyservers_changed), self, 0);
/* Any shared keys to list */
ssd = seahorse_pgp_backend_get_discovery (NULL);
diff --git a/pgp/seahorse-keyserver-sync.c b/pgp/seahorse-keyserver-sync.c
index 3d93c0dc..9da86211 100644
--- a/pgp/seahorse-keyserver-sync.c
+++ b/pgp/seahorse-keyserver-sync.c
@@ -179,61 +179,55 @@ seahorse_keyserver_sync_show (GList *keys, GtkWindow *parent)
void
seahorse_keyserver_sync (GList *keys)
{
- SeahorseServerSource *source;
- SeahorseGpgmeKeyring *keyring;
- gchar *keyserver;
- GCancellable *cancellable;
- gchar **keyservers;
- GPtrArray *keyids;
- GList *l;
- guint i;
-
- if (!keys)
- return;
-
- cancellable = g_cancellable_new ();
-
- keyids = g_ptr_array_new ();
- for (l = keys; l != NULL; l = g_list_next (l))
- g_ptr_array_add (keyids, (gchar *)seahorse_pgp_key_get_keyid (l->data));
- g_ptr_array_add (keyids, NULL);
-
- /* And now synchronizing keys from the servers */
- keyservers = seahorse_servers_get_uris ();
- for (i = 0; keyservers[i] != NULL; i++) {
- source = seahorse_pgp_backend_lookup_remote (NULL, keyservers[i]);
-
- /* This can happen if the URI scheme is not supported */
- if (source == NULL)
- continue;
-
- keyring = seahorse_pgp_backend_get_default_keyring (NULL);
- seahorse_transfer_keyids_async (SEAHORSE_SERVER_SOURCE (source),
- SEAHORSE_PLACE (keyring),
- (const gchar **)keyids->pdata,
- cancellable,
- on_transfer_download_complete,
- g_object_ref (source));
- }
-
- g_ptr_array_free (keyids, TRUE);
- g_strfreev (keyservers);
-
- /* Publishing keys online */
- keyserver = seahorse_app_settings_get_server_publish_to (seahorse_app_settings_instance ());
- if (keyserver && keyserver[0]) {
- source = seahorse_pgp_backend_lookup_remote (NULL, keyserver);
-
- /* This can happen if the URI scheme is not supported */
- if (source != NULL) {
- seahorse_pgp_backend_transfer_async (NULL, keys, SEAHORSE_PLACE (source),
- cancellable, on_transfer_upload_complete,
- g_object_ref (source));
- }
- }
-
- g_free (keyserver);
-
- seahorse_progress_show (cancellable, _("Synchronizing keys…"), FALSE);
- g_object_unref (cancellable);
+ SeahorseServerSource *source;
+ SeahorseGpgmeKeyring *keyring;
+ SeahorsePgpSettings *pgp_settings;
+ g_autofree char *keyserver = NULL;
+ g_autoptr(GCancellable) cancellable = NULL;
+ g_auto(GStrv) keyservers = NULL;
+ g_autoptr(GPtrArray) keyids = NULL;
+
+ if (!keys)
+ return;
+
+ keyring = seahorse_pgp_backend_get_default_keyring (NULL);
+ pgp_settings = seahorse_pgp_settings_instance ();
+ cancellable = g_cancellable_new ();
+
+ keyids = g_ptr_array_new ();
+ for (GList *l = keys; l != NULL; l = g_list_next (l))
+ g_ptr_array_add (keyids, (char *) seahorse_pgp_key_get_keyid (l->data));
+ g_ptr_array_add (keyids, NULL);
+
+ /* And now synchronizing keys from the servers */
+ keyservers = seahorse_pgp_settings_get_uris (pgp_settings);
+ for (guint i = 0; keyservers[i] != NULL; i++) {
+ source = seahorse_pgp_backend_lookup_remote (NULL, keyservers[i]);
+
+ /* This can happen if the URI scheme is not supported */
+ if (source == NULL)
+ continue;
+
+ seahorse_transfer_keyids_async (SEAHORSE_SERVER_SOURCE (source),
+ SEAHORSE_PLACE (keyring),
+ (const char **) keyids->pdata,
+ cancellable,
+ on_transfer_download_complete,
+ g_object_ref (source));
+ }
+
+ /* Publishing keys online */
+ keyserver = seahorse_app_settings_get_server_publish_to (seahorse_app_settings_instance ());
+ if (keyserver && keyserver[0]) {
+ source = seahorse_pgp_backend_lookup_remote (NULL, keyserver);
+
+ /* This can happen if the URI scheme is not supported */
+ if (source != NULL) {
+ seahorse_pgp_backend_transfer_async (NULL, keys, SEAHORSE_PLACE (source),
+ cancellable, on_transfer_upload_complete,
+ g_object_ref (source));
+ }
+ }
+
+ seahorse_progress_show (cancellable, _("Synchronizing keys…"), FALSE);
}
diff --git a/pgp/seahorse-ldap-source.c b/pgp/seahorse-ldap-source.c
index 13c390b3..70d674c6 100644
--- a/pgp/seahorse-ldap-source.c
+++ b/pgp/seahorse-ldap-source.c
@@ -1332,7 +1332,7 @@ seahorse_ldap_source_class_init (SeahorseLDAPSourceClass *klass)
server_class->import_async = seahorse_ldap_source_import_async;
server_class->import_finish = seahorse_ldap_source_import_finish;
- seahorse_servers_register_type ("ldap", _("LDAP Key Server"), seahorse_ldap_is_valid_uri);
+ 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 854dbd7c..b7c88a46 100644
--- a/pgp/seahorse-pgp-backend.c
+++ b/pgp/seahorse-pgp-backend.c
@@ -82,49 +82,46 @@ seahorse_pgp_backend_init (SeahorsePgpBackend *self)
#ifdef WITH_KEYSERVER
static void
-on_settings_keyservers_changed (GSettings *settings,
- gchar *key,
- gpointer user_data)
-{
- SeahorsePgpBackend *self = SEAHORSE_PGP_BACKEND (user_data);
- SeahorseServerSource *source;
- gchar **keyservers;
- GHashTable *check;
- const gchar *uri;
- GHashTableIter iter;
- guint i;
-
- check = g_hash_table_new (g_str_hash, g_str_equal);
- g_hash_table_iter_init (&iter, self->remotes);
- while (g_hash_table_iter_next (&iter, (gpointer*)&uri, (gpointer*)&source))
- g_hash_table_replace (check, (gpointer)uri, source);
-
- /* Load and strip names from keyserver list */
- keyservers = seahorse_servers_get_uris ();
-
- for (i = 0; keyservers[i] != NULL; i++) {
- uri = keyservers[i];
-
- /* If we don't have a keysource then add it */
- if (!g_hash_table_lookup (self->remotes, uri)) {
- source = seahorse_server_source_new (uri);
- if (source != NULL) {
- seahorse_pgp_backend_add_remote (self, uri, source);
- g_object_unref (source);
- }
- }
+on_settings_keyservers_changed (GSettings *settings,
+ const char *key,
+ gpointer user_data)
+{
+ SeahorsePgpBackend *self = SEAHORSE_PGP_BACKEND (user_data);
+ SeahorsePgpSettings *pgp_settings = SEAHORSE_PGP_SETTINGS (settings);
+ SeahorseServerSource *source;
+ g_auto(GStrv) keyservers = NULL;
+ g_autoptr(GHashTable) check = NULL;
+ const char *uri;
+ GHashTableIter iter;
- /* Mark this one as present */
- g_hash_table_remove (check, uri);
- }
+ check = g_hash_table_new (g_str_hash, g_str_equal);
+ g_hash_table_iter_init (&iter, self->remotes);
+ while (g_hash_table_iter_next (&iter, (gpointer*)&uri, (gpointer*)&source))
+ g_hash_table_replace (check, (gpointer)uri, source);
+
+ /* Load and strip names from keyserver list */
+ keyservers = seahorse_pgp_settings_get_uris (pgp_settings);
+
+ for (guint i = 0; keyservers[i] != NULL; i++) {
+ uri = keyservers[i];
+
+ /* If we don't have a keysource then add it */
+ if (!g_hash_table_lookup (self->remotes, uri)) {
+ source = seahorse_server_source_new (uri);
+ if (source != NULL) {
+ seahorse_pgp_backend_add_remote (self, uri, source);
+ g_object_unref (source);
+ }
+ }
- /* Now remove any extras */
- g_hash_table_iter_init (&iter, check);
- while (g_hash_table_iter_next (&iter, (gpointer*)&uri, NULL))
- seahorse_pgp_backend_remove_remote (self, uri);
+ /* Mark this one as present */
+ g_hash_table_remove (check, uri);
+ }
- g_hash_table_destroy (check);
- g_strfreev (keyservers);
+ /* Now remove any extras */
+ g_hash_table_iter_init (&iter, check);
+ while (g_hash_table_iter_next (&iter, (gpointer*)&uri, NULL))
+ seahorse_pgp_backend_remove_remote (self, uri);
}
#endif /* WITH_KEYSERVER */
@@ -167,11 +164,13 @@ seahorse_pgp_backend_constructed (GObject *obj)
self->unknown = seahorse_unknown_source_new ();
#ifdef WITH_KEYSERVER
- g_signal_connect (seahorse_pgp_settings_instance (), "changed::keyservers",
- G_CALLBACK (on_settings_keyservers_changed), self);
+ g_signal_connect (seahorse_pgp_settings_instance (), "changed::keyservers",
+ G_CALLBACK (on_settings_keyservers_changed), self);
- /* Initial loading */
- on_settings_keyservers_changed (G_SETTINGS (seahorse_pgp_settings_instance ()), "keyservers", self);
+ /* Initial loading */
+ on_settings_keyservers_changed (G_SETTINGS (seahorse_pgp_settings_instance ()),
+ "keyservers",
+ self);
#endif
}
@@ -241,11 +240,11 @@ seahorse_pgp_backend_get_property (GObject *obj,
static void
seahorse_pgp_backend_finalize (GObject *obj)
{
- SeahorsePgpBackend *self = SEAHORSE_PGP_BACKEND (obj);
+ SeahorsePgpBackend *self = SEAHORSE_PGP_BACKEND (obj);
#ifdef WITH_KEYSERVER
- g_signal_handlers_disconnect_by_func (seahorse_pgp_settings_instance (),
- on_settings_keyservers_changed, self);
+ g_signal_handlers_disconnect_by_func (seahorse_pgp_settings_instance (),
+ on_settings_keyservers_changed, self);
#endif
g_clear_object (&self->keyring);
diff --git a/src/main.vala b/src/main.vala
index 96d1f9f0..0d1eac0b 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -28,7 +28,6 @@ public int main (string[] args) {
int status = app.run(args);
Seahorse.Registry.cleanup();
- Seahorse.Servers.cleanup();
return status;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]