From b89d90eb8016dbab568f466420e2da14e600cd19 Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Wed, 22 Feb 2023 11:45:26 +0100 Subject: [PATCH] screenshot: Use GLib.BookmarkFile to save recent screenshots Avoid the GTK dependency, and use the common GLib API to store screenshots in recent files. While at it, give it a better exec hint than the implicit "gnome-shell %u" GTK added for us, nobody seems to pay attention to that, but if they ever did it's better to provide sensible information. Part-of: --- js/ui/screenshot.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/js/ui/screenshot.js b/js/ui/screenshot.js index b2ea6fad2..bf54eb069 100644 --- a/js/ui/screenshot.js +++ b/js/ui/screenshot.js @@ -1,7 +1,7 @@ // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- /* exported ScreenshotService, ScreenshotUI, showScreenshotUI, captureScreenshot */ -const { Clutter, Cogl, Gio, GObject, GLib, Graphene, Gtk, Meta, Shell, St } = imports.gi; +const {Clutter, Cogl, Gio, GObject, GLib, Graphene, Meta, Shell, St} = imports.gi; const GrabHelper = imports.ui.grabHelper; const Layout = imports.ui.layout; @@ -2060,6 +2060,33 @@ function _storeScreenshot(bytes, pixbuf) { yield `-${i}`; } + /** + * Adds a record of a screenshot file in the recently used files list. + * + * @param {Gio.File} screenshotFile - The screenshot file. + */ + function saveRecentFile(screenshotFile) { + const recentFile = + GLib.build_filenamev([GLib.get_user_data_dir(), 'recently-used.xbel']); + const uri = screenshotFile.get_uri(); + const bookmarks = new GLib.BookmarksFile(); + try { + bookmarks.load_from_file(recentFile); + } catch (e) { + if (!e.matches(GLib.BookmarkFileError.FILE_NOT_FOUND)) { + log(`Could not open recent file ${uri}: ${e.message}`); + return; + } + } + + try { + bookmarks.add_application(uri, GLib.get_prgname(), 'gio open %u'); + bookmarks.to_file(recentFile); + } catch (e) { + log(`Could not save recent file ${uri}: ${e.message}`); + } + } + const lockdownSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.lockdown' }); const disableSaveToDisk = @@ -2102,7 +2129,7 @@ function _storeScreenshot(bytes, pixbuf) { } // Add it to recent files. - Gtk.RecentManager.get_default().add_item(file.get_uri()); + saveRecentFile(file); } // Create a St.ImageContent icon for the notification. We want