Index: trunk/expressoMail1_2/js/messages_controller.js
===================================================================
--- trunk/expressoMail1_2/js/messages_controller.js	(revision 7200)
+++ trunk/expressoMail1_2/js/messages_controller.js	(revision 7414)
@@ -9,5 +9,70 @@
  * Retorna os cabeçalhos das mensagens a serem desenhadas na caixa de email.
  */
-	messages_proxy.prototype.messages_list = function(folder,msg_range_begin,emails_per_page,sort_box_type,search_box_type,sort_box_reverse,preview_msg_subject,preview_msg_tip,call_back) {
+    messages_proxy.prototype.messages_list = function(folder,msg_range_begin,emails_per_page,sort_box_type,search_box_type,sort_box_reverse,preview_msg_subject,preview_msg_tip,call_back) {
+        if(this.is_local_folder(folder)) {
+
+            //MAILARCHIVER-01
+            
+            //var baseFolder = folder.substr(6);
+            var baseFolder = folder.replace('local_messages_', '');
+            var tree_name;
+            //var drawinginfo = {treeObject: tree_name, treeName: tree_name, folderName: baseFolder};
+            var drawinginfo = {};
+            expresso_mail_archive.update_counters = false;
+            expresso_mail_archive.currentfolder = baseFolder;
+            expresso_mail_archive.getFolderInfo(expresso_mail_archive.currentfolder);
+            expresso_mail_archive.drawdata = drawinginfo;
+
+            if(tree_name == 'tree_folders'){ //only the 'tree_folders' must show messages. 'folders_tree' are just for managment operation
+                var exp_dfs = new Array(baseFolder,msg_range_begin,emails_per_page,sort_box_type,search_box_type, sort_box_reverse,preview_msg_subject,preview_msg_tip);
+                expresso_mail_archive.queryconfig.setExpressoDefaults(exp_dfs);
+                expresso_mail_archive.listMessages();
+            }
+            else{
+                if (!tree_name){
+                    //window.alert('sem arvore para atualizar, com sort_box_type =' + sort_box_type + ' e search_box_type= ' + search_box_type + ' reverse -- ' + sort_box_reverse);
+                    var exp_dfs = new Array(baseFolder,msg_range_begin,emails_per_page,sort_box_type,search_box_type,sort_box_reverse,preview_msg_subject,preview_msg_tip);
+                    expresso_mail_archive.queryconfig.setExpressoDefaults(exp_dfs);
+                    expresso_mail_archive.listMessages();
+                }
+                if(tree_name == 'folders_tree'){
+                    /*var div_export_local = document.createElement('div');
+                    div.style.width
+                    var lbl_check = document.createElement('label');
+                    var check_export_local_recursive = document.createElement('input');
+                    var lbl_combo = document.createElement('label');
+                    var combo_export_local_format = document.createElement('select');*/
+                    //window.alert('alert td content = ' + Element('div_buttons').getElementsByTagName('table')[0].tbody[0].tr[4].td[0].innerHTML);
+                    //Element('div_buttons').appendChild(div_export_local);
+                }
+            }
+
+        }else {
+            $.ajax({
+                url: "controller.php?" + $.param( {action: "$this.imap_functions.get_range_msgs3", 
+                                    folder: folder, 
+                                    msg_range_begin: msg_range_begin, 
+                                    msg_range_end: emails_per_page, 
+                                    sort_box_type: sort_box_type, 
+                                    search_box_type: search_box_type, 
+                                    sort_box_reverse: sort_box_reverse } ),
+
+                  success: function( data ){
+                    data = connector.unserialize(data);
+                    
+                    if( data )
+                        call_back( data );
+                  },
+                  beforeSend: function( jqXHR, settings ){
+                    connector.showProgressBar();
+                  },
+                  complete: function( jqXHR, settings ){
+                    connector.hideProgressBar();
+                  }
+              
+            });
+        }
+    }
+	messages_proxy.prototype.messages_list_old = function(folder,msg_range_begin,emails_per_page,sort_box_type,search_box_type,sort_box_reverse,preview_msg_subject,preview_msg_tip,call_back) {
 		if(this.is_local_folder(folder)) {
 			var real_folder = folder.substr(6);		
@@ -53,9 +118,13 @@
 	messages_proxy.prototype.get_msg = function(msg_number,msg_folder,set_flag,call_back) {
 		if(this.is_local_folder(msg_folder)) {
-			if(set_flag)
-				expresso_local_messages.set_messages_flag(msg_number,"seen");
-			var data = expresso_local_messages.get_local_mail(msg_number);	
-
-			eval("call_back(data)");
+			
+
+            //if(set_flag)
+			// 	expresso_local_messages.set_messages_flag(msg_number,"seen");
+			// var data = expresso_local_messages.get_local_mail(msg_number);	
+            //MAILARCHIVER-02
+            expresso_mail_archive.getMessage(msg_number);
+
+			//eval("call_back(data)");
 		}else{
 		    $.ajax({
@@ -86,7 +155,11 @@
 		if (folder == 'null')
 			folder = get_current_folder();
-		if (msgs_number == 'selected')
-			msgs_number = get_selected_messages();
-		
+
+        //Validação para recuperar o id caso não seja aba de listagem
+		if (msgs_number == 'selected' && currentTab != 0) //Recupera apenas o id da mensagem aberta
+			msgs_number = currentTab.substr(0,currentTab.indexOf('_r'));
+        else if (msgs_number == 'selected' && currentTab == 0)
+            msgs_number = get_selected_messages();
+
 		if(currentTab != 0 && currentTab.indexOf("search_")  >= 0){
 			var content_search = document.getElementById('content_id_'+currentTab);
@@ -100,18 +173,115 @@
 		if (!this.is_local_folder(folder)){
 			delete_msgs(folder, msgs_number, border_ID);
-		}else {
-			if((folder != 'local_Trash') && (expresso_local_messages.as_trash())){
-				expresso_local_messages.move_messages('Trash', msgs_number);
-				this.aux_interface_remove_mails(msgs_number, 'local_Trash', border_ID);
-				draw_tree_local_folders();
-				update_local_box(); 
-				refresh(); 
-			}else{
-			expresso_local_messages.delete_msgs(msgs_number, border_ID);
-			draw_tree_local_folders();
-				update_local_box(); 
-				refresh();
-			}
-		}
+		}else {    
+            //MAILARCHIVER-03
+            var msg_to_delete = Element(msgs_number);
+            //user has preference to show previous message on deleting one
+            if (isNaN(currentTab) && parseInt(preferences.delete_and_show_previous_message) && msg_to_delete) {
+                    if (msg_to_delete.previousSibling){ 
+                            var previous_msg = msg_to_delete.previousSibling.id;
+                            
+                            //user has preference to maintain default folder structure at local folders, so we have trash folder
+                            if(preferences.auto_create_local == 1){
+                                //user has preference to "save" delete messasge on trash folder, so move it to there
+                                if (((preferences.save_deleted_msg == true)) && (folder != expresso_mail_archive.specialfolders.trash)){
+                                    expresso_mail_archive.folder_destination = 'trash';
+                                    expresso_mail_archive.moveMessages(expresso_mail_archive.folder_destination, msgs_number);
+                                    delete_border(currentTab,'false'); 
+                                    expresso_mail_archive.getMessage(previous_msg);
+                                }
+                                //user does not want to save messages deleted on trash. purge them imediatly
+                                else{
+                                    expresso_mail_archive.deleteMessages(msgs_number);
+                                    delete_border(currentTab,'false'); 
+                                    expresso_mail_archive.getMessage(previous_msg)
+                                }                                                                                
+                            }
+                            //maybe, we do not have trash.
+                            else{
+                                //user has preference to "save" delete messasge on trash folder, so move it to there
+                                if (((preferences.save_deleted_msg == true)) && (folder != expresso_mail_archive.specialfolders.trash)){
+                                    expresso_mail_archive.createFolder("","Trash");
+                                    expresso_mail_archive.folder_destination = 'trash';
+                                    expresso_mail_archive.moveMessages(expresso_mail_archive.folder_destination, msgs_number);
+                                    delete_border(currentTab,'false');
+                                    expresso_mail_archive.getMessage(previous_msg);
+                                }
+                                //user does not want to save messages deleted on trash. purge them imediatly
+                                else{
+                                    expresso_mail_archive.deleteMessages(msgs_number);
+                                    delete_border(currentTab,'false'); 
+                                    expresso_mail_archive.getMessage(previous_msg)
+                                }                                          
+                            }
+                    } 
+                    //there is no previous message existing to show. Just delete de view context
+                    else{ 
+                           //user has preference to maintain default folder structure at local folders, so we have trash folder
+                           if(preferences.auto_create_local == 1){
+                                if (((preferences.save_deleted_msg == true)) && (folder != expresso_mail_archive.specialfolders.trash)){
+                                    expresso_mail_archive.folder_destination = 'trash';
+                                    expresso_mail_archive.moveMessages(expresso_mail_archive.folder_destination, msgs_number);
+                                    delete_border(currentTab,'false'); 
+                                }
+                                //user does not want to save messages deleted on trash. purge them imediatly
+                                else{
+                                    expresso_mail_archive.deleteMessages(msgs_number);
+                                    delete_border(currentTab,'false'); 
+                                }                                       
+                           }
+                            //maybe, we do not have trash. Purge message so.
+                            else{
+                                //user has preference to "save" delete messasge on trash folder, so move it to there
+                                if (((preferences.save_deleted_msg == true)) && (folder != expresso_mail_archive.specialfolders.trash)){
+                                    expresso_mail_archive.createFolder("","Trash");
+                                    expresso_mail_archive.folder_destination = 'trash';
+                                    expresso_mail_archive.moveMessages(expresso_mail_archive.folder_destination, msgs_number);
+                                    expresso_mail_archive.getMessage(previous_msg);
+                                }
+                                //user does not want to save messages deleted on trash. purge them imediatly
+                                else{
+                                    expresso_mail_archive.deleteMessages(msgs_number);
+                                }                                    
+                            }
+                    } 
+            } 
+            //user has no preferece to show previous message on deleting
+            else{ 
+                if(preferences.auto_create_local == 1){
+                    //user has preference to "save" delete messasge on trash folder, so move it to there
+                    if (((preferences.save_deleted_msg == true)) && (folder.replace('messages_','') != expresso_mail_archive.specialfolders.trash)){
+                        expresso_mail_archive.folder_destination = 'trash';
+                        expresso_mail_archive.moveMessages(expresso_mail_archive.folder_destination, msgs_number);
+                        delete_border(currentTab,'false');
+                    }
+                    //user does not want to save messages deleted on trash (or is cleaning it). purge them imediatly
+                    else{
+                        expresso_mail_archive.deleteMessages(msgs_number);
+                        delete_border(currentTab,'false');
+                    }                            
+                }
+                //maybe, we do not have trash. Purge message so.
+                else{
+                    //user has preference to "save" delete messasge on trash folder, so move it to there
+                    if (((preferences.save_deleted_msg == true)) && (folder.replace('messages_','') != expresso_mail_archive.specialfolders.trash)){
+                        expresso_mail_archive.createFolder("","Trash");
+                        expresso_mail_archive.folder_destination = 'trash';
+                        expresso_mail_archive.moveMessages(expresso_mail_archive.folder_destination, msgs_number);
+                    }
+                    //user does not want to save messages deleted on trash. purge them imediatly
+                    else{
+                        expresso_mail_archive.deleteMessages(msgs_number);
+                    }     
+                }
+            }
+		}
+
+        //Código adicionado para o correto funcionamento da seleção independente de paginação.
+        $.each(msgs_number.split(","), function(index, value){
+            delete selectedPagingMsgs[value];
+        });
+        totalFolderMsgs = totalFolderMsgs - msgs_number.split(",").length;
+        selectAllFolderMsgs(false);
+        updateSelectedMsgs();
 	}
 	
@@ -119,7 +289,10 @@
 
 		if(info_msg.local_message==true) {
-			return "javascript:download_local_attachment('"+
-					expresso_local_messages.get_url_anexo(info_msg.msg_number,info_msg.attachments[numero_ordem_anexo].pid)+
-					"')";
+            //ma serpro
+			// return "javascript:download_local_attachment('"+
+			// 		expresso_local_messages.get_url_anexo(info_msg.msg_number,info_msg.attachments[numero_ordem_anexo].pid)+
+			// 		"')";
+            //MAILARCHIVER-04
+            return "javascript:download_local_attachment('"+ mail_archive_protocol+'://'+mail_archive_host+':'+mail_archive_port+'/temp/download/' +info_msg.attachments[numero_ordem_anexo].pid + "')";
 		}
 		else {
@@ -139,27 +312,103 @@
 	
 	messages_proxy.prototype.proxy_set_messages_flag = function (flag,msg_number){
-		if(this.is_local_folder(get_current_folder())) {
-			expresso_local_messages.set_messages_flag(msg_number,flag);
-		}
-		else {
-			set_messages_flag(flag,msg_number);
-				// Verifica se a pasta que esta selecionada contem a opção "Não lidas" ativada
-				// caso exista, ele chama novamente a opção "Não lidas" para atualizar a pasta.
-				if('UNSEEN' == search_box_type)
-					return sort_box('UNSEEN','SORTARRIVAL');
-		}
-	}
-	
-	messages_proxy.prototype.proxy_set_message_flag = function (msg_number,flag,func_after_flag_change){
-		var msg_number_folder = Element("new_input_folder_"+msg_number+"_r"); //Mensagens respondidas/encaminhadas
+        //MAILARCHIVER
+        if(this.is_local_folder(get_current_folder())) {           
+            var msglist = get_selected_messages();    
+            var arrlist = msglist.split(",");
+            var operation;
+            var strtag;
+            
+            for(var i in arrlist){
+                switch(flag.toLowerCase()){
+                    case 'unseen':
+                        strtag = "unseen";
+                        operation = 0;
+                        set_msg_as_unread(arrlist[i]);
+                        break;
+                    case 'seen':
+                        strtag = "unseen";
+                        operation = 1;
+                        set_msg_as_read(arrlist[i]);
+                        break;
+                    case 'flagged':
+                        strtag = "flagged, importance_high";
+                        operation = 0;
+                        set_msg_as_flagged(arrlist[i]);
+                        break;
+                    case 'unflagged':
+                        strtag = "flagged, importance_high";
+                        operation = 1;
+                        set_msg_as_unflagged(arrlist[i]);
+                        break;
+                }
+                expresso_mail_archive.currenttag = flag;
+                var tl = expresso_mail_archive.pattern.tagConfig(strtag, arrlist[i], operation);
+                expresso_mail_archive.taglist = tl;                             
+                expresso_mail_archive.progressbar = window.setTimeout("expresso_mail_archive.tagMessage()",1);
+            }
+        }
+        else {
+            set_messages_flag(flag,msg_number);
+            // Verifica se a pasta que esta selecionada contem a opção "Não lidas" ativada
+            // caso exista, ele chama novamente a opção "Não lidas" para atualizar a pasta.
+            if('UNSEEN' == search_box_type)
+                return sort_box('UNSEEN','SORTARRIVAL');
+        }
+	}
+	
+	messages_proxy.prototype.proxy_set_message_flag = function (msg_number,flag,func_after_flag_change,msgid){
+        var msg_number_folder = Element("new_input_folder_"+msg_number+"_r"); //Mensagens respondidas/encaminhadas
 		if(!msg_number_folder)
 			var msg_number_folder = Element("input_folder_"+msg_number+"_r"); //Mensagens abertas
 		var folder = msg_number_folder ?  msg_number_folder.value : get_current_folder();
-		if(this.is_local_folder(folder)) {
-			expresso_local_messages.set_message_flag(msg_number,flag, func_after_flag_change);
-		}
-		else {
-			set_message_flag(msg_number,flag, func_after_flag_change);
-		}
+
+        //MAILARCHIVER
+        if(this.is_local_folder(folder)) {
+            if(!msgid)
+                msgid = msg_number;
+            var taglist = flag;
+            var operation;
+    
+            switch(flag.toLowerCase()){
+                case 'unseen':
+                    strtag = "unseen";
+                    operation = 0;
+                    set_msg_as_unread(msgid);
+                    break;
+                case 'seen':
+                    strtag = "unseen";
+                    operation = 1;
+                    set_msg_as_read(msgid);
+                    break;
+                case 'flagged':
+                    strtag = 'flagged, importance_high';
+                    operation = 0;
+                    set_msg_as_flagged(msgid);
+                    break;
+                case 'unflagged':
+                    strtag = "flagged, importance_high";
+                    operation = 1;
+                    set_msg_as_unflagged(msgid);
+                    break;
+                case 'forwarded':
+                    strtag = "forwarded";
+                    operation = 0;
+                    set_msg_as_flagged(msgid);
+                case 'answered':
+                    strtag = "answered";
+                    operation = 0;
+                    set_msg_as_flagged(msgid);                                  
+                    
+            }                
+            
+            expresso_mail_archive.drawdata = null;
+            var tl = expresso_mail_archive.pattern.tagConfig(strtag, msgid, operation);
+            expresso_mail_archive.currenttag = flag;
+            expresso_mail_archive.taglist = tl;
+            expresso_mail_archive.progressbar = window.setTimeout("expresso_mail_archive.tagMessage()",1);
+        }
+        else {
+            set_message_flag(msg_number,flag, func_after_flag_change);
+        }
 	}
 	
@@ -290,43 +539,68 @@
 		if (! folder || folder == 'null')
 			folder = Element("input_folder_"+msgs_number+"_r") ? Element("input_folder_"+msgs_number+"_r").value : (openTab.imapBox[currentTab] ? openTab.imapBox[currentTab]:get_current_folder());
-		if ((this.is_local_folder(folder)) && (this.is_local_folder(new_folder))) { //Move entre pastas não locais...
-			if (folder == new_folder){
-				write_msg(get_lang('The origin folder and the destination folder are the same.'));
+
+        //MAILARCHIVER-08
+        if (this.is_local_folder(folder)){
+            expresso_mail_archive.update_counters = true;
+            //Folder de onde sai a mensagem Ã© local (armazenamento local)
+            if (folder == new_folder){
+                write_msg(get_lang('The origin folder and the destination folder are the same.'));
 				return;
-			}
-			if(msgs_number=='selected')
-				msgs_number = get_selected_messages();
-			if (new_folder == 'local_root')
-				alert(get_lang("Select a folder!"));
-			if (parseInt(msgs_number) > 0 || msgs_number.length > 0) {
-				expresso_local_messages.move_messages(new_folder.substr(6), msgs_number);
-				this.aux_interface_remove_mails(msgs_number, new_folder_name, border_ID);
-				draw_tree_local_folders();
-                                update_local_box();
-			}
-			else 
-				write_msg(get_lang('No selected message.'));
-		}
-		else 
-			if ((!this.is_local_folder(folder)) && (!this.is_local_folder(new_folder))) { //Move entre pastas locais...
-				move_msgs(folder, msgs_number, border_ID, new_folder, new_folder_name);
-			}else if ((!this.is_local_folder(folder)) && (this.is_local_folder(new_folder))) {
-				if(msgs_number=='selected')
-					archive_msgs(folder,new_folder);
-				else
-					archive_msgs(folder,new_folder,msgs_number);
-
-				
-				draw_tree_local_folders();
-				refresh();
-			}
-			else {
-                //Por Bruno Costa (bruno.vieira-costa@serpro.gov.br) permite o desarquivamento de menssagens chamando a função unarchive_msgs quando uma msg é movida de uma pasta local para uma pasta remota.
-
-				expresso_local_messages.unarchive_msgs(folder,new_folder,msgs_number);
-                //write_msg(get_lang("you can't move mails from local to server folders"));
-			}
-		
-		
+            }                    
+
+            //Validação para recuperar o id caso não seja aba de listagem
+            if (msgs_number == 'selected' && currentTab != 0) //Recupera apenas o id da mensagem aberta
+                msgs_number = currentTab.substr(0,currentTab.indexOf('_r'));
+            else
+                msgs_number = get_selected_messages();
+
+            if (new_folder == 'local_root'){
+                alert(get_lang("Select a folder!"));
+            }
+                        
+            if (parseInt(msgs_number) > 0 || msgs_number.length > 0) {
+                if (this.is_local_folder(new_folder)){
+                    //esta tirando de um folder local para outro folder local
+                    expresso_mail_archive.moveMessages(new_folder.replace("local_messages_",""), msgs_number);
+                    if(currentTab != 0)
+                        delete_border(currentTab,'false'); 
+                }
+                else{
+                    //esta tirando de um folder local para um folder IMAP (desarquivamento)
+                    if(!new_folder){
+                        new_folder = 'INBOX';
+                    }
+                    if(currentTab.toString().indexOf("_r") != -1){
+                        msgs_number = currentTab.toString().substr(0,currentTab.toString().indexOf("_r"));
+                    }                      
+                    expresso_mail_archive.unarchieve(folder, new_folder, msgs_number);
+                }
+            }
+            else{
+                write_msg(get_lang('No selected message.'));
+            }
+        }else{
+            if (this.is_local_folder(new_folder)){
+                //esta tirando de um folder nÃ£o local para um folder local (arquivamento)
+                if(msgs_number=='selected'){
+                    archive_msgs(folder, new_folder);
+                }
+                else{
+                    archive_msgs(folder, new_folder, msgs_number);
+                }
+            }
+            else{
+                //esta tirando de um folder nÃ£o local para outro folder nÃ£o local (move)
+                move_msgs(folder, msgs_number, border_ID, new_folder, new_folder_name);
+            }
+        }	
+
+        //Adicionado código para o correto funcionamento da seleção independente de paginação.
+        $.each(msgs_number.split(","), function(index, value){
+            delete selectedPagingMsgs[value];
+        });
+        totalFolderMsgs = totalFolderMsgs - msgs_number.split(",").length;
+        selectAllFolderMsgs(false);
+        updateSelectedMsgs();
 	}
 	
@@ -462,15 +736,30 @@
 
     messages_proxy.prototype.export_all_messages = function(folder){
-
-		if (!folder){
-			folder = get_current_folder();
-		}
-
-		if(!this.is_local_folder(folder)){
-			export_all_selected_msgs();
-		}else{
-			local_messages.prototype.local_messages_to_export();
-		}
-	}
+        if (!folder) {
+            folder = get_current_folder();
+        }
+
+        if(!this.is_local_folder(folder) || folder.split("_")[0] == "local"){
+            export_all_selected_msgs();
+        }else{
+            local_messages.prototype.local_messages_to_export();
+        }
+	}
+	
+
+
+	/* Airton
+	* Método que vai chamar efetivamente o método Archive.
+	* TO-DO: Tirar desse arquivo o método e passar pra outro arquivo
+	*/
+	messages_proxy.prototype.archive_message = function(folder, msgs_number) {
+		if(msgs_number == 'get_selected_messages')
+			msgs_number = get_selected_messages();
+			
+		expresso_mail_archive.Archive("INBOX", "inbox", msgs_number);
+	}
+	
+	
+	
 	/*
     	messages_proxy.prototype.proxy_export_all_msg=function(){
