From 037e57b687e79ce8adb00c5e515219f81c860271 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Tue, 5 Jan 2010 12:18:17 +0000 Subject: [PATCH] - Validate label names to make sure there is no character forbidden by the file system --- src/misc.h | 14 ++++++++++++++ src/torrentadditiondlg.h | 4 ++++ src/transferlistfilterswidget.h | 23 +++++++++++++++++------ src/transferlistwidget.cpp | 19 ++++++++++++++----- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/misc.h b/src/misc.h index 57473b77e..6e5dc30f0 100644 --- a/src/misc.h +++ b/src/misc.h @@ -183,6 +183,20 @@ public: } } + static QString toValidFileSystemName(QString filename) { + filename = filename.replace("\\", "/"); + QRegExp regex("[/:!?\"*<>|]"); + return filename.replace(regex, " "); + } + + static bool isValidFileSystemName(QString filename) { + filename = filename.replace("\\", "/"); + QRegExp regex("[/:!?\"*<>|]"); + if(filename.contains(regex)) + return false; + return true; + } + #ifdef Q_WS_MAC static QString getFullPath(const FSRef &ref) { diff --git a/src/torrentadditiondlg.h b/src/torrentadditiondlg.h index 31499f4f9..37319cae1 100644 --- a/src/torrentadditiondlg.h +++ b/src/torrentadditiondlg.h @@ -424,6 +424,10 @@ public slots: return; } } + if (!misc::isValidFileSystemName(comboLabel->currentText().trimmed())) { + QMessageBox::warning(this, tr("Invalid label name"), tr("Please don't use any special characters in the label name.")); + return; + } // Save savepath TorrentTempData::setSavePath(hash, savePath.path()); qDebug("Torrent label is: %s", comboLabel->currentText().trimmed().toLocal8Bit().data()); diff --git a/src/transferlistfilterswidget.h b/src/transferlistfilterswidget.h index 5df6e04a7..079f5d7ed 100644 --- a/src/transferlistfilterswidget.h +++ b/src/transferlistfilterswidget.h @@ -41,6 +41,7 @@ #include #include #include +#include #include "transferlistdelegate.h" #include "transferlistwidget.h" @@ -244,8 +245,8 @@ protected slots: } void addLabel(QString label) { - if(label.trimmed().isEmpty()) return; - if(customLabels.contains(label)) return; + label = misc::toValidFileSystemName(label.trimmed()); + if(label.isEmpty() || customLabels.contains(label)) return; QListWidgetItem *newLabel = new QListWidgetItem(labelFilters); newLabel->setText(label + " (0)"); newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png")); @@ -269,10 +270,20 @@ protected slots: } if(act == addAct) { bool ok; - QString label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, "", &ok); - if (ok && !label.isEmpty()) { - addLabel(label); - } + QString label = ""; + bool invalid; + do { + invalid = false; + label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, label, &ok); + if (ok && !label.isEmpty()) { + if(misc::isValidFileSystemName(label)) { + addLabel(label); + } else { + QMessageBox::warning(this, tr("Invalid label name"), tr("Please don't use any special characters in the label name.")); + invalid = true; + } + } + }while(invalid); return; } } diff --git a/src/transferlistwidget.cpp b/src/transferlistwidget.cpp index 215cd9c6d..d2bdb761f 100644 --- a/src/transferlistwidget.cpp +++ b/src/transferlistwidget.cpp @@ -868,11 +868,20 @@ void TransferListWidget::toggleSelectedFirstLastPiecePrio() { void TransferListWidget::askNewLabelForSelection() { // Ask for label bool ok; - QString label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, "", &ok); - if (ok && !label.isEmpty()) { - // Assign label to selection - setSelectionLabel(label); - } + QString label = ""; + bool invalid; + do { + invalid = false; + label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, label, &ok); + if (ok && !label.isEmpty()) { + if(misc::isValidFileSystemName(label)) { + setSelectionLabel(label); + } else { + QMessageBox::warning(this, tr("Invalid label name"), tr("Please don't use any special characters in the label name.")); + invalid = true; + } + } + }while(invalid); } void TransferListWidget::renameSelectedTorrent() {