diff --git a/randomwallpaper@iflow.space/prefs.js b/randomwallpaper@iflow.space/prefs.js index baea0b1..5d194a0 100644 --- a/randomwallpaper@iflow.space/prefs.js +++ b/randomwallpaper@iflow.space/prefs.js @@ -17,6 +17,7 @@ const RWG_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.space.iflow.randomwallpa 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_WALLHAVEN = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.wallhaven'; +const RWG_SETTINGS_SCHEMA_REDDIT = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.reddit'; const RWG_SETTINGS_SCHEMA_GENERIC_JSON = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.genericJSON'; const LoggerModule = Self.imports.logger; @@ -44,6 +45,7 @@ var RandomWallpaperSettings = new Lang.Class({ desktopperSettings: null, unsplashSettings: null, wallhavenSettings: null, + redditSettings: null, genericJsonSettings: null, _wallpaperController: null, @@ -73,6 +75,11 @@ var RandomWallpaperSettings = new Lang.Class({ this.wallhavenSettings = this._builder.get_object('wallhaven-settings'); this.bindWallhaven(); + // Reddit Settings + this._reddit_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_REDDIT); + this.redditSettings = this._builder.get_object('reddit-settings'); + this.bindReddit(); + // Generic JSON Settings this._generic_json_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_GENERIC_JSON); this.genericJsonSettings = this._builder.get_object('generic-json-settings'); @@ -102,7 +109,10 @@ var RandomWallpaperSettings = new Lang.Class({ case 2: // wallhaven this.currentSourceSettingsWidget = this.wallhavenSettings; break; - case 3: // generic JSON + case 3: // reddit + this.currentSourceSettingsWidget = this.redditSettings; + break; + case 4: // generic JSON this.currentSourceSettingsWidget = this.genericJsonSettings; break; default: @@ -227,6 +237,17 @@ var RandomWallpaperSettings = new Lang.Class({ Gio.SettingsBindFlags.DEFAULT); }, + bindReddit: function () { + this._reddit_settings.bind('subreddits', + this._builder.get_object('reddit-subreddits'), + 'text', + Gio.SettingsBindFlags.DEFAULT); + this._reddit_settings.bind('allow-sfw', + this._builder.get_object('reddit-allow-sfw'), + 'active', + Gio.SettingsBindFlags.DEFAULT); + }, + bindGenericJSON: function () { this._builder.get_object('generic-json-docs-link').set_label("More information here"); this._generic_json_settings.bind('generic-json-request-url', diff --git a/randomwallpaper@iflow.space/schemas/gschemas.compiled b/randomwallpaper@iflow.space/schemas/gschemas.compiled index bee5c6b..3f9c5ec 100644 Binary files a/randomwallpaper@iflow.space/schemas/gschemas.compiled and b/randomwallpaper@iflow.space/schemas/gschemas.compiled differ diff --git a/randomwallpaper@iflow.space/schemas/org.gnome.shell.extensions.space.iflow.randomwallpaper.gschema.xml b/randomwallpaper@iflow.space/schemas/org.gnome.shell.extensions.space.iflow.randomwallpaper.gschema.xml index 0911d56..3a6d22b 100644 --- a/randomwallpaper@iflow.space/schemas/org.gnome.shell.extensions.space.iflow.randomwallpaper.gschema.xml +++ b/randomwallpaper@iflow.space/schemas/org.gnome.shell.extensions.space.iflow.randomwallpaper.gschema.xml @@ -5,7 +5,8 @@ - + + + + + "" + Subreddits + These subreddits will be searched. + + + false + SFW + Whether safe images are allowed. + + + diff --git a/randomwallpaper@iflow.space/settings.ui b/randomwallpaper@iflow.space/settings.ui index 535014a..530a5b6 100644 --- a/randomwallpaper@iflow.space/settings.ui +++ b/randomwallpaper@iflow.space/settings.ui @@ -199,6 +199,93 @@ You can also define a prefix that will be added to the image URL. 1 10 + + True + False + 10 + 10 + 10 + 10 + No Settings Available + + + + True + False + gtk-open + + + True + False + 10 + 10 + 10 + 10 + vertical + + + True + False + start + Subreddits (separate with a comma) + + + False + True + 0 + + + + + True + True + e.g.: wallpaper, wallpapers, minimalwallpaper + + + False + True + 1 + + + + + True + False + start + + + SFW (Safe for work) + True + True + False + True + + + True + True + 0 + + + + + False + True + 2 + + + + + True + False + gtk-refresh + + + True + False + gtk-delete + 400 600 @@ -320,7 +407,7 @@ You can also define a prefix that will be added to the image URL. True False True - Also change the gnome lock screen image to the new wallpaper. + Change the gnome lock screen image to the new wallpaper. True True 1 @@ -546,7 +633,7 @@ You can also define a prefix that will be added to the image URL. True False True - Disable the desktop preview of the background while hovering the history items. Try enabeling if you encounter crashes or laggs of the gnome shell while using the extension. + Disable the desktop preview of the background while hovering the history items. Try enabling if you encounter crashes or lags of the gnome-shell while using the extension. True True 1 @@ -688,7 +775,7 @@ You can also define a prefix that will be added to the image URL. True False True - The source that is used to fetch random wallpapers. You can select between unsplash.com, desktoppr.co, wallhaven.cc and a generic JSON source for experienced users. + The source that is used to fetch random wallpapers. You can select between built-in ones or the generic JSON source for experienced users. True 1 0 @@ -724,9 +811,10 @@ You can also define a prefix that will be added to the image URL. False 0 - unsplash.com - desktoppr.co - alpha.wallhaven.cc + Unsplash + Desktoppr + Wallhaven + Reddit Generic JSON @@ -1057,7 +1145,7 @@ You can also define a prefix that will be added to the image URL. True False True - Automatically fetch a new wallpaper based on a period. + Automatically fetch new wallpapers based on an interval. True True 1 @@ -1213,33 +1301,6 @@ You can also define a prefix that will be added to the image URL. - - True - False - 10 - 10 - 10 - 10 - No Settings Available - - - - True - False - gtk-open - - - True - False - gtk-refresh - - - True - False - gtk-delete - 1 1000000 @@ -1456,7 +1517,7 @@ You can also define a prefix that will be added to the image URL. True False - This results in a smaller wallpaper pool but the images are considered to have higher quality. + This option results in a smaller wallpaper pool, but the images are considered to be of higher quality. True 1 0 diff --git a/randomwallpaper@iflow.space/sourceAdapter.js b/randomwallpaper@iflow.space/sourceAdapter.js index a9f2f7e..ad3e3a1 100644 --- a/randomwallpaper@iflow.space/sourceAdapter.js +++ b/randomwallpaper@iflow.space/sourceAdapter.js @@ -8,6 +8,7 @@ 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_WALLHAVEN = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.wallhaven'; +const RWG_SETTINGS_SCHEMA_REDDIT = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.reddit'; const RWG_SETTINGS_SCHEMA_GENERIC_JSON = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.genericJSON'; const SettingsModule = Self.imports.settings; @@ -106,7 +107,7 @@ var DesktopperAdapter = new Lang.Class({ let imageDownloadUrl = encodeURI(response.image.url); if (callback) { - let historyEntry = new HistoryModule.HistoryEntry(null, 'desktopper.co', imageDownloadUrl); + let historyEntry = new HistoryModule.HistoryEntry(null, 'Desktopper', imageDownloadUrl); historyEntry.source.sourceUrl = 'https://www.desktoppr.co/'; callback(historyEntry); } @@ -297,7 +298,7 @@ var WallhavenAdapter = new Lang.Class({ imageDownloadUrl = encodeURI(imageDownloadUrl); if (callback) { - let historyEntry = new HistoryModule.HistoryEntry(null, 'wallhaven.cc', imageDownloadUrl); + let historyEntry = new HistoryModule.HistoryEntry(null, 'Wallhaven', imageDownloadUrl); historyEntry.source.sourceUrl = 'https://alpha.wallhaven.cc/'; historyEntry.source.imageLinkUrl = url; callback(historyEntry); @@ -352,6 +353,60 @@ var WallhavenAdapter = new Lang.Class({ } }); +var RedditAdapter = new Lang.Class({ + Name: "RedditAdapter", + Extends: BaseAdapter, + + _settings: null, + + _init: function () { + this.parent(); + this._settings = new SettingsModule.Settings(RWG_SETTINGS_SCHEMA_REDDIT); + }, + + requestRandomImage: function (callback) { + let session = new Soup.SessionAsync(); + + const subreddits = this._settings.get('subreddits', 'string').split(',').map(s => s.trim()).join('+'); + const require_sfw = this._settings.get('allow-sfw', 'boolean'); + const url = encodeURI(`https://www.reddit.com/r/${subreddits}.json`); + + let message = Soup.Message.new('GET', url); + + if (message === null) { + this._error("Could not create request.", callback); + return; + } + + session.queue_message(message, (session, message) => { + try { + const submissions = JSON.parse(message.response_body.data).data.children.filter(child => { + if(child.data.post_hint !== 'image') return false; + if(require_sfw) return child.data.over_18 === false; + return true; + }); + if(submissions.length === 0) { + this._error("No suitable submissions found!", callback); + return; + } + const random = Math.floor(Math.random() * submissions.length); + const submission = submissions[random].data; + const imageDownloadUrl = submission.preview.images[0].source.url; + + if (callback) { + let historyEntry = new HistoryModule.HistoryEntry(null, 'Reddit', imageDownloadUrl); + historyEntry.source.sourceUrl = 'https://www.reddit.com/' + submission.subreddit_name_prefixed; + historyEntry.source.imageLinkUrl = 'https://www.reddit.com/' + submission.permalink; + callback(historyEntry); + } + } catch (e) { + this._error("Could not create request. (" + e + ")", callback); + return; + } + }); + } +}); + var GenericJsonAdapter = new Lang.Class({ Name: "GenericJsonAdapter", Extends: BaseAdapter, diff --git a/randomwallpaper@iflow.space/wallpaperController.js b/randomwallpaper@iflow.space/wallpaperController.js index f9e9e23..b9d2901 100644 --- a/randomwallpaper@iflow.space/wallpaperController.js +++ b/randomwallpaper@iflow.space/wallpaperController.js @@ -54,6 +54,7 @@ var WallpaperController = new Lang.Class({ this._desktopperAdapter = new SourceAdapter.DesktopperAdapter(); this._unsplashAdapter = new SourceAdapter.UnsplashAdapter(); this._wallhavenAdapter = new SourceAdapter.WallhavenAdapter(); + this._redditAdapter = new SourceAdapter.RedditAdapter(); this._genericJsonAdapter = new SourceAdapter.GenericJsonAdapter(); this._updateHistory(); @@ -98,6 +99,9 @@ var WallpaperController = new Lang.Class({ this.imageSourceAdapter = this._wallhavenAdapter; break; case 3: + this.imageSourceAdapter = this._redditAdapter; + break; + case 4: this.imageSourceAdapter = this._genericJsonAdapter; break; default: