Skip to content

Commit

Permalink
Fixed a bug in handling of scoped security bookmarks, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Dec 30, 2023
1 parent 4c69e36 commit 320a77c
Show file tree
Hide file tree
Showing 51 changed files with 269 additions and 245 deletions.
2 changes: 1 addition & 1 deletion flatpak/com.github.ransome1.sleek.appdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<developer_name>Robin Ahle</developer_name>
<content_rating type="oars-1.1"/>
<releases>
<release version="2.0.4-rc.5" date="2023-12-28"/>
<release version="2.0.4-rc.6" date="2023-12-30"/>
</releases>
<url type="homepage">https://github.com/ransome1/sleek</url>
<url type="contact">https://github.com/ransome1/sleek/issues</url>
Expand Down
2 changes: 1 addition & 1 deletion flatpak/com.github.ransome1.sleek.desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Desktop Entry]
Version=2.0.4-rc.5
Version=2.0.4-rc.6
Name=sleek
Exec=sleek
Type=Application
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sleek",
"version": "2.0.4-rc.5",
"version": "2.0.4-rc.6",
"main": "./src/main/main.tsx",
"scripts": {
"build": "concurrently \"yarn run peggy\" \"yarn run build:main\" \"yarn run build:renderer\"",
Expand Down
2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sleek",
"version": "2.0.4-rc.5",
"version": "2.0.4-rc.6",
"description": "todo.txt manager for Linux, Windows and MacOS, free and open-source (FOSS)",
"synopsis": "todo.txt manager for Linux, Windows and MacOS, free and open-source (FOSS)",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sleek
base: core20
version: "2.0.4-rc.5"
version: "2.0.4-rc.6"
summary: todo.txt manager for Linux, free and open-source (FOSS)
description: |
sleek is an open-source (FOSS) todo manager based on the todo.txt syntax. Stripped down to only the most necessary features, and with a clean and simple interface, sleek aims to help you focus on getting things done.
Expand Down
28 changes: 14 additions & 14 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

2023-12-28 Line 1 rec:1d due:2023-12-29
2023-12-28 Line 1 rec:w due:2024-01-04
2023-12-28 Line 1 rec:2m due:2024-02-28
2023-12-28 Line 1 rec:+1d due:2023-12-30
2023-12-28 Line 1 rec:7w due:2024-02-15
2023-12-28 Line 1 due:2023-07-24 rec:+1b
2023-12-28 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2023-12-28 Water plants @home +quick due:2024-01-04 t:2023-12-25 rec:1w
2023-12-28 Line 1 rec:+1d t:2023-09-20
2023-12-28 Line 1 rec:1d pri:A due:2023-12-29
2023-12-28 (A) Do something rec:d t:2023-12-29 @SomeContext
2023-12-28 Do something rec:0d
2023-12-28 Do something rec:0d due:2023-12-28
2023-12-28 Do something rec:0d due:2023-12-28 t:2023-12-28
2023-12-30 Line 1 rec:1d due:2023-12-31
2023-12-30 Line 1 rec:w due:2024-01-06
2023-12-30 Line 1 rec:2m due:2024-02-29
2023-12-30 Line 1 rec:+1d due:2024-01-01
2023-12-30 Line 1 rec:7w due:2024-02-17
2023-12-30 Line 1 due:2023-07-24 rec:+1b
2023-12-30 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2023-12-30 Water plants @home +quick due:2024-01-06 t:2023-12-27 rec:1w
2023-12-30 Line 1 rec:+1d t:2023-09-20
2023-12-30 Line 1 rec:1d pri:A due:2023-12-31
2023-12-30 (A) Do something rec:d t:2023-12-31 @SomeContext
2023-12-30 Do something rec:0d
2023-12-30 Do something rec:0d due:2023-12-30
2023-12-30 Do something rec:0d due:2023-12-30 t:2023-12-30
2 changes: 1 addition & 1 deletion src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ configStorage.onDidAnyChange(async(settings) => {
}
});

