Implement source settings.

This commit is contained in:
Wolfgang Rumpler 2017-07-20 20:54:23 +02:00
parent ab42930567
commit 1e5dabe0b0
6 changed files with 747 additions and 85 deletions

View file

@ -12,6 +12,11 @@ const Gettext = imports.gettext.domain('space.iflow.randomwallpaper');
//const _ = Gettext.gettext;
const RWG_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.space.iflow.randomwallpaper';
const RWG_SETTINGS_SCHEMA_DESKTOPPER = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.desktopper';
const RWG_SETTINGS_SCHEMA_UNSPLASH = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.unsplash';
const RWG_SETTINGS_SCHEMA_WALLHEAVEN = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.wallheaven';
const LoggerModule = Self.imports.logger;
function init() {
//Convenience.initTranslations();
@ -28,6 +33,14 @@ function buildPrefsWidget() {
/* UI Setup */
const RandomWallpaperSettings = new Lang.Class({
Name: 'RandomWallpaper.Settings',
logger: null,
currentSourceSettingsWidget: null,
noSettings: null,
desktopperSettings: null,
unsplashSettings: null,
wallheavenSettings: null,
_init: function () {
this._settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA);
@ -35,6 +48,25 @@ const RandomWallpaperSettings = new Lang.Class({
//this._builder.set_translation_domain(Self.metadata['gettext-domain']);
this._builder.add_from_file(Self.path + '/settings.ui');
this.logger = new LoggerModule.Logger('RWG3', 'RandomWallpaper.Settings');
this.noSettings = this._builder.get_object('no-settings');
// Desktopper Settings
this._desktopper_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_DESKTOPPER);
this.desktopperSettings = this._builder.get_object('desktopper-settings');
this.bindDesktopper();
// Unsplash Settings
this._unsplash_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_UNSPLASH);
this.unsplashSettings = this._builder.get_object('unsplash-settings');
this.bindUnsplash();
// Wallheaven Settings
this._wallheaven_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_WALLHEAVEN);
this.wallheavenSettings = this._builder.get_object('wallheaven-settings');
this.bindWallheaven();
this._toggleAfSliders();
this.widget = this._builder.get_object('main-widget');
@ -43,6 +75,31 @@ const RandomWallpaperSettings = new Lang.Class({
this._toggleAfSliders();
}.bind(this));
this._builder.get_object('source-combo').connect('changed', (sourceCombo) => {
let container = this._builder.get_object('source-settings-frame');
if (this.currentSourceSettingsWidget !== null) {
container.remove(this.currentSourceSettingsWidget);
}
switch (sourceCombo.active) {
case 0: // desktopper
this.currentSourceSettingsWidget = this.desktopperSettings;
break;
case 1: // unsplash
this.currentSourceSettingsWidget = this.unsplashSettings;
break;
case 2: // wallheaven
this.currentSourceSettingsWidget = this.wallheavenSettings;
break;
default:
this.currentSourceSettingsWidget = this.noSettings;
break;
}
container.add(this.currentSourceSettingsWidget);
});
this._settings.bind('history-length',
this._builder.get_object('history-length'),
'value',
@ -73,6 +130,66 @@ const RandomWallpaperSettings = new Lang.Class({
this._builder.get_object('duration-slider-hours').set_sensitive(false);
this._builder.get_object('duration-slider-minutes').set_sensitive(false);
}
},
bindDesktopper: function () {
this._desktopper_settings.bind('allow-unsafe',
this._builder.get_object('desktopper-allow-unsafe'),
'active',
Gio.SettingsBindFlags.DEFAULT);
},
bindUnsplash: function () {
this._unsplash_settings.bind('unsplash-keyword',
this._builder.get_object('unsplash-keyword'),
'text',
Gio.SettingsBindFlags.DEFAULT);
this._unsplash_settings.bind('username',
this._builder.get_object('unsplash-username'),
'text',
Gio.SettingsBindFlags.DEFAULT);
this._unsplash_settings.bind('image-width',
this._builder.get_object('unsplash-image-width'),
'value',
Gio.SettingsBindFlags.DEFAULT);
this._unsplash_settings.bind('image-height',
this._builder.get_object('unsplash-image-height'),
'value',
Gio.SettingsBindFlags.DEFAULT);
},
bindWallheaven: function () {
this._wallheaven_settings.bind('wallheaven-keyword',
this._builder.get_object('wallheaven-keyword'),
'text',
Gio.SettingsBindFlags.DEFAULT);
this._wallheaven_settings.bind('resolutions',
this._builder.get_object('wallheaven-resolutions'),
'text',
Gio.SettingsBindFlags.DEFAULT);
this._wallheaven_settings.bind('category-general',
this._builder.get_object('wallheaven-category-general'),
'active',
Gio.SettingsBindFlags.DEFAULT);
this._wallheaven_settings.bind('category-anime',
this._builder.get_object('wallheaven-category-anime'),
'active',
Gio.SettingsBindFlags.DEFAULT);
this._wallheaven_settings.bind('category-people',
this._builder.get_object('wallheaven-category-people'),
'active',
Gio.SettingsBindFlags.DEFAULT);
this._wallheaven_settings.bind('allow-sfw',
this._builder.get_object('wallheaven-allow-sfw'),
'active',
Gio.SettingsBindFlags.DEFAULT);
this._wallheaven_settings.bind('allow-sketchy',
this._builder.get_object('wallheaven-allow-sketchy'),
'active',
Gio.SettingsBindFlags.DEFAULT);
}
});

View file

@ -2,13 +2,13 @@
<schemalist gettext-domain='gnome-shell-extensions'>
<enum id='org.gnome.shell.extensions.space.iflow.randomwallpaper.sources'>
<value value='0' nick='desktoppr'/>
<value value='1' nick='unsplash'/>
<value value='2' nick='wallheaven'/>
<value value='0' nick='desktoppr'/>
<value value='1' nick='unsplash'/>
<value value='2' nick='wallheaven'/>
</enum>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper-sources/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper'>
id='org.gnome.shell.extensions.space.iflow.randomwallpaper'>
<key type='i' name='history-length'>
<default>10</default>
@ -38,9 +38,9 @@
</key>
<key name='source' enum='org.gnome.shell.extensions.space.iflow.randomwallpaper.sources'>
<default>'desktoppr'</default>
<summary>Wallpaper Source</summary>
<description>Describs the adapter that will be used.</description>
<default>'desktoppr'</default>
<summary>Wallpaper Source</summary>
<description>Describs the adapter that will be used.</description>
</key>
<key type='as' name='history'>
@ -56,4 +56,77 @@
</key>
</schema>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper-sources/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper.desktopper'>
<key type='b' name='allow-unsafe'>
<default>false</default>
<summary>Allow Unsafe</summary>
<description>Weather the extension should fetch images that are rated as unsafe.</description>
</key>
</schema>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper-sources/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper.unsplash'>
<key type='s' name='unsplash-keyword'>
<default>""</default>
<summary>Keyword</summary>
<description>The keyword will be used to search images.</description>
</key>
<key type='s' name='username'>
<default>""</default>
<summary>Username</summary>
<description>Only fetch random images of a given user.</description>
</key>
<key type='i' name='image-width'>
<default>1920</default>
<summary>Image Width</summary>
<description>The width of the image.</description>
</key>
<key type='i' name='image-height'>
<default>1080</default>
<summary>Image Width</summary>
<description>The height of the image.</description>
</key>
</schema>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper-sources/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper.wallheaven'>
<key type='s' name='wallheaven-keyword'>
<default>""</default>
<summary>Keyword</summary>
<description>The keyword will be used to search images.</description>
</key>
<key type='s' name='resolutions'>
<default>"1920x1200, 1920x1080, 2560x1440, 2560x1600, 3840x1080"</default>
<summary>Resolutions</summary>
<description>The acceptable resolutions.</description>
</key>
<key type='b' name='allow-sfw'>
<default>true</default>
<summary>SFW</summary>
<description>Weather safe images are allowed.</description>
</key>
<key type='b' name='allow-sketchy'>
<default>false</default>
<summary>Sketchy</summary>
<description>Weather sketchy images are allowed.</description>
</key>
<key type='b' name='category-general'>
<default>true</default>
<summary>Category General</summary>
<description>Weather the general category should be searched.</description>
</key>
<key type='b' name='category-anime'>
<default>true</default>
<summary>Category Anime</summary>
<description>Weather the anime category should be searched.</description>
</key>
<key type='b' name='category-people'>
<default>true</default>
<summary>Category People</summary>
<description>Weather the people category should be searched.</description>
</key>
</schema>
</schemalist>

View file

@ -6,31 +6,37 @@ const Self = imports.misc.extensionUtils.getCurrentExtension();
const Convenience = Self.imports.convenience;
let Settings = new Lang.Class({
Name: "Settings",
Name: "Settings",
_settings: null,
_init: function() {
this._settings = Convenience.getSettings();
},
/**
* Settings object.
*
* @param [schema]
* @private
*/
_init: function (schema) {
this._settings = Convenience.getSettings(schema);
},
observe: function(key, callback) {
this._settings.connect('changed::'+key, callback);
},
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;
}
},
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);
},
get: function (key, type) {
return this._settings['get_' + type](key);
},
getSourceAdapter: function() {
getSourceAdapter: function () {
global.log(this._settings.get_enum('source'));
return null;
}
return null;
}
});

