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

Use the same save path editing behaviour in properties panel as in torrent addition dialog

This commit is contained in:
Christophe Dumez 2010-06-27 17:58:08 +00:00
parent c01f7102e6
commit 9f5d31b2b1
4 changed files with 343 additions and 285 deletions

View File

@ -2014,7 +2014,8 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
} }
else if (file_renamed_alert* p = dynamic_cast<file_renamed_alert*>(a.get())) { else if (file_renamed_alert* p = dynamic_cast<file_renamed_alert*>(a.get())) {
QTorrentHandle h(p->handle); QTorrentHandle h(p->handle);
if(h.is_valid() && h.num_files() > 1) { if(h.is_valid()) {
if(h.num_files() > 1) {
// Check if folders were renamed // Check if folders were renamed
QStringList old_path_parts = misc::toQStringU(h.get_torrent_info().orig_files().at(p->index).path.string()).split("/"); QStringList old_path_parts = misc::toQStringU(h.get_torrent_info().orig_files().at(p->index).path.string()).split("/");
old_path_parts.removeLast(); old_path_parts.removeLast();
@ -2026,6 +2027,11 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
qDebug("Detected folder renaming, attempt to delete old folder: %s", qPrintable(old_path)); qDebug("Detected folder renaming, attempt to delete old folder: %s", qPrintable(old_path));
QDir().rmpath(old_path); QDir().rmpath(old_path);
} }
} else {
// Single-file torrent
// Renaming a file corresponds to changing the save path
emit savePathChanged(h);
}
} }
} }
else if (storage_moved_alert* p = dynamic_cast<storage_moved_alert*>(a.get())) { else if (storage_moved_alert* p = dynamic_cast<storage_moved_alert*>(a.get())) {

View File

@ -210,9 +210,14 @@ Bittorrent* PropertiesWidget::getBTSession() const {
void PropertiesWidget::updateSavePath(QTorrentHandle& _h) { void PropertiesWidget::updateSavePath(QTorrentHandle& _h) {
if(h.is_valid() && h == _h) { if(h.is_valid() && h == _h) {
QString p = TorrentPersistentData::getSavePath(h.hash()); QString p;
if(h.has_metadata() && h.num_files() == 1) {
p = h.firstFileSavePath();
} else {
p = TorrentPersistentData::getSavePath(h.hash());
if(p.isEmpty()) if(p.isEmpty())
p = h.save_path(); p = h.save_path();
}
#if defined(Q_WS_WIN) || defined(Q_OS_OS2) #if defined(Q_WS_WIN) || defined(Q_OS_OS2)
p = p.replace("/", "\\"); p = p.replace("/", "\\");
#endif #endif
@ -237,9 +242,14 @@ void PropertiesWidget::loadTorrentInfos(QTorrentHandle &_h) {
try { try {
// Save path // Save path
QString p = TorrentPersistentData::getSavePath(h.hash()); QString p;
if(h.has_metadata() && h.num_files() == 1) {
p = h.firstFileSavePath();
} else {
p = TorrentPersistentData::getSavePath(h.hash());
if(p.isEmpty()) if(p.isEmpty())
p = h.save_path(); p = h.save_path();
}
#if defined(Q_WS_WIN) || defined(Q_OS_OS2) #if defined(Q_WS_WIN) || defined(Q_OS_OS2)
p = p.replace("/", "\\"); p = p.replace("/", "\\");
#endif #endif
@ -747,16 +757,27 @@ void PropertiesWidget::renameSelectedFile() {
void PropertiesWidget::on_changeSavePathButton_clicked() { void PropertiesWidget::on_changeSavePathButton_clicked() {
if(!h.is_valid()) return; if(!h.is_valid()) return;
QString dir; QString new_path;
if(h.has_metadata() && h.num_files() == 1) {
new_path = QFileDialog::getSaveFileName(this, tr("Choose save path"), h.firstFileSavePath());
} else {
const QDir saveDir(h.save_path()); const QDir saveDir(h.save_path());
if(saveDir.exists()){ if(saveDir.exists()){
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), h.save_path()); new_path = QFileDialog::getExistingDirectory(this, tr("Choose save path"), h.save_path());
}else{ }else{
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath()); new_path = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath());
} }
if(!dir.isEmpty()){ }
if(!new_path.isEmpty()){
// Check if savePath exists // Check if savePath exists
QDir savePath(misc::expandPath(dir)); QString save_path_dir = new_path.replace("\\", "/");
QString new_file_name;
if(h.has_metadata() && h.num_files() == 1) {
QStringList parts = save_path_dir.split("/");
new_file_name = parts.takeLast(); // Skip file name
save_path_dir = parts.join("/");
}
QDir savePath(misc::expandPath(save_path_dir));
if(!savePath.exists()){ if(!savePath.exists()){
if(!savePath.mkpath(savePath.absolutePath())){ if(!savePath.mkpath(savePath.absolutePath())){
QMessageBox::critical(0, tr("Save path creation error"), tr("Could not create the save path")); QMessageBox::critical(0, tr("Save path creation error"), tr("Could not create the save path"));
@ -767,7 +788,24 @@ void PropertiesWidget::renameSelectedFile() {
if(!BTSession->useTemporaryFolder() || h.is_seed()) if(!BTSession->useTemporaryFolder() || h.is_seed())
h.move_storage(savePath.absolutePath()); h.move_storage(savePath.absolutePath());
// Update save_path in dialog // Update save_path in dialog
QString display_path = savePath.absolutePath(); QString display_path;
if(h.has_metadata() && h.num_files() == 1) {
// Rename the file
Q_ASSERT(!new_file_name.isEmpty());
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
if(h.file_at(0).compare(new_file_name, Qt::CaseInsensitive) != 0) {
#else
if(h.file_at(0).compare(new_file_name, Qt::CaseSensitive) != 0) {
#endif
qDebug("Renaming single file to %s", qPrintable(new_file_name));
h.rename_file(0, new_file_name);
// Also rename it in the files list model
PropListModel->setData(PropListModel->index(0, 0), new_file_name);
}
display_path = new_path;
} else {
display_path = savePath.absolutePath();
}
#if defined(Q_WS_WIN) || defined(Q_OS_OS2) #if defined(Q_WS_WIN) || defined(Q_OS_OS2)
display_path = display_path.replace("/", "\\"); display_path = display_path.replace("/", "\\");
#endif #endif

View File

@ -459,6 +459,19 @@ QString QTorrentHandle::root_path() const {
return save_path(); return save_path();
} }
QString QTorrentHandle::firstFileSavePath() const {
Q_ASSERT(h.is_valid());
Q_ASSERT(has_metadata());
QString fsave_path = TorrentPersistentData::getSavePath(hash());
if(fsave_path.isEmpty())
fsave_path = save_path();
fsave_path = fsave_path.replace("\\", "/");
if(!fsave_path.endsWith("/"))
fsave_path += "/";
fsave_path += misc::toQStringU(h.get_torrent_info().file_at(0).path.string());
return fsave_path;
}
bool QTorrentHandle::has_error() const { bool QTorrentHandle::has_error() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(h.is_valid());
return h.is_paused() && !h.status().error.empty(); return h.is_paused() && !h.status().error.empty();

View File

@ -131,6 +131,7 @@ class QTorrentHandle {
bool priv() const; bool priv() const;
bool first_last_piece_first() const; bool first_last_piece_first() const;
QString root_path() const; QString root_path() const;
QString firstFileSavePath() const;
bool has_error() const; bool has_error() const;
QString error() const; QString error() const;
void downloading_pieces(bitfield &bf) const; void downloading_pieces(bitfield &bf) const;