diff --git a/src/adapter/baseAdapter.ts b/src/adapter/baseAdapter.ts index 941e491b..3827d06a 100755 --- a/src/adapter/baseAdapter.ts +++ b/src/adapter/baseAdapter.ts @@ -13,7 +13,6 @@ abstract class BaseAdapter { protected _logger: Logger; protected _settings: SettingsModule.Settings; protected _sourceName: string; - protected _wallpaperLocation: string; constructor(params: { defaultName: string; @@ -21,12 +20,10 @@ abstract class BaseAdapter { name: string | null; schemaID: string; schemaPath: string; - wallpaperLocation: string; }) { const path = `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/general/${params.id}/`; this._logger = new Logger('RWG3', `${params.defaultName} adapter`); - this._wallpaperLocation = params.wallpaperLocation; this._settings = new SettingsModule.Settings(params.schemaID, params.schemaPath); this._sourceName = params.name ?? params.defaultName; @@ -51,7 +48,7 @@ abstract class BaseAdapter { * @param {HistoryEntry} historyEntry The historyEntry to fetch */ async fetchFile(historyEntry: HistoryEntry) { - const file = Gio.file_new_for_path(`${this._wallpaperLocation}/${String(historyEntry.name)}`); + const file = Gio.file_new_for_path(historyEntry.path); const fstream = file.replace(null, false, Gio.FileCreateFlags.NONE, null); // craft new message from details @@ -67,8 +64,6 @@ abstract class BaseAdapter { fstream.write(response_data_bytes, null); fstream.close(null); - historyEntry.path = file.get_path(); - return historyEntry; } diff --git a/src/adapter/genericJson.ts b/src/adapter/genericJson.ts index 8a1e771d..d6cbc628 100644 --- a/src/adapter/genericJson.ts +++ b/src/adapter/genericJson.ts @@ -8,14 +8,13 @@ import {BaseAdapter} from './../adapter/baseAdapter.js'; import {HistoryEntry} from './../history.js'; class GenericJsonAdapter extends BaseAdapter { - constructor(id: string, name: string, wallpaperLocation: string) { + constructor(id: string, name: string) { super({ defaultName: 'Generic JSON Source', id, name, schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_GENERIC_JSON, schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/genericJSON/${id}/`, - wallpaperLocation, }); } diff --git a/src/adapter/localFolder.ts b/src/adapter/localFolder.ts index 9765d16f..88d8bf5e 100644 --- a/src/adapter/localFolder.ts +++ b/src/adapter/localFolder.ts @@ -11,14 +11,13 @@ import {HistoryEntry} from './../history.js'; Gio._promisify(Gio.File.prototype, 'copy_async', 'copy_finish'); class LocalFolderAdapter extends BaseAdapter { - constructor(id: string, name: string, wallpaperLocation: string) { + constructor(id: string, name: string) { super({ defaultName: 'Local Folder', id, name, schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_LOCAL_FOLDER, schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/localFolder/${id}/`, - wallpaperLocation, }); } @@ -43,7 +42,7 @@ class LocalFolderAdapter extends BaseAdapter { continue; const historyEntry = new HistoryEntry(null, this._sourceName, randomFilePath); - historyEntry.source.sourceUrl = this._wallpaperLocation; + historyEntry.source.sourceUrl = randomFilePath; if (!this._includesWallpaper(wallpaperResult, historyEntry.source.imageDownloadUrl)) wallpaperResult.push(historyEntry); @@ -60,8 +59,7 @@ class LocalFolderAdapter extends BaseAdapter { async fetchFile(historyEntry: HistoryEntry) { const sourceFile = Gio.File.new_for_uri(historyEntry.source.imageDownloadUrl); - const name = sourceFile.get_basename(); - const targetFile = Gio.File.new_for_path(this._wallpaperLocation + String(name)); + const targetFile = Gio.File.new_for_path(historyEntry.path); // https://gjs.guide/guides/gio/file-operations.html#copying-and-moving-files // This function was rewritten by Gio._promisify @@ -69,8 +67,6 @@ class LocalFolderAdapter extends BaseAdapter { if (!await sourceFile.copy_async(targetFile, Gio.FileCopyFlags.NONE, GLib.PRIORITY_DEFAULT, null, null)) throw new Error('Failed copying image.'); - historyEntry.path = targetFile.get_path(); - return historyEntry; } diff --git a/src/adapter/reddit.ts b/src/adapter/reddit.ts index bf89c20c..ba724ed9 100644 --- a/src/adapter/reddit.ts +++ b/src/adapter/reddit.ts @@ -31,14 +31,13 @@ interface RedditSubmission { } class RedditAdapter extends BaseAdapter { - constructor(id: string, name: string, wallpaperLocation: string) { + constructor(id: string, name: string) { super({ defaultName: 'Reddit', id, name, schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_REDDIT, schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/reddit/${id}/`, - wallpaperLocation, }); } diff --git a/src/adapter/unsplash.ts b/src/adapter/unsplash.ts index bfb66f67..90f3717d 100644 --- a/src/adapter/unsplash.ts +++ b/src/adapter/unsplash.ts @@ -17,14 +17,13 @@ class UnsplashAdapter extends BaseAdapter { 'constraintValue': '', }; - constructor(id: string | null, name: string | null, wallpaperLocation: string) { + constructor(id: string | null, name: string | null) { super({ defaultName: 'Unsplash', id: id ?? '-1', name, schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_UNSPLASH, schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/unsplash/${id}/`, - wallpaperLocation, }); } diff --git a/src/adapter/urlSource.ts b/src/adapter/urlSource.ts index fc3825bd..b7448e1a 100644 --- a/src/adapter/urlSource.ts +++ b/src/adapter/urlSource.ts @@ -4,14 +4,13 @@ import {BaseAdapter} from './../adapter/baseAdapter.js'; import {HistoryEntry} from './../history.js'; class UrlSourceAdapter extends BaseAdapter { - constructor(id: string, name: string, wallpaperLocation: string) { + constructor(id: string, name: string) { super({ defaultName: 'Static URL', id, name, schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_URL_SOURCE, schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/urlSource/${id}/`, - wallpaperLocation, }); } diff --git a/src/adapter/wallhaven.ts b/src/adapter/wallhaven.ts index 40bf7f13..bad8f207 100644 --- a/src/adapter/wallhaven.ts +++ b/src/adapter/wallhaven.ts @@ -39,12 +39,11 @@ class WallhavenAdapter extends BaseAdapter { colors: '', }; - constructor(id: string, name: string, wallpaperLocation: string) { + constructor(id: string, name: string) { super({ id, schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_WALLHAVEN, schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/wallhaven/${id}/`, - wallpaperLocation, name, defaultName: 'Wallhaven', }); diff --git a/src/history.ts b/src/history.ts index 0a2c457a..a2fbae21 100644 --- a/src/history.ts +++ b/src/history.ts @@ -4,6 +4,9 @@ import * as Utils from './utils.js'; import {Settings} from './settings.js'; +// Gets filled by the HistoryController which is constructed at extension startup +let _wallpaperLocation: string; + interface SourceInfo { author: string | null; authorUrl: string | null; @@ -26,7 +29,7 @@ class HistoryEntry { id: string; /** Basename of URI */ name: string | null; // This can be null when an entry from an older version is mapped from settings - path: string | null = null; + path: string; source: SourceInfo; adapter: AdapterInfo | null = { // This can be null when an entry from an older version is mapped from settings id: null, @@ -46,6 +49,7 @@ class HistoryEntry { // extract the name from the url this.name = Utils.fileName(this.source.imageDownloadUrl); this.id = `${this.timestamp}_${this.name}`; + this.path = `${_wallpaperLocation}/${this.id}`; } } @@ -54,10 +58,9 @@ class HistoryController { size = 10; private _settings = new Settings(); - private _wallpaperLocation: string; constructor(wallpaperLocation: string) { - this._wallpaperLocation = wallpaperLocation; + _wallpaperLocation = wallpaperLocation; this.load(); } @@ -159,7 +162,7 @@ class HistoryController { if (firstHistoryElement) this.history = [firstHistoryElement]; - const directory = Gio.file_new_for_path(this._wallpaperLocation); + const directory = Gio.file_new_for_path(_wallpaperLocation); const enumerator = directory.enumerate_children('', Gio.FileQueryInfoFlags.NONE, null); let fileInfo; @@ -173,7 +176,7 @@ class HistoryController { // ignore hidden files and first element if (id[0] !== '.' && id !== firstHistoryElement.id) { - const deleteFile = Gio.file_new_for_path(this._wallpaperLocation + id); + const deleteFile = Gio.file_new_for_path(_wallpaperLocation + id); deleteFile.delete(null); } } while (fileInfo); diff --git a/src/wallpaperController.ts b/src/wallpaperController.ts index c386dceb..24cc77ae 100644 --- a/src/wallpaperController.ts +++ b/src/wallpaperController.ts @@ -198,7 +198,7 @@ class WallpaperController { if (sourceIDs.length < 1 || sourceIDs[0] === '-1') { randomAdapterResult.push({ - adapter: new UnsplashAdapter(null, null, this.wallpaperLocation), + adapter: new UnsplashAdapter(null, null), id: '-1', type: 0, imageCount: count, @@ -240,31 +240,31 @@ class WallpaperController { try { switch (sourceType) { case 0: - imageSourceAdapter = new UnsplashAdapter(sourceID, sourceName, this.wallpaperLocation); + imageSourceAdapter = new UnsplashAdapter(sourceID, sourceName); break; case 1: - imageSourceAdapter = new WallhavenAdapter(sourceID, sourceName, this.wallpaperLocation); + imageSourceAdapter = new WallhavenAdapter(sourceID, sourceName); break; case 2: - imageSourceAdapter = new RedditAdapter(sourceID, sourceName, this.wallpaperLocation); + imageSourceAdapter = new RedditAdapter(sourceID, sourceName); break; case 3: - imageSourceAdapter = new GenericJsonAdapter(sourceID, sourceName, this.wallpaperLocation); + imageSourceAdapter = new GenericJsonAdapter(sourceID, sourceName); break; case 4: - imageSourceAdapter = new LocalFolderAdapter(sourceID, sourceName, this.wallpaperLocation); + imageSourceAdapter = new LocalFolderAdapter(sourceID, sourceName); break; case 5: - imageSourceAdapter = new UrlSourceAdapter(sourceID, sourceName, this.wallpaperLocation); + imageSourceAdapter = new UrlSourceAdapter(sourceID, sourceName); break; default: - imageSourceAdapter = new UnsplashAdapter(null, null, this.wallpaperLocation); + imageSourceAdapter = new UnsplashAdapter(null, null); sourceType = 0; break; } } catch (error) { this._logger.warn('Had errors, fetching with default settings.'); - imageSourceAdapter = new UnsplashAdapter(null, null, this.wallpaperLocation); + imageSourceAdapter = new UnsplashAdapter(null, null); sourceType = 0; } @@ -475,34 +475,9 @@ class WallpaperController { const newImageEntries = await Promise.all(fetchPromises); this._logger.info(`Requested ${newImageEntries.length} new images.`); - // Move file to unique naming - const movePromises = newImageEntries.map(entry => { - if (!entry.path) - return Promise.resolve(false); - - const file = Gio.File.new_for_path(entry.path); - const targetFolder = file.get_parent(); - const targetFile = targetFolder?.get_child(entry.id); - - if (!targetFile) - throw new Error('Failed getting targetFile'); - - entry.path = targetFile.get_path(); - - // This function is Gio._promisified - return file.move_async(targetFile, Gio.FileCopyFlags.NONE, 0, null, null); - }); - - // wait for all images to be moved - await Promise.all(movePromises); - const newWallpaperPaths = newImageEntries.map(element => { - if (element.path) - return element.path; - - // eslint-disable-next-line - return; - }) as string[]; // cast because we made sure it's defined + return element.path; + }); const usedWallpaperPaths = this._fillDisplaysFromHistory(newWallpaperPaths, monitorCount); if (changeType === 3) {