diff --git a/src/data/ui/BackgroundButton.ui b/src/data/ui/BackgroundButton.ui index d55d6d0..ec3d4e1 100644 --- a/src/data/ui/BackgroundButton.ui +++ b/src/data/ui/BackgroundButton.ui @@ -31,16 +31,5 @@ SPDX-License-Identifier: GPL-3.0-or-later Select your background image False - - - - image/jpeg - image/png - image/svg+xml - image/tiff - application/xml - - - diff --git a/src/preferences/BackgroundButton.js b/src/preferences/BackgroundButton.js index 5afde20..beb2d4e 100644 --- a/src/preferences/BackgroundButton.js +++ b/src/preferences/BackgroundButton.js @@ -42,6 +42,7 @@ var BackgroundButton = GObject.registerClass({ super(props); this.#setupSize(); this.#setupDropTarget(); + this.#setupFileChooserFilter(); } #setupSize() { @@ -59,13 +60,7 @@ var BackgroundButton = GObject.registerClass({ const dropTarget = Gtk.DropTarget.new(Gio.File.$gtype, Gdk.DragAction.COPY); dropTarget.connect('drop', (_target, file, _x, _y) => { const contentType = Gio.content_type_guess(file.get_basename(), null)[0]; - if ( - Gio.content_type_equals(contentType, 'image/jpeg') || - Gio.content_type_equals(contentType, 'image/png') || - Gio.content_type_equals(contentType, 'image/svg+xml') || - Gio.content_type_equals(contentType, 'image/tiff') || - Gio.content_type_equals(contentType, 'application/xml') - ) { + if (this.#isContentTypeSupported(contentType)) { this.uri = file.get_uri(); return true; } else { @@ -81,6 +76,24 @@ var BackgroundButton = GObject.registerClass({ this.add_controller(dropTarget); } + #setupFileChooserFilter() { + this._filechooser.filter = new Gtk.FileFilter(); + this._filechooser.filter.add_pixbuf_formats(); + this._filechooser.filter.add_mime_type('application/xml'); + } + + #getSupportedContentTypes() { + return GdkPixbuf.Pixbuf.get_formats().flatMap(format => format.get_mime_types()).concat('application/xml'); + } + + #isContentTypeSupported(contentType) { + for (const supportedContentType of this.#getSupportedContentTypes()) { + if (Gio.content_type_equals(contentType, supportedContentType)) + return true; + } + return false; + } + vfunc_mnemonic_activate() { this.openFileChooser(); } @@ -104,27 +117,26 @@ var BackgroundButton = GObject.registerClass({ if (!uri) return null; - let path; const file = Gio.File.new_for_uri(uri); const contentType = Gio.content_type_guess(file.get_basename(), null)[0]; - if ( - Gio.content_type_equals(contentType, 'image/jpeg') || - Gio.content_type_equals(contentType, 'image/png') || - Gio.content_type_equals(contentType, 'image/svg+xml') || - Gio.content_type_equals(contentType, 'image/tiff') - ) { - path = file.get_path(); - } else if (Gio.content_type_equals(contentType, 'application/xml')) { + + if (!this.#isContentTypeSupported(contentType)) + return null; + + let path; + if (Gio.content_type_equals(contentType, 'application/xml')) { const decoder = new TextDecoder('utf-8'); const contents = decoder.decode(file.load_contents(null)[1]); try { path = contents.match(/(.+)<\/file>/m)[1]; - } catch (_e) {} + } catch (e) { + console.error(e); + return null; + } + } else { + path = file.get_path(); } - if (!path) - return null; - const pixbuf = GdkPixbuf.Pixbuf.new_from_file(path); const scale = pixbuf.width / pixbuf.height > width / height ? height / pixbuf.height : width / pixbuf.width; const thumbPixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.GDK_COLORSPACE_RGB, pixbuf.has_alpha, pixbuf.bits_per_sample, width, height);