transmission/gtk/StatsDialog.cc

169 lines
5.2 KiB
C++
Raw Normal View History

// This file Copyright © 2007-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// 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
#include <memory>
#include <glibmm.h>
#include <glibmm/i18n.h>
#include <fmt/core.h>
#include "PrefsDialog.h"
#include "Session.h"
#include "StatsDialog.h"
#include "Utils.h"
2007-11-27 03:29:51 +00:00
static auto constexpr TR_RESPONSE_RESET = int{ 1 };
2008-04-22 14:07:42 +00:00
class StatsDialog::Impl
2007-11-27 03:29:51 +00:00
{
public:
Impl(StatsDialog& dialog, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core);
~Impl();
TR_DISABLE_COPY_MOVE(Impl)
private:
bool updateStats();
void dialogResponse(int response);
private:
StatsDialog& dialog_;
Glib::RefPtr<Session> const core_;
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
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
Gtk::Label* all_sessions_lb_;
2012-12-13 03:00:57 +00:00
sigc::connection update_stats_tag_;
2007-11-27 03:29:51 +00:00
};
namespace
2007-11-27 03:29:51 +00:00
{
void setLabel(Gtk::Label* l, Glib::ustring const& str)
2007-11-27 03:29:51 +00:00
{
gtr_label_set_text(*l, str);
}
void setLabelFromRatio(Gtk::Label* l, double d)
{
setLabel(l, tr_strlratio(d));
2007-11-27 03:29:51 +00:00
}
auto startedTimesText(uint64_t n)
{
return fmt::format(ngettext("Started {count:L} time", "Started {count:L} times", n), fmt::arg("count", n));
}
} // 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);
return true;
}
StatsDialog::Impl::~Impl()
2007-11-27 03:29:51 +00:00
{
update_stats_tag_.disconnect();
}
2008-04-22 14:07:42 +00:00
void StatsDialog::Impl::dialogResponse(int response)
{
if (response == TR_RESPONSE_RESET)
2008-04-22 14:07:42 +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(
_("These statistics are for your information only. "
"Resetting them doesn't affect the statistics logged by your BitTorrent trackers."));
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
}
if (response == TR_GTK_RESPONSE_TYPE(CLOSE))
{
Add support for GTK 4 (#3916) * Make compact mode switch work for both GTK 3 and 4 * Implement GTK 4-specific view gesture handling * Fix torrents view context menu on GTK 4 * Explicitly show/hide menubar on startup/teardown * Switch from `Gtk::Pixbuf` to `Gio::Icon` for views * Support GTK 4 exceptions based on `std::exception` * Fix options menu setup with GTK 4 * Use `delete-event` (GTK 3) and `close-request` (GTK 4) signals to handle window clousure * Add custom file chooser button implementation GTK 4 drops FileChooserButton widget and suggests implementing it using Button. * Add helpers to set X11 hints with GTK 4 * Remove `HigWorkarea` class that's no longer used * Make main menu shortcuts work with GTK 4 * Make drops work in main window and make dialog with GTK 4 * Remove unused `gtr_action_get_widget()` helper * Fix text direction mark setup with GTK 4 (due to switch to enum class) * Fix file tree font size calculation with GTK 4 * Fix crash during shutdown with GTK 4 * Switch from `RadioButton` to `CheckButton` for compatibility with GTK 4 * Fix opening files with GTK 4 * Rework torrent cell renderer to support both GTK 3 and 4 * Disable system tray icon support with GTK 4 * Fix windows positioning with GTK 4 * Fix focus event handling with GTK 4 * Adapt to tree model row/iterator changes in GTK 4 * Adapt to toplevel/root window changes in GTK 4 * Adapt to clipboard changes in GTK 4 * Adapt to icon/theme changes in GTK 4 * Adapt to file/path changes in GTK 4 * Random leftover fixes for GTK 4 compatibility * Clean up unused code * Move GTK 3 *.ui files into a subdirectory * Add GTK 4 *.ui files * Search for both GTK 3 and 4 during configuration
2022-10-08 22:50:03 +00:00
dialog_.close();
}
2007-11-27 03:29:51 +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))
{
set_transient_for(parent);
}
StatsDialog::~StatsDialog() = default;
std::unique_ptr<StatsDialog> StatsDialog::create(Gtk::Window& parent, Glib::RefPtr<Session> const& core)
{
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));
}
StatsDialog::Impl::Impl(StatsDialog& dialog, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core)
: dialog_(dialog)
, core_(core)
, 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
{
dialog_.set_default_response(TR_GTK_RESPONSE_TYPE(CLOSE));
dialog_.signal_response().connect(sigc::mem_fun(*this, &Impl::dialogResponse));
updateStats();
update_stats_tag_ = Glib::signal_timeout().connect_seconds(
sigc::mem_fun(*this, &Impl::updateStats),
SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS);
2007-11-27 03:29:51 +00:00
}