From 3d35312cc4badb64a2c26db8e80c1a01f2d1c12b Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 12 Apr 2008 13:37:22 -0400
Subject: [PATCH] - Fixed and optimized 'read_when_deleted': mark as read on server side   in one action when marking as deleted, fixed js bugs when deleting   from message preview page    

---
 CHANGELOG                   |    6 ++
 program/include/main.inc    |    2 
 program/steps/mail/mark.inc |    9 +++
 program/js/app.js           |  137 ++++++++++++++++++++++++++++-----------------
 4 files changed, 101 insertions(+), 53 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 00502ab..f44f16e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,12 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2008/04/12 (alec)
+----------
+- Fixed and optimized 'read_when_deleted': mark as read on server side
+  in one action when marking as deleted, fixed js bugs when deleting
+  from message preview page 
+
 2008/04/12 (thomasb)
 ----------
 - Changed codebase to PHP5 with autoloader
diff --git a/program/include/main.inc b/program/include/main.inc
index 6d548d8..09a53f0 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -465,7 +465,7 @@
   $OUTPUT = new rcube_template($CONFIG, $GLOBALS['_task']);
   $OUTPUT->set_env('comm_path', $GLOBALS['COMM_PATH']);
 
-  foreach (array('read_when_deleted', 'flag_for_deletion') as $js_config_var)
+  foreach (array('flag_for_deletion') as $js_config_var)
     $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]);
 
   if (!empty($GLOBALS['_framed']))
