From 41b43ba00e9110e6f415014382e11af7fe381fcb Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 28 Nov 2008 14:01:35 -0500
Subject: [PATCH] Don't set client busy when automatically checking for new messages (#1485276)

---
 program/js/app.js |  352 ++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 204 insertions(+), 148 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index 7a8b5e9..f301e55 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -202,6 +202,8 @@
             }
           if (this.env.drafts_mailbox)
             this.enable_command('savedraft', true);
+            
+          document.onmouseup = function(e){ return p.doc_mouse_up(e); };
           }
 
         if (this.env.messagecount)
@@ -266,7 +268,7 @@
 
         this.set_page_buttons();
         
-        if (this.env.address_sources && !this.env.address_sources[this.env.source].readonly)
+        if (this.env.address_sources && this.env.address_sources[this.env.source] && !this.env.address_sources[this.env.source].readonly)
           this.enable_command('add', true);
         
         if (this.env.cid)
@@ -288,8 +290,8 @@
         this.enable_command('preferences', 'identities', 'save', 'folders', true);
         
         if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity') {
-          this.enable_command('add', 'delete', this.env.multiple_identities);
-          this.enable_command('edit', true);
+          this.enable_command('add', this.env.identities_level < 2);
+          this.enable_command('delete', 'edit', true);
         }
 
         if (this.env.action=='edit-identity' || this.env.action=='add-identity')
@@ -364,9 +366,9 @@
   this.start_keepalive = function()
     {
     if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.mailboxlist)
-      this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
+      this._int = setInterval(function(){ ref.check_for_recent(false); }, this.env.keep_alive * 1000);
     else if (this.env.keep_alive && !this.env.framed && this.task!='login')
-      this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);    
+      this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
     }
 
   this.init_message_row = function(row)
@@ -423,6 +425,7 @@
     var input_replyto = rcube_find_object('_replyto');
     var input_subject = rcube_find_object('_subject');
     var input_message = rcube_find_object('_message');
+    var draftid = rcube_find_object('_draft_saveid');
 
     // init live search events
     if (input_to)
@@ -433,8 +436,12 @@
       this.init_address_input_events(input_bcc);
 
     // add signature according to selected identity
-    if (input_from && input_from.type=='select-one')
+    if (input_from && input_from.type=='select-one' && (!draftid || draftid.value=='')
+	// if we have HTML editor, signature is added in callback
+	&& rcube_find_object('_is_html').value != '1')
+      {
       this.change_identity(input_from);
+      }
 
     if (input_to && input_to.value=='')
       input_to.focus();
@@ -453,20 +460,13 @@
   this.init_address_input_events = function(obj)
     {
     var handler = function(e){ return ref.ksearch_keypress(e,this); };
-    var handler2 = function(e){ return ref.ksearch_blur(e,this); };
     
     if (obj.addEventListener)
-    {
       obj.addEventListener(bw.safari ? 'keydown' : 'keypress', handler, false);
-      obj.addEventListener('blur', handler2, false);
-    }
     else
-    {
       obj.onkeydown = handler;
-      obj.onblur = handler2;
-    }
 
-    obj.setAttribute('autocomplete', 'off');       
+    obj.setAttribute('autocomplete', 'off');
     };
 
 
@@ -560,7 +560,7 @@
         var sort_col = a_sort[0];
         var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
         var header;
