From f0f98fb108885aca3c29f759128040ef9e015e68 Mon Sep 17 00:00:00 2001
From: svncommit <devs@roundcube.net>
Date: Mon, 24 Jul 2006 14:41:27 -0400
Subject: [PATCH] Improvements to Draft handling

---
 CHANGELOG                            |    9 +++
 index.php                            |    4 
 program/steps/mail/upload.inc        |    1 
 program/steps/mail/compose.inc       |   39 +++++-------
 skins/default/templates/compose.html |    7 --
 program/steps/mail/sendmail.inc      |   44 ++++++++++----
 program/js/app.js                    |   40 +++++++++---
 7 files changed, 88 insertions(+), 56 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 51f9b8e..28a0986 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,15 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2006/07/24 (richs)
+----------
+- Draft window no longer reloads. It saves to an iframe in the background instead (fixes bug #1483869)
+- Draft timer now part of program/js/app.js instead of skins/default/templates/compose.inc
+- Draft saving now properly returns an error when saving fails
+- Draft timer stops and resets properly when attachments are uploaded, or when saving manually
+- Old compose session/attachments are now cleaned up when a new/forward/reply/draft is made/opened
+
+
 2006/07/19 (thomasb)
 ----------
 - Correct entity encoding of link urls (HTML validity)
diff --git a/index.php b/index.php
index 9668d52..c496b4d 100644
--- a/index.php
+++ b/index.php
@@ -291,8 +291,8 @@
     include('program/steps/mail/rss.inc');
 
   // kill compose entry from session
-  if (isset($_SESSION['compose']))
-    rcmail_compose_cleanup();
+//  if (isset($_SESSION['compose']))
+//    rcmail_compose_cleanup();
     
   // make sure the message count is refreshed
   $IMAP->messagecount($_SESSION['mbox'], 'ALL', TRUE);
diff --git a/program/js/app.js b/program/js/app.js
index b5388b2..a5ae90d 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -17,8 +17,10 @@
 var CONTROL_KEY = 1;
 var SHIFT_KEY = 2;
 var CONTROL_SHIFT_KEY = 3;
+var DRAFT_AUTOSAVE = 10; // Minutes
 
 var rcube_webmail_client;
+var rcube_save_timer;
 
 function rcube_webmail()
   {
@@ -39,7 +41,7 @@
   this.dblclick_time = 600;
   this.message_time = 5000;
   this.request_timeout = 180000;
-  this.kepp_alive_interval = 60000;
+  this._interval = 60000;
   this.mbox_expression = new RegExp('[^0-9a-z\-_]', 'gi');
   this.env.blank_img = 'skins/default/images/blank.gif';
   
@@ -254,10 +256,10 @@
       this.display_message(this.pending_message[0], this.pending_message[1]);
       
     // start interval for keep-alive/recent_check signal
-    if (this.kepp_alive_interval && this.task=='mail' && this.gui_objects.messagelist)
-      this.kepp_alive_int = setInterval(this.ref+'.check_for_recent()', this.kepp_alive_interval);
+    if (this._interval && this.task=='mail' && this.gui_objects.messagelist)
+      this._int = setInterval(this.ref+'.check_for_recent()', this.keep_alive_interval);
     else if (this.task!='login')
-      this.kepp_alive_int = setInterval(this.ref+'.send_keep_alive()', this.kepp_alive_interval);
+      this._int = setInterval(this.ref+'.send_keep_alive()', this.keep_alive_interval);
     };
 
   // reset last clicked if user clicks on anything other than the message table
@@ -436,8 +438,11 @@
     
     // get summary of all field values
     this.cmp_hash = this.compose_field_hash();
+ 
+    // start the auto-save timer
+    this.auto_save_start();
+    
     };
-
 
   this.init_address_input_events = function(obj)
     {
@@ -913,15 +918,19 @@
         break;
 
       case 'savedraft':
+	// Reset the auto-save timer
+        self.clearTimeout(rcube_save_timer);
+
         if (!this.gui_objects.messageform)
           break;
-        
+
 	// if saving Drafts is disabled in main.inc.php
 	if (!this.env.drafts_mailbox)
 	  break;
- 
+
         this.set_busy(true, 'savingmessage');
         var form = this.gui_objects.messageform;
+	form.target = "savetarget";
         form.submit();
         break;
 
@@ -943,6 +952,9 @@
         this.show_attachment_form(true);
         
       case 'send-attachment':
+        // Reset the auto-save timer
+        self.clearTimeout(rcube_save_timer);
+
         this.upload_file(props)      
         break;
       
@@ -1952,7 +1964,11 @@
     return true;
     };
     
