Skip to content

Commit

Permalink
Merge pull request qbittorrent#7596 from evsh/path-history-items
Browse files Browse the repository at this point in the history
Add option to tune download history list length. Closes qbittorrent#4043.
  • Loading branch information
zeule authored Nov 10, 2017
2 parents 04cec39 + 1fed324 commit 08755a2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
56 changes: 46 additions & 10 deletions src/gui/addnewtorrentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include "base/preferences.h"
#include "base/settingsstorage.h"
#include "base/settingvalue.h"
#include "base/net/downloadmanager.h"
#include "base/net/downloadhandler.h"
#include "base/bittorrent/session.h"
Expand All @@ -58,24 +59,28 @@
#include "ui_addnewtorrentdialog.h"
#include "addnewtorrentdialog.h"

#define SETTINGS_KEY(name) "AddNewTorrentDialog/" name
const QString KEY_ENABLED = SETTINGS_KEY("Enabled");
const QString KEY_DEFAULTCATEGORY = SETTINGS_KEY("DefaultCategory");
const QString KEY_TREEHEADERSTATE = SETTINGS_KEY("TreeHeaderState");
const QString KEY_WIDTH = SETTINGS_KEY("Width");
const QString KEY_EXPANDED = SETTINGS_KEY("Expanded");
const QString KEY_TOPLEVEL = SETTINGS_KEY("TopLevel");
const QString KEY_SAVEPATHHISTORY = SETTINGS_KEY("SavePathHistory");

namespace
{
#define SETTINGS_KEY(name) "AddNewTorrentDialog/" name
const QString KEY_ENABLED = SETTINGS_KEY("Enabled");
const QString KEY_DEFAULTCATEGORY = SETTINGS_KEY("DefaultCategory");
const QString KEY_TREEHEADERSTATE = SETTINGS_KEY("TreeHeaderState");
const QString KEY_WIDTH = SETTINGS_KEY("Width");
const QString KEY_EXPANDED = SETTINGS_KEY("Expanded");
const QString KEY_TOPLEVEL = SETTINGS_KEY("TopLevel");
const QString KEY_SAVEPATHHISTORY = SETTINGS_KEY("SavePathHistory");
const char KEY_SAVEPATHHISTORYLENGTH[] = SETTINGS_KEY("SavePathHistoryLength");

// just a shortcut
inline SettingsStorage *settings()
{
return SettingsStorage::instance();
}
}

constexpr int AddNewTorrentDialog::minPathHistoryLength;
constexpr int AddNewTorrentDialog::maxPathHistoryLength;

AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
: QDialog(parent)
, ui(new Ui::AddNewTorrentDialog)
Expand All @@ -93,6 +98,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP

ui->savePath->setMode(FileSystemPathEdit::Mode::DirectorySave);
ui->savePath->setDialogCaption(tr("Choose save path"));
ui->savePath->setMaxVisibleItems(20);

auto session = BitTorrent::Session::instance();

Expand Down Expand Up @@ -175,6 +181,34 @@ void AddNewTorrentDialog::setTopLevel(bool value)
SettingsStorage::instance()->storeValue(KEY_TOPLEVEL, value);
}

int AddNewTorrentDialog::savePathHistoryLength()
{
return savePathHistoryLengthSetting();
}

void AddNewTorrentDialog::setSavePathHistoryLength(int value)
{
Q_ASSERT(value >= minPathHistoryLength);
Q_ASSERT(value <= maxPathHistoryLength);
const int oldValue = savePathHistoryLength();
if (oldValue != value) {
savePathHistoryLengthSetting() = value;
settings()->storeValue(KEY_SAVEPATHHISTORY,
QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, value)));
}
}

CachedSettingValue<int> &AddNewTorrentDialog::savePathHistoryLengthSetting()
{
const int defaultHistoryLength = 8;
static CachedSettingValue<int> setting(KEY_SAVEPATHHISTORYLENGTH, defaultHistoryLength,
[](int v)
{
return std::max(minPathHistoryLength, std::min(maxPathHistoryLength, v));
});
return setting;
}

