Quarts/Quartz/projectpageppesentor.cpp
Станислав Хозяенок 35b85f7489 Предрелизная версия
2025-07-01 10:26:51 +07:00

208 lines
5.8 KiB
C++

#include "projectpageppesentor.h"
#include "projectpageactivity.h"
#include "settingsrepository.h"
#include "httpproxyserver.h"
#include "QDesktopServices"
#include "models/PasportModel.h"
#include "views/projectstatusfragment.h"
class ProjectPagePesentorModel {
public:
QString& getCurrentPath() {
return currentPath;
}
void setCurrentPath(const QString & path) {
currentPath = path;
}
HttpProxyServer& getHTTPServer() {return *HttpProxyServer::Instance;}
private:
QString currentPath = "";
};
ProjectPagePesentor::ProjectPagePesentor(ProjectPageActivity *activity, QObject *parent)
:QObject(parent), activity(activity), model(new ProjectPagePesentorModel)
{
connect(activity, &ProjectPageActivity::fileClick, this, &ProjectPagePesentor::showPasport);
connect(activity, &ProjectPageActivity::afterDeleteProject, this, &ProjectPagePesentor::updateListItems);
init();
}
void ProjectPagePesentor::updateDirrectory(const QString& dirrectoryPath) {
QString dirrectoryRootPath = SettingsRepository::instance().defaultProjectDirectory();
HttpProxyServer&server = model->getHTTPServer();
QStringList folders {}, files {};
QString relative = getRelativePath(dirrectoryPath, dirrectoryRootPath);
model->setCurrentPath(relative);
if ( relative.startsWith("/") == false) {
relative= "/" + relative;
}
findProjectFiles(dirrectoryRootPath+relative, folders, files);
activity->setProjectFolders(files);
}
QString ProjectPagePesentor::getRelativePath(const QString& fullPath, const QString& baseDir) {
// Приводим пути к каноническому виду
QDir base(baseDir);
QDir full(fullPath);
// Получаем относительный путь
QString relativePath = base.relativeFilePath(fullPath);
if(relativePath == ".") {
return "/";
}
if (relativePath.startsWith("..")) {
qWarning() << "Путь не находится внутри базовой директории";
return "/";
// return fullPath; // или обработай по-своему
}
return relativePath;
}
void ProjectPagePesentor::doActionsOnPathNotSet()
{
throw std::logic_error("Function not implemented");
}
void ProjectPagePesentor::findProjectFiles(const QString&dirrectoryPath, QStringList& folders, QStringList& files) {
QDir dir(dirrectoryPath);
QStringList entries = dir.entryList(QDir::Dirs);
int idx = 1;
if (model->getCurrentPath() == "/") {
idx = 2;
}
for (int idxLenght=entries.length(); idx<idxLenght; ++idx) {
const QString& entry = entries[idx];
QString fullPath = dirrectoryPath + "/" + entry;
QDir subDir(fullPath);
QString pathMathDataCheck = subDir.absoluteFilePath("metadata.json");
QFileInfo metadataFile(pathMathDataCheck);
if (metadataFile.exists()) {
files.append(entry);
} else {
folders.append(entry);
}
}
}
ProjectPagePesentor::~ProjectPagePesentor()
{
delete model;
}
void ProjectPagePesentor::init()
{
updateListItems();
ProjectStatusFragment * fragment = activity->getPasportProject();
if (fragment) {
connect(fragment,&ProjectStatusFragment::showProject, this, &ProjectPagePesentor::fileHandlerAbsolute);
connect(fragment,&ProjectStatusFragment::copyToClipBoardSignal, [this](QString text){});
}
}
void ProjectPagePesentor::folderHandler(QString name)
{
QString path = model->getCurrentPath();
path += "/";
path += name;
model->setCurrentPath(path);
updateDirrectory(SettingsRepository::instance().defaultProjectDirectory() +"/" + path);
}
void ProjectPagePesentor::showPasport(QString name)
{
QString defaultProjectDirrectory = SettingsRepository::instance().defaultProjectDirectory();
QString currentPath = defaultProjectDirrectory;
currentPath += model->getCurrentPath();
QString projectPath = "/";
projectPath += name;
currentPath += projectPath;
auto relative = getRelativePath(currentPath, defaultProjectDirrectory);
QString absolutePath = defaultProjectDirrectory +"/" + relative;
PasportModel model;
model.setName(name);
model.setPath(absolutePath);
activity->updatePasport(model);
}
void ProjectPagePesentor::fileHandler(QString name)
{
QString defaultProjectDirrectory = SettingsRepository::instance().defaultProjectDirectory();
QString currentPath = defaultProjectDirrectory;
currentPath += model->getCurrentPath();
QString projectPath = "/";
projectPath += name;
currentPath += projectPath;
auto relative = getRelativePath(currentPath, defaultProjectDirrectory);
if (relative.startsWith("/") == false){
relative= "/" + relative;
}
showByRelative(relative);
}
void ProjectPagePesentor::fileHandlerAbsolute(QString currentPath)
{
QString defaultProjectDirrectory = SettingsRepository::instance().defaultProjectDirectory();
auto relative = getRelativePath(currentPath, defaultProjectDirrectory);
if (relative.startsWith("/") == false){
relative= "/" + relative;
}
showByRelative(relative);
}
void ProjectPagePesentor::updateListItems()
{
QString dirrectoryPath = SettingsRepository::instance().defaultProjectDirectory();
if (dirrectoryPath == "") {
throw std::logic_error("Function not implemented");
}
updateDirrectory(dirrectoryPath);
}
void ProjectPagePesentor::showByRelative(QString relative){
HttpProxyServer &srv = *HttpProxyServer::Instance;
qint16 port = srv.getLocalProt();
QString pathUrl = QString("http://127.0.0.1") + ":" + QString::number(port) + "/viewer";
if (relative == "/") return;
if (relative.startsWith("/") == false){
relative= "/" + relative;
}
srv.setProjectPath(relative);
QUrl url(pathUrl);
QDesktopServices::openUrl(url);
}