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