configStorage.onDidChange('files', async (files: FileObject[] | undefined) => {
configStorage.onDidChange('files', (files: FileObject[] | null) => {
try {
if(files) {
createFileWatcher(files);
Expand Down
61 changes: 30 additions & 31 deletions src/main/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow } from 'electron';
import { app, BrowserWindow, nativeTheme } from 'electron';
import path from 'path';
import fs from 'fs';
import { configStorage } from './config';
Expand All @@ -13,8 +13,9 @@ import './modules/Ipc';
const environment: string | undefined = process.env.NODE_ENV;
const files: FileObject[] = (configStorage.get('files') as FileObject[]) || [];
let tray: boolean = configStorage.get('tray');
const colorTheme = configStorage.get('colorTheme');
let mainWindow: BrowserWindow | null = null;
let eventListeners: Record<string, any> = {};
let eventListeners: Record<string, any | undefined> = {};

const handleCreateWindow = () => {
if(mainWindow) {
Expand All @@ -29,18 +30,18 @@ const handleClosed = async () => {

mainWindow = null;

delete eventListeners.handleReadyToShow;
delete eventListeners.handleClosed;
delete eventListeners.handleResize;
delete eventListeners.handleMove;
delete eventListeners.handleShow;
delete eventListeners.handleMaximize;
delete eventListeners.handleUnmaximize;
delete eventListeners.handleCreateWindow;
delete eventListeners.handleWindowAllClosed;
delete eventListeners.handleWillQuit;
delete eventListeners.handleBeforeQuit;
delete eventListeners.watcher;
eventListeners.handleReadyToShow = undefined;
eventListeners.handleClosed = undefined;
eventListeners.handleResize = undefined;
eventListeners.handleMove = undefined;
eventListeners.handleShow = undefined;
eventListeners.handleMaximize = undefined;
eventListeners.handleUnmaximize = undefined;
eventListeners.handleCreateWindow = undefined;
eventListeners.handleWindowAllClosed = undefined;
eventListeners.handleWillQuit = undefined;
eventListeners.handleBeforeQuit = undefined;
eventListeners.watcher = undefined;
}

const handleResize = () => {
Expand Down Expand Up @@ -112,11 +113,11 @@ const createMainWindow = () => {
mainWindow.webContents.openDevTools();
}

mainWindow?.loadURL(resolveHtmlPath('index.html'));
handleWindowSizeAndPosition();

handleWindowSizeAndPosition();
nativeTheme.themeSource = colorTheme;

handleTheme();
mainWindow?.loadURL(resolveHtmlPath('index.html'));

mainWindow
.on('ready-to-show', handleReadyToShow)
Expand All @@ -127,14 +128,13 @@ const createMainWindow = () => {
.on('maximize', handleMaximize)
.on('unmaximize', handleUnmaximize);

eventListeners
.handleReadyToShow = handleReadyToShow
.handleClosed = handleClosed
.handleResize = handleResize
.handleMove = handleMove
.handleShow = handleShow
.handleMaximize = handleMaximize
.handleUnmaximize = handleUnmaximize;
eventListeners.handleReadyToShow = handleReadyToShow
eventListeners.handleClosed = handleClosed
eventListeners.handleResize = handleResize
eventListeners.handleMove = handleMove
eventListeners.handleShow = handleShow
eventListeners.handleMaximize = handleMaximize
eventListeners.handleUnmaximize = handleUnmaximize;

const customStylesPath: string = configStorage.get('customStylesPath');
if(customStylesPath) {
Expand Down Expand Up @@ -170,15 +170,15 @@ const handleBeforeQuit = () => {
app.releaseSingleInstanceLock();
}

const handleOpenFile = (event, path) => {
const handleOpenFile = (path: string) => {
if(path) addFile(path, null);
};

app
.on('window-all-closed', handleWindowAllClosed)
.on('before-quit', handleBeforeQuit)
.on('activate', handleCreateWindow)
.on('open-file', handleOpenFile)
.on('open-file', () => handleOpenFile(path))
.whenReady()
.then(() => {

Expand All @@ -190,10 +190,9 @@ app
createTray();
}

eventListeners
.handleCreateWindow = handleCreateWindow
.handleWindowAllClosed = handleWindowAllClosed
.handleBeforeQuit = handleBeforeQuit;
eventListeners.handleCreateWindow = handleCreateWindow
eventListeners.handleWindowAllClosed = handleWindowAllClosed
eventListeners.handleBeforeQuit = handleBeforeQuit;

})
.catch(console.error);
Expand Down
24 changes: 13 additions & 11 deletions src/main/modules/File/Archive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import { replaceFileContent } from './Write';
import { mainWindow } from '../../main';
import { createTodoObjects } from '../ProcessDataRequest/CreateTodoObjects';

let stopAccessingSecurityScopedResource: any;

function handleRequestArchive(): void {
const activeFile = getActiveFile();
mainWindow!.webContents.send('triggerArchiving', Boolean(activeFile?.doneFilePath));
}

async function extractTodoStringsFromFile(filePath: string, complete: boolean | null, bookmark: string | null): Promise<string[]> {
if(bookmark) stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);

const content = await fs.readFile(filePath, 'utf8');
async function extractTodoObjectsFromFile(filePath: string, complete: boolean | null, bookmark: string | null): Promise<string[]> {
let fileContent;

if(bookmark) stopAccessingSecurityScopedResource();
if(process.mas && bookmark) {
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);
fileContent = await fs.readFile(filePath, 'utf8');
stopAccessingSecurityScopedResource()
} else {
fileContent = await fs.readFile(filePath, 'utf8');
}

const todoObjects = await createTodoObjects(content);
const todoObjects = await createTodoObjects(fileContent);

return todoObjects
.filter((todoObject) => todoObject && (complete === null || todoObject.complete === complete))
Expand All @@ -37,14 +39,14 @@ async function archiveTodos(): Promise<string> {
return 'Archiving file is not defined';
}

const completedTodos = await extractTodoStringsFromFile(activeFile.todoFilePath, true, activeFile.todoFileBookmark);
const completedTodos = await extractTodoObjectsFromFile(activeFile.todoFilePath, true, activeFile.todoFileBookmark);

if(completedTodos.length === 0) {
return 'No completed todos found';
}

const uncompletedTodos = await extractTodoStringsFromFile(activeFile.todoFilePath, false, activeFile.todoFileBookmark);
const todosFromDoneFile = await extractTodoStringsFromFile(activeFile.doneFilePath, true, activeFile.doneFileBookmark);
const uncompletedTodos = await extractTodoObjectsFromFile(activeFile.todoFilePath, false, activeFile.todoFileBookmark);
const todosFromDoneFile = await extractTodoObjectsFromFile(activeFile.doneFilePath, true, activeFile.doneFileBookmark);

const stringDoneFile = todosFromDoneFile.length === 0 ? completedTodos.join('\n') : todosFromDoneFile.join('\n') + '\n' + completedTodos.join('\n');
const stringTodoFile = uncompletedTodos.join('\n');
Expand Down
21 changes: 13 additions & 8 deletions src/main/modules/File/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ async function openFile(setDoneFile: boolean): Promise<void> {
});
if(!result.canceled && result.filePaths.length > 0) {
const filePath: string = result.filePaths[0];
const securityScopedBookmark: string | null = result.bookmarks?.[0] || null;
const bookmark: string | null = result.bookmarks?.[0] || null;

if(setDoneFile) {
addDoneFile(filePath, securityScopedBookmark);
addDoneFile(filePath, bookmark);
} else {
addFile(filePath, securityScopedBookmark);
addFile(filePath, bookmark);
}
}
return;
Expand All @@ -44,17 +44,22 @@ async function createFile(setDoneFile: boolean): Promise<void> {

if(!result.canceled && result.filePath) {
const filePath: string = result.filePath;
const securityScopedBookmark: string | null = result.bookmark || null;
const bookmark: string | null = result.bookmark || null;

await fs.writeFile(filePath, '');
if(process.mas && bookmark) {
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);
await fs.writeFile(filePath, '');
stopAccessingSecurityScopedResource()
} else {
await fs.writeFile(filePath, '');
}

if(setDoneFile) {
addDoneFile(filePath, securityScopedBookmark);
addDoneFile(filePath, bookmark);
} else {
addFile(filePath, securityScopedBookmark);
addFile(filePath, bookmark);
}
}

return;
}

Expand Down
17 changes: 9 additions & 8 deletions src/main/modules/File/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import { createMenu } from '../Menu';
import path from 'path';
import { mainWindow } from '../../main';

let stopAccessingSecurityScopedResource: any;

async function readFileContent(filePath: string, bookmark: string | null) {
async function readFileContent(filePath: string, bookmark: string | null): Promise<string | null> {
if(!filePath) {
return null;
}

if(bookmark) app.startAccessingSecurityScopedResource(bookmark)

const fileContent = await fs.promises.readFile(filePath, 'utf8');

if(bookmark) stopAccessingSecurityScopedResource()
let fileContent;

if(process.mas && bookmark) {
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);
fileContent = await fs.promises.readFile(filePath, 'utf8');
stopAccessingSecurityScopedResource()
} else {
fileContent = await fs.promises.readFile(filePath, 'utf8');
}
return fileContent;
}

Expand Down
11 changes: 7 additions & 4 deletions src/main/modules/File/Watcher.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chokidar, { FSWatcher } from 'chokidar';
import processDataRequest from '../ProcessDataRequest/ProcessDataRequest';
import { eventListeners } from '../../main';
import { configStorage } from '../../config';
import { eventListeners } from '../../main';

let watcher: FSWatcher | null = null;

Expand All @@ -22,12 +22,15 @@ function createFileWatcher(files: FileObject[]): void {
console.log(`FileWatcher.ts: New file added: ${file}`);
})
.on('change', async (file) => {
console.log(`File ${file} has been changed`);
await processDataRequest();
try {
await processDataRequest();
console.log(`File ${file} has been changed`);
} catch(error) {
console.error(error.message);
}
})
.on('unlink', (file) => {
console.log(`FileWatcher.ts: FileObject ${file} has been unlinked`);

const updatedFiles = files.filter((item) => item.todoFilePath !== file);
configStorage.set('files', updatedFiles);
})
Expand Down
26 changes: 14 additions & 12 deletions src/main/modules/File/Write.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { getActiveFile } from './Active';
import { configStorage } from '../../config';
import { replaceSpeakingDatesWithAbsoluteDates } from '../Date';

let stopAccessingSecurityScopedResource: any;

async function removeLineFromFile(id: number): Promise<string> {
const activeFile: FileObject | null = getActiveFile();
if(!activeFile) {
Expand All @@ -19,11 +17,13 @@ async function removeLineFromFile(id: number): Promise<string> {
lines.splice(id, 1);
const modifiedContent = lines.join('\n');

if(bookmark) app.startAccessingSecurityScopedResource(bookmark)

await fs.writeFile(todoFilePath, modifiedContent, 'utf8');

if(bookmark) stopAccessingSecurityScopedResource()
if(process.mas && bookmark) {
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);
await fs.writeFile(todoFilePath, modifiedContent, 'utf8');
stopAccessingSecurityScopedResource()
} else {
await fs.writeFile(todoFilePath, modifiedContent, 'utf8');
}

return `Line ${id} removed from file`;
}
Expand Down Expand Up @@ -68,11 +68,13 @@ async function writeTodoObjectToFile(id: number, string: string): Promise<void>
}
}

if(bookmark) app.startAccessingSecurityScopedResource(bookmark)

await fs.writeFile(todoFilePath, lines.join('\n'), 'utf8');

if(bookmark) stopAccessingSecurityScopedResource()
if(process.mas && bookmark) {
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);
await fs.writeFile(todoFilePath, lines.join('\n'), 'utf8');
stopAccessingSecurityScopedResource()
} else {
await fs.writeFile(todoFilePath, lines.join('\n'), 'utf8');
}
}

async function replaceFileContent(string: string, filePath: string) {
Expand Down
Loading

0 comments on commit 320a77c

Please sign in to comment.