DPS/DataPlatform/mainwindow.cpp

314 lines
9.7 KiB
C++
Raw Permalink Normal View History

2025-06-23 10:41:33 +08:00
#pragma execution_character_set("utf-8")
#include "mainwindow.h"
#include "QWebEngineView"
#include "QVBoxLayout"
#include <QtWidgets>
#include <iostream>
//#include <QApplication>
#include <QWebEngineView.h>
#include "TitleBar.h"
#include "SideBar.h"
//#include "ProjectManager.h"
//#include "CodeGenerate.h"
//#include "DataModelListView.h"
//#include "DataModelGraphView.h"
#include "SystemSettingModule.h"
#include "Session.h"
#include "SettingDialog.h"
#include "Base.h"
#include "HomePage.h"
#include "ProjectEventModule.h"
#include "DataModelEventModule.h"
#include "DataPackEventModule.h"
#include "DataManager.h"
#include "CodeGenerateEventModule.h"
#include"SearchEditEventModule.h"
#include "XmlEditEventModule.h"
#include "easylogging++.h"
INITIALIZE_EASYLOGGINGPP
//#define WEBDEGUB
bool CanFileWritable(QString fileName)
{
bool bret = false;
#ifdef Q_OS_WIN
DWORD length = 0;
TCHAR* c_str = nullptr;
if (sizeof(TCHAR) == 1)
c_str = (TCHAR*)fileName.toLocal8Bit().constData();
else
c_str = (TCHAR*)fileName.utf16();
TCHAR* tfn = nullptr;
if (!::GetFileSecurity(c_str, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION, NULL, NULL, &length) &&
ERROR_INSUFFICIENT_BUFFER == ::GetLastError()) {
PSECURITY_DESCRIPTOR security = static_cast<PSECURITY_DESCRIPTOR>(::malloc(length));
if (security && ::GetFileSecurity(c_str, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION, security, length, &length)) {
HANDLE hToken = NULL;
if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_IMPERSONATE | TOKEN_QUERY |
TOKEN_DUPLICATE | STANDARD_RIGHTS_READ, &hToken)) {
HANDLE hImpersonatedToken = NULL;
if (::DuplicateToken(hToken, SecurityImpersonation, &hImpersonatedToken)) {
GENERIC_MAPPING mapping = { 0xFFFFFFFF };
PRIVILEGE_SET privileges = { 0 };
DWORD grantedAccess = 0, privilegesLength = sizeof(privileges);
BOOL result = FALSE;
mapping.GenericRead = FILE_GENERIC_READ | FILE_GENERIC_READ;
mapping.GenericWrite = FILE_GENERIC_WRITE;
mapping.GenericExecute = FILE_GENERIC_EXECUTE;
mapping.GenericAll = FILE_ALL_ACCESS;
DWORD genericAccessRights = FILE_GENERIC_WRITE;
::MapGenericMask(&genericAccessRights, &mapping);
if (::AccessCheck(security, hImpersonatedToken, genericAccessRights,
&mapping, &privileges, &privilegesLength, &grantedAccess, &result))
{
if (result == TRUE) {
if (((grantedAccess & GENERIC_WRITE) == GENERIC_WRITE)
|| ((grantedAccess & FILE_GENERIC_WRITE) == FILE_GENERIC_WRITE))
bret = true;
}
}
::CloseHandle(hImpersonatedToken);
}
::CloseHandle(hToken);
}
::free(security);
}
}
#else
bret = true;
#endif
return QFileInfo(fileName).isWritable() && QFileInfo(fileName).isReadable() && bret;
}
//#define WEBDEGUB
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), _webView(nullptr)
{
LOG(INFO) << CommonHelper::utf8ToStdString("---------DPS is begin!-------------");
#ifdef WEBDEGUB
_webView = new WebEngineView(QUrl(BaseUrl + "#/debug/SearchEditModule/save-data-to-file"), this);
#else
QString indexHtmlPath = QCoreApplication::applicationDirPath() + "/" + "html/index.html";
_webView = new WebEngineView(QUrl::fromLocalFile(indexHtmlPath), this);
2025-06-23 10:41:33 +08:00
#endif // WEBDEGUB
this->setCentralWidget(_webView);
_eventModule = new SignalEventModule("MainWindow");
QObject::connect(_eventModule, &SignalEventModule::onmessage, this, &MainWindow::onMessage);
_webView->addEventModule(_eventModule);
// _pDataRoot = new DataManager();
DataManager& root = GetDataRoot();
2025-06-23 18:01:09 +08:00
//初始化Session数据。
2025-06-23 10:41:33 +08:00
Session::_session = new Session(this);
Session::_session->_titleBar = new TitleBar(this);
_webView->addEventModule(Session::getSession()->titleBar());
Session::_session->_sideBar = new SideBar(this);
_webView->addEventModule(Session::getSession()->sideBar());
// Session::_session->_projectManager = new ProjectManager((QMainWindow*)this);
// eventDispatch->addEventModule(Session::getSession()->projectManager());
Session::_session->_homePage = new HomePage((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->homePage());
Session::_session->_codeGenerate = new CodeGenerateEventModule((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->codeGenerate());
_webView->addEventModule(new SystemSettingModule(nullptr, &root));
Session::_session->_projectEventModule = new ProjectEventModule((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->projectEventModule());
Session::_session->_dataModelEventModule = new DataModelEventModule((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->dataModelEventModule());
Session::_session->_dataPackEvenModule = new DataPackEventModule((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->dataPackEventModule());
Session::_session->_searchEditModule = new SearchEditEventModule((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->searchEditEventModule());
Session::_session->_xmlEditModule = new XmlEditEventModule((QMainWindow*)this, &root);
_webView->addEventModule(Session::getSession()->xmlEditEventModule());
#ifdef Q_OS_WIN
_frameless = std::move(std::unique_ptr<QFramelessHelper>(new QFramelessHelper(this, _webView, true, (QMainWindow*)this)));
#endif
}
MainWindow::~MainWindow()
{
}
void MainWindow::Close()
{
if (Session::_session->_codeGenerate)
{
Session::_session->_codeGenerate->closeCodeDisplay();
}
this->close();
}
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, qintptr* result)
#else
bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
#endif
{
if (_frameless && _frameless->nativeEvent(eventType, message, result))
return true;
return QMainWindow::nativeEvent(eventType, message, result);
}
bool MainWindow::onMessage(const std::string& eventName, json& parameter)
{
LOG(INFO) << CommonHelper::utf8ToStdString("MainWindow receive message.......");
LOG(INFO) << eventName;
LOG(INFO) << CommonHelper::jsonToString(parameter);
TRY{
if (eventName == "opened") {
initalApplication();
return true;
}
else if (eventName == "db-connect")
{
DBConnect(parameter);
return true;
}
}
CATCH(parameter);
return false;
}
void MainWindow::message(const QString& message, int type)
{
json j;
j["value"] = CommonHelper::qstringToUtf8(message);
j["type"] = type;
_eventModule->send("message", j);
}
void MainWindow::initalApplication()
{
changeModule("Homepage");
}
void MainWindow::changeModule(const std::string& moduleName)
{
Session::getSession()->sideBar()->changeModule(moduleName);
}
void MainWindow::changeMaximize()
{
if (this->isMaximized())
this->showNormal();
else
this->showMaximized();
Session::getSession()->titleBar()->send("maximized", this->isMaximized());
}
void MainWindow::changeEvent(QEvent* event)
{
if (event->type() != QEvent::WindowStateChange) return;
Session::getSession()->titleBar()->send("maximized", this->isMaximized());
}
void MainWindow::openSettingDialog()
{
_settingDialog = std::move(std::unique_ptr<SettingDialog>(new SettingDialog(this)));
_settingDialog->setMinimumSize(800, 600);
_settingDialog->setModal(true);
_settingDialog->show();
}
bool MainWindow::DBConnect(json& parameter)
{
using namespace DBPlatformSpace;
ResultMsg rm;
auto data = parameter["data"];
// std::string sysDBPath = data["sysDBPath"];
QString sysDBPath = CommonHelper::utf8ToQString(data["sysDBPath"]);
DataManager& dataRoot = GetDataRoot();
QString dbFile;
if (sysDBPath == "")
{
SysManager& sysMgr = GetSysManager();
dbFile = sysMgr._sysDBPath;
}
else
{
dbFile = sysDBPath;
}
2025-06-23 18:01:09 +08:00
//是否回滚失败后重启
2025-06-23 10:41:33 +08:00
QString tempFile = QCoreApplication::applicationDirPath() + "/rollback.txt";
QFile rollBackFile(tempFile);
if (rollBackFile.exists())
{
if (rollBackFile.open(QIODevice::ReadOnly))
{
2025-06-23 18:01:09 +08:00
message("导入模型出错,程序已重启");
2025-06-23 10:41:33 +08:00
QTextStream stream(&rollBackFile);
QStringList list = stream.readAll().split(",");
QString tmpFile = list.at(0);
LOG(INFO) << CommonHelper::qstringToStdString(tmpFile);
rollBackFile.close();
bool ret = QFile::remove(tmpFile);
ret = QFile::rename(tmpFile + ".bak", dbFile);
if (!ret)
{
LOG(INFO) << "rename failed";
}
LOG(INFO) << "remove and rename " << CommonHelper::qstringToStdString(dbFile);
QFile::remove(tempFile);
}
}
QString msg = "MainWindow::DBConnect: DBFile:" + dbFile;
LOG(INFO) << CommonHelper::qstringToString(msg);
QFile file(dbFile);
if (!file.exists() || !CanFileWritable(dbFile))
{
2025-06-23 18:01:09 +08:00
//dbp文件不存在
message("数据库文件不存在或读写权限不完整,建议管理员权限", 2);
LOG(ERROR) << CommonHelper::utf8ToStdString("MainWindow::DBConnect no dbFile或 db不具写权限");
2025-06-23 10:41:33 +08:00
_eventModule->broadcast("connect-status-change", json({ {"status",false} }));
return false;
}
if (dataRoot.pTheDBPlatform)
{
delete dataRoot.pTheDBPlatform;
dataRoot.pTheDBPlatform = nullptr;
dataRoot.clearAllProject();
}
try {
dataRoot.pTheDBPlatform = new DBPlatformSpace::DBPlatformNew(CommonHelper::qstringToStdString(dbFile));
}
catch (...) {
2025-06-23 18:01:09 +08:00
parameter["error"] = "连接数据库文件失败";
2025-06-23 10:41:33 +08:00
return false;
}
string st = DBPlatformNew::getCurrPlatformStatus();
if (st == "OK")
{
dataRoot._dbConnected = true;
LOG(INFO) << CommonHelper::utf8ToStdString("MainWindow DBConnect::DBPlatformNew connect success");
_eventModule->broadcast("connect-status-change", json({ {"status",true} }));
return true;
}
LOG(INFO) << CommonHelper::utf8ToStdString("MainWindow DBConnect::DBPlatformNew connect failed");
_eventModule->broadcast("connect-status-change", json({ {"status",false} }));
return false;
}