Files
SysTray-X/webext/js/defaults.js

877 lines
24 KiB
JavaScript
Raw Normal View History

2021-09-04 22:16:18 +02:00
//
// Get the prefered storage
//
2023-09-06 22:04:41 +02:00
function storage() {
2021-09-04 22:16:18 +02:00
console.log("Using local storage");
return browser.storage.local;
}
2023-11-13 20:14:30 +01:00
//
// Get API count method preference
//
async function getApiCountMethod() {
function resolve(result) {
const apiCountMethod = result.apiCountMethod || "false";
return apiCountMethod;
}
function reject() {
return "false";
}
return await storage().get("apiCountMethod").then(resolve, reject);
}
2021-09-04 22:16:18 +02:00
//
// Get window startup state
//
2023-11-14 22:43:51 +01:00
async function getStartupType() {
2021-09-04 22:16:18 +02:00
function resolve(result) {
2023-11-15 13:03:21 +01:00
const startupType = result.startupType || "0";
2023-11-14 22:43:51 +01:00
return startupType;
2021-09-04 22:16:18 +02:00
}
function reject() {
2023-11-14 22:43:51 +01:00
return "2";
2021-09-04 22:16:18 +02:00
}
2023-11-14 22:43:51 +01:00
return await storage().get("startupType").then(resolve, reject);
2021-09-04 22:16:18 +02:00
}
//
// Get window restore position state
//
async function getRestorePositionsState() {
function resolve(result) {
const restorePositions = result.restorePositions || "false";
return restorePositions;
}
function reject() {
return "false";
}
return await storage().get("restorePositions").then(resolve, reject);
}
//
// Get window startup window positions
//
async function getStartupWindowPositions() {
function resolve(result) {
const windowPositions = result.windowPositions || [];
return windowPositions;
}
function reject() {
return [];
}
return await storage().get("windowPositions").then(resolve, reject);
}
//
// Get close type
//
async function getCloseType() {
function resolve(result) {
return result.closeType || "1";
}
function reject() {
return undefined;
}
return await storage().get("closeType").then(resolve, reject);
}
//
// Get KDE integration, default icon hide
//
async function getHideDefaultIcon() {
function resolve(result) {
const hideDefaultIcon = result.hideDefaultIcon || "false";
return hideDefaultIcon === "true";
}
function reject() {
return false;
}
return await storage().get("hideDefaultIcon").then(resolve, reject);
}
2020-01-24 22:20:30 +01:00
//
2020-05-22 22:54:10 +02:00
// Set default default icon
2020-01-24 22:20:30 +01:00
// Use <div> as storage
//
async function getDefaultIcon() {
2021-09-04 22:16:18 +02:00
function resolve(result) {
const defaultIconMime = result.defaultIconMime || "";
const defaultIcon = result.defaultIcon || "";
return { defaultIconMime, defaultIcon };
2020-05-22 22:54:10 +02:00
}
2021-09-04 22:16:18 +02:00
function reject() {
return "";
2020-05-22 22:54:10 +02:00
}
const { defaultIconMime, defaultIcon } = await storage()
2021-09-04 22:16:18 +02:00
.get(["defaultIconMime", "defaultIcon"])
.then(resolve, reject);
2020-05-22 22:54:10 +02:00
if (defaultIconMime === "" || defaultIcon === "") {
2020-05-22 22:54:10 +02:00
const toDataURL = (url) =>
fetch(url)
.then((response) => response.blob())
.then(
(blob) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
})
);
// Convert image to storage param
var defaultIconPath;
if (SysTrayX.Info.browserInfo.majorVersion < 115) {
defaultIconPath = "icons/Thunderbird.png";
} else {
defaultIconPath = "icons/Thunderbird115.png";
}
const { defaultIconMimeUrl, defaultIconBase64Url } = await toDataURL(
defaultIconPath
2020-05-22 22:54:10 +02:00
).then((dataUrl) => {
const data = dataUrl.split(":").pop().split(",");
return {
defaultIconMimeUrl: data[0].split(";")[0],
defaultIconBase64Url: data[1],
2020-05-22 22:54:10 +02:00
};
});
// Store default icon (base64)
2021-09-04 22:16:18 +02:00
await storage().set({
defaultIconMime: defaultIconMimeUrl,
defaultIcon: defaultIconBase64Url,
2020-05-22 22:54:10 +02:00
});
// Store in HTML
const defaultIconDiv = document.getElementById("defaultIcon");
defaultIconDiv.setAttribute("data-default-icon-mime", defaultIconMimeUrl);
defaultIconDiv.setAttribute("data-default-icon", defaultIconBase64Url);
2020-05-22 22:54:10 +02:00
}
}
//
// Set default unread icon
// Use <div> as storage
//
async function getIcon() {
2021-09-04 22:16:18 +02:00
function resolve(result) {
const iconMime = result.iconMime || "";
const icon = result.icon || "";
return { iconMime, icon };
2020-01-24 22:20:30 +01:00
}
2021-09-04 22:16:18 +02:00
function reject() {
return "";
2020-01-24 22:20:30 +01:00
}
const { iconMime, icon } = await storage()
2021-09-04 22:16:18 +02:00
.get(["iconMime", "icon"])
.then(resolve, reject);
2020-01-24 22:20:30 +01:00
if (iconMime === "" || icon === "") {
const toDataURL = (url) =>
2020-01-24 22:20:30 +01:00
fetch(url)
.then((response) => response.blob())
2020-01-24 22:20:30 +01:00
.then(
(blob) =>
2020-01-24 22:20:30 +01:00
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
})
);
// Convert image to storage param
const { iconMimeUrl, iconBase64Url } = await toDataURL(
"icons/blank-icon.png"
).then((dataUrl) => {
const data = dataUrl.split(":").pop().split(",");
return { iconMimeUrl: data[0].split(";")[0], iconBase64Url: data[1] };
});
2020-01-24 22:20:30 +01:00
// Store default icon (base64)
2021-09-04 22:16:18 +02:00
await storage().set({
iconMime: iconMimeUrl,
icon: iconBase64Url,
2020-01-24 22:20:30 +01:00
});
// Store in HTML
const iconDiv = document.getElementById("icon");
iconDiv.setAttribute("data-icon-mime", iconMimeUrl);
iconDiv.setAttribute("data-icon", iconBase64Url);
2020-01-24 22:20:30 +01:00
}
}
2020-02-29 20:36:08 +01:00
2023-12-16 19:11:50 +01:00
//
// Find a path in the account folder tree
//
function findFolder(rootFolder, path) {
function traverse(folder) {
if (folder === undefined) {
return undefined;
}
if (folder.path === path) {
return folder;
}
if (folder.subFolders === undefined) {
return undefined;
}
for (let sub of folder.subFolders) {
const found = traverse(sub);
if( found !== undefined ) {
return found;
}
}
return undefined;
}
const found = traverse(rootFolder);
return found;
}
//
2020-05-12 21:38:03 +02:00
// Get filters
//
2020-05-12 21:38:03 +02:00
async function getFilters() {
2023-12-16 19:11:50 +01:00
async function resolve(result) {
2021-09-12 16:51:55 +02:00
let filters = result.filters || undefined;
2020-05-13 21:24:29 +02:00
2021-09-12 16:51:55 +02:00
if (filters === undefined || filters.length === 0) {
//
// No filters defined, create base filters
//
console.debug("Stored filters: undefined or empty");
2020-05-21 12:12:10 +02:00
2021-09-12 16:51:55 +02:00
filters = [];
for (const account of SysTrayX.Messaging.accounts) {
2023-12-16 19:11:50 +01:00
let inbox;
if (account.rootFolder !== undefined) {
inbox = account.rootFolder.subFolders.filter(
(folder) => folder.type === "inbox"
);
} else {
inbox = account.folders.filter(
(folder) => folder.type === "inbox"
);
}
2020-05-13 21:24:29 +02:00
2021-09-12 16:51:55 +02:00
if (inbox.length > 0) {
2023-09-06 22:04:41 +02:00
filters.push({
accountId: inbox[0].accountId,
version: SysTrayX.Info.version,
2023-12-16 19:11:50 +01:00
folders: [{mailFolderId: inbox[0].id, path: inbox[0].path}],
2023-09-06 22:04:41 +02:00
});
2021-09-12 16:51:55 +02:00
}
}
2021-09-04 22:16:18 +02:00
2021-09-12 16:51:55 +02:00
console.debug("Force new filters: " + JSON.stringify(filters));
2021-08-12 22:33:29 +02:00
} else {
2021-09-12 16:51:55 +02:00
console.debug("Stored filters: " + JSON.stringify(filters));
2023-12-16 19:11:50 +01:00
if (filters[0].folders.length > 0) {
if (typeof(filters[0].folders[0]) === "string") {
console.debug("Conversion needed");
// Convert the filter folder path list to an object
// containing at least the path and if available the mail folder id (TB 121 and newer)
const newFilters = [];
for (const account of SysTrayX.Messaging.accounts) {
2024-04-27 20:23:40 +02:00
let rootFolder = account.rootFolder
if( rootFolder === undefined ) {
rootFolder = {
path: "",
subFolders: account.folders
};
}
2023-12-16 19:11:50 +01:00
const filter = filters.filter(
(filter) => filter.accountId === account.id
);
console.debug("Found filters: " + filter.length);
console.debug("Found filter: " + JSON.stringify(filter));
if (filter.length === 1) {
let newFilter = {
accountId: filter[0].accountId,
version: SysTrayX.Info.version,
folders: []
};
for (const path of filter[0].folders) {
2024-04-27 20:23:40 +02:00
const accountFolder = findFolder(rootFolder, path);
2023-12-16 19:11:50 +01:00
if (accountFolder !== undefined)
{
newFilter.folders.push({mailFolderId: accountFolder.id, path: accountFolder.path});
}
}
newFilters.push(newFilter);
}
}
filters = newFilters;
console.debug("Force new filters: " + JSON.stringify(filters));
// Save the new filters
await storage().set({
filters: filters,
});
} else {
if (filters[0].folders[0].mailFolderId === undefined &&
SysTrayX.Info.browserInfo.majorVersion >= 121) {
console.debug("Update needed");
// Update filter folder object
// with the the mail folder id
const newFilters = [];
for (const account of SysTrayX.Messaging.accounts) {
const filter = filters.filter(
(filter) => filter.accountId === account.id
);
console.debug("Found filters: " + filter.length);
console.debug("Found filter: " + JSON.stringify(filter));
if (filter.length === 1) {
let newFilter = {
accountId: filter[0].accountId,
version: SysTrayX.Info.version,
folders: []
};
for (const folder of filter[0].folders) {
const accountFolder = findFolder(account.rootFolder, folder.path)
if (accountFolder !== undefined)
{
newFilter.folders.push({mailFolderId: accountFolder.id, path: accountFolder.path});
}
}
newFilters.push(newFilter);
}
}
filters = newFilters;
console.debug("Force new filters: " + JSON.stringify(filters));
// Save the new filters
await storage().set({
filters: filters,
});
}
}
}
2021-08-12 22:33:29 +02:00
}
2021-09-12 16:51:55 +02:00
return filters;
2021-08-10 19:58:16 +02:00
}
2021-09-04 22:16:18 +02:00
function reject() {
return undefined;
}
2021-09-04 22:16:18 +02:00
return await storage().get("filters").then(resolve, reject);
}
//
// Get count type
//
async function getCountType() {
2021-09-04 22:16:18 +02:00
function resolve(result) {
return result.countType || "0";
}
2021-09-04 22:16:18 +02:00
function reject() {
2023-11-13 20:14:30 +01:00
return "0";
2020-08-02 21:32:09 +02:00
}
2021-09-04 22:16:18 +02:00
return await storage().get("countType").then(resolve, reject);
2020-08-02 21:32:09 +02:00
}
2021-08-10 19:58:16 +02:00
2023-11-13 20:14:30 +01:00
//
// Get show new indicator
//
async function getShowNewIndicator() {
function resolve(result) {
return result.showNewIndicator || "false";
}
function reject() {
return "false";
}
return await storage().get("showNewIndicator").then(resolve, reject);
}
//
2023-08-25 20:44:32 +02:00
// Get start app parameters
//
2023-08-25 18:18:20 +02:00
async function getStartAppParam() {
function resolve(result) {
2023-08-25 18:18:20 +02:00
const startApp = result.startApp || "";
const startAppArgs = result.startAppArgs || "";
return { startApp, startAppArgs };
}
function reject() {
2023-08-25 18:18:20 +02:00
return { startApp: "", startAppArgs: "" };
}
return await storage()
2023-08-25 18:18:20 +02:00
.get(["startApp", "startAppArgs"])
.then(resolve, reject);
}
//
// Get new message defaults
//
async function getNewMessageDefaults() {
function resolve(result) {
const newMessageDefaults = result.newMessageDefaults || "";
if (newMessageDefaults !== "")
{
return JSON.parse(newMessageDefaults);
}
return {};
}
function reject() {
return {};
}
return await storage().get("newMessageDefaults").then(resolve, reject);
}
2021-08-10 19:58:16 +02:00
// Helper funcs for TB91 and later folder handling
2021-08-10 21:39:35 +02:00
async function getMailFolderInfo(folder) {
return await browser.folders.getFolderInfo(folder);
}
2021-08-10 19:58:16 +02:00
// Check if a folder is in the filter list
2021-08-10 20:13:33 +02:00
function isFolderInFilters(folder) {
2021-09-12 16:51:55 +02:00
const accountIndex = SysTrayX.Messaging.filters.findIndex(
(account) => account.accountId === folder.accountId
2021-08-10 19:58:16 +02:00
);
2021-09-12 16:51:55 +02:00
if (accountIndex !== -1) {
2023-12-16 19:11:50 +01:00
const folders = SysTrayX.Messaging.filters[accountIndex].folders;
if (folders[0] === "string") {
// Filters pre TB 121
return (
folders.filter(
(path) => path === folder.path
).length > 0
);
} else {
// Filters TB 121
return (
folders.map(f => f.path).filter(
(path) => path === folder.path
).length > 0
);
}
2021-09-12 16:51:55 +02:00
} else {
return false;
}
2021-08-10 19:58:16 +02:00
}
// Check if the parent folder of a folder is in the filter list
2021-08-10 20:13:33 +02:00
function isParentFolderInFilters(folder) {
2021-08-10 19:58:16 +02:00
const parentPath = folder.path.substring(0, folder.path.lastIndexOf("/"));
2021-09-12 16:51:55 +02:00
const accountIndex = SysTrayX.Messaging.filters.findIndex(
(account) => account.accountId === folder.accountId
2021-08-10 19:58:16 +02:00
);
2021-09-12 16:51:55 +02:00
if (accountIndex !== -1) {
2023-12-16 19:11:50 +01:00
const folders = SysTrayX.Messaging.filters[accountIndex].folders;
if (folders[0] === "string") {
// Filters pre TB 121
return (
folders.filter(
(path) => path === parentPath
).length > 0
);
} else {
// Filters TB 121
return (
folders.map(f => f.path).filter(
(path) => path === parentPath
).length > 0
);
}
2021-09-12 16:51:55 +02:00
} else {
return false;
}
2021-08-10 19:58:16 +02:00
}
// Delete a folder from the filter list
2021-09-04 22:16:18 +02:00
async function deleteFolderFromFilters(folder) {
2021-09-12 16:51:55 +02:00
const accountIndex = SysTrayX.Messaging.filters.findIndex(
(account) => account.accountId === folder.accountId
2021-08-10 19:58:16 +02:00
);
2021-09-12 16:51:55 +02:00
if (accountIndex !== -1) {
const account = SysTrayX.Messaging.filters[accountIndex];
2023-12-16 19:11:50 +01:00
if (account.folders[0] === "string" ) {
// Filters pre TB 121
account.folders = account.folders.filter((path) => path !== folder.path);
} else {
// Filters TB 121
account.folders = account.folders.filter((f) => f.path !== folder.path);
}
2021-09-12 16:51:55 +02:00
// Store the new filters
await storage().set({
filters: SysTrayX.Messaging.filters,
});
}
2021-08-10 19:58:16 +02:00
}
// Get the account name from an id
function getAccountName(id) {
2021-09-12 16:51:55 +02:00
const account = SysTrayX.Messaging.accounts.find(
2021-08-10 19:58:16 +02:00
(account) => account.id === id
2021-09-12 16:51:55 +02:00
);
2021-08-10 19:58:16 +02:00
return account.name;
}
// Add a folder to the filter list
2021-09-04 22:16:18 +02:00
async function addFolderToFilters(newFolder) {
2021-09-12 16:51:55 +02:00
const accountIndex = SysTrayX.Messaging.filters.findIndex(
(account) => account.accountId === newFolder.accountId
);
2021-09-12 16:51:55 +02:00
if (accountIndex !== -1) {
const account = SysTrayX.Messaging.filters[accountIndex];
2023-12-16 19:11:50 +01:00
if (account.folders[0] === "string" ) {
account.folders.push(newFolder.path);
} else {
account.folders.push({mailFolderId: newFolder.id, path: newFolder.path});
}
2021-09-12 16:51:55 +02:00
// Store the new filter
await storage().set({
filters: SysTrayX.Messaging.filters,
});
}
}
2023-09-10 21:08:31 +02:00
// Collect unread mail
const collectUnreadMail = async () => {
2024-07-28 21:27:59 +02:00
// Count the initial unread messages
2023-09-10 21:08:31 +02:00
for (const filter of SysTrayX.Messaging.filters) {
2023-12-16 19:11:50 +01:00
const accountId = filter.accountId;
for (const storedFolder of filter.folders) {
let path;
2024-07-28 21:27:59 +02:00
let folderParam;
2023-12-16 19:11:50 +01:00
if (typeof(storedFolder) === "string") {
// Filters pre TB 121
path = storedFolder;
2024-07-28 21:27:59 +02:00
folderParam = {
accountId: accountId,
path: storedFolder,
};
2023-12-16 19:11:50 +01:00
} else {
// Filters TB 121
2024-07-28 21:27:59 +02:00
if (storedFolder.mailFolderId === undefined ||
SysTrayX.Info.browserInfo.majorVersion < 121) {
// TB 121 filter setup but older TB
path = storedFolder.path;
folderParam = {
accountId: accountId,
path: path,
};
} else {
path = storedFolder.path;
folderParam = storedFolder.mailFolderId;
2023-12-16 19:11:50 +01:00
}
2023-09-10 21:08:31 +02:00
}
2024-07-28 21:27:59 +02:00
// Check unread mails
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folderParam);
} catch (err) {
//console.debug("Filter error: " + err);
//console.debug("Filter error: " + JSON.stringify(folder));
// Get all accounts
SysTrayX.Messaging.accounts = await browser.accounts.list();
// Check the filters for the accounts
SysTrayX.Messaging.accountFilterCheck();
}
2024-07-29 22:03:24 +02:00
//console.debug("collectUnreadMail: folderParam" + JSON.stringify(folderParam));
//console.debug("collectUnreadMail: mailFolderInfo" + JSON.stringify(mailFolderInfo));
2023-09-10 21:08:31 +02:00
if (mailFolderInfo.unreadMessageCount !== undefined) {
2023-12-16 19:11:50 +01:00
if (SysTrayX.Messaging.unread[accountId] === undefined) {
SysTrayX.Messaging.unread[accountId] = {};
2023-09-10 21:08:31 +02:00
}
2023-12-16 19:11:50 +01:00
SysTrayX.Messaging.unread[accountId][path] =
2023-09-10 21:08:31 +02:00
mailFolderInfo.unreadMessageCount;
}
2024-07-28 21:27:59 +02:00
// Check if the new mails have been read, removed from new storage
if (SysTrayX.Messaging.new[accountId] !== undefined &&
SysTrayX.Messaging.new[accountId][path] !== undefined ) {
const messages = SysTrayX.Messaging.new[accountId][path];
2024-07-29 22:03:24 +02:00
//console.debug("collectUnreadMail: new stored" + JSON.stringify(messages));
2024-07-28 21:27:59 +02:00
if (messages.length > 0) {
const newMessages = [];
for (let i = 0; i < messages.length; ++i) {
const message = messages[i];
const getHeaderPromise = (messageId) =>
new Promise((res) => res(messenger.messages.get(messageId)));
const header = await getHeaderPromise(message.id);
2024-07-29 22:03:24 +02:00
//console.debug("collectUnreadMail: header" + JSON.stringify(header));
if (header.read === false &&
(header.new === undefined || header.new === true) &&
header.headerMessageId !== "") {
2024-07-28 21:27:59 +02:00
newMessages.push(message);
2024-07-29 22:03:24 +02:00
//console.debug("collectUnreadMail: renew" + JSON.stringify(message));
2024-07-28 21:27:59 +02:00
}
}
SysTrayX.Messaging.new[accountId][path] = [
...newMessages,
];
}
}
2023-09-10 21:08:31 +02:00
}
}
2024-07-28 21:27:59 +02:00
}
2023-09-10 21:08:31 +02:00
// Count and send the unread and new mails
2024-07-28 21:27:59 +02:00
const sendMailCountPre115 = async () => {
2023-11-13 20:14:30 +01:00
if (SysTrayX.Info.browserInfo.majorVersion < 115 || SysTrayX.Messaging.apiCountMethod === "false") {
2023-09-11 21:31:07 +02:00
// Collect the unread mail
2024-07-28 21:27:59 +02:00
await collectUnreadMail();
2023-09-11 21:31:07 +02:00
// Count the collected mail
let unreadCount = 0;
let newCount = 0;
SysTrayX.Messaging.filters.forEach((filter) => {
const accountId = filter.accountId;
2023-12-16 19:11:50 +01:00
filter.folders.forEach((storedFolder) => {
let path;
if (typeof(storedFolder) === "string") {
// Filters pre TB 121
path = storedFolder;
} else {
// Filters TB 121
path = storedFolder.path;
}
2023-09-11 21:31:07 +02:00
if (SysTrayX.Messaging.unread[accountId] !== undefined) {
if (SysTrayX.Messaging.unread[accountId][path] !== undefined) {
unreadCount = unreadCount + SysTrayX.Messaging.unread[accountId][path];
}
}
2023-09-11 21:31:07 +02:00
if (SysTrayX.Messaging.new[accountId] !== undefined) {
if (SysTrayX.Messaging.new[accountId][path] !== undefined) {
newCount = newCount + SysTrayX.Messaging.new[accountId][path].length;
}
}
2023-09-11 21:31:07 +02:00
});
});
2023-09-11 21:31:07 +02:00
//console.debug("Filters: " + JSON.stringify(SysTrayX.Messaging.filters));
//console.debug("New: " + JSON.stringify(SysTrayX.Messaging.new));
2024-07-28 21:27:59 +02:00
//console.debug("sendMailCountPre115 Unread storage: " + JSON.stringify(SysTrayX.Messaging.unread));
//console.debug("sendMailCountPre115 New storage: " + JSON.stringify(SysTrayX.Messaging.new));
2023-10-03 21:46:28 +02:00
//console.debug("sendMailCountPre115 Unread: " + unreadCount);
//console.debug("sendMailCountPre115 New: " + newCount);
2023-09-09 19:58:00 +02:00
2023-09-11 21:31:07 +02:00
SysTrayX.Link.postSysTrayXMessage( { mailCount: { unread: unreadCount, new: newCount } } );
}
2022-07-03 14:16:05 +02:00
};
2023-09-06 21:21:06 +02:00
2023-09-10 21:08:31 +02:00
// Count and send the unread and new mails (>TB115)
2023-09-11 21:31:07 +02:00
const sendMailCount = async () => {
2023-11-13 20:14:30 +01:00
if (SysTrayX.Info.browserInfo.majorVersion >= 115 && SysTrayX.Messaging.apiCountMethod === "true") {
2023-09-06 21:21:06 +02:00
2023-10-16 22:09:00 +02:00
// New only works for >=TB106
2023-09-06 21:21:06 +02:00
2023-09-11 21:31:07 +02:00
let unreadCount = 0;
let newCount = 0;
2023-09-06 21:21:06 +02:00
2023-11-13 20:14:30 +01:00
if( SysTrayX.Messaging.countType === "1" || SysTrayX.Messaging.showNewIndicator === "true" ) {
// Get both unread and new message count
2023-12-16 21:05:24 +01:00
if (SysTrayX.Info.browserInfo.majorVersion < 121) {
for (const filter of SysTrayX.Messaging.filters) {
for (const storedFolder of filter.folders) {
let listParam;
if (typeof(storedFolder) === "string") {
// Filters pre TB 121
listParam = {
accountId: filter.accountId,
path: storedFolder,
};
} else {
// Filters TB 121
2024-04-27 20:23:40 +02:00
listParam = {
accountId: filter.accountId,
path: storedFolder.path,
};
2023-09-11 21:31:07 +02:00
}
2023-12-16 21:05:24 +01:00
async function* listMessages(listParam) {
let page = await messenger.messages.list(listParam);
2023-11-13 20:14:30 +01:00
for (let message of page.messages) {
yield message;
}
2023-12-16 21:05:24 +01:00
while (page.id) {
page = await messenger.messages.continueList(page.id);
for (let message of page.messages) {
yield message;
}
}
}
let messages = listMessages(listParam);
for await (let message of messages) {
if( message.new )
{
newCount = newCount + 1;
}
if( !message.read )
{
unreadCount = unreadCount + 1;
}
2023-11-13 20:14:30 +01:00
}
}
2023-12-16 21:05:24 +01:00
}
} else {
// Unread and new count TB 121 and newer
for (const filter of SysTrayX.Messaging.filters) {
for (const folder of filter.folders) {
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folder.mailFolderId);
} catch (err) {
//console.debug("Filter error: " + err);
//console.debug("Filter error: " + JSON.stringify(folder));
// Get all accounts
SysTrayX.Messaging.accounts = await browser.accounts.list();
// Check the filters for the accounts
SysTrayX.Messaging.accountFilterCheck();
}
if (mailFolderInfo.unreadMessageCount !== undefined) {
unreadCount = unreadCount + mailFolderInfo.unreadMessageCount;
2023-11-13 20:14:30 +01:00
}
2023-12-16 21:05:24 +01:00
if (mailFolderInfo.newMessageCount !== undefined) {
newCount = newCount + mailFolderInfo.newMessageCount;
2023-11-13 20:14:30 +01:00
}
2023-09-11 21:31:07 +02:00
}
2023-09-06 21:21:06 +02:00
}
2023-11-13 20:14:30 +01:00
}
} else {
// Only unread count
for (const filter of SysTrayX.Messaging.filters) {
2023-12-16 19:11:50 +01:00
for (const storedFolder of filter.folders) {
let folderParam;
if (typeof(storedFolder) === "string") {
// Filters pre TB 121
folderParam = {
accountId: filter.accountId,
path: storedFolder,
};
} else {
// Filters TB 121
folderParam = storedFolder.mailFolderId;
}
2023-11-13 20:14:30 +01:00
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folder);
} catch (err) {
//console.debug("Filter error: " + err);
//console.debug("Filter error: " + JSON.stringify(folder));
// Get all accounts
2023-12-16 19:11:50 +01:00
SysTrayX.Messaging.accounts = await browser.accounts.list();
2023-11-13 20:14:30 +01:00
// Check the filters for the accounts
SysTrayX.Messaging.accountFilterCheck();
2023-09-11 21:31:07 +02:00
}
2023-09-06 21:21:06 +02:00
2023-11-13 20:14:30 +01:00
if (mailFolderInfo.unreadMessageCount !== undefined) {
unreadCount = unreadCount + mailFolderInfo.unreadMessageCount;
2023-09-11 21:31:07 +02:00
}
2023-09-06 21:21:06 +02:00
}
}
}
2023-10-03 21:46:28 +02:00
//console.debug("sendMailCount Unread: " + unreadCount);
//console.debug("sendMailCount New: " + newCount);
2023-09-11 21:31:07 +02:00
SysTrayX.Link.postSysTrayXMessage( { mailCount: { unread: unreadCount, new: newCount } } );
}
2023-09-06 21:21:06 +02:00
};