Commit 75806ee2 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Fix possible profile corruption when switching to a never used profile.

Fixes #1320
parent 7941aa67
Pipeline #238747 passed with stage
in 4 minutes and 1 second
......@@ -33,15 +33,10 @@ std::unique_ptr<ProfileRepository> &ProfileRepository::get()
return instance;
}
void ProfileRepository::refresh(bool fullRefresh)
void ProfileRepository::refresh()
{
QWriteLocker locker(&m_mutex);
if (fullRefresh) {
// Reset all profiles
m_profiles.clear();
}
// Helper function to check a profile and print debug info
auto check_profile = [&](std::unique_ptr<ProfileModel> &profile, const QString &file) {
if (m_profiles.count(file) > 0) {
......@@ -156,6 +151,7 @@ QString ProfileRepository::findMatchingProfile(ProfileInfo *profile) const
const QString ProfileRepository::saveProfile(ProfileInfo *profile, QString profilePath)
{
bool newProfile = false;
if (profilePath.isEmpty()) {
int i = 0;
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/profiles/"));
......@@ -170,6 +166,9 @@ const QString ProfileRepository::saveProfile(ProfileInfo *profile, QString profi
}
}
QFile file(profilePath);
if (file.exists()) {
newProfile = false;
}
if (!file.open(QIODevice::WriteOnly)) {
KMessageBox::error(nullptr, i18n("Cannot open file %1", profilePath));
return QString();
......@@ -192,7 +191,13 @@ const QString ProfileRepository::saveProfile(ProfileInfo *profile, QString profi
profilePath.clear();
}
file.close();
refresh(true);
if (!newProfile) {
// We edited an existing profile, remove it to trigger a reload
if (m_profiles.count(profilePath) > 0) {
m_profiles.erase(profilePath);
}
}
refresh();
return profilePath;
}
......@@ -204,7 +209,12 @@ bool ProfileRepository::deleteProfile(const QString &path)
}
if (!success) {
qCDebug(KDENLIVE_LOG) << "//// Cannot delete profile " << path << ", does not seem to be custom one";
} else {
if (m_profiles.count(path) > 0) {
// remove the stored profile
m_profiles.erase(path);
}
refresh();
}
refresh(true);
return success;
}
......@@ -28,7 +28,7 @@ public:
static std::unique_ptr<ProfileRepository> &get();
/** @brief Reloads all the profiles from the disk */
void refresh(bool fullRefresh = false);
void refresh();
/** @brief Returns a list of all the pairs (description, path) of all the profiles loaded */
QVector<QPair<QString, QString>> getAllProfiles() const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment