1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-02 09:55:55 +00:00

Code cleanup in torrent addition dialog

Magnet link related fixes to torrent addition dialog
This commit is contained in:
Christophe Dumez 2010-07-19 17:29:48 +00:00
parent 214bb13843
commit bcdf4e42fa
2 changed files with 569 additions and 574 deletions

View File

@ -61,27 +61,20 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
display_path = display_path.replace("/", "\\"); display_path = display_path.replace("/", "\\");
#endif #endif
savePathTxt->addItem(display_path); savePathTxt->addItem(display_path);
// Load save path history
QStringList raw_path_history = getSavePathHistory();
foreach(const QString &sp, raw_path_history) {
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
if(sp.compare(display_path, Qt::CaseInsensitive) != 0) {
#else
if(sp.compare(display_path, Qt::CaseSensitive) != 0) {
#endif
path_history << sp;
QString dsp = sp;
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
dsp = dsp.replace("/", "\\");
#endif
savePathTxt->addItem(dsp);
}
}
if(Preferences::addTorrentsInPause()) { if(Preferences::addTorrentsInPause()) {
addInPause->setChecked(true); addInPause->setChecked(true);
//addInPause->setEnabled(false); //addInPause->setEnabled(false);
} }
// Load custom labels
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
settings.beginGroup(QString::fromUtf8("TransferListFilters"));
const QStringList &customLabels = settings.value("customLabels", QStringList()).toStringList();
comboLabel->addItem("");
foreach(const QString& label, customLabels) {
comboLabel->addItem(label);
}
#if LIBTORRENT_VERSION_MINOR < 15 #if LIBTORRENT_VERSION_MINOR < 15
addInSeed->setVisible(false); addInSeed->setVisible(false);
#endif #endif
@ -152,33 +145,26 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
is_magnet = true; is_magnet = true;
this->from_url = magnet_uri; this->from_url = magnet_uri;
// Disable Save path combox and browse button
// Save path should be default for magnet links
savePathTxt->setEnabled(false);
browseButton->setEnabled(false);
// Get torrent hash // Get torrent hash
hash = misc::magnetUriToHash(magnet_uri); hash = misc::magnetUriToHash(magnet_uri);
if(hash.isEmpty()) { if(hash.isEmpty()) {
BTSession->addConsoleMessage(tr("Unable to decode magnet link:")+QString::fromUtf8(" '")+from_url+QString::fromUtf8("'"), QString::fromUtf8("red")); BTSession->addConsoleMessage(tr("Unable to decode magnet link:")+QString::fromUtf8(" '")+from_url+QString::fromUtf8("'"), QString::fromUtf8("red"));
return; return;
} }
// Set torrent name
fileName = misc::magnetUriToName(magnet_uri); fileName = misc::magnetUriToName(magnet_uri);
if(fileName.isEmpty()) { if(fileName.isEmpty()) {
fileName = tr("Magnet Link"); fileName = tr("Magnet Link");
} else {
QString save_path = savePathTxt->currentText();
if(!save_path.endsWith(QDir::separator()))
save_path += QDir::separator();
savePathTxt->setEditText(save_path + fileName);
} }
fileNameLbl->setText(QString::fromUtf8("<center><b>")+fileName+QString::fromUtf8("</b></center>")); fileNameLbl->setText(QString::fromUtf8("<center><b>")+fileName+QString::fromUtf8("</b></center>"));
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(updateSavePathCurrentText(QString)));
// Update display // Update display
updateDiskSpaceLabels(); updateDiskSpaceLabels();
// Load custom labels
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
settings.beginGroup(QString::fromUtf8("TransferListFilters"));
QStringList customLabels = settings.value("customLabels", QStringList()).toStringList();
comboLabel->addItem("");
foreach(const QString& label, customLabels) {
comboLabel->addItem(label);
}
// No need to display torrent content // No need to display torrent content
hideTorrentContent(); hideTorrentContent();
} }
@ -189,6 +175,7 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
close(); close();
return; return;
} }
this->filePath = filePath; this->filePath = filePath;
this->from_url = from_url; this->from_url = from_url;
// Getting torrent file informations // Getting torrent file informations
@ -213,37 +200,8 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
close(); close();
return; return;
} }
if(nbFiles == 1) { // Truncate root folder
// Update properties model whenever the file path is edited
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(renameTorrentNameInModel(QString)));
}
QString root_folder = misc::truncateRootFolder(t); QString root_folder = misc::truncateRootFolder(t);
QString save_path = savePathTxt->currentText();
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
save_path = save_path.replace("/", "\\");
#endif
if(!save_path.endsWith(QDir::separator()))
save_path += QDir::separator();
// If the torrent has a root folder, append it to the save path
if(!root_folder.isEmpty()) {
savePathTxt->setEditText(save_path + root_folder);
// Update other combo items
for(int i=0; i<savePathTxt->count(); ++i) {
savePathTxt->setItemText(i, savePathTxt->itemText(i) + root_folder);
}
}
if(nbFiles == 1) {
// single file torrent
QString single_file_relpath = misc::toQStringU(t->file_at(0).path.string());
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
single_file_relpath = single_file_relpath.replace("/", "\\");
#endif
savePathTxt->setEditText(save_path + single_file_relpath);
// Update other combo items
for(int i=0; i<savePathTxt->count(); ++i) {
savePathTxt->setItemText(i, savePathTxt->itemText(i) + single_file_relpath);
}
}
// Setting file name // Setting file name
fileName = misc::toQStringU(t->name()); fileName = misc::toQStringU(t->name());
hash = misc::toQString(t->info_hash()); hash = misc::toQString(t->info_hash());
@ -258,32 +216,54 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
if(t->num_files() > 1) { if(t->num_files() > 1) {
// List files in torrent // List files in torrent
PropListModel->setupModelData(*t); PropListModel->setupModelData(*t);
// Expand first item if possible
torrentContentList->expand(PropListModel->index(0, 0));
connect(PropDelegate, SIGNAL(filteredFilesChanged()), this, SLOT(updateDiskSpaceLabels())); connect(PropDelegate, SIGNAL(filteredFilesChanged()), this, SLOT(updateDiskSpaceLabels()));
// Loads files path in the torrent // Loads files path in the torrent
for(uint i=0; i<nbFiles; ++i) { for(uint i=0; i<nbFiles; ++i) {
files_path << misc::toQStringU(t->file_at(i).path.string()); files_path << misc::toQStringU(t->file_at(i).path.string());
} }
} }
// Load save path history
loadSavePathHistory();
// Connect slots
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(updateDiskSpaceLabels())); connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(updateDiskSpaceLabels()));
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(updateSavePathCurrentText(QString))); connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(updateSavePathCurrentText(QString)));
updateDiskSpaceLabels(); if(nbFiles == 1) {
// Load custom labels // Update properties model whenever the file path is edited
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(renameTorrentNameInModel(QString)));
settings.beginGroup(QString::fromUtf8("TransferListFilters"));
const QStringList &customLabels = settings.value("customLabels", QStringList()).toStringList();
comboLabel->addItem("");
foreach(const QString& label, customLabels) {
comboLabel->addItem(label);
} }
QString save_path = savePathTxt->currentText();
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
save_path = save_path.replace("/", "\\");
#endif
if(!save_path.endsWith(QDir::separator()))
save_path += QDir::separator();
// If the torrent has a root folder, append it to the save path
if(!root_folder.isEmpty()) {
save_path += root_folder;
}
if(nbFiles == 1) {
// single file torrent
QString single_file_relpath = misc::toQStringU(t->file_at(0).path.string());
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
single_file_relpath = single_file_relpath.replace("/", "\\");
#endif
save_path += single_file_relpath;
}
savePathTxt->setEditText(save_path);
// Show the dialog // Show the dialog
show(); show();
// Hide useless widgets
if(t->num_files() <= 1) if(t->num_files() <= 1)
hideTorrentContent(); hideTorrentContent();
} }
void torrentAdditionDialog::displayContentListMenu(const QPoint&) { void torrentAdditionDialog::displayContentListMenu(const QPoint&) {
Q_ASSERT(!is_magnet && t->num_files() > 1);
QMenu myFilesLlistMenu; QMenu myFilesLlistMenu;
const QModelIndexList &selectedRows = torrentContentList->selectionModel()->selectedRows(0); const QModelIndexList &selectedRows = torrentContentList->selectionModel()->selectedRows(0);
QAction *actRename = 0; QAction *actRename = 0;
@ -326,6 +306,7 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
} }
void torrentAdditionDialog::renameSelectedFile() { void torrentAdditionDialog::renameSelectedFile() {
Q_ASSERT(!is_magnet && t->num_files() > 1);
const QModelIndexList &selectedIndexes = torrentContentList->selectionModel()->selectedRows(0); const QModelIndexList &selectedIndexes = torrentContentList->selectionModel()->selectedRows(0);
Q_ASSERT(selectedIndexes.size() == 1); Q_ASSERT(selectedIndexes.size() == 1);
const QModelIndex &index = selectedIndexes.first(); const QModelIndex &index = selectedIndexes.first();
@ -461,8 +442,9 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
} }
void torrentAdditionDialog::on_browseButton_clicked(){ void torrentAdditionDialog::on_browseButton_clicked(){
Q_ASSERT(!is_magnet);
QString new_path; QString new_path;
if(!is_magnet && t->num_files() == 1) { if(t->num_files() == 1) {
new_path = QFileDialog::getSaveFileName(this, tr("Choose save path"), savePathTxt->currentText()); new_path = QFileDialog::getSaveFileName(this, tr("Choose save path"), savePathTxt->currentText());
} else { } else {
QString root_folder; QString root_folder;
@ -510,11 +492,13 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
} }
bool torrentAdditionDialog::allFiltered() const { bool torrentAdditionDialog::allFiltered() const {
Q_ASSERT(!is_magnet);
return PropListModel->allFiltered(); return PropListModel->allFiltered();
} }
void torrentAdditionDialog::savePiecesPriorities(){ void torrentAdditionDialog::savePiecesPriorities(){
qDebug("Saving pieces priorities"); qDebug("Saving pieces priorities");
Q_ASSERT(!is_magnet);
const std::vector<int> &priorities = PropListModel->getFilesPriorities(t->num_files()); const std::vector<int> &priorities = PropListModel->getFilesPriorities(t->num_files());
TorrentTempData::setFilesPriority(hash, priorities); TorrentTempData::setFilesPriority(hash, priorities);
} }
@ -700,7 +684,18 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
} }
} }
QStringList torrentAdditionDialog::getSavePathHistory() const { void torrentAdditionDialog::loadSavePathHistory() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
return settings.value("TorrentAdditionDlg/save_path_history").toStringList(); // Load save path history
QStringList raw_path_history = settings.value("TorrentAdditionDlg/save_path_history").toStringList();
foreach(const QString &sp, raw_path_history) {
if(QDir(sp) != QDir(defaultSavePath)) {
QString dsp = sp;
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
dsp = dsp.replace("/", "\\");
#endif
path_history << sp;
savePathTxt->addItem(dsp);
}
}
} }

