Use local storage only. Fix icon handling.

This commit is contained in:
Ximi1970
2021-09-05 13:55:39 +02:00
parent 51ef376b36
commit 7301b0c190
5 changed files with 134 additions and 97 deletions

View File

@@ -5,7 +5,7 @@ SysTray-X is a system tray extension for Thunderbird 68+. The addon uses the Web
The addon and system tray application can do:
- custom new mail icon
- display number of unread / new mails
- display number of unread mails
- show / hide Thunderbird (minimize)
- minimizing hides to tray (remove Thunderbird from taskbar, pager and switcher when minimized)
- minimize on startup

View File

@@ -18,7 +18,7 @@ SysTrayX.Info = {
console.debug("Info Addon version: " + this.version);
console.debug("Info Platform: " + JSON.stringify(this.platformInfo));
console.debug("Info Browser: " + JSON.stringify(this.browserInfo));
console.debug("Info Storage: " + this.storageType);
// console.debug("Info Storage: " + this.storageType);
},
};
@@ -141,40 +141,10 @@ SysTrayX.Messaging = {
SysTrayX.Messaging.listenerFolderDeleted
);
for (const filter of SysTrayX.Messaging.filters) {
for (const path of filter.folders) {
const folder = {
accountId: filter.accountId,
path: path,
};
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folder);
} catch (err) {
console.debug("Filter error: " + err);
console.debug("Filter error: " + JSON.stringify(folder));
}
if (mailFolderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[folder.accountId] === undefined) {
SysTrayX.Messaging.unread[folder.accountId] = {};
}
SysTrayX.Messaging.unread[folder.accountId][folder.path] =
mailFolderInfo.unreadMessageCount;
}
}
}
let count = 0;
Object.keys(SysTrayX.Messaging.unread).forEach((key) =>
Object.keys(SysTrayX.Messaging.unread[key]).forEach(
(path) => (count = count + SysTrayX.Messaging.unread[key][path])
)
);
SysTrayX.Link.postSysTrayXMessage({ unreadMail: count });
// Get the unread count
const getcountUnreadPromise = () =>
new Promise((res) => res(SysTrayX.Messaging.countUnread()));
await getcountUnreadPromise();
// Set count listener
browser.folders.onFolderInfoChanged.addListener(
@@ -231,6 +201,43 @@ SysTrayX.Messaging = {
SysTrayX.Link.postSysTrayXMessage({ unreadMail: count });
},
countUnread: async function () {
for (const filter of SysTrayX.Messaging.filters) {
for (const path of filter.folders) {
const folder = {
accountId: filter.accountId,
path: path,
};
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folder);
} catch (err) {
console.debug("Filter error: " + err);
console.debug("Filter error: " + JSON.stringify(folder));
}
if (mailFolderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[folder.accountId] === undefined) {
SysTrayX.Messaging.unread[folder.accountId] = {};
}
SysTrayX.Messaging.unread[folder.accountId][folder.path] =
mailFolderInfo.unreadMessageCount;
}
}
}
let count = 0;
Object.keys(SysTrayX.Messaging.unread).forEach((key) =>
Object.keys(SysTrayX.Messaging.unread[key]).forEach(
(path) => (count = count + SysTrayX.Messaging.unread[key][path])
)
);
SysTrayX.Link.postSysTrayXMessage({ unreadMail: count });
},
onCloseButton: function () {
SysTrayX.Link.postSysTrayXMessage({ window: "minimized_all" });
/*
@@ -248,8 +255,6 @@ SysTrayX.Messaging = {
if ("storageType" in changes && changes["storageType"].newValue) {
SysTrayX.Info.storageType = changes["storageType"].newValue;
console.debug("StorageType chnaged: " + SysTrayX.Info.storageType);
}
if ("filters" in changes && changes["filters"].newValue) {
@@ -257,6 +262,11 @@ SysTrayX.Messaging = {
if (SysTrayX.Info.browserInfo.majorVersion < 91) {
browser.folderChange.setFilters(SysTrayX.Messaging.filters);
} else {
// Update unread count
const getcountUnreadPromise = () =>
new Promise((res) => res(SysTrayX.Messaging.countUnread()));
await getcountUnreadPromise();
}
}
@@ -437,9 +447,10 @@ SysTrayX.Messaging = {
},
sendPreferences: async function () {
const getter = await storage()
await storage()
.get([
"debug",
"storageType",
"minimizeType",
"closeType",
"startMinimized",
@@ -467,6 +478,7 @@ SysTrayX.Messaging = {
sendPreferencesStorage: function (result) {
const debug = result.debug || "false";
const storageType = result.storageType || "local";
const minimizeType = result.minimizeType || "1";
const closeType = result.closeType || "1";
const startMinimized = result.startMinimized || "false";
@@ -500,25 +512,26 @@ SysTrayX.Messaging = {
// Send it to the app
SysTrayX.Link.postSysTrayXMessage({
preferences: {
debug: debug,
minimizeType: minimizeType,
closeType: closeType,
startMinimized: startMinimized,
restorePositions: restorePositions,
defaultIconType: defaultIconType,
defaultIconMime: defaultIconMime,
defaultIcon: defaultIcon,
hideDefaultIcon: hideDefaultIcon,
iconType: iconType,
iconMime: iconMime,
icon: icon,
showNumber: showNumber,
numberColor: numberColor,
numberSize: numberSize,
numberAlignment: numberAlignment,
numberMargins: numberMargins,
countType: countType,
theme: theme,
debug,
storageType,
minimizeType,
closeType,
startMinimized,
restorePositions,
defaultIconType,
defaultIconMime,
defaultIcon,
hideDefaultIcon,
iconType,
iconMime,
icon,
showNumber,
numberColor,
numberSize,
numberAlignment,
numberMargins,
countType,
theme,
},
});
@@ -737,9 +750,12 @@ async function start() {
//
// Get storage type
//
SysTrayX.Info.storageType = "local";
/*
SysTrayX.Info.storageType = await browser.storage.sync
.get("storageType")
.then((result) => result.storageType || "sync");
.then((result) => result.storageType || "local");
*/
// Get the prefered start state
const state = await getStartupState();
@@ -790,8 +806,8 @@ async function start() {
SysTrayX.Info.displayInfo();
// Used sync storage
const inUse = await browser.storage.sync.getBytesInUse();
console.log("Storage in use: " + inUse);
// const inUse = await browser.storage.sync.getBytesInUse();
// console.log("Storage in use: " + inUse);
// Init defaults before everything
@@ -800,8 +816,13 @@ async function start() {
kdeIntegration: true,
});
getDefaultIcon();
getIcon();
// Get the default icons
const getDefaultIconPromise = () =>
new Promise((res) => res(getDefaultIcon()));
await getDefaultIconPromise();
const getIconPromise = () => new Promise((res) => res(getIcon()));
await getIconPromise();
// Setup the link first
SysTrayX.Link.init();

View File

@@ -2,6 +2,9 @@
// Get the prefered storage
//
function storage() {
return browser.storage.local;
/*
if (SysTrayX.Info.storageType === "sync") {
console.log("Using sync storage");
return browser.storage.sync;
@@ -9,6 +12,7 @@ function storage() {
console.log("Using local storage");
return browser.storage.local;
}
*/
}
//
@@ -96,18 +100,20 @@ async function getHideDefaultIcon() {
//
async function getDefaultIcon() {
function resolve(result) {
return result.defaultIconMime && result.defaultIcon;
const defaultIconMime = result.defaultIconMime || "";
const defaultIcon = result.defaultIcon || "";
return { defaultIconMime, defaultIcon };
}
function reject() {
return false;
return "";
}
const defaultIconStored = await storage()
const { defaultIconMime, defaultIcon } = await storage()
.get(["defaultIconMime", "defaultIcon"])
.then(resolve, reject);
if (!defaultIconStored) {
if (defaultIconMime === "" || defaultIcon === "") {
const toDataURL = (url) =>
fetch(url)
.then((response) => response.blob())
@@ -122,26 +128,26 @@ async function getDefaultIcon() {
);
// Convert image to storage param
let { defaultIconMime, defaultIconBase64 } = await toDataURL(
const { defaultIconMimeUrl, defaultIconBase64Url } = await toDataURL(
"icons/Thunderbird.png"
).then((dataUrl) => {
const data = dataUrl.split(":").pop().split(",");
return {
defaultIconMime: data[0].split(";")[0],
defaultIconBase64: data[1],
defaultIconMimeUrl: data[0].split(";")[0],
defaultIconBase64Url: data[1],
};
});
// Store default icon (base64)
await storage().set({
defaultIconMime: defaultIconMime,
defaultIcon: defaultIconBase64,
defaultIconMime: defaultIconMimeUrl,
defaultIcon: defaultIconBase64Url,
});
// Store in HTML
const defaultIconDiv = document.getElementById("defaultIcon");
defaultIconDiv.setAttribute("data-default-icon-mime", defaultIconMime);
defaultIconDiv.setAttribute("data-default-icon", defaultIconBase64);
defaultIconDiv.setAttribute("data-default-icon-mime", defaultIconMimeUrl);
defaultIconDiv.setAttribute("data-default-icon", defaultIconBase64Url);
}
}
@@ -151,18 +157,20 @@ async function getDefaultIcon() {
//
async function getIcon() {
function resolve(result) {
return result.iconMime && result.icon;
const iconMime = result.iconMime || "";
const icon = result.icon || "";
return { iconMime, icon };
}
function reject() {
return false;
return "";
}
const iconStored = await storage()
const { iconMime, icon } = await storage()
.get(["iconMime", "icon"])
.then(resolve, reject);
if (!iconStored) {
if (iconMime === "" || icon === "") {
const toDataURL = (url) =>
fetch(url)
.then((response) => response.blob())
@@ -177,23 +185,23 @@ async function getIcon() {
);
// Convert image to storage param
let { iconMime, iconBase64 } = await toDataURL("icons/blank-icon.png").then(
(dataUrl) => {
const { iconMimeUrl, iconBase64Url } = await toDataURL(
"icons/blank-icon.png"
).then((dataUrl) => {
const data = dataUrl.split(":").pop().split(",");
return { iconMime: data[0].split(";")[0], iconBase64: data[1] };
}
);
return { iconMimeUrl: data[0].split(";")[0], iconBase64Url: data[1] };
});
// Store default icon (base64)
await storage().set({
iconMime: iconMime,
icon: iconBase64,
iconMime: iconMimeUrl,
icon: iconBase64Url,
});
// Store in HTML
const iconDiv = document.getElementById("icon");
iconDiv.setAttribute("data-icon-mime", iconMime);
iconDiv.setAttribute("data-icon", iconBase64);
iconDiv.setAttribute("data-icon-mime", iconMimeUrl);
iconDiv.setAttribute("data-icon", iconBase64Url);
}
}

View File

@@ -11,6 +11,6 @@ SysTrayX.Info = {
"Info Platform: " + JSON.stringify(SysTrayX.Info.platformInfo)
);
console.debug("Info Browser: " + JSON.stringify(SysTrayX.Info.browserInfo));
console.debug("Info Storage: " + SysTrayX.Info.storageType);
// console.debug("Info Storage: " + SysTrayX.Info.storageType);
},
};

View File

@@ -2,21 +2,24 @@
// Get the prefered storage
//
function storage() {
return browser.storage.local;
/*
if (SysTrayX.Info.storageType === "sync") {
console.log("Using sync storage");
return browser.storage.sync;
} else {
console.log("Using local storage");
return browser.storage.local;
}
*/
}
SysTrayX.SaveOptions = {
start: async function (e) {
e.preventDefault();
/*
//
// Save storage type preferences
// Always in sync space
@@ -28,9 +31,10 @@ SysTrayX.SaveOptions = {
SysTrayX.Info.storageType = storageType === "0" ? "sync" : "local";
// Store storage type preferences
browser.storage.sync.set({
await browser.storage.sync.set({
storageType: SysTrayX.Info.storageType,
});
*/
//
// Save accounts and filters
@@ -301,8 +305,8 @@ SysTrayX.SaveOptions = {
addonprefchanged: true,
});
const inUse = await browser.storage.sync.getBytesInUse();
console.log("Storage in use: " + inUse);
// const inUse = await browser.storage.sync.getBytesInUse();
// console.log("Storage in use: " + inUse);
},
};
@@ -500,7 +504,7 @@ SysTrayX.RestoreOptions = {
// Restore storage type callbacks
//
setStorageType: function (result) {
const storageType = result.storageType || "sync";
const storageType = result.storageType || "local";
if (storageType === "sync") {
document.querySelector(
@@ -1183,10 +1187,12 @@ async function start() {
SysTrayX.Info.version = browser.runtime.getManifest().version;
// Get storage type
SysTrayX.Info.storageType = "local"
/*
SysTrayX.Info.storageType = await browser.storage.sync
.get("storageType")
.then((result) => result.storageType || "sync");
.then((result) => result.storageType || "local");
*/
SysTrayX.Info.displayInfo();
// Set link in options pageF
@@ -1198,6 +1204,8 @@ async function start() {
if (SysTrayX.Info.browserInfo.majorVersion > 89) {
document.getElementById("counttype").style.display = "none";
}
// Disable debug/test items
document.getElementById("storageselect").style.display = "none";
// Setup account tree
const accountsInitPromise = () =>