-        
+
         // no sort order specified: toggle
         if (sort_order==null)
           {
@@ -574,9 +574,9 @@
           break;
 
         // set table header class
-        if (header = document.getElementById('rcmHead'+this.env.sort_col))
+        if (header = document.getElementById('rcm'+this.env.sort_col))
           this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
-        if (header = document.getElementById('rcmHead'+sort_col))
+        if (header = document.getElementById('rcm'+sort_col))
           this.set_classname(header, 'sorted'+sort_order, true);
 
         // save new sort properties
@@ -815,28 +815,35 @@
         break;
       
       case 'checkmail':
-        this.check_for_recent();
+        this.check_for_recent(true);
         break;
       
       case 'compose':
         var url = this.env.comm_path+'&_action=compose';
        
-        if (this.task=='mail' && this.env.mailbox==this.env.drafts_mailbox)
+        if (this.task=='mail')
+        {
+          url += '&_mbox='+urlencode(this.env.mailbox);
+          
+          if (this.env.mailbox==this.env.drafts_mailbox)
           {
-          var uid;
-          if (uid = this.get_single_uid())
-            url += '&_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox);
+            var uid;
+            if (uid = this.get_single_uid())
+              url += '&_draft_uid='+uid;
           }
+          else if (props)
+             url += '&_to='+urlencode(props);
+        }
         // modify url if we're in addressbook
         else if (this.task=='addressbook')
           {
           // switch to mail compose step directly
           if (props && props.indexOf('@') > 0)
-          {
+            {
             url = this.get_task_url('mail', url);
             this.redirect(url + '&_to='+urlencode(props));
             break;
-          }
+            }
           
           // use contact_id passed as command parameter
           var a_cids = new Array();
@@ -855,8 +862,6 @@
 
           break;
           }
-        else if (props)
-           url += '&_to='+urlencode(props);
 
         // don't know if this is necessary...
         url = url.replace(/&_framed=1/, "");
@@ -1163,6 +1168,9 @@
       this.contact_list.blur();
       model = this.env.address_sources;
     }
+    else if (this.ksearch_value) {
+      this.ksearch_blur();
+    }
     
     // handle mouse release when dragging
     if (this.drag_active && model) {
@@ -1404,6 +1412,24 @@
       }
     };
 
+  // list messages of a specific mailbox using filter
+  this.filter_mailbox = function(filter)
+    {
+      var search;
+      if (this.gui_objects.qsearchbox)
+        search = this.gui_objects.qsearchbox.value;
+      
+      this.message_list.clear();
+
+      // reset vars
+      this.env.current_page = 1;
+      this.set_busy(true, 'searching');
+      this.http_request('search', '_filter='+filter
+    	    + (search ? '&_q='+urlencode(search) : '')
+    	    + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : ''), true);
+    }
+
+
   // list messages of a specific mailbox
   this.list_mailbox = function(mbox, page, sort)
     {
@@ -1575,6 +1601,8 @@
       rows[uid].forwarded = status;
     else if (flag == 'flagged')
       rows[uid].flagged = status;
+
+    this.env.messages[uid] = rows[uid];
     }
 
   // set message row status, class and icon
@@ -1607,6 +1635,17 @@
       {
       rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
       this.set_classname(rows[uid].obj, 'deleted', false);
+      }
+
+    if (rows[uid].flagged && rows[uid].classname.indexOf('flagged')<0)
+      {
+      rows[uid].classname += ' flagged';
+      this.set_classname(rows[uid].obj, 'flagged', true);
+      }
+    else if (!rows[uid].flagged && rows[uid].classname.indexOf('flagged')>=0)
+      {
+      rows[uid].classname = rows[uid].classname.replace(/\s*flagged/, '');
+      this.set_classname(rows[uid].obj, 'flagged', false);
       }
 
     this.set_message_icon(uid);
@@ -1708,14 +1747,14 @@
           this.message_list.remove_row(id, (n == selection.length-1));
         else
         {
-          rows[id].deleted = true;
+          this.set_message_status(id, 'deleted', true);
           if (this.env.read_when_deleted)
-    	    rows[id].unread = false;
+    	    this.set_message_status(id, 'unread', false);
 	  this.set_message(id);
         }
       }
     }
-    
+
     // also send search request to get the right messages 
     if (this.env.search_request) 
       add_url += '&_search='+this.env.search_request;
@@ -1885,11 +1924,7 @@
       {
       uid = a_uids[i];
       if (rows[uid])
-        {
-        rows[uid].unread = false;
-        rows[uid].read = true;
-	this.set_message(uid);
-        }
+        this.set_message(uid, 'unread', false);
       }
   };
   
@@ -1974,23 +2009,31 @@
       return false;
       }
 
+    // Apply spellcheck changes if spell checker is active
+    this.stop_spellchecking();
+
     return true;
     };
 
+  this.stop_spellchecking = function()
+    {
+    if (this.env.spellcheck && !this.spellcheck_ready) {
+      exec_event(this.env.spellcheck.check_link, 'click');
+      this.set_spellcheck_state('ready');
+      }
+    };
+
   this.display_spellcheck_controls = function(vis)
