From c119247182560dde7e528777cb7448eba4a3f582 Mon Sep 17 00:00:00 2001 From: Wolfgang Rumpler Date: Fri, 10 Feb 2017 10:08:27 +0100 Subject: [PATCH] init timer feature, implement a simple settings interface --- randomwallpaper@iflow.space/Timer.js | 70 +++++++++++-------- randomwallpaper@iflow.space/settings.js | 34 +++++++++ randomwallpaper@iflow.space/settings.ui | 2 +- .../wallpaperController.js | 40 +++++++---- 4 files changed, 102 insertions(+), 44 deletions(-) create mode 100644 randomwallpaper@iflow.space/settings.js diff --git a/randomwallpaper@iflow.space/Timer.js b/randomwallpaper@iflow.space/Timer.js index 931df0c..f75d381 100644 --- a/randomwallpaper@iflow.space/Timer.js +++ b/randomwallpaper@iflow.space/Timer.js @@ -1,39 +1,53 @@ const Lang = imports.lang; const GLib = imports.gi.GLib; -const Convenience = Self.imports.convenience; +const Prefs = Self.imports.settings; let AFTimer = new Lang.Class({ - _timeout: null, - _timoutEndCallback: null, + _timeout: null, + _timoutEndCallback: null, - _init: function() { - this._settings = Convenience.getSettings(); - this._settings.connect('changed::minutes_elapsed', this._loadSettings.bind(this)); - this._settings.connect('changed::minutes_', this._loadSettings.bind(this)); - } - - registerCallback: function(callback) { - this._timoutEndCallback = callback; - } - - start: function(delay) { - if (this._timeout) { - this.stop(); + _init: function() { + this._settings = new Prefs.Settings(); + // this._settings.observe('minutes_elapsed', function() { // TODO: determine what to do }); + this._settings.observe('minutes', this._loadSettings.bind(this)); } - // TODO: calc elapsed time - // TODO: check > 0 - - this._timeout = GLib.timeout_add(, delay, function() { - - }); - } - - stop: function(delay, callback) { - if (_timeout) { - Glib.source_remove(_timeout) + _remainingMinutes: function() { + // TODO + } + + registerCallback: function(callback) { + this._timoutEndCallback = callback; + }, + + begin: function() { + if (this._timeout) { + this.pause(); + } + + //this._settings.get() + + // TODO: calc elapsed time + // TODO: check > 0 + + this._timeout = GLib.timeout_add(Glib.PRIORITY_DEFAULT, delay, function() { + this._settings.set(minutes_elapsed) + }.bind(this)); + }, + + stop: function() { + if (_timeout) { + Glib.source_remove(_timeout) + this._settings.set('minutes_elapsed', 'int', 0) + } + }, + + pause: function() { + if (_timeout) { + Glib.source_remove(_timeout) + this._settings.set('minutes_elapsed', 'int', this._remainingMinutes()) + } } - } }); diff --git a/randomwallpaper@iflow.space/settings.js b/randomwallpaper@iflow.space/settings.js new file mode 100644 index 0000000..21d536f --- /dev/null +++ b/randomwallpaper@iflow.space/settings.js @@ -0,0 +1,34 @@ +const Lang = imports.lang; +const Glib = imports.gi.GLib; +const Gio = imports.gi.Gio; + +const Self = imports.misc.extensionUtils.getCurrentExtension(); +const Convenience = Self.imports.convenience; + +let Settings = new Lang.Class({ + Name: "Settings", + + _init: function() { + this._settings = Convenience.getSettings(); + }, + + observe: function(key, callback) { + this._settings.connect('changed::'+key, callback); + }, + + set: function(key, type, value) { + if (this._settings['set_'+type](key, value)){ + Gio.Settings.sync(); // wait for write + } else { + throw "Could not set " + key + " (type: " + type + ") with the value " + value; + } + }, + + get: function(key, type) { + return this._settings['get_'+type](key); + }, + + getSourceAdapter: function() { + return null; + } +}); diff --git a/randomwallpaper@iflow.space/settings.ui b/randomwallpaper@iflow.space/settings.ui index 0323fa3..3943df1 100644 --- a/randomwallpaper@iflow.space/settings.ui +++ b/randomwallpaper@iflow.space/settings.ui @@ -216,7 +216,7 @@ True False start - History lenght + History length 0 diff --git a/randomwallpaper@iflow.space/wallpaperController.js b/randomwallpaper@iflow.space/wallpaperController.js index 5b1b246..807e7ff 100644 --- a/randomwallpaper@iflow.space/wallpaperController.js +++ b/randomwallpaper@iflow.space/wallpaperController.js @@ -12,6 +12,7 @@ const Gio = imports.gi.Gio; const Self = imports.misc.extensionUtils.getCurrentExtension(); const SourceAdapter = Self.imports.sourceAdapter; const Convenience = Self.imports.convenience; +const Prefs = Self.imports.settings; let WallpaperController = new Lang.Class({ Name: "WallpaperController", @@ -23,7 +24,7 @@ let WallpaperController = new Lang.Class({ history: [], imageSourceAdapter: undefined, - autoFetch : { + _autoFetch : { active: false, duration: 30, }, @@ -32,29 +33,38 @@ let WallpaperController = new Lang.Class({ this.extensionMeta = extensionMeta; this.wallpaperlocation = this.extensionMeta.path + '/wallpapers/'; - this._settings = Convenience.getSettings(); - this._settings.connect('changed', this._loadSettings.bind(this)); - this._loadSettings(); + this._settings = new Prefs.Settings(); + this._settings.observe('history-length', this._updateHistory.bind(this)); + this._settings.observe('auto-fetch', this._updateAutoFetching.bind(this)); + this._settings.observe('minutes', this._updateAutoFetching.bind(this)); + this._settings.observe('hours', this._updateAutoFetching.bind(this)); + + this._updateHistory(); + this._updateAutoFetching(); this.history = this._loadHistory(); this.currentWallpaper = this._getCurrentWallpaper(); this.imageSourceAdapter = new SourceAdapter.DesktopperAdapter(); this.imageSourceAdapter = new SourceAdapter.WallheavenAdapter(); - - if (this.autoFetch.active) { - - } }, - _loadSettings: function() { - this.historySize = this._settings.get_int('history-length'); - this.autoFetch.active = this._settings.get_boolean('auto-fetch'); + _updateHistory: function() { + this.historySize = this._settings.get('history-length', 'int'); + }, + _updateAutoFetching: function() { let duration = 0; - duration += this._settings.get_int('minutes'); - duration += this._settings.get_int('hours') * 60; - this.autoFetch.duration = duration; + duration += this._settings.get('minutes', 'int'); + duration += this._settings.get('hours', 'int') * 60; + this._autoFetch.duration = duration; + this._autoFetch.active = this._settings.get('auto-fetch', 'boolean'); + + if (this._autoFetch.active) { + // TODO: this._timer.begin(this._autoFetch.duration); + } else { + // TODO: this._timer.end(this._autoFetch.duration); + } }, /* @@ -184,7 +194,7 @@ let WallpaperController = new Lang.Class({ }, deleteOldPictures: function() { - this.historySize = this._settings.get_int('history-length'); + this.historySize = this._settings.get('history-length', 'int'); let deleteFile; while(this.history.length > this.historySize) { deleteFile = Gio.file_new_for_path(this.wallpaperlocation + this.history.pop());