Index: sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/js/mail_archiver.js
===================================================================
--- sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/js/mail_archiver.js	(revision 4909)
+++ sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/js/mail_archiver.js	(revision 4942)
@@ -142,4 +142,5 @@
     this.counter = 0;
     this.message_list = new Array();
+    this.messageslisted = new Array();
     this.total_messages = 0;
     this.messages_processed = 0;
@@ -149,7 +150,9 @@
     this.folders = null;
     this.currentfolder = 'local_root';
+    this.currentmessage = null;
     this.drawdata = null;
     this.onprocess = false;
     this.queryconfig = new MAQueryConfig();
+    this.messagesourcehandler = new Cxf_Transport_Handler();
     this.specialfolders = {
                             "inbox":"local_inbox", 
@@ -370,5 +373,5 @@
             cExecute("$this.exporteml.js_source_var",arch_handler,"folder="+url_decode(source_folder)+"&msgs_to_export="+expresso_mail_archive.message_list[0]);
         }
-        else throw('Ops. Message list stack is empty!');
+        else throw('Ops. Message list stack is empty!\nMessage list count = ' + expresso_mail_archive.message_list.length + '\nFirst child = ' + expresso_mail_archive.message_list[0]);
     }
     catch(e){
@@ -741,5 +744,5 @@
         var testing_data_xml = '<?xml version="1.0" encoding="UTF-8"?><query lowerIndex="0" upperIndex="50"><folder id="'+folderid+'"/><order asc="date"/><order desc="subject"/></query>';
         var testing_data_json_mapped = '{"query":{"@lowerIndex":"0", "@upperIndex":"50", "folder":[{"@id":"'+folderid+'"}], "order":[{"@asc":"date", "@desc":"subject"}]}}';
-
+        //window.alert('Query config lower = ' + expresso_mail_archive.queryconfig.lowerIndex + ' - upper = ' + expresso_mail_archive.queryconfig.upperIndex + ' folder = ' + expresso_mail_archive.queryconfig.folder);
         if (expresso_mail_archive.queryconfig.folder != null)
             var testing_data = expresso_mail_archive.queryconfig.query();
@@ -775,6 +778,12 @@
     msglist["num_msgs"] = returnService.getReturn().length;
     //window.alert('print_r\n\n' + print_r(msglist));
+    expresso_mail_archive.messageslisted = msglist;
+    //window.alert('expresso_mail_archive.message_list.length = ' + expresso_mail_archive.message_list.length);
+    //window.alert(print_r(expresso_mail_archive.message_list));
     expresso_mail_archive.drawdata = {messagesList:msglist};
     expresso_mail_archive.drawMessagesList();
+    
+    window.alert('get ajax...');
+    expresso_mail_archive.getMessageSource();
 }
 
@@ -799,9 +808,87 @@
     alternate_border(0);
     Element("border_id_0").innerHTML = "&nbsp;" + lang_folder(fcaption) + '&nbsp;<font face="Verdana" size="1" color="#505050">[<span id="new_m">&nbsp;</span> / <span id="tot_m"></span>]</font>';
-    draw_box(data_to_draw, expresso_mail_archive.currentfolder, true);
+    draw_box(data_to_draw, 'local_' + expresso_mail_archive.currentfolder, true);
     draw_paging(expresso_mail_archive.drawdata.messagesList.length);
     Element("tot_m").innerHTML = expresso_mail_archive.drawdata.messagesList.length;
     //update_menu();
     
