Functioning options page

This commit is contained in:
Mahesh Asolkar 2018-10-28 15:51:10 -07:00
parent cbdd1e3231
commit b19d1a0038
7 changed files with 168 additions and 35 deletions

View File

@ -9,6 +9,14 @@ function emptyEm() {
browser.folder_actions.emptyTrashFolders(); browser.folder_actions.emptyTrashFolders();
browser.folder_actions.emptyJunkFolders(); browser.folder_actions.emptyJunkFolders();
console.log("Empty-em: Emptying.. Done"); console.log("Empty-em: Emptying.. Done");
// TODO: Do this on done event
browser.notifications.create("emptyem-done-notify", {
"type": "basic",
"iconUrl": browser.extension.getURL("icons/icon.png"),
"title": "Empty 'em done!",
"message": "Cleaned Trash and Junk folders"
});
} }
// Handlers // Handlers

72
data/common.js Normal file
View File

@ -0,0 +1,72 @@
// -----------------------------------------------
// Common functions used in Options handling
// -----------------------------------------------
//
// Get default values of preferences
//
function getPrefDefaults() {
return {
prefs: {
overrideDeleteConfirm: false,
selectTrashDelete: false,
selectJunkDelete: false,
consoleDebug: false,
disableDoneNotification: false,
alsoCompact: false
}
};
}
//
// Get preferences from storage and return the structure asynchronously
//
async function getPrefs() {
let getting = await browser.storage.sync.get("prefs");
let fr = "";
if ($.isEmptyObject(getting)) {
prefs = getPrefDefaults();
fr = "defaults";
} else {
prefs = getting;
fr = "storage";
};
console.log("Loaded prefs from " + fr);
return getting;
}
//
// Handle the event of an option change in options UI. Event is passed in as
// the 'ch' argument. This function records the changed option in to the
// options storage and returns the updated structure asynchronously
//
async function changeOption(ch) {
var i = ch[0].id;
var c = ch[0].checked;
console.log("Pref: " + i + " -> " + c);
if (i == "overrideDeleteConfirm") { prefs.prefs.overrideDeleteConfirm = c; }
else if (i == "selectTrashDelete") { prefs.prefs.selectTrashDelete = c; }
else if (i == "selectJunkDelete") { prefs.prefs.selectJunkDelete = c; }
else if (i == "consoleDebug") { prefs.prefs.consoleDebug = c; }
else if (i == "disableDoneNotification") { prefs.prefs.disableDoneNotification = c; }
else if (i == "alsoCompact") { prefs.prefs.alsoCompact = c; }
await browser.storage.sync.set(prefs);
prefs = await getPrefs();
return prefs;
}
//
// Given the preferences structure, update the options IO with appropriate values
//
function updateOptionsForm(prefs) {
$("#overrideDeleteConfirm").prop('checked', prefs.prefs.overrideDeleteConfirm);
$("#selectTrashDelete").prop('checked', prefs.prefs.selectTrashDelete);
$("#selectJunkDelete").prop('checked', prefs.prefs.selectJunkDelete);
$("#selectJunkDelete").prop('checked', prefs.prefs.selectJunkDelete);
$("#consoleDebug").prop('checked', prefs.prefs.consoleDebug);
$("#disableDoneNotification").prop('checked', prefs.prefs.disableDoneNotification);
$("#alsoCompact").prop('checked', prefs.prefs.alsoCompact);
}

View File