View file

@ -2,6 +2,29 @@
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkBox" id="desktopper-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkCheckButton" id="desktopper-allow-unsafe">
<property name="label" translatable="yes">Allow unsafe images (also fetch images that are marked as unsafe)</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<object class="GtkAdjustment" id="duration-hours">
<property name="upper">23</property>
<property name="value">1</property>
@ -131,34 +154,22 @@
</packing>
</child>
<child>
<object class="GtkFrame">
<object class="GtkFrame" id="source-settings-frame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkAlignment">
<placeholder/>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="label" translatable="yes">No Settings Available</property>
<style>
<class name="dim-label"/>
</style>
</object>
</child>
<property name="label" translatable="yes">Source Settings</property>
</object>
</child>
<child type="label_item">
<placeholder/>
</child>
</object>
<packing>
<property name="left_attach">0</property>
@ -466,4 +477,362 @@
</packing>
</child>
</object>
<object class="GtkLabel" id="no-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="label" translatable="yes">No Settings Available</property>
<style>
<class name="dim-label"/>
</style>
</object>
<object class="GtkAdjustment" id="unsplash-image-height">
<property name="lower">1</property>
<property name="upper">1000000</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkAdjustment" id="unsplash-image-width">
<property name="lower">1</property>
<property name="upper">1000000</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkBox" id="unsplash-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Username</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="unsplash-username">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_bottom">10</property>
<property name="placeholder_text" translatable="yes">@username</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Keyword</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="unsplash-keyword">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_bottom">10</property>
<property name="placeholder_text" translatable="yes">Enter a keyword ...</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="hexpand">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Image Width</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="input_purpose">number</property>
<property name="adjustment">unsplash-image-width</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="hexpand">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Image Height</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="input_purpose">number</property>
<property name="adjustment">unsplash-image-height</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
<object class="GtkBox" id="wallheaven-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Keyword</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="wallheaven-keyword">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_bottom">10</property>
<property name="placeholder_text" translatable="yes">Enter a keyword ...</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Allowed Content Ratings</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButtonBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">10</property>
<property name="layout_style">start</property>
<child>
<object class="GtkCheckButton" id="wallheaven-allow-sfw">
<property name="label" translatable="yes">SFW (Safe for work)</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="wallheaven-allow-sketchy">
<property name="label" translatable="yes">Sketchy</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Categories</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkButtonBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">10</property>
<property name="layout_style">start</property>
<child>
<object class="GtkCheckButton" id="wallheaven-category-general">
<property name="label" translatable="yes">General</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="wallheaven-category-anime">
<property name="label" translatable="yes">Anime</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="wallheaven-category-people">
<property name="label" translatable="yes">People</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Resolutions</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">6</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="wallheaven-resolutions">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="placeholder_text" translatable="yes">1920x1080, 1920x1200</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">7</property>
</packing>
</child>
</object>
</interface>