-  {
+    {
     if (this.env.spellcheck) {
       // stop spellchecking process
-      if (!vis && !this.spellcheck_ready) 
-        {
-	exec_event(this.env.spellcheck.check_link, 'click');
-	this.set_spellcheck_state('ready');
-	}
+      if (!vis)
+	this.stop_spellchecking();
 			      
       this.env.spellcheck.check_link.style.visibility = vis ? 'visible' : 'hidden';
       this.env.spellcheck.switch_lan_pic.style.visibility = vis ? 'visible' : 'hidden';
-    }
-  };
+      }
+    };
 
   this.set_spellcheck_state = function(s)
     {
@@ -2079,6 +2122,8 @@
           message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
         }
 
+      message = message.replace(/[\r\n]+$/, '');
+      
       // add the new signature string
       if (this.env.signatures && this.env.signatures[id])
         {
@@ -2089,7 +2134,7 @@
           }
         if (sig.indexOf('-- ')!=0)
           sig = '-- \n'+sig;
-        message += '\n'+sig;
+        message += '\n\n'+sig;
         }
       }
     else
@@ -2112,7 +2157,7 @@
 
 	if (this.env.signatures[id])
 	  {
-	  newsig = String(this.env.signatures[id]['text']).replace("\n", "\r\n");
+	  newsig = this.env.signatures[id]['text'];
 	  htmlsig = this.env.signatures[id]['is_html'];
 	  }
 
@@ -2254,7 +2299,7 @@
     };
 
   // send remote request to search mail or contacts
-  this.qsearch = function(value)
+  this.qsearch = function(value, addurl)
     {
     if (value != '')
       {
@@ -2263,12 +2308,18 @@
       else if (this.contact_list) {
         this.contact_list.clear(true);
         this.show_contentframe(false);
-      }
+        }
+
+      if (this.gui_objects.search_filter)
+      addurl = '&_filter=' + this.gui_objects.search_filter.value;
 
       // reset vars
       this.env.current_page = 1;
       this.set_busy(true, 'searching');
-      this.http_request('search', '_q='+urlencode(value)+(this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : '')+(this.env.source ? '&_source='+urlencode(this.env.source) : ''), true);
+      this.http_request('search', '_q='+urlencode(value)
+        + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : '')
+        + (this.env.source ? '&_source='+urlencode(this.env.source) : '')
+        + (addurl ? addurl : ''), true);
       }
     return true;
     };
@@ -2296,10 +2347,7 @@
 
   // handler for keyboard events on address-fields
   this.ksearch_keypress = function(e, obj)
