Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • akiraohgaki/ocs-url
  • ab3875o/ocs-url
  • dembego3/ocs-url
  • arakun/ocs-url
  • longviauroy/ocs-url
  • rolfen/ocs-url
  • hemm/ocs-url
  • simonsvw0000/ocs-url
  • dfn2/ocs-url
  • rws77/ocs-url
  • bitwalk/ocs-url
  • visdom/ocs-url
  • ricatfarker/ocs-url
  • mussah/ocs-url
  • tigefa/ocs-url
  • cobalt2727/ocs-url
  • ammark226/ocs-url
  • violethaze74/ocs-url
  • armedssault/ocs-url
  • billflick/ocs-url
  • kimblejeremy/ocs-url
  • yuvrajsm/ocs-url
  • wawmart/ocs-url
  • jhefry/ocs-url
  • robcdntruckin/ocs-url
  • bigmake2266/ocs-url
  • kamil-chbeir/ocs-url
  • jocker73/ocs-url
  • laboties/ocs-url
  • smekke61279-522014/ocs-url
  • coolduck/ocs-url
  • zulfikar-lahiya/ocs-url
  • faz-83/ocs-url
  • dado105/ocs-url
34 results
Show changes
/**
* qtlib
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0
* @link https://github.com/akiraohgaki/qtlib
*/
#include "qtlib_networkresource.h"
#include <QEventLoop>
#include "qtlib_file.h"
namespace qtlib {
NetworkResource::NetworkResource(const QString &id, const QUrl &url, bool async, QObject *parent)
: QObject(parent), id_(id), url_(url), async_(async)
{
setManager(new QNetworkAccessManager(this));
}
NetworkResource::~NetworkResource()
{
manager()->deleteLater();
}
NetworkResource::NetworkResource(const NetworkResource &other, QObject *parent)
: QObject(parent)
{
setId(other.id());
setUrl(other.url());
setAsync(other.async());
setRequest(other.request());
setManager(new QNetworkAccessManager(this));
}
NetworkResource &NetworkResource::operator =(const NetworkResource &other)
{
setId(other.id());
setUrl(other.url());
setAsync(other.async());
setRequest(other.request());
return *this;
}
QString NetworkResource::id() const
{
return id_;
}
void NetworkResource::setId(const QString &id)
{
id_ = id;
}
QUrl NetworkResource::url() const
{
return url_;
}
void NetworkResource::setUrl(const QUrl &url)
{
url_ = url;
}
bool NetworkResource::async() const
{
return async_;
}
void NetworkResource::setAsync(bool async)
{
async_ = async;
}
QNetworkRequest NetworkResource::request() const
{
return request_;
}
void NetworkResource::setRequest(const QNetworkRequest &request)
{
request_ = request;
}
QNetworkAccessManager *NetworkResource::manager() const
{
return manager_;
}
QNetworkReply *NetworkResource::reply() const
{
return reply_;
}
QString NetworkResource::method() const
{
return method_;
}
QString NetworkResource::contentType() const
{
return contentType_;
}
QByteArray NetworkResource::contentData() const
{
return contentData_;
}
NetworkResource *NetworkResource::head()
{
setMethod("HEAD");
return send(url(), async());
}
NetworkResource *NetworkResource::get()
{
setMethod("GET");
return send(url(), async());
}
NetworkResource *NetworkResource::post(const QByteArray &contentData, const QString &contentType)
{
setMethod("POST");
setContentType(contentType);
setContentData(contentData);
return send(url(), async());
}
NetworkResource *NetworkResource::post(const QUrlQuery &contentData)
{
setMethod("POST");
setContentType("application/x-www-form-urlencoded");
setContentData(contentData.toString(QUrl::FullyEncoded).toUtf8());
return send(url(), async());
}
NetworkResource *NetworkResource::put(const QByteArray &contentData, const QString &contentType)
{
setMethod("PUT");
setContentType(contentType);
setContentData(contentData);
return send(url(), async());
}
NetworkResource *NetworkResource::put(const QUrlQuery &contentData)
{
setMethod("PUT");
setContentType("application/x-www-form-urlencoded");
setContentData(contentData.toString(QUrl::FullyEncoded).toUtf8());
return send(url(), async());
}
NetworkResource *NetworkResource::deleteResource()
{
setMethod("DELETE");
return send(url(), async());
}
bool NetworkResource::isFinishedWithNoError()
{
if (reply()->isFinished() && reply()->error() == QNetworkReply::NoError) {
return true;
}
return false;
}
QByteArray NetworkResource::readData()
{
QByteArray data;
if (isFinishedWithNoError()) {
data = reply()->readAll();
}
return data;
}
bool NetworkResource::saveData(const QString &path)
{
if (isFinishedWithNoError()) {
return qtlib::File(path).writeData(reply()->readAll());
}
return false;
}
void NetworkResource::abort()
{
if (reply()->isRunning()) {
reply()->abort();
}
}
void NetworkResource::replyFinished()
{
if (isFinishedWithNoError()) {
// Check if redirection
// Note: An auto redirection option is available since Qt 5.6
QUrl redirectUrl;
if (reply()->hasRawHeader("Location")) {
redirectUrl.setUrl(QString(reply()->rawHeader("Location")));
}
else if (reply()->hasRawHeader("Refresh")) {
redirectUrl.setUrl(QString(reply()->rawHeader("Refresh")).split("url=").last());
}
if (!redirectUrl.isEmpty()) {
if (redirectUrl.isRelative()) {
redirectUrl = reply()->url().resolved(redirectUrl);
}
reply()->deleteLater();
send(redirectUrl, true);
return;
}
}
emit finished(this);
}
void NetworkResource::replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
emit downloadProgress(id(), bytesReceived, bytesTotal);
}
void NetworkResource::replyUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
emit uploadProgress(id(), bytesSent, bytesTotal);
}
void NetworkResource::setManager(QNetworkAccessManager *manager)
{
manager_ = manager;
}
void NetworkResource::setReply(QNetworkReply *reply)
{
reply_ = reply;
}
void NetworkResource::setMethod(const QString &method)
{
method_ = method;
}
void NetworkResource::setContentType(const QString &contentType)
{
contentType_ = contentType;
}
void NetworkResource::setContentData(const QByteArray &contentData)
{
contentData_ = contentData;
}
NetworkResource *NetworkResource::send(const QUrl &url, bool async)
{
QNetworkRequest networkRequest = request();
networkRequest.setUrl(url);
if (method() == "HEAD") {
setReply(manager()->head(networkRequest));
}
else if (method() == "GET") {
setReply(manager()->get(networkRequest));
}
else if (method() == "POST") {
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType()));
setReply(manager()->post(networkRequest, contentData()));
}
else if (method() == "PUT") {
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType()));
setReply(manager()->put(networkRequest, contentData()));
}
else if (method() == "DELETE") {
setReply(manager()->deleteResource(networkRequest));
}
else {
Q_ASSERT(false);
}
connect(reply(), &QNetworkReply::finished, this, &NetworkResource::replyFinished);
connect(reply(), &QNetworkReply::downloadProgress, this, &NetworkResource::replyDownloadProgress);
connect(reply(), &QNetworkReply::uploadProgress, this, &NetworkResource::replyUploadProgress);
if (!async) {
QEventLoop eventLoop;
connect(this, &NetworkResource::finished, &eventLoop, &QEventLoop::quit);
eventLoop.exec();
}
return this;
}
} // namespace qtlib
/**
* qtlib
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0
* @link https://github.com/akiraohgaki/qtlib
*/
#pragma once
#include <QObject>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
namespace qtlib {
class NetworkResource : public QObject
{
Q_OBJECT
public:
explicit NetworkResource(const QString &id = "", const QUrl &url = QUrl(), bool async = true, QObject *parent = 0);
~NetworkResource();
NetworkResource(const NetworkResource &other, QObject *parent = 0);
NetworkResource &operator =(const NetworkResource &other);
QString id() const;
void setId(const QString &id);
QUrl url() const;
void setUrl(const QUrl &url);
bool async() const;
void setAsync(bool async);
QNetworkRequest request() const;
void setRequest(const QNetworkRequest &request);
QNetworkAccessManager *manager() const;
QNetworkReply *reply() const;
QString method() const;
QString contentType() const;
QByteArray contentData() const;
NetworkResource *head();
NetworkResource *get();
NetworkResource *post(const QByteArray &contentData, const QString &contentType);
NetworkResource *post(const QUrlQuery &contentData);
NetworkResource *put(const QByteArray &contentData, const QString &contentType);
NetworkResource *put(const QUrlQuery &contentData);
NetworkResource *deleteResource();
bool isFinishedWithNoError();
QByteArray readData();
bool saveData(const QString &path);
signals:
void finished(NetworkResource *resource);
void downloadProgress(QString id, qint64 bytesReceived, qint64 bytesTotal);
void uploadProgress(QString id, qint64 bytesSent, qint64 bytesTotal);
public slots:
void abort();
private slots:
void replyFinished();
void replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void replyUploadProgress(qint64 bytesSent, qint64 bytesTotal);
private:
void setManager(QNetworkAccessManager *manager);
void setReply(QNetworkReply *reply);
void setMethod(const QString &method);
void setContentType(const QString &contentType);
void setContentData(const QByteArray &contentData);
NetworkResource *send(const QUrl &url, bool async);
QString id_;
QUrl url_;
bool async_;
QNetworkRequest request_;
QNetworkAccessManager *manager_;
QNetworkReply *reply_;
QString method_;
QString contentType_;
QByteArray contentData_;
};
} // namespace qtlib
/**
* qtlib
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0
* @link https://github.com/akiraohgaki/qtlib
*/
#include "qtlib_ocsapi.h"
#include <QXmlStreamReader>
#include "qtlib_json.h"
#include "qtlib_networkresource.h"
namespace qtlib {
// OCS-API Specification
// https://www.freedesktop.org/wiki/Specifications/open-collaboration-services/
OcsApi::OcsApi(const QString &id, const QUrl &baseUrl, const QString &userName, const QString &password, QObject *parent)
: QObject(parent), id_(id), baseUrl_(baseUrl), userName_(userName), password_(password)
{}
OcsApi::OcsApi(const OcsApi &other, QObject *parent)
: QObject(parent)
{
setId(other.id());
setBaseUrl(other.baseUrl());
setUserName(other.userName());
setPassword(other.password());
}
OcsApi &OcsApi::operator =(const OcsApi &other)
{
setId(other.id());
setBaseUrl(other.baseUrl());
setUserName(other.userName());
setPassword(other.password());
return *this;
}
QString OcsApi::id() const
{
return id_;
}
void OcsApi::setId(const QString &id)
{
id_ = id;
}
QUrl OcsApi::baseUrl() const
{
return baseUrl_;
}
void OcsApi::setBaseUrl(const QUrl &baseUrl)
{
baseUrl_ = baseUrl;
}
QString OcsApi::userName() const
{
return userName_;
}
void OcsApi::setUserName(const QString &userName)
{
userName_ = userName;
}
QString OcsApi::password() const
{
return password_;
}
void OcsApi::setPassword(const QString &password)
{
password_ = password;
}
QJsonObject OcsApi::getConfig()
{
QUrl url = baseUrl().resolved(QUrl("config"));
url.setQuery("format=json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::checkPerson()
{
QUrl url = baseUrl().resolved(QUrl("person/check"));
QUrlQuery formData;
formData.addQueryItem("login", userName());
formData.addQueryItem("password", password());
formData.addQueryItem("format", "json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.post(formData)->readData()).toObject();
}
QJsonObject OcsApi::getPersonDataSet(const QUrlQuery &query)
{
QUrl url = baseUrl().resolved(QUrl("person/data"));
url.setUserName(userName());
url.setPassword(password());
QUrlQuery newQuery(query);
newQuery.removeQueryItem("format");
newQuery.addQueryItem("format", "json");
url.setQuery(newQuery);
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getPersonData(const QString &personId)
{
QUrl url = baseUrl().resolved(QUrl("person/data/" + personId));
url.setUserName(userName());
url.setPassword(password());
url.setQuery("format=json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getPersonSelf()
{
QUrl url = baseUrl().resolved(QUrl("person/self"));
url.setUserName(userName());
url.setPassword(password());
url.setQuery("format=json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentCategories()
{
QUrl url = baseUrl().resolved(QUrl("content/categories"));
url.setQuery("format=json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentDataSet(const QUrlQuery &query)
{
QUrl url = baseUrl().resolved(QUrl("content/data"));
QUrlQuery newQuery(query);
newQuery.removeQueryItem("format");
newQuery.addQueryItem("format", "json");
url.setQuery(newQuery);
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentData(const QString &contentId)
{
QUrl url = baseUrl().resolved(QUrl("content/data/" + contentId));
url.setQuery("format=json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonObject OcsApi::getContentDownload(const QString &contentId, const QString &itemId)
{
QUrl url = baseUrl().resolved(QUrl("content/download/" + contentId + "/" + itemId));
url.setQuery("format=json");
qtlib::NetworkResource resource(url.toString(), url, false);
return qtlib::Json(resource.get()->readData()).toObject();
}
QJsonArray OcsApi::getProviderFile(const QUrl &url)
{
QJsonArray providers;
qtlib::NetworkResource resource(url.toString(), url, false);
QXmlStreamReader reader(resource.get()->readData());
QStringList whitelist;
whitelist << "id" << "location" << "name" << "icon" << "termsofuse" << "register";
while (!reader.atEnd() && !reader.hasError()) {
reader.readNext();
if (reader.isStartElement() && reader.name() == "provider") {
QJsonObject provider;
provider["_providerfile"] = url.toString();
providers.append(provider);
continue;
}
QString elementName = reader.name().toString();
if (!providers.isEmpty() && whitelist.contains(elementName)) {
int i(providers.size() - 1);
QJsonObject provider = providers[i].toObject();
provider[elementName] = reader.readElementText();
providers[i] = provider;
}
}
return providers;
}
} // namespace qtlib
/**
* qtlib
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0
* @link https://github.com/akiraohgaki/qtlib
*/
#pragma once
#include <QObject>
#include <QUrl>
#include <QUrlQuery>
#include <QJsonObject>
#include <QJsonArray>
namespace qtlib {
class OcsApi : public QObject
{
Q_OBJECT
public:
explicit OcsApi(const QString &id = "", const QUrl &baseUrl = QUrl(), const QString &userName = "", const QString &password = "", QObject *parent = 0);
OcsApi(const OcsApi &other, QObject *parent = 0);
OcsApi &operator =(const OcsApi &other);
QString id() const;
void setId(const QString &id);
QUrl baseUrl() const;
void setBaseUrl(const QUrl &baseUrl);
QString userName() const;
void setUserName(const QString &userName);
QString password() const;
void setPassword(const QString &password);
QJsonObject getConfig();
QJsonObject checkPerson();
QJsonObject getPersonDataSet(const QUrlQuery &query = QUrlQuery());
QJsonObject getPersonData(const QString &personId);
QJsonObject getPersonSelf();
QJsonObject getContentCategories();
QJsonObject getContentDataSet(const QUrlQuery &query = QUrlQuery());
QJsonObject getContentData(const QString &contentId);
QJsonObject getContentDownload(const QString &contentId, const QString &itemId);
static QJsonArray getProviderFile(const QUrl &url);
private:
QString id_;
QUrl baseUrl_;
QString userName_;
QString password_;
};
} // namespace qtlib
This diff is collapsed.
/**
* qtlib
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0
* @link https://github.com/akiraohgaki/qtlib
*/
#pragma once
#include <QObject>
namespace qtlib {
class Package : public QObject
{
Q_OBJECT
public:
explicit Package(const QString &path = "", QObject *parent = 0);
Package(const Package &other, QObject *parent = 0);
Package &operator =(const Package &other);
QString path() const;
void setPath(const QString &path);
#ifdef QTLIB_UNIX
bool installAsProgram(const QString &newPath);
bool installAsFile(const QString &newPath);
bool installAsArchive(const QString &destinationDirPath);
bool installAsPlasmapkg(const QString &type = "plasmoid");
bool uninstallAsPlasmapkg(const QString &type = "plasmoid");
#endif
#ifdef Q_OS_ANDROID
bool installAsApk();
#endif
private:
#ifdef QTLIB_UNIX
bool execute(const QString &program, const QStringList &arguments);
#endif
QString path_;
};
} // namespace qtlib
#include <QObject>
#include <QCoreApplication>
#include <QDebug>
#include "qtlib_file.h"
#include "qtlib_dir.h"
#include "qtlib_json.h"
#include "qtlib_config.h"
#include "qtlib_networkresource.h"
#include "qtlib_ocsapi.h"
#include "qtlib_package.h"
class Test : public QObject
{
public:
Test() {}
virtual ~Test() {}
void start()
{
qDebug() << "Start";
qtlib::NetworkResource *resource = new qtlib::NetworkResource(
"LGPL-3.0",
QUrl("https://api.opensource.org/license/LGPL-3.0"),
false,
this);
connect(resource, &qtlib::NetworkResource::downloadProgress, this, &Test::downloadProgress);
QJsonObject result = qtlib::Json(resource->get()->readData()).toObject();
qDebug() << resource->id() << ":" << result["name"].toString();
connect(resource, &qtlib::NetworkResource::finished, this, &Test::finished);
resource->setId(result["name"].toString());
resource->setUrl(QUrl(result["text"].toArray()[0].toObject()["url"].toString()));
resource->setAsync(true);
resource->get();
}
public slots:
void finished(qtlib::NetworkResource *resource)
{
QString path = qtlib::Dir::tempPath() + "/" + resource->url().fileName();
resource->saveData(path);
qDebug() << "Downloaded" << resource->id() << ":" << path;
qDebug() << "Finished";
resource->deleteLater();
QCoreApplication::exit();
}
void downloadProgress(QString id, qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << "Progress" << id << ":" << bytesReceived << "/" << bytesTotal;
}
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Test test;
test.start();
return app.exec();
}
include(src/lib/qtlib/qtlib.pri)
include(src/app/app.pri)
TARGET = xdgurl
TEMPLATE = app
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
RESOURCES += src/desktop/desktop.qrc
DISTFILES += \
README.md \
src/desktop/xdgurl.desktop \
src/desktop/appimage-desktopintegration \
pkg/build.sh \
pkg/ubuntu/debian/changelog \
pkg/ubuntu/debian/compat \
pkg/ubuntu/debian/control \
pkg/ubuntu/debian/copyright \
pkg/ubuntu/debian/rules \
pkg/ubuntu/debian/xdgurl.install \
pkg/fedora/xdgurl.spec \
pkg/arch/PKGBUILD
include(deployment.pri)