2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-11-27 03:29:51 +00:00
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "StatsDialog.h"
|
|
|
|
|
2022-12-29 02:42:20 +00:00
|
|
|
#include "GtkCompat.h"
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "PrefsDialog.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm/i18n.h>
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <glibmm/main.h>
|
|
|
|
#include <glibmm/ustring.h>
|
|
|
|
#include <gtkmm/label.h>
|
|
|
|
#include <gtkmm/messagedialog.h>
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include <memory>
|
2007-11-27 03:29:51 +00:00
|
|
|
|
2024-02-17 19:31:49 +00:00
|
|
|
static auto constexpr TR_RESPONSE_RESET = 1;
|
2008-04-22 14:07:42 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
class StatsDialog::Impl
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
2022-09-07 22:25:04 +00:00
|
|
|
Impl(StatsDialog& dialog, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core);
|
2021-10-18 20:22:31 +00:00
|
|
|
~Impl();
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(Impl)
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
|
|
|
bool updateStats();
|
|
|
|
void dialogResponse(int response);
|
|
|
|
|
|
|
|
private:
|
|
|
|
StatsDialog& dialog_;
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
Gtk::Label* one_up_lb_;
|
|
|
|
Gtk::Label* one_down_lb_;
|
|
|
|
Gtk::Label* one_ratio_lb_;
|
|
|
|
Gtk::Label* one_time_lb_;
|
2012-12-13 03:00:57 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::Label* all_up_lb_;
|
|
|
|
Gtk::Label* all_down_lb_;
|
|
|
|
Gtk::Label* all_ratio_lb_;
|
|
|
|
Gtk::Label* all_time_lb_;
|
2012-12-13 03:00:57 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::Label* all_sessions_lb_;
|
2012-12-13 03:00:57 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
sigc::connection update_stats_tag_;
|
2007-11-27 03:29:51 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void setLabel(Gtk::Label* l, Glib::ustring const& str)
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
gtr_label_set_text(*l, str);
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void setLabelFromRatio(Gtk::Label* l, double d)
|
|
|
|
{
|
|
|
|
setLabel(l, tr_strlratio(d));
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
auto startedTimesText(uint64_t n)
|
|
|
|
{
|
|
|
|
return fmt::format(ngettext("Started {count:L} time", "Started {count:L} times", n), fmt::arg("count", n));
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool StatsDialog::Impl::updateStats()
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto stats = tr_sessionGetStats(core_->get_session());
|
|
|
|
setLabel(one_up_lb_, tr_strlsize(stats.uploadedBytes));
|
|
|
|
setLabel(one_down_lb_, tr_strlsize(stats.downloadedBytes));
|
|
|
|
setLabel(one_time_lb_, tr_format_time(stats.secondsActive));
|
|
|
|
setLabelFromRatio(one_ratio_lb_, stats.ratio);
|
|
|
|
|
|
|
|
stats = tr_sessionGetCumulativeStats(core_->get_session());
|
|
|
|
setLabel(all_sessions_lb_, startedTimesText(stats.sessionCount));
|
|
|
|
setLabel(all_up_lb_, tr_strlsize(stats.uploadedBytes));
|
|
|
|
setLabel(all_down_lb_, tr_strlsize(stats.downloadedBytes));
|
|
|
|
setLabel(all_time_lb_, tr_format_time(stats.secondsActive));
|
|
|
|
setLabelFromRatio(all_ratio_lb_, stats.ratio);
|
2020-08-18 10:36:10 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
return true;
|
2008-06-05 20:41:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
StatsDialog::Impl::~Impl()
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
update_stats_tag_.disconnect();
|
|
|
|
}
|
2008-04-22 14:07:42 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void StatsDialog::Impl::dialogResponse(int response)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (response == TR_RESPONSE_RESET)
|
2008-04-22 14:07:42 +00:00
|
|
|
{
|
2022-09-10 18:15:01 +00:00
|
|
|
auto w = std::make_shared<Gtk::MessageDialog>(
|
|
|
|
dialog_,
|
|
|
|
_("Reset your statistics?"),
|
|
|
|
false,
|
|
|
|
TR_GTK_MESSAGE_TYPE(QUESTION),
|
|
|
|
TR_GTK_BUTTONS_TYPE(NONE),
|
|
|
|
true);
|
|
|
|
w->add_button(_("_Cancel"), TR_GTK_RESPONSE_TYPE(CANCEL));
|
|
|
|
w->add_button(_("_Reset"), TR_RESPONSE_RESET);
|
|
|
|
w->set_secondary_text(
|
2021-10-18 20:22:31 +00:00
|
|
|
_("These statistics are for your information only. "
|
|
|
|
"Resetting them doesn't affect the statistics logged by your BitTorrent trackers."));
|
2022-09-10 18:15:01 +00:00
|
|
|
w->signal_response().connect(
|
|
|
|
[this, w](int inner_response) mutable
|
|
|
|
{
|
|
|
|
if (inner_response == TR_RESPONSE_RESET)
|
|
|
|
{
|
|
|
|
tr_sessionClearStats(core_->get_session());
|
|
|
|
updateStats();
|
|
|
|
}
|
|
|
|
w.reset();
|
|
|
|
});
|
|
|
|
w->show();
|
2008-04-22 14:07:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 13:19:54 +00:00
|
|
|
if (response == TR_GTK_RESPONSE_TYPE(CLOSE))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
dialog_.close();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
StatsDialog::StatsDialog(
|
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Gtk::Window& parent,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
|
|
|
: Gtk::Dialog(cast_item)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, builder, core))
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
set_transient_for(parent);
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
StatsDialog::~StatsDialog() = default;
|
|
|
|
|
|
|
|
std::unique_ptr<StatsDialog> StatsDialog::create(Gtk::Window& parent, Glib::RefPtr<Session> const& core)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const builder = Gtk::Builder::create_from_resource(gtr_get_full_resource_path("StatsDialog.ui"));
|
|
|
|
return std::unique_ptr<StatsDialog>(gtr_get_widget_derived<StatsDialog>(builder, "StatsDialog", parent, core));
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
StatsDialog::Impl::Impl(StatsDialog& dialog, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core)
|
2021-10-18 20:22:31 +00:00
|
|
|
: dialog_(dialog)
|
|
|
|
, core_(core)
|
2022-09-07 22:25:04 +00:00
|
|
|
, one_up_lb_(gtr_get_widget<Gtk::Label>(builder, "current_uploaded_value_label"))
|
|
|
|
, one_down_lb_(gtr_get_widget<Gtk::Label>(builder, "current_downloaded_value_label"))
|
|
|
|
, one_ratio_lb_(gtr_get_widget<Gtk::Label>(builder, "current_ratio_value_label"))
|
|
|
|
, one_time_lb_(gtr_get_widget<Gtk::Label>(builder, "current_duration_value_label"))
|
|
|
|
, all_up_lb_(gtr_get_widget<Gtk::Label>(builder, "total_uploaded_value_label"))
|
|
|
|
, all_down_lb_(gtr_get_widget<Gtk::Label>(builder, "total_downloaded_value_label"))
|
|
|
|
, all_ratio_lb_(gtr_get_widget<Gtk::Label>(builder, "total_ratio_value_label"))
|
|
|
|
, all_time_lb_(gtr_get_widget<Gtk::Label>(builder, "total_duration_value_label"))
|
|
|
|
, all_sessions_lb_(gtr_get_widget<Gtk::Label>(builder, "start_count_label"))
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2022-09-10 13:19:54 +00:00
|
|
|
dialog_.set_default_response(TR_GTK_RESPONSE_TYPE(CLOSE));
|
2022-09-07 22:25:04 +00:00
|
|
|
dialog_.signal_response().connect(sigc::mem_fun(*this, &Impl::dialogResponse));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
updateStats();
|
|
|
|
update_stats_tag_ = Glib::signal_timeout().connect_seconds(
|
2021-11-12 09:12:50 +00:00
|
|
|
sigc::mem_fun(*this, &Impl::updateStats),
|
2021-10-18 20:22:31 +00:00
|
|
|
SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS);
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|