Newer
Older
#include "../core/config.h"
#include "../core/network.h"
#include "../utility/file.h"
#include "../utility/json.h"
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)
connect(_asyncNetwork, &Core::Network::finished, this, &XdgUrl::_downloaded);
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;
}
void XdgUrl::_saveDownloadedFile(QNetworkReply *reply)
if (!temporaryFile.open() || temporaryFile.write(reply->readAll()) == -1) {
result["error"] = QString("save_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
QMimeDatabase mimeDb;
QString mimeType = mimeDb.mimeTypeForFile(temporaryFile.fileName()).name();
if (mimeType == "text/html" || mimeType == "application/xhtml+xml") {
result["error"] = QString("filetype_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
QString type = _metadata["type"].toString();
QString destination = _destinations[type].toString();
QString path = destination + "/" + _metadata["filename"].toString();
Utility::File::makeDir(destination);
Utility::File::remove(path); // Remove previous downloaded file
if (!temporaryFile.copy(path)) {
result["error"] = QString("save_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
result["success"] = QString("download_success");
result["destination"] = destination;
emit finished(Utility::Json::convertObjToStr(result));
void XdgUrl::_installDownloadedFile(QNetworkReply *reply)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
QJsonObject result;
QTemporaryFile temporaryFile;
if (!temporaryFile.open() || temporaryFile.write(reply->readAll()) == -1) {
result["error"] = QString("save_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
QMimeDatabase mimeDb;
QString mimeType = mimeDb.mimeTypeForFile(temporaryFile.fileName()).name();
if (mimeType == "text/html" || mimeType == "application/xhtml+xml") {
result["error"] = QString("filetype_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
QString type = _metadata["type"].toString();
QString destination = _destinations[type].toString();
QString path = destination + "/" + _metadata["filename"].toString();
Utility::File::makeDir(destination);
Utility::File::remove(path); // Remove previous downloaded file
if ((type == "plasma_plasmoids" || type == "plasma4_plasmoids" || type == "plasma5_plasmoids")
&& Utility::Package::installPlasmapkg(temporaryFile.fileName(), "plasmoid")) {
qInfo("The plasmoid has been installed");
}
else if ((type == "plasma_look_and_feel" || type == "plasma5_look_and_feel")
&& Utility::Package::installPlasmapkg(temporaryFile.fileName(), "lookandfeel")) {
qInfo("The plasma look and feel has been installed");
}
else if ((type == "plasma_desktopthemes" || type == "plasma5_desktopthemes")
&& Utility::Package::installPlasmapkg(temporaryFile.fileName(), "theme")) {
qInfo("The plasma desktop theme has been installed");
}
else if (type == "kwin_effects"
&& Utility::Package::installPlasmapkg(temporaryFile.fileName(), "kwineffect")) {
qInfo("The KWin effect has been installed");
}
else if (type == "kwin_scripts"
&& Utility::Package::installPlasmapkg(temporaryFile.fileName(), "kwinscript")) {
qInfo("The KWin script has been installed");
}
else if (type == "kwin_tabbox"
&& Utility::Package::installPlasmapkg(temporaryFile.fileName(), "windowswitcher")) {
qInfo("The KWin window switcher has been installed");
}
else if (Utility::Package::uncompressArchive(temporaryFile.fileName(), destination)) {
qInfo("The archive file has been uncompressed into " + destination);
}
else if (temporaryFile.copy(path)) {
qInfo("Saved the file as " + path);
}
else {
result["error"] = QString("install_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
result["success"] = QString("install_success");
result["destination"] = destination;
result["path"] = path;
emit finished(Utility::Json::convertObjToStr(result));
if (reply->error() != QNetworkReply::NoError) {
result["error"] = QString("network_error");
emit finished(Utility::Json::convertObjToStr(result));
return;
}
// If the network reply has a refresh header, retry download
if (reply->hasRawHeader("Refresh")) {
QString refreshUrl = QString(reply->rawHeader("Refresh")).split("url=").last();
if (refreshUrl.startsWith("/")) {
}
_asyncNetwork->get(QUrl(refreshUrl));
return;
}
if (_metadata["command"].toString() == "download") {
}
else if (_metadata["command"].toString() == "install") {
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()) {