2 var MBstrBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"].
3 getService(Components.interfaces.nsIStringBundleService);
4 var mboximportbundle = MBstrBundleService.createBundle("chrome://openerp_plugin/locale/mboximport.properties");
7 function saveMsgAsEML(msguri,file,append,uriArray,hdrArray,fileArray) {
14 QueryInterface : function(iid) {
15 if (iid.equals(Components.interfaces.nsIStreamListener) ||
16 iid.equals(Components.interfaces.nsIMsgHeaderSink) ||
17 iid.equals(Components.interfaces.nsISupports))
20 throw Components.results.NS_NOINTERFACE;
24 onStartRequest : function (aRequest, aContext) {
25 this.scriptStream = Components.classes['@mozilla.org/binaryinputstream;1'].createInstance(Components.interfaces.nsIBinaryInputStream);
28 onStopRequest : function (aRequest, aContext, aStatusCode) {
29 this.scriptStream = null;
32 if (this.emailtext != "") {
33 var data = this.emailtext + "\n";
34 // Some Imap servers don't add to the message the "From" prologue
35 if (data && ! data.match(/^From/)) {
37 var prologue = "From - " + now.toString() + "\n";
40 data = IETescapeBeginningFrom(data);
42 var fileClone = file.clone();
43 IETwriteDataOnDisk(fileClone,data,true,this.sub,null);
47 var sub = getSubjectForHdr(hdr);
49 var parts = hdrArray[IETexported].split("§][§^^§");
53 var data = this.emailtext.replace(/^From.+\r?\n]/, "");
54 data = IETescapeBeginningFrom(data);
55 var clone = file.clone();
56 // The name is taken from the subject "corrected"
57 clone.append(sub+".eml");
58 clone.createUnique(0,0644);
59 var time = (hdr.dateInSeconds)*1000;
60 IETwriteDataOnDisk(clone,data,false,null,time);
62 IETexported = IETexported + 1;
63 IETwritestatus(mboximportbundle.GetStringFromName("exported")+" "+IETexported+" "+mboximportbundle.GetStringFromName("msgs")+" "+IETtotal);
65 if (IETexported < IETtotal) {
67 var nextUri = uriArray[IETexported];
68 var nextFile = fileArray[IETexported];
70 else if (! hdrArray) {
71 var nextUri = uriArray[IETexported];
75 parts = hdrArray[IETexported].split("§][§^^§");
76 var nextUri = parts[5];
79 saveMsgAsEML(nextUri,nextFile,append,uriArray,hdrArray,fileArray);
87 onDataAvailable : function (aRequest, aContext, aInputStream, aOffset, aCount) {
88 this.scriptStream.setInputStream(aInputStream);
89 var chunk = this.scriptStream.readBytes(aCount);
90 this.emailtext += chunk;
93 var mms = messenger.messageServiceFromURI(msguri).QueryInterface(Components.interfaces.nsIMsgMessageService);
94 var hdr = mms.messageURIToMsgHdr(msguri);
95 mms.streamMessage(msguri, myEMLlistner, msgWindow, null, false, null);
99 function nametoascii(str) {
100 if (! gPrefBranch.getBoolPref("mboximport.export.filenames_toascii")) {
101 str = str.replace(/[\x00-\x19]/g,"_");
102 return str.replace(/[\/\\:,<>*\?\"\|]/g,"_");
105 str = str.replace(/[^a-zA-Z0-9]/g,"_");
107 str = "Undefinied_or_empty";
112 function IETwriteDataOnDisk(file,data,append,name,time) {
113 var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
114 .createInstance(Components.interfaces.nsIFileOutputStream);
117 foStream.init(file, 0x02 | 0x08 | 0x10, 0664, 0); // write, create, append
120 foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
122 foStream.write(data,data.length);
124 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
125 .getService(Components.interfaces.nsIPrefBranch);
126 if (time && prefs.getBoolPref("mboximport.export.set_filetime"))
127 file.lastModifiedTime = time;
132 function IETescapeBeginningFrom(data) {
133 // Workaround to fix the "From " in beginning line problem in body messages
134 // See https://bugzilla.mozilla.org/show_bug.cgi?id=119441 and
135 // https://bugzilla.mozilla.org/show_bug.cgi?id=194382
136 // TB2 has uncorrect beahviour with html messages
137 // This is not very fine, but I didnt' find anything better...
138 var datacorrected = data.replace(/\nFrom /g, "\n From ");
139 return datacorrected;
143 function getPredefinedFolder(type) {
144 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
145 .getService(Components.interfaces.nsIPrefBranch);
147 // type 1 = all messages
148 // type 2 = selected messages
150 var use_dir = "mboximport.exportMSG.use_dir";
153 if(navigator.userAgent.indexOf('Linux')!= -1){
156 else if(navigator.userAgent.indexOf('Win')!= -1){
159 else if(navigator.userAgent.indexOf('Mac OS X')!= -1){
163 var localFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
164 localFile.initWithPath(dir_path);
173 function getSubjectForHdr(hdr) {
174 var emlNameType = gPrefBranch.getIntPref("mboximport.exportEML.filename_format");
175 var mustcorrectname = gPrefBranch.getBoolPref("mboximport.export.filenames_toascii");
176 var subMaxLen = gPrefBranch.getIntPref("mboximport.subject.max_length")-1;
177 if (hdr.mime2DecodedSubject)
179 var subj = hdr.mime2DecodedSubject.substring(0, subMaxLen);
182 subj = nametoascii(subj);
183 var dateInSec = hdr.dateInSeconds;
184 var msgDate8601string = dateInSecondsTo8601(dateInSec);
185 var key = hdr.messageKey;
186 if (emlNameType == 2) {
187 var pattern = gPrefBranch.getCharPref("mboximport.export.filename_pattern");
188 pattern = pattern.replace("%s",subj);
189 pattern = pattern.replace("%k",key);
190 pattern = pattern.replace("%d", msgDate8601string);
191 pattern = pattern.replace(/-%e/g, "");
192 pattern = pattern.replace(/[\x00-\x19]/g,"_");
197 var fname = msgDate8601string+"-"+subj+"-"+hdr.messageKey;
200 fname = fname.replace(/[\x00-\x19]/g,"_");
202 fname = nametoascii(fname);
204 fname = fname.replace(/[\/\\:,<>*\?\"\|]/g,"_");
208 function dateInSecondsTo8601(secs) {
209 var addTime = gPrefBranch.getBoolPref("mboximport.export.filenames_addtime");
210 var msgDate = new Date(secs*1000);
211 var msgDate8601 = msgDate.getFullYear();
212 if (msgDate.getMonth() < 9)
213 var month = "0"+(msgDate.getMonth()+1);
215 var month = msgDate.getMonth()+1;
216 if (msgDate.getDate() < 10)
217 var day = "0"+ msgDate.getDate();
219 var day = msgDate.getDate();
220 var msgDate8601string = msgDate8601.toString()+month.toString()+day.toString();
221 if (addTime && gPrefBranch.getIntPref("mboximport.exportEML.filename_format") == 2) {
222 if (msgDate.getHours() < 10)
223 var hours = "0"+msgDate.getHours();
225 var hours = msgDate.getHours();
226 if (msgDate.getMinutes() < 10)
227 var min = "0"+msgDate.getMinutes();
229 var min = msgDate.getMinutes();
230 msgDate8601string += "-"+ hours.toString() + min.toString();
232 return msgDate8601string;
235 function IETwritestatus(text) {
236 document.getElementById("statusText").setAttribute("label", text);
237 var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
238 var delay = prefs.getIntPref("mboximport.delay.clean_statusbar");
240 window.setTimeout(function(){IETdeletestatus(text);}, delay);
243 function IETdeletestatus(text) {
244 if (document.getElementById("statusText").getAttribute("label") == text)
245 document.getElementById("statusText").setAttribute("label", "");