+}
+
+MailArchiver.prototype.getMessageHeaders = function(msgId){
+    var msgfound = false;
+    var headerMsg;
+    
+    //Get message header info, from message list operation previously invoked
+    for (var w=0; w < expresso_mail_archive.messageslisted.length; w++){
+        //window.alert('comparando messageslisted "' +expresso_mail_archive.messageslisted[w]["msg_number"]+ '" com o id parametrizado "' +msgId+ '"');
+        if(expresso_mail_archive.messageslisted[w]["msg_number"] == msgId){
+            headerMsg = expresso_mail_archive.messageslisted[w];
+            msgfound = true;
+            break;
+        }
+    }
+    //Message does not exists at message list. it will not be loaded.
+    if(!msgfound){
+        alert('ops... the message"'+msgId+'" was not found at messages list, so it cannot be readed!')
+        headerMsg = null;
+        return;
+    }
+    return(headerMsg);
+}
+
+MailArchiver.prototype.getMessage = function(msgdata){
+    try{
+        if((msgdata.length <= 0) || (msgdata == null)){
+            window.alert('no msgdata received. aborting local message load')
+            return;
+        }
+
+        expresso_mail_archive.currentmessage = expresso_mail_archive.getMessageHeaders(msgdata)["msg_number"];
+        //window.alert('current message guid = ' + expresso_mail_archive.currentmessage);
+
+        //window.alert('vai invocar getMessageBody com msgid=' + expresso_mail_archive.currentmessage);
+        ArchiveServices.getMessageBody(expresso_mail_archive.getMessageOperationOK, expresso_mail_archive.getMessageOperationFailure, sessid, expresso_mail_archive.currentmessage);
+    }
+    catch(e){
+        window.alert('Ops...getMessage fails at:\n' + e);
+    }
+}
+
+MailArchiver.prototype.getMessageOperationOK = function(returnService){
+    if(returnService.getReturn().length <= 0){
+        window.alert('Oh no: service return data is zero length...');
+        return;
+    }
+    
+    //window.alert('Get header info from message "' + expresso_mail_archive.currentmessage + '"');
+    
+    
+    var msgBody = returnService.getReturn();
+    var msgHeaders = expresso_mail_archive.getMessageHeaders(expresso_mail_archive.currentmessage);
+    var expSerializedMessage = expresso_mail_archive.toExpressoMessage(msgHeaders, msgBody);
+    //window.alert('Serialized data\n\n' + print_r(expSerializedMessage));
+    show_msg(expSerializedMessage);
+}
+
+MailArchiver.prototype.getMessageOperationFailure = function(ServiceFault){
+    window.alert('Message fails do be loaded.');
+}
+
+//Under develpment this one
+MailArchiver.prototype.getMessageSource = function(){
+    expresso_mail_archive.messagesourcehandler.onreadystatechange = AlerteO();
+    //expresso_mail_archive.messagesourcehandler.setRequestHeader("SOAPAction", "");
+    //expresso_mail_archive.messagesourcehandler.setRequestHeader("Access-Control-Allow-Origin", "*");
+    //expresso_mail_archive.messagesourcehandler.setRequestHeader("Access-Control-Allow-Credentials", "true");
+    window.alert('vai chamar via ajax o texto');
+    expresso_mail_archive.messagesourcehandler.open("GET", "http://10.200.242.186:4333/mail/Sent/teste.txt", true);
+    //expresso_mail_archive.messagesourcehandler.setRequestHeader("MessageType", "CALL");
+    //expresso_mail_archive.messagesourcehandler.setRequestHeader("Access-Control-Allow-Origin", "*");
+    //expresso_mail_archive.messagesourcehandler.setRequestHeader("Access-Control-Allow-Credentials", "true");    
+    expresso_mail_archive.messagesourcehandler.send();
+}
+
+function AlerteO(){
+    window.alert('Ajax lido:\n\nState = ' + expresso_mail_archive.messagesourcehandler.readyState + '\nStatus = ' + expresso_mail_archive.messagesourcehandler.status);
 }
 
@@ -839,6 +926,7 @@
 +           's:'+dataEmail.length+':"'+dataEmail+'";'
 +           's:4:"full";'
-+           's:'+(dataName.length + dataEmail.length + 3)+':"'+dataName+'" <'+dataEmail+'>;'
++           's:'+(dataName.length + dataEmail.length + 3)+':"'+dataName+' <'+dataEmail+'>";'
 +       '}';
