Sélectionner une révision Git
AbstractDocument.hh 2,73 Kio
#ifndef VIVY_ABSTRACT_DOCUMENT_H
#define VIVY_ABSTRACT_DOCUMENT_H
#ifndef __cplusplus
#error "This is a C++ header"
#endif
#include "Utils.hh"
#include "Uuid.hh"
#include <QFileInfo>
#include <QDir>
namespace Vivy
{
class AbstractDocument : public QObject {
Q_OBJECT
VIVY_UNMOVABLE_OBJECT(AbstractDocument)
public:
enum class Type : quint64 {
Vivy = Utils::toUnderlying(Utils::DocumentType::Vivy),
Script = Utils::toUnderlying(Utils::DocumentType::VivyScript)
};
protected:
AbstractDocument(Type childType, const QString &documentName)
: type(childType)
, name(documentName)
{
}
// Automate a part of the rename process, just need to provide a "success"
// callback and the new file info.
void copyWith(const QFileInfo &newFile, auto success)
{
const QFileInfo oldFile(getName());
// Create folder if needed
QDir dirOp;
const QString newAbsDirPath = newFile.dir().absolutePath();
if (!dirOp.exists(newAbsDirPath)) {
qInfo() << "Create folder " << newAbsDirPath;
dirOp.mkpath(newAbsDirPath);
}
if (QFile::copy(oldFile.absoluteFilePath(), newFile.absoluteFilePath()))
return success();
throw std::runtime_error("Failed to copy " + oldFile.absoluteFilePath().toStdString() +
" to " + newFile.absoluteFilePath().toStdString());
}
// Automate a part of the rename process, just need to provide a "success"
// callback and the new file info.
void renameWith(const QFileInfo &newFile, auto success)
{
const QFileInfo oldFile(getName());
// Create folder if needed
QDir dirOp;
const QString newAbsDirPath = newFile.dir().absolutePath();
if (!dirOp.exists(newAbsDirPath)) {
qInfo() << "Create folder " << newAbsDirPath;
dirOp.mkpath(newAbsDirPath);
}
if (dirOp.rename(oldFile.absoluteFilePath(), newFile.absoluteFilePath()))
return success();
throw std::runtime_error("Failed to rename " + oldFile.absoluteFilePath().toStdString() +
" to " + newFile.absoluteFilePath().toStdString());
}
Type type;
QString name{};
const Uuid uuid{};
public:
virtual void copy(const QString &) = 0;
virtual void rename(const QString &) = 0;
virtual void save() = 0;
QString getName() const noexcept { return name; }
Type getType() const noexcept { return type; }
Uuid getUuid() const noexcept { return uuid; }
signals:
void documentChanged();
};
}
bool operator==(const Vivy::AbstractDocument &a, const Vivy::AbstractDocument &b) noexcept;
#endif // VIVY_ABSTRACT_DOCUMENT_H