Skip to content

Commit

Permalink
Merge pull request picotorrent#1180 from vktr/feature/export-torrents
Browse files Browse the repository at this point in the history
Add context menu actions to export torrent
  • Loading branch information
vktr authored Jun 1, 2021
2 parents fc8ed9e + 18b0e67 commit fb0aaad
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ add_executable(
src/picotorrent/ui/dialogs/preferencesgeneralpage
src/picotorrent/ui/dialogs/preferenceslabelspage
src/picotorrent/ui/dialogs/preferencesproxypage
src/picotorrent/ui/dialogs/textoutputdialog

# Filters
src/picotorrent/ui/filters/pqltorrentfilter
Expand Down
6 changes: 5 additions & 1 deletion lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,9 @@
"file_filter_all_files": "All files (*.*)|*.*",
"ip_filter_enabled": "IP filter: ✔️",
"ip_filter_disabled": "IP filter: ❌",
"copyright_text": "© 2015-2021"
"copyright_text": "© 2015-2021",
"export": "Export",
"magnet_link_s": "Magnet link(s)",
"torrent_file_s": "Torrent file(s)",
"exported_magnet_link_s": "Exported magnet link(s)"
}
3 changes: 2 additions & 1 deletion src/picotorrent/bittorrent/torrenthandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ namespace BitTorrent
void ClearLabel();
void SetLabel(int id, std::string const& name, bool muted = false);

libtorrent::torrent_handle& WrappedHandle();

private:
TorrentHandle(Session* session, libtorrent::torrent_handle const& th);

void BuildStatus(libtorrent::torrent_status const& ts);
std::unique_ptr<TorrentStatus> Update(libtorrent::torrent_status const& ts);
libtorrent::torrent_handle& WrappedHandle();

Session* m_session;
std::unique_ptr<libtorrent::torrent_handle> m_th;
Expand Down
41 changes: 41 additions & 0 deletions src/picotorrent/ui/dialogs/textoutputdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "textoutputdialog.hpp"

#include "../translator.hpp"

using pt::UI::Dialogs::TextOutputDialog;

TextOutputDialog::TextOutputDialog(wxWindow* parent, wxWindowID id, std::wstring const& title, std::wstring const& desc)
: wxDialog(parent, id, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
m_outputText(new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxHSCROLL | wxTE_MULTILINE))
{
auto buttonsSizer = new wxBoxSizer(wxHORIZONTAL);
wxButton* ok = new wxButton(this, wxID_OK);
ok->SetDefault();

buttonsSizer->Add(ok);

auto mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->AddSpacer(FromDIP(11));
mainSizer->Add(new wxStaticText(this, wxID_ANY, desc), 0, wxLEFT | wxRIGHT, FromDIP(11));
mainSizer->AddSpacer(FromDIP(5));
mainSizer->Add(m_outputText, 1, wxLEFT | wxRIGHT | wxEXPAND, FromDIP(11));
mainSizer->AddSpacer(FromDIP(7));
mainSizer->Add(buttonsSizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_RIGHT, FromDIP(11));

this->SetSizerAndFit(mainSizer);
this->SetSize(FromDIP(wxSize(400, 250)));

m_outputText->SetFocus();
m_outputText->SetFont(
wxFont(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, wxT("Consolas")));
}

TextOutputDialog::~TextOutputDialog()
{
}

void TextOutputDialog::SetOutputText(std::string const& text)
{
m_outputText->SetValue(text);
m_outputText->SetInsertionPointEnd();
}
29 changes: 29 additions & 0 deletions src/picotorrent/ui/dialogs/textoutputdialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <string>

namespace pt
{
namespace UI
{
namespace Dialogs
{
class TextOutputDialog : public wxDialog
{
public:
TextOutputDialog(wxWindow* parent, wxWindowID id, std::wstring const& title, std::wstring const& desc);
virtual ~TextOutputDialog();

void SetOutputText(std::string const& text);

private:
wxTextCtrl* m_outputText;
};
}
}
}
59 changes: 59 additions & 0 deletions src/picotorrent/ui/torrentcontextmenu.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#include "torrentcontextmenu.hpp"

