Newer
Older
#include "../core/config.h"
#include "../core/network.h"
#include "../utility/file.h"
#include "../utility/json.h"
#include "xdgurl.h"
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)
_destinations = _loadDestinations();
_archiveTypes = _loadArchiveTypes();
metadata["scheme"] = QString("xdg");
metadata["command"] = QString("download");
metadata["url"] = QString("");
metadata["type"] = QString("downloads");
metadata["filename"] = QString("");
}
if (query.hasQueryItem("url") && !query.queryItemValue("url").isEmpty()) {
metadata["url"] = query.queryItemValue("url", QUrl::FullyDecoded);
}
if (query.hasQueryItem("type") && !query.queryItemValue("type").isEmpty()) {
metadata["type"] = query.queryItemValue("type", QUrl::FullyDecoded);
}
if (query.hasQueryItem("filename") && !query.queryItemValue("filename").isEmpty()) {
metadata["filename"] = QUrl(query.queryItemValue("filename", QUrl::FullyDecoded)).fileName();
if (!metadata["url"].toString().isEmpty() && metadata["filename"].toString().isEmpty()) {
metadata["filename"] = QUrl(metadata["url"].toString()).fileName();
QString XdgUrl::_convertPathString(const QString &path)
{
QString newPath = path;
if (newPath.contains("$HOME")) {
newPath.replace("$HOME", Utility::File::homePath());
}
else if (newPath.contains("$XDG_DATA")) {
newPath.replace("$XDG_DATA", Utility::File::xdgDataHomePath());
}
else if (newPath.contains("$KDE_DATA")) {
newPath.replace("$KDE_DATA", Utility::File::kdeDataHomePath());
}
{
QJsonObject destinations;
QJsonObject appConfigDestinations = _appConfig->get("destinations");
QJsonObject appConfigDestinationsAlias = _appConfig->get("destinations_alias");
QJsonObject userConfigDestinations = _userConfig->get("destinations");
QJsonObject userConfigDestinationsAlias = _userConfig->get("destinations_alias");
foreach (const QString key, appConfigDestinations.keys()) {
destinations[key] = _convertPathString(appConfigDestinations[key].toString());
}
foreach (const QString key, appConfigDestinationsAlias.keys()) {
QString value = appConfigDestinationsAlias[key].toString();
if (destinations.contains(value)) {
destinations[key] = destinations.value(value);
}
}
if (!userConfigDestinations.isEmpty()) {
foreach (const QString key, userConfigDestinations.keys()) {
destinations[key] = _convertPathString(userConfigDestinations[key].toString());
}
}
if (!userConfigDestinationsAlias.isEmpty()) {
foreach (const QString key, userConfigDestinationsAlias.keys()) {
QString value = userConfigDestinationsAlias[key].toString();
if (destinations.contains(value)) {
destinations[key] = destinations.value(value);
}
}
}
return destinations;
}
QJsonObject archiveTypes;
QJsonObject appConfigArchiveTypes = _appConfig->get("archive_types");
QJsonObject userConfigArchiveTypes = _userConfig->get("archive_types");
archiveTypes = appConfigArchiveTypes;
if (!userConfigArchiveTypes.isEmpty()) {
foreach (const QString key, userConfigArchiveTypes.keys()) {
archiveTypes[key] = userConfigArchiveTypes.value(key);
}
}
return archiveTypes;
bool XdgUrl::_installPlasmapkg(const QString &path, const QString &type)
{
QStringList arguments;
arguments << "-t" << type << "-i" << path;
if (process.waitForFinished()) {
return true;
}
}
bool XdgUrl::_uncompressArchive(const QString &path, const QString &targetDir)
{
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
QMimeDatabase mimeDb;
QString mimeType = mimeDb.mimeTypeForFile(path).name();
QString archiveType;
QProcess process;
QString program;
QStringList arguments;
if (_archiveTypes.contains(mimeType)) {
archiveType = _archiveTypes[mimeType].toString();
if (archiveType == "tar") {
program = "tar";
arguments << "-xf" << path << "-C" << targetDir;
}
else if (archiveType == "zip") {
program = "unzip";
arguments << "-o" << path << "-d" << targetDir;
}
else if (archiveType == "7z") {
program = "7z";
arguments << "x" << path << "-o" + targetDir; // No space between -o and directory
}
else if (archiveType == "rar") {
program = "unrar";
arguments << "e" << path << targetDir;
}
process.start(program, arguments);
if (process.waitForFinished()) {
process.waitForReadyRead();
return true;
}
}
return false;
/**
* Private slots
*/
void XdgUrl::_saveDownloadedFile(QNetworkReply *reply)
QString XdgUrl::getXdgUrl()
{
return _xdgUrl;
}
QString XdgUrl::getMetadata()
{
return Utility::Json::convertObjToStr(_metadata);
}
bool XdgUrl::isValid()
{
bool isValid = true;
if (_metadata["scheme"].toString() != "xdg" && _metadata["scheme"].toString() != "xdgs") {
if (_metadata["command"].toString() != "download" && _metadata["command"].toString() != "install") {
isValid = false;
}
return isValid;
}
/**
* xdgs scheme is a reserved name, so the process of xdgs
* is the same process of the xdg scheme currently.
*/
if (isValid()) {
if (_metadata["command"].toString() == "download") {
connect(_asyncNetwork, &Core::Network::finished, this, &XdgUrl::_saveDownloadedFile);
connect(_asyncNetwork, &Core::Network::finished, this, &XdgUrl::_installDownloadedFile);