Skip to content
Extraits de code Groupes Projets
Vérifiée Valider e1a61d80 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

LIB: Reduce the number of lines of code with a copyOrRenameWith utility

parent 76f6952f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!18Implement the VivyDocument specification
......@@ -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;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter