RandomWallpaperGnome3/randomwallpaper@iflow.space/history.js

155 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-07-20 11:48:13 +00:00
// Filesystem
const Gio = imports.gi.Gio;
const Self = imports.misc.extensionUtils.getCurrentExtension();
const Prefs = Self.imports.settings;
const LoggerModule = Self.imports.logger;
var HistoryEntry = class {
2017-07-20 11:48:13 +00:00
constructor(author, source, url) {
this.id = null;
this.path = null;
this.source = null;
2017-07-20 11:48:13 +00:00
this.timestamp = new Date().getTime();
this.source = {
author: author,
authorUrl: null,
source: source,
sourceUrl: null,
2018-04-12 16:06:19 +00:00
imageDownloadUrl: url, // URL used for downloading the image
imageLinkUrl: url // URL used for linking back to the website of the image
};
}
2017-07-20 11:48:13 +00:00
};
2017-07-20 11:48:13 +00:00
var HistoryController = class {
2017-07-20 11:48:13 +00:00
constructor(wallpaperlocation) {
2017-07-20 11:48:13 +00:00
this.logger = new LoggerModule.Logger('RWG3', 'HistoryController');
this.size = 10;
this.history = [];
2017-07-20 11:48:13 +00:00
this._settings = new Prefs.Settings();
this._wallpaperlocation = wallpaperlocation;
this.load();
}
2017-07-20 11:48:13 +00:00
insert(historyElement) {
2017-07-20 11:48:13 +00:00
this.history.unshift(historyElement);
this._deleteOldPictures();
this.save();
}
2017-07-20 11:48:13 +00:00
/**
* Set the given id to to the first history element (the current one)
* @param id
* @returns {boolean}
*/
promoteToActive(id) {
2017-07-20 11:48:13 +00:00
let element = this.get(id);
if (element === null) {
return false;
}
element.timestamp = new Date().getTime();
2018-07-27 15:36:47 +00:00
this.history = this.history.sort((elem1, elem2) => {
return elem1.timestamp < elem2.timestamp
});
2017-07-20 11:48:13 +00:00
this.save();
return true;
}
2017-07-20 11:48:13 +00:00
/**
* Returns the corresponding HistoryEntry or null
* @param id
* @returns {*}
*/
get(id) {
2017-07-20 11:48:13 +00:00
for (let elem of this.history) {
if (elem.id == id) {
return elem;
}
}
return null;
}
2017-07-20 11:48:13 +00:00
/**
* Load the history from the gschema
*/
load() {
2017-07-20 11:48:13 +00:00
this.size = this._settings.get('history-length', 'int');
let stringHistory = this._settings.get('history', 'strv');
this.history = stringHistory.map(elem => {
return JSON.parse(elem)
});
}
2017-07-20 11:48:13 +00:00
/**
* Save the history to the gschema
*/
save() {
2018-07-27 15:36:47 +00:00
let stringHistory = this.history.map(elem => {
return JSON.stringify(elem)
});
2017-07-20 11:48:13 +00:00
this._settings.set('history', 'strv', stringHistory);
Gio.Settings.sync();
}
2017-07-20 11:48:13 +00:00
/**
* Clear the history and delete all photos except the current one.
* @returns {boolean}
*/
clear() {
2017-07-20 11:48:13 +00:00
let firstHistoryElement = this.history[0];
if (firstHistoryElement)
this.history = [firstHistoryElement];
let directory = Gio.file_new_for_path(this._wallpaperlocation);
let enumerator = directory.enumerate_children('', Gio.FileQueryInfoFlags.NONE, null);
let fileinfo;
let deleteFile;
do {
fileinfo = enumerator.next_file(null);
if (!fileinfo) {
break;
}
let id = fileinfo.get_name();
// ignore hidden files and first element
if (id[0] != '.' && id != firstHistoryElement.id) {
deleteFile = Gio.file_new_for_path(this._wallpaperlocation + id);
deleteFile.delete(null);
}
2018-07-27 15:36:47 +00:00
} while (fileinfo);
2017-07-20 11:48:13 +00:00
this.save();
return true;
}
2017-07-20 11:48:13 +00:00
/**
* Delete all pictures that have no slot in the history.
* @private
*/
_deleteOldPictures() {
2017-07-20 11:48:13 +00:00
this.size = this._settings.get('history-length', 'int');
let deleteFile;
2018-07-27 15:36:47 +00:00
while (this.history.length > this.size) {
2017-07-20 11:48:13 +00:00
deleteFile = Gio.file_new_for_path(this.history.pop().path);
deleteFile.delete(null);
}
}
2017-07-20 11:48:13 +00:00
};