Grammalecte  Check-in [0b14dcdc9e]

Overview
Comment:[tb] delete file_handler.js: deprecated code
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | tb
Files: files | file ages | folders
SHA3-256: 0b14dcdc9ea3d10f076ca1c659cefdc512f2261360f062793e40a7cca921ccf1
User & Date: olr on 2020-07-20 08:34:33
Other Links: manifest | tags
Context
2020-07-21
06:35
[fx] add a note for reviewers check-in: 57dc4f6ed0 user: olr tags: trunk, fx
2020-07-20
08:34
[tb] delete file_handler.js: deprecated code check-in: 0b14dcdc9e user: olr tags: trunk, tb
08:32
[tb] delete editor.js: deprecated code check-in: 17feca742f user: olr tags: trunk, tb
Changes

Deleted gc_lang/fr/mailext/content/file_handler.js version [430edd781d].

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"});
            }
        });
    }
}