Sélectionner une révision Git
-
Kubat a rédigé
- Make this for CCLS to be happy - Include this "precompiled header" everywhere - Make includes from the src folder, and leave all ambiguity
Kubat a rédigé- Make this for CCLS to be happy - Include this "precompiled header" everywhere - Make includes from the src folder, and leave all ambiguity
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;
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;
dirOp.remove(newFile.absoluteFilePath());
if (!dirOp.remove(newFile.absoluteFilePath()))
throw std::runtime_error("Failed to remove " +
newFile.absoluteFilePath().toStdString());
}
if (dirOp.rename(oldFile.absoluteFilePath(), newFile.absoluteFilePath())) {
success();
save();
}
else
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