[polari] Add openURL helper to Utils



commit 305fca7dbb6c95dd16a4a7d28ca5855b7856baff
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Tue Nov 3 01:05:08 2015 +0800

    Add openURL helper to Utils
    
    In order to bring the new application window to front
    and get indication from the shell that we are opening an
    application we need to use a GAppLaunchContext.
    
    And it would be nice to get some feedback if we got
    an error from opening the URL. For instance if no
    handler is present.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=757493

 src/chatView.js |    9 ++++-----
 src/utils.js    |   16 ++++++++++++++++
 2 files changed, 20 insertions(+), 5 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index 2422921..b3b027c 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -485,10 +485,9 @@ const ChatView = new Lang.Class({
         let menu = new Gtk.Menu();
 
         let item = new Gtk.MenuItem({ label: _("Open Link") });
-        item.connect('activate',
-            function() {
-                Gio.AppInfo.launch_default_for_uri(url, null);
-            });
+        item.connect('activate', function() {
+            Utils.openURL(url, Gtk.get_current_event_time());
+        });
         menu.append(item);
 
         item = new Gtk.MenuItem({ label: _("Copy Link Address") });
@@ -533,7 +532,7 @@ const ChatView = new Lang.Class({
                         this._showUrlContextMenu(url, button, event.get_time());
                     return Gdk.EVENT_STOP;
                 } else if (this._clickedUrl == url) {
-                    Gio.AppInfo.launch_default_for_uri(url, null);
+                    Utils.openURL(url, event.get_time());
                     return Gdk.EVENT_STOP;
                 }
                 break;
diff --git a/src/utils.js b/src/utils.js
index 22de6fd..9aa1d7e 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -20,11 +20,14 @@
  *
  */
 
+const Gdk = imports.gi.Gdk;
+const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
 const Gtk = imports.gi.Gtk;
 const Soup = imports.gi.Soup;
 const Tp = imports.gi.TelepathyGLib;
 
+const AppNotifications = imports.appNotifications;
 const Signals = imports.signals;
 
 const GPASTE_BASEURL = 'https://paste.gnome.org/'
@@ -102,6 +105,19 @@ function findUrls(str) {
     return res;
 }
 
+function openURL(url, timestamp) {
+    let ctx = Gdk.Display.get_default().get_app_launch_context();
+    ctx.set_timestamp(timestamp);
+    try {
+        Gio.AppInfo.launch_default_for_uri(url, ctx);
+    } catch(e) {
+        let n = new AppNotifications.SimpleOutput(_("Failed to open link"));
+        let app = Gio.Application.get_default();
+        app.notificationQueue.addNotification(n);
+        debug("failed to open %s: %s".format(url, e.message));
+    }
+}
+
 function gpaste(text, title, callback) {
     let params = {
         title: title,


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