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

625 lines
15 KiB
JavaScript
Raw Normal View History

2021-09-04 22:16:18 +02:00
//
// Get the prefered storage
//
function storage(store) {
if (SysTrayX.Info.browserInfo.majorVersion < 91 || store === "sync") {
2021-09-04 22:16:18 +02:00
console.log("Using sync storage");
return browser.storage.sync;
} else {
console.log("Using local storage");
return browser.storage.local;
}
}
//
// Get window startup state
//
async function getStartupState() {
function resolve(result) {
const startMinimized = result.startMinimized || "false";
return startMinimized === "true" ? "minimized" : "normal";
}
function reject() {
return "normal";
}
return await storage().get("startMinimized").then(resolve, reject);
}
//
// 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
const { defaultIconMimeUrl, defaultIconBase64Url } = await toDataURL(
2020-05-22 22:54:10 +02:00
"icons/Thunderbird.png"
).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
//
2020-05-21 22:49:27 +02:00
// Check if the filters are for existing accounts
2021-09-04 22:16:18 +02:00
//
2020-05-21 22:49:27 +02:00
function checkAccountFilters(filters) {
let filtersChanged = false;
let newFilters = [];
// Create an id -> name map
accountNames = {};
for (const account of SysTrayX.Messaging.accounts) {
accountNames[account.id] = account.name;
}
for (let i = 0; i < filters.length; ++i) {
let filter = filters[i];
const accountName = accountNames[filter.folder.accountId];
if (accountName === undefined) {
filtersChanged = true;
continue;
}
// Store the passed filter
newFilters.push(filter);
}
return filtersChanged ? newFilters : undefined;
}
// Create a folders array with paths for TB74+
function createFoldersArrayPre74(baseFolders) {
let result = [];
let level = { result };
baseFolders.forEach((folder) => {
folder.path
.slice(1)
.split("/")
.reduce((r, name, i, a) => {
if (!r[name]) {
r[name] = { result: [] };
r.result.push({
accountId: folder.accountId,
name: folder.name,
2020-12-08 21:32:23 +01:00
type: folder.type,
2020-05-21 22:49:27 +02:00
path: folder.path,
subFolders: r[name].result,
});
}
return r[name];
}, level);
});
return createFoldersArray(result);
}
// Create a folders array with paths for TB74+
function createFoldersArray(baseFolders) {
let newFolders = [];
function traverse(path, folders) {
if (!folders) {
return;
}
for (let i = 0; i < folders.length; ++i) {
newFolders.push({
...folders[i],
pathOrig: folders[i].path,
path: path + "/" + folders[i].name,
});
traverse(folders[i].path, folders[i].subFolders);
}
}
traverse("", baseFolders);
return newFolders;
}
// Check if the filter folders still exists
function checkFolderFilters(filters) {
let filtersChanged = false;
let newFilters = [];
// Create an id -> name map
accountNames = {};
accountFolders = {};
for (const account of SysTrayX.Messaging.accounts) {
accountNames[account.id] = account.name;
2021-09-04 22:16:18 +02:00
if (SysTrayX.Info.browserInfo.majorVersion < 74) {
2020-05-21 22:49:27 +02:00
// Pre TB74 accounts API
accountFolders[account.id] = createFoldersArrayPre74(account.folders);
} else {
// TB74+ accounts API
accountFolders[account.id] = createFoldersArray(account.folders);
}
}
for (let i = 0; i < filters.length; ++i) {
let filter = filters[i];
const found = accountFolders[filter.folder.accountId].filter((folder) => {
if (filter.folder.version) {
2020-12-08 21:32:23 +01:00
const folderPaths = folder.path.split("/");
const folderPathLast = folderPaths.pop();
const folderPathFirst = folderPaths.join("/");
const filterFolderPaths = filter.folder.path.split("/");
const filterFolderPathLast = filterFolderPaths.pop();
const filterFolderPathFirst = filterFolderPaths.join("/");
if (
folderPathFirst === filterFolderPathFirst &&
folderPathLast !== filterFolderPathLast &&
((folder.type === "inbox" && filter.folder.type === "inbox") ||
(folder.type === "drafts" && filter.folder.type === "drafts") ||
2020-12-08 21:59:02 +01:00
(folder.type === "sent" && filter.folder.type === "sent") ||
(folder.type === "trash" && filter.folder.type === "trash") ||
(folder.type === "templates" &&
filter.folder.type === "templates") ||
(folder.type === "archives" && filter.folder.type === "archives") ||
(folder.type === "junk" && filter.folder.type === "junk") ||
2020-12-08 21:32:23 +01:00
(folder.type === "outbox" && filter.folder.type === "outbox"))
) {
filter.folder.path = folder.path;
filter.folder.name = folder.name;
filtersChanged = true;
return true;
}
if (folder.path === filter.folder.path) {
2020-12-08 21:59:02 +01:00
if (
folder.type != filter.folder.type &&
filter.folder.type == undefined
) {
2020-12-08 21:32:23 +01:00
filter.folder.type = folder.type !== undefined ? folder.type : "";
filtersChanged = true;
}
return true;
}
return false;
2020-05-21 22:49:27 +02:00
} else {
return folder.pathOrig === filter.folder.path;
}
});
if (found.length === 0) {
2020-12-08 21:32:23 +01:00
console.debug("Removed filter: " + JSON.stringify(filter));
2020-05-21 22:49:27 +02:00
filtersChanged = true;
continue;
}
// Upgrade filter
let folder = filter.folder;
if (filter.folder.accountName === undefined) {
folder.accountName = accountNames[found[0].accountId];
filtersChanged = true;
}
if (filter.folder.name === undefined) {
folder.name = found[0].name;
filtersChanged = true;
}
if (filter.folder.version === undefined) {
2021-09-04 22:16:18 +02:00
folder.version = SysTrayX.Info.version;
2020-05-21 22:49:27 +02:00
folder.path = found[0].path;
2020-05-21 23:49:54 +02:00
folder.name = found[0].path.split("/").pop();
2020-05-21 22:49:27 +02:00
filtersChanged = true;
}
// Store the passed filter
newFilters.push(filter);
}
return filtersChanged ? newFilters : undefined;
}
2020-05-10 23:44:12 +02:00
//
2020-05-12 21:38:03 +02:00
// Check filters
2020-05-10 23:44:12 +02:00
//
2020-05-13 21:24:29 +02:00
function checkFilters(filters) {
2020-05-14 20:41:06 +02:00
let newFilters = [];
2020-05-13 21:24:29 +02:00
2021-09-04 22:16:18 +02:00
if (filters === undefined || filters.length === 0) {
//
// No filters defined, create base filters
//
2020-05-13 23:54:59 +02:00
for (const account of SysTrayX.Messaging.accounts) {
const inbox = account.folders.filter((folder) => folder.type == "inbox");
if (inbox.length > 0) {
2021-09-04 22:16:18 +02:00
let folder = {};
if (SysTrayX.Info.browserInfo.majorVersion < 91) {
// console.debug("Folder pre 91: " + JSON.stringify(inbox[0]));
folder = {
...inbox[0],
accountName: account.name,
path: "/" + inbox[0].name,
version: SysTrayX.Info.version,
};
delete folder.subFolders;
newFilters.push({
unread: true,
folder: folder,
});
} else {
// console.debug("Folder 91+: " + JSON.stringify(inbox[0]));
newFilters.push({
accountId: inbox[0].accountId,
version: SysTrayX.Info.version,
folders: [inbox[0].path],
});
}
2020-05-13 23:54:59 +02:00
}
}
} else {
const convertTo91Filters =
SysTrayX.Info.browserInfo.majorVersion >= 91 &&
filters[0].unread !== undefined;
console.debug("Convert filter: " + convertTo91Filters);
2020-05-21 22:49:27 +02:00
// Check the filters
2021-09-04 22:16:18 +02:00
newFilters = filters;
2020-05-14 20:41:06 +02:00
2021-09-04 22:16:18 +02:00
/*
2020-05-21 22:49:27 +02:00
let filtersChanged = false;
let tmpFilters = undefined;
2020-05-14 20:41:06 +02:00
2020-05-21 22:49:27 +02:00
// Check if the filters are for the current accounts
tmpFilters = checkAccountFilters(newFilters);
if (tmpFilters) {
newFilters = tmpFilters;
2021-09-04 22:16:18 +02:00
console.debug("Filters Accounts updated")
2020-05-21 22:49:27 +02:00
filtersChanged = true;
2020-05-14 20:41:06 +02:00
}
2020-05-21 22:49:27 +02:00
// Check if the filters are for the current folders
tmpFilters = checkFolderFilters(newFilters);
if (tmpFilters) {
newFilters = tmpFilters;
2021-09-04 22:16:18 +02:00
console.debug("Filters Folders updated")
2020-05-21 22:49:27 +02:00
filtersChanged = true;
}
2020-05-13 23:19:17 +02:00
2020-05-21 22:49:27 +02:00
if (filtersChanged) {
2020-05-13 23:54:59 +02:00
//
// Store the converted filters
//
2021-09-04 22:16:18 +02:00
await storage().set({
2020-05-13 21:24:29 +02:00
filters: newFilters,
});
}
2021-09-04 22:16:18 +02:00
*/
2020-05-10 23:44:12 +02:00
}
2020-05-13 21:24:29 +02:00
return newFilters;
2020-05-10 23:44:12 +02:00
}
//
2020-05-12 21:38:03 +02:00
// Get filters
//
2020-05-12 21:38:03 +02:00
async function getFilters() {
2021-09-04 22:16:18 +02:00
function resolve(result) {
2020-05-14 20:41:06 +02:00
const filters = result.filters || undefined;
2020-05-13 21:24:29 +02:00
2021-09-04 22:16:18 +02:00
if (filters !== undefined) {
console.debug("Stored filters: " + JSON.stringify(filters));
} else {
console.debug("Stored filters: undefined");
}
2020-05-21 12:12:10 +02:00
2020-05-14 20:41:06 +02:00
const newFilters = checkFilters(filters);
2020-05-13 21:24:29 +02:00
2021-08-17 23:21:11 +02:00
if (filters === undefined || filters.length === 0) {
2021-09-04 22:16:18 +02:00
console.debug("Force new filters: " + JSON.stringify(newFilters));
2021-08-12 22:33:29 +02:00
return newFilters;
} else {
return filters;
}
/*
2020-05-21 22:49:27 +02:00
console.debug("Checked filters: " + JSON.stringify(newFilters));
2020-05-21 12:12:10 +02:00
2021-08-11 22:22:10 +02:00
return newFilters;
*/
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() {
2020-08-02 21:32:09 +02:00
return undefined;
}
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
// 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-08-10 19:58:16 +02:00
return (
SysTrayX.Messaging.filters.filter(
(filter) =>
filter.folder.accountId === folder.accountId &&
(filter.folder.path === folder.path ||
filter.folder.path.toUpperCase() === folder.path)
).length !== 0
);
}
// 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("/"));
return (
SysTrayX.Messaging.filters.filter(
(filter) =>
filter.folder.accountId === folder.accountId &&
(filter.folder.path === parentPath ||
filter.folder.path.toUpperCase() === parentPath)
).length !== 0
);
}
// Delete a folder from the filter list
2021-09-04 22:16:18 +02:00
async function deleteFolderFromFilters(folder) {
2021-08-10 19:58:16 +02:00
const newFilters = SysTrayX.Messaging.filters.filter(
(filter) =>
!(
2021-08-10 20:13:33 +02:00
filter.folder.accountId === folder.accountId &&
(filter.folder.path === folder.path ||
filter.folder.path.toUpperCase() === folder.path)
2021-08-10 19:58:16 +02:00
)
);
// Store the new filters
2021-09-04 22:16:18 +02:00
await storage().set({
2021-08-10 19:58:16 +02:00
filters: newFilters,
});
}
// Get the account name from an id
function getAccountName(id) {
const account = SysTrayX.Messaging.accounts.filter(
(account) => account.id === id
)[0];
return account.name;
}
// Add a folder to the filter list
2021-09-04 22:16:18 +02:00
async function addFolderToFilters(newFolder) {
2021-08-10 19:58:16 +02:00
const folder = {
...newFolder,
accountName: getAccountName(newFolder.accountId),
2021-09-04 22:16:18 +02:00
version: SysTrayX.Info.version,
2021-08-10 19:58:16 +02:00
};
const filter = {
unread: true,
folder: folder,
};
const newFilters = SysTrayX.Messaging.filters;
newFilters.push(filter);
// Store the new filters
2021-09-04 22:16:18 +02:00
await storage().set({
2021-08-10 19:58:16 +02:00
filters: newFilters,
});
}
//
// Storage move
//
async function storageMove() {
const src = "sync";
const dst = "local";
//
// Debug state
//
await storage(src)
.get("debug")
.then(
async (result) =>
result.debug &&
(await storage(dst).set({
debug: `${result.debug}`,
}))
);
}