@ -120,6 +120,7 @@ body {
</div> </div>
<script src="jquery.min.js"></script> <script src="jquery.min.js"></script>
<script src="common.js"></script>
<script src="options.js"></script> <script src="options.js"></script>
</body> </body>
</html> </html>

View File

@ -1,3 +1,19 @@
// ----------------------------------------------- // -----------------------------------------------
// Options UI and handling // Options UI and handling
// ----------------------------------------------- // -----------------------------------------------
//
// Load preferences from options storage. When done, update the options UI with
// values. Then, set up the listener for option changes.
//
async function loadOptions() {
let prefs = await getPrefs();
updateOptionsForm(prefs);
$("input[type=checkbox]").change(function() {
changeOption($(this));
});
}
$(function() {
loadOptions();
});

View File

@ -11,6 +11,10 @@ class FolderActionsProvider extends ExtensionCommon.EventEmitter {
super(); super();
this.extension = extension; this.extension = extension;
this.options = {
consoleDebug: 0,
alsoCompact: 0
};
} }
get type() { get type() {
@ -56,11 +60,11 @@ class FolderActionsProvider extends ExtensionCommon.EventEmitter {
this.factory = factory.QueryInterface(Ci.nsIFactory); this.factory = factory.QueryInterface(Ci.nsIFactory);
registrar.registerFactory( registrar.registerFactory(
this.classID, `Cloud file provider for ${this.extension.id}`, contractID, this.factory this.classID, `Folder Actions provider for ${this.extension.id}`, contractID, this.factory
); );
XPCOMUtils.categoryManager.addCategoryEntry( XPCOMUtils.categoryManager.addCategoryEntry(
"cloud-files", this.extension.id, contractID, false, true "folder-actions", this.extension.id, contractID, false, true
); );
} }
@ -68,7 +72,7 @@ class FolderActionsProvider extends ExtensionCommon.EventEmitter {
let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.unregisterFactory(this.classID, this.factory); registrar.unregisterFactory(this.classID, this.factory);
XPCOMUtils.categoryManager.deleteCategoryEntry("cloud-files", this.extension.id, false); XPCOMUtils.categoryManager.deleteCategoryEntry("folder-actions", this.extension.id, false);
} }
} }
@ -91,10 +95,18 @@ this.folder_actions = class extends ExtensionAPI {
} }
} }
debugMsg(msg) {
if (this.provider.options.consoleDebug > 0) {
console.log(msg);
}
}
getFolderInfo() { getFolderInfo() {
let data = []; let data = [];
let self = this;
toArray(fixIterator(MailServices.accounts.accounts,Components.interfaces.nsIMsgAccount)).map(function(account) { toArray(fixIterator(MailServices.accounts.accounts,Components.interfaces.nsIMsgAccount)).
map(function(account) {
let info = { let info = {
server: null, server: null,
type: null, type: null,
@ -107,13 +119,13 @@ this.folder_actions = class extends ExtensionAPI {
let find_flagged_folders = function(folder,inf) { let find_flagged_folders = function(folder,inf) {
if (folder.getFlag(Ci.nsMsgFolderFlags.Trash)) { if (folder.getFlag(Ci.nsMsgFolderFlags.Trash)) {
console.log("Folder: " + folder.URI + " has Trash flag "); self.debugMsg("Folder: " + folder.URI + " has Trash flag ");
inf.trashFolder = folder; inf.trashFolder = folder;
console.log(folder); self.debugMsg(folder);
} else if (folder.getFlag(Ci.nsMsgFolderFlags.Junk)) { } else if (folder.getFlag(Ci.nsMsgFolderFlags.Junk)) {
console.log("Folder: " + folder.URI + " has Junk flag "); self.debugMsg("Folder: " + folder.URI + " has Junk flag ");
inf.junkFolder = folder; inf.junkFolder = folder;
console.log(folder); self.debugMsg(folder);
} else { } else {
if (folder.hasSubFolders) { if (folder.hasSubFolders) {
toArray(fixIterator(folder.subFolders, Ci.nsMsgFolder)).map(function (f) { toArray(fixIterator(folder.subFolders, Ci.nsMsgFolder)).map(function (f) {
@ -135,7 +147,7 @@ this.folder_actions = class extends ExtensionAPI {
}); });
data.push(info); data.push(info);
}); });
console.log(data); this.debugMsg(data);
return data; return data;
} }
@ -147,29 +159,32 @@ this.folder_actions = class extends ExtensionAPI {
return { return {
folder_actions: { folder_actions: {
emptyFolder: function(folderName) { emptyFolder: function(folderName) {
let finfo = self.getFolderInfo(); let finfo = self.getFolderInfo();
}, },
emptyTrashFolders: function() { emptyTrashFolders: function() {
let finfo = self.getFolderInfo(); let finfo = self.getFolderInfo();
finfo.map(function(info) { finfo.map(function(info) {
if (info.trashFolder) { if (info.trashFolder) {
console.log("emptyTrashFolders: Considering " + info.trashFolder.URI); self.debugMsg("emptyTrashFolders: Considering " + info.trashFolder.URI);
console.log("emptyTrashFolders: canDeleteMessages? " + info.trashFolder.canDeleteMessages); self.debugMsg("emptyTrashFolders: canDeleteMessages? " + info.trashFolder.canDeleteMessages);
console.log("emptyTrashFolders: hasSubFolders? " + info.trashFolder.hasSubFolders); self.debugMsg("emptyTrashFolders: hasSubFolders? " + info.trashFolder.hasSubFolders);
console.log(info.trashFolder); self.debugMsg(info.trashFolder);
info.trashFolder.emptyTrash(null,null); info.trashFolder.emptyTrash(null,null);
} }
}); });
}, },
emptyJunkFolders: function() { emptyJunkFolders: function() {
let finfo = self.getFolderInfo(); let finfo = self.getFolderInfo();
finfo.map(function(info) { finfo.map(function(info) {
if (info.junkFolder) { if (info.junkFolder) {
console.log("emptyTrashFolders: Considering " + info.junkFolder.URI); self.debugMsg("emptyTrashFolders: Considering " + info.junkFolder.URI);
console.log("emptyTrashFolders: canDeleteMessages? " + info.junkFolder.canDeleteMessages); self.debugMsg("emptyTrashFolders: canDeleteMessages? " + info.junkFolder.canDeleteMessages);
console.log("emptyTrashFolders: hasSubFolders? " + info.junkFolder.hasSubFolders); self.debugMsg("emptyTrashFolders: hasSubFolders? " + info.junkFolder.hasSubFolders);
console.log(info.junkFolder); self.debugMsg(info.junkFolder);
var junk_msgs = Cc["@mozilla.org/array;1"] var junk_msgs = Cc["@mozilla.org/array;1"]
.createInstance(Ci.nsIMutableArray); .createInstance(Ci.nsIMutableArray);
var enumerator = info.junkFolder.messages; var enumerator = info.junkFolder.messages;
@ -183,6 +198,10 @@ this.folder_actions = class extends ExtensionAPI {
} }
} }
}); });
},
setOptions: function(options) {
self.provider.options = Object.assign(self.provider.options, options);
} }
} }
}; };

View File

@ -21,16 +21,16 @@
"namespace": "folder_actions", "namespace": "folder_actions",
"events": [ "events": [
{ {
"name": "onEmptyFolder", "name": "onEmptyTrash",
"type": "function", "type": "function",
"description": "Fired when a folder is emptied", "description": "Fired when Trash folders are emptied",
"parameters": [ "parameters": []
{ },
"name": "folderName", {
"$ref": "MailFolder", "name": "onEmptyJunk",
"description": "The folder to be emptied" "type": "function",
} "description": "Fired when Junk folders are emptied",
] "parameters": []
} }
], ],
"types": [ "types": [
@ -44,18 +44,37 @@
"description": "Name of the folder to be emptied" "description": "Name of the folder to be emptied"
} }
} }
},
{
"id": "ApiOptions",
"type": "object",
"description": "Folder API options",
"properties": {
"consoleDebug": {
"type": "integer",
"minimum": 0,
"optional": true,
"description": "If set to 1, debug messages are printed out in console log"
},
"alsoCompact": {
"type": "integer",
"minimum": 0,
"optional": true,
"description": "If set to 1, also compact folder after emptying"
}
}
} }
], ],
"functions": [ "functions": [
{ {
"name": "emptyFolder", "name": "setOptions",
"type": "function", "type": "function",
"description": "Empties specified mail folder", "description": "Sets options for this API",
"parameters": [ "parameters": [
{ {
"name": "folderName", "name": "options",
"$ref": "MailFolder", "$ref": "ApiOptions",
"description": "The mail folder to empty" "description": "API Options object"
} }
] ]
}, },

View File

@ -16,6 +16,8 @@
} }
}, },
"permissions": ["storage", "notifications"],
"browser_action": { "browser_action": {
"default_icon": { "default_icon": {
"16": "icons/icon.png", "16": "icons/icon.png",
@ -24,10 +26,6 @@
"default_title": "Empty 'em" "default_title": "Empty 'em"
}, },
// "folder_actions": {
// "name": "EmptyEm"
// },
"options_ui": { "options_ui": {
"page": "data/options.html" "page": "data/options.html"
}, },