File: documentmanager.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (123 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "documentmanager.hpp"

#include <QWaitCondition>

#include <algorithm>
#include <filesystem>
#include <stdexcept>

#include <apps/opencs/model/doc/loader.hpp>

#ifndef Q_MOC_RUN
#include <components/files/configurationmanager.hpp>
#endif

#include "document.hpp"

CSMDoc::DocumentManager::DocumentManager(const Files::ConfigurationManager& configuration)
    : mConfiguration(configuration)
    , mEncoding(ToUTF8::WINDOWS_1252)
{
    std::filesystem::path projectPath = configuration.getUserDataPath() / "projects";

    if (!std::filesystem::is_directory(projectPath))
        std::filesystem::create_directories(projectPath);

    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);
}

CSMDoc::DocumentManager::~DocumentManager()
{
    mLoaderThread.quit();
    mLoader.stop();
    mLoader.hasThingsToDo().wakeAll();
    mLoaderThread.wait();

    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_)
{
    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, mDataPaths, mArchives);
}

void CSMDoc::DocumentManager::insertDocument(CSMDoc::Document* document)
{
    mDocuments.push_back(document);

    connect(document, SIGNAL(mergeDone(CSMDoc::Document*)), this, SLOT(insertDocument(CSMDoc::Document*)));

    emit loadRequest(document);

    mLoader.hasThingsToDo().wakeAll();
}

void CSMDoc::DocumentManager::removeDocument(CSMDoc::Document* document)
{
    std::vector<Document*>::iterator iter = std::find(mDocuments.begin(), mDocuments.end(), document);

    if (iter == mDocuments.end())
        throw std::runtime_error("removing invalid document");

    emit documentAboutToBeRemoved(document);

    mDocuments.erase(iter);
    document->deleteLater();

    if (mDocuments.empty())
        emit lastDocumentDeleted();
}

void CSMDoc::DocumentManager::setResourceDir(const std::filesystem::path& parResDir)
{
    mResDir = std::filesystem::absolute(parResDir);
}

void CSMDoc::DocumentManager::setEncoding(ToUTF8::FromType encoding)
{
    mEncoding = encoding;
}

void CSMDoc::DocumentManager::documentLoaded(Document* document)
{
    emit documentAdded(document);
    emit loadingStopped(document, true, "");
}

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(
    const Files::PathContainer& dataPaths, const std::vector<std::string>& archives)
{
    mDataPaths = dataPaths;
    mArchives = archives;
}