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: The addon and system tray application can do:
- custom new mail icon - custom new mail icon
- display number of unread / new mails - display number of unread mails
- show / hide Thunderbird (minimize) - show / hide Thunderbird (minimize)
- minimizing hides to tray (remove Thunderbird from taskbar, pager and switcher when minimized) - minimizing hides to tray (remove Thunderbird from taskbar, pager and switcher when minimized)
- minimize on startup - minimize on startup

View File

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

View File

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

View File

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