Grammalecte  Check-in [1dd9310e28]

Overview
Comment:[tb] new API: fix dictionary loading + remove obsolete echo function
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tb | tbnext
Files: files | file ages | folders
SHA3-256: 1dd9310e286b9537fcd0265d07fe0182603487844dbdaac9290bb49a3b495ee6
User & Date: olr on 2018-03-14 19:49:31
Other Links: branch diff | manifest | tags
Context
2018-03-14
19:50
[tb] spellchecker: tabulations to spaces check-in: ce4a3cc724 user: olr tags: tb, tbnext
19:49
[tb] new API: fix dictionary loading + remove obsolete echo function check-in: 1dd9310e28 user: olr tags: tb, tbnext
15:37
[tb] remove dialog for bundled options > common dialog box check-in: 617b4e1348 user: olr tags: tb, tbnext
Changes

Modified gc_lang/fr/tb/content/spellchecker.js from [225bd1d02e] to [7f146a3994].

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
	https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile
	https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm
*/

"use strict";

/*

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js", {});
*/


const FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils;
const AddonManager = Cu.import("resource://gre/modules/AddonManager.jsm").AddonManager;

/*
const parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
const persodict = Cc["@mozilla.org/spellchecker/personaldictionary;1"].getService(Ci.mozIPersonalDictionary);
*/

//const system = require("sdk/system");


