Skip to content

Commit

Permalink
refactor: add tr_torrentTrackers() (#2282)
Browse files Browse the repository at this point in the history
* refactor: add tr_torrentTrackers()
  • Loading branch information
ckerr committed Dec 8, 2021
1 parent 0a85c3a commit ab0c498
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 428 deletions.
167 changes: 72 additions & 95 deletions gtk/DetailsDialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1847,24 +1847,24 @@ Glib::ustring tr_strltime_rounded(time_t t)
return tr_strltime(t);
}

void appendAnnounceInfo(tr_tracker_stat const* const st, time_t const now, Gtk::TextDirection direction, std::ostream& gstr)
void appendAnnounceInfo(tr_tracker_view const& tracker, time_t const now, Gtk::TextDirection direction, std::ostream& gstr)
{
if (st->hasAnnounced && st->announceState != TR_TRACKER_INACTIVE)
if (tracker.hasAnnounced && tracker.announceState != TR_TRACKER_INACTIVE)
{
gstr << '\n';
gstr << text_dir_mark[direction];
auto const timebuf = tr_strltime_rounded(now - st->lastAnnounceTime);
auto const timebuf = tr_strltime_rounded(now - tracker.lastAnnounceTime);

if (st->lastAnnounceSucceeded)
if (tracker.lastAnnounceSucceeded)
{
gstr << gtr_sprintf(
_("Got a list of %1$s%2$'d peers%3$s %4$s ago"),
success_markup_begin,
st->lastAnnouncePeerCount,
tracker.lastAnnouncePeerCount,
success_markup_end,
timebuf);
}
else if (st->lastAnnounceTimedOut)
else if (tracker.lastAnnounceTimedOut)
{
gstr << gtr_sprintf(
_("Peer list request %1$stimed out%2$s %3$s ago; will retry"),
Expand All @@ -1877,13 +1877,13 @@ void appendAnnounceInfo(tr_tracker_stat const* const st, time_t const now, Gtk::
gstr << gtr_sprintf(
_("Got an error %1$s\"%2$s\"%3$s %4$s ago"),
err_markup_begin,
st->lastAnnounceResult,
tracker.lastAnnounceResult,
err_markup_end,
timebuf);
}
}

switch (st->announceState)
switch (tracker.announceState)
{
case TR_TRACKER_INACTIVE:
gstr << '\n';
Expand All @@ -1894,7 +1894,7 @@ void appendAnnounceInfo(tr_tracker_stat const* const st, time_t const now, Gtk::
case TR_TRACKER_WAITING:
gstr << '\n';
gstr << text_dir_mark[direction];
gstr << gtr_sprintf(_("Asking for more peers in %s"), tr_strltime_rounded(st->nextAnnounceTime - now));
gstr << gtr_sprintf(_("Asking for more peers in %s"), tr_strltime_rounded(tracker.nextAnnounceTime - now));
break;

case TR_TRACKER_QUEUED:
Expand All @@ -1908,26 +1908,26 @@ void appendAnnounceInfo(tr_tracker_stat const* const st, time_t const now, Gtk::
gstr << text_dir_mark[direction];
gstr << gtr_sprintf(
_("Asking for more peers now… <small>%s</small>"),
tr_strltime_rounded(now - st->lastAnnounceStartTime));
tr_strltime_rounded(now - tracker.lastAnnounceStartTime));
break;
}
}

void appendScrapeInfo(tr_tracker_stat const* const st, time_t const now, Gtk::TextDirection direction, std::ostream& gstr)
void appendScrapeInfo(tr_tracker_view const& tracker, time_t const now, Gtk::TextDirection direction, std::ostream& gstr)
{
if (st->hasScraped)
if (tracker.hasScraped)
{
gstr << '\n';
gstr << text_dir_mark[direction];
auto const timebuf = tr_strltime_rounded(now - st->lastScrapeTime);
auto const timebuf = tr_strltime_rounded(now - tracker.lastScrapeTime);

if (st->lastScrapeSucceeded)
if (tracker.lastScrapeSucceeded)
{
gstr << gtr_sprintf(
_("Tracker had %s%'d seeders and %'d leechers%s %s ago"),
success_markup_begin,
st->seederCount,
st->leecherCount,
tracker.seederCount,
tracker.leecherCount,
success_markup_end,
timebuf);
}
Expand All @@ -1936,21 +1936,21 @@ void appendScrapeInfo(tr_tracker_stat const* const st, time_t const now, Gtk::Te
gstr << gtr_sprintf(
_("Got a scrape error \"%s%s%s\" %s ago"),
err_markup_begin,
st->lastScrapeResult,
tracker.lastScrapeResult,
err_markup_end,
timebuf);
}
}

switch (st->scrapeState)
switch (tracker.scrapeState)
{
case TR_TRACKER_INACTIVE:
break;

case TR_TRACKER_WAITING:
gstr << '\n';
gstr << text_dir_mark[direction];
gstr << gtr_sprintf(_("Asking for peer counts in %s"), tr_strltime_rounded(st->nextScrapeTime - now));
gstr << gtr_sprintf(_("Asking for peer counts in %s"), tr_strltime_rounded(tracker.nextScrapeTime - now));
break;

case TR_TRACKER_QUEUED:
Expand All @@ -1964,33 +1964,33 @@ void appendScrapeInfo(tr_tracker_stat const* const st, time_t const now, Gtk::Te
gstr << text_dir_mark[direction];
gstr << gtr_sprintf(
_("Asking for peer counts now… <small>%s</small>"),
tr_strltime_rounded(now - st->lastScrapeStartTime));
tr_strltime_rounded(now - tracker.lastScrapeStartTime));
break;
}
}

void buildTrackerSummary(
std::ostream& gstr,
std::string const& key,
tr_tracker_stat const* st,
tr_tracker_view const& tracker,
bool showScrape,
Gtk::TextDirection direction)
{
// hostname
gstr << text_dir_mark[direction];
gstr << (st->isBackup ? "<i>" : "<b>");
gstr << Glib::Markup::escape_text(!key.empty() ? gtr_sprintf("%s - %s", st->host, key) : st->host);
gstr << (st->isBackup ? "</i>" : "</b>");
gstr << (tracker.isBackup ? "<i>" : "<b>");
gstr << Glib::Markup::escape_text(!key.empty() ? gtr_sprintf("%s - %s", tracker.host, key) : tracker.host);
gstr << (tracker.isBackup ? "</i>" : "</b>");

if (!st->isBackup)
if (!tracker.isBackup)
{
time_t const now = time(nullptr);

appendAnnounceInfo(st, now, direction, gstr);
appendAnnounceInfo(tracker, now, direction, gstr);

if (showScrape)
{
appendScrapeInfo(st, now, direction, gstr);
appendScrapeInfo(tracker, now, direction, gstr);
}
}
}
Expand Down Expand Up @@ -2092,16 +2092,13 @@ void DetailsDialog::Impl::refreshTracker(std::vector<tr_torrent*> const& torrent
bool const showScrape = scrape_check_->get_active();

/* step 1: get all the trackers */
std::vector<int> statCount;
std::vector<tr_tracker_stat*> stats;

statCount.reserve(torrents.size());
stats.reserve(torrents.size());
for (auto const* torrent : torrents)
auto trackers = std::multimap<tr_torrent const*, tr_tracker_view>{};
for (auto const* tor : torrents)
{
int count = 0;
stats.push_back(tr_torrentTrackers(torrent, &count));
statCount.push_back(count);
for (size_t i = 0, n = tr_torrentTrackerCount(tor); i < n; ++i)
{
trackers.emplace(tor, tr_torrentTracker(tor, i));
}
}

/* step 2: mark all the trackers in the list as not-updated */
Expand All @@ -2110,61 +2107,49 @@ void DetailsDialog::Impl::refreshTracker(std::vector<tr_torrent*> const& torrent
row[tracker_cols.was_updated] = false;
}

/* step 3: add any new trackers */
for (size_t i = 0; i < statCount.size(); ++i)
/* step 3: add / update trackers */
for (auto const& [tor, tracker] : trackers)
{
int const jn = statCount.at(i);
auto const torrent_id = tr_torrentId(tor);

for (int j = 0; j < jn; ++j)
// build the key to find the row
gstr.str({});
gstr << torrent_id << '\t' << tracker.tier << '\t' << tracker.announce;
if (hash.find(gstr.str()) == hash.end())
{
tr_torrent const* tor = torrents.at(i);
tr_tracker_stat const* st = &stats.at(i)[j];
int const torrent_id = tr_torrentId(tor);
// if we didn't have that row, add it
auto const iter = store->append();
(*iter)[tracker_cols.torrent_id] = torrent_id;
(*iter)[tracker_cols.tracker_id] = tracker.id;
(*iter)[tracker_cols.key] = gstr.str();

/* build the key to find the row */
gstr.str({});
gstr << torrent_id << '\t' << st->tier << '\t' << st->announce;

if (hash.find(gstr.str()) == hash.end())
{
auto const iter = store->append();
(*iter)[tracker_cols.torrent_id] = torrent_id;
(*iter)[tracker_cols.tracker_id] = st->id;
(*iter)[tracker_cols.key] = gstr.str();

auto const p = store->get_path(iter);
hash.emplace(gstr.str(), Gtk::TreeRowReference(store, p));
gtr_get_favicon_from_url(
session,
st->announce,
[ref = Gtk::TreeRowReference(store, p)](auto const& pixbuf) mutable { favicon_ready_cb(pixbuf, ref); });
}
auto const p = store->get_path(iter);
hash.emplace(gstr.str(), Gtk::TreeRowReference(store, p));
gtr_get_favicon_from_url(
session,
tracker.announce,
[ref = Gtk::TreeRowReference(store, p)](auto const& pixbuf) mutable { favicon_ready_cb(pixbuf, ref); });
}
}

/* step 4: update the peers */
for (size_t i = 0; i < torrents.size(); ++i)
/* step 4: update the rows */
auto const summary_name = std::string(std::size(torrents) == 1 ? tr_torrentName(torrents.front()) : "");
for (auto const& [tor, tracker] : trackers)
{
tr_torrent const* tor = torrents.at(i);
auto const summary_name = std::string(torrents.size() > 1 ? tr_torrentName(tor) : "");

for (int j = 0; j < statCount.at(i); ++j)
{
tr_tracker_stat const* st = &stats.at(i)[j];
auto const torrent_id = tr_torrentId(tor);

/* build the key to find the row */
gstr.str({});
gstr << tr_torrentId(tor) << '\t' << st->tier << '\t' << st->announce;
auto const iter = store->get_iter(hash.at(gstr.str()).get_path());
// build the key to find the row
gstr.str({});
gstr << torrent_id << '\t' << tracker.tier << '\t' << tracker.announce;
auto const iter = store->get_iter(hash.at(gstr.str()).get_path());

/* update the row */
gstr.str({});
buildTrackerSummary(gstr, summary_name, st, showScrape, dialog_.get_direction());
(*iter)[tracker_cols.text] = gstr.str();
(*iter)[tracker_cols.is_backup] = st->isBackup;
(*iter)[tracker_cols.tracker_id] = st->id;
(*iter)[tracker_cols.was_updated] = true;
}
// update the row
gstr.str({});
buildTrackerSummary(gstr, summary_name, tracker, showScrape, dialog_.get_direction());
(*iter)[tracker_cols.text] = gstr.str();
(*iter)[tracker_cols.is_backup] = tracker.isBackup;
(*iter)[tracker_cols.tracker_id] = tracker.id;
(*iter)[tracker_cols.was_updated] = true;
}

/* step 5: remove trackers that have disappeared */
Expand All @@ -2186,12 +2171,6 @@ void DetailsDialog::Impl::refreshTracker(std::vector<tr_torrent*> const& torrent
}

edit_trackers_button_->set_sensitive(tracker_list_get_current_torrent_id() >= 0);

/* cleanup */
for (size_t i = 0; i < stats.size(); ++i)
{
tr_torrentTrackersFree(stats[i], statCount[i]);
}
}

void DetailsDialog::Impl::onScrapeToggled()
Expand Down Expand Up @@ -2274,19 +2253,18 @@ std::string get_editable_tracker_list(tr_torrent const* tor)
{
std::ostringstream gstr;
int tier = 0;
tr_info const* inf = tr_torrentInfo(tor);

for (unsigned int i = 0; i < inf->trackerCount; ++i)
for (size_t i = 0, n = tr_torrentTrackerCount(tor); i < n; ++i)
{
tr_tracker_info const* t = &inf->trackers[i];
auto const tracker = tr_torrentTracker(tor, i);

if (tier != t->tier)
if (tier != tracker.tier)
{
tier = t->tier;
tier = tracker.tier;
gstr << '\n';
}

gstr << t->announce << '\n';
gstr << tracker.announce << '\n';
}

auto str = gstr.str();
Expand Down Expand Up @@ -2649,8 +2627,7 @@ void DetailsDialog::Impl::set_torrents(std::vector<int> const& ids)
{
int const id = ids.front();
auto const* tor = core_->find_torrent(id);
auto const* inf = tr_torrentInfo(tor);
title = gtr_sprintf(_("%s Properties"), inf->name);
title = gtr_sprintf(_("%s Properties"), tr_torrentName(tor));

file_list_->set_torrent(id);
file_list_->show();
Expand Down
22 changes: 10 additions & 12 deletions gtk/FilterBar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,12 @@ bool tracker_filter_model_update(Glib::RefPtr<Gtk::TreeStore> const& tracker_mod
for (auto const& row : tmodel->children())
{
auto const* tor = static_cast<tr_torrent const*>(row.get_value(torrent_cols.torrent));
auto const* const inf = tr_torrentInfo(tor);

std::set<std::string const*> keys;

for (unsigned int i = 0; i < inf->trackerCount; ++i)
for (size_t i = 0, n = tr_torrentTrackerCount(tor); i < n; ++i)
{
auto const* const key = &*strings.insert(gtr_get_host_from_url(inf->trackers[i].announce)).first;
auto const* const key = &*strings.insert(gtr_get_host_from_url(tr_torrentTracker(tor, i).announce)).first;

if (auto const count = hosts_hash.find(key); count == hosts_hash.end())
{
Expand Down Expand Up @@ -384,21 +383,20 @@ namespace

bool test_tracker(tr_torrent const* tor, int active_tracker_type, Glib::ustring const& host)
{
bool matches = true;

if (active_tracker_type == TRACKER_FILTER_TYPE_HOST)
if (active_tracker_type != TRACKER_FILTER_TYPE_HOST)
{
auto const* const inf = tr_torrentInfo(tor);

matches = false;
return true;
}

for (unsigned int i = 0; !matches && i < inf->trackerCount; ++i)
for (size_t i = 0, n = tr_torrentTrackerCount(tor); i < n; ++i)
{
if (gtr_get_host_from_url(tr_torrentTracker(tor, i).announce) == host)
{
matches = gtr_get_host_from_url(inf->trackers[i].announce) == host;
return true;
}
}

return matches;
return false;
}

/***
Expand Down
Loading

0 comments on commit ab0c498

Please sign in to comment.