Code formatting

Refactor
This commit is contained in:
Chocobo1 2017-01-30 20:22:03 +08:00
parent 0939875ca8
commit 51e3e5438b
2 changed files with 116 additions and 113 deletions

View file

@ -71,7 +71,7 @@ public:
private slots: private slots:
void showAdvancedSettings(bool show); void showAdvancedSettings(bool show);
void displayContentTreeMenu(const QPoint&); void displayContentTreeMenu(const QPoint &);
void updateDiskSpaceLabel(); void updateDiskSpaceLabel();
void onSavePathChanged(int); void onSavePathChanged(int);
void renameSelectedFile(); void renameSelectedFile();
@ -93,7 +93,7 @@ private:
bool loadMagnet(const BitTorrent::MagnetUri &magnetUri); bool loadMagnet(const BitTorrent::MagnetUri &magnetUri);
void populateSavePathComboBox(); void populateSavePathComboBox();
void saveSavePathHistory() const; void saveSavePathHistory() const;
int indexOfSavePath(const QString& save_path); int indexOfSavePath(const QString &save_path);
void loadState(); void loadState();
void saveState(); void saveState();
void setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText = QString()); void setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText = QString());

View file

@ -210,7 +210,7 @@ void PropertiesWidget::showPiecesDownloaded(bool show)
void PropertiesWidget::setVisibility(bool visible) void PropertiesWidget::setVisibility(bool visible)
{ {
if (!visible && ( state == VISIBLE) ) { if (!visible && (state == VISIBLE)) {
QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget()); QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget());
m_ui->stackedProperties->setVisible(false); m_ui->stackedProperties->setVisible(false);
slideSizes = hSplitter->sizes(); slideSizes = hSplitter->sizes();
@ -222,7 +222,7 @@ void PropertiesWidget::setVisibility(bool visible)
return; return;
} }
if (visible && ( state == REDUCED) ) { if (visible && (state == REDUCED)) {
m_ui->stackedProperties->setVisible(true); m_ui->stackedProperties->setVisible(true);
QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget()); QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget());
hSplitter->handle(1)->setDisabled(false); hSplitter->handle(1)->setDisabled(false);
@ -363,7 +363,7 @@ void PropertiesWidget::readSettings()
void PropertiesWidget::saveSettings() void PropertiesWidget::saveSettings()
{ {
Preferences *const pref = Preferences::instance(); Preferences *const pref = Preferences::instance();
pref->setPropVisible(state==VISIBLE); pref->setPropVisible(state == VISIBLE);
// Splitter sizes // Splitter sizes
QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget()); QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget());
QList<int> sizes; QList<int> sizes;
@ -397,10 +397,10 @@ void PropertiesWidget::loadDynamicData()
m_ui->wasted->setText(Utils::Misc::friendlyUnit(m_torrent->wastedSize())); m_ui->wasted->setText(Utils::Misc::friendlyUnit(m_torrent->wastedSize()));
m_ui->upTotal->setText(tr("%1 (%2 this session)").arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload())) m_ui->upTotal->setText(tr("%1 (%2 this session)").arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload()))
.arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadUpload()))); .arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadUpload())));
m_ui->dlTotal->setText(tr("%1 (%2 this session)").arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload())) m_ui->dlTotal->setText(tr("%1 (%2 this session)").arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload()))
.arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadDownload()))); .arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadDownload())));
m_ui->lbl_uplimit->setText(m_torrent->uploadLimit() <= 0 ? QString::fromUtf8(C_INFINITY) : Utils::Misc::friendlyUnit(m_torrent->uploadLimit(), true)); m_ui->lbl_uplimit->setText(m_torrent->uploadLimit() <= 0 ? QString::fromUtf8(C_INFINITY) : Utils::Misc::friendlyUnit(m_torrent->uploadLimit(), true));
@ -416,8 +416,8 @@ void PropertiesWidget::loadDynamicData()
m_ui->lbl_elapsed->setText(elapsed_txt); m_ui->lbl_elapsed->setText(elapsed_txt);
m_ui->lbl_connections->setText(tr("%1 (%2 max)", "%1 and %2 are numbers, e.g. 3 (10 max)") m_ui->lbl_connections->setText(tr("%1 (%2 max)", "%1 and %2 are numbers, e.g. 3 (10 max)")
.arg(m_torrent->connectionsCount()) .arg(m_torrent->connectionsCount())
.arg(m_torrent->connectionsLimit() < 0 ? QString::fromUtf8(C_INFINITY) : QString::number(m_torrent->connectionsLimit()))); .arg(m_torrent->connectionsLimit() < 0 ? QString::fromUtf8(C_INFINITY) : QString::number(m_torrent->connectionsLimit())));
m_ui->label_eta_val->setText(Utils::Misc::userFriendlyDuration(m_torrent->eta())); m_ui->label_eta_val->setText(Utils::Misc::userFriendlyDuration(m_torrent->eta()));
@ -429,20 +429,20 @@ void PropertiesWidget::loadDynamicData()
m_ui->shareRatio->setText(ratio > BitTorrent::TorrentHandle::MAX_RATIO ? QString::fromUtf8(C_INFINITY) : Utils::String::fromDouble(ratio, 2)); m_ui->shareRatio->setText(ratio > BitTorrent::TorrentHandle::MAX_RATIO ? QString::fromUtf8(C_INFINITY) : Utils::String::fromDouble(ratio, 2));
m_ui->label_seeds_val->setText(tr("%1 (%2 total)", "%1 and %2 are numbers, e.g. 3 (10 total)") m_ui->label_seeds_val->setText(tr("%1 (%2 total)", "%1 and %2 are numbers, e.g. 3 (10 total)")
.arg(QString::number(m_torrent->seedsCount())) .arg(QString::number(m_torrent->seedsCount()))
.arg(QString::number(m_torrent->totalSeedsCount()))); .arg(QString::number(m_torrent->totalSeedsCount())));
m_ui->label_peers_val->setText(tr("%1 (%2 total)", "%1 and %2 are numbers, e.g. 3 (10 total)") m_ui->label_peers_val->setText(tr("%1 (%2 total)", "%1 and %2 are numbers, e.g. 3 (10 total)")
.arg(QString::number(m_torrent->leechsCount())) .arg(QString::number(m_torrent->leechsCount()))
.arg(QString::number(m_torrent->totalLeechersCount()))); .arg(QString::number(m_torrent->totalLeechersCount())));
m_ui->label_dl_speed_val->setText(tr("%1 (%2 avg.)", "%1 and %2 are speed rates, e.g. 200KiB/s (100KiB/s avg.)") m_ui->label_dl_speed_val->setText(tr("%1 (%2 avg.)", "%1 and %2 are speed rates, e.g. 200KiB/s (100KiB/s avg.)")
.arg(Utils::Misc::friendlyUnit(m_torrent->downloadPayloadRate(), true)) .arg(Utils::Misc::friendlyUnit(m_torrent->downloadPayloadRate(), true))
.arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload() / (1 + m_torrent->activeTime() - m_torrent->finishedTime()), true))); .arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload() / (1 + m_torrent->activeTime() - m_torrent->finishedTime()), true)));
m_ui->label_upload_speed_val->setText(tr("%1 (%2 avg.)", "%1 and %2 are speed rates, e.g. 200KiB/s (100KiB/s avg.)") m_ui->label_upload_speed_val->setText(tr("%1 (%2 avg.)", "%1 and %2 are speed rates, e.g. 200KiB/s (100KiB/s avg.)")
.arg(Utils::Misc::friendlyUnit(m_torrent->uploadPayloadRate(), true)) .arg(Utils::Misc::friendlyUnit(m_torrent->uploadPayloadRate(), true))
.arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload() / (1 + m_torrent->activeTime()), true))); .arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload() / (1 + m_torrent->activeTime()), true)));
m_ui->label_last_complete_val->setText(m_torrent->lastSeenComplete().isValid() ? m_torrent->lastSeenComplete().toString(Qt::DefaultLocaleShortDate) : tr("Never")); m_ui->label_last_complete_val->setText(m_torrent->lastSeenComplete().isValid() ? m_torrent->lastSeenComplete().toString(Qt::DefaultLocaleShortDate) : tr("Never"));
@ -674,118 +674,121 @@ void PropertiesWidget::renameSelectedFile()
const QModelIndexList selectedIndexes = m_ui->filesList->selectionModel()->selectedRows(0); const QModelIndexList selectedIndexes = m_ui->filesList->selectionModel()->selectedRows(0);
if (selectedIndexes.size() != 1) if (selectedIndexes.size() != 1)
return; return;
const QModelIndex index = selectedIndexes.first(); const QModelIndex index = selectedIndexes.first();
if (!index.isValid()) if (!index.isValid())
return; return;
// Ask for new name // Ask for new name
bool ok; bool ok = false;
QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"), QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"),
tr("New name:"), QLineEdit::Normal, tr("New name:"), QLineEdit::Normal,
index.data().toString(), &ok).trimmed(); index.data().toString(), &ok).trimmed();
if (ok && !new_name_last.isEmpty()) { if (!ok || new_name_last.isEmpty())
if (!Utils::Fs::isValidFileSystemName(new_name_last)) { return;
MessageBoxRaised::warning(this, tr("The file could not be renamed"),
tr("This file name contains forbidden characters, please choose a different one."), if (!Utils::Fs::isValidFileSystemName(new_name_last)) {
QMessageBox::Ok); MessageBoxRaised::warning(this, tr("The file could not be renamed"),
tr("This file name contains forbidden characters, please choose a different one."),
QMessageBox::Ok);
return;
}
if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) {
// File renaming
const int file_index = PropListModel->getFileIndex(index);
if (!m_torrent || !m_torrent->hasMetadata()) return;
QString old_name = m_torrent->filePath(file_index);
if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB"))
new_name_last += ".!qB";
QStringList path_items = old_name.split("/");
path_items.removeLast();
path_items << new_name_last;
QString new_name = path_items.join("/");
if (old_name == new_name) {
qDebug("Name did not change");
return; return;
} }
if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) { new_name = Utils::Fs::expandPath(new_name);
// File renaming qDebug("New name: %s", qPrintable(new_name));
const int file_index = PropListModel->getFileIndex(index); // Check if that name is already used
if (!m_torrent || !m_torrent->hasMetadata()) return; for (int i = 0; i < m_torrent->filesCount(); ++i) {
QString old_name = m_torrent->filePath(file_index); if (i == file_index) continue;
if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB")) if (Utils::Fs::sameFileNames(m_torrent->filePath(i), new_name)) {
new_name_last += ".!qB"; // Display error message
QStringList path_items = old_name.split("/"); MessageBoxRaised::warning(this, tr("The file could not be renamed"),
path_items.removeLast(); tr("This name is already in use in this folder. Please use a different name."),
path_items << new_name_last; QMessageBox::Ok);
QString new_name = path_items.join("/");
if (old_name == new_name) {
qDebug("Name did not change");
return; return;
} }
new_name = Utils::Fs::expandPath(new_name);
qDebug("New name: %s", qPrintable(new_name));
// Check if that name is already used
for (int i = 0; i < m_torrent->filesCount(); ++i) {
if (i == file_index) continue;
if (Utils::Fs::sameFileNames(m_torrent->filePath(i), new_name)) {
// Display error message
MessageBoxRaised::warning(this, tr("The file could not be renamed"),
tr("This name is already in use in this folder. Please use a different name."),
QMessageBox::Ok);
return;
}
}
const bool force_recheck = QFile::exists(m_torrent->savePath(true) + "/" + new_name);
qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name));
m_torrent->renameFile(file_index, new_name);
// Force recheck
if (force_recheck) m_torrent->forceRecheck();
// Rename if torrent files model too
if (new_name_last.endsWith(".!qB"))
new_name_last.chop(4);
PropListModel->setData(index, new_name_last);
} }
else { const bool force_recheck = QFile::exists(m_torrent->savePath(true) + "/" + new_name);
// Folder renaming qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name));
QStringList path_items; m_torrent->renameFile(file_index, new_name);
path_items << index.data().toString(); // Force recheck
QModelIndex parent = PropListModel->parent(index); if (force_recheck) m_torrent->forceRecheck();
while (parent.isValid()) { // Rename if torrent files model too
path_items.prepend(parent.data().toString()); if (new_name_last.endsWith(".!qB"))
parent = PropListModel->parent(parent); new_name_last.chop(4);
} PropListModel->setData(index, new_name_last);
const QString old_path = path_items.join("/"); }
path_items.removeLast(); else {
path_items << new_name_last; // Folder renaming
QString new_path = path_items.join("/"); QStringList path_items;
if (Utils::Fs::sameFileNames(old_path, new_path)) { path_items << index.data().toString();
qDebug("Name did not change"); QModelIndex parent = PropListModel->parent(index);
while (parent.isValid()) {
path_items.prepend(parent.data().toString());
parent = PropListModel->parent(parent);
}
const QString old_path = path_items.join("/");
path_items.removeLast();
path_items << new_name_last;
QString new_path = path_items.join("/");
if (Utils::Fs::sameFileNames(old_path, new_path)) {
qDebug("Name did not change");
return;
}
if (!new_path.endsWith("/")) new_path += "/";
// Check for overwriting
for (int i = 0; i < m_torrent->filesCount(); ++i) {
const QString &current_name = m_torrent->filePath(i);
#if defined(Q_OS_UNIX) || defined(Q_WS_QWS)
if (current_name.startsWith(new_path, Qt::CaseSensitive)) {
#else
if (current_name.startsWith(new_path, Qt::CaseInsensitive)) {
#endif
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."),
QMessageBox::Ok);
return; return;
} }
if (!new_path.endsWith("/")) new_path += "/"; }
// Check for overwriting bool force_recheck = false;
for (int i = 0; i < m_torrent->filesCount(); ++i) { // Replace path in all files
const QString &current_name = m_torrent->filePath(i); for (int i = 0; i < m_torrent->filesCount(); ++i) {
#if defined(Q_OS_UNIX) || defined(Q_WS_QWS) const QString current_name = m_torrent->filePath(i);
if (current_name.startsWith(new_path, Qt::CaseSensitive)) { if (current_name.startsWith(old_path)) {
#else QString new_name = current_name;
if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { new_name.replace(0, old_path.length(), new_path);
#endif if (!force_recheck && QDir(m_torrent->savePath(true)).exists(new_name))
QMessageBox::warning(this, tr("The folder could not be renamed"), force_recheck = true;
tr("This name is already in use in this folder. Please use a different name."), new_name = Utils::Fs::expandPath(new_name);
QMessageBox::Ok); qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name));
return; m_torrent->renameFile(i, new_name);
}
}
bool force_recheck = false;
// Replace path in all files
for (int i = 0; i < m_torrent->filesCount(); ++i) {
const QString current_name = m_torrent->filePath(i);
if (current_name.startsWith(old_path)) {
QString new_name = current_name;
new_name.replace(0, old_path.length(), new_path);
if (!force_recheck && QDir(m_torrent->savePath(true)).exists(new_name))
force_recheck = true;
new_name = Utils::Fs::expandPath(new_name);
qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name));
m_torrent->renameFile(i, new_name);
}
}
// Force recheck
if (force_recheck) m_torrent->forceRecheck();
// Rename folder in torrent files model too
PropListModel->setData(index, new_name_last);
// Remove old folder
const QDir old_folder(m_torrent->savePath(true) + "/" + old_path);
int timeout = 10;
while (!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) {
// FIXME: We should not sleep here (freezes the UI for 1 second)
QThread::msleep(100);
--timeout;
} }
} }
// Force recheck
if (force_recheck) m_torrent->forceRecheck();
// Rename folder in torrent files model too
PropListModel->setData(index, new_name_last);
// Remove old folder
const QDir old_folder(m_torrent->savePath(true) + "/" + old_path);
int timeout = 10;
while (!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) {
// FIXME: We should not sleep here (freezes the UI for 1 second)
QThread::msleep(100);
--timeout;
}
} }
} }