[polari] urlPreview: Explicitly handle network failure
- From: Florian Müllner <fmuellner src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [polari] urlPreview: Explicitly handle network failure
- Date: Wed, 5 Aug 2020 13:47:09 +0000 (UTC)
commit e9dd08c5a838ff329747bb7a5f0c3316df00e003
Author: Florian Müllner <fmuellner gnome org>
Date: Tue Jul 28 02:26:13 2020 +0200
urlPreview: Explicitly handle network failure
We cannot generate a thumbnail without network access, so don't even
try. This allows us to distinguish this failure from failed thumbnails,
and retry when the network becomes available instead of waiting for the
next application start.
https://gitlab.gnome.org/GNOME/polari/-/issues/149
src/urlPreview.js | 35 ++++++++++++++++++++++++++++++++---
1 file changed, 32 insertions(+), 3 deletions(-)
---
diff --git a/src/urlPreview.js b/src/urlPreview.js
index 5ff426f6..3e8e4de1 100644
--- a/src/urlPreview.js
+++ b/src/urlPreview.js
@@ -19,6 +19,11 @@ class Thumbnailer {
GLib.mkdir_with_parents(this._thumbnailsDir, 0o755);
}
+ get _hasNetwork() {
+ const monitor = Gio.NetworkMonitor.get_default();
+ return monitor.state_valid && monitor.network_available;
+ }
+
getThumbnail(uri) {
return new Promise((resolve, reject) => {
const filename = this._generateFilename(uri);
@@ -31,6 +36,8 @@ class Thumbnailer {
async _processData(data) {
if (await this._thumbExists(data))
this._generationDone(data);
+ else if (!this._hasNetwork)
+ this._generationUnavailable(data);
else if (!this._subProc)
this._generateThumbnail(data);
else
@@ -48,6 +55,13 @@ class Thumbnailer {
this._processData(nextData);
}
+ _generationUnavailable(data) {
+ this._generationDone(data, new Gio.IOErrorEnum({
+ code: Gio.IOErrorEnum.NETWORK_UNREACHABLE,
+ message: 'Network unreachable',
+ }));
+ }
+
async _generateThumbnail(data) {
let { filename, uri } = data;
this._subProc = Gio.Subprocess.new(
@@ -115,10 +129,22 @@ var URLPreview = GObject.registerClass({
});
this._label.get_style_context().add_class(Gtk.STYLE_CLASS_DIM_LABEL);
this.add(this._label);
+
+ this._networkMonitor = Gio.NetworkMonitor.get_default();
+ this._networkChangedId = this._networkMonitor.connect('network-changed',
+ this._maybeLoadImage.bind(this));
+
+ this.connect('destroy', this._onDestroy.bind(this));
+ }
+
+ _onDestroy() {
+ if (this._networkChangedId)
+ this._networkMonitor.disconnect(this._networkChangedId);
+ this._networkChangedId = 0;
}
async _maybeLoadImage() {
- if (this._imageLoaded)
+ if (this._imageLoaded || !this.get_mapped())
return;
this._imageLoaded = true;
@@ -132,7 +158,10 @@ var URLPreview = GObject.registerClass({
const filename = await thumbnailer.getThumbnail(this.uri);
this._image.set_from_file(filename);
} catch (e) {
- log(`Thumbnail generation for ${this.uri} failed: ${e}`);
+ if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NETWORK_UNREACHABLE))
+ this._imageLoaded = false;
+ else
+ log(`Thumbnail generation for ${this.uri} failed: ${e}`);
this._image.set({
icon_name: 'image-x-generic-symbolic',
pixel_size: 64,
@@ -150,7 +179,7 @@ var URLPreview = GObject.registerClass({
}
vfunc_map() {
- this._maybeLoadImage();
super.vfunc_map();
+ this._maybeLoadImage();
}
});
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]