1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// JavaScript
"use strict";
// Assuming that Cc, Ci and Cu are already loaded
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm", {});
//ChromeUtils.import("resource://gre/modules/osfile.jsm")
const oFileHandler = {
// https://developer.mozilla.org/fr/docs/Mozilla/JavaScript_code_modules/OSFile.jsm/OS.File_for_the_main_thread
xDataFolder: null,
prepareDataFolder: function () {
let xDirectoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
// this is a reference to the profile dir (ProfD) now.
let xExtFolder = xDirectoryService.get("ProfD", Ci.nsIFile);
xExtFolder.append("grammalecte-data");
if (!xExtFolder.exists() || !xExtFolder.isDirectory()) {
// read and write permissions to owner and group, read-only for others.
xExtFolder.create(Ci.nsIFile.DIRECTORY_TYPE, 774);
}
this.xDataFolder = xExtFolder;
},
createPathFileName: function (sFilename) {
let spfDest = this.xDataFolder.path;
spfDest += (/^[A-Z]:/.test(this.xDataFolder.path)) ? "\\" + sFilename : "/" + sFilename;
return spfDest;
},
loadFile: async function (sFilename) {
if (!this.xDataFolder) {
this.prepareDataFolder();
}
try {
let array = await OS.File.read(this.createPathFileName(sFilename));
let xDecoder = new TextDecoder();
return xDecoder.decode(array);
}
catch (e) {
console.error(e);
return null;
}
},
loadAs: function (callback) {
let xFilePicker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
xFilePicker.init(window, "Charger fichier", Ci.nsIFilePicker.modeOpen);
xFilePicker.appendFilters(Ci.nsIFilePicker.filterAll | Ci.nsIFilePicker.filterText);
xFilePicker.open(async function (nReturnValue) {
if (nReturnValue == Ci.nsIFilePicker.returnOK || nReturnValue == Ci.nsIFilePicker.returnReplace) {
console.log(xFilePicker.file.path);
try {
let array = await OS.File.read(xFilePicker.file.path);
let xDecoder = new TextDecoder();
callback(xDecoder.decode(array));
}
catch (e) {
console.error(e);
callback(null);
}
}
});
},
saveFile: function (sFilename, sData) {
if (!this.xDataFolder) {
this.prepareDataFolder();
}
let xEncoder = new TextEncoder();
let xEncodedRes = xEncoder.encode(sData);
console.log("save dictionary: " + this.createPathFileName(sFilename));
OS.File.writeAtomic(this.createPathFileName(sFilename), xEncodedRes);
//OS.File.writeAtomic(this.createPathFileName(sFilename), xEncodedRes, {tmpPath: "file.txt.tmp"}); // error with a temporary file (can’t move it)
},
deleteFile: function (sFilename) {
if (!this.xDataFolder) {
this.prepareDataFolder();
}
OS.File.remove(this.createPathFileName(sFilename), {ignoreAbsent: true});
},
saveAs: function (sData) {
// save anywhere with file picker
let xFilePicker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
xFilePicker.init(window, "Enregistrer sous", Ci.nsIFilePicker.modeSave);
xFilePicker.appendFilters(Ci.nsIFilePicker.filterAll | Ci.nsIFilePicker.filterText);
xFilePicker.open(function (nReturnValue) {
if (nReturnValue == Ci.nsIFilePicker.returnOK || nReturnValue == Ci.nsIFilePicker.returnReplace) {
let xEncoder = new TextEncoder();
let xEncodedRes = xEncoder.encode(sData);
OS.File.writeAtomic(xFilePicker.file.path, xEncodedRes, {tmpPath: "file.txt.tmp"});
}
});
}
}
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|