File indexing completed on 2024-05-19 04:48:24

0001 #include "cmakeprojectmanager.h"
0002 
0003 #include <QDebug>
0004 #include <QDir>
0005 #include <QJsonObject>
0006 
0007 #include <MauiKit3/FileBrowsing/fmstatic.h>
0008 
0009 #include "cmakeapi.h"
0010 #include "cmakedata.h"
0011 
0012 #include "controllers/cmakeproject.h"
0013 #include "controllers/processes/configureprocess.h"
0014 #include "controllers/projectmanager.h"
0015 #include "controllers/projectpreferences.h"
0016 #include "processmanager.h"
0017 
0018 #include "models/cmakeprojectsmodel.h"
0019 
0020 CMakeProjectManager::CMakeProjectManager(ProjectManager *parent) : QObject(parent)
0021 ,m_projectsModel(new CMakeProjectsModel(this))
0022 ,m_project(new CMakeProject(this))
0023 ,m_process(new ProcessManager(m_project))
0024 ,m_root(parent) //root project
0025 {
0026   connect(this, &CMakeProjectManager::statusChanged, [this](Status status)
0027   {
0028     if(status == Status::Ready)
0029       {
0030         this->readIndexReply();
0031       }
0032   });
0033 
0034   connect(m_process, &ProcessManager::configureStatusChanged, [this](ProcessManager::Status status)
0035   {
0036     switch(status)
0037       {
0038       case ProcessManager::Status::Running:
0039         this->setStatus(Status::Loading);
0040         break;
0041       case ProcessManager::Status::Finished:
0042         this->setStatus(Status::Ready);
0043         break;
0044       case ProcessManager::Status::Error:
0045         this->setStatus(Status::Error);
0046         break;
0047       default:
0048         this->setStatus(Status::Error);
0049         break;
0050       }
0051 
0052   });
0053 }
0054 
0055 void CMakeProjectManager::init()
0056 {
0057   this->initBuildDir(); //first create needed build directory
0058   this->initServer(); //init the server then
0059   this->initConfigure(); //run configuration to get server replies
0060 }
0061 
0062 CMakeProjectsModel *CMakeProjectManager::projectsModel() const
0063 {
0064   return m_projectsModel;
0065 }
0066 
0067 ProcessManager *CMakeProjectManager::process() const
0068 {
0069   return m_process;
0070 }
0071 
0072 CMakeProject *CMakeProjectManager::project() const
0073 {
0074   return m_project;
0075 }
0076 
0077 ProjectManager *CMakeProjectManager::root()
0078 {
0079   return m_root;
0080 }
0081 
0082 CMakeProjectManager::Status CMakeProjectManager::status() const
0083 {
0084   return m_status;
0085 }
0086 
0087 void CMakeProjectManager::initServer()
0088 {
0089   CMake::FileApi::writeClientQueryFile(m_root->preferences()->buildDir().toLocalFile());
0090 }
0091 
0092 void CMakeProjectManager::initConfigure()
0093 {
0094   m_process->configure ();
0095 }
0096 
0097 void CMakeProjectManager::initBuildDir()
0098 {
0099   const QDir dir(m_root->preferences()->buildDir().toLocalFile());
0100   if(!dir.exists())
0101     {
0102       if(!dir.mkpath("."))
0103         {
0104           qWarning() << "Working Build directory could not be created at << " << dir.absolutePath();
0105           return;
0106         }
0107     }
0108 }
0109 
0110 void CMakeProjectManager::readIndexReply()
0111 {
0112   const auto sourceDir = m_root->projectPath().toLocalFile();
0113   const auto buildDir = m_root->preferences()->buildDir().toLocalFile();
0114 
0115   auto indexResponse = CMake::FileApi::findReplyIndexFile(buildDir);
0116 
0117   qDebug() << sourceDir << buildDir;
0118   qDebug() << indexResponse.keys();
0119 
0120   auto projects = CMake::FileApi::parseReplyIndexFile(indexResponse,sourceDir, buildDir);
0121 
0122   m_projectsModel->setProjectsData(projects); //parent each project to the model
0123 
0124   if(!projects.isEmpty ())
0125     {
0126       m_project->setData (projects.first ());
0127     }
0128 }
0129 
0130 void CMakeProjectManager::setStatus(const CMakeProjectManager::Status &status)
0131 {
0132   if(m_status == status)
0133     {
0134       return;
0135     }
0136 
0137   m_status = status;
0138   emit statusChanged(m_status);
0139 }