mirror of
https://gitee.com/spark-store-project/spark-store
synced 2026-06-22 22:23:49 +08:00
添加API调用接口,添加读取分类,添加SpkUtils实用函数
使用了SpkSidebarTree子类实现对TreeWidget的特殊要求:不能取消选择,不能拖拽选择。 API调用接口暂时写死。API会获取 配置文件已添加但暂不使用。
This commit is contained in:
+8
-3
@@ -96,12 +96,12 @@ void SpkLogger::Error(QString message, const bool pop)
|
||||
// .arg(message));
|
||||
// msgbox.exec(); // I don't know whether we need to show it non-modal.
|
||||
SpkMsgBox::StaticExec(QObject::tr("Spark Store has encountered an error.\n"
|
||||
"Parts of the experience is expected to be broken.\n\n"
|
||||
"Details:\n%1"),
|
||||
"Parts of the experience is expected to be broken.\n\n"),
|
||||
QObject::tr("Spark Store Error"),
|
||||
QMessageBox::Critical,
|
||||
QMessageBox::Ok,
|
||||
message);
|
||||
message,
|
||||
true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,3 +122,8 @@ void SpkLogger::Critical(QString message)
|
||||
exit(2);
|
||||
}
|
||||
|
||||
void SpkLogger::Notify(QString message)
|
||||
{
|
||||
Q_UNUSED(message);
|
||||
}
|
||||
|
||||
|
||||
+38
-1
@@ -1,12 +1,16 @@
|
||||
|
||||
#include <spkui_general.h>
|
||||
#include "dtk/spkdtkplugin.h"
|
||||
#include <QPluginLoader>
|
||||
#include <QDir>
|
||||
#include <QApplication>
|
||||
#include <QtNetwork/QNetworkProxy>
|
||||
#include "dtk/spkdtkplugin.h"
|
||||
#include "gitver.h"
|
||||
#include "spkstore.h"
|
||||
#include "spkutils.h"
|
||||
|
||||
SpkStore *SpkStore::Instance = nullptr;
|
||||
static void InstallDefaultConfigs();
|
||||
|
||||
SpkStore::SpkStore(bool aCli, QString &aLogPath)
|
||||
{
|
||||
@@ -17,8 +21,27 @@ SpkStore::SpkStore(bool aCli, QString &aLogPath)
|
||||
Instance = this;
|
||||
|
||||
// Finish all essential initialization after this.
|
||||
if(QFileInfo(QDir::homePath() + "/.config/spark-store/config").exists())
|
||||
mCfg = new QSettings(QDir::homePath() + "/.config/spark-store/config", QSettings::IniFormat);
|
||||
else
|
||||
{
|
||||
mCfg = new QSettings(":/info/default_config", QSettings::IniFormat);
|
||||
#ifndef NDEBUG
|
||||
if(!qgetenv("SPARK_NO_INSTALL_CONFIG").toInt())
|
||||
{
|
||||
if(!QFile::copy(":/info/default_config", QDir::homePath() + "/.config/spark-store/config"))
|
||||
sErrPop(tr("Cannot install default config file!"));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
mNetMgr = new QNetworkAccessManager(this);
|
||||
mNetMgr->setProxy(QNetworkProxy(QNetworkProxy::NoProxy)); // FIXME
|
||||
mDistroName = SpkUtils::GetDistroName();
|
||||
mApiRequestUrl = "https://store.deepinos.org/api/"; // TODO: CHECK BEFORE 4.0 RELEASE
|
||||
mUserAgentStr = QString("Spark-Store/%1 Distro/%2")
|
||||
.arg(GitVer::DescribeTags())
|
||||
.arg(mDistroName);
|
||||
|
||||
// Finish all essential initialization before this.
|
||||
if(aCli)
|
||||
@@ -36,3 +59,17 @@ SpkStore::~SpkStore()
|
||||
delete mMainWindow;
|
||||
delete mLogger;
|
||||
}
|
||||
|
||||
QNetworkReply *SpkStore::SendApiRequest(QString aPath, QJsonDocument aParam)
|
||||
{
|
||||
QNetworkRequest request;
|
||||
request.setUrl(mApiRequestUrl + aPath);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
request.setHeader(QNetworkRequest::UserAgentHeader, mUserAgentStr);
|
||||
return mNetMgr->post(request, aParam.isEmpty() ? "{}" : aParam.toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
static void InstallDefaultConfigs()
|
||||
{
|
||||
//TODO:STUB
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
|
||||
#include <QDebug>
|
||||
#include "spkutils.h"
|
||||
|
||||
void SpkUtils::VerifySingleRequest(QPointer<QNetworkReply> aReply)
|
||||
{
|
||||
if(aReply.isNull())
|
||||
return;
|
||||
aReply->disconnect(SIGNAL(finished()));
|
||||
aReply->abort();
|
||||
aReply->deleteLater();
|
||||
}
|
||||
|
||||
QString SpkUtils::GetDistroName()
|
||||
{
|
||||
QSettings osRelease("/etc/os-release", QSettings::IniFormat);
|
||||
return osRelease.value("PRETTY_NAME", "Unknown Distro").toString();
|
||||
}
|
||||
|
||||
bool SpkUtils::VerifyReplyJson(QNetworkReply *aReply, QJsonValue &aRetDoc)
|
||||
{
|
||||
QJsonParseError err;
|
||||
QByteArray rawjson = aReply->readAll();
|
||||
qDebug() << "Received:" << rawjson;
|
||||
QJsonDocument ret = QJsonDocument::fromJson(rawjson, &err);
|
||||
QJsonObject replyObject;
|
||||
if(err.error != QJsonParseError::NoError)
|
||||
{
|
||||
sNotify(QObject::tr("Failed to parse server reply! Error %1.").arg(err.error));
|
||||
sErr(QObject::tr("VerifyReplyJson: returned JSON of request to %1 is unreadable.")
|
||||
.arg(aReply->url().toString()));
|
||||
return false;
|
||||
}
|
||||
if(!ret.isObject())
|
||||
{
|
||||
sNotify(QObject::tr("Server sent back an invalid response."));
|
||||
sErr(QObject::tr("VerifyReplyJson: returned JSON of request to %1 is not an Object.")
|
||||
.arg(aReply->url().toString()));
|
||||
return false;
|
||||
}
|
||||
replyObject = ret.object();
|
||||
if(!replyObject.contains("code"))
|
||||
{
|
||||
sWarn(QObject::tr("VerifyReplyJson: reply of request to %1 doesn't have a code.")
|
||||
.arg(aReply->url().toString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto OpRetCode = replyObject.value("code");
|
||||
if(!OpRetCode.isDouble())
|
||||
{
|
||||
sWarn(QObject::tr("VerifyReplyJson: Reply of request to %1 has a non-numeric code.")
|
||||
.arg(aReply->url().toString()));
|
||||
}
|
||||
else if(OpRetCode.toInt() != 0)
|
||||
{
|
||||
sNotify(QObject::tr("Server sent back an failure message; code: %1.")
|
||||
.arg(OpRetCode.toInt()));
|
||||
sErr(QObject::tr("VerifyReplyJson: Request to %1 failed with code %2.")
|
||||
.arg(aReply->url().toString()).arg(OpRetCode.toInt()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(!replyObject.contains("data"))
|
||||
{
|
||||
sNotify(QObject::tr("Server did not reply with any data."));
|
||||
sErr(QObject::tr("VerifyReplyJson: Reply of request to %1 didn't include any data.")
|
||||
.arg(aReply->url().toString()));
|
||||
return false;
|
||||
}
|
||||
aRetDoc = replyObject.value("data");
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpkUtils::DeleteReplyLater(QNetworkReply *aReply)
|
||||
{
|
||||
QObject::connect(aReply, &QNetworkReply::finished, aReply, &QObject::deleteLater);
|
||||
}
|
||||
Reference in New Issue
Block a user