Sélectionner une révision Git
channels_controller.rb
VivyDocumentStore.cc 1,91 Kio
#include "VivyDocumentStore.hh"
#include "VivyDocument.hh"
#include <stdexcept>
std::weak_ptr<VivyDocument>
VivyDocumentStore::loadDocument(const QString &file)
{
QFileInfo fileinfo(file);
QString baseName = fileinfo.baseName();
if (documents.count(baseName) > 0) {
qCritical() << "A document with the same name " << baseName << " was already loaded!";
throw std::runtime_error("The document was already opened");
}
if (VivyDocument *ret = VivyDocument::fromPath(file)) {
qDebug() << "Register document " << baseName;
documents[baseName] = std::shared_ptr<VivyDocument>(ret);
return std::weak_ptr{ documents[baseName] };
} else {
qDebug() << "Failed to create document from " << file;
throw std::runtime_error("Failed to create the document");
}
}
bool
VivyDocumentStore::isDocumentPresent(const QString &name) noexcept
{
return documents.count(name) >= 1;
}
std::weak_ptr<VivyDocument>
VivyDocumentStore::newDocument()
{
QString newDocName = newDocumentBaseName + QString::number(newDocumentNumber++);
if (auto ret = std::make_shared<VivyDocument>(newDocName)) {
qDebug() << "Create new document " << newDocName;
documents[newDocName] = ret;
return std::weak_ptr{ documents[newDocName] };
} else {
qDebug() << "Failed to create new document " << newDocName;
throw std::runtime_error("Failed to create the document");
}
}
void
VivyDocumentStore::closeDocument(const QString &name) noexcept
{
qDebug() << "Store is closing the document " << name;
documents.remove(name);
}
std::weak_ptr<VivyDocument>
VivyDocumentStore::getDocument(const QString &name) const
{
if (!documents.contains(name)) {
qCritical() << "Couldn't find the document " << name;
throw std::runtime_error("Can't find the document");
}
return std::weak_ptr<VivyDocument>{ documents.value(name) };
}