[geary/bug/714643-account-id: 4/4] Use an opaque string instead of primary email address as account id.



commit ec600220526bdedca71606d902c499537e79a256
Author: Michael James Gratton <mike vee net>
Date:   Wed Jul 20 16:43:01 2016 +1000

    Use an opaque string instead of primary email address as account id.
    
    Bug 714643.
    
    * src/engine/api/geary-engine.vala (Engine::create_orphan_account):
      Remove id param, just generate a new id as needed. Fix call site.

 src/client/accounts/add-edit-page.vala |    2 +-
 src/engine/api/geary-engine.vala       |   30 ++++++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 3 deletions(-)
---
diff --git a/src/client/accounts/add-edit-page.vala b/src/client/accounts/add-edit-page.vala
index dc2beff..b4b9674 100644
--- a/src/client/accounts/add-edit-page.vala
+++ b/src/client/accounts/add-edit-page.vala
@@ -656,7 +656,7 @@ public class AddEditPage : Gtk.Box {
         if (this.id == null) {
             // New account
             try {
-                info = Geary.Engine.instance.create_orphan_account(this.email_address);
+                info = Geary.Engine.instance.create_orphan_account();
             } catch (Error err) {
                 debug("Unable to create account %s for %s: %s",
                       this.id, this.email_address, err.message);
diff --git a/src/engine/api/geary-engine.vala b/src/engine/api/geary-engine.vala
index 924f2a7..9042e5e 100644
--- a/src/engine/api/geary-engine.vala
+++ b/src/engine/api/geary-engine.vala
@@ -16,6 +16,10 @@
  */
 
 public class Geary.Engine : BaseObject {
+
+    private const string ID_PREFIX = "account_";
+    private const string ID_FORMAT = "account_%02u";
+
     [Flags]
     public enum ValidationOption {
         NONE = 0,
@@ -249,11 +253,33 @@ public class Geary.Engine : BaseObject {
     }
 
     /**
-     * Returns a new account for the given account id not yet stored on disk.
+     * Returns a new account, not yet stored on disk.
+     *
+     * Throws an error if the engine has not been opened or if an
+     * invalid account id is generated.
      */
-    public AccountInformation create_orphan_account(string id) throws Error {
+    public AccountInformation create_orphan_account() throws Error {
         check_opened();
 
+        // We might want to allow the client to specify the id, but
+        // just generate one here for now: Use a common prefix and a
+        // numeric suffix, starting at 1. To generate the next id,
+        // find the last account and increment its suffix.
+
+        string? last_account = this.accounts.keys.fold<string?>((next, last) => {
+                string? result = last;
+                if (next.has_prefix(ID_PREFIX)) {
+                    result = (last == null || strcmp(last, next) > 0) ? next : last;
+                }
+                return result;
+            },
+            null);
+        uint next_id = 1;
+        if (last_account != null) {
+            next_id = int.parse(last_account.substring(ID_PREFIX.length)) + 1;
+        }
+        string id = ID_FORMAT.printf(next_id);
+
         if (accounts.has_key(id))
             throw new EngineError.ALREADY_EXISTS("Account %s already exists", id);
 


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