#include <filesystem>
#include <fstream>

#include <libtorrent/create_torrent.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <wx/clipbrd.h>

#include "../bittorrent/torrenthandle.hpp"
#include "../bittorrent/torrentstatus.hpp"
#include "../core/configuration.hpp"
#include "../core/utils.hpp"
#include "dialogs/textoutputdialog.hpp"
#include "translator.hpp"

namespace fs = std::filesystem;
using pt::UI::Dialogs::TextOutputDialog;
using pt::UI::TorrentContextMenu;

TorrentContextMenu::TorrentContextMenu(wxWindow* parent, std::shared_ptr<pt::Core::Configuration> cfg, std::vector<pt::BitTorrent::TorrentHandle*> const& selectedTorrents)
Expand All @@ -28,6 +33,10 @@ TorrentContextMenu::TorrentContextMenu(wxWindow* parent, std::shared_ptr<pt::Cor
removeMenu->Append(ptID_REMOVE, i18n("remove_torrent"));
removeMenu->Append(ptID_REMOVE_FILES, i18n("remove_torrent_and_files"));

wxMenu* exportMenu = new wxMenu();
exportMenu->Append(ptID_EXPORT_MAGNET_LINK, i18n("magnet_link_s"));
exportMenu->Append(ptID_EXPORT_TORRENT_FILE, i18n("torrent_file_s"));

bool allPaused = std::all_of(
selectedTorrents.begin(),
selectedTorrents.end(),
Expand Down Expand Up @@ -115,6 +124,7 @@ TorrentContextMenu::TorrentContextMenu(wxWindow* parent, std::shared_ptr<pt::Cor
}

AppendSeparator();
AppendSubMenu(exportMenu, i18n("export"));
Append(ptID_MOVE, i18n("move"));
AppendSubMenu(removeMenu, i18n("remove"));
AppendSeparator();
Expand All @@ -139,6 +149,55 @@ TorrentContextMenu::TorrentContextMenu(wxWindow* parent, std::shared_ptr<pt::Cor
[&](wxCommandEvent&) { for (auto t : selectedTorrents) { t->Pause(); } },
TorrentContextMenu::ptID_PAUSE);

Bind(
wxEVT_MENU,
[&](wxCommandEvent&)
{
std::stringstream ss;

for (auto torrent : selectedTorrents)
{
ss << lt::make_magnet_uri(torrent->WrappedHandle()) << "\n";
}

TextOutputDialog dlg(m_parent, wxID_ANY, i18n("magnet_link_s"), i18n("exported_magnet_link_s"));
dlg.SetOutputText(ss.str());
dlg.ShowModal();
},
TorrentContextMenu::ptID_EXPORT_MAGNET_LINK);

Bind(
wxEVT_MENU,
[&](wxCommandEvent&)
{
wxDirDialog dlg(
m_parent,
i18n("select_destination"),
wxEmptyString,
wxDD_DIR_MUST_EXIST);

if (dlg.ShowModal() != wxID_OK)
{
return;
}

fs::path outputDir = Utils::toStdString(dlg.GetPath().ToStdWstring());

for (auto torrent : selectedTorrents)
{
if (auto tf = torrent->WrappedHandle().torrent_file_with_hashes())
{
lt::create_torrent ct(*tf.get());
lt::entry e = ct.generate();

std::string fileName = tf->name() + ".torrent";
std::ofstream out(outputDir / fileName, std::ios::binary);
lt::bencode(std::ostreambuf_iterator<char>(out), e);
}
}
},
TorrentContextMenu::ptID_EXPORT_TORRENT_FILE);

Bind(
wxEVT_MENU,
[&](wxCommandEvent&)
Expand Down
2 changes: 2 additions & 0 deletions src/picotorrent/ui/torrentcontextmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace UI
ptID_FORCE_RECHECK,
ptID_FORCE_REANNOUNCE,
ptID_SEQUENTIAL_DOWNLOAD,
ptID_EXPORT_MAGNET_LINK,
ptID_EXPORT_TORRENT_FILE,
ptID_LABELS_NONE,
ptID_LABELS_USER
};
Expand Down

0 comments on commit fb0aaad

Please sign in to comment.