Index: branches/2.3/expressoMail1_2/js/local_messages.js
===================================================================
--- branches/2.3/expressoMail1_2/js/local_messages.js	(revision 6340)
+++ branches/2.3/expressoMail1_2/js/local_messages.js	(revision 6702)
@@ -143,7 +143,10 @@
 	if(this.localServer == null)
 	    this.localServer = google.gears.factory.create('beta.localserver');
-	if(this.store == null)
+                if(this.store==null){
+		    this.store = this.localServer.openStore('store-expresso');
+		    if(this.store==null)
 	    this.store = this.localServer.createStore('test-store');
     }
+}
 }
 
@@ -153,18 +156,33 @@
 	this.create_objects();
 		
-    var db_in_other_use = true;
-    var start_trying = new Date().getTime();
-    while (db_in_other_use) {
+		var db_is_opened = true;
+		while (db_is_opened) {
 	try {
-	    this.dbGears.open('database-test');
-	    db_in_other_use = false;
+				this.dbGears.open('database-expresso');
+				db_is_opened = true;
 	}
 	catch (ex) {
-				if(new Date().getTime()-start_trying>10000) { //too much time trying, throw an exception
-					throw ex;
-	}
-    }
+				db_is_opened=false;
+	}
+    }
+
+		try{
+		    var rs = this.dbGears.execute('select count(*) from mail');
+		    rs.close();
+		}catch(ex){
+			this.dbGears.remove();
+			var db_is_opened = true;
+			while (db_is_opened) {
+			    try{
+				this.dbGears.open('database-test');
+				db_is_opened = true;
 		}
+			    catch(ex){
+				db_is_opened=false;
+			    }
+			}
+		    }
 			
+
     //		this.dbGears.open('database-test');
     this.dbGears.execute('create table if not exists folder (folder text,uid_usuario int,unique(folder,uid_usuario))');
@@ -570,6 +588,12 @@
 local_messages.prototype.getInputFileFromAnexo = function (element,url) { 
     this.init_local_messages();
-    fileSubmitter = this.store.createFileSubmitter();
+		try{
+		    var fileSubmitter = this.store.createFileSubmitter();
     fileSubmitter.setFileInputElement(element,url);
+		}catch(e){
+		    this.store = this.localServer.openStore('test-store');
+		    var fileSubmitter = this.store.createFileSubmitter();
+		    fileSubmitter.setFileInputElement(element,url);
+		}
     this.finalize();
 }
@@ -614,5 +638,5 @@
     }
     Element('chk_box_select_all_messages').checked = false;
-	
+		
 }
 	
@@ -797,6 +821,13 @@
 	var temp = new Array();
         if (rs.field(0).indexOf('/') == 0){
-               var rs1 = this.dbGears.execute('update folder set folder=? where rowid=?',[rs.field(0).slice(1),rs.field(1)]);
-               temp[0] = rs.field(0).slice(1);
+            try{   
+                   var newName = rs.field(0).slice(1);
+                   var rs1 = this.dbGears.execute('update folder set folder=? where rowid=?',[newName,rs.field(1)]);
+                }catch(e){
+                    newName = newName+"_"+rs.field(1)
+                    var rs1 = this.dbGears.execute('update folder set folder=? where rowid=?',[newName,rs.field(1)]);
+                }
+               
+               temp[0] = newName;
                rs1.close();
        }else{
@@ -1230,7 +1261,7 @@
 
 //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Dessarquiva msgs locais pegando o codigo fonte das mesmas e mandando via POST para o servidor
-//para que elas sejam inseridas no imap pela funÃ§Ã£o  imap_functions.unarchive_mail.
+//para que elas sejam inseridas no imap pela função  imap_functions.unarchive_mail.
 local_messages.prototype.unarchive_msgs = function (folder,new_folder,msgs_number){
-
+    write_msg("Realizando o desarquivamento...",true);
     if(!new_folder)
 	new_folder='INBOX';
@@ -1243,18 +1274,17 @@
              expresso_local_messages.delete_msgs(archived.join());
         if(currentTab != 0)
-            delete_border(currentTab,'false');
-        
-        if (!data.error)
+        delete_border(currentTab,'false');
+        clean_msg();
+        if (!data.archived || data.archived.length == 0)
+        {
+                write_msg(get_lang('No messages were unarchived'));
+        }else if (!data.error)
         {
             write_msg(get_lang('All messages are successfully unarchived'));
+        }else 
+        {
+              write_msg(get_lang("Some messages weren't successfully unarchived"));
         }
-        else if (!data.archived)
-            {
-                write_msg(get_lang('No messages were unarchived'));
-            }
-            else
-            {
-                write_msg(get_lang("Some messages weren't successfully unarchived"));
-            }
+
     }
 
@@ -1262,5 +1292,4 @@
             msgs_number = currentTab.toString().substr(0,currentTab.toString().indexOf("_r"));
     }
-	
     if(msgs_number =='selected' || !msgs_number)
     {
@@ -1270,4 +1299,12 @@
             return;
         }
+        var totalSize = this.dbGears.execute("select sum(size) from mail where rowid in ("+msgs_number+")");
+        if(totalSize.field(0) > 30*1024*1024){
+            clean_msg();
+            write_msg('Por favor selecione menos de 30MB de mensagens para desarquivar');
+            this.finalize();
+            return;
+        }
+        
         var rs = this.dbGears.execute("select mail,timestamp,rowid from mail where rowid in ("+msgs_number+")");
         var source="";
@@ -1279,8 +1316,10 @@
                 mail=connector.unserialize(rs.field(0));
                 mail.msg_source?source_tmp = escape(mail.msg_source):source_tmp = escape(this.get_src(mail.url_export_file));
+                if(typeof (source_tmp) != 'undefined' && source_tmp != "" && source_tmp.toLowerCase().indexOf("return-path:") == -1){
                     flags+="#@#@#@"+mail["Answered"]+":"+mail["Draft"]+":"+mail["Flagged"]+":"+mail["Unseen"];
                     source+="#@#@#@"+source_tmp;
                     timestamp+="#@#@#@"+rs.field(1);
                     id+="#@#@#@"+rs.field(2);
+                }    
                 rs.next();
         }
@@ -1290,15 +1329,24 @@
     else
     {
-        var rs = this.dbGears.execute("select mail,timestamp,rowid from mail where rowid="+msgs_number);
+        var rs = this.dbGears.execute("select mail,timestamp,rowid,size from mail where rowid="+msgs_number);
+        if(rs.field(3) > 30*1024*1024){
+            clean_msg();
+            write_msg('Por favor selecione menos de 30MB de mensagens para desarquivar');
+            this.finalize();
+            return;
+        }
         mail=connector.unserialize(rs.field(0));
         var source ="";
         mail.msg_source?source = escape(mail.msg_source):source = escape(this.get_src(mail.url_export_file));
+        if(typeof (source) != 'undefined' && source != '' && source.toLowerCase().indexOf("return-path:") == -1){
             flags = mail["Answered"]+":"+mail["Draft"]+":"+mail["Flagged"]+":"+mail["Unseen"];
             timestamp=rs.field(1);
             id=rs.field(2);
             rs.close();
+        }    
         this.finalize();
-    }
-    params="&folder="+new_folder+"&source="+source+"&timestamp="+timestamp+"&flags="+flags+"&id="+id;
+        
+    }
+    params="&folder="+escape(new_folder)+"&source="+source+"&timestamp="+timestamp+"&flags="+flags+"&id="+id;
     cExecute ("$this.imap_functions.unarchive_mail&", handler_unarchive, params);
 }
