diff --git a/src/autosave.cpp b/src/autosave.cpp index 50a07bf0b520cbc71829f5cfb74777bdf3e92d25..7e293b6cdba0f1954a881a3138e3651249dc11d7 100644 --- a/src/autosave.cpp +++ b/src/autosave.cpp @@ -10,23 +10,17 @@ Autosave::Autosave(Petrack &petrack) : mPetrack(petrack) { mTimer = new QTimer{this}; connect(mTimer, &QTimer::timeout, this, &Autosave::savePet); - // check so it doesn't autosave while the user is in the dialog where they can decide to load an existing - // autosave - // TODO: Should I ever start it here or just delete this call entirely? - if(!checkAutosave()) - { - startTimer(); - } } Autosave::~Autosave() { - // NOTE: Currently also deletes on Keyboard Interrupt (Ctrl + C) - // Replace by Method call on closeEvent in Petrack to circumvent this? + // NOTE: Currently this would also delete on Keyboard Interrupt (Ctrl + C) + // Replaced by Method call in closeEvent in Petrack to circumvent this // if Project-Object from Issue 88 gets implemented, // Autosave should be a Member of the project, not Petrack - deleteAutosave(); + // then the destructor could maybe be used to delete the autosave + // deleteAutosave(); } void Autosave::trackPersonModified() @@ -40,11 +34,18 @@ void Autosave::trackPersonModified() } } +/** + * @brief Checks if autosave for currently loaded project exists + * @return true if autosave file exists, else false + */ bool Autosave::checkAutosave() { return !getAutosave().empty(); } +/** + * @brief Delete all autosave files for currently loaded project (.pet and .trc) + */ void Autosave::deleteAutosave() { auto autosaves = getAutosave(); @@ -58,8 +59,13 @@ void Autosave::deleteAutosave() } } +/** + * @brief Loads autosave files for currently loaded project + */ void Autosave::loadAutosave() { + // TODO: Rename autosave to name from save and then load it?!?! Needs to have a different name than autosave, or + // does it? auto autosaveFiles = getAutosave(); if(autosaveFiles.empty()) { @@ -85,9 +91,13 @@ void Autosave::loadAutosave() } } +/** + * @brief Returns whether a given file is an autosave + * @param file file to test + * @return true when autosave, else false; if file does not exist, false + */ bool Autosave::isAutosave(const QString &file) { - // TODO: Check if absolute and relative Paths are correctly handled! QFileInfo fileInfo{file}; if(fileInfo.exists() && fileInfo.isFile()) { @@ -96,6 +106,9 @@ bool Autosave::isAutosave(const QString &file) return false; } +/** + * @brief Starts timer for time-dependent autosave (pet-file) + */ void Autosave::startTimer() { if(!mTimer->isActive()) @@ -106,6 +119,9 @@ void Autosave::startTimer() } } +/** + * @brief Stops timer for time-dependent autosave (pet-file) + */ void Autosave::stopTimer() { if(mTimer->isActive()) @@ -114,12 +130,20 @@ void Autosave::stopTimer() } } + QString Autosave::buildAutosaveName(const QString &projectFileName, const QString &ending) { QFileInfo projectFile{projectFileName}; return projectFile.dir().filePath("." + projectFile.baseName() + "_autosave" + ending); } +/** + * @brief Saves the .pet-file + * + * This method is called by the timeout signal of mTimer. + * It saves the pet-file to a hidden file with a name derived from + * the name of the currently loaded project + */ void Autosave::savePet() { auto projectName = mPetrack.getProFileName(); @@ -151,6 +175,13 @@ void Autosave::savePet() } } +/** + * @brief Saves the .trc-file + * + * This method is called by trackPersonModified after a set number of modifications. + * It saves the trc-file to a hidden file with a name derived from + * the name of the currently loaded project + */ void Autosave::saveTrc() { auto projectName = mPetrack.getProFileName(); @@ -176,6 +207,10 @@ void Autosave::saveTrc() } } +/** + * @brief Returns a list of autosave files for the current project + * @return list of autosaves; may be empty + */ QStringList Autosave::getAutosave() { auto projectPath = QFileInfo(mPetrack.getProFileName()); @@ -190,7 +225,7 @@ QStringList Autosave::getAutosave() list.append(autosavePetName); } auto autosaveTrcName = buildAutosaveName(projectPath.absoluteFilePath(), ".trc"); - QFileInfo autosaveTrc{autosavePetName}; + QFileInfo autosaveTrc{autosaveTrcName}; if(autosaveTrc.exists()) { list.append(autosaveTrcName); diff --git a/src/petrack.cpp b/src/petrack.cpp index a1653c32ae7489e342aa99b3fe1c2f67de5f992b..4acc684a323bd3a1a6b3cee146278878300dd722 100644 --- a/src/petrack.cpp +++ b/src/petrack.cpp @@ -2546,6 +2546,7 @@ void Petrack::closeEvent(QCloseEvent *event) if(maybeSave()) { writeSettings(); + mAutosave.deleteAutosave(); event->accept(); } else