var oSpellControl = {
	xSCEngine: null,
	init: function () {
		if (this.xSCEngine === null) {
			try {
				let sSpellchecker = "@mozilla.org/spellchecker/myspell;1";
				if ("@mozilla.org/spellchecker/hunspell;1" in Cc) {
				    sSpellchecker = "@mozilla.org/spellchecker/hunspell;1";
				}
				if ("@mozilla.org/spellchecker/engine;1" in Cc) {
				    sSpellchecker = "@mozilla.org/spellchecker/engine;1";
				}
				this.xSCEngine = Cc[sSpellchecker].getService(Ci.mozISpellCheckingEngine);
			}
			catch (e) {
				echo("Can’t initiate the spellchecker.");
				Cu.reportError(e);
			}
		}
	},
	getDictionariesList: function () {
		this.init();
		try {







>










<
<
<
<
<
<
<








|


|




|







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
	https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile
	https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm
*/

"use strict";

/*
// Loaded in another file
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js", {});
*/


const FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils;
const AddonManager = Cu.import("resource://gre/modules/AddonManager.jsm").AddonManager;









var oSpellControl = {
	xSCEngine: null,
	init: function () {
		if (this.xSCEngine === null) {
			try {
				let sSpellchecker = "@mozilla.org/spellchecker/myspell;1";
				if ("@mozilla.org/spellchecker/hunspell;1" in Cc) {
					sSpellchecker = "@mozilla.org/spellchecker/hunspell;1";
				}
				if ("@mozilla.org/spellchecker/engine;1" in Cc) {
					sSpellchecker = "@mozilla.org/spellchecker/engine;1";
				}
				this.xSCEngine = Cc[sSpellchecker].getService(Ci.mozISpellCheckingEngine);
			}
			catch (e) {
				console.log("Can’t initiate the spellchecker.");
				Cu.reportError(e);
			}
		}
	},
	getDictionariesList: function () {
		this.init();
		try {
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139


140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
				return true;
			}
			catch (e) {
				Cu.reportError(e);
				return false;
			}
		} else {
			echo("Warning. No dictionary for locale: " + sLocale);
			echo("Existing dictionaries: " + this.getDictionariesList().join(" | "));
		}
		return false;
	},
	check: function (sWord) {
		// todo: check in personal dict?
		try {
			return this.xSCEngine.check(sWord);
		}
		catch (e) {
			Cu.reportError(e);
			return false;
		}
	},
	suggest: function (sWord) {
		try {
			let lSugg = {};
			this.xSCEngine.suggest(sWord, lSugg, {});
			return lSugg.value;
		   	// lSugg.value is a JavaScript Array of strings
		}
		catch (e) {
			Cu.reportError(e);
			return ['#Erreur.'];
		}
	},
	addDirectory: function (sFolder) {
		try {
			let xNsiFolder = new FileUtils.File(sFolder);
			this.xSCEngine.addDirectory(xNsiFolder);
		}
		catch (e) {
			echo("Unable to add directory: " + sFolder);
			Cu.reportError(e);
		}
	},
	removeDirectory: function (sFolder) {
		// does not work but no exception raised (bug?)
		try {
			let xNsiFolder = new FileUtils.File(sFolder);
			this.xSCEngine.removeDirectory(xNsiFolder);
		}
		catch (e) {
			echo("Unable to remove directory: " + sFolder);
			Cu.reportError(e);
		}
	},
	setExtensionDictFolder: function (sDictName, bActivate) {
		try {
			let that = this;
			let sPath = "/content/dictionaries/" + sDictName;
			AddonManager.getAddonByID("French-GC-TB@grammalecte.net", function (addon) {
				let xURI = addon.getResourceURI(sPath);
				console.log("> " + xURI.path);
				let sFolder = xURI.path;
				//if (system.platform === "winnt") {
				if (sFolder) {


					sFolder = sFolder.slice(1).replace(/\//g, "\\\\");
				}
				console.log("> " + sFolder);
				if (bActivate) {
					that.addDirectory(sFolder);
				} else {
					that.removeDirectory(sFolder);

				}
			});
		}
		catch (e) {
			echo("Unable to add extension folder");
			Cu.reportError(e);
		}
	}
};







|
|


















|












|










|









|
|
<
|
>
>
|
|
|
|
|
|
|
>




|




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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
				return true;
			}
			catch (e) {
				Cu.reportError(e);
				return false;
			}
		} else {
			console.log("Warning. No dictionary for locale: " + sLocale);
			console.log("Existing dictionaries: " + this.getDictionariesList().join(" | "));
		}
		return false;
	},
	check: function (sWord) {
		// todo: check in personal dict?
		try {
			return this.xSCEngine.check(sWord);
		}
		catch (e) {
			Cu.reportError(e);
			return false;
		}
	},
	suggest: function (sWord) {
		try {
			let lSugg = {};
			this.xSCEngine.suggest(sWord, lSugg, {});
			return lSugg.value;
			// lSugg.value is a JavaScript Array of strings
		}
		catch (e) {
			Cu.reportError(e);
			return ['#Erreur.'];
		}
	},
	addDirectory: function (sFolder) {
		try {
			let xNsiFolder = new FileUtils.File(sFolder);
			this.xSCEngine.addDirectory(xNsiFolder);
		}
		catch (e) {
			console.log("Unable to add directory: " + sFolder);
			Cu.reportError(e);
		}
	},
	removeDirectory: function (sFolder) {
		// does not work but no exception raised (bug?)
		try {
			let xNsiFolder = new FileUtils.File(sFolder);
			this.xSCEngine.removeDirectory(xNsiFolder);
		}
		catch (e) {
			console.log("Unable to remove directory: " + sFolder);
			Cu.reportError(e);
		}
	},
	setExtensionDictFolder: function (sDictName, bActivate) {
		try {
			let that = this;
			let sPath = "/content/dictionaries/" + sDictName;
			AddonManager.getAddonByID("French-GC-TB@grammalecte.net", function (addon) {
				let xURI = addon.getResourceURI(sPath);
				//console.log(xURI);
				let sFolder = xURI.filePath;

				if (sFolder !== undefined) {
					if (/^\/[A-Z]:\//.test(sFolder)) {
						// Windows path
						sFolder = sFolder.slice(1).replace(/\//g, "\\\\");
					}
					console.log("folder: " + sFolder);
					if (bActivate) {
						that.addDirectory(sFolder);
					} else {
						that.removeDirectory(sFolder);
					}
				}
			});
		}
		catch (e) {
			console.log("Unable to add extension folder");
			Cu.reportError(e);
		}
	}
};