Implement switchable image sources, add logger module

This commit is contained in:
Wolfgang Rumpler 2017-07-19 14:23:28 +02:00
parent 04c25ec82a
commit 4596a39c39
8 changed files with 141 additions and 84 deletions

3
.gitignore vendored
View file

@ -1 +1,4 @@
.idea
# Temporary ui files
**/*~

0
debug.sh Normal file → Executable file
View file

View file

@ -0,0 +1,32 @@
const Lang = imports.lang;
let Logger = new Lang.Class({
Name: "Logger",
_prefix: null,
_callingClass: null,
_init: function(prefix, callingClass) {
this._prefix = prefix;
this._callingClass = callingClass;
},
_log: function(level, message) {
global.log(`${this._prefix} [${level}] >> ${this._callingClass} :: ${message}`);
},
debug: function (message) {
this._log("DEBUG", message);
},
info: function (message) {
this._log("INFO", message);
},
warning: function (message) {
this._log("WARNING", message);
},
error: function (message) {
this._log("ERROR", message);
}
});

View file

@ -14,65 +14,65 @@ const Gettext = imports.gettext.domain('space.iflow.randomwallpaper');
const RWG_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.space.iflow.randomwallpaper';
function init() {
//Convenience.initTranslations();
//Convenience.initTranslations();
}
function buildPrefsWidget() {
let settings = new RandomWallpaperSettings();
let widget = settings.widget;
widget.show_all();
let settings = new RandomWallpaperSettings();
let widget = settings.widget;
widget.show_all();
return widget;
return widget;
}
/* UI Setup */
const RandomWallpaperSettings = new Lang.Class({
Name: 'RandomWallpaper.Settings',
Name: 'RandomWallpaper.Settings',
_init: function() {
this._settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA);
this._builder = new Gtk.Builder();
//this._builder.set_translation_domain(Self.metadata['gettext-domain']);
this._builder.add_from_file(Self.path + '/settings.ui');
_init: function () {
this._settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA);
this._builder = new Gtk.Builder();
//this._builder.set_translation_domain(Self.metadata['gettext-domain']);
this._builder.add_from_file(Self.path + '/settings.ui');
this._toggleAfSliders();
this._toggleAfSliders();
this.widget = this._builder.get_object('main-widget');
this.widget = this._builder.get_object('main-widget');
this._builder.get_object('af-switch').connect('notify::active', function(toggleSwitch) {
this._toggleAfSliders();
}.bind(this))
this._builder.get_object('af-switch').connect('notify::active', function (toggleSwitch) {
this._toggleAfSliders();
}.bind(this));
this._settings.bind('history-length',
this._builder.get_object('history-length'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('minutes',
this._builder.get_object('duration-minutes'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('hours',
this._builder.get_object('duration-hours'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('source',
this._builder.get_object('source-combo'),
'active-id',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('auto-fetch',
this._builder.get_object('af-switch'),
'active',
Gio.SettingsBindFlags.DEFAULT);
},
this._settings.bind('history-length',
this._builder.get_object('history-length'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('minutes',
this._builder.get_object('duration-minutes'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('hours',
this._builder.get_object('duration-hours'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('source',
this._builder.get_object('source-combo'),
'active-id',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('auto-fetch',
this._builder.get_object('af-switch'),
'active',
Gio.SettingsBindFlags.DEFAULT);
},
_toggleAfSliders: function() {
if(this._builder.get_object('af-switch').active) {
this._builder.get_object('duration-slider-hours').set_sensitive(true);
this._builder.get_object('duration-slider-minutes').set_sensitive(true);
} else {
this._builder.get_object('duration-slider-hours').set_sensitive(false);
this._builder.get_object('duration-slider-minutes').set_sensitive(false);
}
}
_toggleAfSliders: function () {
if (this._builder.get_object('af-switch').active) {
this._builder.get_object('duration-slider-hours').set_sensitive(true);
this._builder.get_object('duration-slider-minutes').set_sensitive(true);
} else {
this._builder.get_object('duration-slider-hours').set_sensitive(false);
this._builder.get_object('duration-slider-minutes').set_sensitive(false);
}
}
});

View file

@ -7,6 +7,7 @@ const Convenience = Self.imports.convenience;
let Settings = new Lang.Class({
Name: "Settings",
_settings: null,
_init: function() {
this._settings = Convenience.getSettings();
@ -29,6 +30,7 @@ let Settings = new Lang.Class({
},
getSourceAdapter: function() {
global.log(this._settings.get_enum('source'));
return null;
}
});

View file

@ -114,9 +114,9 @@
<property name="can_focus">False</property>
<property name="active_id">0</property>
<items>
<item id="desktoppr" translatable="yes">desktoppr.co</item>
<item id="unsplash" translatable="yes">unsplash.com</item>
<item id="wallheaven" translatable="yes">alpha.wallheaven.cc</item>
<item id="desktoppr">desktoppr.co</item>
<item id="unsplash">unsplash.com (not implemented, defaults to desktopper)</item>
<item id="wallheaven">alpha.wallheaven.cc (experimental)</item>
</items>
</object>
<packing>

View file

@ -7,30 +7,30 @@ const Json = imports.gi.Json;
let DesktopperAdapter = new Lang.Class({
Name: "DesktopperAdapter",
/*
fetch a random image url from desktopper.cc
and call callback function with the URL of the image
*/
requestRandomImage: function(callback){
fetch a random image url from desktopper.cc
and call callback function with the URL of the image
*/
requestRandomImage: function (callback) {
let session = new Soup.SessionAsync();
let message = Soup.Message.new('GET', 'https://api.desktoppr.co/1/wallpapers/random')
let message = Soup.Message.new('GET', 'https://api.desktoppr.co/1/wallpapers/random');
let parser = new Json.Parser();
var _this = this;
session.queue_message(message, function(session, message) {
session.queue_message(message, function (session, message) {
parser.load_from_data(message.response_body.data, -1);
let data = parser.get_root().get_object()
let data = parser.get_root().get_object();
let response = data.get_object_member('response');
let imageUrl = response.get_object_member('image').get_string_member('url');
if (callback) {
callback(imageUrl);
};
}
});
}
})
});
let WallheavenAdapter = new Lang.Class({
Name: "WallheavenAdapter",
@ -41,18 +41,18 @@ let WallheavenAdapter = new Lang.Class({
'purity': '110', // SFW, sketchy
'sorting': 'random',
'category': '111', // General, Anime, People
'resolutions': ['1920x1200','2560x1440'],
'resolutions': ['1920x1200', '2560x1440']
},
/*
fetch a random image url from wallheaven.cc with the given options
and call callback function with the URL of the image
*/
requestRandomImage: function(callback){
fetch a random image url from wallheaven.cc with the given options
and call callback function with the URL of the image
*/
requestRandomImage: function (callback) {
let session = new Soup.SessionAsync();
let options = this.options;
let optionsString=""
let optionsString = "";
for (var key in options) {
if (options.hasOwnProperty(key)) {
@ -64,40 +64,40 @@ let WallheavenAdapter = new Lang.Class({
}
}
// remove last '&'
optionsString = optionsString.slice(0,-1);
optionsString = optionsString.slice(0, -1);
let url = 'http://alpha.wallhaven.cc/search?'+optionsString;
let url = 'http://alpha.wallhaven.cc/search?' + optionsString;
let message = Soup.Message.new('GET', url);
var _this = this;
session.queue_message(message, function(session, message) {
session.queue_message(message, function (session, message) {
let body = message.response_body.data;
let urlArray = body.match(new RegExp(/http[s]*:\/\/alpha.wallhaven.cc\/wallpaper\/[0-9]+/g));
// remove dublicates from array
let uniqueUrlArray = urlArray.filter(function(item, pos) {
let uniqueUrlArray = urlArray.filter(function (item, pos) {
return urlArray.indexOf(item) == pos;
});
// get a random entry from the array
var url = uniqueUrlArray[Math.floor(Math.random()*uniqueUrlArray.length)];
var url = uniqueUrlArray[Math.floor(Math.random() * uniqueUrlArray.length)];
message = Soup.Message.new('GET', url);
session.queue_message(message, function(){
session.queue_message(message, function () {
let body = message.response_body.data;
let imageUrl = body.match(new RegExp(/\/\/wallpapers.wallhaven.cc\/wallpapers\/full\/.*?"/))[0];
imageUrl = imageUrl.slice(0,-1);
imageUrl = imageUrl.slice(0, -1);
imageUrl = 'http:' + imageUrl;
if (callback) {
callback(imageUrl);
};
}
})
});
}
})
});

View file

@ -15,9 +15,12 @@ const Convenience = Self.imports.convenience;
const Prefs = Self.imports.settings;
const Timer = Self.imports.timer;
const LoggerModule = Self.imports.logger;
let WallpaperController = new Lang.Class({
Name: "WallpaperController",
extensionMeta: null,
logger: null,
wallpaperlocation: '',
currentWallpaper: '',
@ -54,8 +57,10 @@ let WallpaperController = new Lang.Class({
this.history = this._loadHistory();
this.currentWallpaper = this._getCurrentWallpaper();
this.imageSourceAdapter = new SourceAdapter.DesktopperAdapter();
this.imageSourceAdapter = new SourceAdapter.WallheavenAdapter();
this._desktopperAdapter = new SourceAdapter.DesktopperAdapter();
this._wallheavenAdapter = new SourceAdapter.WallheavenAdapter();
this.logger = new LoggerModule.Logger('RWG3', 'WallpaperController');
},
_updateHistory: function() {
@ -81,6 +86,19 @@ let WallpaperController = new Lang.Class({
forwards the request to the adapter
*/
_requestRandomImageFromAdapter: function(callback){
this.imageSourceAdapter = this._desktopperAdapter;
switch (this._settings.get('source', 'enum')) {
case 0:
this.imageSourceAdapter = this._desktopperAdapter;
break;
case 2:
this.imageSourceAdapter = this._wallheavenAdapter;
break;
default:
this.imageSourceAdapter = this._desktopperAdapter;
break;
}
this.imageSourceAdapter.requestRandomImage(callback);
},
@ -110,7 +128,7 @@ let WallpaperController = new Lang.Class({
// call callback with the name and the full filepath of the written file as parameter
if (callback) {
callback(name, output_file.get_path());
};
}
});
},
@ -138,7 +156,7 @@ let WallpaperController = new Lang.Class({
return name;
}
};
}
return false;
},
@ -159,7 +177,7 @@ let WallpaperController = new Lang.Class({
// call callback if given
if (callback) {
callback();
};
}
} else {
// TODO: error handling
}
@ -186,14 +204,14 @@ let WallpaperController = new Lang.Class({
if (!fileinfo) {
break;
};
}
let name = fileinfo.get_name();
// ignore hidden files
if (name[0] != '.') {
history.push(fileinfo.get_name());
};
}
} while(fileinfo);
@ -227,8 +245,10 @@ let WallpaperController = new Lang.Class({
this._timer.begin(); // reset timer
let _this = this;
this._requestRandomImageFromAdapter(function(imageUrl){
_this._fetchFile(imageUrl, function(historyid, path) {
this._requestRandomImageFromAdapter((imageUrl) => {
this.logger.debug("Requesting image: "+imageUrl);
_this._fetchFile(imageUrl, (historyid, path) => {
// insert file into history
_this.history.unshift(historyid);
@ -239,7 +259,7 @@ let WallpaperController = new Lang.Class({
});
if (callback) {
callback();
};
}
});
});
});
@ -248,7 +268,7 @@ let WallpaperController = new Lang.Class({
_backgroundTimout: function(delay) {
if (this.timeout) {
return;
};
}
let _this = this;
delay = delay || 200;