Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option of automatically picking up URLs from the clipboard on window focus #1633

Merged
merged 8 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion libtransmission/quark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace std::literals;
namespace
{

auto constexpr my_static = std::array<std::string_view, 389>{ ""sv,
auto constexpr my_static = std::array<std::string_view, 390>{ ""sv,
"activeTorrentCount"sv,
"activity-date"sv,
"activityDate"sv,
Expand Down Expand Up @@ -274,6 +274,7 @@ auto constexpr my_static = std::array<std::string_view, 389>{ ""sv,
"ratio-limit"sv,
"ratio-limit-enabled"sv,
"ratio-mode"sv,
"read-clipboard"sv,
"recent-download-dir-1"sv,
"recent-download-dir-2"sv,
"recent-download-dir-3"sv,
Expand Down
1 change: 1 addition & 0 deletions libtransmission/quark.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ enum
TR_KEY_ratio_limit,
TR_KEY_ratio_limit_enabled,
TR_KEY_ratio_mode,
TR_KEY_read_clipboard,
TR_KEY_recent_download_dir_1,
TR_KEY_recent_download_dir_2,
TR_KEY_recent_download_dir_3,
Expand Down
53 changes: 46 additions & 7 deletions qt/MainWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ MainWindow::MainWindow(Session& session, Prefs& prefs, TorrentModel& model, bool
connect(&filter_model_, &TorrentFilter::rowsRemoved, this, refresh_header_soon);
connect(ui_.listView, &TorrentView::headerDoubleClicked, filter_bar, &FilterBar::clear);

static std::array<int, 16> constexpr InitKeys = {
static std::array<int, 17> constexpr InitKeys = {
Prefs::ALT_SPEED_LIMIT_ENABLED, //
Prefs::COMPACT_VIEW, //
Prefs::DSPEED, //
Expand All @@ -326,6 +326,7 @@ MainWindow::MainWindow(Session& session, Prefs& prefs, TorrentModel& model, bool
Prefs::MAIN_WINDOW_X, //
Prefs::RATIO, //
Prefs::RATIO_ENABLED, //
Prefs::READ_CLIPBOARD, //
Prefs::SHOW_TRAY_ICON, //
Prefs::SORT_MODE, //
Prefs::SORT_REVERSED, //
Expand Down Expand Up @@ -1211,12 +1212,10 @@ void MainWindow::refreshPref(int key)
break;

case Prefs::COMPACT_VIEW:
{
b = prefs_.getBool(key);
ui_.action_CompactView->setChecked(b);
ui_.listView->setItemDelegate(b ? torrent_delegate_min_ : torrent_delegate_);
break;
}
b = prefs_.getBool(key);
ui_.action_CompactView->setChecked(b);
ui_.listView->setItemDelegate(b ? torrent_delegate_min_ : torrent_delegate_);
break;

case Prefs::MAIN_WINDOW_X:
case Prefs::MAIN_WINDOW_Y:
Expand Down Expand Up @@ -1244,6 +1243,10 @@ void MainWindow::refreshPref(int key)
break;
}

case Prefs::READ_CLIPBOARD:
auto_add_clipboard_links = prefs_.getBool(Prefs::READ_CLIPBOARD);
break;

default:
break;
}
Expand Down Expand Up @@ -1598,6 +1601,42 @@ void MainWindow::dropEvent(QDropEvent* event)
}
}

bool MainWindow::event(QEvent* e)
{
if (e->type() != QEvent::WindowActivate || !auto_add_clipboard_links)
{
return QMainWindow::event(e);
}

QString const text = QGuiApplication::clipboard()->text().trimmed();
if (text.endsWith(QStringLiteral(".torrent"), Qt::CaseInsensitive) ||
text.startsWith(QStringLiteral("magnet:"), Qt::CaseInsensitive))
{
for (QString const& entry : text.split(QLatin1Char('\n')))
{
QString key = entry.trimmed();

if (key.isEmpty())
{
continue;
}

if (QUrl const url(key); url.isLocalFile())
{
key = url.toLocalFile();
}

if (!clipboard_processed_keys_.contains(key))
{
clipboard_processed_keys_.append(key);
trApp->addTorrent(AddData(key));
}
}
}

return QMainWindow::event(e);
}

/***
****
***/
Expand Down
3 changes: 3 additions & 0 deletions qt/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public slots:
void contextMenuEvent(QContextMenuEvent*) override;
void dragEnterEvent(QDragEnterEvent*) override;
void dropEvent(QDropEvent*) override;
bool event(QEvent*) override;

private slots:
void addTorrents(QStringList const& filenames);
Expand Down Expand Up @@ -178,6 +179,8 @@ private slots:
QWidget* filter_bar_ = {};
QAction* alt_speed_action_ = {};
QString error_message_;
bool auto_add_clipboard_links = {};
QStringList clipboard_processed_keys_ = {};

QString const total_ratio_stats_mode_name_ = QStringLiteral("total-ratio");
QString const total_transfer_stats_mode_name_ = QStringLiteral("total-transfer");
Expand Down
2 changes: 2 additions & 0 deletions qt/Prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ std::array<Prefs::PrefItem, Prefs::PREFS_COUNT> const Prefs::Items{
{ COMPLETE_SOUND_COMMAND, TR_KEY_torrent_complete_sound_command, QVariant::StringList },
{ COMPLETE_SOUND_ENABLED, TR_KEY_torrent_complete_sound_enabled, QVariant::Bool },
{ USER_HAS_GIVEN_INFORMED_CONSENT, TR_KEY_user_has_given_informed_consent, QVariant::Bool },
{ READ_CLIPBOARD, TR_KEY_read_clipboard, QVariant::Bool },

/* libtransmission settings */
{ ALT_SPEED_LIMIT_UP, TR_KEY_alt_speed_up, QVariant::Int },
Expand Down Expand Up @@ -493,6 +494,7 @@ void Prefs::initDefaults(tr_variant* d) const
dictAdd(d, TR_KEY_sort_mode, SortMode);
dictAdd(d, TR_KEY_statusbar_stats, StatsMode);
dictAdd(d, TR_KEY_watch_dir, download_dir);
dictAdd(d, TR_KEY_read_clipboard, false);
}

/***
Expand Down
1 change: 1 addition & 0 deletions qt/Prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Prefs : public QObject
COMPLETE_SOUND_COMMAND,
COMPLETE_SOUND_ENABLED,
USER_HAS_GIVEN_INFORMED_CONSENT,
READ_CLIPBOARD,
/* core prefs */
FIRST_CORE_PREF,
ALT_SPEED_LIMIT_UP = FIRST_CORE_PREF,
Expand Down
1 change: 1 addition & 0 deletions qt/PrefsDialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ void PrefsDialog::initDownloadingTab()
linkWidgetToPref(ui_.watchDirEdit, Prefs::DIR_WATCH);
linkWidgetToPref(ui_.showTorrentOptionsDialogCheck, Prefs::OPTIONS_PROMPT);
linkWidgetToPref(ui_.startAddedTorrentsCheck, Prefs::START);
linkWidgetToPref(ui_.detectTorrentFromClipboard, Prefs::READ_CLIPBOARD);
linkWidgetToPref(ui_.trashTorrentFileCheck, Prefs::TRASH_ORIGINAL);
linkWidgetToPref(ui_.downloadDirButton, Prefs::DOWNLOAD_DIR);
linkWidgetToPref(ui_.downloadDirEdit, Prefs::DOWNLOAD_DIR);
Expand Down
68 changes: 39 additions & 29 deletions qt/PrefsDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>545</width>
<width>601</width>
<height>547</height>
</rect>
</property>
Expand All @@ -17,7 +17,7 @@
<item>
<widget class="QTabWidget" name="tabs">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<property name="elideMode">
<enum>Qt::ElideNone</enum>
Expand Down Expand Up @@ -273,6 +273,13 @@
<property name="leftMargin">
<number>18</number>
</property>
<item row="5" column="0">
<widget class="QLabel" name="downloadDirLabel">
<property name="text">
<string>Save to &amp;Location:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="watchDirCheck">
<property name="text">
Expand All @@ -283,47 +290,50 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QStackedWidget" name="watchDirStack">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="startAddedTorrentsCheck">
<property name="text">
<string>&amp;Start added torrents</string>
</property>
<widget class="PathButton" name="watchDirButton"/>
<widget class="QLineEdit" name="watchDirEdit"/>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="showTorrentOptionsDialogCheck">
<item row="6" column="1">
<widget class="FreeSpaceLabel" name="downloadDirFreeSpaceLabel">
<property name="text">
<string>Show the Torrent Options &amp;dialog</string>
<string notr="true">...</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="startAddedTorrentsCheck">
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="showTorrentOptionsDialogCheck">
<property name="text">
<string>&amp;Start added torrents</string>
<string>Show the Torrent Options &amp;dialog</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="trashTorrentFileCheck">
<property name="text">
<string>Mo&amp;ve the .torrent file to the trash</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="downloadDirLabel">
<property name="text">
<string>Save to &amp;Location:</string>
<item row="0" column="1">
<widget class="QStackedWidget" name="watchDirStack">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<widget class="PathButton" name="watchDirButton"/>
<widget class="QLineEdit" name="watchDirEdit"/>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QStackedWidget" name="downloadDirStack">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand All @@ -335,13 +345,13 @@
<widget class="QLineEdit" name="downloadDirEdit"/>
</widget>
</item>
<item row="5" column="1">
<widget class="FreeSpaceLabel" name="downloadDirFreeSpaceLabel">
<property name="text">
<string notr="true">...</string>
<item row="3" column="0">
<widget class="QCheckBox" name="detectTorrentFromClipboard">
<property name="toolTip">
<string>Reads user clipboard content for torrents</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="text">
<string>Detect new torrents from clipboard</string>
</property>
</widget>
</item>
Expand Down