+    //window.alert('from = ' + tplFrom);
     }
     //Data adrress from mailbox is empty, null, or canot be correctly returned by service invocation (will return 'unknow mailbox' string)
@@ -993,6 +1081,132 @@
 }
 
-MailArchiver.prototype.toExpressoMail = function(data){
-    
+MailArchiver.prototype.toExpressoBody = function(mheader){
+    var tplBody = 'a:33:{'
++                   's:4:"body";N;'
++                   's:11:"attachments";N;'
++                   's:6:"thumbs";N;'
++                   's:9:"signature";N;'
++                   's:10:"Importance";s:6:"Normal";'
++                   's:6:"Recent";s:1:" ";'
++                   's:6:"Unseen";s:1:"N";'
++                   's:7:"Deleted";s:1:" ";'
++                   's:7:"Flagged";b:0;'
++                   's:8:"Answered";s:1:" ";'
++                   's:5:"Draft";s:1:" ";'
++                   's:10:"msg_number";s:4:"2241";'
++                   's:10:"msg_folder";s:5:"INBOX";'
++                   's:5:"udate";s:5:"10:01";'
++                   's:7:"msg_day";s:10:"10/05/2011";'
++                   's:8:"msg_hour";s:5:"10:02";'
++                   's:8:"fulldate";s:36:"10/05/2011 10:01 (05:35 horas atras)";'
++                   's:9:"smalldate";s:5:"10:04";'
++                   's:4:"from";N;'
++                   's:6:"sender";a:3:{'
++                       's:4:"name";N;'
++                       's:5:"email";s:46:"listaregional-pae-bounces@grupos.serpro.gov.br";'
++                       's:4:"full";s:46:"listaregional-pae-bounces@grupos.serpro.gov.br";'
++                   '}'
++                   's:10:"toaddress2";s:32:"comunicacao.social@serpro.gov.br";'
++                   's:2:"cc";N;'
++                   's:3:"bcc";N;'
++                   's:8:"reply_to";N;'
++                   's:7:"subject";N;'
++                   's:4:"Size";s:4:"1234";'
++                   's:15:"reply_toaddress";s:45:"com.social <comunicacao.social@serpro.gov.br>";'
++                   's:9:"timestamp";i:1305032476;'
++                   's:5:"login";i:944889240;'
++                   's:6:"header";a:16:{'
++                      's:11:"ContentType";s:6:"normal";'
++                      's:10:"Importance";s:6:"Normal";'
++                      's:6:"Recent";s:1:" ";'
++                      's:6:"Unseen";s:1:"N";'
++                      's:8:"Answered";s:1:" ";'
++                      's:5:"Draft";s:1:" ";'
++                      's:7:"Deleted";s:1:" ";'
++                      's:7:"Flagged";b:0;'
++                      's:10:"msg_number";s:4:"2251";'
++                      's:5:"udate";s:10:"10/05/2011";'
++                      's:11:"offsetToGMT";i:-10800;'
++                      's:8:"aux_date";N;'
++                      's:4:"from";a:2:{'
++                         's:4:"name";s:10:"com.social";'
++                         's:5:"email";s:32:"comunicacao.social@serpro.gov.br";'
++                      '}'
++                      's:2:"to";a:2:{'
++                         's:4:"name";s:32:"comunicacao.social@serpro.gov.br";'
++                         's:5:"email";s:32:"comunicacao.social@serpro.gov.br";'
++                      '}'
++                      's:7:"subject";s:28:"Primeira leitura - 10/5/2011";'
++                      's:4:"Size";N;'
++                      's:10:"attachment";a:2:{'
++                         's:5:"names";b:0;'
++                         's:18:"number_attachments";i:0;'
++                      '}'
++                  '}'
++                  's:12:"array_attach";N;'
++                  's:15:"url_export_file";s:160:"inc/gotodownload.php?idx_file=/var/www/expressov2/expressoMail1_2/inc/../tmpLclAtt/source_38a3e960115351578aa182ae1d6c2b7b.txt&newfilename=fonte_da_mensagem.txt";'
++                  's:2:"to";N;'
++              '}';
+    return(tplBody);
+}
+
+MailArchiver.prototype.toExpressoMessage = function(mheader, mbody){
+    var strMessage = null;
+    var strMessage1 = expresso_mail_archive.toExpressoBody(mheader);
+    //var str3 = connector.unserialize(strMessage1);
+    //strMessage =  eval("(" + strMessage1 + ")");
+    strMessage = connector.unserialize(strMessage1);
+    
+    window.alert('dump strMessage:\n\n' + print_r(strMessage,0));
+    
+    strMessage['from'] = mheader["from"];
+    //strMessage['from'] = expresso_mail_archive.toExpressoBody(mheader, mbody);
+    strMessage['subject'] = mheader["subject"];
+    strMessage['body'] = mbody;
+    strMessage['to'] = mheader["to"];
+    strMessage['cc'] = mheader["cc"];
+    strMessage['local_message'] = true;
+    strMessage['folder'] = 'local_' + expresso_mail_archive.currentfolder;
+    strMessage['msg_number'] = mheader["msg_number"];
+    //window.alert('strMessage dump\n\n' + print_r(strMessage));
+    return(strMessage);
+
+
+/*
+ *	retorno['from'] = connector.unserialize(rs.field(2));
+	retorno['subject'] = rs.field(3);
+	retorno['body'] = rs.field(4);
+	//Codigo que as imagens embutidas em emails (com multipart/related ou multipart/mixed) sejam corretamente mostradas em emails arquivados. Os links do
+	//tipo "./inc/show_embedded_attach.php?msg_folder=[folder]&msg_num=[msg_num]&msg_part=[part]"
+	//sÃ£o substituidos pelos links dos anexos capturados pelo gears.
+
+	var thumbs= retorno.thumbs;
+	var anexos= retorno.array_attach;
+	for (i in anexos)
+	{
+	    if(anexos[i]['url'] && anexos[i]['url'].match(/((jpg)|(jpeg)|(png)|(gif)|(bmp))/gi))
+	    {
+		var er_imagens = new RegExp("\\.\\/inc\\/show_embedded_attach.php\\?msg_folder=[\\w/]+\\&msg_num=[0-9]+\\&msg_part="+anexos[i]['pid']);
+		var Result_imagens = er_imagens.exec(retorno['body']);
+		retorno['body'] = retorno['body'].replace(Result_imagens,anexos[i]['url']);
+		if(thumbs && thumbs[i]){
+		    er_imagens = new RegExp("\\.\\/inc\\/show_thumbs.php\\?file_type=image\\/[\\w]+\\&msg_num=[0-9]+\\&msg_folder=[\\w/%]+\\&msg_part="+anexos[i]['pid']);
+		    Result_imagens = er_imagens.exec(thumbs[i]);
+		    thumbs[i] = thumbs[i].replace(Result_imagens,"'"+anexos[i]['url']+"'");
+		    er_imagens = new RegExp("\\.\\/inc\\/show_img.php\\?msg_num=[0-9]+\\&msg_folder=[\\w/%]+\\&msg_part="+anexos[i]['pid']);
+		    Result_imagens = er_imagens.exec(thumbs[i]);
+		    thumbs[i] = thumbs[i].replace(Result_imagens,anexos[i]['url']);
+		    thumbs[i] = thumbs[i].replace(/<IMG/i,'<img width="120"');
+		}
+	    }
+	}
+
+	retorno['to'] = connector.unserialize(rs.field(5));
+	retorno['cc'] = connector.unserialize(rs.field(6));
+
+	retorno['local_message'] = true;
+	retorno['msg_folder'] = "local_"+rs.field(7); //Now it's a local folder
+	retorno['msg_number'] = rs.field(0)+plus_id; //the message number is the rowid
+ **/    
 }
 