-    
+  this.auto_save_start = function()
+    {
+    rcube_save_timer = self.setTimeout('rcmail.command("savedraft","",this)',DRAFT_AUTOSAVE * 60000);
+    }
+ 
   this.compose_field_hash = function()
     {
     // check input fields
@@ -2052,6 +2068,7 @@
   // upload attachment file
   this.upload_file = function(form)
     {
+    
     if (!form)
       return false;
       
@@ -2952,15 +2969,16 @@
   // display a system message
   this.display_message = function(msg, type, hold)
     {
+    this.set_busy(false);
     if (!this.loaded)  // save message in order to display after page loaded
       {
       this.pending_message = new Array(msg, type);
       return true;
       }
-    
+  
     if (!this.gui_objects.message)
       return false;
-      
+     
     if (this.message_timer)
       clearTimeout(this.message_timer);
     
@@ -2971,7 +2989,7 @@
     this.gui_objects.message._rcube = this;
     this.gui_objects.message.innerHTML = cont;
     this.gui_objects.message.style.display = 'block';
-    
+ 
     if (type!='loading')
       this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
     
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 7e6d306..877948c 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -22,16 +22,6 @@
 
 require_once('Mail/mimeDecode.php');
 
-
-$MESSAGE_FORM = NULL;
-$REPLY_MESSAGE = NULL;
-$FORWARD_MESSAGE = NULL;
-$DRAFT_MESSAGE = NULL;
-
-
-if (!is_array($_SESSION['compose']))
-  $_SESSION['compose'] = array('id' => uniqid(rand()));
-
 // remove an attachment
 if ($_action=='remove-attachment' && !empty($_GET['_filename']))
   {
@@ -46,6 +36,18 @@
         exit;
         }
   }
+
+$MESSAGE_FORM = NULL;
+$REPLY_MESSAGE = NULL;
+$FORWARD_MESSAGE = NULL;
+$DRAFT_MESSAGE = NULL;
+
+// nothing below is called during message composition, only at "new/forward/reply/draft" initialization
+// since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
+// compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
+
+rcmail_compose_cleanup();
+$_SESSION['compose'] = array('id' => uniqid(rand()));
 
 // add some labels to client
 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'savingmessage', 'messagesaved');
@@ -376,18 +378,9 @@
   
   $out = $form_start ? "$form_start\n" : '';
 
-  // Check if a previous save was done so we can delete it upon the next save
-  if (!empty($_POST['_draft_newsaveid']))
-    $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => $_POST['_draft_newsaveid']));
-  else if (strlen($DRAFT_MESSAGE['headers']->messageID) > 6)
-    $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => str_replace(array('<','>'),"",$DRAFT_MESSAGE['headers']->messageID) ));
+  $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => str_replace(array('<','>'),"",$DRAFT_MESSAGE['headers']->messageID) ));
+  $out .= $saveid->show();
 
-  if ($saveid) 
-    $out .= $saveid->show();
-
-  $newsaveid = new hiddenfield(array('name' => '_draft_newsaveid', 'value' => sprintf('%s@%s', md5(uniqid('rcmail'.rand(),true)), $_SESSION['imap_host']) ));
-  $out .= $newsaveid->show();
- 
   $drafttoggle = new hiddenfield(array('name' => '_draft', 'value' => 'yes'));
   $out .= $drafttoggle->show();
 
@@ -417,7 +410,9 @@
 
     rcube_add_label('checking');
     }
-  
+ 
+  $out .= '<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
+ 
   return $out;
   }
 
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index 33c1a40..d0e0f78 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -65,12 +65,10 @@
 if (strlen($_POST['_draft_saveid']) > 3)
   $olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST);
 
-if (strlen($_POST['_draft_newsaveid']) > 3)
-  $newdraftmessageid = get_input_value('_draft_newsaveid', RCUBE_INPUT_POST);
+$message_id = sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), $_SESSION['imap_host']);
 
 $savedraft = !empty($_POST['_draft']) ? TRUE : FALSE;
 
- 
 /****** check submission and compose message ********/
 
 
