Skip to content
Snippets Groups Projects
Commit 11b274a7 authored by akiraohgaki's avatar akiraohgaki
Browse files

Remove qtlib

parent ae3ffa86
No related branches found
No related tags found
No related merge requests found
Showing
with 0 additions and 1503 deletions
*.pro.user
# qtlib
A library for Qt app.
Copyright: Akira Ohgaki
License: LGPL-3+
QT += \
core \
network
HEADERS += \
$${PWD}/src/qtlib_file.h \
$${PWD}/src/qtlib_dir.h \
$${PWD}/src/qtlib_json.h \
$${PWD}/src/qtlib_config.h \
$${PWD}/src/qtlib_networkresource.h \
$${PWD}/src/qtlib_ocsapi.h \
$${PWD}/src/qtlib_package.h
SOURCES += \
$${PWD}/src/qtlib_file.cpp \
$${PWD}/src/qtlib_dir.cpp \
$${PWD}/src/qtlib_json.cpp \
$${PWD}/src/qtlib_config.cpp \
$${PWD}/src/qtlib_networkresource.cpp \
$${PWD}/src/qtlib_ocsapi.cpp \
$${PWD}/src/qtlib_package.cpp
INCLUDEPATH += $${PWD}/src
unix:!ios:!android {
DEFINES += QTLIB_UNIX
}
android {
QT += androidextras
}
include(qtlib.pri)
DISTFILES += README.md
/**
* 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_config.h"
#include "qtlib_file.h"
#include "qtlib_dir.h"
#include "qtlib_json.h"
namespace qtlib {
Config::Config(const QString &configDirPath, QObject *parent)
: QObject(parent), configDirPath_(configDirPath)
{}
Config::Config(const Config &other, QObject *parent)
: QObject(parent)
{
setConfigDirPath(other.configDirPath());
}
Config &Config::operator =(const Config &other)
{
setConfigDirPath(other.configDirPath());
return *this;
}
QString Config::configDirPath() const
{
return configDirPath_;
}
void Config::setConfigDirPath(const QString &configDirPath)
{
configDirPath_ = configDirPath;
}
QJsonObject Config::get(const QString &name)
{
QString configFilePath = configDirPath() + "/" + name + ".json";
QByteArray json = qtlib::File(configFilePath).readData();
if (json.isEmpty()) {
json = QString("{}").toUtf8(); // Blank JSON data as default
}
return qtlib::Json(json).toObject();
}
bool Config::set(const QString &name, const QJsonObject &object)
{
QString configFilePath = configDirPath() + "/" + name + ".json";
QByteArray json = qtlib::Json(object).toJson();
qtlib::Dir(configDirPath()).make();
return qtlib::File(configFilePath).writeData(json);
}
} // 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 <QJsonObject>
namespace qtlib {
class Config : public QObject
{
Q_OBJECT
public:
explicit Config(const QString &configDirPath = "", QObject *parent = 0);
Config(const Config &other, QObject *parent = 0);
Config &operator =(const Config &other);
QString configDirPath() const;
void setConfigDirPath(const QString &configDirPath);
QJsonObject get(const QString &name);
bool set(const QString &name, const QJsonObject &object);
private:
QString configDirPath_;
};
} // 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_dir.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
namespace qtlib {
Dir::Dir(const QString &path, QObject *parent)
: QObject(parent), path_(path)
{}
Dir::Dir(const Dir &other, QObject *parent)
: QObject(parent)
{
setPath(other.path());
}
Dir &Dir::operator =(const Dir &other)
{
setPath(other.path());
return *this;
}
QString Dir::path() const
{
return path_;
}
void Dir::setPath(const QString &path)
{
path_ = path;
}
bool Dir::exists()
{
return QDir(path()).exists();
}
QFileInfoList Dir::list()
{
QDir dir(path());
dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
//dir.setSorting(QDir::DirsFirst | QDir::Name);
return dir.entryInfoList();
}
bool Dir::make()
{
// This function will create all parent directories
QDir dir(path());
if (!dir.exists() && dir.mkpath(path())) {
return true;
}
return false;
}
bool Dir::copy(const QString &newPath)
{
// This function will copy files recursively
return copyRecursively(path(), newPath);
}
bool Dir::move(const QString &newPath)
{
return QDir(path()).rename(path(), newPath);
}
bool Dir::remove()
{
// This function will remove files recursively
return QDir(path()).removeRecursively();
}
QString Dir::rootPath()
{
return QDir::rootPath();
}
QString Dir::tempPath()
{
return QDir::tempPath();
}
QString Dir::homePath()
{
return QDir::homePath();
}
QString Dir::genericDataPath()
{
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
}
QString Dir::genericConfigPath()
{
return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
}
QString Dir::genericCachePath()
{
return QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
}
QString Dir::kdehomePath()
{
// KDE System Administration/Environment Variables
// https://userbase.kde.org/KDE_System_Administration/Environment_Variables
// KDE 4 maybe uses $KDEHOME
QString kdehomePath = QString::fromLocal8Bit(qgetenv("KDEHOME").constData());
if (kdehomePath.isEmpty()) {
kdehomePath = homePath() + "/.kde";
}
return kdehomePath;
}
bool Dir::copyRecursively(const QString &srcPath, const QString &newPath)
{
QFileInfo fileInfo(srcPath);
if (fileInfo.isSymLink() && !fileInfo.exists()) {
// Ignore broken symlink
return true;
}
else if (fileInfo.isFile()) {
return QFile(srcPath).copy(newPath);
}
else if (fileInfo.isDir()) {
QDir newDir(newPath);
QString newDirName = newDir.dirName();
newDir.cdUp();
if (newDir.mkdir(newDirName)) {
QDir dir(srcPath);
dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
QStringList entries = dir.entryList();
foreach (const QString &entry, entries) {
if (!copyRecursively(srcPath + "/" + entry, newPath + "/" + entry)) {
return false;
}
}
return true;
}
}
return false;
}
} // 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 <QFileInfoList>
namespace qtlib {
class Dir : public QObject
{
Q_OBJECT
public:
explicit Dir(const QString &path = "", QObject *parent = 0);
Dir(const Dir &other, QObject *parent = 0);
Dir &operator =(const Dir &other);
QString path() const;
void setPath(const QString &path);
bool exists();
QFileInfoList list();
bool make();
bool copy(const QString &newPath);
bool move(const QString &newPath);
bool remove();
static QString rootPath();
static QString tempPath();
static QString homePath();
static QString genericDataPath();
static QString genericConfigPath();
static QString genericCachePath();
static QString kdehomePath();
private:
bool copyRecursively(const QString &srcPath, const QString &newPath);
QString path_;
};
} // 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_file.h"
#include <QIODevice>
#include <QTextStream>
#include <QFile>
namespace qtlib {
File::File(const QString &path, QObject *parent)
: QObject(parent), path_(path)
{}
File::File(const File &other, QObject *parent)
: QObject(parent)
{
setPath(other.path());
}
File &File::operator =(const File &other)
{
setPath(other.path());
return *this;
}
QString File::path() const
{
return path_;
}
void File::setPath(const QString &path)
{
path_ = path;
}
bool File::exists()
{
return QFile(path()).exists();
}
QByteArray File::readData()
{
QByteArray data;
QFile file(path());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
data = file.readAll();
file.close();
}
return data;
}
bool File::writeData(const QByteArray &data)
{
QFile file(path());
if (file.open(QIODevice::WriteOnly)) {
file.write(data);
file.close();
return true;
}
return false;
}
QString File::readText()
{
QString data;
QFile file(path());
if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
in.setCodec("UTF-8");
data = in.readAll();
file.close();
}
return data;
}
bool File::writeText(const QString &data)
{
QFile file(path());
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out.setCodec("UTF-8");
out << data;
file.close();
return true;
}
return false;
}
bool File::copy(const QString &newPath)
{
return QFile(path()).copy(newPath);
}
bool File::move(const QString &newPath)
{
return QFile(path()).rename(newPath);
}
bool File::remove()
{
return QFile(path()).remove();
}
} // 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>
namespace qtlib {
class File : public QObject
{
Q_OBJECT
public:
explicit File(const QString &path = "", QObject *parent = 0);
File(const File &other, QObject *parent = 0);
File &operator =(const File &other);
QString path() const;
void setPath(const QString &path);
bool exists();
QByteArray readData();
bool writeData(const QByteArray &data);
QString readText();
bool writeText(const QString &data);
bool copy(const QString &newPath);
bool move(const QString &newPath);
bool remove();
private:
QString path_;
};
} // 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_json.h"
#include <QJsonDocument>
#include <QJsonParseError>
namespace qtlib {
Json::Json(const QByteArray &json, QObject *parent)
: QObject(parent), json_(json)
{}
Json::Json(const QJsonObject &object, QObject *parent)
: QObject(parent)
{
fromObject(object);
}
Json::Json(const QJsonArray &array, QObject *parent)
: QObject(parent)
{
fromArray(array);
}
Json::Json(const Json &other, QObject *parent)
: QObject(parent)
{
setJson(other.json());
}
Json &Json::operator =(const Json &other)
{
setJson(other.json());
return *this;
}
QByteArray Json::json() const
{
return json_;
}
void Json::setJson(const QByteArray &json)
{
json_ = json;
}
void Json::fromObject(const QJsonObject &object)
{
setJson(QJsonDocument(object).toJson());
}
void Json::fromArray(const QJsonArray &array)
{
setJson(QJsonDocument(array).toJson());
}
QByteArray Json::toJson()
{
return QJsonDocument::fromJson(json()).toJson();
}
QJsonObject Json::toObject()
{
return QJsonDocument::fromJson(json()).object();
}
QJsonArray Json::toArray()
{
return QJsonDocument::fromJson(json()).array();
}
bool Json::isValid()
{
QJsonParseError parseError;
QJsonDocument::fromJson(json(), &parseError);
if (parseError.error == QJsonParseError::NoError) {
return true;
}
return false;
}
bool Json::isObject()
{
return QJsonDocument::fromJson(json()).isObject();
}
bool Json::isArray()
{
return QJsonDocument::fromJson(json()).isArray();
}
} // 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 <QJsonObject>
#include <QJsonArray>
namespace qtlib {
class Json : public QObject
{
Q_OBJECT
public:
explicit Json(const QByteArray &json = QByteArray(), QObject *parent = 0);
explicit Json(const QJsonObject &object, QObject *parent = 0);
explicit Json(const QJsonArray &array, QObject *parent = 0);
Json(const Json &other, QObject *parent = 0);
Json &operator =(const Json &other);
QByteArray json() const;
void setJson(const QByteArray &json);
void fromObject(const QJsonObject &object);
void fromArray(const QJsonArray &array);
QByteArray toJson();
QJsonObject toObject();
QJsonArray toArray();
bool isValid();
bool isObject();
bool isArray();
private:
QByteArray json_;
};
} // 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_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
/**
* 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_package.h"
#ifdef QTLIB_UNIX
#include <QJsonObject>
#include <QMimeDatabase>
#include <QProcess>
#endif
#ifdef Q_OS_ANDROID
#include <QAndroidJniObject>
#endif
namespace qtlib {
Package::Package(const QString &path, QObject *parent)
: QObject(parent), path_(path)
{}
Package::Package(const Package &other, QObject *parent)
: QObject(parent)
{
setPath(other.path());
}
Package &Package::operator =(const Package &other)
{
setPath(other.path());
return *this;
}
QString Package::path() const
{
return path_;
}
void Package::setPath(const QString &path)
{
path_ = path;
}
#ifdef QTLIB_UNIX
bool Package::installAsProgram(const QString &newPath)
{
QStringList arguments;
arguments << "-m" << "755" << "-p" << path() << newPath;
return execute("install", arguments);
}
bool Package::installAsFile(const QString &newPath)
{
QStringList arguments;
arguments << "-m" << "644" << "-p" << path() << newPath;
return execute("install", arguments);
}
bool Package::installAsArchive(const QString &destinationDirPath)
{
QJsonObject archiveTypes;
archiveTypes["application/x-tar"] = QString("tar");
archiveTypes["application/x-gzip"] = QString("tar");
archiveTypes["application/gzip"] = QString("tar");
archiveTypes["application/x-bzip"] = QString("tar");
archiveTypes["application/x-bzip2"] = QString("tar");
archiveTypes["application/x-xz"] = QString("tar");
archiveTypes["application/x-lzma"] = QString("tar");
archiveTypes["application/x-lzip"] = QString("tar");
archiveTypes["application/x-compressed-tar"] = QString("tar");
archiveTypes["application/x-bzip-compressed-tar"] = QString("tar");
archiveTypes["application/x-bzip2-compressed-tar"] = QString("tar");
archiveTypes["application/x-xz-compressed-tar"] = QString("tar");
archiveTypes["application/x-lzma-compressed-tar"] = QString("tar");
archiveTypes["application/x-lzip-compressed-tar"] = QString("tar");
archiveTypes["application/zip"] = QString("zip");
archiveTypes["application/x-7z-compressed"] = QString("7z");
archiveTypes["application/x-rar"] = QString("rar");
archiveTypes["application/x-rar-compressed"] = QString("rar");
QString mimeType = QMimeDatabase().mimeTypeForFile(path()).name();
if (archiveTypes.contains(mimeType)) {
QString archiveType = archiveTypes[mimeType].toString();
QString program;
QStringList arguments;
if (archiveType == "tar") {
program = "tar";
arguments << "-xf" << path() << "-C" << destinationDirPath;
}
else if (archiveType == "zip") {
program = "unzip";
arguments << "-o" << path() << "-d" << destinationDirPath;
}
else if (archiveType == "7z") {
program = "7z";
arguments << "x" << path() << "-o" + destinationDirPath; // No space between -o and directory
}
else if (archiveType == "rar") {
program = "unrar";
arguments << "e" << path() << destinationDirPath;
}
return execute(program, arguments);
}
return false;
}
bool Package::installAsPlasmapkg(const QString &type)
{
QStringList arguments;
arguments << "-t" << type << "-i" << path();
return execute("plasmapkg2", arguments);
}
bool Package::uninstallAsPlasmapkg(const QString &type)
{
QStringList arguments;
arguments << "-t" << type << "-r" << path();
return execute("plasmapkg2", arguments);
}
#endif
#ifdef Q_OS_ANDROID
bool Package::installAsApk()
{
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if (activity.isValid()) {
QString filePath = path();
if (filePath.startsWith("file://", Qt::CaseInsensitive)) {
filePath.replace("file://localhost", "", Qt::CaseInsensitive);
filePath.replace("file://", "", Qt::CaseInsensitive);
}
QAndroidJniObject fileUri = QAndroidJniObject::fromString("file://" + filePath);
QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", fileUri.object());
QAndroidJniObject mimeType = QAndroidJniObject::fromString("application/vnd.android.package-archive");
QAndroidJniObject ACTION_VIEW = QAndroidJniObject::getStaticObjectField("android/content/Intent", "ACTION_VIEW", "Ljava/lang/String");
QAndroidJniObject FLAG_ACTIVITY_NEW_TASK = QAndroidJniObject::getStaticObjectField("android/content/Intent", "FLAG_ACTIVITY_NEW_TASK", "Ljava/lang/Integer");
QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", ACTION_VIEW.object());
intent = intent.callObjectMethod("setDataAndType", "(Landroid/net/Uri;Ljava/lang/String;)Landroid/content/Intent;", uri.object(), mimeType.object());
intent = intent.callObjectMethod("setFlags", "(I)Landroid/content/Intent;", FLAG_ACTIVITY_NEW_TASK.object());
activity.callMethod<void>("startActivity", "(Landroid/content/Intent;)V", intent.object());
return true;
}
return false;
}
#endif
#ifdef QTLIB_UNIX
bool Package::execute(const QString &program, const QStringList &arguments)
{
QProcess process;
process.start(program, arguments);
if (process.waitForFinished()) {
process.waitForReadyRead();
return true;
}
return false;
}
#endif
} // 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>
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment