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

feat: Implement set_wallpaper_uri method in Wallpaper.Portal #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
106 changes: 103 additions & 3 deletions src/Wallpaper/Portal.vala
Original file line number Diff line number Diff line change
@@ -1,14 +1,114 @@
[DBus (name = "org.freedesktop.impl.portal.Wallpaper")]
public class Wallpaper.Portal : Object {
private DBusConnection connection;
private const string BACKGROUND_SCHEMA = "org.gnome.desktop.background";
private const string LOCKSCREEN_SCHEMA = "org.gnome.desktop.screensaver";

public Portal (DBusConnection connection) {
this.connection = connection;
}


public void show_preview_image(string uri, string parent_window) throws GLib.Error {

var window = new Gtk.Window();
window.title = "Image Preview";
window.set_default_size(400, 400);
var image = new Gtk.Image.from_file(uri);
image.set_size_request(400, 400);
var box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
SoumyaRanjanPatnaik marked this conversation as resolved.
Show resolved Hide resolved
box.append(image);
window.set_child(box);
window.show();

}


private void set_gsettings(string schema, string key, string value) throws Error {

var settings = new GLib.Settings(schema);
settings.set_string(key, value);
settings.apply();


}

public void set_background_wallpaper(string uri) throws Error {
set_gsettings(BACKGROUND_SCHEMA, "picture-uri", uri);
SoumyaRanjanPatnaik marked this conversation as resolved.
Show resolved Hide resolved
}

public void set_lockscreen_wallpaper(string uri) throws Error {
set_gsettings(LOCKSCREEN_SCHEMA, "picture-uri", uri);
SoumyaRanjanPatnaik marked this conversation as resolved.
Show resolved Hide resolved
}


[DBus (name = "SetWallpaperURI")]
public async uint set_wallpaper_u_r_i(GLib.ObjectPath handle, string app_id, string parent_window, string uri, GLib.HashTable<string, GLib.Variant> options) throws DBusError, IOError {
debug ("set_wallpaper_u_r_i");
return 0;
public async uint set_wallpaper_uri(GLib.ObjectPath handle, string app_id, string parent_window, string uri, GLib.HashTable<string, GLib.Variant> options, out uint response) throws DBusError, IOError {
debug("set_wallpaper_uri");


string get_uri="";

try {
get_uri = Uri.parse(uri, UriFlags.NONE).get_path();
} catch (GLib.UriError e) {
stderr.printf("Error parsing URI: %s\n", e.message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why swallow exception and continue? this seems like a critical error, shouldn't we re-throw or at least exit the function early?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}

bool show_preview = false;
string set_on = "";
if (options != null) {
GLib.Variant? showPreviewVariant = options.lookup("show-preview");
if (showPreviewVariant != null && showPreviewVariant.get_boolean()) {
show_preview = true;
}

GLib.Variant? setOnVariant = options.lookup("set-on");
if (setOnVariant != null) {
set_on = setOnVariant.get_string();
}
}

debug("show-preview: %s", show_preview ? "true" : "false");
debug("set-on: %s", set_on);

if (show_preview) {
try{
show_preview_image(get_uri,parent_window);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would probably want to wait for confirmation from the user before proceeding to set the wallpaper.

} catch (GLib.Error e) {
stderr.printf("Error showing preview: %s\n", e.message);
return response=1;
SoumyaRanjanPatnaik marked this conversation as resolved.
Show resolved Hide resolved
}
}

uint response_code = 0;

if (set_on == "background") {
try {
set_background_wallpaper(get_uri);
response_code = 0;
} catch (GLib.Error e) {
response_code = 1;
}
} else if (set_on == "lockscreen") {
try {
set_lockscreen_wallpaper(get_uri);
response_code = 0;
} catch (GLib.Error e) {
response_code = 1;
}
} else if (set_on == "both") {
try {
set_background_wallpaper(get_uri);
set_lockscreen_wallpaper(get_uri);
response_code = 0;
} catch (GLib.Error e) {
response_code = 1;
}
} else {
response_code = 2;
SoumyaRanjanPatnaik marked this conversation as resolved.
Show resolved Hide resolved
}

return response=response_code;
}
}