Skip to content

Commit

Permalink
Nearly complete rewrite using MailExtensions with Experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
Welpy-cw committed Jun 8, 2024
1 parent d1fcef7 commit 1a03919
Show file tree
Hide file tree
Showing 25 changed files with 898 additions and 3,527 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 eykamp, 2020 Welpy-cw
Copyright (c) 2013 eykamp, 2024 Hartmut Welpmann [:welpy-cw]

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
15 changes: 0 additions & 15 deletions README.md

This file was deleted.

62 changes: 62 additions & 0 deletions api/FolderAccount/implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var { ExtensionCommon } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionCommon.sys.mjs"
);

var { MailServices } = ChromeUtils.importESModule(
"resource:///modules/MailServices.sys.mjs"
);

var FolderAccount = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
return {
FolderAccount: {
async getDisplayedFolder(tabId) {
return context.extension.folderManager.convert(
context.extension.tabManager.get(tabId).nativeTab.folder
);
},
async getRelatedMessageFolder(tabId) {
const tabObject = context.extension.tabManager.get(tabId);
const realTabWindow = tabObject.window;
const folder = realTabWindow.gMessenger.msgHdrFromURI(
realTabWindow.gMsgCompose.originalMsgURI
).folder;
return context.extension.folderManager.convert(folder);
},
async getFolderAccountSettings() {
const folderPrefs = new Map();
const branch = Services.prefs.getBranch("extensions.folderaccount.");
for (const child of branch.getChildList("")) {
let pref;
try {
pref = branch.getCharPref(child);
} catch (e) {
continue;
}
const matches = child.match(
/(?<setting>addToCcOnReply|overrideReturnAddress|replyTo(OnReplyForward)?|to)?\.?(?<folder>.*)/
);
const folderURI = matches.groups["folder"];
const settingKey = matches.groups["setting"] ?? "identityId";
if (/addToCcOnReply|overrideReturnAddress|replyToOnReplyForward/.test(settingKey)) {
pref = pref == "true";
}
folderPrefs.set(folderURI, {
...folderPrefs.get(folderURI),
[settingKey]: pref,
});
}
const mailFolderPrefs = new Map();
folderPrefs.forEach((value, key) => {
const folder = MailServices.folderLookup.getFolderForURL(key);
const mailFolder = context.extension.folderManager.convert(folder);
if (mailFolder) {
mailFolderPrefs.set(mailFolder.id, value);
}
});
return mailFolderPrefs;
},
},
};
}
};
40 changes: 40 additions & 0 deletions api/FolderAccount/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"namespace": "FolderAccount",
"functions": [
{
"name": "getDisplayedFolder",
"description": "Get the currently displayed folder in the tab",
"type": "function",
"async": true,
"parameters": [
{
"name": "tabId",
"description": "The last focused tab",
"type": "integer"
}
]
},
{
"name": "getRelatedMessageFolder",
"description": "Get the folder of the related message of the currently displayed compose window",
"type": "function",
"async": true,
"parameters": [
{
"name": "tabId",
"description": "The current compose tab",
"type": "integer"
}
]
},
{
"name": "getFolderAccountSettings",
"description": "Get all the Folder Account settings from its preference branch for migration.",
"type": "function",
"async": true,
"parameters": []
}
]
}
]
60 changes: 60 additions & 0 deletions api/LegacyPrefs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## 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

Add the [LegacyPrefs API](https://github.com/thundernest/addon-developer-support/tree/master/auxiliary-apis/LegacyPrefs) to your add-on. Your `manifest.json` needs an entry like this:

```
"experiment_apis": {
"LegacyPrefs": {
"schema": "api/LegacyPrefs/schema.json",
"parent": {
"scopes": ["addon_parent"],
"paths": [["LegacyPrefs"]],
"script": "api/LegacyPrefs/implementation.js"
}
}
},
```

## API Functions

This API provides the following functions:

### 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.

## API Events

This API provides the following events:

### onChanged.addListener(listener, branch)

Register a listener which is notified each time a value in the specified branch is changed. The listener returns the name and the new value of the changed preference.

Example:

```
browser.LegacyPrefs.onChanged.addListener(async (name, value) => {
console.log(`Changed value in "mailnews.": ${name} = ${value}`);
}, "mailnews.");
```

---

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).
Loading

0 comments on commit 1a03919

Please sign in to comment.