[geary/wip/714922-multiple-addresses: 1/4] Add alternate emails to account information, composer



commit 74fcf770456cfc572afc17387b366df983392721
Author: Charles Lindsay <chaz yorba org>
Date:   Thu Mar 20 11:21:40 2014 -0700

    Add alternate emails to account information, composer

 src/client/composer/composer-window.vala          |  110 ++++++++++++--------
 src/engine/api/geary-account-information.vala     |   17 +++-
 src/engine/imap-db/outbox/smtp-outbox-folder.vala |    2 +-
 3 files changed, 81 insertions(+), 48 deletions(-)
---
diff --git a/src/client/composer/composer-window.vala b/src/client/composer/composer-window.vala
index d0a83e4..209976e 100644
--- a/src/client/composer/composer-window.vala
+++ b/src/client/composer/composer-window.vala
@@ -13,6 +13,15 @@ public class ComposerWindow : Gtk.Window {
         FORWARD
     }
     
+    private class FromAddressMap {
+        public Geary.Account account;
+        public string email;
+        public FromAddressMap(Geary.Account a, string e) {
+            account = a;
+            email = e;
+        }
+    }
+    
     public const string ACTION_UNDO = "undo";
     public const string ACTION_REDO = "redo";
     public const string ACTION_CUT = "cut";
