Browse Source

refactor(profileinfo): Move some methods in profile info class

reviewable/pr4716/r5
Diadlo 8 years ago
parent
commit
49915c0a45
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
  1. 20
      src/core/core.cpp
  2. 1
      src/core/core.h
  3. 50
      src/model/profile/profileinfo.cpp

20
src/core/core.cpp

@ -943,26 +943,6 @@ void Core::setStatus(Status status) @@ -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<QChar> 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
*/

1
src/core/core.h

@ -46,7 +46,6 @@ public: @@ -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);

50
src/model/profile/profileinfo.cpp

@ -117,6 +117,32 @@ QString ProfileInfo::getProfileName() const @@ -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<QChar> 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) @@ -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) @@ -142,8 +168,22 @@ IProfileInfo::RenameResult ProfileInfo::renameProfile(const QString &name)
return RenameResult::OK;
}
// TODO: Find out what is dangerous?
/**
* @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 cusom place.
* @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 @@ -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 @@ -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<QString> ProfileInfo::removeProfile()
@ -208,7 +248,7 @@ IProfileInfo::SaveResult ProfileInfo::saveQr(const QImage& image, const QString& @@ -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;
}

Loading…
Cancel
Save