void AddNewTorrentDialog::loadState()
{
m_headerState = settings()->loadValue(KEY_TREEHEADERSTATE).toByteArray();
Expand Down Expand Up @@ -362,14 +396,16 @@ void AddNewTorrentDialog::saveSavePathHistory() const
QDir selectedSavePath(ui->savePath->selectedPath());
// Get current history
QStringList history = settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList();
if (history.size() > savePathHistoryLength())
history = history.mid(0, savePathHistoryLength());
QList<QDir> historyDirs;
foreach (const QString dir, history)
historyDirs << QDir(dir);
if (!historyDirs.contains(selectedSavePath)) {
// Add save path to history
history.push_front(selectedSavePath.absolutePath());
// Limit list size
if (history.size() > 8)
if (history.size() > savePathHistoryLength())
history.pop_back();
// Save history
settings()->storeValue(KEY_SAVEPATHHISTORY, history);
Expand Down
7 changes: 7 additions & 0 deletions src/gui/addnewtorrentdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ namespace Ui
class TorrentContentFilterModel;
class TorrentFileGuard;
class PropListDelegate;
template <typename T> class CachedSettingValue;

class AddNewTorrentDialog: public QDialog
{
Q_OBJECT

public:
static constexpr int minPathHistoryLength = 0;
static constexpr int maxPathHistoryLength = 99;

~AddNewTorrentDialog();

static bool isEnabled();
static void setEnabled(bool value);
static bool isTopLevel();
static void setTopLevel(bool value);
static int savePathHistoryLength();
static void setSavePathHistoryLength(int value);

static void show(QString source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent);
static void show(QString source, QWidget *parent);
Expand Down Expand Up @@ -99,6 +105,7 @@ private slots:
void setupTreeview();
void setCommentText(const QString &str) const;
void setSavePath(const QString &newPath);
static CachedSettingValue<int> &savePathHistoryLengthSetting();

void showEvent(QShowEvent *event) override;

Expand Down
8 changes: 7 additions & 1 deletion src/gui/advancedsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "base/bittorrent/session.h"
#include "base/preferences.h"
#include "base/unicodestrings.h"
#include "gui/addnewtorrentdialog.h"
#include "gui/mainwindow.h"

enum AdvSettingsCols
Expand Down Expand Up @@ -69,6 +70,7 @@ enum AdvSettingsRows
TORRENT_ADDED_NOTIFICATIONS,
CONFIRM_REMOVE_ALL_TAGS,
DOWNLOAD_TRACKER_FAVICON,
SAVE_PATH_HISTORY_LENGTH,
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
USE_ICON_THEME,
#endif
Expand Down Expand Up @@ -197,6 +199,7 @@ void AdvancedSettings::saveAdvancedSettings()
mainWindow->setTorrentAddedNotificationsEnabled(cb_torrent_added_notifications.isChecked());
// Misc GUI properties
mainWindow->setDownloadTrackerFavicon(cb_tracker_favicon.isChecked());
AddNewTorrentDialog::setSavePathHistoryLength(spinSavePathHistoryLength.value());

// Tracker
session->setTrackerEnabled(cb_tracker_status.isChecked());
Expand Down Expand Up @@ -420,7 +423,10 @@ void AdvancedSettings::loadAdvancedSettings()
// Download tracker's favicon
cb_tracker_favicon.setChecked(mainWindow->isDownloadTrackerFavicon());
addRow(DOWNLOAD_TRACKER_FAVICON, tr("Download tracker's favicon"), &cb_tracker_favicon);

// Save path history length
spinSavePathHistoryLength.setRange(AddNewTorrentDialog::minPathHistoryLength, AddNewTorrentDialog::maxPathHistoryLength);
spinSavePathHistoryLength.setValue(AddNewTorrentDialog::savePathHistoryLength());
addRow(SAVE_PATH_HISTORY_LENGTH, tr("Save path history length"), &spinSavePathHistoryLength);
// Tracker State
cb_tracker_status.setChecked(session->isTrackerEnabled());
addRow(TRACKER_STATUS, tr("Enable embedded tracker"), &cb_tracker_status);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/advancedsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private slots:

QLabel labelQbtLink, labelLibtorrentLink;
QSpinBox spin_cache, spin_save_resume_data_interval, outgoing_ports_min, outgoing_ports_max, spin_list_refresh, spin_maxhalfopen, spin_tracker_port, spin_cache_ttl,
spinSendBufferWatermark, spinSendBufferLowWatermark, spinSendBufferWatermarkFactor;
spinSendBufferWatermark, spinSendBufferLowWatermark, spinSendBufferWatermarkFactor, spinSavePathHistoryLength;
QCheckBox cb_os_cache, cb_recheck_completed, cb_resolve_countries, cb_resolve_hosts, cb_super_seeding,
cb_program_notifications, cb_torrent_added_notifications, cb_tracker_favicon, cb_tracker_status,
cb_confirm_torrent_recheck, cb_confirm_remove_all_tags, cb_listen_ipv6, cb_announce_all_trackers, cb_announce_all_tiers,
Expand Down
10 changes: 10 additions & 0 deletions src/gui/fspathedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,16 @@ void FileSystemPathComboEdit::setCurrentIndex(int index)
editWidget<WidgetType>()->setCurrentIndex(index);
}

int FileSystemPathComboEdit::maxVisibleItems() const
{
return editWidget<WidgetType>()->maxVisibleItems();
}

void FileSystemPathComboEdit::setMaxVisibleItems(int maxItems)
{
editWidget<WidgetType>()->setMaxVisibleItems(maxItems);
}

QString FileSystemPathComboEdit::editWidgetText() const
{
return editWidget<WidgetType>()->currentText();
Expand Down
3 changes: 3 additions & 0 deletions src/gui/fspathedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class FileSystemPathComboEdit: public FileSystemPathEdit
int currentIndex() const;
void setCurrentIndex(int index);

int maxVisibleItems() const;
void setMaxVisibleItems(int maxItems);

private:
QString editWidgetText() const override;
void setEditWidgetText(const QString &text) override;
Expand Down

0 comments on commit 08755a2

Please sign in to comment.