Sélectionner une révision Git
AbstractDocument.hh 3,53 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 (newFile.exists()) {
qWarning() << "Deleting the already existing" << newFile;
if (!dirOp.remove(newFile.absoluteFilePath()))
throw std::runtime_error("Failed to remove " +
newFile.absoluteFilePath().toStdString());
}
if (QFile::copy(oldFile.absoluteFilePath(), newFile.absoluteFilePath())) {
success();
save();
}
else
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;