1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 23:23:53 +00:00
openmw/apps/opencs/model/doc/documentmanager.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
3.9 KiB
C++
Raw Normal View History

2012-11-21 16:31:18 +00:00
#include "documentmanager.hpp"
2022-10-19 17:02:00 +00:00
#include <QWaitCondition>
#include <algorithm>
#include <filesystem>
2022-10-19 17:02:00 +00:00
#include <stdexcept>
#include <apps/opencs/model/doc/loader.hpp>
#ifndef Q_MOC_RUN
#include <components/files/configurationmanager.hpp>
#endif
2012-11-21 16:31:18 +00:00
#include "document.hpp"
CSMDoc::DocumentManager::DocumentManager(const Files::ConfigurationManager& configuration)
2018-11-13 19:07:01 +00:00
: mConfiguration(configuration)
, mEncoding(ToUTF8::WINDOWS_1252)
, mFsStrict(false)
{
std::filesystem::path projectPath = configuration.getUserDataPath() / "projects";
if (!std::filesystem::is_directory(projectPath))
std::filesystem::create_directories(projectPath);
2014-04-24 13:09:25 +00:00
mLoader.moveToThread(&mLoaderThread);
mLoaderThread.start();
connect(&mLoader, &Loader::documentLoaded, this, &DocumentManager::documentLoaded);
connect(&mLoader, &Loader::documentNotLoaded, this, &DocumentManager::documentNotLoaded);
connect(this, &DocumentManager::loadRequest, &mLoader, &Loader::loadDocument);
connect(&mLoader, &Loader::nextStage, this, &DocumentManager::nextStage);
connect(&mLoader, &Loader::nextRecord, this, &DocumentManager::nextRecord);
connect(this, &DocumentManager::cancelLoading, &mLoader, &Loader::abortLoading);
connect(&mLoader, &Loader::loadMessage, this, &DocumentManager::loadMessage);
}
2012-11-21 16:31:18 +00:00
CSMDoc::DocumentManager::~DocumentManager()
{
2014-04-24 13:09:25 +00:00
mLoaderThread.quit();
mLoader.stop();
2014-04-24 13:09:25 +00:00
mLoader.hasThingsToDo().wakeAll();
mLoaderThread.wait();
2012-11-21 16:31:18 +00:00
for (std::vector<Document*>::iterator iter(mDocuments.begin()); iter != mDocuments.end(); ++iter)
delete *iter;
}
bool CSMDoc::DocumentManager::isEmpty()
{
return mDocuments.empty();
}
void CSMDoc::DocumentManager::addDocument(
const std::vector<std::filesystem::path>& files, const std::filesystem::path& savePath, bool new_)
2012-11-21 16:31:18 +00:00
{
Document* document = makeDocument(files, savePath, new_);
insertDocument(document);
}
CSMDoc::Document* CSMDoc::DocumentManager::makeDocument(
const std::vector<std::filesystem::path>& files, const std::filesystem::path& savePath, bool new_)
{
return new Document(mConfiguration, files, new_, savePath, mResDir, mEncoding, mBlacklistedScripts, mFsStrict,
mDataPaths, mArchives);
}
2012-11-21 16:31:18 +00:00
void CSMDoc::DocumentManager::insertDocument(CSMDoc::Document* document)
{
2012-11-21 16:31:18 +00:00
mDocuments.push_back(document);
connect(document, SIGNAL(mergeDone(CSMDoc::Document*)), this, SLOT(insertDocument(CSMDoc::Document*)));
emit loadRequest(document);
2014-04-24 13:09:25 +00:00
mLoader.hasThingsToDo().wakeAll();
2012-11-21 16:31:18 +00:00
}
void CSMDoc::DocumentManager::removeDocument(CSMDoc::Document* document)
2012-11-21 16:31:18 +00:00
{
std::vector<Document*>::iterator iter = std::find(mDocuments.begin(), mDocuments.end(), document);
if (iter == mDocuments.end())
throw std::runtime_error("removing invalid document");
2012-11-21 16:31:18 +00:00
emit documentAboutToBeRemoved(document);
2012-11-21 16:31:18 +00:00
mDocuments.erase(iter);
2015-04-21 00:25:19 +00:00
document->deleteLater();
2012-11-21 16:31:18 +00:00
if (mDocuments.empty())
emit lastDocumentDeleted();
}
void CSMDoc::DocumentManager::setResourceDir(const std::filesystem::path& parResDir)
{
mResDir = std::filesystem::absolute(parResDir);
2013-12-26 00:24:43 +00:00
}
2014-04-24 13:09:25 +00:00
void CSMDoc::DocumentManager::setEncoding(ToUTF8::FromType encoding)
{
mEncoding = encoding;
}
void CSMDoc::DocumentManager::setBlacklistedScripts(const std::vector<std::string>& scriptIds)
{
mBlacklistedScripts = scriptIds;
}
2014-04-24 13:09:25 +00:00
void CSMDoc::DocumentManager::documentLoaded(Document* document)
{
emit documentAdded(document);
2014-04-29 12:17:25 +00:00
emit loadingStopped(document, true, "");
2014-04-24 13:09:25 +00:00
}
void CSMDoc::DocumentManager::documentNotLoaded(Document* document, const std::string& error)
{
emit loadingStopped(document, false, error);
if (error.empty()) // do not remove the document yet, if we have an error
removeDocument(document);
}
void CSMDoc::DocumentManager::setFileData(
bool strict, const Files::PathContainer& dataPaths, const std::vector<std::string>& archives)
{
mFsStrict = strict;
mDataPaths = dataPaths;
mArchives = archives;
}