2012-11-21 16:31:18 +00:00
|
|
|
#include "documentmanager.hpp"
|
|
|
|
|
2013-09-27 09:36:06 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2013-09-27 13:24:58 +00:00
|
|
|
#ifndef Q_MOC_RUN
|
|
|
|
#include <components/files/configurationmanager.hpp>
|
|
|
|
#endif
|
|
|
|
|
2012-11-21 16:31:18 +00:00
|
|
|
#include "document.hpp"
|
|
|
|
|
2013-09-27 13:24:58 +00:00
|
|
|
CSMDoc::DocumentManager::DocumentManager (const Files::ConfigurationManager& configuration)
|
2018-11-13 19:07:01 +00:00
|
|
|
: mConfiguration (configuration), mEncoding (ToUTF8::WINDOWS_1252), mFsStrict(false)
|
2013-09-27 09:36:06 +00:00
|
|
|
{
|
2013-12-26 00:24:43 +00:00
|
|
|
boost::filesystem::path projectPath = configuration.getUserDataPath() / "projects";
|
2013-09-27 13:24:58 +00:00
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory (projectPath))
|
|
|
|
boost::filesystem::create_directories (projectPath);
|
2014-04-24 13:09:25 +00:00
|
|
|
|
|
|
|
mLoader.moveToThread (&mLoaderThread);
|
|
|
|
mLoaderThread.start();
|
|
|
|
|
|
|
|
connect (&mLoader, SIGNAL (documentLoaded (Document *)),
|
|
|
|
this, SLOT (documentLoaded (Document *)));
|
|
|
|
connect (&mLoader, SIGNAL (documentNotLoaded (Document *, const std::string&)),
|
|
|
|
this, SLOT (documentNotLoaded (Document *, const std::string&)));
|
2014-04-29 12:27:44 +00:00
|
|
|
connect (this, SIGNAL (loadRequest (CSMDoc::Document *)),
|
|
|
|
&mLoader, SLOT (loadDocument (CSMDoc::Document *)));
|
2014-05-03 13:33:35 +00:00
|
|
|
connect (&mLoader, SIGNAL (nextStage (CSMDoc::Document *, const std::string&, int)),
|
|
|
|
this, SIGNAL (nextStage (CSMDoc::Document *, const std::string&, int)));
|
2014-06-26 09:41:21 +00:00
|
|
|
connect (&mLoader, SIGNAL (nextRecord (CSMDoc::Document *, int)),
|
|
|
|
this, SIGNAL (nextRecord (CSMDoc::Document *, int)));
|
2014-05-03 14:44:50 +00:00
|
|
|
connect (this, SIGNAL (cancelLoading (CSMDoc::Document *)),
|
|
|
|
&mLoader, SLOT (abortLoading (CSMDoc::Document *)));
|
2014-05-10 11:18:40 +00:00
|
|
|
connect (&mLoader, SIGNAL (loadMessage (CSMDoc::Document *, const std::string&)),
|
|
|
|
this, SIGNAL (loadMessage (CSMDoc::Document *, const std::string&)));
|
2013-09-27 09:36:06 +00:00
|
|
|
}
|
2012-11-21 16:31:18 +00:00
|
|
|
|
|
|
|
CSMDoc::DocumentManager::~DocumentManager()
|
|
|
|
{
|
2014-04-24 13:09:25 +00:00
|
|
|
mLoaderThread.quit();
|
2016-06-10 16:10:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-29 22:37:58 +00:00
|
|
|
bool CSMDoc::DocumentManager::isEmpty()
|
|
|
|
{
|
|
|
|
return mDocuments.empty();
|
|
|
|
}
|
|
|
|
|
2014-04-21 07:02:58 +00:00
|
|
|
void CSMDoc::DocumentManager::addDocument (const std::vector<boost::filesystem::path>& files, const boost::filesystem::path& savePath,
|
2013-02-04 12:46:54 +00:00
|
|
|
bool new_)
|
2012-11-21 16:31:18 +00:00
|
|
|
{
|
2015-08-16 13:24:48 +00:00
|
|
|
Document *document = makeDocument (files, savePath, new_);
|
|
|
|
insertDocument (document);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSMDoc::Document *CSMDoc::DocumentManager::makeDocument (
|
|
|
|
const std::vector< boost::filesystem::path >& files,
|
|
|
|
const boost::filesystem::path& savePath, bool new_)
|
|
|
|
{
|
2017-08-22 02:31:19 +00:00
|
|
|
return new Document (mConfiguration, files, new_, savePath, mResDir, &mFallbackMap, mEncoding, mBlacklistedScripts, mFsStrict, mDataPaths, mArchives);
|
2015-08-16 13:24:48 +00:00
|
|
|
}
|
2012-11-21 16:31:18 +00:00
|
|
|
|
2015-08-16 13:24:48 +00:00
|
|
|
void CSMDoc::DocumentManager::insertDocument (CSMDoc::Document *document)
|
|
|
|
{
|
2012-11-21 16:31:18 +00:00
|
|
|
mDocuments.push_back (document);
|
|
|
|
|
2015-08-16 13:24:48 +00:00
|
|
|
connect (document, SIGNAL (mergeDone (CSMDoc::Document*)),
|
|
|
|
this, SLOT (insertDocument (CSMDoc::Document*)));
|
|
|
|
|
2014-04-29 12:27:44 +00:00
|
|
|
emit loadRequest (document);
|
2014-04-24 13:09:25 +00:00
|
|
|
|
|
|
|
mLoader.hasThingsToDo().wakeAll();
|
2012-11-21 16:31:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 07:39:39 +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())
|
2014-04-26 11:11:27 +00:00
|
|
|
throw std::runtime_error ("removing invalid document");
|
2012-11-21 16:31:18 +00:00
|
|
|
|
2015-08-06 10:52:10 +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
|
|
|
|
2014-04-26 11:11:27 +00:00
|
|
|
if (mDocuments.empty())
|
|
|
|
emit lastDocumentDeleted();
|
2013-10-17 16:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMDoc::DocumentManager::setResourceDir (const boost::filesystem::path& parResDir)
|
|
|
|
{
|
|
|
|
mResDir = boost::filesystem::system_complete(parResDir);
|
2013-12-26 00:24:43 +00:00
|
|
|
}
|
2014-04-24 13:09:25 +00:00
|
|
|
|
2016-01-06 11:58:36 +00:00
|
|
|
void CSMDoc::DocumentManager::setFallbackMap(const std::map<std::string, std::string>& fallbackMap)
|
|
|
|
{
|
|
|
|
mFallbackMap = Fallback::Map(fallbackMap);
|
|
|
|
}
|
|
|
|
|
2014-05-12 08:32:57 +00:00
|
|
|
void CSMDoc::DocumentManager::setEncoding (ToUTF8::FromType encoding)
|
|
|
|
{
|
|
|
|
mEncoding = encoding;
|
|
|
|
}
|
|
|
|
|
2014-07-21 10:15:21 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-05-06 07:39:39 +00:00
|
|
|
emit loadingStopped (document, false, error);
|
|
|
|
|
|
|
|
if (error.empty()) // do not remove the document yet, if we have an error
|
|
|
|
removeDocument (document);
|
2015-03-19 16:49:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 02:31:19 +00:00
|
|
|
void CSMDoc::DocumentManager::setFileData(bool strict, const Files::PathContainer& dataPaths, const std::vector<std::string>& archives)
|
2015-03-19 16:49:41 +00:00
|
|
|
{
|
2017-08-22 02:31:19 +00:00
|
|
|
mFsStrict = strict;
|
|
|
|
mDataPaths = dataPaths;
|
|
|
|
mArchives = archives;
|
2015-03-19 16:49:41 +00:00
|
|
|
}
|