make name of notebook/folder/note case-insensitive

This commit is contained in:
Le Tan 2017-08-08 20:11:44 +08:00
parent c2fe857e3a
commit 91d33c3f5c
6 changed files with 42 additions and 22 deletions

View File

@ -229,9 +229,10 @@ void VNewNotebookDialog::handleInputChanged()
bool nameOk = !name.isEmpty(); bool nameOk = !name.isEmpty();
if (pathOk && nameOk) { if (pathOk && nameOk) {
// Check if the name conflicts with existing notebook name. // Check if the name conflicts with existing notebook name.
// Case-insensitive.
int idx = -1; int idx = -1;
for (idx = 0; idx < m_notebooks.size(); ++idx) { for (idx = 0; idx < m_notebooks.size(); ++idx) {
if (m_notebooks[idx]->getName() == name) { if (m_notebooks[idx]->getName().toLower() == name.toLower()) {
break; break;
} }
} }
@ -239,7 +240,7 @@ void VNewNotebookDialog::handleInputChanged()
if (idx < m_notebooks.size()) { if (idx < m_notebooks.size()) {
nameOk = false; nameOk = false;
showWarnLabel = true; showWarnLabel = true;
QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name already exists. " QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
"Please choose another name.") "Please choose another name.")
.arg(vconfig.c_warningTextStyle); .arg(vconfig.c_warningTextStyle);
m_warnLabel->setText(nameConflictText); m_warnLabel->setText(nameConflictText);

View File

@ -90,9 +90,10 @@ void VNotebookInfoDialog::handleInputChanged()
if (nameOk && name != m_notebook->getName()) { if (nameOk && name != m_notebook->getName()) {
// Check if the name conflicts with existing notebook name. // Check if the name conflicts with existing notebook name.
// Case-insensitive.
int idx = -1; int idx = -1;
for (idx = 0; idx < m_notebooks.size(); ++idx) { for (idx = 0; idx < m_notebooks.size(); ++idx) {
if (m_notebooks[idx]->getName() == name) { if (m_notebooks[idx]->getName().toLower() == name.toLower()) {
break; break;
} }
} }
@ -100,7 +101,7 @@ void VNotebookInfoDialog::handleInputChanged()
if (idx < m_notebooks.size()) { if (idx < m_notebooks.size()) {
nameOk = false; nameOk = false;
showWarnLabel = true; showWarnLabel = true;
QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name already exists. " QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
"Please choose another name.") "Please choose another name.")
.arg(vconfig.c_warningTextStyle); .arg(vconfig.c_warningTextStyle);
m_warnLabel->setText(nameConflictText); m_warnLabel->setText(nameConflictText);

View File

@ -206,14 +206,15 @@ VDirectory *VDirectory::createSubDirectory(const QString &p_name)
return ret; return ret;
} }
VDirectory *VDirectory::findSubDirectory(const QString &p_name) VDirectory *VDirectory::findSubDirectory(const QString &p_name, bool p_caseSensitive)
{ {
if (!open()) { if (!open()) {
return NULL; return NULL;
} }
QString name = p_caseSensitive ? p_name : p_name.toLower();
for (int i = 0; i < m_subDirs.size(); ++i) { for (int i = 0; i < m_subDirs.size(); ++i) {
if (p_name == m_subDirs[i]->getName()) { if (name == (p_caseSensitive ? m_subDirs[i]->getName() : m_subDirs[i]->getName().toLower())) {
return m_subDirs[i]; return m_subDirs[i];
} }
} }
@ -221,14 +222,15 @@ VDirectory *VDirectory::findSubDirectory(const QString &p_name)
return NULL; return NULL;
} }
VFile *VDirectory::findFile(const QString &p_name) VFile *VDirectory::findFile(const QString &p_name, bool p_caseSensitive)
{ {
if (!open()) { if (!open()) {
return NULL; return NULL;
} }
QString name = p_caseSensitive ? p_name : p_name.toLower();
for (int i = 0; i < m_files.size(); ++i) { for (int i = 0; i < m_files.size(); ++i) {
if (p_name == m_files[i]->getName()) { if (name == (p_caseSensitive ? m_files[i]->getName() : m_files[i]->getName().toLower())) {
return m_files[i]; return m_files[i];
} }
} }
@ -693,12 +695,18 @@ VFile *VDirectory::tryLoadFile(QStringList &p_filePath)
VFile *file = NULL; VFile *file = NULL;
#if defined(Q_OS_WIN)
bool caseSensitive = false;
#else
bool caseSensitive = true;
#endif
if (p_filePath.size() == 1) { if (p_filePath.size() == 1) {
// File. // File.
file = findFile(p_filePath.at(0)); file = findFile(p_filePath.at(0), caseSensitive);
} else { } else {
// Directory. // Directory.
VDirectory *dir = findSubDirectory(p_filePath.at(0)); VDirectory *dir = findSubDirectory(p_filePath.at(0), caseSensitive);
if (dir) { if (dir) {
p_filePath.removeFirst(); p_filePath.removeFirst();
file = dir->tryLoadFile(p_filePath); file = dir->tryLoadFile(p_filePath);

View File

@ -21,10 +21,10 @@ public:
VDirectory *createSubDirectory(const QString &p_name); VDirectory *createSubDirectory(const QString &p_name);
// Returns the VDirectory with the name @p_name directly in this directory. // Returns the VDirectory with the name @p_name directly in this directory.
VDirectory *findSubDirectory(const QString &p_name); VDirectory *findSubDirectory(const QString &p_name, bool p_caseSensitive);
// Returns the VFile with the name @p_name directly in this directory. // Returns the VFile with the name @p_name directly in this directory.
VFile *findFile(const QString &p_name); VFile *findFile(const QString &p_name, bool p_caseSensitive);
// If current dir or its sub-dir contains @p_file. // If current dir or its sub-dir contains @p_file.
bool containsFile(const VFile *p_file) const; bool containsFile(const VFile *p_file) const;

View File

@ -347,12 +347,15 @@ void VDirectoryTree::newSubDirectory()
VNewDirDialog dialog(tr("Create Folder"), info, text, defaultText, this); VNewDirDialog dialog(tr("Create Folder"), info, text, defaultText, this);
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
QString name = dialog.getNameInput(); QString name = dialog.getNameInput();
if (curDir->findSubDirectory(name)) { // Case-insensitive.
info = tr("Name already exists in <span style=\"%1\">%2</span>. Please choose another name.") if (curDir->findSubDirectory(name, false)) {
info = tr("Name (case-insensitive) already exists in "
"<span style=\"%1\">%2</span>. Please choose another name.")
.arg(vconfig.c_dataTextStyle).arg(curDir->getName()); .arg(vconfig.c_dataTextStyle).arg(curDir->getName());
defaultText = name; defaultText = name;
continue; continue;
} }
VDirectory *subDir = curDir->createSubDirectory(name); VDirectory *subDir = curDir->createSubDirectory(name);
if (!subDir) { if (!subDir) {
VUtils::showMessage(QMessageBox::Warning, tr("Warning"), VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
@ -382,8 +385,9 @@ void VDirectoryTree::newRootDirectory()
VNewDirDialog dialog(tr("Create Root Folder"), info, text, defaultText, this); VNewDirDialog dialog(tr("Create Root Folder"), info, text, defaultText, this);
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
QString name = dialog.getNameInput(); QString name = dialog.getNameInput();
if (rootDir->findSubDirectory(name)) { if (rootDir->findSubDirectory(name, false)) {
info = tr("Name already exists in notebook <span style=\"%1\">%2</span>. Please choose another name.") info = tr("Name (case-insensitive) already exists in "
"notebook <span style=\"%1\">%2</span>. Please choose another name.")
.arg(vconfig.c_dataTextStyle).arg(m_notebook->getName()); .arg(vconfig.c_dataTextStyle).arg(m_notebook->getName());
defaultText = name; defaultText = name;
continue; continue;
@ -461,11 +465,13 @@ void VDirectoryTree::editDirectoryInfo()
if (name == curName) { if (name == curName) {
return; return;
} }
if (parentDir->findSubDirectory(name)) {
info = "Name already exists. Please choose another name."; if (parentDir->findSubDirectory(name, false)) {
info = "Name (case-insensitive) already exists. Please choose another name.";
defaultName = name; defaultName = name;
continue; continue;
} }
if (!curDir->rename(name)) { if (!curDir->rename(name)) {
VUtils::showMessage(QMessageBox::Warning, tr("Warning"), VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
tr("Fail to rename folder <span style=\"%1\">%2</span>.") tr("Fail to rename folder <span style=\"%1\">%2</span>.")

View File

@ -146,8 +146,10 @@ void VFileList::fileInfo(VFile *p_file)
if (name == curName) { if (name == curName) {
return; return;
} }
if (dir->findFile(name)) {
info = "Name already exists. Please choose another name."; // Case-insensitive when creating note.
if (dir->findFile(name, false)) {
info = "Name (case-insensitive) already exists. Please choose another name.";
defaultName = name; defaultName = name;
continue; continue;
} }
@ -238,11 +240,13 @@ void VFileList::newFile()
VNewFileDialog dialog(tr("Create Note"), info, text, defaultText, this); VNewFileDialog dialog(tr("Create Note"), info, text, defaultText, this);
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
QString name = dialog.getNameInput(); QString name = dialog.getNameInput();
if (m_directory->findFile(name)) { // Case-insensitive when creating note.
info = tr("Name already exists. Please choose another name."); if (m_directory->findFile(name, false)) {
info = tr("Name (case-insensitive) already exists. Please choose another name.");
defaultText = name; defaultText = name;
continue; continue;
} }
VFile *file = m_directory->createFile(name); VFile *file = m_directory->createFile(name);
if (!file) { if (!file) {
VUtils::showMessage(QMessageBox::Warning, tr("Warning"), VUtils::showMessage(QMessageBox::Warning, tr("Warning"),