Skip to content

Commit

Permalink
Merge pull request #999 from vktr/feature/better-add-torrent-dialog
Browse files Browse the repository at this point in the history
Better 'Add torrent' dialog
  • Loading branch information
vktr committed Oct 27, 2020
2 parents b2ce6c1 + d8805b4 commit 5ab11f5
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 160 deletions.
4 changes: 3 additions & 1 deletion lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,7 @@
"eta_hms_format": "{0}h {1}m {2}s",
"eta_ms_format": "{0}m {1}s",
"eta_s_format": "{0}s",
"per_second_format": "{0}/s"
"per_second_format": "{0}/s",
"contents": "Contents",
"tier": "Tier"
}
402 changes: 272 additions & 130 deletions src/picotorrent/ui/dialogs/addtorrentdialog.cpp

Large diffs are not rendered by default.

36 changes: 31 additions & 5 deletions src/picotorrent/ui/dialogs/addtorrentdialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <wx/wx.h>
#endif

#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/download_priority.hpp>
#include <libtorrent/fwd.hpp>
#include <libtorrent/info_hash.hpp>
Expand All @@ -13,11 +14,19 @@
#include <vector>

class wxBitmapComboBox;
class wxButton;
class wxCheckBox;
class wxChoice;
class wxDataViewCtrl;
class wxDataViewEvent;
class wxDataViewItemArray;
class wxListView;
class wxSplitterWindow;

namespace pt::BitTorrent
{
class Session;
}

namespace pt
{
Expand All @@ -37,11 +46,10 @@ namespace Dialogs
class AddTorrentDialog : public wxDialog
{
public:
AddTorrentDialog(wxWindow* parent, wxWindowID id, std::vector<libtorrent::add_torrent_params>& params, std::shared_ptr<Core::Database> db, std::shared_ptr<Core::Configuration> cfg);
AddTorrentDialog(wxWindow* parent, wxWindowID id, libtorrent::add_torrent_params& params, std::shared_ptr<Core::Database> db, std::shared_ptr<Core::Configuration> cfg, std::shared_ptr<BitTorrent::Session> session);
virtual ~AddTorrentDialog();

std::vector<libtorrent::add_torrent_params> GetTorrentParams() { return m_params; };
void MetadataFound(std::shared_ptr<libtorrent::torrent_info> const& ti);
libtorrent::add_torrent_params GetTorrentParams() { return m_params; };

private:
enum
Expand All @@ -53,22 +61,34 @@ namespace Dialogs
ptID_FILE_LIST,
ptID_SEQUENTIAL_DOWNLOAD,
ptID_START_TORRENT,
ptID_TRACKERS_ADD,
ptID_TRACKERS_REMOVE,
ptID_OK,
ptID_CANCEL,

ptID_CONTEXT_MENU_DO_NOT_DOWNLOAD,
ptID_CONTEXT_MENU_LOW,
ptID_CONTEXT_MENU_NORMAL,
ptID_CONTEXT_MENU_MAXIMUM,
};

void MetadataFound(std::shared_ptr<libtorrent::torrent_info> const& ti);

wxString GetTorrentDisplayName(libtorrent::add_torrent_params const& params);
wxString GetTorrentDisplaySize(libtorrent::add_torrent_params const& params);
wxString GetTorrentDisplayInfoHash(libtorrent::add_torrent_params const& params);
wxString GetTorrentDisplayComment(libtorrent::add_torrent_params const& params);

void Load(size_t index);
void Load();
void OnAddTracker(wxCommandEvent&);
void OnCancel(wxCommandEvent&);
void OnOk(wxCommandEvent&);
void OnRemoveTracker(wxCommandEvent&);
void ReloadTrackers();
void SetFilePriorities(wxDataViewItemArray& items, libtorrent::download_priority_t prio);
void ShowFileContextMenu(wxDataViewEvent&);

wxSplitterWindow* m_splitter;
wxChoice* m_torrents;
wxStaticText* m_torrentName;
wxStaticText* m_torrentSize;
Expand All @@ -80,12 +100,18 @@ namespace Dialogs
wxDataViewCtrl* m_filesView;
wxCheckBox* m_sequentialDownload;
wxCheckBox* m_startTorrent;
wxListView* m_peers;
wxListView* m_trackers;
wxButton* m_addTracker;
wxButton* m_removeTracker;

Models::FileStorageModel* m_filesModel;

libtorrent::add_torrent_params m_params;

std::shared_ptr<Core::Configuration> m_cfg;
std::shared_ptr<Core::Database> m_db;
std::vector<libtorrent::add_torrent_params> m_params;
std::shared_ptr<BitTorrent::Session> m_session;
std::set<libtorrent::info_hash_t> m_manualSavePath;
};
}
Expand Down
12 changes: 10 additions & 2 deletions src/picotorrent/ui/dialogs/addtrackerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ using pt::UI::Dialogs::AddTrackerDialog;

