[geary/wip/714104-refine-account-dialog: 155/180] Make ServiceProvier enuum use "value" (de)seriaisation style



commit 854c0fe479a4556bf954ca89ac74e57a38706b0d
Author: Michael James Gratton <mike vee net>
Date:   Tue Aug 7 17:48:23 2018 +1000

    Make ServiceProvier enuum use "value" (de)seriaisation style
    
    This make makes it consistent with ServiceInformation enums.

 src/client/accounts/accounts-manager.vala  |   4 +-
 src/client/accounts/add-edit-page.vala     |  16 ++++-
 src/engine/api/geary-service-provider.vala | 103 ++++++-----------------------
 3 files changed, 37 insertions(+), 86 deletions(-)
---
diff --git a/src/client/accounts/accounts-manager.vala b/src/client/accounts/accounts-manager.vala
index da4dcaf2..87701de0 100644
--- a/src/client/accounts/accounts-manager.vala
+++ b/src/client/accounts/accounts-manager.vala
@@ -613,7 +613,7 @@ public class Accounts.Manager : GLib.Object {
         config.set_string(REAL_NAME_KEY, info.primary_mailbox.name);
         config.set_string(PRIMARY_EMAIL_KEY, info.primary_mailbox.address);
         config.set_string(NICKNAME_KEY, info.nickname);
-        config.set_string(SERVICE_PROVIDER_KEY, info.service_provider.to_string());
+        config.set_string(SERVICE_PROVIDER_KEY, info.service_provider.to_value());
         config.set_int(ORDINAL_KEY, info.ordinal);
         config.set_int(PREFETCH_PERIOD_DAYS_KEY, info.prefetch_period_days);
         config.set_bool(SAVE_SENT_MAIL_KEY, info.save_sent_mail);
@@ -754,7 +754,7 @@ public class Accounts.Manager : GLib.Object {
                               string fallback_login)
         throws GLib.Error {
 
-        Geary.ServiceProvider provider = Geary.ServiceProvider.from_string(
+        Geary.ServiceProvider provider = Geary.ServiceProvider.for_value(
             config.get_string(SERVICE_PROVIDER_KEY,
                               Geary.ServiceProvider.GMAIL.to_string())
         );
diff --git a/src/client/accounts/add-edit-page.vala b/src/client/accounts/add-edit-page.vala
index 21a38bb4..2e989748 100644
--- a/src/client/accounts/add-edit-page.vala
+++ b/src/client/accounts/add-edit-page.vala
@@ -316,8 +316,13 @@ public class AddEditPage : Gtk.Box {
         check_save_drafts = (Gtk.CheckButton) builder.get_object("check: save_drafts"); 
 
         // Build list of service providers.
-        foreach (Geary.ServiceProvider p in Geary.ServiceProvider.get_providers())
-            combo_service.append_text(p.display_name());
+        foreach (Geary.ServiceProvider p in new Geary.ServiceProvider[] {
+                Geary.ServiceProvider.GMAIL,
+                Geary.ServiceProvider.OUTLOOK,
+                Geary.ServiceProvider.YAHOO,
+                Geary.ServiceProvider.OTHER,
+                    })
+            combo_service.append_text(p.to_value());
         
         reset_all();
         
@@ -878,7 +883,12 @@ public class AddEditPage : Gtk.Box {
     }
     
     public void set_service_provider(Geary.ServiceProvider provider) {
-        foreach (Geary.ServiceProvider p in Geary.ServiceProvider.get_providers()) {
+        foreach (Geary.ServiceProvider p in new Geary.ServiceProvider[] {
+                Geary.ServiceProvider.GMAIL,
+                Geary.ServiceProvider.OUTLOOK,
+                Geary.ServiceProvider.YAHOO,
+                Geary.ServiceProvider.OTHER,
+                    }) {
             if (p == provider)
                 combo_service.set_active(p);
         }
diff --git a/src/engine/api/geary-service-provider.vala b/src/engine/api/geary-service-provider.vala
index 574b874c..c795591d 100644
--- a/src/engine/api/geary-service-provider.vala
+++ b/src/engine/api/geary-service-provider.vala
@@ -1,4 +1,6 @@
-/* Copyright 2016 Software Freedom Conservancy Inc.
+/*
+ * Copyright 2016 Software Freedom Conservancy Inc.
+ * 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.
@@ -13,88 +15,27 @@ public enum Geary.ServiceProvider {
     YAHOO,
     OUTLOOK,
     OTHER;
-    
-    public static ServiceProvider[] get_providers() {
-        return { GMAIL, YAHOO, OUTLOOK, OTHER };
-    }
-    
-    /**
-     * Returns the service provider in a serialized form.
-     *
-     * @see from_string
-     */
-    public string to_string() {
-        switch (this) {
-            case GMAIL:
-                return "GMAIL";
-            
-            case YAHOO:
-                return "YAHOO";
-            
-            case OUTLOOK:
-                return "OUTLOOK";
-            
-            case OTHER:
-                return "OTHER";
-            
-            default:
-                assert_not_reached();
-        }
-    }
-    
-    /**
-     * Returns the service provider's name in a translated UTF-8 string suitable for display to the
-     * user.
-     */
-    public string display_name() {
-        switch (this) {
-            case GMAIL:
-                return _("Gmail");
-            
-            case YAHOO:
-                return _("Yahoo! Mail");
-            
-            case OUTLOOK:
-                return _("Outlook.com");
-            
-            case OTHER:
-                return _("Other");
-            
-            default:
-                assert_not_reached();
+
+    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
+        );
     }
 
-    /**
-     * Converts a string form of the service provider (returned by
-     * {@link to_string} to a {@link ServiceProvider} value.
-     *
-     * Throws an error if the string is not valid.
-     *
-     * @see to_string
-     */
-    public static ServiceProvider from_string(string str) throws Error {
-        switch (str.up()) {
-            case "GMAIL":
-                return GMAIL;
-            
-            case "YAHOO":
-                return YAHOO;
-            
-            case "OUTLOOK":
-                return OUTLOOK;
-            
-            case "OTHER":
-                return OTHER;
-
-            default:
-                // Could use a better errordomain here, but for now
-                // this only gets used when parsing keyfiles in
-                // AccountInfo.
-                throw new KeyFileError.INVALID_VALUE(
-                    "Unknown service provider type: %s", str
-                );
-        }
+    public string to_value() {
+        string value = to_string();
+        return value.substring(value.last_index_of("_") + 1);
     }
-}
 
+}


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