View File

@ -70,6 +70,7 @@ public:
void showLoad(QString filePath, QString from_url=QString::null); void showLoad(QString filePath, QString from_url=QString::null);
QString getCurrentTruncatedSavePath(QString* root_folder_or_file_name = 0) const; QString getCurrentTruncatedSavePath(QString* root_folder_or_file_name = 0) const;
QString getTruncatedSavePath(QString save_path, QString* root_folder_or_file_name = 0) const; QString getTruncatedSavePath(QString save_path, QString* root_folder_or_file_name = 0) const;
bool allFiltered() const;
public slots: public slots:
void displayContentListMenu(const QPoint&); void displayContentListMenu(const QPoint&);
@ -77,13 +78,12 @@ public slots:
void updateDiskSpaceLabels(); void updateDiskSpaceLabels();
void on_browseButton_clicked(); void on_browseButton_clicked();
void on_CancelButton_clicked(); void on_CancelButton_clicked();
bool allFiltered() const;
void savePiecesPriorities(); void savePiecesPriorities();
void on_OkButton_clicked(); void on_OkButton_clicked();
void renameTorrentNameInModel(QString file_path); void renameTorrentNameInModel(QString file_path);
void hideTorrentContent(); void hideTorrentContent();
void saveTruncatedPathHistory(); void saveTruncatedPathHistory();
QStringList getSavePathHistory() const; void loadSavePathHistory();
void updateLabelInSavePath(QString label); void updateLabelInSavePath(QString label);
void updateSavePathCurrentText(QString path); void updateSavePathCurrentText(QString path);
void resetComboLabelIndex(QString text); void resetComboLabelIndex(QString text);