Skip to content

Commit

Permalink
Merge pull request #83 from jobisoft/dev
Browse files Browse the repository at this point in the history
Call first run as a WebExtension page
  • Loading branch information
sphh authored Mar 17, 2021
2 parents bfc2ceb + d94168c commit b2b62ce
Show file tree
Hide file tree
Showing 14 changed files with 528 additions and 178 deletions.
81 changes: 81 additions & 0 deletions api/LatexIt/implementation.js
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;
}

}
};
}
};
37 changes: 37 additions & 0 deletions api/LatexIt/schema.json
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" }
}
]
}
]
}
]
27 changes: 27 additions & 0 deletions api/LegacyPrefs/README.md
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).
81 changes: 72 additions & 9 deletions api/LegacyPrefs/implementation.js
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");

Expand All @@ -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;
}

}
Expand Down
48 changes: 48 additions & 0 deletions api/LegacyPrefs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,54 @@
}
]
},
{
"name": "getPref",
"type": "function",
"async": true,
"description": "Gets a value from the legacy pref system.",
"parameters": [
{
"name": "aName",
"type": "string",
"description": "Name of the preference."
},
{
"name": "aFallback",
"type": "string",
"description": "Value to be returned, if the requested preference does not exist.",
"optional": true,
"default": null
}
]
},
{
"name": "setPref",
"type": "function",
"async": true,
"description": "Sets a value for an existing pref of the legacy pref system.",
"parameters": [
{
"name": "aName",
"type": "string",
"description": "Name of the preference."
},
{
"name": "aValue",
"choices": [
{
"type": "string"
},
{
"type": "integer"
},
{
"type": "boolean"
}
],
"description": "Value to be set."
}
]
},
{
"name": "clearUserPref",
"type": "function",
Expand Down
Loading

0 comments on commit b2b62ce

Please sign in to comment.