diff --git a/program/js/app.js b/program/js/app.js
index a62d8d8..3ed6817 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -1407,7 +1407,7 @@
   this.delete_messages = function()
     {
     var selection = this.message_list ? this.message_list.get_selection() : new Array();
-    
+
     // exit if no mailbox specified or if selection is empty
     if (!this.env.uid && !selection.length)
         return;
@@ -1458,8 +1458,9 @@
   this._with_selected_messages = function(action, lock, add_url)
     {
     var a_uids = new Array();
+
     if (this.env.uid)
-      a_uids[a_uids.length] = this.env.uid;
+      a_uids[0] = this.env.uid;
     else
       {
       var selection = this.message_list.get_selection();
@@ -1468,10 +1469,11 @@
         {
         id = selection[n];
         a_uids[a_uids.length] = id;
+
         this.message_list.remove_row(id, (n == selection.length-1));
         }
       }
-      
+    
     // also send search request to get the right messages 
     if (this.env.search_request) 
       add_url += '&_search='+this.env.search_request;
@@ -1487,20 +1489,23 @@
     var a_uids = new Array();
     var r_uids = new Array();
     var selection = this.message_list ? this.message_list.get_selection() : new Array();
-    
+
     if (uid)
       a_uids[0] = uid;
     else if (this.env.uid)
       a_uids[0] = this.env.uid;
     else if (this.message_list)
       {
-      for (var id, n=0; n<selection.length; n++)
+      for (var n=0; n<selection.length; n++)
         {
     	  a_uids[a_uids.length] = selection[n];
         }
       }
 
-    for (var id, n=0; n<a_uids.length; n++)
+    if (!this.message_list)
+      r_uids = a_uids;
+    else
+      for (var id, n=0; n<a_uids.length; n++)
       {
         id = a_uids[n];
         if ((flag=='read' && this.message_list.rows[id].unread) 
@@ -1511,11 +1516,11 @@
 	    r_uids[r_uids.length] = id;
 	  }
       }
-    
+
     // nothing to do
     if (!r_uids.length)
       return;
-      
+
     switch (flag)
       {
         case 'read':
@@ -1581,11 +1586,11 @@
         parent.rcmail.set_classname(rows[uid].obj, 'unread', false);
 
         if (rows[uid].replied && parent.rcmail.env.repliedicon)
-          icn_src = parent.rcmail.env.repliedicon;
+    	  icn_src = parent.rcmail.env.repliedicon;
         else if (parent.rcmail.env.messageicon)
           icn_src = parent.rcmail.env.messageicon;
       
-        if (rows[uid].icon && icn_src)
+	if (rows[uid].icon && icn_src)
           rows[uid].icon.src = icn_src;
       }
   }
@@ -1594,17 +1599,11 @@
   // mark all message rows as deleted/undeleted
   this.toggle_delete_status = function(a_uids)
   {
-    if (this.env.read_when_deleted)
-      this.mark_message('read',a_uids);
-
-    // if deleting message from "view message" don't bother with delete icon
-    if (this.env.action == "show")
-      return false;
-
-    var rows = this.message_list.rows;
+    var rows = this.message_list ? this.message_list.rows : new Array();
+    
     if (a_uids.length==1)
     {
-      if (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0)
+      if (!rows.length || (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0))
         this.flag_as_deleted(a_uids);
       else
         this.flag_as_undeleted(a_uids);
@@ -1636,12 +1635,8 @@
 
   this.flag_as_undeleted = function(a_uids)
   {
-    // if deleting message from "view message" don't bother with delete icon
-    if (this.env.action == "show")
-      return false;
-
     var icn_src;
-    var rows = this.message_list.rows;
+    var rows = this.message_list ? this.message_list.rows : new Array();
       
     for (var i=0; i<a_uids.length; i++)
     {
@@ -1660,6 +1655,7 @@
           icn_src = this.env.repliedicon;
         else if (this.env.messageicon)
           icn_src = this.env.messageicon;
+
         if (rows[uid].icon && icn_src)
           rows[uid].icon.src = icn_src;
       }
@@ -1672,30 +1668,66 @@
   
   this.flag_as_deleted = function(a_uids)
   {
-    // if deleting message from "view message" don't bother with delete icon
-    if (this.env.action == "show")
-      return false;
-
-    var rows = this.message_list.rows;
+    var add_url = '';
+    var r_uids = new Array();
+    var rows = this.message_list ? this.message_list.rows : new Array();
+    
     for (var i=0; i<a_uids.length; i++)
-    {
+      {
       uid = a_uids[i];
-      if (rows[uid]) {
+      if (rows[uid])
+        {
         rows[uid].deleted = true;
         
-        if (rows[uid].classname.indexOf('deleted')<0) {
+        if (rows[uid].classname.indexOf('deleted')<0)
+	  {
           rows[uid].classname += ' deleted';
           this.set_classname(rows[uid].obj, 'deleted', true);
-        }
-        if (rows[uid].icon && this.env.deletedicon)
+          }
+        
+	if (rows[uid].icon && this.env.deletedicon)
           rows[uid].icon.src = this.env.deletedicon;
-      }
-    }
 
-    this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete');
+	if (rows[uid].unread)
+	  r_uids[r_uids.length] = uid;
+        }
+      }
+
+    if (r_uids.length)
+      add_url = '&_ruid='+r_uids.join(',');
+
+    this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete'+add_url);
     return true;  
   };
 
+
+  // flag as read without mark request (called from backend)
+  // argument should be a coma-separated list of uids
+  this.flag_deleted_as_read = function(uids)
+  {
+    var icn_src;
+    var rows = this.message_list ? this.message_list.rows : new Array();
+    var str = String(uids);
+    var a_uids = new Array();
+
+    a_uids = str.split(',');
+
+    for (var uid, i=0; i<a_uids.length; i++)
+      {
+      uid = a_uids[i];
+      if (rows[uid])
+        {
+        rows[uid].unread = false;
+	rows[uid].read = true;
+        
+        rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
+        this.set_classname(rows[uid].obj, 'unread', false);
+
+        if (rows[uid].icon)
+          rows[uid].icon.src = this.env.deletedicon;
+        }
+      }
+  };
 
   /*********************************************************/
   /*********           login form methods          *********/
@@ -3502,27 +3534,28 @@
           this.message_list.init();
 
       case 'purge':
-      case 'expunge':
-        if (!this.env.messagecount)
-        {
-          // clear preview pane content
-          if (this.env.contentframe)
-            this.show_contentframe(false);
-          // disable commands useless when mailbox is empty
-          this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'mark', 'viewsource',
-          'print', 'load-attachment', 'purge', 'expunge', 'select-all', 'select-none', 'sort', false);
-        }
+      case 'expunge':      
+	if (!this.env.messagecount)
+    	  {
+	    // clear preview pane content
+	    if (this.env.contentframe)
+	      this.show_contentframe(false);
+	    // disable commands useless when mailbox is empty
+	    this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'mark', 'viewsource',
+	      'print', 'load-attachment', 'purge', 'expunge', 'select-all', 'select-none', 'sort', false);
+	  }
 
-      break;
+	break;
 
       case 'list':
-        this.msglist_select(this.message_list);
+	this.msglist_select(this.message_list);
 
       case 'check-recent':
       case 'getunread':
-        this.enable_command('show', 'expunge', 'select-all', 'select-none', 'sort', (this.env.messagecount > 0));
-        this.enable_command('purge', (this.env.messagecount && (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox)));
-        break;
+	this.enable_command('show', 'expunge', 'select-all', 'select-none', 'sort', (this.env.messagecount > 0));
+	this.enable_command('purge', (this.env.messagecount && (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox)));
+
+	break;
 
       }
 
diff --git a/program/steps/mail/mark.inc b/program/steps/mail/mark.inc
index 53194b5..830f1fe 100644
--- a/program/steps/mail/mark.inc
+++ b/program/steps/mail/mark.inc
@@ -29,6 +29,15 @@
   $flag = $a_flags_map[$flag] ? $a_flags_map[$flag] : strtoupper($flag);
   $marked = $IMAP->set_flag($uids, $flag);
 
+  if($flag == 'DELETED' && $CONFIG['read_when_deleted'] && !empty($_POST['_ruid']))
+    {
+    $uids = get_input_value('_ruid', RCUBE_INPUT_POST);
+    $read = $IMAP->set_flag($uids, 'SEEN');
+    
+    if ($read != -1)
+      $OUTPUT->command('flag_deleted_as_read', $uids);
+    }
+
   if ($marked != -1)
   {
     $mbox_name = $IMAP->get_mailbox_name();

--
Gitblit v1.9.1