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 4658)
+++ sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/js/mail_archiver.js	(revision 4658)
@@ -0,0 +1,534 @@
+/* 
+ * Mail Archive JS API
+ * 
+ * This JavaScript file is the core to use MailArchiver embeded at Expresso suite.
+ * It contains all the resources used to handle local messages stored at the brand
+ * new solution Mail Archiver (a embeded application server running at client side).
+ *
+ * This feature is a replacement for Google Gears(obsolete by now), used by
+ * Expresso to store local messages at user workstation hard disk.
+ * 
+ * @author Fernando Wendt [fernando-alberto.wendt@serpro.gov.br]
+ *
+ * @status under development
+ */
+
+var sessid = 'sessionId';
+
+function IterateIt(){
+    var deph = 0;
+    var returnstr = "";
+
+    for (i in arguments[0]){
+      if((arguments[0][i]) && (arguments[0][i].visited == null)){
+          arguments[0][i].visited = true;
+
+          if(typeof(arguments[0][i]) == 'object'){
+            returnstr += '+ Property "' + i + '"[object ' + deph + ']\n<br>';
+            //returnstr += IterateIt(arguments[0][i]);
+            deph++;
+          }
+      }
+    else returnstr += 'Property "' + i + '" -> ' + arguments[0][i] + ' [' + typeof(i) + ']\n<br>';
+    }
+    return (returnstr);
+}
+
+function dump(arr,level) {
+	var dumped_text = "";
+	if(!level) level = 0;
+	
+	//The padding given at the beginning of the line.
+	var level_padding = "";
+	for(var j=0;j<level+1;j++) level_padding += "    ";
+	
+	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
+		for(var item in arr) {
+			var value = arr[item];
+			
+			if(typeof(value) == 'object') { //If it is an array,
+				dumped_text += level_padding + "'" + item + "' ...\n<br>";
+				dumped_text += dump(value,level+1);
+			} else {
+				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n<br>";
+			}
+		}
+	} else { //Stings/Chars/Numbers etc.
+		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
+	}
+	return dumped_text;
+}
+
+//Main object structure: object property data definition
+function MailArchiver() { 
+    this.enabled = null;
+    this.interval = 500;
+    this.timer = null;
+    this.counter = 0;
+    this.message_list = new Array();
+    this.total_messages = 0;
+    this.messages_processed = 0;
+    this.messages_fail = 0;
+    this.folder_origin = null;
+    this.folder_destination = null;
+    this.folders = null;
+    this.currentfolder = 'local_root';
+    this.drawdata = null;
+    this.onprocess = false;
+}
+
+//Invoked at some possible MA crash issue
+MailArchiver.prototype.resetObject = function(){
+    expresso_mail_archive.message_list = new Array();
+    expresso_mail_archive.total_messages = 0;
+    expresso_mail_archive.messages_processed = 0;
+    expresso_mail_archive.messages_fail = 0;
+    expresso_mail_archive.folder_origin = null;
+    expresso_mail_archive.folder_destination = null;
+    expresso_mail_archive.folder = null;
+}
+
+/*SystemCheck method: test if MailArchive is installed and/or running at user workstation
+*Try to get a instance of ArchiveServices object - the handler of services provided by
+*the MailArchive services provider interface, from user workstation.
+*/
+MailArchiver.prototype.SystemCheck = function(){
+    expresso_mail_archive.enabled = ((ArchiveServices) ? true : false);
+}
+
+//Turns on the listener timer to check services availability
+MailArchiver.prototype.ActivateStatusListener = function(obj){
+    if (document.getElementById('mail_archiver_retry'))
+        tem_tag = true;
+    else
+        tem_tag = false;
+    
+    try{
+        if (obj.enabled){
+            obj.interval = 500; //set default timer to 1ms (imediate lauching)
+            draw_footer_box(get_current_folder());
+            obj.getServicesStatus(obj);
+        }
+        else{
+            throw "this is not enabled: " + obj;
+            window.alert('Nope: obj.enabled is not true...');
+        }
+    }
+    catch (e){
+        window.alert('ActivateStatusListener error: ' + e);
+    }
+}
+
+//Turns off the service status listener, at a crash issue
+MailArchiver.prototype.DeactivateStatusListener = function(obj){
+    window.clearInterval(obj.timer);
+    obj.enabled = null;
+    obj.timer=null;
+    obj.interval = 500;
+    ArchiveServices = null;
+    connector.purgeCache();
+    
+    draw_footer_box(get_current_folder());
+    auto_refresh();
+
+    if(document.getElementById('mail_archiver_retry')){
+        document.getElementById('mail_archiver_retry').parentNode.removeChild(document.getElementById('mail_archiver_retry'));
+    }
+    else{
+        //to do, by now, means nothing
+    }
+}
+
+//ServiceStatus callback OK
+MailArchiver.prototype.getServicesStatusOK = function(serviceData){
+    if ((serviceData.getReturn() == "STARTED") || (serviceData.getReturn() == "RUNNING")){
+        //If there is no timer activated to services check interval, set it up.
+        if(expresso_mail_archive.enabled){
+           if(expresso_mail_archive.timer == null){
+                expresso_mail_archive.interval = 5000;
+                expresso_mail_archive.timer = window.setInterval(expresso_mail_archive.getServicesStatus, expresso_mail_archive.interval);
+           }
+        }
+        else{
+            window.clearInterval(expresso_mail_archive.timer);
+            write_msg(get_lang('Your preferences are setting to use local messages, but Mail Archiver does not seems to be running or installed at this workstation. By now, we are turning off this feature. Check it out!'),true);
+        }
+    }
+    else{
+        expresso_mail_archive.enabled = false;
+        window.clearInterval(expresso_mail_archive.timer);
+        write_msg(get_lang('Your preferences are setting to use local messages, but Mail Archiver does not seems to be running or installed at this workstation. By now, we are turning off this feature. Check it out!'),true);
+    }
+}
+
+//Dettach resources handler
+MailArchiver.prototype.DeactivateResources = function(obj){   
+    if(obj.timer)
+        window.clearInterval(obj.timer);
+    write_msg(get_lang('3Mail Archiver is not responding. There is some communicating issue hang it up. Some services may not work properly. Check it out!'));
+    obj.DeactivateStatusListener(obj);
+    return;
+}
+
+//Services Status Fail callback
+MailArchiver.prototype.getServicesStatusFailure = function(serviceData){
+    window.alert('getServicesStatusFailure feature: ' + serviceData);
+}
+
+//Service Status handler
+MailArchiver.prototype.getServicesStatus = function(obj){
+    try{
+        if(ArchiveServices){
+            try{
+                ArchiveServices.getState(expresso_mail_archive.getServicesStatusOK, expresso_mail_archive.getServicesStatusFailure, "true");
+            }catch (e){
+                throw "Service failure status: getState";
+            }
+        }
+        else {
+            throw "No ArchiveServices object present. Sorry, but leaving now...";
+        }
+    }catch (e){
+        window.alert('getServiceStatus exception:' + e);
+    }
+    expresso_mail_archive.counter = expresso_mail_archive.counter +1;
+}
+
+/*Restart method: re-initializes all the Mail Archive structure
+ **/
+MailArchiver.prototype.Restart = function(obj){
+    window.alert('Trying reboot...');
+
+    obj.SystemCheck();
+    obj.ActivateStatusListener(obj);
+    //tree_folders.getNodeById('local_root')._refresh();
+    auto_refresh();
+}
+
+/*Startup method: initializes all the Mail Archive structure to work so far as possible
+ *Looks like the old 'gears_init', from Gears
+ **/
+MailArchiver.prototype.Startup = function(){
+    this.SystemCheck();
+}
+
+MailArchiver.prototype.CreateMessageList = function(msg_list){
+    if((msg_list != null) && (msg_list != "") && (msg_list != " ")){
+        if (expresso_mail_archive.message_list.length == 0){
+            //Get all the messages ids by pass at msgs_id to "message_list" object array propertie - if more than one exists
+            if(msg_list.indexOf(',') != -1){
+                var tmp_list = msg_list.split(',');
+                for(var i=0; i < tmp_list.length; i++){
+                    expresso_mail_archive.message_list.push(tmp_list[i]);
+                }
+            }
+            //Push message list to process the only one
+            else expresso_mail_archive.message_list.push(msg_list);
+        }
+    }
+    //msg_list is corrupted. aborting population
+    else expresso_mail_archive.message_list = new Array();
+}
+
+//Archive Operation
+MailArchiver.prototype.Archive = function(source_folder,destination_folder,msgs_id){
+    window.alert('tentando arquivar a mensagem [' + msgs_id + '], do folder ['+source_folder+'] para a pasta destino ['+destination_folder+']');
+    try{
+        document.getElementById('main_title').innerHTML = get_lang("Starting to archive messages");
+        expresso_mail_archive.CreateMessageList(msgs_id);
+
+        if((expresso_mail_archive.message_list.length > 0) && (expresso_mail_archive.message_list[0] != '')){
+
+            //Sets the message counter
+            expresso_mail_archive.total_messages = expresso_mail_archive.message_list.length;
+            
+            //Sets the folders properties: destination and origin
+            expresso_mail_archive.folder_origin = source_folder;
+            window.alert('substr(0,5) = ' + destination_folder.substr(0,5));
+            if(destination_folder.substr(0,5) == 'local')
+                expresso_mail_archive.folder_destination = destination_folder.substr(6,destination_folder.length);
+            else
+                expresso_mail_archive.folder_destination = destination_folder;
+            
+            window.alert('destination_folder = ' + expresso_mail_archive.folder_destination);
+
+            var arch_handler = function(data){
+                //Store the message source from Ajax request by now (string data)
+                var js_var = new String(data);
+                try{
+                    //Archive services needs session id, message source and destination folder
+                    document.getElementById('main_title').innerHTML = get_lang("Archiving message on folder") + " " + expresso_mail_archive.folder_destination +"...";
+                    ArchiveServices.archive(expresso_mail_archive.getArchiveOperationOK, expresso_mail_archive.getArchiveOperationFailure, sessid, data ,expresso_mail_archive.folder_destination);
+                }
+                catch(e){
+                    window.alert('ArchiveServices.archive eror: ' + e);
+                }
+            }
+            
+            document.getElementById('main_title').innerHTML = get_lang("Archiving messages " + expresso_mail_archive.messages_processed + " of " + expresso_mail_archive.message_list.length);
+            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!');
+    }
+    catch(e){
+        window.alert('Archive error: ' + e);
+    }
+}
+
+//Archive callback OK
+MailArchiver.prototype.getArchiveOperationOK = function(status_message){
+    expresso_mail_archive.messages_processed++;
+    if(expresso_mail_archive.message_list.length > 1){
+       document.getElementById('main_title').innerHTML = get_lang("Message " + expresso_mail_archive.messages_processed + " of " + expresso_mail_archive.total_messages + " successfully archived.");
+       expresso_mail_archive.message_list.shift();
+       expresso_mail_archive.Archive(expresso_mail_archive.folder_origin, expresso_mail_archive.folder_destination, expresso_mail_archive.message_list);
+    }
+    else{
+        document.getElementById('main_title').innerHTML = get_lang("All done. End of archive messages operation.");
+        expresso_mail_archive.resetObject();
+        window.setTimeout("eval('document.getElementById(\"main_title\").innerHTML =\"Expresso Mail\"')",3000);
+    }
+}
+//Archive callback Fail
+MailArchiver.prototype.getArchiveOperationFailure = function(error_message){
+    window.alert('Hi! getArchiveOperationFailure comes with ' + error_message.getReturn());
+    window.setTimeout("eval('document.getElementById(\"main_title\").innerHTML =\"Expresso Mail\"')",3000);
+}
+
+//getFolderList Operation
+MailArchiver.prototype.getFoldersList = function(basefolder){   
+    try{
+        ArchiveServices.listFolders(expresso_mail_archive.getFoldersListOperationOK, expresso_mail_archive.getFoldersListOperationFailure, sessid, basefolder);
+    }
+    catch (e){
+        window.alert('Ops...List folders fails at:\n' + e.description);
+    } 
+}
+
+//getFolderList callback OK
+MailArchiver.prototype.getFoldersListOperationOK = function(folderlist){
+    //Internal variable used to map all XML data return from WS invokated
+    var lfolders = new Array();
+    
+    //Mapping XML data to a handler data structure
+    if(folderlist.getReturn().length > 0){
+        var lfolders = new Array();
+        for(i=0; i<folderlist.getReturn().length; i++){
+            (folderlist.getReturn()[i].getFolderCount() > 0 ) ? folderChild = 1 : folderChild = 0;
+
+            //Store folder data at this format: {folder name, number of messages contained, folder has child nodes, folder id, folder parent id, folder full path}
+            //var folderData = new Array(folderlist.getReturn()[i].getName(), folderlist.getReturn()[i].getMessageCount(), folderChild, folderlist.getReturn()[i].getId(), folderlist.getReturn()[i].getParentId(), folderlist.getReturn()[i].getPath());
+            var folderData = {name: folderlist.getReturn()[i].getName(), messages: folderlist.getReturn()[i].getMessageCount(), haschild: folderChild, id: folderlist.getReturn()[i].getId(), parentid: folderlist.getReturn()[i].getParentId(), path: folderlist.getReturn()[i].getPath()};
+            lfolders.push(folderData);
+        }
+
+        //Sets folders property at main object
+        expresso_mail_archive.folders = lfolders;
+        
+        if(expresso_mail_archive.folders[0]["parentid"] == "")
+            expresso_mail_archive.currentfolder = 'local_root';
+        else
+            expresso_mail_archive.currentfolder = expresso_mail_archive.folders[0]["parentid"];
+        
+        //Folders dumping :)
+        /*var strFolders = '---';
+        for (var w=0; w < expresso_mail_archive.folders.length; w++){
+            strFolders += '\nFolder ' + w + ' -->';
+            for (x in expresso_mail_archive.folders[w]){
+                strFolders += '\n----------' + x + ':= ' + expresso_mail_archive.folders[w][x];
+            }
+            strFolders += '\n<---';
+        }
+        strFolders += '\n---';
+        alert(lfolders.length + ' folders mapeados, com default = ' + expresso_mail_archive.currentfolder + '\nfolders list:\n\n' + strFolders);
+        */
+
+        //If something UI related have been flagged, handle it
+        if (expresso_mail_archive.drawdata){
+            expresso_mail_archive.drawFolderTree();
+        }
+    }
+}
+
+//getFolderList callback Fail
+MailArchiver.prototype.getFoldersListOperationFailure = function(error, http_msg){
+    window.alert('List folders mistake:' + error + '\nhttp_error = ' + http_msg);
+}
+
+//drawFolderTree operation, if requested to
+MailArchiver.prototype.drawFolderTree = function(){
+    var localBaseName  = 'local_';
+    var objectTree = expresso_mail_archive.drawdata.treeObject;
+    var evalobjectTree = eval(expresso_mail_archive.drawdata.treeName);
+   
+    //Add nodes to tree
+    if(expresso_mail_archive.folders.length > 0){
+        for(i=0; i<expresso_mail_archive.folders.length; i++){
+            var folder_caption;
+            
+            //Special folders treatment: Inbox, Outbox, Drafts, Sent and Trash folders
+            if((expresso_mail_archive.folders[i]["name"] == 'Inbox') || (expresso_mail_archive.folders[i]["name"] == 'Outbox') || (expresso_mail_archive.folders[i]["name"] == 'Drafts') || (expresso_mail_archive.folders[i]["name"] == 'Sent') || (expresso_mail_archive.folders[i]["name"] == 'Trash')){
+                folder_caption = get_lang(expresso_mail_archive.folders[i]["name"]);
+            }
+            else{
+                folder_caption = expresso_mail_archive.folders[i]["name"];
+            }
+            
+            var n_demo = new dNode({id: localBaseName + expresso_mail_archive.folders[i]["id"], caption: folder_caption, onClick: "change_folder('"+ localBaseName + expresso_mail_archive.folders[i]["id"] +"','"+ localBaseName + expresso_mail_archive.folders[i]["id"]+"', '" + expresso_mail_archive.drawdata.treeName + "')", plusSign:expresso_mail_archive.folders[i]["haschild"]});
+            
+            //Adjust the id node names
+            if(expresso_mail_archive.currentfolder.substr(0,5) != 'local'){
+                evalobjectTree.add(n_demo, localBaseName + expresso_mail_archive.currentfolder);
+            }
+            else{
+                evalobjectTree.add(n_demo,expresso_mail_archive.currentfolder);
+            }
+        }
+    }
+}
+
+//getFaultInfo operation
+MailArchiver.prototype.getFaultInfo = function(){
+    try{
+        ArchiveServices.getFaultInfo(expresso_mail_archive.getFaultInfoOperationOK, expresso_mail_archive.getFaultInfoOperationFailure, sessid);
+    }    
+    catch(e){
+        write_msg(get_lang('getFaultInfo invoke fails = ' + e),true);
+    }
+}
+
+//getFaultInfo callback OK
+MailArchiver.prototype.getFaultInfoOperationOK = function(faultinfo){
+    try{
+        write_msg(get_lang('MailArchiver remote service reports the following error:' + faultinfo.getReturn().getSoapFaultString(), true));
+    } catch(e){
+        write_msg(get_lang('Ops! It was not possible to capture service error data.', true));
+    }
+}
+
+//getFaultInfo callback Fail
+MailArchiver.prototype.getFaultInfoOperationFailure = function(errorCode, errorDesc){
+    try{
+        write_msg(get_lang('SoapFault capture fails at:' + errorCode + ' | ' + errorDesc, true));
+    } catch(e){
+        write_msg(get_lang('Service error mapping', true));
+    }    
+}
+
+//createFolder operation
+MailArchiver.prototype.createFolder = function(parentFolder, folderName){
+    try{
+        ArchiveServices.createFolder(expresso_mail_archive.createFolderOperationOK, expresso_mail_archive.createFolderOperationFailure, sessid, parentFolder, folderName);
+    }
+    catch (e){
+        window.alert('Ops...create folder fails at:\n' + e);
+    } 
+    
+}
+
+//createFolder callback OK
+MailArchiver.prototype.createFolderOperationOK = function (folderObject){
+    draw_tree_local_folders();
+    ttreeBox.update_folder();    
+}
+
+//createFolder callback Fail
+MailArchiver.prototype.createFolderOperationFailure = function (message){
+    window.alert('Folder creation fails...\n->' + message.getReturn()); 
+}
+
+//deleteFolder operation
+MailArchiver.prototype.deleteFolder = function(folderName){
+    try{
+        ArchiveServices.deleteFolder(expresso_mail_archive.deleteFolderOperationOK, expresso_mail_archive.deleteFolderOperationFailure, sessid, folderName, true);
+    }
+    catch (e){
+        window.alert('Ops...delete folder fails at:\n' + e);
+    } 
+    
+}
+
+//deleteFolder callback OK
+MailArchiver.prototype.deleteFolderOperationOK = function (folderObject){
+    var fo = IterateIt(folderObject);
+    write_msg(get_lang("The folder " + expresso_mail_archive.drawdata.folderName + " was successfully removed"));
+    draw_tree_local_folders();
+    ttreeBox.update_folder();	
+}
+
+//deleteFolder callback Fail
+MailArchiver.prototype.deleteFolderOperationFailure = function (message){
+    window.alert('Folder deletion fails...\n->' + message.getReturn()); 
+}
+
+//renameFolder operation
+MailArchiver.prototype.renameFolder = function(folderId, newFolderName){
+    try{
+        ArchiveServices.renameFolder(expresso_mail_archive.renameFolderOperationOK, expresso_mail_archive.renameFolderOperationFailure, sessid, folderId, newFolderName);
+    }
+    catch (e){
+        window.alert('Ops...renaming folder fails at:\n' + e);
+    } 
+    
+}
+
+//renameFolder callback OK
+MailArchiver.prototype.renameFolderOperationOK = function (returnService){
+    var evalobjectTree = eval(expresso_mail_archive.drawdata.treeName);
+    evalobjectTree.update_folder();
+}
+
+//renameFolder callback Fail
+MailArchiver.prototype.renameFolderOperationFailure = function (ServiceFault){
+    window.alert('Folder rename fails...\n->' + ServiceFault.getReturn()); 
+    alert(get_lang("cannot rename folder. try other folder name"));
+}
+
+//getFolderInfo operation
+MailArchiver.prototype.getFolderInfo = function (folderId){
+    try{
+        ArchiveServices.getFolderInfo(expresso_mail_archive.getFolderInfoOperationOK, expresso_mail_archive.getFolderInfoOperationFailure, sessid, folderId);
+    }
+    catch (e){
+        window.alert('Ops...getFolderInfo fails at:\n' + e);
+    }     
+}
+
+//getFolderInfo callback OK
+MailArchiver.prototype.getFolderInfoOperationOK = function(returnService){
+   var folder_info = {id : returnService.getReturn().getId(), name : returnService.getReturn().getName(), parent : returnService.getReturn().getParentId(), numfolders: returnService.getReturn().getFolderCount(), nummessages: returnService.getReturn().getMessageCount()};
+   expresso_mail_archive.folder = folder_info;
+}
+
+//getFolderInfo callback Fail
+MailArchiver.prototype.getFolderInfoOperationFailure = function (ServiceFault){
+    window.alert('getFolderInfo service fails...\n->' + ServiceFault.getReturn()); 
+}
+
+//listMessages operation
+MailArchiver.prototype.listMessages = function(listQuery){
+    try{
+        ///ArchiveServices.listMessages()
+    }
+    catch (e){
+        window.alert('Ops...listMessages fails at:\n' + e);
+    }
+}
+
+//listMessages callback OK
+MailArchiver.prototype.listMessagesOperationOK = function(returnService){
+    
+}
+
+//listMessages callback Fail
+MailArchiver.prototype.listMessagesOperationFailure = function(serviceFault){
+    
+}
+
+/*
+ *The all pourpose JavaScript variable, used by all related functionalityes at
+ *Expresso Mail to handle this object at user gui.
+ */
+
+var expresso_mail_archive;
+expresso_mail_archive = new MailArchiver();
