Browse Source

Improved file saving in torrent addition dialog (uTorrent like)

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
ac80dd78a8
  1. 18
      src/misc.cpp
  2. 73
      src/torrentadditiondlg.cpp
  3. 1
      src/torrentadditiondlg.h

18
src/misc.cpp

@ -179,6 +179,14 @@ long long misc::freeDiskSpaceOnPath(QString path) {
} }
QString misc::truncateRootFolder(boost::intrusive_ptr<torrent_info> t) { QString misc::truncateRootFolder(boost::intrusive_ptr<torrent_info> t) {
if(t->num_files() == 1) {
// Single file torrent
// Remove possible subfolders
QString path = QString::fromUtf8(t->file_at(0).path.string().c_str());
QStringList path_parts = path.split("/", QString::SkipEmptyParts);
t->rename_file(0, path_parts.last().toUtf8().data());
return QString::null;
}
QString root_folder; QString root_folder;
int i = 0; int i = 0;
for(torrent_info::file_iterator it = t->begin_files(); it < t->end_files(); it++) { for(torrent_info::file_iterator it = t->begin_files(); it < t->end_files(); it++) {
@ -194,9 +202,17 @@ QString misc::truncateRootFolder(boost::intrusive_ptr<torrent_info> t) {
} }
QString misc::truncateRootFolder(libtorrent::torrent_handle h) { QString misc::truncateRootFolder(libtorrent::torrent_handle h) {
torrent_info t = h.get_torrent_info();
if(t.num_files() == 1) {
// Single file torrent
// Remove possible subfolders
QString path = QString::fromUtf8(t.file_at(0).path.string().c_str());
QStringList path_parts = path.split("/", QString::SkipEmptyParts);
t.rename_file(0, path_parts.last().toUtf8().data());
return QString::null;
}
QString root_folder; QString root_folder;
int i = 0; int i = 0;
torrent_info t = h.get_torrent_info();
for(torrent_info::file_iterator it = t.begin_files(); it < t.end_files(); it++) { for(torrent_info::file_iterator it = t.begin_files(); it < t.end_files(); it++) {
QString path = QString::fromUtf8(it->path.string().c_str()); QString path = QString::fromUtf8(it->path.string().c_str());
QStringList path_parts = path.split("/", QString::SkipEmptyParts); QStringList path_parts = path.split("/", QString::SkipEmptyParts);

73
src/torrentadditiondlg.cpp

@ -101,6 +101,12 @@ void torrentAdditionDialog::saveSettings() {
settings.setValue("TorrentAdditionDlg/pos", pos()); settings.setValue("TorrentAdditionDlg/pos", pos());
} }
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::showLoadMagnetURI(QString magnet_uri) { void torrentAdditionDialog::showLoadMagnetURI(QString magnet_uri) {
show(); show();
is_magnet = true; is_magnet = true;
@ -170,14 +176,35 @@ void torrentAdditionDialog::showLoad(QString filePath, QString from_url) {
close(); close();
return; return;
} }
nbFiles = t->num_files();
if(nbFiles == 0) {
// Empty torrent file!?
close();
return;
}
if(nbFiles == 1) {
// Update properties model whenever the file path is edited
connect(savePathTxt, SIGNAL(textChanged(QString)), this, SLOT(renameTorrentNameInModel(QString)));
}
QString root_folder = misc::truncateRootFolder(t); QString root_folder = misc::truncateRootFolder(t);
QString save_path = savePathTxt->text();
#ifdef Q_WS_WIN
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()) { if(!root_folder.isEmpty()) {
QString save_path = savePathTxt->text(); savePathTxt->setText(save_path + root_folder + QDir::separator());
if(!save_path.endsWith(QDir::separator())) }
save_path += QDir::separator(); if(nbFiles == 1) {
savePathTxt->setText(save_path + root_folder); // single file torrent
QString single_file_relpath = misc::toQStringU(t->file_at(0).path.string());
#ifdef Q_WS_WIN
single_file_relpath = single_file_relpath.replace("/", "\\");
#endif
savePathTxt->setText(save_path+single_file_relpath);
} }
nbFiles = t->num_files();
// 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());
@ -217,7 +244,7 @@ void torrentAdditionDialog::displayContentListMenu(const QPoint&) {
QMenu myFilesLlistMenu; QMenu myFilesLlistMenu;
const QModelIndexList &selectedRows = torrentContentList->selectionModel()->selectedRows(0); const QModelIndexList &selectedRows = torrentContentList->selectionModel()->selectedRows(0);
QAction *actRename = 0; QAction *actRename = 0;
if(selectedRows.size() == 1) { if(selectedRows.size() == 1 && t->num_files() > 1) {
actRename = myFilesLlistMenu.addAction(QIcon(QString::fromUtf8(":/Icons/oxygen/edit_clear.png")), tr("Rename...")); actRename = myFilesLlistMenu.addAction(QIcon(QString::fromUtf8(":/Icons/oxygen/edit_clear.png")), tr("Rename..."));
myFilesLlistMenu.addSeparator(); myFilesLlistMenu.addSeparator();
} }
@ -381,23 +408,27 @@ void torrentAdditionDialog::renameSelectedFile() {
} }
void torrentAdditionDialog::on_browseButton_clicked(){ void torrentAdditionDialog::on_browseButton_clicked(){
QString dir; QString new_path;
QString save_path = savePathTxt->text(); QString save_path = savePathTxt->text();
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
save_path = save_path.replace("\\", "/"); save_path = save_path.replace("\\", "/");
#endif #endif
save_path = misc::expandPath(save_path); save_path = misc::expandPath(save_path);
const QDir &saveDir(save_path); if(t->num_files() == 1) {
if(!save_path.isEmpty() && saveDir.exists()){ new_path = QFileDialog::getSaveFileName(this, tr("Choose save path"), save_path);
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), saveDir.absolutePath()); } else {
}else{ const QDir saveDir(save_path);
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath()); if(!save_path.isEmpty() && saveDir.exists()){
new_path = QFileDialog::getExistingDirectory(this, tr("Choose save path"), saveDir.absolutePath());
}else{
new_path = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath());
}
} }
if(!dir.isNull()){ if(!new_path.isEmpty()){
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
dir = dir.replace("/", "\\"); new_path = new_path.replace("/", "\\");
#endif #endif
savePathTxt->setText(dir); savePathTxt->setText(new_path);
} }
} }
@ -424,7 +455,15 @@ void torrentAdditionDialog::renameSelectedFile() {
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
save_path = save_path.replace("\\", "/"); save_path = save_path.replace("\\", "/");
#endif #endif
QDir savePath(misc::expandPath(save_path)); save_path = misc::expandPath(save_path);
if(t->num_files() == 1) {
// Remove file name
QStringList parts = save_path.split("/", QString::SkipEmptyParts);
const QString single_file_name = parts.takeLast();
files_path.replace(0, single_file_name);
save_path = "/"+parts.join("/");
}
QDir savePath(save_path);
// Check if savePath exists // Check if savePath exists
if(!savePath.exists()){ if(!savePath.exists()){
if(!savePath.mkpath(savePath.path())){ if(!savePath.mkpath(savePath.path())){
@ -467,7 +506,7 @@ void torrentAdditionDialog::renameSelectedFile() {
// Skip file checking and directly start seeding // Skip file checking and directly start seeding
if(addInSeed->isChecked()) { if(addInSeed->isChecked()) {
// Check if local file(s) actually exist // Check if local file(s) actually exist
if(is_magnet || savePath.exists(misc::toQStringU(t->name()))) { if(is_magnet || QFile::exists(savePathTxt->text())) {
TorrentTempData::setSeedingMode(hash, true); TorrentTempData::setSeedingMode(hash, true);
} else { } else {
QMessageBox::warning(0, tr("Seeding mode error"), tr("You chose to skip file checking. However, local files do not seem to exist in the current destionation folder. Please disable this feature or update the save path.")); QMessageBox::warning(0, tr("Seeding mode error"), tr("You chose to skip file checking. However, local files do not seem to exist in the current destionation folder. Please disable this feature or update the save path."));

1
src/torrentadditiondlg.h

@ -78,6 +78,7 @@ public slots:
bool allFiltered() const; bool allFiltered() const;
void savePiecesPriorities(); void savePiecesPriorities();
void on_OkButton_clicked(); void on_OkButton_clicked();
void renameTorrentNameInModel(QString file_path);
public slots: public slots:
void updateLabelInSavePath(QString label); void updateLabelInSavePath(QString label);

Loading…
Cancel
Save