OfficeAssistant_Win10/OfficeAssistant_msvc/mainwindow.cpp

125 lines
4.1 KiB
C++

//
// Created by HW on 2023/07/26.
//
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
#include "mainwindow.h"
#include "ui_MainWindow.h"
#include "netio.h"
#include "config.h"
ExitManager exit_manager;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
agree = false;
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect deskRect = desktopWidget->availableGeometry();
resize(deskRect.width()*0.8, deskRect.height()*0.8);
setWindowState(Qt::WindowMaximized);
mainWindowLayout=new MainWindowLayout(this);
setCentralWidget(mainWindowLayout);
setWindowTitle(QString::fromLocal8Bit(TITLE));
QString path = QApplication::applicationDirPath();
path += LOGO_TITLEBAR;
icon = new QIcon(path);
setWindowIcon(*icon);
setStyleSheet("*{font-family:SimHei;}");
QFile file(QApplication::applicationDirPath() + DEFAULT_FILE);
if(!file.open(QIODevice::Text|QIODevice::ReadOnly))
{
QMessageBox::warning(nullptr, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("无法打开配置文件"));
exit_manager.exit(1);
}
QByteArray buffer;
buffer = file.readAll();
file.close();
QJsonDocument document = QJsonDocument::fromJson(buffer);
if(document.isObject())
{
if(document.object().value("agree").isUndefined())
{
QMessageBox msg;
msg.setWindowTitle(QString::fromLocal8Bit("用户体验改善计划"));
msg.setText(QString::fromLocal8Bit("为改善我们的服务质量,我们将向服务器发送以下信息:\n"
"1.您的计算机配置;\n"
"2.您计算机上的软件安装列表;\n"
"3.您是否设置了开机启动,以及是不是通过开机启动的方式启动本软件。\n"
"第二项信息是我们提供服务所必需的,如您选择不同意但继续使用本软件,则第二项数据将正常发送,但第一项和第三项数据不会发送。选择不同意不影响您使用本软件。"));
msg.addButton(QString::fromLocal8Bit("同意"), QMessageBox::AcceptRole);
msg.addButton(QString::fromLocal8Bit("不同意"), QMessageBox::RejectRole);
int ret = msg.exec();
if(ret==QMessageBox::AcceptRole)
{
QJsonObject obj_root = document.object();
obj_root.insert("agree", true);
document.setObject(obj_root);
agree = true;
DeviceRequest* device_request = new DeviceRequest;//构造函数直接发出去数据了
delete device_request;
}else
{
QJsonObject obj_root = document.object();
obj_root.insert("agree", false);
document.setObject(obj_root);
agree = false;
}
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
file.write(document.toJson());
file.close();
}
}
else if(document.object().value("agree").toBool())
{
agree = true;
DeviceRequest* device_request = new DeviceRequest;
delete device_request;
}
}
else
{
QMessageBox msg;
msg.setWindowTitle(QString::fromLocal8Bit("用户体验改善计划"));
msg.setText(QString::fromLocal8Bit("为改善我们的服务质量,我们将向服务器发送以下信息:\n"
"1.您的计算机配置;\n"
"2.您计算机上的软件安装列表;\n"
"3.您是否设置了开机启动,以及是不是通过开机启动的方式启动本软件。\n"
"第二项信息是我们提供服务所必需的,如您选择不同意但继续使用本软件,则第二项数据将正常发送,但第一项和第三项数据不会发送。选择不同意不影响您使用本软件。"));
msg.addButton(QString::fromLocal8Bit("同意"), QMessageBox::AcceptRole);
msg.addButton(QString::fromLocal8Bit("不同意"), QMessageBox::RejectRole);
int ret = msg.exec();
if (ret == QMessageBox::AcceptRole)
{
QJsonObject obj_root;
obj_root.insert("agree", true);
document.setObject(obj_root);
agree = true;
DeviceRequest* device_request = new DeviceRequest;//构造函数直接发出去数据了
delete device_request;
}
else
{
QJsonObject obj_root;
obj_root.insert("agree", false);
document.setObject(obj_root);
agree = false;
}
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
file.write(document.toJson());
file.close();
}
}
}
MainWindow::~MainWindow() {
SQLiteHelper sqlite_helper;
sqlite_helper.db.close();
QSqlDatabase::removeDatabase("mydb");
delete icon;
this->setCentralWidget(nullptr);
delete mainWindowLayout;
delete ui;
}