-    {
-    if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
-      return true;
-
+  {
     if (this.ksearch_timer)
       clearTimeout(this.ksearch_timer);
 
@@ -2315,24 +2363,13 @@
           break;
           
         var dir = key==38 ? 1 : 0;
-        var next;
         
         highlight = document.getElementById('rcmksearchSelected');
         if (!highlight)
           highlight = this.ksearch_pane.ul.firstChild;
         
-        if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
-          {
-          highlight.removeAttribute('id');
-          this.set_classname(highlight, 'selected', false);
-          }
-
-        if (next)
-          {
-          next.setAttribute('id', 'rcmksearchSelected');
-          this.set_classname(next, 'selected', true);
-          this.ksearch_selected = next._rcm_id;
-          }
+        if (highlight)
+          this.ksearch_select(dir ? highlight.previousSibling : highlight.nextSibling);
 
         return rcube_event.cancel(e);
 
@@ -2340,7 +2377,7 @@
         if(mod == SHIFT_KEY)
           break;
 
-      case 13:  // enter     
+      case 13:  // enter
         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
           break;
 
@@ -2361,7 +2398,22 @@
     this.ksearch_input = obj;
     
     return true;
-    };
+  };
+  
+  this.ksearch_select = function(node)
+  {
+    var current = document.getElementById('rcmksearchSelected');
+    if (current && node) {
+      current.removeAttribute('id');
+      this.set_classname(current, 'selected', false);
+    }
+
+    if (node) {
+      node.setAttribute('id', 'rcmksearchSelected');
+      this.set_classname(node, 'selected', true);
+      this.ksearch_selected = node._rcm_id;
+    }
+  };
 
   this.insert_recipient = function(id)
   {
@@ -2387,10 +2439,13 @@
 
   // address search processor
   this.ksearch_get_results = function()
-    {
+  {
     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
-    if (inp_value===null)
+    if (inp_value === null)
       return;
+      
+    if (this.ksearch_pane && this.ksearch_pane.visible)
+      this.ksearch_pane.show(0);
 
     // get string from current cursor pos to last comma
     var cpos = this.get_caret_pos(this.ksearch_input);
@@ -2400,45 +2455,45 @@
     // trim query string
     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
 
-    if (!q.length || q==this.ksearch_value)
-      {
-      if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
-        this.ksearch_pane.show(0);
-
-      return;
-      }
+    // Don't (re-)search if string is empty or if the last results are still active
+    if (!q.length || q == this.ksearch_value)
+        return;
 
     this.ksearch_value = q;
     
-    // start searching the contact list
-    var a_results = new Array();
-    var a_result_ids = new Array();
-    var c=0;
-    for (var i=0; i<this.env.contacts.length; i++)
-      {
-      if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
-        {
-        a_results[c] = this.env.contacts[i];
-        a_result_ids[c++] = i;
-        
-        if (c==15)  // limit search results
-          break;
-        }
-      }
+    this.display_message('searching', 'loading', true);
+    this.http_post('autocomplete', '_search='+q);
+  };
 
+  this.ksearch_query_results = function(results)
+  {
+    this.hide_message();
+    this.env.contacts = results ? results : [];
+
+    var result_ids = new Array();
+    var c=0;
+    for (var i=0; i < this.env.contacts.length; i++) {
+      result_ids[c++] = i;
+      if (c == 15)  // limit search results
+        break;
+    }
+    
+    this.ksearch_display_results(this.env.contacts, result_ids, c);
+  };
+
+  this.ksearch_display_results = function (a_results, a_result_ids, c)
+  {
     // display search results
-    if (c && a_results.length)
-      {
+    if (c && a_results.length) {
       var p, ul, li;
       
       // create results pane if not present
-      if (!this.ksearch_pane)
-        {
+      if (!this.ksearch_pane) {
         ul = document.createElement('UL');
         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
         this.ksearch_pane.elm.appendChild(ul);
         this.ksearch_pane.ul = ul;
-        }
+      }
       else
         ul = this.ksearch_pane.ul;
 
@@ -2446,51 +2501,58 @@
       ul.innerHTML = '';
             
       // add each result line to list
-      for (i=0; i<a_results.length; i++)
-        {
+      for (i=0; i<a_results.length; i++) {
         li = document.createElement('LI');
-        li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
+        li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;').replace(new RegExp('('+this.ksearch_value+')', 'ig'), '<b>$1</b>');
+        li.onmouseover = function(){ ref.ksearch_select(this); };
+        li.onmouseup = function(){ ref.ksearch_click(this) };
         li._rcm_id = a_result_ids[i];
         ul.appendChild(li);
-        }
+      }
 
       // check if last selected item is still in result list
-      if (this.ksearch_selected!==null)
-        {
+      if (this.ksearch_selected !== null) {
         p = find_in_array(this.ksearch_selected, a_result_ids);
-        if (p>=0 && ul.childNodes)
-          {
+        if (p >= 0 && ul.childNodes) {
           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
           this.set_classname(ul.childNodes[p], 'selected', true);
-          }
+        }
         else
           this.ksearch_selected = null;
-        }
+      }
       
       // if no item selected, select the first one
-      if (this.ksearch_selected===null)
-        {
+      if (this.ksearch_selected === null) {
         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
         this.set_classname(ul.firstChild, 'selected', true);
         this.ksearch_selected = a_result_ids[0];
-        }
+      }
 
       // move the results pane right under the input box and make it visible
       var pos = rcube_get_object_pos(this.ksearch_input);
       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
-      this.ksearch_pane.show(1); 
-      }
+      this.ksearch_pane.show(1);
+    }
     // hide results pane
     else
       this.ksearch_hide();
-    };
+  };
+  
+  this.ksearch_click = function(node)
+  {
+    this.insert_recipient(node._rcm_id);
+    this.ksearch_hide();
+    
+    if (ref.ksearch_input)
+      this.ksearch_input.focus();
+  };
 
-  this.ksearch_blur = function(e, obj)
+  this.ksearch_blur = function()
     {
     if (this.ksearch_timer)
       clearTimeout(this.ksearch_timer);
 
-    this.ksearch_value = '';      
+    this.ksearch_value = '';
     this.ksearch_input = null;
     
     this.ksearch_hide();
@@ -2502,7 +2564,7 @@
     this.ksearch_selected = null;
     
     if (this.ksearch_pane)
-      this.ksearch_pane.show(0);    
+      this.ksearch_pane.show(0);
     };
 
 
@@ -2810,7 +2872,9 @@
       var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
       var basename = this.env.folder.replace(reg, '');
       var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
-      this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname));
+
+      this.set_busy(true, 'foldermoving');
+      this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname), true);
       }
     this.drag_active = false;
     this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
