2014-04-24 13:09:25 +00:00
|
|
|
|
|
|
|
#include "loader.hpp"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include "document.hpp"
|
|
|
|
|
2014-05-03 13:05:02 +00:00
|
|
|
CSMDoc::Loader::Stage::Stage() : mFile (0), mRecordsLeft (false) {}
|
2014-05-03 10:07:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 13:09:25 +00:00
|
|
|
CSMDoc::Loader::Loader()
|
|
|
|
{
|
|
|
|
QTimer *timer = new QTimer (this);
|
|
|
|
|
|
|
|
connect (timer, SIGNAL (timeout()), this, SLOT (load()));
|
2014-05-03 13:05:02 +00:00
|
|
|
timer->start();
|
2014-04-24 13:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QWaitCondition& CSMDoc::Loader::hasThingsToDo()
|
|
|
|
{
|
|
|
|
return mThingsToDo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMDoc::Loader::load()
|
|
|
|
{
|
|
|
|
if (mDocuments.empty())
|
|
|
|
{
|
|
|
|
mMutex.lock();
|
|
|
|
mThingsToDo.wait (&mMutex);
|
|
|
|
mMutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:07:05 +00:00
|
|
|
std::vector<std::pair<Document *, Stage> >::iterator iter = mDocuments.begin();
|
2014-04-24 13:09:25 +00:00
|
|
|
|
|
|
|
Document *document = iter->first;
|
|
|
|
|
2014-05-03 10:07:05 +00:00
|
|
|
int size = static_cast<int> (document->getContentFiles().size());
|
|
|
|
|
|
|
|
if (document->isNew())
|
|
|
|
--size;
|
|
|
|
|
|
|
|
bool done = false;
|
2014-04-24 13:09:25 +00:00
|
|
|
|
2014-05-03 13:33:35 +00:00
|
|
|
const int batchingSize = 100;
|
|
|
|
|
2014-04-24 13:09:25 +00:00
|
|
|
try
|
|
|
|
{
|
2014-05-03 13:05:02 +00:00
|
|
|
if (iter->second.mRecordsLeft)
|
|
|
|
{
|
2014-05-03 13:33:35 +00:00
|
|
|
for (int i=0; i<batchingSize; ++i) // do not flood the system with update signals
|
|
|
|
if (document->getData().continueLoading())
|
|
|
|
{
|
|
|
|
iter->second.mRecordsLeft = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit nextRecord (document);
|
2014-05-03 13:05:02 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:07:05 +00:00
|
|
|
if (iter->second.mFile<size)
|
|
|
|
{
|
2014-05-03 11:01:29 +00:00
|
|
|
boost::filesystem::path path = document->getContentFiles()[iter->second.mFile];
|
|
|
|
|
2014-05-03 13:33:35 +00:00
|
|
|
int steps = document->getData().startLoading (path, iter->second.mFile<size-1, false);
|
2014-05-03 13:05:02 +00:00
|
|
|
iter->second.mRecordsLeft = true;
|
2014-05-03 13:33:35 +00:00
|
|
|
|
|
|
|
emit nextStage (document, path.filename().string(), steps/batchingSize);
|
2014-05-03 10:07:05 +00:00
|
|
|
}
|
|
|
|
else if (iter->second.mFile==size)
|
|
|
|
{
|
2014-05-03 13:33:35 +00:00
|
|
|
int steps = document->getData().startLoading (document->getProjectPath(), false, true);
|
2014-05-03 13:05:02 +00:00
|
|
|
iter->second.mRecordsLeft = true;
|
2014-05-03 13:33:35 +00:00
|
|
|
|
|
|
|
emit nextStage (document, "Project File", steps/batchingSize);
|
2014-05-03 10:07:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
++(iter->second.mFile);
|
2014-04-24 13:09:25 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2014-05-03 10:07:05 +00:00
|
|
|
mDocuments.erase (iter);
|
2014-04-24 13:09:25 +00:00
|
|
|
emit documentNotLoaded (document, e.what());
|
2014-05-03 10:07:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
{
|
|
|
|
mDocuments.erase (iter);
|
|
|
|
emit documentLoaded (document);
|
2014-04-24 13:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:27:44 +00:00
|
|
|
void CSMDoc::Loader::loadDocument (CSMDoc::Document *document)
|
2014-04-24 13:09:25 +00:00
|
|
|
{
|
2014-05-03 10:07:05 +00:00
|
|
|
mDocuments.push_back (std::make_pair (document, Stage()));
|
2014-04-24 13:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:44:50 +00:00
|
|
|
void CSMDoc::Loader::abortLoading (CSMDoc::Document *document)
|
2014-04-24 13:09:25 +00:00
|
|
|
{
|
2014-05-03 10:07:05 +00:00
|
|
|
for (std::vector<std::pair<Document *, Stage> >::iterator iter = mDocuments.begin();
|
2014-04-24 13:09:25 +00:00
|
|
|
iter!=mDocuments.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (iter->first==document)
|
|
|
|
{
|
|
|
|
mDocuments.erase (iter);
|
|
|
|
emit documentNotLoaded (document, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 14:44:50 +00:00
|
|
|
}
|