[geary/wip/714104-refine-account-dialog] Clean up Geary.Protocol string management



commit e794c8522cbda4d368ea0066a0331a5fbf75a2bd
Author: Michael Gratton <mike vee net>
Date:   Wed Dec 26 16:08:56 2018 +1030

    Clean up Geary.Protocol string management

 src/client/application/secret-mediator.vala   | 12 ++++++++----
 src/engine/api/geary-service-information.vala | 15 +++++----------
 2 files changed, 13 insertions(+), 14 deletions(-)
---
diff --git a/src/client/application/secret-mediator.vala b/src/client/application/secret-mediator.vala
index fa645e0c..85264ef5 100644
--- a/src/client/application/secret-mediator.vala
+++ b/src/client/application/secret-mediator.vala
@@ -68,7 +68,7 @@ public class SecretMediator : Geary.CredentialsMediator, Object {
                 debug(
                     "Unable to fetch libsecret password for %s: %s %s",
                     account.id,
-                    service.protocol.to_string(),
+                    to_proto_value(service.protocol),
                     service.credentials.user
                 );
             }
@@ -127,7 +127,7 @@ public class SecretMediator : Geary.CredentialsMediator, Object {
                 debug(
                     "Unable to store libsecret password for %s: %s %s",
                     account.id,
-                    service.protocol.to_string(),
+                    to_proto_value(service.protocol),
                     service.credentials.user
                 );
             }
@@ -201,7 +201,7 @@ public class SecretMediator : Geary.CredentialsMediator, Object {
             SecretMediator.schema,
             new_attrs(service),
             Secret.COLLECTION_DEFAULT,
-            "Geary %s password".printf(service.protocol.to_value()),
+            "Geary %s password".printf(to_proto_value(service.protocol)),
             password,
             cancellable
         );
@@ -211,12 +211,16 @@ public class SecretMediator : Geary.CredentialsMediator, Object {
         HashTable<string,string> table = new HashTable<string,string>(
             str_hash, str_equal
         );
-        table.insert(ATTR_PROTO, service.protocol.to_value());
+        table.insert(ATTR_PROTO, to_proto_value(service.protocol));
         table.insert(ATTR_HOST, service.host);
         table.insert(ATTR_LOGIN, service.credentials.user);
         return table;
     }
 
+    private inline string to_proto_value(Geary.Protocol protocol) {
+        return protocol.to_value().ascii_up();
+    }
+
     private async string? migrate_old_password(Geary.ServiceInformation service,
                                                GLib.Cancellable? cancellable)
         throws GLib.Error {
diff --git a/src/engine/api/geary-service-information.vala b/src/engine/api/geary-service-information.vala
index cd1531cf..e0df4abf 100644
--- a/src/engine/api/geary-service-information.vala
+++ b/src/engine/api/geary-service-information.vala
@@ -15,20 +15,15 @@ public enum Geary.Protocol {
 
     public static Protocol for_value(string value)
         throws EngineError {
-        switch (value.ascii_up()) {
-        case "IMAP":
-            return IMAP;
-        case "SMTP":
-            return SMTP;
-        }
-        throw new EngineError.BAD_PARAMETERS(
-            "Unknown Protocol value: %s", value
+        return ObjectUtils.from_enum_nick<Protocol>(
+            typeof(Protocol), value.ascii_down()
         );
     }
 
     public string to_value() {
-        string value = to_string();
-        return value.substring(value.last_index_of("_") + 1);
+        return ObjectUtils.to_enum_nick<Protocol>(
+            typeof(Protocol), this
+        );
     }
 
 }


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