From 7d6e78c1e055ded23c771fe69a9ae22fd0df4fe9 Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Sat, 24 Sep 2016 14:36:30 +0900
Subject: [PATCH] Parse XDG-URL

---
 src/handlers/xdgurl.cpp | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/src/handlers/xdgurl.cpp b/src/handlers/xdgurl.cpp
index 333b103..11519f9 100644
--- a/src/handlers/xdgurl.cpp
+++ b/src/handlers/xdgurl.cpp
@@ -1,4 +1,6 @@
 #include <QDebug>
+#include <QUrl>
+#include <QUrlQuery>
 
 #include "../core/config.h"
 #include "../core/network.h"
@@ -12,10 +14,46 @@ namespace Handlers {
 XdgUrl::XdgUrl(const QString &xdgUrl, Core::Config *appConfig, Core::Config *userConfig, Core::Network *asyncNetwork, QObject *parent) :
     QObject(parent), _xdgUrl(xdgUrl), _appConfig(appConfig), _userConfig(userConfig), _asyncNetwork(asyncNetwork)
 {
+    _meta = _parse();
 }
 
 QJsonObject XdgUrl::_parse()
 {
+    QUrl url(_xdgUrl);
+    QUrlQuery query(url);
+    QJsonObject meta;
+
+    meta["scheme"] = QString("xdg");
+    meta["command"] = QString("download");
+    meta["url"] = QString("");
+    meta["type"] = QString("downloads");
+    meta["filename"] = QString("");
+
+    if (!url.scheme().isEmpty()) {
+        meta["scheme"] = url.scheme();
+    }
+
+    if (!url.host().isEmpty()) {
+        meta["command"] = url.host();
+    }
+
+    if (query.hasQueryItem("url") && !query.queryItemValue("url").isEmpty()) {
+        meta["url"] = query.queryItemValue("url", QUrl::FullyDecoded);
+    }
+
+    if (query.hasQueryItem("type") && !query.queryItemValue("type").isEmpty()) {
+        meta["type"] = query.queryItemValue("type", QUrl::FullyDecoded);
+    }
+
+    if (query.hasQueryItem("filename") && !query.queryItemValue("filename").isEmpty()) {
+        meta["filename"] = query.queryItemValue("filename", QUrl::FullyDecoded).split("?").at(0);
+    }
+
+    if (!meta["url"].toString().isEmpty() && meta["filename"].toString().isEmpty()) {
+        meta["filename"] = QUrl(meta["url"].toString()).fileName();
+    }
+
+    return meta;
 }
 
 bool XdgUrl::_installPlasmapkg(const QString &path, const QString &type)
-- 
GitLab