[polari] utils: Fall back to soup2



commit 2ec5d2848ac8371d331828d04b1215db6332118c
Author: Florian Müllner <fmuellner gnome org>
Date:   Tue Aug 3 13:35:44 2021 +0200

    utils: Fall back to soup2
    
    We cannot assume yet that Soup3 is available. Until that is the case,
    import Soup3 dynamically and fall back to Soup2 on failure.
    
    https://gitlab.gnome.org/GNOME/polari/-/merge_requests/207

 src/main.js  |  4 +++-
 src/utils.js | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)
---
diff --git a/src/main.js b/src/main.js
index fa8c569c..7cf26a3f 100755
--- a/src/main.js
+++ b/src/main.js
@@ -22,7 +22,6 @@ pkg.require({
     'Pango': '1.0',
     'PangoCairo': '1.0',
     'Secret': '1',
-    'Soup': '3.0',
     'TelepathyGLib': '0.12',
     'TelepathyLogger': '0.2',
 });
@@ -31,6 +30,9 @@ pkg.requireSymbol('GLib', '2.0', 'log_variant');
 pkg.requireSymbol('Gspell', '1', 'Entry');
 pkg.requireSymbol('Gtk', '3.0', 'ScrolledWindow.propagate_natural_width');
 
+if (!pkg.checkSymbol('Soup', '3.0'))
+    pkg.requireSymbol('Soup', '2.4');
+
 Log.init();
 
 import Application from './application.js';
diff --git a/src/utils.js b/src/utils.js
index ce1d4fd7..0796c55c 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -26,11 +26,41 @@ import Gio from 'gi://Gio';
 import GLib from 'gi://GLib';
 import Gtk from 'gi://Gtk';
 import Secret from 'gi://Secret';
-import Soup from 'gi://Soup?version=3.0';
 import Tp from 'gi://TelepathyGLib';
 
 import * as AppNotifications from './appNotifications.js';
 
+import gi from 'gi';
+let Soup;
+
+try {
+    Soup = gi.require('Soup', '3.0');
+} catch (e) {
+    Soup = gi.require('Soup', '2.4');
+
+    Soup.Message.new_from_encoded_form =
+        function (method, uri, form) {
+            const message = Soup.Message.new_from_uri(method, new Soup.URI(uri));
+            message.set_request(
+                Soup.FORM_MIME_TYPE_URLENCODED,
+                Soup.MemoryUse.COPY,
+                form);
+            return message;
+        };
+
+    Soup.Session.prototype.send_and_read_async =
+        function (message, prio, cancellable, callback) {
+            this.queue_message(message, () => callback(this, message));
+        };
+    Soup.Session.prototype.send_and_read_finish =
+        function (message) {
+            if (message.status_code !== Soup.KnownStatusCode.OK)
+                return null;
+
+            return message.response_body.flatten().get_as_bytes();
+        };
+}
+
 Gio._promisify(Secret, 'password_store', 'password_store_finish');
 Gio._promisify(Secret, 'password_lookup', 'password_lookup_finish');
 Gio._promisify(Secret, 'password_clear', 'password_clear_finish');


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]