Skip to content

Commit

Permalink
Merge pull request #1164 from vktr/feature/pql-label-support
Browse files Browse the repository at this point in the history
Support label filtering in PQL
  • Loading branch information
vktr committed Apr 30, 2021
2 parents 13e6a7e + 77e8a0e commit 3a355ed
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/picotorrent/bittorrent/addparams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace pt::BitTorrent
struct AddParams
{
int labelId;
std::string labelName;
};
}
7 changes: 5 additions & 2 deletions src/picotorrent/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void Session::OnAlert()
TorrentHandle* handle = new TorrentHandle(this, ata->handle);

AddParams* add = ata->params.userdata.get<AddParams>();
if (add && add->labelId > 0) { handle->SetLabelMuted(add->labelId); }
if (add && add->labelId > 0) { handle->SetLabel(add->labelId, add->labelName, true); }

m_torrents.insert({ ata->handle.info_hashes(), handle });

Expand Down Expand Up @@ -906,9 +906,10 @@ void Session::LoadIPFilter(std::string const& path)

void Session::LoadTorrents()
{
auto stmt = m_db->CreateStatement("SELECT t.info_hash, tmu.magnet_uri, trd.resume_data, tmu.save_path, IFNULL(t.label_id, -1) FROM torrent t\n"
auto stmt = m_db->CreateStatement("SELECT t.info_hash, tmu.magnet_uri, trd.resume_data, tmu.save_path, IFNULL(t.label_id, -1), lbl.name AS label_name FROM torrent t\n"
"LEFT JOIN torrent_magnet_uri tmu ON t.info_hash = tmu.info_hash\n"
"LEFT JOIN torrent_resume_data trd ON t.info_hash = trd.info_hash\n"
"LEFT JOIN label lbl ON t.label_id = t.label_id\n"
"ORDER BY t.queue_position ASC");

while (stmt->Read())
Expand All @@ -917,6 +918,7 @@ void Session::LoadTorrents()
std::string magnet_uri = stmt->GetString(1);
std::string save_path = stmt->GetString(3);
int label_id = stmt->GetInt(4);
std::string label_name = stmt->GetString(5);

std::vector<char> resume_data;
stmt->GetBlob(2, resume_data);
Expand Down Expand Up @@ -955,6 +957,7 @@ void Session::LoadTorrents()
if (label_id > 0)
{
params.userdata.get<AddParams>()->labelId = label_id;
params.userdata.get<AddParams>()->labelName = label_name;
}

m_session->async_add_torrent(params);
Expand Down
11 changes: 5 additions & 6 deletions src/picotorrent/bittorrent/torrenthandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,15 @@ void TorrentHandle::BuildStatus(libtorrent::torrent_status const& ts)
m_status = Update(ts);
}

void TorrentHandle::SetLabel(int id, std::string const& name)
void TorrentHandle::SetLabel(int id, std::string const& name, bool muted)
{
m_labelId = id;
m_labelName = name;
m_session->UpdateTorrentLabel(this);
}

void TorrentHandle::SetLabelMuted(int id)
{
m_labelId = id;
if (!muted)
{
m_session->UpdateTorrentLabel(this);
}
}

std::unique_ptr<TorrentStatus> TorrentHandle::Update(lt::torrent_status const& ts)
Expand Down
3 changes: 1 addition & 2 deletions src/picotorrent/bittorrent/torrenthandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ namespace BitTorrent
// Labels
int Label();
void ClearLabel();
void SetLabel(int id, std::string const& name);
void SetLabel(int id, std::string const& name, bool muted = false);

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

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

Expand Down
17 changes: 17 additions & 0 deletions src/picotorrent/ui/filters/pqltorrentfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static std::map<std::string, std::function<bool(Value const&)>> FieldValidators
{ "size", [](Value const& v) { return v.value_int.has_value() || v.value_float.has_value(); } },
{ "name", [](Value const& v) { return v.value_string.has_value(); } },
{ "status", [](Value const& v) { return v.value_string.has_value(); } },
{ "label", [](Value const& v) { return v.value_string.has_value(); } },
};

typedef std::function<bool(TorrentStatus const&)> FilterFunc;
Expand Down Expand Up @@ -292,6 +293,22 @@ class FilterVisitor : public pt::PQL::QueryBaseVisitor
});
}

if (ref == "label")
{
std::string term = value.value_string.value();

if (oper == Operator::CONTAINS)
{
return FilterFunc(
[term](TorrentStatus const& ts)
{
return ts.labelName.find(term) != std::string::npos;
});
}

return FilterFunc([oper, term](TorrentStatus const& ts) { return Compare(ts.labelName, term, oper); });
}

throw QueryException(
"Unknown field: '" + ref + "'",
ctx->getStart()->getLine(),
Expand Down
6 changes: 5 additions & 1 deletion src/picotorrent/ui/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ using pt::UI::MainFrame;

const char* WindowTitle = "PicoTorrent";

#define LABEL_ICON_SIZE 16

MainFrame::MainFrame(std::shared_ptr<Core::Environment> env, std::shared_ptr<Core::Database> db, std::shared_ptr<Core::Configuration> cfg, pt::CommandLineOptions const& options)
: wxFrame(nullptr, wxID_ANY, WindowTitle, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, "MainFrame"),
m_env(env),
Expand Down Expand Up @@ -862,9 +864,11 @@ void MainFrame::ShowTorrentContextMenu(wxCommandEvent&)
void MainFrame::UpdateLabels()
{
std::map<int, std::tuple<std::string, std::string>> labels;

for (auto const& label : m_cfg->GetLabels())
{
labels.insert({ label.id, { label.name, label.color } });
}
m_torrentListModel->UpdateLabels(labels);

m_torrentListModel->UpdateLabels(labels, FromDIP(LABEL_ICON_SIZE));
}
4 changes: 2 additions & 2 deletions src/picotorrent/ui/models/torrentlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ void TorrentListModel::GetValueByRow(wxVariant& variant, uint32_t row, uint32_t
}
}

void TorrentListModel::UpdateLabels(std::map<int, std::tuple<std::string, std::string>> const& labels)
void TorrentListModel::UpdateLabels(std::map<int, std::tuple<std::string, std::string>> const& labels, int size)
{
std::vector<BitTorrent::TorrentHandle*> torrents;

Expand Down Expand Up @@ -600,7 +600,7 @@ void TorrentListModel::UpdateLabels(std::map<int, std::tuple<std::string, std::s

m_labelsColors.insert({ id, wxColor(color) });

wxBitmap bmp(24, 24);
wxBitmap bmp(size, size);

{
wxMemoryDC dc;
Expand Down
2 changes: 1 addition & 1 deletion src/picotorrent/ui/models/torrentlistmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace pt::UI::Models

bool SetValueByRow(const wxVariant&, uint32_t, uint32_t) wxOVERRIDE { return false; }

void UpdateLabels(std::map<int, std::tuple<std::string, std::string>> const& labels);
void UpdateLabels(std::map<int, std::tuple<std::string, std::string>> const& labels, int size);

private:
void ApplyFilter();
Expand Down

0 comments on commit 3a355ed

Please sign in to comment.