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 4955)
+++ /sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/js/mail_archiver.js	(revision 4969)
@@ -154,5 +154,7 @@
     this.onprocess = false;
     this.queryconfig = new MAQueryConfig();
+    this.pattern = new MAPattern();
     this.messagesourcehandler = new Cxf_Transport_Handler();
+    this.ServiceReturnObject = null;
     this.specialfolders = {
                             "inbox":"local_inbox", 
@@ -858,4 +860,6 @@
 MailArchiver.prototype.listMessagesOperationOK = function(returnService){
     var msglist = new Array();
+    expresso_mail_archive.ServiceReturnObject = new Array();
+    //window.alert('messages data\n\n' + print_r(returnService.getReturn()));
     /*window.alert('Dados da mensagem:\n->From ' + returnService.getReturn()[0].getFrom()
                                       +'\n->To ' + returnService.getReturn()[0].getTo()
@@ -867,12 +871,15 @@
     for(var i=0; i< returnService.getReturn().length; i++){
         //window.alert('quer deserializar:\n\n' + print_r(returnService.getReturn()[i]));
-        var ExpSerialized = expresso_mail_archive.toExpressoHeader(returnService.getReturn()[i]);
+        var ExpSerialized = expresso_mail_archive.pattern.toExpressoHeader(returnService.getReturn()[i]);
         //window.alert('expSerialized ' + i + '\n\n --\n\n' + print_r(ExpSerialized));
         //var test = connector.unserialize(ExpSerialized);
         //window.alert('unserialized = \n\n' + test);
         msglist.push(connector.unserialize(ExpSerialized));//usando connector.unserialize sobre o header mapeado no layout abaixo
+        expresso_mail_archive.ServiceReturnObject.push(returnService.getReturn()[i]); //add each one service return data into this embeded object array
+        //window.alert('ServiceReturnObject.lenght = ' + expresso_mail_archive.ServiceReturnObject.length + '\n\n:' + expresso_mail_archive.ServiceReturnObject[i]);
     }
     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);
@@ -880,7 +887,4 @@
     expresso_mail_archive.drawdata = {messagesList:msglist};
     expresso_mail_archive.drawMessagesList();
-    
-    //window.alert('get ajax...');
-   // expresso_mail_archive.getMessageSource();
 }
 
@@ -931,4 +935,5 @@
         return;
     }
+    //window.alert('getMessageHeaders\n\n' + print_r(headerMsg));
     return(headerMsg);
 }
@@ -962,6 +967,8 @@
     
     var msgBody = returnService.getReturn();
+    //window.alert('getMessageOperationOK headers get..');
     var msgHeaders = expresso_mail_archive.getMessageHeaders(expresso_mail_archive.currentmessage);
-    var expSerializedMessage = expresso_mail_archive.toExpressoMessage(msgHeaders, msgBody);
+    //var expSerializedMessage = expresso_mail_archive.toExpressoMessage(msgHeaders, msgBody);
+    var expSerializedMessage = expresso_mail_archive.pattern.toExpressoMail(msgHeaders, msgBody);
     //window.alert('Serialized data\n\n' + print_r(expSerializedMessage));
     show_msg(expSerializedMessage);
@@ -972,340 +979,13 @@
 }
 
-//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);
-}
-
-// -----------------------------------SERIALIZED DATA TEMPLATES ----------------
-MailArchiver.prototype.toExpressoAddress = function(data){
-    if((typeof(data) != 'undefined') && (data.length > 0)){
-        /*var dataFormat = expresso_mail_archive.toExpressoFormatNameMail(data);
-        var dataName = dataFormat[0];
-        var dataEmail = dataFormat[1];
-        */
-        //window.alert('em toExpressoFrom com data = \n' + data + '[' + typeof(data) + ']('+data.length+')');
-        var Fromdata = eval("(" + data + ")");
-        //window.alert('temos ' + Fromdata.mailbox.length + ' destinatarios/remetentes a lidar...');
-        //window.alert('name = ' + Fromdata.mailbox["@name"] + '\nlocal part = ' + Fromdata.mailbox["@localPart"] + '\ndomain = ' + Fromdata.mailbox["@domain"]);
-     
-        //one address
-        if(typeof(Fromdata.mailbox.length) == 'undefined'){
-            var dataName = Fromdata.mailbox["@name"];               
-            var dataEmail = Fromdata.mailbox["@localPart"] + '@' + Fromdata.mailbox["@domain"];
-            if(dataName == 'null')
-                dataName = dataEmail;
-        }
-        //address list: to header(msgs list), only first metters
-        else{
-            var dataName = Fromdata.mailbox[0]["@name"];
-            var dataEmail = Fromdata.mailbox[0]["@localPart"] + '@' + Fromdata.mailbox[0]["@domain"];
-            if(dataName == 'null')
-                dataName = dataEmail;
-        }
-
-        var tplFrom = 'a:3:{'
-+           's:4:"name";'
-+           's:'+dataName.length+':"'+dataName+'";'
-+           's:5:"email";'
-+           's:'+dataEmail.length+':"'+dataEmail+'";'
-+           's:4:"full";'
-+           '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)
-    else {
-        tplFrom = 'a:2{s:4:"name";s:14:"Unknow mailbox";s:5:"email";s:15:"Unknow_mailbox";}';
-    }
-    return(tplFrom);
-}
-
-MailArchiver.prototype.toExpressoSubject = function(data){
-    if((typeof(data) != 'undefined') && (data.length > 0)){
-        var tplSbj = 's:'+data.length+':"'+data+'";'
-    }
-    //Subject is empty, null, or canot be correctly returned by service invocation (will return 'Message without subject' string)
-    else {
-        tplSbj = 's:25:"[Message without subject]";';
-    }
-    return(tplSbj);
-}
-
-MailArchiver.prototype.toExpressoAttachmentsHeader = function(data){
-    if((typeof(data) != 'undefined') && (data.length > 0)){
-        var Attdata = eval("(" + data + ")");
-        if(typeof(Attdata.attachment) != 'undefined'){                    
-            var tplAttatch = 'a:2:{'
-+               's:5:"names";';
-            //More than 1 attachment - because length will be 'undefined' if just one exists
-            if(Attdata.attachment.length > 0){
-                var strnamesatt = "";
-                for(var x=0; x<Attdata.attachment.length; x++){
-                  var attId = Attdata.attachment[x]["@id"];
-                  var attName = Attdata.attachment[x]["@name"];
-                  var attSize = Attdata.attachment[x]["@size"];
-                  var attMediaType = Attdata.attachment[x]["@mediaType"];
-                  var attSubType = Attdata.attachment[x]["@subType"];
-                  var attEncoding = Attdata.attachment[x]["@encoding"];
-                  
-                  strnamesatt += attName + ', ';
-                }
-                strnamesatt = strnamesatt.substr(0, strnamesatt.length-2);
-                var intnumberatt = Attdata.attachment.length;
-            }
-            //Just one attachment
-            else{
-                var attId = Attdata.attachment["@id"];
-                var attName = Attdata.attachment["@name"];
-                var attSize = Attdata.attachment["@size"];
-                var attMediaType = Attdata.attachment["@mediaType"];
-                var attSubType = Attdata.attachment["@subType"];
-                var attEncoding = Attdata.attachment["@encoding"];
-                strnamesatt = attName;
-                var intnumberatt = 1;
-            }
-            
-            tplAttatch += 's:'+strnamesatt.length+':"'+strnamesatt+'";'
-+               's:18:"number_attachments";i:' +intnumberatt+';'
-+           '}';
-        }
-        else {
-            tplAttatch = 'a:2:{s:5:"names";b:0;s:18:"number_attachments";i:0;}';    
-        }
-    }
-    //No attatch data
-    else {
-        tplAttatch = 'a:2:{s:5:"names";b:0;s:18:"number_attachments";i:0;}';
-    }
-    return(tplAttatch);
-}
-
-MailArchiver.prototype.toExpressoDate = function(data){
-    var ndate = new Date(data);
-    //ndate.setTime(data);
-    var odate = zeroFill(ndate.getDate()) + '/' + zeroFill(ndate.getMonth()+1) + '/' + zeroFill(ndate.getFullYear() + ' ' + zeroFill(ndate.getHours()) + ':' + zeroFill(ndate.getMinutes()) + ':' + zeroFill(ndate.getSeconds()));
-    return(odate);
-}
-
-MailArchiver.prototype.toExpressoFlags = function(data){
-    if((data.length > 0) && (typeof(data) != 'undefined')){
-        var Tagdata = eval("(" + data + ")");
-        //More than 1 attachment - because length will be 'undefined' if just one exists
-        if(Tagdata.tag.length >= 0){
-            var strnamestag = "";
-            for(var x=0; x<Tagdata.tag.length; x++){
-                var tagvalue = Tagdata.tag[x]["@value"];
-                if(tagvalue.toLowerCase().indexOf('recent')!= -1)
-                    strnamestag += 's:6:"Recent";s:1:"N";';
-                if(tagvalue.toLowerCase().indexOf('unseen')!= -1)
-                    strnamestag += 's:6:"Unseen";s:1:"U";';
-                if(tagvalue.toLowerCase().indexOf('answered')!= -1)
-                    strnamestag += 's:8:"Answered";s:1:"A";';
-                if(tagvalue.toLowerCase().indexOf('forwarded')!= -1)
-                    strnamestag += 's:9:"Forwarded";s:1:"F"';
-                if(tagvalue.toLowerCase().indexOf('draft')!= -1)
-                    strnamestag += 's:5:"Draft";s:1:"X";';
-                if(tagvalue.toLowerCase().indexOf('importance')!= -1)
-                    strnamestag += 's:10:"Importance";s:6:"High";';
-                if(tagvalue.toLowerCase().indexOf('flagged')!= -1)
-                    strnamestag += 's:7:"Flagged";s:1:"F";';                       
-            } 
-            var tplTags = strnamestag;
-        }
-        else{
-            var strnamestag = "";
-            var tagvalue = Tagdata.tag["@value"];
-            if(tagvalue.toLowerCase().indexOf('recent')!= -1)
-                strnamestag += 's:6:"Recent";s:1:"N";';
-            if(tagvalue.toLowerCase().indexOf('unseen')!= -1)
-                strnamestag += 's:6:"Unseen";s:1:"U";';
-            if(tagvalue.toLowerCase().indexOf('answered')!= -1)
-                strnamestag += 's:8:"Answered";s:1:"A";';
-            if(tagvalue.toLowerCase().indexOf('forwarded')!= -1)
-                strnamestag += 's:9:"Forwarded";s:1:"F"';
-            if(tagvalue.toLowerCase().indexOf('draft')!= -1)
-                strnamestag += 's:5:"Draft";s:1:"X";';
-            if(tagvalue.toLowerCase().indexOf('importance')!= -1)
-                strnamestag += 's:10:"Importance";s:6:"High";';
-            if(tagvalue.toLowerCase().indexOf('flagged')!= -1)
-                strnamestag += 's:7:"Flagged";s:1:"F";'; 
-            var tplTags = strnamestag;
-        }
-    }
-    else{
-        tplTags = 's:6:"Recent";s:1:" ";s:6:"Unseen";s:1:" ";s:8:"Answered";s:1:" ";s:9:"Forwarded";s:1:" ";s:5:"Draft";s:1:" ";s:10:"Importance";s:6:"Normal";s:7:"Flagged";b:0;';
-    }
-    return(tplTags);
-}
-
-MailArchiver.prototype.toExpressoHeader = function(data){   
-    var tplHeader = 'a:16:{'
-+           's:11:"ContentType";'
-+           's:' + data.getContentType().length + ':"' + data.getContentType() + '";'
-+           expresso_mail_archive.toExpressoFlags(data.getTags())
-+           's:10:"msg_number";'
-+           's:'+data.getId().length+':"'+data.getId()+'";'
-+           's:5:"udate";'
-+           'i:' + parseInt(data.getDate()/1000) + ';'
-+           's:9:"timestamp";'
-+           'i:' + parseInt(data.getDate()/1000) + ';'
-+           's:4:"from";'
-+           expresso_mail_archive.toExpressoAddress(data.getFrom())
-+           's:2:"to";'
-+           expresso_mail_archive.toExpressoAddress(data.getTo())
-+           's:7:"subject";'
-+           expresso_mail_archive.toExpressoSubject(data.getSubject())
-+           's:4:"Size";'
-+           'i:'+data.getSize()+';'
-+           's:10:"attachment";'
-+           expresso_mail_archive.toExpressoAttachmentsHeader(data.getAttachments())
-+           '}';
-    //window.alert('Serialized data\n\n' + tplHeader + '\ngmtoffset: ' + gmtoffset);
-    return(tplHeader);
-}
-
-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
- **/    
-}
-
+
+MailArchiver.prototype.getMessageServiceObject = function(msgid){
+    for(var i=0; i < expresso_mail_archive.ServiceReturnObject.length; i++){
+        if (expresso_mail_archive.ServiceReturnObject[i].getId() == msgid){
+            return(expresso_mail_archive.ServiceReturnObject[i]);
+        }
+    }
+    return(null);
+}
 
 MailArchiver.prototype.Query = function(offlow, offup, qdata){
