[polari/wip/bastianilso/irc-url-handling: 70/71] app: open IRC links



commit ca4a9c2687aed004d0922e26d44f952af8e38366
Author: Bastian Ilsø <hougaard junior gmail com>
Date:   Thu Feb 11 17:18:16 2016 +0100

    app: open IRC links
    
    Register Polari as handler of IRC URLS and add
    support for opening IRC URLs pointing to rooms on
    the networks the user have added.

 data/org.gnome.Polari.desktop.in |    1 +
 src/application.js               |   60 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 1 deletions(-)
---
diff --git a/data/org.gnome.Polari.desktop.in b/data/org.gnome.Polari.desktop.in
index b505911..3373cb6 100644
--- a/data/org.gnome.Polari.desktop.in
+++ b/data/org.gnome.Polari.desktop.in
@@ -4,6 +4,7 @@ _Comment=An Internet Relay Chat Client for GNOME
 Exec=gapplication launch org.gnome.Polari
 Icon=polari
 Type=Application
+MimeType=x-scheme-handler/irc;
 StartupNotify=true
 DBusActivatable=true
 X-GNOME-UsesNotifications=true
diff --git a/src/application.js b/src/application.js
index 13fa603..d991d48 100644
--- a/src/application.js
+++ b/src/application.js
@@ -26,7 +26,8 @@ const Application = new Lang.Class({
     Signals: { 'prepare-shutdown': {} },
 
     _init: function() {
-        this.parent({ application_id: 'org.gnome.Polari' });
+        this.parent({ application_id: 'org.gnome.Polari',
+                      flags: Gio.ApplicationFlags.HANDLES_OPEN });
 
         GLib.set_application_name('Polari');
         this._window = null;
@@ -146,6 +147,63 @@ const Application = new Lang.Class({
         this._window.present();
     },
 
+    _openURIs: function(uris, time) {
+        let map = {};
+
+        this._accountsMonitor.dupAccounts().forEach(function(a) {
+            if (!a.enabled)
+                return;
+
+            let params = a.dup_parameters_vardict().deep_unpack();
+            map[a.get_object_path()] = params.server.deep_unpack();
+        });
+
+        let action = this.lookup_action('join-room');
+        uris.forEach(Lang.bind(this, function(uri) {
+            let uriRegEx = /^(irc?:\/\/)([\da-z\.-]+):?(\d+)?\/(?:%23)?([\w \. \+-]*)/;
+            let server, port, room;
+            try {
+                [,, server, port, room] = uri.match(uriRegEx);
+                if (!room) throw new Error('No Room');
+            } catch(e) {
+                let label = _("Could not open the link.");
+                let n = new AppNotifications.CloseNotification(label,
+                                                               'dialog-error-symbolic');
+                this.notificationQueue.addNotification(n);
+                log("Invalid URI");
+                return;
+            }
+
+            let matches = Object.keys(map).filter(function(a) {
+                return map[a] == server;
+            });
+
+            if (!matches.length) {
+                log("No matching account");
+                return;
+            }
+
+            action.activate(new GLib.Variant('(ssu)',
+                            [matches[0], '#' + room, time]));
+        }));
+    },
+
+    vfunc_open: function(files) {
+        this.activate();
+
+        let time = Utils.getTpEventTime();
+        let uris = files.map(function(f) { return f.get_uri(); });
+
+        let quark = Tp.AccountManager.get_feature_quark_core();
+        if (this._accountsMonitor.accountManager.is_prepared(quark))
+            this._openURIs(uris, time);
+        else
+            this._accountsMonitor.connect('account-manager-prepared', Lang.bind(this,
+                function(mon) {
+                    this._openURIs(uris, time);
+                }));
+    },
+
     _updateAccountAction: function(action) {
         action.enabled = this._accountsMonitor.dupAccounts().filter(
             function(a) {


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