@@ -3372,7 +3436,7 @@
         else
           cell.innerHTML = this.get_label(this.coltypes[n]);
 
-        cell.id = 'rcmHead'+col;
+        cell.id = 'rcm'+col;
         }
       else if (col == 'subject' && this.message_list)
         this.message_list.subject_col = n+1;
@@ -3401,7 +3465,11 @@
     
     var row = document.createElement('TR');
     row.id = 'rcmrow'+uid;
-    row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
+    row.className = 'message'
+	+ (even ? ' even' : ' odd')
+        + (flags.unread ? ' unread' : '')
+	+ (flags.deleted ? ' deleted' : '')
+	+ (flags.flagged ? ' flagged' : '');		    
 
     if (this.message_list.in_selection(uid))
       row.className += ' selected';
@@ -3439,17 +3507,14 @@
           col.innerHTML = '<img src="'+this.env.flaggedicon+'" alt="" />';
         else if(this.env.unflaggedicon)
           col.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
-	}
+      }
+      else if (c=='attachment')
+        col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '';
       else
         col.innerHTML = cols[c];
 
       row.appendChild(col);
       }
-
-    col = document.createElement('TD');
-    col.className = 'icon';
-    col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '';
-    row.appendChild(col);
 
     this.message_list.insert_row(row, attop);
 
@@ -3551,6 +3616,16 @@
       }
     };
 
+  // notifies that a new message(s) has hit the mailbox
+  this.new_message_focus = function()
+    {
+    // focus main window
+    if (this.env.framed && window.parent)
+      window.parent.focus();
+    else
+      window.focus();
+    }
+
   // add row to contacts list
   this.add_contact_row = function(cid, cols, select)
     {
@@ -3579,27 +3654,6 @@
     
     this.contact_list.insert_row(row);
     this.enable_command('export', (this.contact_list.rowcount > 0));
-    };
-
-  this.toggle_editor = function(checkbox, textAreaId)
-    {
-    var ischecked = checkbox.checked;
-    var composeElement = document.getElementById(textAreaId);
-    
-    if (ischecked)
-      {
-      var existingPlainText = composeElement.value;
-      var htmlText = "<pre>" + existingPlainText + "</pre>";
-      composeElement.value = htmlText;
-      tinyMCE.execCommand('mceAddControl', true, textAreaId);
-      }
-    else
-      {
-      var thisMCE = tinyMCE.get(textAreaId);
-      var existingHtml = thisMCE.getContent();
-      this.html2plain(existingHtml, textAreaId);
-      tinyMCE.execCommand('mceRemoveControl', true, textAreaId);
-      }
     };
 
   this.toggle_prefer_html = function(checkbox)
@@ -3862,12 +3916,14 @@
     };
 
   // send periodic request to check for recent messages
-  this.check_for_recent = function()
+  this.check_for_recent = function(setbusy)
     {
     if (this.busy)
       return;
 
-    this.set_busy(true, 'checkingmail');
+    if (setbusy)
+      this.set_busy(true, 'checkingmail');
+
     this.http_request('check-recent', (this.env.search_request ? '_search='+this.env.search_request+'&' : '') + '_t='+(new Date().getTime()), true);
     };
 
@@ -4099,10 +4155,10 @@
   
   }  // end class rcube_http_request
 
-
 // helper function to call the init method with a delay
 function call_init(o)
   {
     window.setTimeout('if (window[\''+o+'\'] && window[\''+o+'\'].init) { '+o+'.init(); }',
-                      bw.win ? 500 : 200);
+        bw.win ? 500 : 200);
   }
+

--
Gitblit v1.9.1