Grammalecte  Check-in [7260392d1d]

Overview
Comment:[tb] lexicon editor: export (attempt with FilePicker)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tb | multid
Files: files | file ages | folders
SHA3-256: 7260392d1dddf7d2159accf8774aac294192b81942cb02fab7d018fc896a36e1
User & Date: olr on 2018-03-17 16:36:49
Original Comment: [tb] export (attempt with FilePicker)
Other Links: branch diff | manifest | tags
Context
2018-03-17
16:48
[tb] lexicon editor: export > use OS.File check-in: e376521f18 user: olr tags: tb, multid
16:36
[tb] lexicon editor: export (attempt with FilePicker) check-in: 7260392d1d user: olr tags: tb, multid
15:32
[tb] lexicon editor: update check-in: 550fb30fe3 user: olr tags: tb, multid
Changes

Modified gc_lang/fr/tb/content/lex_editor.js from [f08cbf555e] to [bd1a477dcb].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// JavaScript

"use strict";

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.grammarchecker.");

var FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils





/*
    Common functions
*/

function showError (e) {










|
<
<







1
2
3
4
5
6
7
8
9
10
11


12
13
14
15
16
17
18
// JavaScript

"use strict";

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.grammarchecker.");

var FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils
var NetUtil = Cu.import("resource://gre/modules/NetUtil.jsm").NetUtil;




/*
    Common functions
*/

function showError (e) {
484
485
486
487
488
489
490
491
492
493
494

495

496









497


498









499

500






501
502
503
504
505
506
507
508
        enableElement("export_button");
    },

    import: function () {
        console.log("import");
    },

    export: function () {
        let sJSON = JSON.stringify(this.oIBDAWG.getJSON());
        let xFile = new FileUtils.File("D:\\fr.personal.json");
        xFile.createUnique(FileUtils.NORMAL_FILE_TYPE, 555);

        console.log(xFile);

        let xFile2 = new File([ sJSON ], "D:\\fr.test.personal.json", {type: 'text/plain'}); 









        let sURL = URL.createObjectURL(xFile2);


        console.log(xFile2);









    }

}








const oLexiconTable = new Table("lexicon_table", ["Flexions", "Lemmes", "Étiquettes"], [10, 7, 10],"progress_lexicon", "num_entries");
const oGenWordsTable = new Table("generated_words_table", ["Flexions", "Étiquettes"], [10, 10], "progress_new_words");

oBinaryDict.load();
oBinaryDict.listen();
oGenerator.listen();







|


|
>
|
>
|
>
>
>
>
>
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>








482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
        enableElement("export_button");
    },

    import: function () {
        console.log("import");
    },

    export2: function () {
        let sJSON = JSON.stringify(this.oIBDAWG.getJSON());
        let xFile = new FileUtils.File("D:\\fr.personal.json");
        xFile.createUnique(FileUtils.NORMAL_FILE_TYPE, 644);
        xFileHandler = FileUtils.openSafeFileOutputStream(xFile, FileUtils.MODE_CREATE);
        console.log(xFileHandler);
    },

    export: function () {
        let sJSON = JSON.stringify(this.oIBDAWG.getJSON());
        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) {
                // write file 
                // OS.File.writeAtomic(xFilePicker.file.path, sJSON, {tmpPath: "file.txt.tmp"}); 

                // You can also optionally pass a flags parameter here. It defaults to
                // FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
                console.log(xFilePicker);
                let xOutStream = FileUtils.openSafeFileOutputStream(xFilePicker.file);
                let xConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
                xConverter.charset = "UTF-8";
                let xInStream = xConverter.convertToInputStream(sJSON);
                // The last argument (the callback) is optional.
                NetUtil.asyncCopy(xInStream, xOutStream, function (status) {
                    if (!Components.isSuccessCode(status)) {
                        console.log(status);
                        return;
                    }
                });
             }
        });
    }
}





const oLexiconTable = new Table("lexicon_table", ["Flexions", "Lemmes", "Étiquettes"], [10, 7, 10],"progress_lexicon", "num_entries");
const oGenWordsTable = new Table("generated_words_table", ["Flexions", "Étiquettes"], [10, 10], "progress_new_words");

oBinaryDict.load();
oBinaryDict.listen();
oGenerator.listen();