1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

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

This commit is contained in:
Diadlo 2017-10-07 20:03:49 +03:00
parent 4a11a0eb22
commit 49915c0a45
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
3 changed files with 45 additions and 26 deletions

View File

@ -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
*/

View File

@ -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);

View File

@ -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)
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<QString> 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;
}