2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 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-06-18 03:40:41 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
#include <array>
|
2022-10-08 22:50:03 +00:00
|
|
|
#include <stack>
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <string>
|
2021-11-12 09:12:50 +00:00
|
|
|
#include <string_view>
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <unordered_map>
|
2009-11-27 14:55:52 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/i18n.h>
|
2009-11-27 14:55:52 +00:00
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2009-11-27 14:55:52 +00:00
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "Actions.h"
|
|
|
|
#include "Prefs.h"
|
|
|
|
#include "PrefsDialog.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
2009-11-27 14:55:52 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
using VariantString = Glib::Variant<Glib::ustring>;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
Session* myCore = nullptr;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
void action_cb(Gio::SimpleAction& action, gpointer user_data)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
gtr_actions_handler(action.get_name(), user_data);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
void sort_changed_cb(Gio::SimpleAction& action, Glib::VariantBase const& value, gpointer /*user_data*/)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
action.set_state(value);
|
|
|
|
myCore->set_pref(TR_KEY_sort_mode, Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(value).get());
|
2007-12-19 02:46:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
std::array<std::string_view, 2> const show_toggle_entries = {
|
|
|
|
"toggle-main-window"sv,
|
|
|
|
"toggle-message-log"sv,
|
2007-09-21 16:29:09 +00:00
|
|
|
};
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
void toggle_pref_cb(Gio::SimpleAction& action, gpointer /*user_data*/)
|
2007-12-19 18:42:33 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
auto const key = action.get_name();
|
|
|
|
bool val = false;
|
|
|
|
action.get_state(val);
|
2020-08-18 10:36:10 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
action.set_state(Glib::Variant<bool>::create(!val));
|
2007-12-19 18:42:33 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
myCore->set_pref(tr_quark_new({ key.c_str(), key.size() }), !val);
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
std::array<std::string_view, 6> const pref_toggle_entries = {
|
|
|
|
"alt-speed-enabled"sv, //
|
|
|
|
"compact-view"sv, //
|
|
|
|
"sort-reversed"sv, //
|
|
|
|
"show-filterbar"sv, //
|
|
|
|
"show-statusbar"sv, //
|
|
|
|
"show-toolbar"sv, //
|
2021-10-18 20:22:31 +00:00
|
|
|
};
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2022-08-27 00:45:46 +00:00
|
|
|
std::array<std::string_view, 29> const entries = {
|
2021-11-12 09:12:50 +00:00
|
|
|
"copy-magnet-link-to-clipboard"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"open-torrent-from-url"sv,
|
|
|
|
"open-torrent"sv,
|
2021-11-12 09:12:50 +00:00
|
|
|
"torrent-start"sv,
|
|
|
|
"torrent-start-now"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"show-stats"sv,
|
|
|
|
"donate"sv,
|
2021-11-12 09:12:50 +00:00
|
|
|
"torrent-verify"sv,
|
|
|
|
"torrent-stop"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"pause-all-torrents"sv,
|
|
|
|
"start-all-torrents"sv,
|
2021-11-12 09:12:50 +00:00
|
|
|
"relocate-torrent"sv,
|
|
|
|
"remove-torrent"sv,
|
|
|
|
"delete-torrent"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"new-torrent"sv,
|
|
|
|
"quit"sv,
|
2021-11-12 09:12:50 +00:00
|
|
|
"select-all"sv,
|
|
|
|
"deselect-all"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"edit-preferences"sv,
|
2021-11-12 09:12:50 +00:00
|
|
|
"show-torrent-properties"sv,
|
|
|
|
"open-torrent-folder"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"show-about-dialog"sv,
|
|
|
|
"help"sv,
|
2021-11-12 09:12:50 +00:00
|
|
|
"torrent-reannounce"sv,
|
|
|
|
"queue-move-top"sv,
|
|
|
|
"queue-move-up"sv,
|
|
|
|
"queue-move-down"sv,
|
|
|
|
"queue-move-bottom"sv,
|
2022-08-27 00:45:46 +00:00
|
|
|
"present-main-window"sv,
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
Gtk::Builder* myBuilder = nullptr;
|
|
|
|
|
|
|
|
std::unordered_map<Glib::ustring, Glib::RefPtr<Gio::SimpleAction>> key_to_action;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
void gtr_actions_set_core(Glib::RefPtr<Session> const& core)
|
2007-12-19 18:42:33 +00:00
|
|
|
{
|
2022-09-30 22:15:21 +00:00
|
|
|
myCore = core.get();
|
2007-12-19 18:42:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 00:45:46 +00:00
|
|
|
Glib::RefPtr<Gio::SimpleActionGroup> gtr_actions_init(Glib::RefPtr<Gtk::Builder> const& builder, gpointer callback_user_data)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2022-09-30 22:15:21 +00:00
|
|
|
myBuilder = builder.get();
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
auto const action_group = Gio::SimpleActionGroup::create();
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const match = gtr_pref_string_get(TR_KEY_sort_mode);
|
|
|
|
|
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
auto const action_name = Glib::ustring("sort-torrents");
|
|
|
|
auto const action = Gio::SimpleAction::create_radio_string(action_name, match);
|
2022-09-30 22:15:21 +00:00
|
|
|
action->signal_activate().connect([a = action.get(), callback_user_data](auto const& value)
|
2021-11-12 09:12:50 +00:00
|
|
|
{ sort_changed_cb(*a, value, callback_user_data); });
|
|
|
|
action_group->add_action(action);
|
2021-12-01 23:11:57 +00:00
|
|
|
key_to_action.try_emplace(action_name, action);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
for (auto const& action_name_view : show_toggle_entries)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
auto const action_name = Glib::ustring(std::string(action_name_view));
|
|
|
|
auto const action = Gio::SimpleAction::create_bool(action_name);
|
2022-09-30 22:15:21 +00:00
|
|
|
action->signal_activate().connect([a = action.get(), callback_user_data](auto const& /*value*/)
|
2021-11-12 09:12:50 +00:00
|
|
|
{ action_cb(*a, callback_user_data); });
|
|
|
|
action_group->add_action(action);
|
2021-12-01 23:11:57 +00:00
|
|
|
key_to_action.try_emplace(action_name, action);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
for (auto const& action_name_view : pref_toggle_entries)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
auto const action_name = Glib::ustring(std::string(action_name_view));
|
|
|
|
auto const action = Gio::SimpleAction::create_bool(action_name, gtr_pref_flag_get(tr_quark_new(action_name_view)));
|
2022-09-30 22:15:21 +00:00
|
|
|
action->signal_activate().connect([a = action.get(), callback_user_data](auto const& /*value*/)
|
2021-11-12 09:12:50 +00:00
|
|
|
{ toggle_pref_cb(*a, callback_user_data); });
|
|
|
|
action_group->add_action(action);
|
2021-12-01 23:11:57 +00:00
|
|
|
key_to_action.try_emplace(action_name, action);
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-27 00:45:46 +00:00
|
|
|
for (auto const& action_name_view : entries)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
auto const action_name = Glib::ustring(std::string(action_name_view));
|
|
|
|
auto const action = Gio::SimpleAction::create(action_name);
|
2022-09-30 22:15:21 +00:00
|
|
|
action->signal_activate().connect([a = action.get(), callback_user_data](auto const& /*value*/)
|
2021-11-12 09:12:50 +00:00
|
|
|
{ action_cb(*a, callback_user_data); });
|
|
|
|
action_group->add_action(action);
|
2021-12-01 23:11:57 +00:00
|
|
|
key_to_action.try_emplace(action_name, action);
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
2007-12-19 02:46:30 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
return action_group;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
Glib::RefPtr<Gio::SimpleAction> get_action(Glib::ustring const& name)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return key_to_action.at(name);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_action_activate(Glib::ustring const& name)
|
|
|
|
{
|
|
|
|
get_action(name)->activate();
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_action_set_sensitive(Glib::ustring const& name, bool b)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
get_action(name)->set_enabled(b);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
void gtr_action_set_toggled(Glib::ustring const& name, bool b)
|
2009-08-16 03:09:06 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
get_action(name)->set_state(Glib::Variant<bool>::create(b));
|
2009-08-16 03:09:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
Glib::RefPtr<Glib::Object> gtr_action_get_object(Glib::ustring const& name)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
return myBuilder->get_object(name);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
|
|
|
|
Glib::RefPtr<Gio::ListModel> gtr_shortcuts_get_from_menu(Glib::RefPtr<Gio::MenuModel> const& menu)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
auto result = Gio::ListStore<Gtk::Shortcut>::create();
|
|
|
|
|
|
|
|
std::stack<Glib::RefPtr<Gio::MenuModel>> links;
|
|
|
|
links.push(menu);
|
|
|
|
|
|
|
|
while (!links.empty())
|
|
|
|
{
|
|
|
|
auto const link = links.top();
|
|
|
|
links.pop();
|
|
|
|
|
|
|
|
for (int i = 0; i < link->get_n_items(); ++i)
|
|
|
|
{
|
|
|
|
Glib::ustring action_name;
|
|
|
|
Glib::ustring action_accel;
|
|
|
|
|
|
|
|
for (auto it = link->iterate_item_attributes(i); it->next();)
|
|
|
|
{
|
|
|
|
if (auto const name = it->get_name(); name == "action")
|
|
|
|
{
|
|
|
|
action_name = Glib::VariantBase::cast_dynamic<VariantString>(it->get_value()).get();
|
|
|
|
}
|
|
|
|
else if (name == "accel")
|
|
|
|
{
|
|
|
|
action_accel = Glib::VariantBase::cast_dynamic<VariantString>(it->get_value()).get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!action_name.empty() && !action_accel.empty())
|
|
|
|
{
|
|
|
|
result->append(Gtk::Shortcut::create(
|
|
|
|
Gtk::ShortcutTrigger::parse_string(action_accel),
|
|
|
|
Gtk::NamedAction::create(action_name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = link->iterate_item_links(i); it->next();)
|
|
|
|
{
|
|
|
|
links.push(it->get_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2022-10-08 22:50:03 +00:00
|
|
|
|
|
|
|
#endif
|