From 50e620daf287a049ca60e30f066a48fd7bc72dbd Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Tue, 22 Dec 2009 10:29:39 +0000 Subject: [PATCH] - Handle paths with (~, ., ..) properly --- src/bittorrent.cpp | 4 ++- src/misc.h | 17 ++++++++++++ src/options_imp.cpp | 58 +++++++++++++++++++++++++--------------- src/propertieswidget.cpp | 10 +++---- src/torrentadditiondlg.h | 11 ++++---- 5 files changed, 67 insertions(+), 33 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 781416f1d..5943aca78 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -1836,11 +1836,13 @@ QString Bittorrent::getSavePath(QString hash) { } qDebug("getSavePath, got save_path from persistent data: %s", savePath.toLocal8Bit().data()); } + // Clean path + savePath = misc::expandPath(savePath); // Checking if savePath Dir exists // create it if it is not QDir saveDir(savePath); if(!saveDir.exists()) { - if(!saveDir.mkpath(saveDir.path())) { + if(!saveDir.mkpath(saveDir.absolutePath())) { std::cerr << "Couldn't create the save directory: " << saveDir.path().toLocal8Bit().data() << "\n"; // XXX: Do something else? } diff --git a/src/misc.h b/src/misc.h index 4e10dd130..6f31a53fa 100644 --- a/src/misc.h +++ b/src/misc.h @@ -317,6 +317,23 @@ public: return QDateTime::fromTime_t(mktime(&tm)).toString(Qt::DefaultLocaleLongDate); } + // Replace ~ in path + static QString expandPath(QString path) { + path = path.trimmed(); + if(path.isEmpty()) return path; + if(path.length() == 1) { + if(path[0] == '~' ) return QDir::homePath(); + } + if(path[0] == '~' && path[1] == QDir::separator()) { + path = path.replace(0, 1, QDir::homePath()); + } else { + if(QDir::isAbsolutePath(path)) { + path = QDir(path).absolutePath(); + } + } + return QDir::cleanPath(path); + } + // Take a number of seconds and return an user-friendly // time duration like "1d 2h 10m". static QString userFriendlyDuration(qlonglong seconds) { diff --git a/src/options_imp.cpp b/src/options_imp.cpp index 6826af6a6..e58baf5b1 100644 --- a/src/options_imp.cpp +++ b/src/options_imp.cpp @@ -921,11 +921,11 @@ QString options_imp::getSavePath() const{ if(textSavePath->text().trimmed().isEmpty()){ textSavePath->setText(home+QString::fromUtf8("qBT_dir")); } - return textSavePath->text(); + return misc::expandPath(textSavePath->text()); } QString options_imp::getTempPath() const { - return textTempPath->text(); + return misc::expandPath(textTempPath->text()); } bool options_imp::isTempPathEnabled() const { @@ -1310,9 +1310,9 @@ void options_imp::setLocale(QString locale){ // Return scan dir set in options QString options_imp::getScanDir() const { if(checkScanDir->isChecked()){ - return textScanDir->text().trimmed(); + return misc::expandPath(textScanDir->text()); }else{ - return QString(); + return QString::null; } } @@ -1332,22 +1332,28 @@ int options_imp::getActionOnDblClOnTorrentFn() const { // Display dialog to choose scan dir void options_imp::on_browseScanDirButton_clicked() { -#ifdef Q_WS_WIN - QString dir = QFileDialog::getExistingDirectory(this, tr("Choose scan directory"), QDir::rootPath()); -#else - QString dir = QFileDialog::getExistingDirectory(this, tr("Choose scan directory"), QDir::homePath()); -#endif + QString scan_path = misc::expandPath(textScanDir->text()); + QDir scanDir(scan_path); + QString dir; + if(!scan_path.isEmpty() && scanDir.exists()) { + dir = QFileDialog::getExistingDirectory(this, tr("Choose scan directory"), scanDir.absolutePath()); + } else { + dir = QFileDialog::getExistingDirectory(this, tr("Choose scan directory"), QDir::homePath()); + } if(!dir.isNull()){ textScanDir->setText(dir); } } void options_imp::on_browseFilterButton_clicked() { -#ifdef Q_WS_WIN - QString ipfilter = QFileDialog::getOpenFileName(this, tr("Choose an ip filter file"), QDir::rootPath(), tr("Filters")+QString(" (*.dat *.p2p *.p2b)")); -#else - QString ipfilter = QFileDialog::getOpenFileName(this, tr("Choose an ip filter file"), QDir::homePath(), tr("Filters")+QString(" (*.dat *.p2p *.p2b)")); -#endif + QString filter_path = misc::expandPath(textFilterPath->text()); + QDir filterDir(filter_path); + QString ipfilter; + if(!filter_path.isEmpty() && filterDir.exists()) { + ipfilter = QFileDialog::getOpenFileName(this, tr("Choose an ip filter file"), filterDir.absolutePath(), tr("Filters")+QString(" (*.dat *.p2p *.p2b)")); + } else { + ipfilter = QFileDialog::getOpenFileName(this, tr("Choose an ip filter file"), QDir::homePath(), tr("Filters")+QString(" (*.dat *.p2p *.p2b)")); + } if(!ipfilter.isNull()){ textFilterPath->setText(ipfilter); } @@ -1355,20 +1361,28 @@ void options_imp::on_browseFilterButton_clicked() { // Display dialog to choose save dir void options_imp::on_browseSaveDirButton_clicked(){ - QString def_path = QDir::homePath(); - if(!textSavePath->text().isEmpty()) - def_path = textSavePath->text(); - QString dir = QFileDialog::getExistingDirectory(this, tr("Choose a save directory"), def_path); + QString save_path = misc::expandPath(textSavePath->text()); + QDir saveDir(save_path); + QString dir; + if(!save_path.isEmpty() && saveDir.exists()) { + dir = QFileDialog::getExistingDirectory(this, tr("Choose a save directory"), saveDir.absolutePath()); + } else { + dir = QFileDialog::getExistingDirectory(this, tr("Choose a save directory"), QDir::homePath()); + } if(!dir.isNull()){ textSavePath->setText(dir); } } void options_imp::on_browseTempDirButton_clicked(){ - QString def_path = QDir::homePath(); - if(!textTempPath->text().isEmpty()) - def_path = textTempPath->text(); - QString dir = QFileDialog::getExistingDirectory(this, tr("Choose a save directory"), def_path); + QString temp_path = misc::expandPath(textTempPath->text()); + QDir tempDir(temp_path); + QString dir; + if(!temp_path.isEmpty() && tempDir.exists()) { + dir = QFileDialog::getExistingDirectory(this, tr("Choose a save directory"), tempDir.absolutePath()); + } else { + dir = QFileDialog::getExistingDirectory(this, tr("Choose a save directory"), QDir::homePath()); + } if(!dir.isNull()){ textTempPath->setText(dir); } diff --git a/src/propertieswidget.cpp b/src/propertieswidget.cpp index 4f4a8767f..ce8a02b8a 100644 --- a/src/propertieswidget.cpp +++ b/src/propertieswidget.cpp @@ -636,20 +636,20 @@ void PropertiesWidget::renameSelectedFile() { } if(!dir.isNull()){ // Check if savePath exists - QDir savePath(dir); + QDir savePath(misc::expandPath(dir)); if(!savePath.exists()){ - if(!savePath.mkpath(savePath.path())){ + if(!savePath.mkpath(savePath.absolutePath())){ QMessageBox::critical(0, tr("Save path creation error"), tr("Could not create the save path")); return; } } // Save savepath - TorrentPersistentData::saveSavePath(h.hash(), savePath.path()); + TorrentPersistentData::saveSavePath(h.hash(), savePath.absolutePath()); // Actually move storage if(!BTSession->useTemporaryFolder() || h.is_seed()) - h.move_storage(savePath.path()); + h.move_storage(savePath.absolutePath()); // Update save_path in dialog - save_path->setText(savePath.path()); + save_path->setText(savePath.absolutePath()); } } diff --git a/src/torrentadditiondlg.h b/src/torrentadditiondlg.h index 68a137f2f..8d3dfa044 100644 --- a/src/torrentadditiondlg.h +++ b/src/torrentadditiondlg.h @@ -205,7 +205,7 @@ public: public slots: void updateDiskSpaceLabels() { - long long available = misc::freeDiskSpaceOnPath(savePathTxt->text()); + long long available = misc::freeDiskSpaceOnPath(misc::expandPath(savePathTxt->text())); lbl_disk_space->setText(misc::friendlyUnit(available)); // Determine torrent size @@ -235,9 +235,10 @@ public slots: void on_browseButton_clicked(){ QString dir; - QDir saveDir(savePathTxt->text()); - if(saveDir.exists()){ - dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), savePathTxt->text()); + QString save_path = misc::expandPath(savePathTxt->text()); + QDir saveDir(save_path); + if(!save_path.isEmpty() && saveDir.exists()){ + dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), saveDir.absolutePath()); }else{ dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath()); } @@ -261,11 +262,11 @@ public slots: } void on_OkButton_clicked(){ - QDir savePath(savePathTxt->text()); if(savePathTxt->text().trimmed().isEmpty()){ QMessageBox::critical(0, tr("Empty save path"), tr("Please enter a save path")); return; } + QDir savePath(misc::expandPath(savePathTxt->text())); // Check if savePath exists if(!savePath.exists()){ if(!savePath.mkpath(savePath.path())){