The Battle for Wesnoth  1.19.5+dev
mp_alerts_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/window.hpp"
25 
26 #include "mp_ui_alerts.hpp"
28 
29 #include <functional>
30 
31 #include "gettext.hpp"
32 
33 namespace gui2::dialogs
34 {
35 
36 /**
37  * Sets up the toggle buttons for a set of three MP lobby alerts
38  *
39  * @param pref_item_sound prefs_list constant for the sound preference
40  * @param pref_item_notif prefs_list constant for the notification preference
41  * @param pref_item_lobby prefs_list constant for the lobby preference
42  */
43 #define SETUP_ITEMS(pref_item_sound, pref_item_notif, pref_item_lobby) \
44 { \
45 toggle_button* sound = find_widget<toggle_button>(prefs_list::pref_item_sound, false, true); \
46 sound->set_value(prefs::get().pref_item_sound()); \
47 connect_signal_mouse_left_click(*sound, std::bind([sound]() { prefs::get().set_##pref_item_sound(sound->get_value_bool()); })); \
48 \
49 toggle_button* notif = find_widget<toggle_button>(prefs_list::pref_item_notif, false, true); \
50 \
51 if (!desktop::notifications::available()) { \
52  notif->set_value(false); \
53  notif->set_active(false); \
54  prefs::get().set_##pref_item_notif(false); \
55 } else { \
56  notif->set_active(true); \
57  notif->set_value(prefs::get().pref_item_notif()); \
58  connect_signal_mouse_left_click(*notif, std::bind([notif]() { prefs::get().set_##pref_item_notif(notif->get_value_bool()); }));\
59 } \
60 \
61 toggle_button* lobby = find_widget<toggle_button>(prefs_list::pref_item_lobby, false, true); \
62 lobby->set_value(prefs::get().pref_item_lobby()); \
63 connect_signal_mouse_left_click(*lobby, std::bind([lobby]() { prefs::get().set_##pref_item_lobby(lobby->get_value_bool()); })); \
64 }
65 
66 /**
67  * Set the defaults on the UI after clearing the preferences.
68  *
69  * @param pref_item_sound prefs_list constant for the sound preference
70  * @param pref_item_notif prefs_list constant for the notification preference
71  * @param pref_item_lobby prefs_list constant for the lobby preference
72  */
73 #define RESET_DEFAULT(pref_item_sound, pref_item_notif, pref_item_lobby) \
74 window.find_widget<toggle_button>(prefs_list::pref_item_sound).set_value(prefs::get().pref_item_sound()); \
75 window.find_widget<toggle_button>(prefs_list::pref_item_notif).set_value(prefs::get().pref_item_notif()); \
76 window.find_widget<toggle_button>(prefs_list::pref_item_lobby).set_value(prefs::get().pref_item_lobby());
77 
78 
80 {
81  // clear existing preferences for MP alerts
83 
84  // each preference knows its own default, so after clearing you get the default by just using the getter
85  RESET_DEFAULT(player_joins_sound, player_joins_notif, player_joins_lobby)
86  RESET_DEFAULT(player_leaves_sound, player_leaves_notif, player_leaves_lobby)
87  RESET_DEFAULT(private_message_sound, private_message_notif, private_message_lobby)
88  RESET_DEFAULT(friend_message_sound, friend_message_notif, friend_message_lobby)
89  RESET_DEFAULT(public_message_sound, public_message_notif, public_message_lobby)
90  RESET_DEFAULT(server_message_sound, server_message_notif, server_message_lobby)
91  RESET_DEFAULT(ready_for_start_sound, ready_for_start_notif, ready_for_start_lobby)
92  RESET_DEFAULT(game_has_begun_sound, game_has_begun_notif, game_has_begun_lobby)
93  RESET_DEFAULT(turn_changed_notif, turn_changed_notif, turn_changed_lobby)
94  RESET_DEFAULT(game_created_sound, game_created_notif, game_created_lobby)
95 }
96 
98 
100  : modal_dialog(window_id())
101 {
102 }
103 
105 {
106  SETUP_ITEMS(player_joins_sound, player_joins_notif, player_joins_lobby)
107  SETUP_ITEMS(player_leaves_sound, player_leaves_notif, player_leaves_lobby)
108  SETUP_ITEMS(private_message_sound, private_message_notif, private_message_lobby)
109  SETUP_ITEMS(friend_message_sound, friend_message_notif, friend_message_lobby)
110  SETUP_ITEMS(public_message_sound, public_message_notif, public_message_lobby)
111  SETUP_ITEMS(server_message_sound, server_message_notif, server_message_lobby)
112  SETUP_ITEMS(ready_for_start_sound, ready_for_start_notif, ready_for_start_lobby)
113  SETUP_ITEMS(game_has_begun_sound, game_has_begun_notif, game_has_begun_lobby)
114  SETUP_ITEMS(turn_changed_notif, turn_changed_notif, turn_changed_lobby)
115  SETUP_ITEMS(game_created_sound, game_created_notif, game_created_lobby)
116 
118  label* nlabel = find_widget<label>("notification_label", false, true);
119  nlabel->set_tooltip(_("This build of wesnoth does not include support for desktop notifications, contact your package manager"));
120  }
121 
122  toggle_button* in_lobby;
123  in_lobby = find_widget<toggle_button>(prefs_list::ready_for_start_lobby, false, true);
125 
126  in_lobby = find_widget<toggle_button>(prefs_list::game_has_begun_lobby, false, true);
128 
129  in_lobby = find_widget<toggle_button>(prefs_list::turn_changed_sound, false, true); // If we get a sound for this then don't remove this button
131 
132  in_lobby = find_widget<toggle_button>(prefs_list::turn_changed_lobby, false, true);
134 
135  button& defaults = find_widget<button>("revert_to_defaults");
136  connect_signal_mouse_left_click(defaults, std::bind([this](){ revert_to_default_pref_values(*this); }));
137 }
138 
140 {
141 }
142 
143 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
Abstract base class for all modal dialogs.
virtual void post_show() override
Actions to be taken after the window has been shown.
virtual void pre_show() override
Actions to be taken before showing the window.
void set_tooltip(const t_string &tooltip)
void set_visible(const visibility visible)
Definition: widget.cpp:479
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
void clear_mp_alert_prefs()
The most recently selected add-on id from the editor.
static prefs & get()
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
#define RESET_DEFAULT(pref_item_sound, pref_item_notif, pref_item_lobby)
Set the defaults on the UI after clearing the preferences.
#define SETUP_ITEMS(pref_item_sound, pref_item_notif, pref_item_lobby)
Sets up the toggle buttons for a set of three MP lobby alerts.
bool available()
Returns whether we were compiled with support for desktop notifications.
static void revert_to_default_pref_values(window &window)
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177