Skip to content

Commit

Permalink
Add simple url source
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucki committed Dec 12, 2022
1 parent 9b9c5ee commit 52567bc
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ blueprint-compiler batch-compile "$BASEDIR/ui" "$BASEDIR/ui" \
"$BASEDIR/ui/reddit.blp" \
"$BASEDIR/ui/sourceRow.blp" \
"$BASEDIR/ui/unsplash.blp" \
"$BASEDIR/ui/urlSource.blp" \
"$BASEDIR/ui/wallhaven.blp"

cd "$BASEDIR" || exit 1
Expand Down
59 changes: 59 additions & 0 deletions [email protected]/adapter/urlSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const Self = imports.misc.extensionUtils.getCurrentExtension();
const HistoryModule = Self.imports.history;
const JSONPath = Self.imports.jsonpath.jsonpath;
const SettingsModule = Self.imports.settings;

const BaseAdapter = Self.imports.adapter.baseAdapter;

const RWG_SETTINGS_SCHEMA_URL_SOURCE = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.sources.urlSource';

var UrlSourceAdapter = class extends BaseAdapter.BaseAdapter {
constructor(id, wallpaperLocation) {
super(wallpaperLocation);
let path = `/org/gnome/shell/extensions/space-iflow-randomwallpaper/sources/urlSource/${id}/`;
this._settings = new SettingsModule.Settings(RWG_SETTINGS_SCHEMA_URL_SOURCE, path);
}

requestRandomImage(callback) {
let imageDownloadUrl = this._settings.get("image-url", "string");
let authorName = this._settings.get("author-name", "string");
let authorUrl = this._settings.get("author-url", "string");
let domainUrl = this._settings.get("domain", "string");
let postUrl = this._settings.get("domain", "string");

let identifier = this._settings.get("name", "string");
if (identifier === null || identifier === "") {
identifier = 'Static URL';
}

if (typeof postUrl !== 'string' || !postUrl instanceof String) {
postUrl = null;
}

if (typeof authorName !== 'string' || !authorName instanceof String) {
authorName = null;
}

if (typeof authorUrl !== 'string' || !authorUrl instanceof String) {
authorUrl = null;
}

if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(authorName, identifier, imageDownloadUrl);

if (authorUrl !== null && authorUrl !== "") {
historyEntry.source.authorUrl = authorUrl;
}

if (postUrl !== null && postUrl !== "") {
historyEntry.source.imageLinkUrl = postUrl;
}

if (domainUrl !== null && domainUrl !== "") {
historyEntry.source.sourceUrl = domainUrl;
}

callback(historyEntry);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
</schema>

<schema id='org.gnome.shell.extensions.space.iflow.randomwallpaper.sources.localFolder'>

<key type='s' name='name'>
<default>"Local Folder"</default>
<summary>Name</summary>
Expand All @@ -340,4 +341,44 @@

</schema>

<schema id='org.gnome.shell.extensions.space.iflow.randomwallpaper.sources.urlSource'>

<key type='s' name='name'>
<default>"Static URL"</default>
<summary>Name</summary>
<description>Name for this source.</description>
</key>

<key type='s' name='image-url'>
<default>""</default>
<summary>Image URL</summary>
<description>The URL to fetch.</description>
</key>

<key type='s' name='domain'>
<default>""</default>
<summary>Domain</summary>
<description>The domain used for linking the main page.</description>
</key>

<key type='s' name='author-name'>
<default>""</default>
<summary>Author name</summary>
<description>The name of the image author.</description>
</key>

<key type='s' name='author-url'>
<default>""</default>
<summary>Author URL</summary>
<description>The URL to link to the author.</description>
</key>

<key type='s' name='post-url'>
<default>""</default>
<summary>Post URL</summary>
<description>The URL to link to the image page.</description>
</key>

</schema>

</schemalist>
2 changes: 1 addition & 1 deletion [email protected]/ui/localFolder.blp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ template LocalFolderSettingsGroup : Adw.PreferencesGroup {
title: _("General");

Adw.EntryRow folder_row {
title: "Folder";
title: _("Folder");

Button folder {
valign: center;
Expand Down
16 changes: 16 additions & 0 deletions [email protected]/ui/sourceRow.blp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ template SourceRow : Adw.ExpanderRow {
title: bind source_name.text;
show-enable-switch: true;

// Doesn't look good and prone to missclicks
// [action]
// Button button_delete {
// valign: center;

// styles [
// "destructive-action",
// ]

// Adw.ButtonContent {
// icon-name: "user-trash-symbolic";
// valign: center;
// }
// }

Box {
orientation: vertical;
spacing: 14;
Expand All @@ -30,6 +45,7 @@ template SourceRow : Adw.ExpanderRow {
"Reddit",
_("Generic JSON"),
_("Local Folder"),
_("Static URL"),
]
};
}
Expand Down
4 changes: 4 additions & 0 deletions [email protected]/ui/sourceRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Wallhaven = Self.imports.ui.wallhaven;
const Reddit = Self.imports.ui.reddit;
const GenericJson = Self.imports.ui.genericJson;
const LocalFolder = Self.imports.ui.localFolder;
const UrlSource = Self.imports.ui.urlSource;

// https://gitlab.gnome.org/GNOME/gjs/-/blob/master/examples/gtk4-template.js
var SourceRow = GObject.registerClass({
Expand Down Expand Up @@ -74,6 +75,9 @@ var SourceRow = GObject.registerClass({
case 4: // Local Folder
targetWidget = new LocalFolder.LocalFolderSettingsGroup(this);
break;
case 5: // Static URL
targetWidget = new UrlSource.UrlSourceSettingsGroup(this);
break;
default:
targetWidget = null;
this.logger.error("The selected source has no corresponding widget!")
Expand Down
75 changes: 75 additions & 0 deletions [email protected]/ui/urlSource.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Gtk 4.0;
using Adw 1;

template UrlSourceSettingsGroup : Adw.PreferencesGroup {
Adw.PreferencesGroup {
title: _("General");

Adw.EntryRow domain {
title: _("Domain");
input-purpose: url;

LinkButton {
valign: center;
uri: bind domain.text;

Adw.ButtonContent {
icon-name: "globe-symbolic";
}

styles [
"flat",
]
}
}

Adw.EntryRow image_url {
title: _("Image URL");

LinkButton {
valign: center;
uri: bind image_url.text;

Adw.ButtonContent {
icon-name: "globe-symbolic";
}

styles [
"flat",
]
}
}

Adw.EntryRow post_url {
title: _("Post URL");
input-purpose: free_form;

LinkButton {
valign: center;
uri: bind post_url.text;

Adw.ButtonContent {
icon-name: "globe-symbolic";
}

styles [
"flat",
]
}
}
}

Adw.PreferencesGroup {
title: _("Author");

Adw.EntryRow author_name {
title: _("Name");
input-purpose: free_form;
}

Adw.EntryRow author_url {
title: _("URL");
input-purpose: free_form;
}
}
}
55 changes: 55 additions & 0 deletions [email protected]/ui/urlSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Adw = imports.gi.Adw;
const ExtensionUtils = imports.misc.extensionUtils;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;

const Self = ExtensionUtils.getCurrentExtension();
const Convenience = Self.imports.convenience;

const RWG_SETTINGS_SCHEMA_LOCAL_FOLDER = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.sources.urlSource';

var UrlSourceSettingsGroup = GObject.registerClass({
GTypeName: 'UrlSourceSettingsGroup',
Template: GLib.filename_to_uri(Self.path + '/ui/urlSource.ui', null),
InternalChildren: [
'author_name',
'author_url',
'domain',
'image_url',
'post_url',
]
}, class UrlSourceSettingsGroup extends Adw.PreferencesGroup {
constructor(parent_row, params = {}) {
super(params);

const path = `/org/gnome/shell/extensions/space-iflow-randomwallpaper/sources/urlSource/${parent_row.id}/`;
this._settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_LOCAL_FOLDER, path);

this._settings.bind('name',
parent_row.source_name,
'text',
Gio.SettingsBindFlags.DEFAULT);

this._settings.bind('author-name',
this._author_name,
'text',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('author-url',
this._author_url,
'text',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('domain',
this._domain,
'text',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('image-url',
this._image_url,
'text',
Gio.SettingsBindFlags.DEFAULT);
this._settings.bind('post-url',
this._post_url,
'text',
Gio.SettingsBindFlags.DEFAULT);
}
});
4 changes: 4 additions & 0 deletions [email protected]/wallpaperController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const GenericJsonAdapter = Self.imports.adapter.genericJson;
const LocalFolderAdapter = Self.imports.adapter.localFolder;
const RedditAdapter = Self.imports.adapter.reddit;
const UnsplashAdapter = Self.imports.adapter.unsplash;
const UrlSourceAdapter = Self.imports.adapter.urlSource;
const WallhavenAdapter = Self.imports.adapter.wallhaven;

const RWG_SETTINGS_SCHEMA_BACKEND_CONNECTION = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.backend-connection';
Expand Down Expand Up @@ -160,6 +161,9 @@ var WallpaperController = class {
case 4:
imageSourceAdapter = new LocalFolderAdapter.LocalFolderAdapter(source.id, this.wallpaperlocation);
break;
case 5:
imageSourceAdapter = new UrlSourceAdapter.UrlSourceAdapter(source.id, this.wallpaperlocation);
break;
default:
imageSourceAdapter = new UnsplashAdapter.UnsplashAdapter(null, this.wallpaperlocation);
// TODO: log error and abort, raise exception?
Expand Down

0 comments on commit 52567bc

Please sign in to comment.