1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-22 20:44:15 +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,41 +61,34 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
display_path = display_path.replace("/", "\\");
#endif
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()) {
addInPause->setChecked(true);
//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
addInSeed->setVisible(false);
#endif
// Set Add button as default
OkButton->setDefault(true);
}
}
torrentAdditionDialog::~torrentAdditionDialog() {
torrentAdditionDialog::~torrentAdditionDialog() {
saveSettings();
delete PropDelegate;
delete PropListModel;
}
}
void torrentAdditionDialog::readSettings() {
void torrentAdditionDialog::readSettings() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
// Restore size and position
resize(settings.value(QString::fromUtf8("TorrentAdditionDlg/size"), size()).toSize());
@ -109,9 +102,9 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
torrentContentList->setColumnWidth(i, contentColsWidths.at(i));
}
}
}
}
void torrentAdditionDialog::saveSettings() {
void torrentAdditionDialog::saveSettings() {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
if(!is_magnet && t.get() && t->is_valid() && t->num_files() > 1) {
QStringList contentColsWidths;
@ -124,15 +117,15 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
settings.setValue("TorrentAdditionDlg/size", size()+QSize(0, hidden_height));
qDebug("pos: (%d, %d)", pos().x(), pos().y());
settings.setValue("TorrentAdditionDlg/pos", pos());
}
}
void torrentAdditionDialog::renameTorrentNameInModel(QString file_path) {
void torrentAdditionDialog::renameTorrentNameInModel(QString file_path) {
file_path = file_path.replace("\\", "/");
// Rename in torrent files model too
PropListModel->setData(PropListModel->index(0, 0), file_path.split("/", QString::SkipEmptyParts).last());
}
}
void torrentAdditionDialog::hideTorrentContent() {
void torrentAdditionDialog::hideTorrentContent() {
// Disable useless widgets
hidden_height += torrentContentList->height();
torrentContentList->setVisible(false);
@ -145,50 +138,44 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
// Resize main window
setMinimumSize(0, 0);
resize(width(), height()-hidden_height);
}
}
void torrentAdditionDialog::showLoadMagnetURI(QString magnet_uri) {
void torrentAdditionDialog::showLoadMagnetURI(QString magnet_uri) {
show();
is_magnet = true;
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
hash = misc::magnetUriToHash(magnet_uri);
if(hash.isEmpty()) {
BTSession->addConsoleMessage(tr("Unable to decode magnet link:")+QString::fromUtf8(" '")+from_url+QString::fromUtf8("'"), QString::fromUtf8("red"));
return;
}
// Set torrent name
fileName = misc::magnetUriToName(magnet_uri);
if(fileName.isEmpty()) {
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>"));
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(updateSavePathCurrentText(QString)));
// Update display
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
hideTorrentContent();
}
}
void torrentAdditionDialog::showLoad(QString filePath, QString from_url) {
void torrentAdditionDialog::showLoad(QString filePath, QString from_url) {
is_magnet = false;
if(!QFile::exists(filePath)) {
close();
return;
}
this->filePath = filePath;
this->from_url = from_url;
// Getting torrent file informations
@ -213,37 +200,8 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
close();
return;
}
if(nbFiles == 1) {
// Update properties model whenever the file path is edited
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(renameTorrentNameInModel(QString)));
}
// Truncate root folder
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
fileName = misc::toQStringU(t->name());
hash = misc::toQString(t->info_hash());
@ -258,32 +216,54 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
if(t->num_files() > 1) {
// List files in torrent
PropListModel->setupModelData(*t);
// Expand first item if possible
torrentContentList->expand(PropListModel->index(0, 0));
connect(PropDelegate, SIGNAL(filteredFilesChanged()), this, SLOT(updateDiskSpaceLabels()));
// Loads files path in the torrent
for(uint i=0; i<nbFiles; ++i) {
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(updateSavePathCurrentText(QString)));
updateDiskSpaceLabels();
// 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);
}
// Show the dialog
show();
if(t->num_files() <= 1)
hideTorrentContent();
if(nbFiles == 1) {
// Update properties model whenever the file path is edited
connect(savePathTxt, SIGNAL(editTextChanged(QString)), this, SLOT(renameTorrentNameInModel(QString)));
}
void torrentAdditionDialog::displayContentListMenu(const QPoint&) {
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();
// Hide useless widgets
if(t->num_files() <= 1)
hideTorrentContent();
}
void torrentAdditionDialog::displayContentListMenu(const QPoint&) {
Q_ASSERT(!is_magnet && t->num_files() > 1);
QMenu myFilesLlistMenu;
const QModelIndexList &selectedRows = torrentContentList->selectionModel()->selectedRows(0);
QAction *actRename = 0;
@ -323,9 +303,10 @@ 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);
Q_ASSERT(selectedIndexes.size() == 1);
const QModelIndex &index = selectedIndexes.first();
@ -461,8 +442,9 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
}
void torrentAdditionDialog::on_browseButton_clicked(){
Q_ASSERT(!is_magnet);
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());
} else {
QString root_folder;
@ -510,11 +492,13 @@ torrentAdditionDialog::torrentAdditionDialog(GUI *parent, Bittorrent* _BTSession
}
bool torrentAdditionDialog::allFiltered() const {
Q_ASSERT(!is_magnet);
return PropListModel->allFiltered();
}
void torrentAdditionDialog::savePiecesPriorities(){
qDebug("Saving pieces priorities");
Q_ASSERT(!is_magnet);
const std::vector<int> &priorities = PropListModel->getFilesPriorities(t->num_files());
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"));
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);
QString getCurrentTruncatedSavePath(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:
void displayContentListMenu(const QPoint&);
@ -77,13 +78,12 @@ public slots:
void updateDiskSpaceLabels();
void on_browseButton_clicked();
void on_CancelButton_clicked();
bool allFiltered() const;
void savePiecesPriorities();
void on_OkButton_clicked();
void renameTorrentNameInModel(QString file_path);
void hideTorrentContent();
void saveTruncatedPathHistory();
QStringList getSavePathHistory() const;
void loadSavePathHistory();
void updateLabelInSavePath(QString label);
void updateSavePathCurrentText(QString path);
void resetComboLabelIndex(QString text);