View file

@ -5,6 +5,11 @@ const Self = imports.misc.extensionUtils.getCurrentExtension();
const Soup = imports.gi.Soup;
const Json = imports.gi.Json;
const RWG_SETTINGS_SCHEMA_DESKTOPPER = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.desktopper';
const RWG_SETTINGS_SCHEMA_UNSPLASH = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.unsplash';
const RWG_SETTINGS_SCHEMA_WALLHEAVEN = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.wallheaven';
const SettingsModule = Self.imports.settings;
const HistoryModule = Self.imports.history;
const LoggerModule = Self.imports.logger;
@ -13,7 +18,6 @@ let BaseAdapter = new Lang.Class({
Name: "BaseAdapter",
logger: null,
_init: function () {
this.logger = new LoggerModule.Logger('RWG3', 'BaseAdapter');
},
@ -28,8 +32,7 @@ let BaseAdapter = new Lang.Class({
callback(null);
},
fileName: function(uri)
{
fileName: function (uri) {
let base = new String(uri).substring(uri.lastIndexOf('/') + 1);
return base;
},
@ -40,18 +43,33 @@ let DesktopperAdapter = new Lang.Class({
Name: "DesktopperAdapter",
Extends: BaseAdapter,
_settings: null,
_init: function () {
this.parent();
this._settings = new SettingsModule.Settings(RWG_SETTINGS_SCHEMA_DESKTOPPER);
},
requestRandomImage: function (callback) {
let session = new Soup.SessionAsync();
let message = Soup.Message.new('GET', 'https://api.desktoppr.co/1/wallpapers/random');
let parser = new Json.Parser();
let url = 'https://api.desktoppr.co/1/wallpapers/random';
let allowUnsafe = this._settings.get('allow-unsafe', 'boolean');
if (allowUnsafe) {
url += '?safe_filter=all';
} else {
url += '?safe_filter=safe';
}
url = encodeURI(url);
this.logger.debug("Base URL: " + url);
let message = Soup.Message.new('GET', url);
session.queue_message(message, (session, message) => {
parser.load_from_data(message.response_body.data, -1);
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');
let data = JSON.parse(message.response_body.data);
let response = data.response;
let imageUrl = encodeURI(response.image.url);
if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(null, 'desktopper.co', imageUrl);
@ -69,10 +87,32 @@ let UnsplashAdapter = new Lang.Class({
sourceName: 'Unsplash',
sourceUrl: 'https://unsplash.com/',
_settings: null,
// query options
options: {
'username': '',
'query': '',
'w': 1920,
'h': 1080,
},
_init: function () {
this.parent();
this._settings = new SettingsModule.Settings(RWG_SETTINGS_SCHEMA_UNSPLASH);
},
requestRandomImage: function (callback) {
let session = new Soup.SessionAsync();
let url = 'https://api.unsplash.com/photos/random';
url += '?client_id=64daf439e9b579dd566620c0b07022706522d87b255d06dd01d5470b7f193b8d';
this._readOptionsFromSettings();
let optionsString = this._generateOptionsString();
let url = 'https://api.unsplash.com/photos/random?' + optionsString;
url += 'client_id=64daf439e9b579dd566620c0b07022706522d87b255d06dd01d5470b7f193b8d';
url = encodeURI(url);
this.logger.debug("Base URL: " + url);
let message = Soup.Message.new('GET', url);
let utmParameters = '?utm_source=RandomWallpaperGnome3&utm_medium=referral&utm_campaign=api-credit';
@ -80,59 +120,75 @@ let UnsplashAdapter = new Lang.Class({
session.queue_message(message, (session, message) => {
let data = JSON.parse(message.response_body.data);
let imageUrl = data.links.download;
let imageUrl = encodeURI(data.links.download + utmParameters);
let authorName = data.user.name;
let authorUrl = data.user.links.html;
this.logger.debug(authorName);
this.logger.debug(authorUrl);
let authorUrl = encodeURI(data.user.links.html);
if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(authorName, this.sourceName, imageUrl+utmParameters);
historyEntry.source.sourceUrl = this.sourceUrl+utmParameters;
historyEntry.source.authorUrl = authorUrl+utmParameters;
let historyEntry = new HistoryModule.HistoryEntry(authorName, this.sourceName, encodeURI(imageUrl));
historyEntry.source.sourceUrl = encodeURI(this.sourceUrl + utmParameters);
historyEntry.source.authorUrl = encodeURI(authorUrl + utmParameters);
callback(historyEntry);
}
});
},
_generateOptionsString: function () {
let options = this.options;
let optionsString = "";
for (let key in options) {
if (options.hasOwnProperty(key)) {
if (options[key]) {
optionsString += key + "=" + options[key] + "&";
}
}
}
return optionsString;
},
_readOptionsFromSettings: function () {
this.options.query = this._settings.get('unsplash-keyword', 'string');
this.options.username = this._settings.get('username', 'string');
if (this.options.username[0] === '@') {
this.options.username = this.options.username.substring(1); // remove @ prefix
}
this.options.w = this._settings.get('image-width', 'int');
this.options.h = this._settings.get('image-height', 'int');
}
});
let WallheavenAdapter = new Lang.Class({
Name: "WallheavenAdapter",
Extends: BaseAdapter,
_settings: null,
// query options
options: {
'q': '',
'purity': '110', // SFW, sketchy
'sorting': 'random',
'category': '111', // General, Anime, People
'categories': '111', // General, Anime, People
'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
*/
_init: function () {
this.parent();
this._settings = new SettingsModule.Settings(RWG_SETTINGS_SCHEMA_WALLHEAVEN);
},
requestRandomImage: function (callback) {
let session = new Soup.SessionAsync();
let options = this.options;
let optionsString = "";
for (let key in options) {
if (options.hasOwnProperty(key)) {
if (Array.isArray(options[key])) {
optionsString += key + "=" + options[key].join() + "&";
} else {
optionsString += key + "=" + options[key] + "&";
}
}
}
// remove last '&'
optionsString = optionsString.slice(0, -1);
this._readOptionsFromSettings();
let optionsString = this._generateOptionsString();
let url = 'http://alpha.wallhaven.cc/search?' + optionsString;
url = encodeURI(url);
this.logger.debug("Base URL: " + url);
let message = Soup.Message.new('GET', url);
@ -155,6 +211,7 @@ let WallheavenAdapter = new Lang.Class({
let imageUrl = body.match(new RegExp(/\/\/wallpapers.wallhaven.cc\/wallpapers\/full\/.*?"/))[0];
imageUrl = imageUrl.slice(0, -1);
imageUrl = 'http:' + imageUrl;
imageUrl = encodeURI(imageUrl);
if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(null, 'wallhaven.cc', imageUrl);
@ -165,5 +222,45 @@ let WallheavenAdapter = new Lang.Class({
});
},
_generateOptionsString: function () {
let options = this.options;
let optionsString = "";
for (let key in options) {
if (options.hasOwnProperty(key)) {
if (Array.isArray(options[key])) {
optionsString += key + "=" + options[key].join() + "&";
} else {
if (options[key]) {
optionsString += key + "=" + options[key] + "&";
}
}
}
}
return optionsString;
},
_readOptionsFromSettings: function () {
this.options.q = this._settings.get('wallheaven-keyword', 'string');
this.options.resolutions = this._settings.get('resolutions', 'string').split(',');
this.options.resolutions = this.options.resolutions.map((elem) => {
return elem.trim();
});
let categories = [];
categories.push(+this._settings.get('category-general', 'boolean')); // + is implicit conversion to int
categories.push(+this._settings.get('category-anime', 'boolean'));
categories.push(+this._settings.get('category-people', 'boolean'));
this.options.categories = categories.join('');
let purity = [];
purity.push(+this._settings.get('allow-sfw', 'boolean'));
purity.push(+this._settings.get('allow-sketchy', 'boolean'));
purity.push(0); // required by wallheaven
this.options.purity = purity.join('');
}
});