@@ -96,20 +94,11 @@
 $to_address_arr = $IMAP->decode_address_list($mailto);
 $identity_arr = rcmail_get_identity(get_input_value('_from', RCUBE_INPUT_POST));
 
-
 $from = $identity_arr['mailto'];
 $first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto;
 
-
-// try the message-id submitted by the compose form
-if ($newdraftmessageid)
-  $message_id = sprintf('<%s>',$newdraftmessageid);
-else
-  $message_id = sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), $_SESSION['imap_host']);
-  
 if (empty($identity_arr['string']))
   $identity_arr['string'] = $from;
-
 
 // compose headers array
 $headers = array('Date' => date('D, j M Y G:i:s O'),
@@ -321,10 +310,19 @@
 
   // raise error if saving failed
   if (!$saved)
+    {
+    // clear the "saving message" busy status, and display failure
+    $errorout = sprintf("parent.%s.display_message('%s', '%s');",
+                       $JS_OBJECT_NAME,
+                       addslashes(rep_specialchars_output(rcube_label(array('name' => 'errorsaving', 'vars' => NULL)))),
+                       'error');
+    print "<html><script type=\"text/javascript\">$errorout</script></html>";
     raise_error(array('code' => 800,
                       'type' => 'imap',
                       'file' => __FILE__,
                       'message' => "Could not save message in $CONFIG[$store_target]"), TRUE, FALSE);
+    exit;
+    }
 
   if ($olddraftmessageid)
     {
@@ -332,6 +330,7 @@
     $a_deleteid = $IMAP->search($CONFIG['drafts_mbox'],'HEADER Message-ID',$olddraftmessageid);
     $deleted = $IMAP->delete_message($IMAP->get_uid($a_deleteid[0],$CONFIG['drafts_mbox']),$CONFIG['drafts_mbox']);
 
+    // raise error if deletion of old draft failed
     if (!$deleted)
       raise_error(array('code' => 800,
                         'type' => 'imap',
@@ -342,8 +341,25 @@
 
 if ($savedraft)
   {
-  show_message('messagesaved', 'confirmation');
-  rcmail_overwrite_action('compose');
+  // clear the "saving message" busy status, and display success
+  $frameout = sprintf("parent.%s.display_message('%s', '%s');",
+                     $JS_OBJECT_NAME,
+                     addslashes(rep_specialchars_output(rcube_label(array('name' => 'messagesaved', 'vars' => NULL)))),
+                     'confirmation');
+
+  // update "_draft_saveid" on the page, which is used to delete a previous draft
+  $frameout .= 'var foundid = parent.rcube_find_object("_draft_saveid",parent.document);';
+  $frameout .= sprintf('foundid.value = "%s";',str_replace(array('<','>'),"",$message_id));
+
+  // update the "cmp_hash" to prevent "Unsaved changes" warning
+  $frameout .= sprintf("parent.%s.cmp_hash = parent.%s.compose_field_hash();",$JS_OBJECT_NAME,$JS_OBJECT_NAME);
+  
+  // start the auto-save timer again
+  $frameout .= sprintf("parent.%s.auto_save_start();",$JS_OBJECT_NAME);
+
+  // send html page with JS calls as response
+  print "<html><script type=\"text/javascript\">$frameout</script></html>";
+  exit;
   }
 else
   {
diff --git a/program/steps/mail/upload.inc b/program/steps/mail/upload.inc
index 39164bb..0af8eed 100644
--- a/program/steps/mail/upload.inc
+++ b/program/steps/mail/upload.inc
@@ -60,6 +60,7 @@
 {
 $response
 parent.$JS_OBJECT_NAME.show_attachment_form(false);
+parent.$JS_OBJECT_NAME.auto_save_start();
 }
 </script>
 </html>
diff --git a/skins/default/templates/compose.html b/skins/default/templates/compose.html
index db5fe99..ed44bb5 100644
--- a/skins/default/templates/compose.html
+++ b/skins/default/templates/compose.html
@@ -20,13 +20,6 @@
   return false;
   }
 
-function rcmail_auto_save()
-  {
-  rcmail.command('savedraft','',this);
-  self.setTimeout('rcmail_auto_save()',300000);
-  }
-self.setTimeout('rcmail_auto_save()',300000);
- 
 //-->
 </script>
 </head>

--
Gitblit v1.9.1