Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple preference window from wallpaperController (Requires #141) #144

Merged
merged 3 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,51 @@ const ExtensionUtils = imports.misc.extensionUtils;

const Self = ExtensionUtils.getCurrentExtension();
const SourceRow = Self.imports.ui.source_row;

const Settings = Self.imports.settings;
const WallpaperController = Self.imports.wallpaperController;
const LoggerModule = Self.imports.logger;

const RWG_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.space.iflow.randomwallpaper';
const RWG_SETTINGS_SCHEMA_BACKEND_CONNECTION = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.backend-connection';

const LoggerModule = Self.imports.logger;

function init(metaData) {
//Convenience.initTranslations();
}

// https://gjs.guide/extensions/overview/anatomy.html#prefs-js
// The code in prefs.js will be executed in a separate Gtk process
// Here you will not have access to code running in GNOME Shell, but fatal errors or mistakes will be contained within that process.
// In this process you will be using the Gtk toolkit, not Clutter.

// https://gjs.guide/extensions/development/preferences.html#preferences-window
// Gnome 42+
function fillPreferencesWindow(window) {
new RandomWallpaperSettings(window);
}

// 40 < Gnome < 42
// function buildPrefsWidget() {
// let window = new Adw.PreferencesWindow();
// new RandomWallpaperSettings(window);
// return window;
// }

/* UI Setup */
var RandomWallpaperSettings = class {
_backendConnection = null;

constructor(window) {
this.logger = new LoggerModule.Logger('RWG3', 'RandomWallpaper.Settings');

this._wallpaperController = null;
this._sources = [];
this.available_rows = {};

this._settings = ExtensionUtils.getSettings(RWG_SETTINGS_SCHEMA);
this._backendConnection = new Settings.Settings(RWG_SETTINGS_SCHEMA_BACKEND_CONNECTION);
this._backendConnection.set('pause-timer', 'boolean', true);

this._builder = new Gtk.Builder();
//this._builder.set_translation_domain(Self.metadata['gettext-domain']);
this._builder.add_from_file(Self.path + '/ui/page_general.ui');
Expand Down Expand Up @@ -70,11 +90,11 @@ var RandomWallpaperSettings = class {
'active',
Gio.SettingsBindFlags.DEFAULT);

this._wallpaperController = new WallpaperController.WallpaperController(true);
this._bindButtons();

window.connect('close-request', () => {
this._saveSources();
this._backendConnection.set('pause-timer', 'boolean', false);
});

window.add(this._builder.get_object('page_general'));
Expand All @@ -96,25 +116,27 @@ var RandomWallpaperSettings = class {
newWallpaperButton.get_child().set_label("Loading ...");
newWallpaperButton.set_sensitive(false);

this._wallpaperController.update();
this._wallpaperController.fetchNewWallpaper(() => {
this._wallpaperController.update();
newWallpaperButton.get_child().set_label(origNewWallpaperText);
newWallpaperButton.set_sensitive(true);
// The backend sets this back to false after fetching the image - listen for that event.
let handler = this._backendConnection.observe('request-new-wallpaper', () => {
if (!this._backendConnection.get('request-new-wallpaper', 'boolean')) {
newWallpaperButton.get_child().set_label(origNewWallpaperText);
newWallpaperButton.set_sensitive(true);
this._backendConnection.disconnect(handler);
}
});

this._backendConnection.set('request-new-wallpaper', 'boolean', true);
});

this._builder.get_object('clear_history').connect('clicked', () => {
this._wallpaperController.update();
this._wallpaperController.deleteHistory();
this._backendConnection.set('clear-history', 'boolean', true);
});

this._builder.get_object('open_wallpaper_folder').connect('clicked', () => {
let uri = GLib.filename_to_uri(this._wallpaperController.wallpaperlocation, "");
Gio.AppInfo.launch_default_for_uri(uri, Gio.AppLaunchContext.new());
this._backendConnection.set('open-folder', 'boolean', true);
});

this._builder.get_object('button_new_source').connect('clicked', button => {
this._builder.get_object('button_new_source').connect('clicked', () => {
let source_row = new SourceRow.SourceRow();
this.available_rows[source_row.id] = source_row;
this._builder.get_object('sources_list').add(source_row);
Expand All @@ -130,17 +152,17 @@ var RandomWallpaperSettings = class {
}
});

source_row.connect('notify::enable-expansion', (row) => {
source_row.connect('notify::enable-expansion', () => {
this._saveSources();
});

source_row.button_delete.connect('clicked', button => {
source_row.button_delete.connect('clicked', () => {
this._builder.get_object('sources_list').remove(source_row);
delete this.available_rows[source_row.id];
this._saveSources();
});

source_row.combo.connect('notify::selected', comboRow => {
source_row.combo.connect('notify::selected', () => {
this._saveSources();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@

</schema>

<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper/backend-connection/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper.backend-connection'>

<key type='b' name='clear-history'>
<default>false</default>
<summary>Clear history request</summary>
<description>Request clearing the history.</description>
</key>

<key type='b' name='open-folder'>
<default>false</default>
<summary>Open folder request</summary>
<description>Request opening the folder.</description>
</key>

<key type='b' name='pause-timer'>
<default>false</default>
<summary>Pause the timer</summary>
<description>Pause the background timer if running.</description>
</key>

<key type='b' name='request-new-wallpaper'>
<default>false</default>
<summary>Request a new wallpaper</summary>
<description>Manually request a new wallpaper.</description>
</key>

</schema>

<!-- <enum id='org.gnome.shell.extensions.space.iflow.randomwallpaper.unsplash.constraints'>
<value value='0' nick='unconstrained'/>
<value value='1' nick='user'/>
Expand Down
49 changes: 49 additions & 0 deletions [email protected]/sourceAdapter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Gio = imports.gi.Gio;

const Self = imports.misc.extensionUtils.getCurrentExtension();

const RWG_SETTINGS_SCHEMA_UNSPLASH = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.sources.unsplash';
Expand Down Expand Up @@ -46,6 +48,53 @@ var BaseAdapter = class {
return base;
}

/**
* copy file from uri to local wallpaper directory and calls the given callback with the name and the full filepath
* of the written file as parameter.
* @param uri
* @param callback(name, path, error)
*/
fetchFile(uri, callback) {
//extract the name from the url and
let date = new Date();
let name = date.getTime() + '_' + this.fileName(uri); // timestamp ensures uniqueness

let bowl = new SoupBowl.Bowl();

let file = Gio.file_new_for_path(this.wallpaperlocation + String(name));
let fstream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);

// start the download
let request = bowl.Soup.Message.new('GET', uri);

bowl.send_and_receive(request, (response_data_bytes) => {
if (!response_data_bytes) {
fstream.close(null);

if (callback) {
callback(null, null, 'Not a valid response');
}

return;
}

try {
fstream.write(response_data_bytes, null);

fstream.close(null);

// call callback with the name and the full filepath of the written file as parameter
if (callback) {
callback(name, file.get_path());
}
} catch (e) {
if (callback) {
callback(null, null, e);
}
}
});
}

_isURIEncoded(uri) {
uri = uri || '';

Expand Down
Loading