2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2005-2023 Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-02-07 07:35:33 +00:00
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "MainWindow.h"
|
2007-02-07 07:35:33 +00:00
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "Actions.h"
|
|
|
|
#include "FilterBar.h"
|
2022-12-29 02:42:20 +00:00
|
|
|
#include "GtkCompat.h"
|
2022-12-21 21:26:25 +00:00
|
|
|
#include "ListModelAdapter.h"
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "Prefs.h"
|
|
|
|
#include "PrefsDialog.h"
|
|
|
|
#include "Session.h"
|
2022-12-21 21:26:25 +00:00
|
|
|
#include "Torrent.h"
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "TorrentCellRenderer.h"
|
|
|
|
#include "Utils.h"
|
2007-02-07 07:35:33 +00:00
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h> // tr_formatter_speed_KBps()
|
|
|
|
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <gdkmm/cursor.h>
|
|
|
|
#include <gdkmm/rectangle.h>
|
|
|
|
#include <giomm/menu.h>
|
|
|
|
#include <giomm/menuitem.h>
|
|
|
|
#include <giomm/menumodel.h>
|
|
|
|
#include <giomm/simpleaction.h>
|
|
|
|
#include <giomm/simpleactiongroup.h>
|
2022-12-26 21:13:21 +00:00
|
|
|
#include <glibmm/i18n.h>
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <glibmm/main.h>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
#include <glibmm/ustring.h>
|
|
|
|
#include <glibmm/variant.h>
|
|
|
|
#include <gtkmm/image.h>
|
|
|
|
#include <gtkmm/label.h>
|
|
|
|
#include <gtkmm/menubutton.h>
|
|
|
|
#include <gtkmm/scrolledwindow.h>
|
|
|
|
#include <gtkmm/togglebutton.h>
|
|
|
|
#include <gtkmm/treemodel.h>
|
|
|
|
#include <gtkmm/treeselection.h>
|
|
|
|
#include <gtkmm/treeview.h>
|
|
|
|
#include <gtkmm/treeviewcolumn.h>
|
|
|
|
#include <gtkmm/widget.h>
|
|
|
|
#include <gtkmm/window.h>
|
|
|
|
|
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
#include <gtkmm/popovermenu.h>
|
|
|
|
#else
|
|
|
|
#include <gdkmm/display.h>
|
|
|
|
#include <gdkmm/window.h>
|
|
|
|
#include <gtkmm/menu.h>
|
|
|
|
#endif
|
2022-12-26 21:13:21 +00:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
using namespace std::string_literals;
|
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
|
|
|
using VariantInt = Glib::Variant<int>;
|
|
|
|
using VariantDouble = Glib::Variant<double>;
|
|
|
|
using VariantString = Glib::Variant<Glib::ustring>;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
auto constexpr OptionsMenuActionGroupName = "options-menu"sv;
|
|
|
|
auto constexpr StatsMenuActionGroupName = "stats-menu"sv;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
class MainWindow::Impl
|
2007-02-07 07:35:33 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
struct OptionMenuInfo
|
|
|
|
{
|
|
|
|
Glib::RefPtr<Gio::SimpleAction> action;
|
|
|
|
Glib::RefPtr<Gio::MenuItem> on_item;
|
|
|
|
Glib::RefPtr<Gio::Menu> section;
|
|
|
|
};
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
2022-09-07 22:25:04 +00:00
|
|
|
Impl(
|
|
|
|
MainWindow& window,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Glib::RefPtr<Gio::ActionGroup> const& actions,
|
|
|
|
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)
|
|
|
|
|
2022-11-10 19:35:31 +00:00
|
|
|
[[nodiscard]] Glib::RefPtr<Gtk::TreeSelection> get_selection() const;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
void refresh();
|
|
|
|
|
|
|
|
void prefsChanged(tr_quark key);
|
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
auto& signal_selection_changed()
|
|
|
|
{
|
|
|
|
return signal_selection_changed_;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
2022-12-21 21:26:25 +00:00
|
|
|
void init_view(Gtk::TreeView* view, Glib::RefPtr<FilterBar::Model> const& model);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
Glib::RefPtr<Gio::MenuModel> createOptionsMenu();
|
2022-11-10 19:35:31 +00:00
|
|
|
Glib::RefPtr<Gio::MenuModel> createSpeedMenu(Glib::RefPtr<Gio::SimpleActionGroup> const& actions, tr_direction dir);
|
|
|
|
Glib::RefPtr<Gio::MenuModel> createRatioMenu(Glib::RefPtr<Gio::SimpleActionGroup> const& actions);
|
2022-09-08 16:58:28 +00:00
|
|
|
|
|
|
|
Glib::RefPtr<Gio::MenuModel> createStatsMenu();
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-12-28 14:47:53 +00:00
|
|
|
void on_popup_menu(double event_x, double event_y);
|
2021-11-12 09:12:50 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void onSpeedToggled(std::string const& action_name, tr_direction dir, bool enabled);
|
2021-10-18 20:22:31 +00:00
|
|
|
void onSpeedSet(tr_direction dir, int KBps);
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void onRatioToggled(std::string const& action_name, bool enabled);
|
2021-10-18 20:22:31 +00:00
|
|
|
void onRatioSet(double ratio);
|
|
|
|
|
|
|
|
void updateStats();
|
|
|
|
void updateSpeeds();
|
|
|
|
|
|
|
|
void syncAltSpeedButton();
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void status_menu_toggled_cb(std::string const& action_name, Glib::ustring const& val);
|
|
|
|
void onOptionsClicked();
|
2021-10-18 20:22:31 +00:00
|
|
|
void alt_speed_toggled_cb();
|
|
|
|
void onAltSpeedToggledIdle();
|
|
|
|
|
|
|
|
private:
|
2021-11-12 09:12:50 +00:00
|
|
|
MainWindow& window_;
|
2022-09-07 22:25:04 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2021-11-12 09:12:50 +00:00
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
sigc::signal<void()> signal_selection_changed_;
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
Glib::RefPtr<Gio::ActionGroup> options_actions_;
|
|
|
|
Glib::RefPtr<Gio::ActionGroup> stats_actions_;
|
|
|
|
|
|
|
|
std::array<OptionMenuInfo, 2> speed_menu_info_;
|
|
|
|
OptionMenuInfo ratio_menu_info_;
|
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
TorrentCellRenderer* renderer_ = nullptr;
|
|
|
|
Gtk::TreeViewColumn* column_ = nullptr;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::ScrolledWindow* scroll_ = nullptr;
|
|
|
|
Gtk::TreeView* view_ = nullptr;
|
2022-09-07 22:25:04 +00:00
|
|
|
Gtk::Widget* toolbar_ = nullptr;
|
|
|
|
FilterBar* filter_;
|
|
|
|
Gtk::Widget* status_ = nullptr;
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::Label* ul_lb_ = nullptr;
|
|
|
|
Gtk::Label* dl_lb_ = nullptr;
|
|
|
|
Gtk::Label* stats_lb_ = nullptr;
|
|
|
|
Gtk::Image* alt_speed_image_ = nullptr;
|
|
|
|
Gtk::ToggleButton* alt_speed_button_ = nullptr;
|
|
|
|
sigc::connection pref_handler_id_;
|
2022-10-08 22:50:03 +00:00
|
|
|
IF_GTKMM4(Gtk::PopoverMenu*, Gtk::Menu*) popup_menu_ = nullptr;
|
2021-10-18 20:22:31 +00:00
|
|
|
};
|
2007-02-07 07:35:33 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-02-07 07:35:33 +00:00
|
|
|
|
2022-11-14 18:27:15 +00:00
|
|
|
void MainWindow::Impl::on_popup_menu([[maybe_unused]] double event_x, [[maybe_unused]] double event_y)
|
2007-02-07 07:35:33 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
if (popup_menu_ == nullptr)
|
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
auto const menu = gtr_action_get_object<Gio::Menu>("main-window-popup");
|
|
|
|
|
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
2022-10-13 16:18:33 +00:00
|
|
|
popup_menu_ = Gtk::make_managed<Gtk::PopoverMenu>(menu, Gtk::PopoverMenu::Flags::NESTED);
|
2022-10-08 22:50:03 +00:00
|
|
|
popup_menu_->set_parent(window_);
|
2022-10-13 16:18:33 +00:00
|
|
|
popup_menu_->set_has_arrow(false);
|
|
|
|
popup_menu_->set_halign(window_.get_direction() == Gtk::TextDirection::RTL ? Gtk::Align::END : Gtk::Align::START);
|
2022-10-08 22:50:03 +00:00
|
|
|
#else
|
|
|
|
popup_menu_ = Gtk::make_managed<Gtk::Menu>(menu);
|
2021-11-12 09:12:50 +00:00
|
|
|
popup_menu_->attach_to_widget(window_);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2021-11-12 09:12:50 +00:00
|
|
|
}
|
2020-08-18 10:36:10 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
2022-11-14 18:27:15 +00:00
|
|
|
int view_x = 0;
|
|
|
|
int view_y = 0;
|
|
|
|
view_->convert_bin_window_to_widget_coords(static_cast<int>(event_x), static_cast<int>(event_y), view_x, view_y);
|
2022-10-08 22:50:03 +00:00
|
|
|
double window_x = 0;
|
|
|
|
double window_y = 0;
|
|
|
|
view_->translate_coordinates(window_, view_x, view_y, window_x, window_y);
|
|
|
|
popup_menu_->set_pointing_to(Gdk::Rectangle(window_x, window_y, 1, 1));
|
|
|
|
popup_menu_->popup();
|
|
|
|
#else
|
|
|
|
popup_menu_->popup_at_pointer(nullptr);
|
|
|
|
#endif
|
2007-02-07 07:35:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
bool tree_view_search_equal_func(
|
|
|
|
Glib::RefPtr<Gtk::TreeModel> const& /*model*/,
|
|
|
|
int /*column*/,
|
|
|
|
Glib::ustring const& key,
|
2022-10-08 22:50:03 +00:00
|
|
|
Gtk::TreeModel::const_iterator const& iter)
|
2007-02-07 07:35:33 +00:00
|
|
|
{
|
2022-12-21 21:26:25 +00:00
|
|
|
static auto const& self_col = Torrent::get_columns().self;
|
|
|
|
|
|
|
|
auto const name = iter->get_value(self_col)->get_name_collated();
|
2021-10-18 20:22:31 +00:00
|
|
|
return name.find(key.lowercase()) == Glib::ustring::npos;
|
2007-02-07 07:35:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
2011-09-12 06:11:04 +00:00
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
void MainWindow::Impl::init_view(Gtk::TreeView* view, Glib::RefPtr<FilterBar::Model> const& model)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-21 21:26:25 +00:00
|
|
|
static auto const& torrent_cols = Torrent::get_columns();
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
view->set_search_column(torrent_cols.name_collated);
|
|
|
|
view->set_search_equal_func(&tree_view_search_equal_func);
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
column_ = view->get_column(0);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
renderer_ = Gtk::make_managed<TorrentCellRenderer>();
|
|
|
|
column_->pack_start(*renderer_, false);
|
2022-12-21 21:26:25 +00:00
|
|
|
column_->add_attribute(renderer_->property_torrent(), torrent_cols.self);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if !GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
view->signal_popup_menu().connect_notify([this]() { on_popup_menu(0, 0); });
|
|
|
|
#endif
|
|
|
|
setup_tree_view_button_event_handling(
|
|
|
|
*view,
|
|
|
|
[this, view](guint /*button*/, TrGdkModifierType /*state*/, double view_x, double view_y, bool context_menu_requested)
|
|
|
|
{
|
|
|
|
return on_tree_view_button_pressed(
|
|
|
|
*view,
|
|
|
|
view_x,
|
|
|
|
view_y,
|
|
|
|
context_menu_requested,
|
|
|
|
sigc::mem_fun(*this, &Impl::on_popup_menu));
|
|
|
|
},
|
|
|
|
[view](double view_x, double view_y) { return on_tree_view_button_released(*view, view_x, view_y); });
|
2021-10-18 20:22:31 +00:00
|
|
|
view->signal_row_activated().connect([](auto const& /*path*/, auto* /*column*/)
|
|
|
|
{ gtr_action_activate("show-torrent-properties"); });
|
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
view->set_model(IF_GTKMM4(ListModelAdapter::create<Torrent>(model), model));
|
|
|
|
|
|
|
|
view->get_selection()->signal_changed().connect([this]() { signal_selection_changed_.emit(); });
|
2007-02-07 07:35:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::prefsChanged(tr_quark const key)
|
2007-12-19 19:16:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (key)
|
2007-12-19 19:16:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_compact_view:
|
2021-10-18 20:22:31 +00:00
|
|
|
renderer_->property_compact() = gtr_pref_flag_get(key);
|
2012-12-22 20:35:19 +00:00
|
|
|
/* since the cell size has changed, we need gtktreeview to revalidate
|
|
|
|
* its fixed-height mode values. Unfortunately there's not an API call
|
2022-10-08 22:50:03 +00:00
|
|
|
* for that, but this seems to work for both GTK 3 and 4 */
|
|
|
|
view_->set_fixed_height_mode(false);
|
|
|
|
view_->set_row_separator_func({});
|
|
|
|
view_->unset_row_separator_func();
|
|
|
|
view_->set_fixed_height_mode(true);
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_show_statusbar:
|
2021-10-18 20:22:31 +00:00
|
|
|
status_->set_visible(gtr_pref_flag_get(key));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_show_filterbar:
|
2021-10-18 20:22:31 +00:00
|
|
|
filter_->set_visible(gtr_pref_flag_get(key));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_show_toolbar:
|
2021-10-18 20:22:31 +00:00
|
|
|
toolbar_->set_visible(gtr_pref_flag_get(key));
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_statusbar_stats:
|
2021-10-18 20:22:31 +00:00
|
|
|
refresh();
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_KEY_alt_speed_enabled:
|
|
|
|
case TR_KEY_alt_speed_up:
|
|
|
|
case TR_KEY_alt_speed_down:
|
2021-10-18 20:22:31 +00:00
|
|
|
syncAltSpeedButton();
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2007-12-19 19:16:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
MainWindow::Impl::~Impl()
|
2007-12-19 19:16:36 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
pref_handler_id_.disconnect();
|
2007-12-19 19:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void MainWindow::Impl::status_menu_toggled_cb(std::string const& action_name, Glib::ustring const& val)
|
2007-12-20 19:21:21 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
stats_actions_->change_action_state(action_name, VariantString::create(val));
|
|
|
|
core_->set_pref(TR_KEY_statusbar_stats, val.raw());
|
2007-12-20 19:21:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::syncAltSpeedButton()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
bool const b = gtr_pref_flag_get(TR_KEY_alt_speed_enabled);
|
|
|
|
alt_speed_button_->set_active(b);
|
2022-03-21 14:15:48 +00:00
|
|
|
alt_speed_button_->set_tooltip_text(fmt::format(
|
|
|
|
b ? _("Click to disable Alternative Speed Limits\n ({download_speed} down, {upload_speed} up)") :
|
|
|
|
_("Click to enable Alternative Speed Limits\n ({download_speed} down, {upload_speed} up)"),
|
|
|
|
fmt::arg("download_speed", tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_alt_speed_down))),
|
|
|
|
fmt::arg("upload_speed", tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_alt_speed_up)))));
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::alt_speed_toggled_cb()
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
core_->set_pref(TR_KEY_alt_speed_enabled, alt_speed_button_->get_active());
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2007-12-24 00:14:39 +00:00
|
|
|
/***
|
|
|
|
**** FILTER
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::onAltSpeedToggledIdle()
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
core_->set_pref(TR_KEY_alt_speed_enabled, tr_sessionUsesAltSpeed(core_->get_session()));
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-15 15:10:22 +00:00
|
|
|
/***
|
|
|
|
**** Speed limit menu
|
|
|
|
***/
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void MainWindow::Impl::onSpeedToggled(std::string const& action_name, tr_direction dir, bool enabled)
|
2009-04-15 04:28:04 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
options_actions_->change_action_state(action_name, VariantInt::create(enabled ? 1 : 0));
|
|
|
|
core_->set_pref(dir == TR_UP ? TR_KEY_speed_limit_up_enabled : TR_KEY_speed_limit_down_enabled, enabled);
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
2009-04-15 15:10:22 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::onSpeedSet(tr_direction dir, int KBps)
|
2009-04-15 04:28:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
core_->set_pref(dir == TR_UP ? TR_KEY_speed_limit_up : TR_KEY_speed_limit_down, KBps);
|
|
|
|
core_->set_pref(dir == TR_UP ? TR_KEY_speed_limit_up_enabled : TR_KEY_speed_limit_down_enabled, true);
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
2009-04-15 15:10:22 +00:00
|
|
|
|
2022-11-10 19:35:31 +00:00
|
|
|
Glib::RefPtr<Gio::MenuModel> MainWindow::Impl::createSpeedMenu(
|
|
|
|
Glib::RefPtr<Gio::SimpleActionGroup> const& actions,
|
|
|
|
tr_direction dir)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-13 17:36:16 +00:00
|
|
|
auto& info = speed_menu_info_.at(dir);
|
2022-09-08 16:58:28 +00:00
|
|
|
|
|
|
|
auto m = Gio::Menu::create();
|
|
|
|
|
|
|
|
auto const action_name = fmt::format("speed-limit-{}", dir == TR_UP ? "up" : "down");
|
|
|
|
auto const full_action_name = fmt::format("{}.{}", OptionsMenuActionGroupName, action_name);
|
|
|
|
info.action = actions->add_action_radio_integer(
|
|
|
|
action_name,
|
|
|
|
[this, action_name, dir](int value) { onSpeedToggled(action_name, dir, value != 0); },
|
|
|
|
0);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
info.section = Gio::Menu::create();
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
auto speedlimit_off_item = Gio::MenuItem::create(_("Unlimited"), full_action_name);
|
|
|
|
speedlimit_off_item->set_action_and_target(full_action_name, VariantInt::create(0));
|
|
|
|
info.section->append_item(speedlimit_off_item);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
info.on_item = Gio::MenuItem::create("...", full_action_name);
|
|
|
|
info.on_item->set_action_and_target(full_action_name, VariantInt::create(1));
|
|
|
|
info.section->append_item(info.on_item);
|
|
|
|
|
|
|
|
m->append_section(info.section);
|
|
|
|
auto section = Gio::Menu::create();
|
|
|
|
|
|
|
|
auto const stock_action_name = fmt::format("{}-stock", action_name);
|
|
|
|
auto const full_stock_action_name = fmt::format("{}.{}", OptionsMenuActionGroupName, stock_action_name);
|
|
|
|
actions->add_action_with_parameter(
|
|
|
|
stock_action_name,
|
|
|
|
VariantInt::variant_type(),
|
|
|
|
[this, dir](Glib::VariantBase const& value)
|
|
|
|
{ onSpeedSet(dir, Glib::VariantBase::cast_dynamic<VariantInt>(value).get()); });
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-02-24 02:27:28 +00:00
|
|
|
for (auto const KBps : { 50, 100, 250, 500, 1000, 2500, 5000, 10000 })
|
2009-04-15 15:10:22 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
auto item = Gio::MenuItem::create(tr_formatter_speed_KBps(KBps), full_stock_action_name);
|
|
|
|
item->set_action_and_target(full_stock_action_name, VariantInt::create(KBps));
|
|
|
|
section->append_item(item);
|
2009-04-15 15:10:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
m->append_section(section);
|
2017-04-19 12:04:45 +00:00
|
|
|
return m;
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
2009-04-15 15:10:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
**** Speed limit menu
|
|
|
|
***/
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void MainWindow::Impl::onRatioToggled(std::string const& action_name, bool enabled)
|
2009-04-15 04:28:04 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
options_actions_->change_action_state(action_name, VariantInt::create(enabled ? 1 : 0));
|
|
|
|
core_->set_pref(TR_KEY_ratio_limit_enabled, enabled);
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
2019-07-14 21:30:14 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::onRatioSet(double ratio)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
core_->set_pref(TR_KEY_ratio_limit, ratio);
|
|
|
|
core_->set_pref(TR_KEY_ratio_limit_enabled, true);
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 19:35:31 +00:00
|
|
|
Glib::RefPtr<Gio::MenuModel> MainWindow::Impl::createRatioMenu(Glib::RefPtr<Gio::SimpleActionGroup> const& actions)
|
2009-04-15 04:28:04 +00:00
|
|
|
{
|
2022-11-10 19:35:31 +00:00
|
|
|
static auto const stockRatios = std::array<double, 7>({ 0.25, 0.5, 0.75, 1, 1.5, 2, 3 });
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
auto& info = ratio_menu_info_;
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
auto m = Gio::Menu::create();
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
auto const action_name = "ratio-limit"s;
|
|
|
|
auto const full_action_name = fmt::format("{}.{}", OptionsMenuActionGroupName, action_name);
|
|
|
|
info.action = actions->add_action_radio_integer(
|
|
|
|
action_name,
|
|
|
|
[this, action_name](int value) { onRatioToggled(action_name, value != 0); },
|
|
|
|
0);
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
info.section = Gio::Menu::create();
|
|
|
|
|
|
|
|
auto ratio_off_item = Gio::MenuItem::create(_("Seed Forever"), full_action_name);
|
|
|
|
ratio_off_item->set_action_and_target(full_action_name, VariantInt::create(0));
|
|
|
|
info.section->append_item(ratio_off_item);
|
|
|
|
|
|
|
|
info.on_item = Gio::MenuItem::create("...", full_action_name);
|
|
|
|
info.on_item->set_action_and_target(full_action_name, VariantInt::create(1));
|
|
|
|
info.section->append_item(info.on_item);
|
|
|
|
|
|
|
|
m->append_section(info.section);
|
|
|
|
auto section = Gio::Menu::create();
|
|
|
|
|
|
|
|
auto const stock_action_name = fmt::format("{}-stock", action_name);
|
|
|
|
auto const full_stock_action_name = fmt::format("{}.{}", OptionsMenuActionGroupName, stock_action_name);
|
|
|
|
actions->add_action_with_parameter(
|
|
|
|
stock_action_name,
|
|
|
|
VariantDouble::variant_type(),
|
|
|
|
[this](Glib::VariantBase const& value) { onRatioSet(Glib::VariantBase::cast_dynamic<VariantDouble>(value).get()); });
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
for (auto const ratio : stockRatios)
|
2009-04-15 15:10:22 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
auto item = Gio::MenuItem::create(tr_strlratio(ratio), full_stock_action_name);
|
|
|
|
item->set_action_and_target(full_stock_action_name, VariantDouble::create(ratio));
|
|
|
|
section->append_item(item);
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
m->append_section(section);
|
2009-04-15 15:10:22 +00:00
|
|
|
return m;
|
|
|
|
}
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2009-04-15 15:10:22 +00:00
|
|
|
/***
|
|
|
|
**** Option menu
|
|
|
|
***/
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
Glib::RefPtr<Gio::MenuModel> MainWindow::Impl::createOptionsMenu()
|
2009-04-15 15:10:22 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
auto top = Gio::Menu::create();
|
|
|
|
auto actions = Gio::SimpleActionGroup::create();
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
auto section = Gio::Menu::create();
|
|
|
|
section->append_submenu(_("Limit Download Speed"), createSpeedMenu(actions, TR_DOWN));
|
|
|
|
section->append_submenu(_("Limit Upload Speed"), createSpeedMenu(actions, TR_UP));
|
|
|
|
top->append_section(section);
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
section = Gio::Menu::create();
|
|
|
|
section->append_submenu(_("Stop Seeding at Ratio"), createRatioMenu(actions));
|
|
|
|
top->append_section(section);
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
window_.insert_action_group(std::string(OptionsMenuActionGroupName), actions);
|
|
|
|
options_actions_ = actions;
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return top;
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
void MainWindow::Impl::onOptionsClicked()
|
2009-04-15 04:28:04 +00:00
|
|
|
{
|
2022-09-08 16:58:28 +00:00
|
|
|
static auto const update_menu = [](OptionMenuInfo& info, Glib::ustring const& new_on_label, tr_quark on_off_key)
|
|
|
|
{
|
|
|
|
if (auto on_label = Glib::VariantBase::cast_dynamic<VariantString>(info.on_item->get_attribute_value("label")).get();
|
|
|
|
on_label != new_on_label)
|
|
|
|
{
|
|
|
|
info.on_item->set_label(new_on_label);
|
|
|
|
|
|
|
|
// Items aren't refed by menu on insert but their attributes copied instead, so need to replace.
|
|
|
|
// (see https://docs.gtk.org/gio/method.Menu.insert_item.html)
|
|
|
|
info.section->remove(info.section->get_n_items() - 1);
|
|
|
|
info.section->append_item(info.on_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.action->change_state(gtr_pref_flag_get(on_off_key) ? 1 : 0);
|
|
|
|
};
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
update_menu(
|
|
|
|
speed_menu_info_[TR_DOWN],
|
|
|
|
tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_speed_limit_down)),
|
|
|
|
TR_KEY_speed_limit_down_enabled);
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
update_menu(
|
|
|
|
speed_menu_info_[TR_UP],
|
|
|
|
tr_formatter_speed_KBps(gtr_pref_int_get(TR_KEY_speed_limit_up)),
|
|
|
|
TR_KEY_speed_limit_up_enabled);
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
update_menu(
|
|
|
|
ratio_menu_info_,
|
|
|
|
fmt::format(_("Stop at Ratio ({ratio})"), fmt::arg("ratio", tr_strlratio(gtr_pref_double_get(TR_KEY_ratio_limit)))),
|
|
|
|
TR_KEY_ratio_limit_enabled);
|
|
|
|
}
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
Glib::RefPtr<Gio::MenuModel> MainWindow::Impl::createStatsMenu()
|
|
|
|
{
|
2022-11-10 19:35:31 +00:00
|
|
|
struct StatsModeInfo
|
2022-09-08 16:58:28 +00:00
|
|
|
{
|
|
|
|
char const* val;
|
|
|
|
char const* i18n;
|
2022-11-10 19:35:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static auto const stats_modes = std::array<StatsModeInfo, 4>({ {
|
2022-09-08 16:58:28 +00:00
|
|
|
{ "total-ratio", N_("Total Ratio") },
|
|
|
|
{ "session-ratio", N_("Session Ratio") },
|
|
|
|
{ "total-transfer", N_("Total Transfer") },
|
|
|
|
{ "session-transfer", N_("Session Transfer") },
|
2022-11-10 19:35:31 +00:00
|
|
|
} });
|
2022-09-08 16:58:28 +00:00
|
|
|
|
|
|
|
auto top = Gio::Menu::create();
|
|
|
|
auto actions = Gio::SimpleActionGroup::create();
|
|
|
|
|
|
|
|
auto const action_name = "stats-mode"s;
|
|
|
|
auto const full_action_name = fmt::format("{}.{}", StatsMenuActionGroupName, action_name);
|
|
|
|
auto stats_mode_action = actions->add_action_radio_string(
|
|
|
|
action_name,
|
|
|
|
[this, action_name](Glib::ustring const& value) { status_menu_toggled_cb(action_name, value); },
|
|
|
|
gtr_pref_string_get(TR_KEY_statusbar_stats));
|
|
|
|
|
|
|
|
for (auto const& mode : stats_modes)
|
|
|
|
{
|
|
|
|
auto item = Gio::MenuItem::create(_(mode.i18n), full_action_name);
|
|
|
|
item->set_action_and_target(full_action_name, VariantString::create(mode.val));
|
|
|
|
top->append_item(item);
|
|
|
|
}
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
window_.insert_action_group(std::string(StatsMenuActionGroupName), actions);
|
|
|
|
stats_actions_ = actions;
|
2009-04-15 04:28:04 +00:00
|
|
|
|
2022-09-08 16:58:28 +00:00
|
|
|
return top;
|
2009-04-15 04:28:04 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
**** PUBLIC
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::unique_ptr<MainWindow> MainWindow::create(
|
|
|
|
Gtk::Application& app,
|
2022-08-27 00:45:46 +00:00
|
|
|
Glib::RefPtr<Gio::ActionGroup> const& actions,
|
2021-11-01 00:11:23 +00:00
|
|
|
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("MainWindow.ui"));
|
|
|
|
return std::unique_ptr<MainWindow>(gtr_get_widget_derived<MainWindow>(builder, "MainWindow", app, actions, core));
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
MainWindow::MainWindow(
|
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Gtk::Application& app,
|
|
|
|
Glib::RefPtr<Gio::ActionGroup> const& actions,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
|
|
|
: Gtk::ApplicationWindow(cast_item)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, builder, actions, core))
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
|
|
|
app.add_window(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow() = default;
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
MainWindow::Impl::Impl(
|
|
|
|
MainWindow& window,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Glib::RefPtr<Gio::ActionGroup> const& actions,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
2021-11-12 09:12:50 +00:00
|
|
|
: window_(window)
|
|
|
|
, core_(core)
|
2022-09-07 22:25:04 +00:00
|
|
|
, scroll_(gtr_get_widget<Gtk::ScrolledWindow>(builder, "torrents_view_scroll"))
|
|
|
|
, view_(gtr_get_widget<Gtk::TreeView>(builder, "torrents_view"))
|
|
|
|
, toolbar_(gtr_get_widget<Gtk::Widget>(builder, "toolbar"))
|
2022-12-21 21:26:25 +00:00
|
|
|
, filter_(gtr_get_widget_derived<FilterBar>(builder, "filterbar", core_))
|
2022-09-07 22:25:04 +00:00
|
|
|
, status_(gtr_get_widget<Gtk::Widget>(builder, "statusbar"))
|
|
|
|
, ul_lb_(gtr_get_widget<Gtk::Label>(builder, "upload_speed_label"))
|
|
|
|
, dl_lb_(gtr_get_widget<Gtk::Label>(builder, "download_speed_label"))
|
|
|
|
, stats_lb_(gtr_get_widget<Gtk::Label>(builder, "statistics_label"))
|
|
|
|
, alt_speed_image_(gtr_get_widget<Gtk::Image>(builder, "alt_speed_button_image"))
|
|
|
|
, alt_speed_button_(gtr_get_widget<Gtk::ToggleButton>(builder, "alt_speed_button"))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
/* make the window */
|
2021-10-18 20:22:31 +00:00
|
|
|
window.set_title(Glib::get_application_name());
|
|
|
|
window.set_default_size(gtr_pref_int_get(TR_KEY_main_window_width), gtr_pref_int_get(TR_KEY_main_window_height));
|
2022-10-08 22:50:03 +00:00
|
|
|
#if !GTKMM_CHECK_VERSION(4, 0, 0)
|
2021-10-18 20:22:31 +00:00
|
|
|
window.move(gtr_pref_int_get(TR_KEY_main_window_x), gtr_pref_int_get(TR_KEY_main_window_y));
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (gtr_pref_flag_get(TR_KEY_main_window_is_maximized))
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
window.maximize();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 00:45:46 +00:00
|
|
|
window.insert_action_group("win", actions);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** Statusbar
|
|
|
|
**/
|
|
|
|
|
|
|
|
/* gear */
|
2022-09-08 16:58:28 +00:00
|
|
|
auto* gear_button = gtr_get_widget<Gtk::MenuButton>(builder, "gear_button");
|
|
|
|
gear_button->set_menu_model(createOptionsMenu());
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
for (auto* child = gear_button->get_first_child(); child != nullptr; child = child->get_next_sibling())
|
|
|
|
{
|
|
|
|
if (auto* popover = dynamic_cast<Gtk::Popover*>(child); popover != nullptr)
|
|
|
|
{
|
|
|
|
popover->signal_show().connect([this]() { onOptionsClicked(); }, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2022-09-08 16:58:28 +00:00
|
|
|
gear_button->signal_clicked().connect([this]() { onOptionsClicked(); }, false);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* turtle */
|
2021-11-12 09:12:50 +00:00
|
|
|
alt_speed_button_->signal_toggled().connect(sigc::mem_fun(*this, &Impl::alt_speed_toggled_cb));
|
2019-11-06 19:09:27 +00:00
|
|
|
|
|
|
|
/* ratio selector */
|
2022-09-08 16:58:28 +00:00
|
|
|
auto* ratio_button = gtr_get_widget<Gtk::MenuButton>(builder, "ratio_button");
|
|
|
|
ratio_button->set_menu_model(createStatsMenu());
|
2019-11-06 19:09:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** Workarea
|
|
|
|
**/
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
init_view(view_, filter_->get_filter_model());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-01-10 01:43:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* this is to determine the maximum width/height for the label */
|
2020-08-11 18:11:55 +00:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const pango_layout = ul_lb_->create_pango_layout("999.99 kB/s");
|
|
|
|
pango_layout->get_pixel_size(width, height);
|
|
|
|
ul_lb_->set_size_request(width, height);
|
|
|
|
dl_lb_->set_size_request(width, height);
|
2010-01-10 01:43:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* listen for prefs changes that affect the window */
|
2021-10-18 20:22:31 +00:00
|
|
|
prefsChanged(TR_KEY_compact_view);
|
|
|
|
prefsChanged(TR_KEY_show_filterbar);
|
|
|
|
prefsChanged(TR_KEY_show_statusbar);
|
|
|
|
prefsChanged(TR_KEY_statusbar_stats);
|
|
|
|
prefsChanged(TR_KEY_show_toolbar);
|
|
|
|
prefsChanged(TR_KEY_alt_speed_enabled);
|
2021-11-12 09:12:50 +00:00
|
|
|
pref_handler_id_ = core_->signal_prefs_changed().connect(sigc::mem_fun(*this, &Impl::prefsChanged));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
tr_sessionSetAltSpeedFunc(
|
|
|
|
core_->get_session(),
|
2021-12-14 08:43:27 +00:00
|
|
|
[](tr_session* /*s*/, bool /*isEnabled*/, bool /*byUser*/, gpointer p)
|
2021-10-18 20:22:31 +00:00
|
|
|
{ Glib::signal_idle().connect_once([p]() { static_cast<Impl*>(p)->onAltSpeedToggledIdle(); }); },
|
|
|
|
this);
|
|
|
|
|
|
|
|
refresh();
|
2022-09-02 16:21:43 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if !GTKMM_CHECK_VERSION(4, 0, 0)
|
2022-09-02 16:21:43 +00:00
|
|
|
/* prevent keyboard events being sent to the window first */
|
|
|
|
window.signal_key_press_event().connect(
|
|
|
|
[this](GdkEventKey* event) { return gtk_window_propagate_key_event(static_cast<Gtk::Window&>(window_).gobj(), event); },
|
|
|
|
false);
|
|
|
|
window.signal_key_release_event().connect(
|
|
|
|
[this](GdkEventKey* event) { return gtk_window_propagate_key_event(static_cast<Gtk::Window&>(window_).gobj(), event); },
|
|
|
|
false);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2007-02-07 07:35:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::updateStats()
|
2007-12-24 19:12:52 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring buf;
|
|
|
|
auto const* const session = core_->get_session();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* update the stats */
|
2022-01-24 00:53:35 +00:00
|
|
|
if (auto const pch = gtr_pref_string_get(TR_KEY_statusbar_stats); pch == "session-ratio")
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const stats = tr_sessionGetStats(session);
|
2022-04-23 03:41:30 +00:00
|
|
|
buf = fmt::format(_("Ratio: {ratio}"), fmt::arg("ratio", tr_strlratio(stats.ratio)));
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2021-10-18 20:22:31 +00:00
|
|
|
else if (pch == "session-transfer")
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const stats = tr_sessionGetStats(session);
|
2022-03-21 14:15:48 +00:00
|
|
|
buf = fmt::format(
|
|
|
|
C_("current session totals", "Down: {downloaded_size}, Up: {uploaded_size}"),
|
|
|
|
fmt::arg("downloaded_size", tr_strlsize(stats.downloadedBytes)),
|
|
|
|
fmt::arg("uploaded_size", tr_strlsize(stats.uploadedBytes)));
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2021-10-18 20:22:31 +00:00
|
|
|
else if (pch == "total-transfer")
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const stats = tr_sessionGetCumulativeStats(session);
|
2022-03-21 14:15:48 +00:00
|
|
|
buf = fmt::format(
|
|
|
|
C_("all-time totals", "Down: {downloaded_size}, Up: {uploaded_size}"),
|
|
|
|
fmt::arg("downloaded_size", tr_strlsize(stats.downloadedBytes)),
|
|
|
|
fmt::arg("uploaded_size", tr_strlsize(stats.uploadedBytes)));
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* default is total-ratio */
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const stats = tr_sessionGetCumulativeStats(session);
|
2022-03-21 14:15:48 +00:00
|
|
|
buf = fmt::format(_("Ratio: {ratio}"), fmt::arg("ratio", tr_strlratio(stats.ratio)));
|
2007-12-24 19:12:52 +00:00
|
|
|
}
|
2012-12-13 03:37:55 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
stats_lb_->set_text(buf);
|
2007-12-24 19:12:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::updateSpeeds()
|
2007-12-24 19:12:52 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const* const session = core_->get_session();
|
2007-12-24 19:12:52 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (session != nullptr)
|
2008-11-08 02:49:04 +00:00
|
|
|
{
|
2022-03-21 14:15:48 +00:00
|
|
|
auto dn_count = int{};
|
|
|
|
auto dn_speed = double{};
|
|
|
|
auto up_count = int{};
|
|
|
|
auto up_speed = double{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
auto const model = core_->get_model();
|
2022-12-21 21:26:25 +00:00
|
|
|
for (auto i = 0U, count = model->get_n_items(); i < count; ++i)
|
2009-08-07 20:49:06 +00:00
|
|
|
{
|
2022-12-21 21:26:25 +00:00
|
|
|
auto const torrent = gtr_ptr_dynamic_cast<Torrent>(model->get_object(i));
|
|
|
|
dn_count += torrent->get_active_peers_down();
|
|
|
|
dn_speed += torrent->get_speed_down();
|
|
|
|
up_count += torrent->get_active_peers_up();
|
|
|
|
up_speed += torrent->get_speed_up();
|
2009-08-07 20:49:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:29:05 +00:00
|
|
|
dl_lb_->set_text(fmt::format(_("{download_speed} ▼"), fmt::arg("download_speed", tr_formatter_speed_KBps(dn_speed))));
|
2022-03-21 14:15:48 +00:00
|
|
|
dl_lb_->set_visible(dn_count > 0);
|
2008-11-08 02:49:04 +00:00
|
|
|
|
2022-04-01 16:29:05 +00:00
|
|
|
ul_lb_->set_text(fmt::format(_("{upload_speed} ▲"), fmt::arg("upload_speed", tr_formatter_speed_KBps(up_speed))));
|
2022-03-21 14:15:48 +00:00
|
|
|
ul_lb_->set_visible(dn_count > 0 || up_count > 0);
|
2008-11-08 02:49:04 +00:00
|
|
|
}
|
2007-12-24 19:12:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::refresh()
|
2007-02-07 07:35:33 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
impl_->refresh();
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::Impl::refresh()
|
|
|
|
{
|
|
|
|
if (core_ != nullptr && core_->get_session() != nullptr)
|
2007-12-20 19:21:21 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
updateSpeeds();
|
|
|
|
updateStats();
|
2007-12-20 19:21:21 +00:00
|
|
|
}
|
2007-02-07 07:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
Glib::RefPtr<Gtk::TreeSelection> MainWindow::Impl::get_selection() const
|
2007-02-07 07:35:33 +00:00
|
|
|
{
|
2022-12-21 21:26:25 +00:00
|
|
|
return view_->get_selection();
|
2007-02-07 07:35:33 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-28 14:47:53 +00:00
|
|
|
void MainWindow::for_each_selected_torrent(std::function<void(Glib::RefPtr<Torrent> const&)> const& callback) const
|
2010-06-26 15:55:19 +00:00
|
|
|
{
|
2022-12-21 21:26:25 +00:00
|
|
|
for_each_selected_torrent_until(sigc::bind_return(callback, false));
|
|
|
|
}
|
|
|
|
|
2022-12-28 14:47:53 +00:00
|
|
|
bool MainWindow::for_each_selected_torrent_until(std::function<bool(Glib::RefPtr<Torrent> const&)> const& callback) const
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
static auto const& self_col = Torrent::get_columns().self;
|
|
|
|
|
|
|
|
auto const selection = impl_->get_selection();
|
|
|
|
auto const model = selection->get_model();
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
for (auto const& path : selection->get_selected_rows())
|
|
|
|
{
|
|
|
|
auto const torrent = Glib::make_refptr_for_instance(model->get_iter(path)->get_value(self_col));
|
|
|
|
torrent->reference();
|
|
|
|
if (callback(torrent))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::select_all()
|
|
|
|
{
|
|
|
|
impl_->get_selection()->select_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::unselect_all()
|
|
|
|
{
|
|
|
|
impl_->get_selection()->unselect_all();
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
2012-12-13 03:37:55 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MainWindow::set_busy(bool isBusy)
|
|
|
|
{
|
|
|
|
if (get_realized())
|
2010-12-12 16:43:19 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
auto const cursor = isBusy ? Gdk::Cursor::create("wait") : Glib::RefPtr<Gdk::Cursor>();
|
|
|
|
set_cursor(cursor);
|
|
|
|
#else
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const display = get_display();
|
|
|
|
auto const cursor = isBusy ? Gdk::Cursor::create(display, Gdk::WATCH) : Glib::RefPtr<Gdk::Cursor>();
|
|
|
|
get_window()->set_cursor(cursor);
|
|
|
|
display->flush();
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2010-06-26 15:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-21 21:26:25 +00:00
|
|
|
|
|
|
|
sigc::signal<void()>& MainWindow::signal_selection_changed()
|
|
|
|
{
|
|
|
|
return impl_->signal_selection_changed();
|
|
|
|
}
|