AddTrackerDialog::AddTrackerDialog(wxWindow* parent, wxWindowID id)
: wxDialog(parent, id, i18n("add_tracker"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
m_url(new wxTextCtrl(this, wxID_ANY))
m_url(new wxTextCtrl(this, wxID_ANY)),
m_tier(new wxTextCtrl(this, wxID_ANY, "1", wxDefaultPosition, wxSize(parent->FromDIP(30), -1), wxTE_RIGHT))
{
auto buttonsSizer = new wxBoxSizer(wxHORIZONTAL);
wxButton* ok = new wxButton(this, wxID_OK, i18n("cancel"));
wxButton* ok = new wxButton(this, wxID_OK, i18n("ok"));
ok->SetDefault();

buttonsSizer->Add(ok);
Expand All @@ -21,6 +22,8 @@ AddTrackerDialog::AddTrackerDialog(wxWindow* parent, wxWindowID id)
formSizer->AddGrowableCol(1, 1);
formSizer->Add(new wxStaticText(this, wxID_ANY, i18n("url")), 0);
formSizer->Add(m_url, 1, wxEXPAND);
formSizer->Add(new wxStaticText(this, wxID_ANY, i18n("tier")), 0);
formSizer->Add(m_tier, 0);

auto sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(formSizer, 1, wxEXPAND | wxLEFT | wxTOP | wxRIGHT, FromDIP(11));
Expand Down Expand Up @@ -67,6 +70,11 @@ AddTrackerDialog::~AddTrackerDialog()
{
}

int AddTrackerDialog::GetTier()
{
return std::stoi(m_tier->GetValue().ToStdString());
}

std::string AddTrackerDialog::GetUrl()
{
return m_url->GetValue().ToStdString();
Expand Down
2 changes: 2 additions & 0 deletions src/picotorrent/ui/dialogs/addtrackerdialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ namespace Dialogs
AddTrackerDialog(wxWindow* parent, wxWindowID id);
virtual ~AddTrackerDialog();

int GetTier();
std::string GetUrl();

private:
wxTextCtrl* m_url;
wxTextCtrl* m_tier;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/picotorrent/ui/dialogs/createtorrentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct StopPayload
std::string bp;
};

CreateTorrentDialog::CreateTorrentDialog(wxWindow* parent, wxWindowID id, Session* session)
CreateTorrentDialog::CreateTorrentDialog(wxWindow* parent, wxWindowID id, std::shared_ptr<Session> session)
: wxDialog(parent, id, i18n("create_torrent"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
m_session(session)
{
Expand Down
4 changes: 2 additions & 2 deletions src/picotorrent/ui/dialogs/createtorrentdialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Dialogs
class CreateTorrentDialog : public wxDialog
{
public:
explicit CreateTorrentDialog(wxWindow* parent, wxWindowID id, BitTorrent::Session* session);
explicit CreateTorrentDialog(wxWindow* parent, wxWindowID id, std::shared_ptr<BitTorrent::Session> session);
virtual ~CreateTorrentDialog();

private:
Expand Down Expand Up @@ -59,7 +59,7 @@ namespace Dialogs
wxGauge* m_progress;
wxButton* m_create;

BitTorrent::Session* m_session;
std::shared_ptr<BitTorrent::Session> m_session;
std::thread m_worker;
};
}
Expand Down
41 changes: 23 additions & 18 deletions src/picotorrent/ui/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ MainFrame::MainFrame(std::shared_ptr<Core::Environment> env, std::shared_ptr<Cor
this->SendSizeEvent();
}, ptID_EVT_SHOW_STATUS_BAR);

this->Bind(
ptEVT_TORRENT_METADATA_FOUND,
[this](pt::BitTorrent::MetadataFoundEvent& evt)
{
for (auto dlg : m_addDialogs)
{
wxPostEvent(dlg, evt);
}
});

// Update status bar
m_statusBar->UpdateDhtNodesCount(m_cfg->Get<bool>("libtorrent.enable_dht").value() ? 0 : -1);
m_statusBar->UpdateTorrentCount(m_torrentsCount);
Expand All @@ -346,9 +356,7 @@ MainFrame::MainFrame(std::shared_ptr<Core::Environment> env, std::shared_ptr<Cor
MainFrame::~MainFrame()
{
m_taskBarIcon->Hide();

delete m_taskBarIcon;
delete m_session;
}

void MainFrame::AddFilter(wxString const& name, std::function<bool(BitTorrent::TorrentHandle*)> const& filter)
Expand Down Expand Up @@ -462,25 +470,22 @@ void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)
return;
}

Dialogs::AddTorrentDialog dlg(this, wxID_ANY, params, m_db, m_cfg);
for (auto& param : params)
{
auto dlg = new Dialogs::AddTorrentDialog(this, wxID_ANY, param, m_db, m_cfg, m_session);
dlg->Bind(
wxEVT_CLOSE_WINDOW,
[this, dlg](wxCloseEvent& evt)
{
evt.Skip();
m_addDialogs.erase(dlg);
});
dlg->Show();

this->Bind(
ptEVT_TORRENT_METADATA_FOUND,
[&dlg](pt::BitTorrent::MetadataFoundEvent& evt)
{
dlg.MetadataFound(evt.GetData());
});
m_addDialogs.insert(dlg);
}

// search for metadata
m_session->AddMetadataSearch(hashes);

if (dlg.ShowModal() == wxID_OK)
{
for (lt::add_torrent_params const& p : dlg.GetTorrentParams())
{
m_session->AddTorrent(p);
}
}
}

void MainFrame::HandleParams(std::vector<std::string> const& files, std::vector<std::string> const& magnets)
Expand Down
6 changes: 5 additions & 1 deletion src/picotorrent/ui/mainframe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#include <libtorrent/info_hash.hpp>

#include <map>
#include <unordered_set>
#include <vector>

class wxSplitterWindow;
class wxTaskBarIconEvent;

namespace pt::UI::Dialogs { class AddTorrentDialog; }

namespace pt
{
class UpdateChecker;
Expand Down Expand Up @@ -82,6 +85,7 @@ namespace Models
Models::TorrentListModel* m_torrentListModel;
TorrentListView* m_torrentList;

std::shared_ptr<BitTorrent::Session> m_session;
std::shared_ptr<Core::Environment> m_env;
std::shared_ptr<Core::Database> m_db;
std::shared_ptr<Core::Configuration> m_cfg;
Expand All @@ -97,7 +101,7 @@ namespace Models

std::map<size_t, std::function<bool(BitTorrent::TorrentHandle*)>> m_filters;

BitTorrent::Session* m_session;
std::unordered_set<Dialogs::AddTorrentDialog*> m_addDialogs;
std::map<libtorrent::info_hash_t, BitTorrent::TorrentHandle*> m_selection;
int64_t m_torrentsCount;
};
Expand Down

0 comments on commit 5ab11f5

Please sign in to comment.