[geary/wip/714104-refine-account-dialog: 1/15] Add general impls for converting enum values to nicks and back.



commit 72b737c069b017d411fff75bcbfbd194fe48fb48
Author: Michael Gratton <mike vee net>
Date:   Wed Sep 5 23:10:30 2018 +1000

    Add general impls for converting enum values to nicks and back.
    
    Use this for service provider related enums.

 src/engine/api/geary-service-information.vala      | 34 +++++++---------------
 src/engine/api/geary-service-provider.vala         | 19 ++++--------
 src/engine/util/util-object.vala                   | 17 +++++++++++
 .../engine/api/geary-service-information-test.vala | 34 ++++++++++++++++++++++
 test/meson.build                                   |  1 +
 test/test-engine.vala                              |  1 +
 6 files changed, 68 insertions(+), 38 deletions(-)
---
diff --git a/src/engine/api/geary-service-information.vala b/src/engine/api/geary-service-information.vala
index 0f758377..e82cfbab 100644
--- a/src/engine/api/geary-service-information.vala
+++ b/src/engine/api/geary-service-information.vala
@@ -46,22 +46,15 @@ public enum Geary.TlsNegotiationMethod {
 
     public static TlsNegotiationMethod for_value(string value)
         throws EngineError {
-        switch (value.ascii_up()) {
-        case "NONE":
-            return NONE;
-        case "START_TLS":
-            return START_TLS;
-        case "TRANSPORT":
-            return TRANSPORT;
-        }
-        throw new EngineError.BAD_PARAMETERS(
-            "Unknown Protocol value: %s", value
+        return ObjectUtils.from_enum_nick<TlsNegotiationMethod>(
+            typeof(TlsNegotiationMethod), value.ascii_down()
         );
     }
 
     public string to_value() {
-        string value = to_string();
-        return value.substring(value.last_index_of("_") + 1);
+        return ObjectUtils.to_enum_nick<TlsNegotiationMethod>(
+            typeof(TlsNegotiationMethod), this
+        );
     }
 
 }
@@ -78,22 +71,15 @@ public enum Geary.SmtpCredentials {
 
     public static SmtpCredentials for_value(string value)
         throws EngineError {
-        switch (value.ascii_up()) {
-        case "NONE":
-            return Geary.SmtpCredentials.NONE;
-        case "IMAP":
-            return Geary.SmtpCredentials.IMAP;
-        case "CUSTOM":
-            return Geary.SmtpCredentials.CUSTOM;
-        }
-        throw new EngineError.BAD_PARAMETERS(
-            "Unknown SmtpCredentials value: %s", value
+        return ObjectUtils.from_enum_nick<SmtpCredentials>(
+            typeof(SmtpCredentials), value.ascii_down()
         );
     }
 
     public string to_value() {
-        string value = to_string();
-        return value.substring(value.last_index_of("_") + 1);
+        return ObjectUtils.to_enum_nick<SmtpCredentials>(
+            typeof(SmtpCredentials), this
+        );
     }
 
 }
diff --git a/src/engine/api/geary-service-provider.vala b/src/engine/api/geary-service-provider.vala
index f778b100..540526a6 100644
--- a/src/engine/api/geary-service-provider.vala
+++ b/src/engine/api/geary-service-provider.vala
@@ -18,24 +18,15 @@ public enum Geary.ServiceProvider {
 
     public static ServiceProvider for_value(string value)
         throws EngineError {
-        switch (value.ascii_up()) {
-        case "GMAIL":
-            return GMAIL;
-        case "YAHOO":
-            return YAHOO;
-        case "OUTLOOK":
-            return OUTLOOK;
-        case "OTHER":
-            return OTHER;
-        }
-        throw new EngineError.BAD_PARAMETERS(
-            "Unknown Geary.ServiceProvider value: %s", value
+        return ObjectUtils.from_enum_nick<ServiceProvider>(
+            typeof(ServiceProvider), value.ascii_down()
         );
     }
 
     public string to_value() {
-        string value = to_string();
-        return value.substring(value.last_index_of("_") + 1);
+        return ObjectUtils.to_enum_nick<ServiceProvider>(
+            typeof(ServiceProvider), this
+        );
     }
 
     public void setup_service(ServiceInformation service) {
diff --git a/src/engine/util/util-object.vala b/src/engine/util/util-object.vala
index b124d9eb..e47aa5a3 100644
--- a/src/engine/util/util-object.vala
+++ b/src/engine/util/util-object.vala
@@ -42,5 +42,22 @@ public void unmirror_properties(Gee.List<Binding> bindings) {
     bindings.clear();
 }
 
+/** Convenience method for getting an enum value's nick name. */
+internal string to_enum_nick<E>(GLib.Type type, E value) {
+    GLib.EnumClass enum_type = (GLib.EnumClass) type.class_ref();
+    return enum_type.get_value((int) value).value_nick;
 }
 
+/** Convenience method for getting an enum value's from its nick name. */
+internal E from_enum_nick<E>(GLib.Type type, string nick) throws EngineError {
+    GLib.EnumClass enum_type = (GLib.EnumClass) type.class_ref();
+    unowned GLib.EnumValue? e_value = enum_type.get_value_by_nick(nick);
+    if (e_value == null) {
+        throw new EngineError.BAD_PARAMETERS(
+            "Unknown %s enum value: %s", typeof(E).name(), nick
+        );
+    }
+    return (E) e_value.value;
+}
+
+}
diff --git a/test/engine/api/geary-service-information-test.vala 
b/test/engine/api/geary-service-information-test.vala
new file mode 100644
index 00000000..88e54270
--- /dev/null
+++ b/test/engine/api/geary-service-information-test.vala
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2018 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+class Geary.TlsNegotiationMethodTest : TestCase {
+
+
+    public TlsNegotiationMethodTest() {
+        base("Geary.TlsNegotiationMethodTest");
+        add_test("to_value", to_value);
+        add_test("for_value", for_value);
+    }
+
+    public void to_value() throws GLib.Error {
+        assert_string("start-tls", TlsNegotiationMethod.START_TLS.to_value());
+    }
+
+    public void for_value() throws GLib.Error {
+        assert_int(
+            TlsNegotiationMethod.START_TLS,
+            TlsNegotiationMethod.for_value("start-tls"),
+            "start-tls"
+        );
+        assert_int(
+            TlsNegotiationMethod.START_TLS,
+            TlsNegotiationMethod.for_value("Start-TLS"),
+            "Start-TLS"
+        );
+    }
+
+}
\ No newline at end of file
diff --git a/test/meson.build b/test/meson.build
index 494782ae..d28fb86f 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -24,6 +24,7 @@ geary_test_engine_sources = [
   'engine/api/geary-account-information-test.vala',
   'engine/api/geary-attachment-test.vala',
   'engine/api/geary-engine-test.vala',
+  'engine/api/geary-service-information-test.vala',
   'engine/app/app-conversation-test.vala',
   'engine/app/app-conversation-monitor-test.vala',
   'engine/app/app-conversation-set-test.vala',
diff --git a/test/test-engine.vala b/test/test-engine.vala
index c6cfc3ca..83e10dda 100644
--- a/test/test-engine.vala
+++ b/test/test-engine.vala
@@ -27,6 +27,7 @@ int main(string[] args) {
     engine.add_suite(new Geary.EngineTest().get_suite());
     engine.add_suite(new Geary.IdleManagerTest().get_suite());
     engine.add_suite(new Geary.TimeoutManagerTest().get_suite());
+    engine.add_suite(new Geary.TlsNegotiationMethodTest().get_suite());
     engine.add_suite(new Geary.App.ConversationTest().get_suite());
     engine.add_suite(new Geary.App.ConversationSetTest().get_suite());
     // Depends on ConversationTest and ConversationSetTest passing


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