mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-07-12 08:32:21 +08:00
feat: 匿名数据搜集
This commit is contained in:
parent
831bef8233
commit
c00d62c010
85
src/backend/DataCollectorAndUploader.cpp
Normal file
85
src/backend/DataCollectorAndUploader.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include "DataCollectorAndUploader.h"
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QStandardPaths>
|
||||
#include <QNetworkReply>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSettings>
|
||||
#include <QProcess>
|
||||
#include <QDebug>
|
||||
|
||||
DataCollectorAndUploader::DataCollectorAndUploader(QObject *parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void DataCollectorAndUploader::collectAndUploadData()
|
||||
{
|
||||
collectData();
|
||||
}
|
||||
|
||||
void DataCollectorAndUploader::collectData()
|
||||
{
|
||||
QString distributor_id;
|
||||
QString release;
|
||||
QString architecture;
|
||||
|
||||
QSettings config(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini", QSettings::IniFormat);
|
||||
QString version = config.value("build/version").toString();
|
||||
QString uuid = config.value("info/uuid").toString();
|
||||
|
||||
|
||||
// Execute lsb_release --all and capture the output
|
||||
QProcess lsbProcess;
|
||||
lsbProcess.start("lsb_release", QStringList() << "--all");
|
||||
lsbProcess.waitForFinished();
|
||||
QString lsbOutput = lsbProcess.readAllStandardOutput();
|
||||
|
||||
QStringList lines = lsbOutput.split('\n');
|
||||
for (const QString &line : lines) {
|
||||
if (line.contains("Distributor ID:")) {
|
||||
distributor_id = line.split(":").last().trimmed();
|
||||
} else if (line.contains("Release:")) {
|
||||
release = line.split(":").last().trimmed();
|
||||
}
|
||||
}
|
||||
|
||||
// Execute uname -m to get the architecture
|
||||
QProcess unameProcess;
|
||||
unameProcess.start("uname", QStringList() << "-m");
|
||||
unameProcess.waitForFinished();
|
||||
architecture = unameProcess.readAllStandardOutput().trimmed();
|
||||
|
||||
// Create a JSON object
|
||||
QJsonObject json;
|
||||
json.insert("Distributor ID", distributor_id);
|
||||
json.insert("Release", release);
|
||||
json.insert("Architecture", architecture);
|
||||
json.insert("Store_Version", version);
|
||||
json.insert("UUID", uuid);
|
||||
|
||||
|
||||
|
||||
// Convert to byte array
|
||||
QJsonDocument doc(json);
|
||||
QByteArray jsonData = doc.toJson();
|
||||
|
||||
// Initialize a network request
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||
QUrl url("http://zunyun01.store.deepinos.org.cn:12345");
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
// Send the POST request
|
||||
QNetworkReply *reply = manager->post(request, jsonData);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
emit uploadSuccessful();
|
||||
} else {
|
||||
emit uploadFailed(reply->errorString());
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
21
src/backend/DataCollectorAndUploader.h
Normal file
21
src/backend/DataCollectorAndUploader.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef DATACOLLECTORANDUPLOADER_H
|
||||
#define DATACOLLECTORANDUPLOADER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class DataCollectorAndUploader : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit DataCollectorAndUploader(QObject *parent = nullptr);
|
||||
void collectAndUploadData();
|
||||
|
||||
signals:
|
||||
void uploadSuccessful();
|
||||
void uploadFailed(QString errorString);
|
||||
|
||||
private:
|
||||
void collectData();
|
||||
};
|
||||
|
||||
#endif // DATACOLLECTORANDUPLOADER_H
|
13
src/main.cpp
13
src/main.cpp
@ -22,6 +22,8 @@
|
||||
#include <QStandardPaths>
|
||||
#include <QSurfaceFormat>
|
||||
|
||||
#include <backend/DataCollectorAndUploader.h>
|
||||
|
||||
DCORE_USE_NAMESPACE
|
||||
DWIDGET_USE_NAMESPACE
|
||||
|
||||
@ -124,6 +126,17 @@ int main(int argc, char *argv[])
|
||||
// 初始化 config.ini 配置文件
|
||||
Utils::initConfig();
|
||||
|
||||
// 回传版本信息,不涉及个人隐私
|
||||
DataCollectorAndUploader uploader;
|
||||
QObject::connect(&uploader, &DataCollectorAndUploader::uploadSuccessful, [](){
|
||||
qDebug() << "Data uploaded successfully";
|
||||
});
|
||||
QObject::connect(&uploader, &DataCollectorAndUploader::uploadFailed, [](QString error){
|
||||
qDebug() << "Upload failed with error: " << error;
|
||||
});
|
||||
|
||||
uploader.collectAndUploadData();
|
||||
|
||||
// Set display backend
|
||||
Utils::setQPAPlatform();
|
||||
|
||||
|
@ -34,6 +34,7 @@ CONFIG += c++11 link_pkgconfig
|
||||
PKGCONFIG += dtkcore dtkgui dtkwidget
|
||||
|
||||
SOURCES += \
|
||||
backend/DataCollectorAndUploader.cpp \
|
||||
backend/downloadworker.cpp \
|
||||
backend/image_show.cpp \
|
||||
backend/sparkapi.cpp \
|
||||
@ -58,6 +59,7 @@ SOURCES += \
|
||||
mainwindow-dtk.cpp
|
||||
|
||||
HEADERS += \
|
||||
backend/DataCollectorAndUploader.h \
|
||||
backend/downloadworker.h \
|
||||
backend/image_show.h \
|
||||
backend/sparkapi.h \
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <QFile>
|
||||
#include <QUuid>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#define UOSDeveloperModeFile "/var/lib/deepin/developer-mode/enabled"
|
||||
@ -96,6 +97,11 @@ void Utils::initConfig()
|
||||
{
|
||||
config.setValue("runtime/useWayland", useWayland);
|
||||
}
|
||||
|
||||
// check uuid
|
||||
if (!config.contains("info/uuid")){
|
||||
config.setValue("info/uuid", initUUID());
|
||||
}
|
||||
config.sync(); // 写入更改至 config.ini,并同步最新内容
|
||||
|
||||
checkUOSDeveloperMode(); // Check UOS developer mode
|
||||
@ -126,6 +132,14 @@ bool Utils::isUOS()
|
||||
return isUOS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Utils::initUUID 生成 UUID
|
||||
*/
|
||||
QString Utils::initUUID(){
|
||||
QUuid uuid = QUuid::createUuid();
|
||||
return uuid.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Utils::setQPAPlatform Set display backend
|
||||
*/
|
||||
|
@ -12,6 +12,7 @@ public:
|
||||
static bool isWayland();
|
||||
static void initConfig();
|
||||
static bool isUOS();
|
||||
static QString initUUID();
|
||||
static void setQPAPlatform();
|
||||
static void checkUOSDeveloperMode();
|
||||
static QJsonObject parseFeatureJsonFile();
|
||||
|
Loading…
x
Reference in New Issue
Block a user