diff --git a/src/Lib/AbstractDocument.hh b/src/Lib/AbstractDocument.hh index 51d951016a85c921bdbc587e1592ee7e6b616c91..40d1d5d946fcb6bcacb98f93419fc0287ba1ea9f 100644 --- a/src/Lib/AbstractDocument.hh +++ b/src/Lib/AbstractDocument.hh @@ -16,22 +16,7 @@ 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) + void copyOrRenameWith(const QFileInfo &newFile, auto action, auto success) { const QFileInfo oldFile(getName()); @@ -43,6 +28,10 @@ protected: dirOp.mkpath(newAbsDirPath); } + if (newFile.absoluteFilePath() == oldFile.absoluteFilePath()) { + throw std::runtime_error("Can't rename or copy a file to itself!"); + } + if (newFile.exists()) { qWarning() << "Deleting the already existing" << newFile; if (!dirOp.remove(newFile.absoluteFilePath())) @@ -50,47 +39,49 @@ protected: newFile.absoluteFilePath().toStdString()); } - if (QFile::copy(oldFile.absoluteFilePath(), newFile.absoluteFilePath())) { + if (action(oldFile.absoluteFilePath(), newFile.absoluteFilePath())) { success(); save(); } - else - throw std::runtime_error("Failed to copy " + oldFile.absoluteFilePath().toStdString() + - " to " + newFile.absoluteFilePath().toStdString()); + else { + throw std::runtime_error("Failed to copy or rename " + + oldFile.absoluteFilePath().toStdString() + " to " + + newFile.absoluteFilePath().toStdString()); + } + } + +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 renameWith(const QFileInfo &newFile, auto success) + 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; - 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(); - } + auto action = [](const QString &oldFile, const QString &newFileName) noexcept -> bool { + return QFile::copy(oldFile, newFileName); + }; + copyOrRenameWith(newFile, action, success); + } - else - throw std::runtime_error("Failed to rename " + - 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) + { + auto action = [](const QString &oldFile, const QString &newFileName) noexcept -> bool { + return QFile::rename(oldFile, newFileName); + }; + copyOrRenameWith(newFile, action, success); } Type type;