Browse Source

- Remove old folders after renaming

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
7d66c07cef
  1. 13
      src/misc.h
  2. 316
      src/propertieswidget.cpp
  3. 3
      src/torrentfilesmodel.h

13
src/misc.h

@ -269,6 +269,19 @@ public:
list.insert(i, value); list.insert(i, value);
} }
static bool removeEmptyTree(QString path) {
QDir dir(path);
foreach(QString child, dir.entryList(QDir::AllDirs)) {
if(child == "." || child == "..") continue;
return removeEmptyTree(dir.absoluteFilePath(child));
}
QString dir_name = dir.dirName();
if(dir.cdUp()) {
return dir.rmdir(dir_name);
}
return false;
}
static QString magnetUriToHash(QString magnet_uri) { static QString magnetUriToHash(QString magnet_uri) {
QString hash = ""; QString hash = "";
QRegExp regHex("urn:btih:([0-9A-Za-z]+)"); QRegExp regHex("urn:btih:([0-9A-Za-z]+)");

316
src/propertieswidget.cpp

@ -529,181 +529,189 @@ void PropertiesWidget::renameSelectedFile() {
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
if(misc::toQString(h.get_torrent_info().file_at(i).path.string()).compare(new_name, Qt::CaseInsensitive) == 0) { if(misc::toQString(h.get_torrent_info().file_at(i).path.string()).compare(new_name, Qt::CaseInsensitive) == 0) {
#else #else
if(misc::toQString(h.get_torrent_info().file_at(i).path.string()).compare(new_name, Qt::CaseSensitive) == 0) { if(misc::toQString(h.get_torrent_info().file_at(i).path.string()).compare(new_name, Qt::CaseSensitive) == 0) {
#endif #endif
// Display error message // Display error message
QMessageBox::warning(this, tr("The file could not be renamed"), QMessageBox::warning(this, tr("The file could not be renamed"),
tr("This name is already in use in this folder. Please use a different name."), tr("This name is already in use in this folder. Please use a different name."),
QMessageBox::Ok); QMessageBox::Ok);
return; return;
}
} }
} qDebug("Renaming %s to %s", old_name.toLocal8Bit().data(), new_name.toLocal8Bit().data());
qDebug("Renaming %s to %s", old_name.toLocal8Bit().data(), new_name.toLocal8Bit().data()); h.rename_file(file_index, new_name);
h.rename_file(file_index, new_name); // Rename if torrent files model too
// Rename if torrent files model too if(new_name_last.endsWith(".!qB"))
if(new_name_last.endsWith(".!qB")) new_name_last.chop(4);
new_name_last.chop(4); PropListModel->setData(index, new_name_last);
PropListModel->setData(index, new_name_last); } else {
} else { // Folder renaming
// Folder renaming QStringList path_items;
QStringList path_items; path_items << index.data().toString();
path_items << index.data().toString(); QModelIndex parent = PropListModel->parent(index);
QModelIndex parent = PropListModel->parent(index); while(parent.isValid()) {
while(parent.isValid()) { path_items.prepend(parent.data().toString());
path_items.prepend(parent.data().toString()); parent = PropListModel->parent(parent);
parent = PropListModel->parent(parent); }
} QString old_path = path_items.join(QDir::separator());
QString old_path = path_items.join(QDir::separator()); path_items.removeLast();
path_items.removeLast(); path_items << new_name_last;
path_items << new_name_last; QString new_path = path_items.join(QDir::separator());
QString new_path = path_items.join(QDir::separator()); // Check for overwriting
// XXX: Check for overwriting int num_files = h.num_files();
for(int i=0; i<h.num_files(); ++i) { for(int i=0; i<num_files; ++i) {
QString current_name = misc::toQString(h.get_torrent_info().file_at(i).path.string()); QString current_name = misc::toQString(h.get_torrent_info().file_at(i).path.string());
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
if(current_name.contains(new_path, Qt::CaseInsensitive)) { if(current_name.contains(new_path, Qt::CaseInsensitive)) {
#else #else
if(current_name.contains(new_path, Qt::CaseSensitive)) { if(current_name.contains(new_path, Qt::CaseSensitive)) {
#endif #endif
QMessageBox::warning(this, tr("The folder could not be renamed"), QMessageBox::warning(this, tr("The folder could not be renamed"),
tr("This name is already in use in this folder. Please use a different name."), tr("This name is already in use in this folder. Please use a different name."),
QMessageBox::Ok); QMessageBox::Ok);
return; return;
}
}
// Replace path in all files
for(int i=0; i<num_files; ++i) {
QString current_name = misc::toQString(h.get_torrent_info().file_at(i).path.string());
QString new_name = current_name.replace(old_path, new_path);
qDebug("Rename %s to %s", current_name.toLocal8Bit().data(), new_name.toLocal8Bit().data());
h.rename_file(i, new_name);
}
// Rename folder in torrent files model too
PropListModel->setData(index, new_name_last);
// Remove old folder
QDir old_folder(h.save_path()+QDir::separator()+old_path);
int timeout = 10;
while(!misc::removeEmptyTree(old_folder.absolutePath()) && timeout > 0) {
SleeperThread::msleep(100);
--timeout;
}
} }
} }
// Replace path in all files
for(int i=0; i<h.num_files(); ++i) {
QString current_name = misc::toQString(h.get_torrent_info().file_at(i).path.string());
QString new_name = current_name.replace(old_path, new_path);
qDebug("Rename %s to %s", current_name.toLocal8Bit().data(), new_name.toLocal8Bit().data());
h.rename_file(i, new_name);
}
// Rename folder in torrent files model too
PropListModel->setData(index, new_name_last);
} }
}
}
void PropertiesWidget::ignoreSelection(){ void PropertiesWidget::ignoreSelection(){
QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY); QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY);
foreach(const QModelIndex &index, selectedIndexes){ foreach(const QModelIndex &index, selectedIndexes){
if(PropListModel->data(index) != QVariant(IGNORED)){ if(PropListModel->data(index) != QVariant(IGNORED)){
PropListModel->setData(index, QVariant(IGNORED)); PropListModel->setData(index, QVariant(IGNORED));
filteredFilesChanged(); filteredFilesChanged();
}
}
} }
}
}
void PropertiesWidget::normalSelection(){ void PropertiesWidget::normalSelection(){
QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY); QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY);
foreach(const QModelIndex &index, selectedIndexes){ foreach(const QModelIndex &index, selectedIndexes){
if(PropListModel->data(index) != QVariant(NORMAL)){ if(PropListModel->data(index) != QVariant(NORMAL)){
PropListModel->setData(index, QVariant(NORMAL)); PropListModel->setData(index, QVariant(NORMAL));
filteredFilesChanged(); filteredFilesChanged();
}
}
} }
}
}
void PropertiesWidget::highSelection(){ void PropertiesWidget::highSelection(){
QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY); QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY);
foreach(const QModelIndex &index, selectedIndexes){ foreach(const QModelIndex &index, selectedIndexes){
if(PropListModel->data(index) != QVariant(HIGH)){ if(PropListModel->data(index) != QVariant(HIGH)){
PropListModel->setData(index, QVariant(HIGH)); PropListModel->setData(index, QVariant(HIGH));
filteredFilesChanged(); filteredFilesChanged();
}
}
} }
}
}
void PropertiesWidget::maximumSelection(){ void PropertiesWidget::maximumSelection(){
QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY); QModelIndexList selectedIndexes = filesList->selectionModel()->selectedRows(PRIORITY);
foreach(const QModelIndex &index, selectedIndexes){ foreach(const QModelIndex &index, selectedIndexes){
if(PropListModel->data(index) != QVariant(MAXIMUM)){ if(PropListModel->data(index) != QVariant(MAXIMUM)){
PropListModel->setData(index, QVariant(MAXIMUM)); PropListModel->setData(index, QVariant(MAXIMUM));
filteredFilesChanged(); filteredFilesChanged();
}
}
} }
}
}
void PropertiesWidget::askWebSeed(){ void PropertiesWidget::askWebSeed(){
bool ok; bool ok;
// Ask user for a new url seed // Ask user for a new url seed
QString url_seed = QInputDialog::getText(this, tr("New url seed", "New HTTP source"), QString url_seed = QInputDialog::getText(this, tr("New url seed", "New HTTP source"),
tr("New url seed:"), QLineEdit::Normal, tr("New url seed:"), QLineEdit::Normal,
QString::fromUtf8("http://www."), &ok); QString::fromUtf8("http://www."), &ok);
if(!ok) return; if(!ok) return;
qDebug("Adding %s web seed", url_seed.toLocal8Bit().data()); qDebug("Adding %s web seed", url_seed.toLocal8Bit().data());
if(!listWebSeeds->findItems(url_seed, Qt::MatchFixedString).empty()) { if(!listWebSeeds->findItems(url_seed, Qt::MatchFixedString).empty()) {
QMessageBox::warning(this, tr("qBittorrent"), QMessageBox::warning(this, tr("qBittorrent"),
tr("This url seed is already in the list."), tr("This url seed is already in the list."),
QMessageBox::Ok); QMessageBox::Ok);
return; return;
} }
h.add_url_seed(url_seed); h.add_url_seed(url_seed);
// Refresh the seeds list // Refresh the seeds list
loadUrlSeeds(); loadUrlSeeds();
} }
void PropertiesWidget::deleteSelectedUrlSeeds(){ void PropertiesWidget::deleteSelectedUrlSeeds(){
QList<QListWidgetItem *> selectedItems = listWebSeeds->selectedItems(); QList<QListWidgetItem *> selectedItems = listWebSeeds->selectedItems();
bool change = false; bool change = false;
foreach(QListWidgetItem *item, selectedItems){ foreach(QListWidgetItem *item, selectedItems){
QString url_seed = item->text(); QString url_seed = item->text();
h.remove_url_seed(url_seed); h.remove_url_seed(url_seed);
change = true; change = true;
} }
if(change){ if(change){
// Refresh list // Refresh list
loadUrlSeeds(); loadUrlSeeds();
} }
} }
bool PropertiesWidget::applyPriorities() { bool PropertiesWidget::applyPriorities() {
qDebug("Saving pieces priorities"); qDebug("Saving pieces priorities");
std::vector<int> priorities = PropListModel->getFilesPriorities(h.get_torrent_info().num_files()); std::vector<int> priorities = PropListModel->getFilesPriorities(h.get_torrent_info().num_files());
bool first_last_piece_first = false; bool first_last_piece_first = false;
// Save first/last piece first option state // Save first/last piece first option state
if(h.first_last_piece_first()) if(h.first_last_piece_first())
first_last_piece_first = true; first_last_piece_first = true;
// Prioritize the files // Prioritize the files
qDebug("prioritize files: %d", priorities[0]); qDebug("prioritize files: %d", priorities[0]);
h.prioritize_files(priorities); h.prioritize_files(priorities);
// Restore first/last piece first option if necessary // Restore first/last piece first option if necessary
if(first_last_piece_first) if(first_last_piece_first)
h.prioritize_first_last_piece(true); h.prioritize_first_last_piece(true);
return true; return true;
} }
void PropertiesWidget::on_changeSavePathButton_clicked() { void PropertiesWidget::on_changeSavePathButton_clicked() {
if(!h.is_valid()) return; if(!h.is_valid()) return;
QString dir; QString dir;
QDir saveDir(h.save_path()); QDir saveDir(h.save_path());
if(saveDir.exists()){ if(saveDir.exists()){
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), h.save_path()); dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), h.save_path());
}else{ }else{
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath()); dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath());
} }
if(!dir.isNull()){ if(!dir.isNull()){
// Check if savePath exists // Check if savePath exists
QDir savePath(dir); QDir savePath(dir);
if(!savePath.exists()){ if(!savePath.exists()){
if(!savePath.mkpath(savePath.path())){ if(!savePath.mkpath(savePath.path())){
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"));
return; return;
}
}
// Save savepath
TorrentPersistentData::saveSavePath(h.hash(), savePath.path());
// Actually move storage
if(!BTSession->useTemporaryFolder() || h.is_seed())
h.move_storage(savePath.path());
// Update save_path in dialog
save_path->setText(savePath.path());
} }
} }
// Save savepath
TorrentPersistentData::saveSavePath(h.hash(), savePath.path());
// Actually move storage
if(!BTSession->useTemporaryFolder() || h.is_seed())
h.move_storage(savePath.path());
// Update save_path in dialog
save_path->setText(savePath.path());
}
}
void PropertiesWidget::filteredFilesChanged() { void PropertiesWidget::filteredFilesChanged() {
if(h.is_valid()) { if(h.is_valid()) {
applyPriorities(); applyPriorities();
} }
} }

3
src/torrentfilesmodel.h

@ -492,7 +492,8 @@ public:
return; return;
} }
// Create parent folder // Create parent folder
TreeItem *current_parent = new TreeItem(misc::toQString(t.name()), parent); QString root_name = misc::toQString(t.file_at(0).path.string()).split('/').first();
TreeItem *current_parent = new TreeItem(root_name, parent);
//parent->appendChild(current_parent); //parent->appendChild(current_parent);
TreeItem *root_folder = current_parent; TreeItem *root_folder = current_parent;

Loading…
Cancel
Save