2007-11-27 03:29:51 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2007-11-27 03:29:51 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-09-23 19:11:04 +00:00
|
|
|
*
|
2007-11-27 03:29:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "hig.h"
|
|
|
|
#include "stats.h"
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-core.h"
|
2010-02-01 04:54:10 +00:00
|
|
|
#include "tr-prefs.h"
|
2011-03-03 01:59:25 +00:00
|
|
|
#include "util.h"
|
2007-11-27 03:29:51 +00:00
|
|
|
|
2008-04-22 14:07:42 +00:00
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_RESPONSE_RESET = 1
|
2008-04-22 14:07:42 +00:00
|
|
|
};
|
|
|
|
|
2007-11-27 03:29:51 +00:00
|
|
|
struct stat_ui
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
GtkLabel* one_up_lb;
|
|
|
|
GtkLabel* one_down_lb;
|
|
|
|
GtkLabel* one_ratio_lb;
|
|
|
|
GtkLabel* one_time_lb;
|
2012-12-13 03:00:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
GtkLabel* all_up_lb;
|
|
|
|
GtkLabel* all_down_lb;
|
|
|
|
GtkLabel* all_ratio_lb;
|
|
|
|
GtkLabel* all_time_lb;
|
2012-12-13 03:00:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
GtkLabel* all_sessions_lb;
|
2012-12-13 03:00:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TrCore* core;
|
2007-11-27 03:29:51 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void setLabel(GtkLabel* l, char const* str)
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
gtr_label_set_text(l, str);
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void setLabelFromRatio(GtkLabel* l, double d)
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[128];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlratio(buf, d, sizeof(buf));
|
|
|
|
setLabel(l, buf);
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static gboolean updateStats(gpointer gdata)
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[128];
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_session_stats one;
|
|
|
|
tr_session_stats all;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const buflen = sizeof(buf);
|
2021-09-25 10:47:09 +00:00
|
|
|
auto* ui = static_cast<stat_ui*>(gdata);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_sessionGetStats(gtr_core_session(ui->core), &one);
|
|
|
|
tr_sessionGetCumulativeStats(gtr_core_session(ui->core), &all);
|
|
|
|
|
|
|
|
setLabel(ui->one_up_lb, tr_strlsize(buf, one.uploadedBytes, buflen));
|
|
|
|
setLabel(ui->one_down_lb, tr_strlsize(buf, one.downloadedBytes, buflen));
|
|
|
|
setLabel(ui->one_time_lb, tr_strltime(buf, one.secondsActive, buflen));
|
|
|
|
setLabelFromRatio(ui->one_ratio_lb, one.ratio);
|
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
char const* const fmt = ngettext("Started %'d time", "Started %'d times", (int)all.sessionCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
g_snprintf(buf, buflen, fmt, (int)all.sessionCount);
|
|
|
|
setLabel(ui->all_sessions_lb, buf);
|
|
|
|
setLabel(ui->all_up_lb, tr_strlsize(buf, all.uploadedBytes, buflen));
|
|
|
|
setLabel(ui->all_down_lb, tr_strlsize(buf, all.downloadedBytes, buflen));
|
|
|
|
setLabel(ui->all_time_lb, tr_strltime(buf, all.secondsActive, buflen));
|
|
|
|
setLabelFromRatio(ui->all_ratio_lb, all.ratio);
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void dialogDestroyed(gpointer p, GObject* dialog)
|
2008-06-05 20:41:32 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(dialog);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
g_source_remove(GPOINTER_TO_UINT(p));
|
2008-06-05 20:41:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void dialogResponse(GtkDialog* dialog, gint response, gpointer gdata)
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2021-09-25 10:47:09 +00:00
|
|
|
auto* ui = static_cast<stat_ui*>(gdata);
|
2008-04-22 14:07:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (response == TR_RESPONSE_RESET)
|
2008-04-22 14:07:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* primary = _("Reset your statistics?");
|
2021-08-15 09:41:48 +00:00
|
|
|
char const* secondary = _(
|
|
|
|
"These statistics are for your information only. "
|
2017-04-21 07:40:57 +00:00
|
|
|
"Resetting them doesn't affect the statistics logged by your BitTorrent trackers.");
|
2021-09-25 10:47:09 +00:00
|
|
|
auto const flags = GtkDialogFlags(GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL);
|
2017-04-19 12:04:45 +00:00
|
|
|
GtkWidget* w = gtk_message_dialog_new(GTK_WINDOW(dialog), flags, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", primary);
|
2021-08-15 09:41:48 +00:00
|
|
|
gtk_dialog_add_buttons(
|
|
|
|
GTK_DIALOG(w),
|
|
|
|
TR_ARG_TUPLE(_("_Cancel"), GTK_RESPONSE_CANCEL),
|
|
|
|
TR_ARG_TUPLE(_("_Reset"), TR_RESPONSE_RESET),
|
2021-10-06 16:32:17 +00:00
|
|
|
nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(w), "%s", secondary);
|
|
|
|
|
|
|
|
if (gtk_dialog_run(GTK_DIALOG(w)) == TR_RESPONSE_RESET)
|
2010-01-03 17:58:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionClearStats(gtr_core_session(ui->core));
|
|
|
|
updateStats(ui);
|
2010-01-03 17:58:33 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
gtk_widget_destroy(w);
|
2008-04-22 14:07:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (response == GTK_RESPONSE_CLOSE)
|
|
|
|
{
|
|
|
|
gtk_widget_destroy(GTK_WIDGET(dialog));
|
|
|
|
}
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
GtkWidget* gtr_stats_dialog_new(GtkWindow* parent, TrCore* core)
|
2007-11-27 03:29:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
guint i;
|
|
|
|
GtkWidget* d;
|
|
|
|
GtkWidget* t;
|
|
|
|
GtkWidget* l;
|
|
|
|
guint row = 0;
|
|
|
|
struct stat_ui* ui = g_new0(struct stat_ui, 1);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
d = gtk_dialog_new_with_buttons(
|
|
|
|
_("Statistics"),
|
|
|
|
parent,
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
TR_ARG_TUPLE(_("_Reset"), TR_RESPONSE_RESET),
|
|
|
|
TR_ARG_TUPLE(_("_Close"), GTK_RESPONSE_CLOSE),
|
2021-10-06 16:32:17 +00:00
|
|
|
nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
gtk_dialog_set_default_response(GTK_DIALOG(d), GTK_RESPONSE_CLOSE);
|
|
|
|
t = hig_workarea_create();
|
|
|
|
ui->core = core;
|
|
|
|
|
|
|
|
hig_workarea_add_section_title(t, &row, _("Current Session"));
|
2021-10-06 16:32:17 +00:00
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->one_up_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->one_up_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Uploaded:"), l, nullptr);
|
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->one_down_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->one_down_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Downloaded:"), l, nullptr);
|
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->one_ratio_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->one_ratio_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Ratio:"), l, nullptr);
|
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->one_time_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->one_time_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Duration:"), l, nullptr);
|
2019-03-17 05:00:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
hig_workarea_add_section_divider(t, &row);
|
2019-03-17 05:00:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
hig_workarea_add_section_title(t, &row, _("Total"));
|
|
|
|
l = gtk_label_new(_("Started %'d time"));
|
|
|
|
ui->all_sessions_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->all_sessions_lb, TRUE);
|
2019-03-17 05:00:15 +00:00
|
|
|
hig_workarea_add_label_w(t, row, l);
|
|
|
|
++row;
|
2021-10-06 16:32:17 +00:00
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->all_up_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->all_up_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Uploaded:"), l, nullptr);
|
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->all_down_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->all_down_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Downloaded:"), l, nullptr);
|
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->all_ratio_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->all_ratio_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Ratio:"), l, nullptr);
|
|
|
|
l = gtk_label_new(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ui->all_time_lb = GTK_LABEL(l);
|
|
|
|
gtk_label_set_single_line_mode(ui->all_time_lb, TRUE);
|
2021-10-06 16:32:17 +00:00
|
|
|
hig_workarea_add_row(t, &row, _("Duration:"), l, nullptr);
|
2019-03-17 05:00:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
gtr_dialog_set_content(GTK_DIALOG(d), t);
|
|
|
|
|
|
|
|
updateStats(ui);
|
|
|
|
g_object_set_data_full(G_OBJECT(d), "data", ui, g_free);
|
|
|
|
g_signal_connect(d, "response", G_CALLBACK(dialogResponse), ui);
|
|
|
|
i = gdk_threads_add_timeout_seconds(SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS, updateStats, ui);
|
|
|
|
g_object_weak_ref(G_OBJECT(d), dialogDestroyed, GUINT_TO_POINTER(i));
|
|
|
|
return d;
|
2007-11-27 03:29:51 +00:00
|
|
|
}
|