diff --git a/src/core/core.cpp b/src/core/core.cpp index 8b6005620..501fec762 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -943,26 +943,6 @@ void Core::setStatus(Status status) emit statusSet(status); } -QString Core::sanitize(QString name) -{ - // these are pretty much Windows banned filename characters - QList banned{'/', '\\', ':', '<', '>', '"', '|', '?', '*'}; - for (QChar c : banned) { - name.replace(c, '_'); - } - - // also remove leading and trailing periods - if (name[0] == '.') { - name[0] = '_'; - } - - if (name.endsWith('.')) { - name[name.length() - 1] = '_'; - } - - return name; -} - /** * @brief Returns the unencrypted tox save data */ diff --git a/src/core/core.h b/src/core/core.h index 22f6a5a44..a32d71031 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -46,7 +46,6 @@ public: ~Core(); static const QString TOX_EXT; - static QString sanitize(QString name); static QStringList splitMessage(const QString& message, int maxLen); static QByteArray getSaltFromFile(QString filename); diff --git a/src/model/profile/profileinfo.cpp b/src/model/profile/profileinfo.cpp index 620a31296..9032e6d38 100644 --- a/src/model/profile/profileinfo.cpp +++ b/src/model/profile/profileinfo.cpp @@ -117,6 +117,32 @@ QString ProfileInfo::getProfileName() const return profile->getName(); } +/** + * @brief Remove characters not supported for profile name from string. + * @param src Source string. + * @return Sanitized string. + */ +static QString sanitize(const QString& src) +{ + QString name = src; + // these are pretty much Windows banned filename characters + QList banned{'/', '\\', ':', '<', '>', '"', '|', '?', '*'}; + for (QChar c : banned) { + name.replace(c, '_'); + } + + // also remove leading and trailing periods + if (name[0] == '.') { + name[0] = '_'; + } + + if (name.endsWith('.')) { + name[name.length() - 1] = '_'; + } + + return name; +} + /** * @brief Rename profile file. * @param name New profile name. @@ -129,7 +155,7 @@ IProfileInfo::RenameResult ProfileInfo::renameProfile(const QString &name) return RenameResult::EmptyName; } - QString newName = Core::sanitize(name); + QString newName = sanitize(name); if (Profile::exists(newName)) { return RenameResult::ProfileAlreadyExists; @@ -142,8 +168,22 @@ IProfileInfo::RenameResult ProfileInfo::renameProfile(const QString &name) return RenameResult::OK; } +// TODO: Find out what is dangerous? /** - * @brief Save profile in cusom place. + * @brief Dangerous way to find out if a path is writable. + * @param filepath Path to file which should be deleted. + * @return True, if file writeable, false otherwise. + */ +static bool tryRemoveFile(const QString& filepath) +{ + QFile tmp(filepath); + bool writable = tmp.open(QIODevice::WriteOnly); + tmp.remove(); + return writable; +} + +/** + * @brief Save profile in custom place. * @param path Path to save profile. * @return Result code of save operation. */ @@ -154,7 +194,7 @@ IProfileInfo::SaveResult ProfileInfo::exportProfile(const QString &path) const return SaveResult::EmptyPath; } - if (!Nexus::tryRemoveFile(path)) { + if (!tryRemoveFile(path)) { return SaveResult::NoWritePermission; } @@ -167,7 +207,7 @@ IProfileInfo::SaveResult ProfileInfo::exportProfile(const QString &path) const /** * @brief Remove profile. - * @return List of files, which can't be removed automaticaly. + * @return List of files, which couldn't be removed automaticaly. */ // TODO: Use QStringList QVector ProfileInfo::removeProfile() @@ -208,7 +248,7 @@ IProfileInfo::SaveResult ProfileInfo::saveQr(const QImage& image, const QString& return SaveResult::EmptyPath; } - if (!Nexus::tryRemoveFile(path)) { + if (!tryRemoveFile(path)) { return SaveResult::NoWritePermission; }