1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-02 18:04:32 +00:00

- Validate label names to make sure there is no character forbidden by the file system

This commit is contained in:
Christophe Dumez 2010-01-05 12:18:17 +00:00
parent 2b289655c1
commit 037e57b687
4 changed files with 49 additions and 11 deletions

View File

@ -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 #ifdef Q_WS_MAC
static QString getFullPath(const FSRef &ref) static QString getFullPath(const FSRef &ref)
{ {

View File

@ -424,6 +424,10 @@ public slots:
return; 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 // Save savepath
TorrentTempData::setSavePath(hash, savePath.path()); TorrentTempData::setSavePath(hash, savePath.path());
qDebug("Torrent label is: %s", comboLabel->currentText().trimmed().toLocal8Bit().data()); qDebug("Torrent label is: %s", comboLabel->currentText().trimmed().toLocal8Bit().data());

View File

@ -41,6 +41,7 @@
#include <QInputDialog> #include <QInputDialog>
#include <QDragMoveEvent> #include <QDragMoveEvent>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QMessageBox>
#include "transferlistdelegate.h" #include "transferlistdelegate.h"
#include "transferlistwidget.h" #include "transferlistwidget.h"
@ -244,8 +245,8 @@ protected slots:
} }
void addLabel(QString label) { void addLabel(QString label) {
if(label.trimmed().isEmpty()) return; label = misc::toValidFileSystemName(label.trimmed());
if(customLabels.contains(label)) return; if(label.isEmpty() || customLabels.contains(label)) return;
QListWidgetItem *newLabel = new QListWidgetItem(labelFilters); QListWidgetItem *newLabel = new QListWidgetItem(labelFilters);
newLabel->setText(label + " (0)"); newLabel->setText(label + " (0)");
newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png")); newLabel->setData(Qt::DecorationRole, QIcon(":/Icons/oxygen/folder.png"));
@ -269,10 +270,20 @@ protected slots:
} }
if(act == addAct) { if(act == addAct) {
bool ok; bool ok;
QString label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, "", &ok); QString label = "";
if (ok && !label.isEmpty()) { bool invalid;
addLabel(label); 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; return;
} }
} }

View File

@ -868,11 +868,20 @@ void TransferListWidget::toggleSelectedFirstLastPiecePrio() {
void TransferListWidget::askNewLabelForSelection() { void TransferListWidget::askNewLabelForSelection() {
// Ask for label // Ask for label
bool ok; bool ok;
QString label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, "", &ok); QString label = "";
if (ok && !label.isEmpty()) { bool invalid;
// Assign label to selection do {
setSelectionLabel(label); 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() { void TransferListWidget::renameSelectedTorrent() {