[geary/wip/save-sent-713263: 1/2] Basic save sent mail support



commit 00969d22982b940c8843286e65daac97d1c7a00a
Author: Charles Lindsay <chaz yorba org>
Date:   Wed Jan 15 18:25:50 2014 -0800

    Basic save sent mail support

 src/engine/abstract/geary-abstract-account.vala    |    8 +---
 src/engine/api/geary-account.vala                  |    1 +
 .../imap-engine/imap-engine-generic-account.vala   |   42 ++++++++++++++++++++
 .../imap-engine-generic-sent-mail-folder.vala      |   10 +++-
 4 files changed, 52 insertions(+), 9 deletions(-)
---
diff --git a/src/engine/abstract/geary-abstract-account.vala b/src/engine/abstract/geary-abstract-account.vala
index 920a268..1104300 100644
--- a/src/engine/abstract/geary-abstract-account.vala
+++ b/src/engine/abstract/geary-abstract-account.vala
@@ -100,12 +100,8 @@ public abstract class Geary.AbstractAccount : BaseObject, Geary.Account {
         Cancellable? cancellable = null) throws Error;
     
     public virtual Geary.Folder? get_special_folder(Geary.SpecialFolderType special) throws Error {
-        foreach (Folder folder in list_folders()) {
-            if (folder.special_folder_type == special)
-                return folder;
-        }
-        
-        return null;
+        return Geary.traverse<Geary.Folder>(list_folders())
+            .first_matching(f => f.special_folder_type == special);
     }
     
     public abstract async void send_email_async(Geary.ComposedEmail composed, Cancellable? cancellable = 
null)
diff --git a/src/engine/api/geary-account.vala b/src/engine/api/geary-account.vala
index 3c83947..db2769a 100644
--- a/src/engine/api/geary-account.vala
+++ b/src/engine/api/geary-account.vala
@@ -12,6 +12,7 @@ public interface Geary.Account : BaseObject {
         NETWORK_UNAVAILABLE,
         DATABASE_FAILURE,
         EMAIL_DELIVERY_FAILURE,
+        SAVE_SENT_EMAIL_FAILED,
     }
     
     public abstract Geary.AccountInformation information { get; protected set; }
diff --git a/src/engine/imap-engine/imap-engine-generic-account.vala 
b/src/engine/imap-engine/imap-engine-generic-account.vala
index 2fce9e5..45730d7 100644
--- a/src/engine/imap-engine/imap-engine-generic-account.vala
+++ b/src/engine/imap-engine/imap-engine-generic-account.vala
@@ -534,6 +534,48 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
 
     private void on_email_sent(Geary.RFC822.Message rfc822) {
         notify_email_sent(rfc822);
+        handle_sent_email_async.begin(rfc822, null, on_handle_sent_email_async_finished);
+    }
+    
+    private void on_handle_sent_email_async_finished(Object? source, AsyncResult result) {
+        try {
+            handle_sent_email_async.end(result);
+        } catch (Error e) {
+            debug("Error saving sent email: %s", e.message);
+            notify_report_problem(Geary.Account.Problem.SAVE_SENT_EMAIL_FAILED, e);
+        }
+    }
+    
+    private async void handle_sent_email_async(Geary.RFC822.Message rfc822, Cancellable? cancellable)
+        throws Error {
+        // TODO: check account option, simply return
+        
+        Geary.Folder? sent_mail = get_special_folder(Geary.SpecialFolderType.SENT);
+        Geary.FolderSupport.Create? create = sent_mail as Geary.FolderSupport.Create;
+        if (create == null)
+            throw new EngineError.NOT_FOUND("Save sent mail enabled, but no sent mail folder");
+        
+        bool open = false;
+        try {
+            yield create.open_async(Geary.Folder.OpenFlags.FAST_OPEN, cancellable);
+            open = true;
+            
+            yield create.create_email_async(rfc822, null, new DateTime.now_local() /* TODO: is that correct? 
*/,
+                null, cancellable);
+            
+            yield create.close_async(cancellable);
+            open = false;
+        } catch (Error e) {
+            if (open) {
+                try {
+                    yield create.close_async(cancellable);
+                    open = false;
+                } catch (Error e) {
+                    debug("Error closing folder %s: %s", create.to_string(), e.message);
+                }
+            }
+            throw e;
+        }
     }
     
     private ImapDB.EmailIdentifier check_id(Geary.EmailIdentifier id) throws EngineError {
diff --git a/src/engine/imap-engine/imap-engine-generic-sent-mail-folder.vala 
b/src/engine/imap-engine/imap-engine-generic-sent-mail-folder.vala
index 5068284..98efac3 100644
--- a/src/engine/imap-engine/imap-engine-generic-sent-mail-folder.vala
+++ b/src/engine/imap-engine/imap-engine-generic-sent-mail-folder.vala
@@ -5,14 +5,18 @@
  */
 
 // Sent Mail generally is the same as other mail folders, but it doesn't support key features,
-// like archiving (since sent messages are in the archive).
+// like archiving (since sent messages are in the archive).  Instead, it supports appending.
 //
 // Service-specific accounts can use this or subclass it for further customization
 
-private class Geary.ImapEngine.GenericSentMailFolder : GenericFolder {
+private class Geary.ImapEngine.GenericSentMailFolder : GenericFolder, Geary.FolderSupport.Create {
     public GenericSentMailFolder(GenericAccount account, Imap.Account remote, ImapDB.Account local,
         ImapDB.Folder local_folder, SpecialFolderType special_folder_type) {
         base (account, remote, local, local_folder, special_folder_type);
     }
+    
+    public new async Geary.EmailIdentifier? create_email_async(RFC822.Message message, Geary.EmailFlags? 
flags,
+        DateTime? date_received, Geary.EmailIdentifier? id, Cancellable? cancellable) throws Error {
+        return yield base.create_email_async(message, flags, date_received, id, cancellable);
+    }
 }
-


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