Browse code

feat: Try to launch default browser if bridge is unavailable

This feature allows a user to open a session even if the browser is not
running, assuming a bridge is installed on the default browser.

Payson Wallach authored on 13/03/2021 21:32:30
Showing 3 changed files

... ...
@@ -4,7 +4,7 @@ project(
4 4
   'c',
5 5
   license: 'GPL-3.0',
6 6
   meson_version: '>=0.55.0',
7
-  version: '0.1.0'
7
+  version: '0.2.0'
8 8
 )
9 9
 
10 10
 vala = meson.get_compiler('vala')
... ...
@@ -15,6 +15,8 @@ glib_dep = dependency('gtk+-3.0')
15 15
 
16 16
 conf = configuration_data()
17 17
 conf.set_quoted('APP_ID', meson.project_name())
18
+conf.set_quoted('BRIDGE_DBUS_SERVICE_NAME', 'com.paysonwallach.amber.bridge')
19
+conf.set_quoted('BRIDGE_DBUS_OBJECT_PATH', '/com/paysonwallach/amber/bridge')
18 20
 
19 21
 config_h = configure_file(
20 22
   output: 'config.h',
... ...
@@ -17,13 +17,13 @@
17 17
 
18 18
 namespace Amber {
19 19
     [DBus (name = "com.paysonwallach.amber.bridge")]
20
-    public interface HostConnectorBusIFace : Object {
20
+    public interface BrowserProxyBusIFace : Object {
21 21
         public abstract bool open (string[] urls) throws DBusError, IOError;
22 22
 
23 23
     }
24 24
 
25 25
     public class Helper : Application {
26
-        private HostConnectorBusIFace? host_connector;
26
+        private uint watch_id = 0U;
27 27
 
28 28
         public Helper () {
29 29
             Object (
... ...
@@ -31,24 +31,63 @@ namespace Amber {
31 31
                 flags: ApplicationFlags.HANDLES_OPEN);
32 32
         }
33 33
 
34
-        public override void open (File[] files, string hint) {
35
-            var i = 0;
36
-            var uris = new string[files.length];
37
-            foreach (File file in files) {
38
-                uris[i++] = file.get_uri ();
39
-            }
34
+        private void request_proxy_open (string[] uris) throws DBusError, IOError {
35
+            BrowserProxyBusIFace? browser_proxy = Bus.get_proxy_sync (
36
+                BusType.SESSION,
37
+                Config.BRIDGE_DBUS_SERVICE_NAME,
38
+                Config.BRIDGE_DBUS_OBJECT_PATH);
39
+
40
+            browser_proxy.open (uris);
41
+            release ();
42
+        }
43
+
44
+        private void request_browser_launch (string[] uris) {
45
+            var app_info = AppInfo.get_default_for_type ("text/html", false);
46
+            if (app_info == null)
47
+                return;
40 48
 
41 49
             try {
42
-                host_connector = Bus.get_proxy_sync (
50
+                app_info.launch_uris (null, null);
51
+                watch_id = Bus.watch_name (
43 52
                     BusType.SESSION,
44
-                    "com.paysonwallach.amber.bridge",
45
-                    "/com/paysonwallach/amber/bridge");
46
-                host_connector.open (uris);
53
+                    Config.BRIDGE_DBUS_SERVICE_NAME, BusNameWatcherFlags.NONE,
54
+                    () => on_name_acquired (uris));
55
+            } catch (Error err) {
56
+                warning (@"unable to get default type: $(err.message)");
57
+            }
58
+        }
59
+
60
+        private void on_name_acquired (string[] uris) {
61
+            try {
62
+                request_proxy_open (uris);
47 63
             } catch (DBusError err) {
48 64
                 warning (@"DBusError: $(err.message)");
49 65
             } catch (IOError err) {
50 66
                 warning (@"IOError: $(err.message)");
51 67
             }
68
+
69
+            if (watch_id != 0U)
70
+                Bus.unwatch_name (watch_id);
71
+        }
72
+
73
+        public override void open (File[] files, string hint) {
74
+            hold ();
75
+
76
+            var uris = new string[files.length];
77
+            for (var i = 0 ; i < files.length ; i++)
78
+                uris[i] = files[i].get_uri ();
79
+
80
+            try {
81
+                request_proxy_open (uris);
82
+            } catch (DBusError err) {
83
+                if (err.code == DBusError.SERVICE_UNKNOWN) {
84
+                    request_browser_launch (uris);
85
+                } else {
86
+                    warning (@"DBusError: $(err.message)");
87
+                }
88
+            } catch (IOError err) {
89
+                warning (@"IOError: $(err.message)");
90
+            }
52 91
         }
53 92
 
54 93
     }
... ...
@@ -1,4 +1,6 @@
1 1
 [CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "config.h")]
2 2
 namespace Config {
3 3
     public const string APP_ID;
4
+    public const string BRIDGE_DBUS_SERVICE_NAME;
5
+    public const string BRIDGE_DBUS_OBJECT_PATH;
4 6
 }