-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from jobisoft/dev
Call first run as a WebExtension page
- Loading branch information
Showing
14 changed files
with
528 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm"); | ||
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); | ||
|
||
function init_file (path) { | ||
var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); | ||
try { | ||
f.initWithPath(path); | ||
} catch (e) { | ||
dump("Wrong path! "+path+"\n"); | ||
return { | ||
exists: function () { return false; } | ||
}; | ||
} | ||
return f | ||
} | ||
|
||
var LatexIt = class extends ExtensionCommon.ExtensionAPI { | ||
getAPI(context) { | ||
|
||
return { | ||
LatexIt: { | ||
|
||
file_exists: async function(path) { | ||
let file = init_file(path); | ||
return file.exists(); | ||
}, | ||
|
||
search_in_path: async function(isWindows, extraEnvPathSuggestions) { | ||
var env = Cc["@mozilla.org/process/environment;1"].createInstance(Ci.nsIEnvironment); | ||
var sep = isWindows ? ";" : ":"; | ||
var ext = isWindows ? ".exe" : ""; | ||
var dirs = env.get("PATH").split(sep); | ||
|
||
// add suggestions to path | ||
let pathMod = false; | ||
for (let i = 0; i < extraEnvPathSuggestions.length; ++i) { | ||
if (dirs.indexOf(suggestions[i]) < 0) { | ||
dirs.push(suggestions[i]); | ||
pathMod = true; | ||
} | ||
} | ||
// Do we really have to CHANGE the path? We added the suggestions | ||
// for the search and store the full path found. | ||
if (pathMod) { | ||
env.set("PATH", dirs.join(sep)); | ||
dump("New path: " + env.get("PATH") + "\n"); | ||
} | ||
|
||
let found = {}; | ||
|
||
for (var i = 0; i < dirs.length; ++i) { | ||
var latex_bin = init_file(dirs[i]); | ||
if (latex_bin.exists()) { | ||
latex_bin.append("latex"+ext); | ||
if (latex_bin.exists()) { | ||
found.latex = latex_bin.path; | ||
dump("Found latex in "+latex_bin.path+"\n"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
for (var i = 0; i < dirs.length; ++i) { | ||
var dvipng_bin = init_file(dirs[i]); | ||
if (dvipng_bin.exists()) { | ||
dvipng_bin.append("dvipng"+ext); | ||
if (dvipng_bin.exists()) { | ||
found.dvipng = dvipng_bin.path; | ||
dump("Found dvipng in "+dvipng_bin.path+"\n"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return found; | ||
} | ||
|
||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[ | ||
{ | ||
"namespace": "LatexIt", | ||
"functions": [ | ||
{ | ||
"name": "file_exists", | ||
"type": "function", | ||
"async": true, | ||
"description": "check if local file exists", | ||
"parameters": [ | ||
{ | ||
"name": "aPath", | ||
"type": "string", | ||
"description": "aPath" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "search_in_path", | ||
"type": "function", | ||
"async": true, | ||
"description": "search_in_path", | ||
"parameters": [ | ||
{ | ||
"name": "isWindows", | ||
"type": "boolean" | ||
}, | ||
{ | ||
"name": "extraEnvPathSuggestions", | ||
"type": "array", | ||
"items": { "type": "string" } | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Objective | ||
|
||
Use this API to access Thunderbird's system preferences or to migrate your own preferences from the Thunderbird preference system to the local storage of your MailExtension. | ||
|
||
## Usage | ||
|
||
This API provides the following methods: | ||
|
||
### async getPref(aName, [aFallback]) | ||
|
||
Returns the value for the ``aName`` preference. If it is not defined or has no default value assigned, ``aFallback`` will be returned (which defaults to ``null``). | ||
|
||
### async getUserPref(aName) | ||
|
||
Returns the user defined value for the ``aName`` preference. This will ignore any defined default value and will only return an explicitly set value, which differs from the default. Otherwise it will return ``null``. | ||
|
||
### clearUserPref(aName) | ||
|
||
Clears the user defined value for preference ``aName``. Subsequent calls to ``getUserPref(aName)`` will return ``null``. | ||
|
||
### async setPref(aName, aValue) | ||
|
||
Set the ``aName`` preference to the given value. Will return false and log an error to the console, if the type of ``aValue`` does not match the type of the preference. | ||
|
||
--- | ||
|
||
A detailed example using the LegacyPref API to migrate add-on preferences to the local storage can be found in [/scripts/preferences/](https://github.com/thundernest/addon-developer-support/tree/master/scripts/preferences). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,26 @@ | ||
/* | ||
* This file is provided by the addon-developer-support repository at | ||
* https://github.com/thundernest/addon-developer-support | ||
* | ||
* Version: 1.5 | ||
* replace set/getCharPref by set/getStringPref to fix encoding issue | ||
* | ||
* Version: 1.4 | ||
* - setPref() function returns true if the value could be set, otherwise false | ||
* | ||
* Version: 1.3 | ||
* - add setPref() function | ||
* | ||
* Version: 1.2 | ||
* - add getPref() function | ||
* | ||
* Author: John Bieling ([email protected]) | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm"); | ||
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); | ||
|
||
|
@@ -7,37 +30,77 @@ var LegacyPrefs = class extends ExtensionCommon.ExtensionAPI { | |
return { | ||
LegacyPrefs: { | ||
|
||
// get may only return something, if a value is set | ||
getUserPref: async function(aName) { | ||
getLegacyPref: async function(aName, aFallback = null, userPrefOnly = true) { | ||
let prefType = Services.prefs.getPrefType(aName); | ||
if (prefType == Services.prefs.PREF_INVALID) { | ||
return null; | ||
return aFallback; | ||
} | ||
|
||
let value = null; | ||
if (Services.prefs.prefHasUserValue(aName)) { | ||
let value = aFallback; | ||
if (!userPrefOnly || Services.prefs.prefHasUserValue(aName)) { | ||
switch (prefType) { | ||
case Services.prefs.PREF_STRING: | ||
value = Services.prefs.getCharPref(aName, null); | ||
value = Services.prefs.getStringPref(aName, aFallback); | ||
break; | ||
|
||
case Services.prefs.PREF_INT: | ||
value = Services.prefs.getIntPref(aName, null); | ||
value = Services.prefs.getIntPref(aName, aFallback); | ||
break; | ||
|
||
case Services.prefs.PREF_BOOL: | ||
value = Services.prefs.getBoolPref(aName, null); | ||
value = Services.prefs.getBoolPref(aName, aFallback); | ||
break; | ||
|
||
default: | ||
console.error(`Legacy preference <${aName}> has an unknown type of <${prefType}>. Migration skipped.`); | ||
console.error(`Legacy preference <${aName}> has an unknown type of <${prefType}>.`); | ||
} | ||
} | ||
return value; | ||
}, | ||
|
||
// only returns something, if a user pref value is set | ||
getUserPref: async function(aName) { | ||
return await this.getLegacyPref(aName); | ||
}, | ||
|
||
// returns the default value, if no user defined value exists, | ||
// and returns the fallback value, if the preference does not exist | ||
getPref: async function(aName, aFallback = null) { | ||
return await this.getLegacyPref(aName, aFallback, false); | ||
}, | ||
|
||
clearUserPref: function(aName) { | ||
Services.prefs.clearUserPref(aName); | ||
}, | ||
|
||
|
||
// sets a pref | ||
setPref: async function(aName, aValue) { | ||
let prefType = Services.prefs.getPrefType(aName); | ||
if (prefType == Services.prefs.PREF_INVALID) { | ||
return false; | ||
} | ||
|
||
switch (prefType) { | ||
case Services.prefs.PREF_STRING: | ||
Services.prefs.setStringPref(aName, aValue); | ||
return true; | ||
break; | ||
|
||
case Services.prefs.PREF_INT: | ||
Services.prefs.setIntPref(aName, aValue); | ||
return true; | ||
break; | ||
|
||
case Services.prefs.PREF_BOOL: | ||
Services.prefs.setBoolPref(aName, aValue); | ||
return true; | ||
break; | ||
|
||
default: | ||
console.error(`Legacy preference <${aName}> has an unknown type of <${prefType}>.`); | ||
} | ||
return false; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.