@@ -144,6 +153,7 @@ public class ComposerWindow : Gtk.Window {
     private Gtk.Label from_label;
     private Gtk.Label from_single;
     private Gtk.ComboBoxText from_multiple = new Gtk.ComboBoxText();
+    private Gee.ArrayList<FromAddressMap> from_list = new Gee.ArrayList<FromAddressMap>();
     private EmailEntry to_entry;
     private EmailEntry cc_entry;
     private EmailEntry bcc_entry;
@@ -321,7 +331,7 @@ public class ComposerWindow : Gtk.Window {
         
         add_extra_accelerators();
         
-        from = account.information.get_from().to_rfc822_string();
+        from = account.information.get_primary_from().to_rfc822_string();
         update_from_field();
         from_multiple.changed.connect(on_from_changed);
         
@@ -354,7 +364,8 @@ public class ComposerWindow : Gtk.Window {
                 
                 case ComposeType.REPLY:
                 case ComposeType.REPLY_ALL:
-                    string? sender_address = account.information.get_mailbox_address().address;
+                    // TODO: find alternate email address?
+                    string? sender_address = account.information.email;
                     to = Geary.RFC822.Utils.create_to_addresses_for_reply(referred, sender_address);
                     if (compose_type == ComposeType.REPLY_ALL)
                         cc = Geary.RFC822.Utils.create_cc_addresses_for_reply_all(referred, sender_address);
@@ -458,6 +469,7 @@ public class ComposerWindow : Gtk.Window {
         
         // If there's only one account, open the drafts folder.  If there's more than one account,
         // the drafts folder will be opened by on_from_changed().
+        // TODO: determine if there's only one account, not just if the combo box is visible
         if (!from_multiple.visible)
             open_drafts_folder_async.begin(cancellable_drafts);
     }
@@ -1633,6 +1645,22 @@ public class ComposerWindow : Gtk.Window {
         }
     }
     
+    private void add_account_emails_to_from_list(Geary.Account account) {
+        from_multiple.append_text(account.information.email);
+        from_list.add(new FromAddressMap(account, account.information.email));
+        
+        if (account.information.alternate_emails != null) {
+            foreach (string alternate_email in account.information.alternate_emails) {
+                // Displayed in the From dropdown to indicate an "alternate email address"
+                // for an account.  The first printf argument will be the alternate email
+                // address, and the second will be the account's primary email address.
+                string display = _("%1$s via %2$s").printf(alternate_email, account.information.email);
+                from_multiple.append_text(display);
+                from_list.add(new FromAddressMap(account, alternate_email));
+            }
+        }
+    }
+    
     private void update_from_field() {
         from_single.visible = from_multiple.visible = from_label.visible = false;
         
@@ -1645,38 +1673,40 @@ public class ComposerWindow : Gtk.Window {
             return;
         }
         
-        // If there's only one account, show nothing. (From fields are hidden above.)
-        if (accounts.size <= 1)
+        if (accounts.size < 1 || (accounts.size == 1 && Geary.traverse<Geary.AccountInformation>(
+            accounts.values).first().alternate_emails == null))
             return;
         
         from_label.visible = true;
         
+        from_label.set_use_underline(true);
+        from_label.set_mnemonic_widget(from_multiple);
+        // Composer label (with mnemonic underscore) for the account selector
+        // when choosing what address to send a message from.
+        from_label.set_text_with_mnemonic(_("_From:"));
+        
+        from_multiple.visible = true;
+        from_multiple.remove_all();
+        from_list = new Gee.ArrayList<FromAddressMap>();
+        
         if (compose_type == ComposeType.NEW_MESSAGE) {
-            // For new messages, show the account combo-box.
-            from_label.set_use_underline(true);
-            from_label.set_mnemonic_widget(from_multiple);
-            // Composer label (with mnemonic underscore) for the account selector
-            // when choosing what address to send a message from.
-            from_label.set_text_with_mnemonic(_("_From:"));
-            
-            from_multiple.visible = true;
-            from_multiple.remove_all();
-            foreach (Geary.AccountInformation a in accounts.values)
-                from_multiple.append(a.email, a.get_mailbox_address().get_full_address());
+            add_account_emails_to_from_list(account);
+            foreach (Geary.AccountInformation info in accounts.values) {
+                try {
+                    Geary.Account a = Geary.Engine.instance.get_account_instance(info);
+                    if (a != account)
+                        add_account_emails_to_from_list(a);
+                } catch (Error e) {
+                    debug("Error getting account in composer: %s", e.message);
+                }
+            }
             
-            // Set the active account to the currently selected account, or failing that, set it
-            // to the first account in the list.
-            if (!from_multiple.set_active_id(account.information.email))
-                from_multiple.set_active(0);
+            from_multiple.set_active(0);
         } else {
-            // For other types of messages, just show the from account.
-            from_label.set_use_underline(false);
-            // Composer label (without mnemonic underscore) for the account selector
-            // when choosing what address to send a message from.
-            from_label.set_text(_("From:"));
+            add_account_emails_to_from_list(account);
             
-            from_single.label = account.information.get_mailbox_address().get_full_address();
-            from_single.visible = true;
+            // TODO: select address the original message was to, if available.
+            from_multiple.set_active(0);
         }
     }
     
@@ -1684,25 +1714,17 @@ public class ComposerWindow : Gtk.Window {
         if (compose_type != ComposeType.NEW_MESSAGE)
             return;
         
-        // Since we've set the combo box ID to the email addresses, we can
-        // fetch that and use it to grab the account from the engine.
-        string? id = from_multiple.get_active_id();
-        Geary.AccountInformation? new_account_info = null;
+        int index = from_multiple.get_active();
+        if (index < 0)
+            return;
         
-        if (id != null) {
-            try {
-                new_account_info = Geary.Engine.instance.get_accounts().get(id);
-                if (new_account_info != null) {
-                    account = Geary.Engine.instance.get_account_instance(new_account_info);
-                    from = new_account_info.get_from().to_rfc822_string();
-                    set_entry_completions();
-                    
-                    open_drafts_folder_async.begin(cancellable_drafts);
-                }
-            } catch (Error e) {
-                debug("Error updating account in Composer: %s", e.message);
-            }
-        }
+        assert(from_list.size > index);
+        account = from_list.get(index).account;
+        from = new Geary.RFC822.MailboxAddresses.single(new Geary.RFC822.MailboxAddress(
+            account.information.real_name, from_list.get(index).email)).to_rfc822_string();
+        set_entry_completions();
+        
+        open_drafts_folder_async.begin(cancellable_drafts);
         
         reset_draft_timer();
     }
diff --git a/src/engine/api/geary-account-information.vala b/src/engine/api/geary-account-information.vala
index 0c0e4e9..7c1022e 100644
--- a/src/engine/api/geary-account-information.vala
+++ b/src/engine/api/geary-account-information.vala
@@ -10,6 +10,7 @@ public class Geary.AccountInformation : BaseObject {
     private const string GROUP = "AccountInformation";
     private const string REAL_NAME_KEY = "real_name";
     private const string NICKNAME_KEY = "nickname";
+    private const string ALTERNATE_EMAILS_KEY = "alternate_emails";
     private const string SERVICE_PROVIDER_KEY = "service_provider";
     private const string ORDINAL_KEY = "ordinal";
     private const string PREFETCH_PERIOD_DAYS_KEY = "prefetch_period_days";
@@ -55,6 +56,7 @@ public class Geary.AccountInformation : BaseObject {
     public string real_name { get; set; }
     public string nickname { get; set; }
     public string email { get; set; }
+    public Gee.List<string>? alternate_emails { get; set; }
     public Geary.ServiceProvider service_provider { get; set; }
     public int prefetch_period_days { get; set; }
     
@@ -120,6 +122,9 @@ public class Geary.AccountInformation : BaseObject {
         } finally {
             real_name = get_string_value(key_file, GROUP, REAL_NAME_KEY);
             nickname = get_string_value(key_file, GROUP, NICKNAME_KEY);
+            alternate_emails = get_string_list_value(key_file, GROUP, ALTERNATE_EMAILS_KEY);
+            if (alternate_emails.size == 0)
+                alternate_emails = null;
             imap_credentials.user = get_string_value(key_file, GROUP, IMAP_USERNAME_KEY, email);
             imap_remember_password = get_bool_value(key_file, GROUP, IMAP_REMEMBER_PASSWORD_KEY, true);
             smtp_credentials.user = get_string_value(key_file, GROUP, SMTP_USERNAME_KEY, email);
@@ -173,6 +178,12 @@ public class Geary.AccountInformation : BaseObject {
         real_name = from.real_name;
         nickname = from.nickname;
         email = from.email;
+        alternate_emails = null;
+        if (from.alternate_emails != null) {
+            alternate_emails = new Gee.ArrayList<string>();
+            foreach (string alternate_email in from.alternate_emails)
+                alternate_emails.add(alternate_email);
+        }
         service_provider = from.service_provider;
         prefetch_period_days = from.prefetch_period_days;
         save_sent_mail = from.save_sent_mail;
@@ -670,15 +681,15 @@ public class Geary.AccountInformation : BaseObject {
     /**
      * Returns a MailboxAddress object for this account.
      */
-    public RFC822.MailboxAddress get_mailbox_address() {
+    public RFC822.MailboxAddress get_primary_mailbox_address() {
         return new RFC822.MailboxAddress(real_name, email);
     }
     
     /**
      * Returns a MailboxAddresses object with this mailbox address.
      */
-    public RFC822.MailboxAddresses get_from() {
-        return new RFC822.MailboxAddresses.single(get_mailbox_address());
+    public RFC822.MailboxAddresses get_primary_from() {
+        return new RFC822.MailboxAddresses.single(get_primary_mailbox_address());
     }
     
     public static int compare_ascending(AccountInformation a, AccountInformation b) {
diff --git a/src/engine/imap-db/outbox/smtp-outbox-folder.vala 
b/src/engine/imap-db/outbox/smtp-outbox-folder.vala
index 9499aff..2de8990 100644
--- a/src/engine/imap-db/outbox/smtp-outbox-folder.vala
+++ b/src/engine/imap-db/outbox/smtp-outbox-folder.vala
@@ -613,7 +613,7 @@ private class Geary.SmtpOutboxFolder : Geary.AbstractLocalFolder, Geary.FolderSu
         
         if (smtp_err == null) {
             try {
-                yield smtp.send_email_async(_account.information.get_mailbox_address(),
+                yield smtp.send_email_async(_account.information.get_primary_mailbox_address(),
                     rfc822, cancellable);
             } catch (Error send_err) {
                 debug("SMTP send mail error: %s", send_err.message);


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