The Battle for Wesnoth  1.19.0+dev
mp_ui_alerts.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 /**
17  * This namespace provides handlers which play the sounds / notifications
18  * for various mp server events, depending on the preference configuration.
19  */
20 
21 #include "mp_ui_alerts.hpp"
22 
24 #include "formula/string_utils.hpp"
25 #include "game_config.hpp"
26 #include "gettext.hpp"
28 #include "sound.hpp"
29 
30 #include <string>
31 #include <vector>
32 
33 namespace mp::ui_alerts
34 {
35 namespace
36 {
37 bool lobby_pref(const std::string& id)
38 {
39  return prefs::get().mp_alert_option(id, "lobby", get_def_pref_lobby(id));
40 }
41 
42 bool sound_pref(const std::string& id)
43 {
44  return prefs::get().mp_alert_option(id, "sound", get_def_pref_sound(id));
45 }
46 
47 bool notif_pref(const std::string& id)
48 {
49  return prefs::get().mp_alert_option(id, "notif", get_def_pref_notif(id));
50 }
51 } // end anonymous namespace
52 
53 const std::vector<std::string> items{
64 };
65 
66 void game_created(const std::string& scenario, const std::string& name)
67 {
68  if(!lobby_pref(pref_constants::game_created)) {
69  return;
70  }
71 
72  if(sound_pref(pref_constants::game_created)) {
74  }
75 
76  if(notif_pref(pref_constants::game_created)) {
77  const std::string message = VGETTEXT("A game ($name|, $scenario|) has been created", {{"name", name}, {"scenario", scenario}});
79  }
80 }
81 
82 void player_joins(bool is_lobby)
83 {
84  if(is_lobby && !lobby_pref(pref_constants::player_joins)) {
85  return;
86  }
87 
88  if(sound_pref(pref_constants::player_joins)) {
90  }
91 
92  if(notif_pref(pref_constants::player_joins)) {
93  desktop::notifications::send(_("Wesnoth"), _("A player has joined"), desktop::notifications::OTHER);
94  }
95 }
96 
97 void player_leaves(bool is_lobby)
98 {
99  if(is_lobby && !lobby_pref(pref_constants::player_leaves)) {
100  return;
101  }
102 
103  if(sound_pref(pref_constants::player_leaves)) {
105  }
106 
107  if(notif_pref(pref_constants::player_leaves)) {
108  desktop::notifications::send(_("Wesnoth"), _("A player has left"), desktop::notifications::OTHER);
109  }
110 }
111 
112 void public_message(bool is_lobby, const std::string& sender, const std::string& message)
113 {
114  if(is_lobby && !lobby_pref(pref_constants::public_message)) {
115  return;
116  }
117 
118  if(sound_pref(pref_constants::public_message)) {
120  }
121 
122  if(notif_pref(pref_constants::public_message)) {
124  }
125 }
126 
127 void friend_message(bool is_lobby, const std::string& sender, const std::string& message)
128 {
129  if(is_lobby && !lobby_pref(pref_constants::friend_message)) {
130  return;
131  }
132 
133  if(sound_pref(pref_constants::friend_message)) {
135  }
136 
137  if(notif_pref(pref_constants::friend_message)) {
139  }
140 }
141 
142 void private_message(bool is_lobby, const std::string& sender, const std::string& message)
143 {
144  if(is_lobby && !lobby_pref(pref_constants::private_message)) {
145  return;
146  }
147 
148  if(sound_pref(pref_constants::private_message)) {
150  }
151 
152  if(notif_pref(pref_constants::private_message)) {
154  }
155 }
156 
157 void server_message(bool is_lobby, const std::string& sender, const std::string& message)
158 {
159  if(is_lobby && !lobby_pref(pref_constants::server_message)) {
160  return;
161  }
162 
163  if(sound_pref(pref_constants::server_message)) {
165  }
166 
167  if(notif_pref(pref_constants::server_message)) {
169  }
170 }
171 
173 {
174  if(sound_pref(pref_constants::ready_for_start)) {
175  if(prefs::get().ui_sound_on()) {
176  // this is play_bell instead of play_UI_sound to economize on sound channels. UI only has two
177  // sounds, and turn bell has a dedicated channel.
179  }
180  }
181 
182  if(notif_pref(pref_constants::ready_for_start)) {
183  desktop::notifications::send(_("Wesnoth"), _("Ready to start!"), desktop::notifications::OTHER);
184  }
185 }
186 
188 {
189  if(sound_pref(pref_constants::game_has_begun)) {
191  }
192 
193  if(notif_pref(pref_constants::game_has_begun)) {
194  desktop::notifications::send(_("Wesnoth"), _("Game has begun!"), desktop::notifications::OTHER);
195  }
196 }
197 
198 void turn_changed(const std::string& player_name)
199 {
200  if(notif_pref(pref_constants::turn_changed)) {
201  utils::string_map player;
202  player["name"] = player_name;
203  desktop::notifications::send(_("Turn changed"), VGETTEXT("$name has taken control", player), desktop::notifications::TURN_CHANGED);
204  }
205 }
206 
207 bool get_def_pref_sound(const std::string& id)
208 {
210 }
211 
212 bool get_def_pref_notif(const std::string& id)
213 {
214  return (desktop::notifications::available() && (
220  ));
221 }
222 
223 bool get_def_pref_lobby(const std::string& id)
224 {
226 }
227 
228 } // end namespace mp_ui_alerts
bool mp_alert_option(const std::string &id, const std::string &type, bool def=false)
static prefs & get()
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition: gettext.hpp:93
bool available()
Returns whether we were compiled with support for desktop notifications.
void send(const std::string &, const std::string &, type)
Displays a desktop notification message, from owner, of type t.
std::string public_message
std::string private_message
std::string player_leaves
std::string server_message
std::string friend_message
std::string ready_for_start
std::string game_has_begun
std::string game_created
std::string player_joins
static bool ui_sound_on()
This namespace provides handlers which play the sounds / notifications for various mp server events,...
void turn_changed(const std::string &player_name)
void public_message(bool is_lobby, const std::string &sender, const std::string &message)
const std::vector< std::string > items
bool get_def_pref_lobby(const std::string &id)
void game_created(const std::string &scenario, const std::string &name)
void private_message(bool is_lobby, const std::string &sender, const std::string &message)
void player_joins(bool is_lobby)
void game_has_begun()
void friend_message(bool is_lobby, const std::string &sender, const std::string &message)
void ready_for_start()
bool get_def_pref_sound(const std::string &id)
bool get_def_pref_notif(const std::string &id)
void server_message(bool is_lobby, const std::string &sender, const std::string &message)
void player_leaves(bool is_lobby)
const std::string friend_message
Definition: preferences.hpp:67
const std::string player_joins
Definition: preferences.hpp:64
const std::string player_leaves
Definition: preferences.hpp:65
const std::string ready_for_start
Definition: preferences.hpp:70
const std::string game_has_begun
Definition: preferences.hpp:71
const std::string private_message
Definition: preferences.hpp:66
const std::string public_message
Definition: preferences.hpp:68
const std::string game_created
Definition: preferences.hpp:73
const std::string turn_changed
Definition: preferences.hpp:72
const std::string server_message
Definition: preferences.hpp:69
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1064
void play_bell(const std::string &files)
Definition: sound.cpp:1048
std::map< std::string, t_string > string_map