The Battle for Wesnoth  1.15.1+dev
mp_ui_alerts.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * This namespace provides handlers which play the sounds / notificaitons
17  * for various mp server events, depending on the preference configuration.
18  */
19 
20 #include "mp_ui_alerts.hpp"
21 
23 #include "formula/string_utils.hpp"
24 #include "game_config.hpp"
25 #include "gettext.hpp"
26 #include "preferences/general.hpp"
27 #include "sound.hpp"
28 
29 #include <string>
30 #include <vector>
31 
32 namespace mp_ui_alerts {
33 
34 namespace {
35 
36 bool lobby_pref(const std::string& id)
37 {
38  return preferences::get(id + "_lobby", get_def_pref_lobby(id));
39 }
40 
41 bool sound_pref(const std::string& id)
42 {
43  return preferences::get(id + "_sound", get_def_pref_sound(id));
44 }
45 
46 bool notif_pref(const std::string& id)
47 {
48  return preferences::get(id + "_notif", get_def_pref_notif(id));
49 }
50 
51 } // end anonymous namespace
52 
53 // Note: This list must agree with data/gui/.../lobby_sound_options.cfg
54 const std::vector<std::string> items {"player_joins", "player_leaves", "private_message", "friend_message", "public_message", "server_message", "ready_for_start", "game_has_begun", "turn_changed"};
55 
56 void player_joins(bool is_lobby)
57 {
58  std::string id = "player_enters";
59  if (is_lobby && !lobby_pref(id)) {
60  return ;
61  }
62  if (sound_pref(id)) {
64  }
65  if (notif_pref(id)) {
66  desktop::notifications::send(_("Wesnoth"), _("A player has joined"), desktop::notifications::OTHER);
67  }
68 }
69 
70 void player_leaves(bool is_lobby)
71 {
72  std::string id = "player_leaves";
73  if (is_lobby && !lobby_pref(id)) {
74  return ;
75  }
76  if (sound_pref(id)) {
78  }
79  if (notif_pref(id)) {
80  desktop::notifications::send(_("Wesnoth"), _("A player has left"), desktop::notifications::OTHER);
81  }
82 }
83 
84 void public_message(bool is_lobby, const std::string & sender, const std::string & message)
85 {
86  std::string id = "public_message";
87  if (is_lobby && !lobby_pref(id)) {
88  return ;
89  }
90  if (sound_pref(id)) {
92  }
93  if (notif_pref(id)) {
95  }
96 }
97 
98 void friend_message(bool is_lobby, const std::string & sender, const std::string & message)
99 {
100  std::string id = "friend_message";
101  if (is_lobby && !lobby_pref(id)) {
102  return ;
103  }
104  if (sound_pref(id)) {
106  }
107  if (notif_pref(id)) {
109  }
110 }
111 
112 void private_message(bool is_lobby, const std::string & sender, const std::string & message)
113 {
114  std::string id = "private_message";
115  if (is_lobby && !lobby_pref(id)) {
116  return ;
117  }
118  if (sound_pref(id)) {
120  }
121  if (notif_pref(id)) {
123  }
124 }
125 
126 void server_message(bool is_lobby, const std::string & sender, const std::string & message)
127 {
128  std::string id = "server_message";
129  if (is_lobby && !lobby_pref(id)) {
130  return ;
131  }
132  if (sound_pref(id)) {
134  }
135  if (notif_pref(id)) {
137  }
138 }
139 
141 {
142  std::string id = "ready_for_start";
143  if (sound_pref(id)) {
144  if (preferences::UI_sound_on()) {
145  sound::play_bell(game_config::sounds::ready_for_start); //this is play_bell instead of play_UI_sound to economize on sound channels. UI only has two sounds, and turn bell has a dedicated channel.
146  }
147  }
148  if (notif_pref(id)) {
149  desktop::notifications::send(_("Wesnoth"), _("Ready to start!"), desktop::notifications::OTHER);
150  }
151 }
152 
154 {
155  std::string id = "game_has_begun";
156  if (sound_pref(id)) {
158  }
159  if (notif_pref(id)) {
160  desktop::notifications::send(_("Wesnoth"), _ ("Game has begun!"), desktop::notifications::OTHER);
161  }
162 }
163 
164 void turn_changed(const std::string & player_name)
165 {
166  std::string id = "turn_changed";
167  if (notif_pref(id)) {
168  utils::string_map player;
169  player["name"] = player_name;
170  desktop::notifications::send(_("Turn changed"), VGETTEXT("$name has taken control", player), desktop::notifications::TURN_CHANGED);
171  }
172 }
173 
174 bool get_def_pref_sound(const std::string & id) {
175  return (id != "public_message" && id != "friend_message");
176 }
177 
178 bool get_def_pref_notif(const std::string & id) {
179  return (desktop::notifications::available() && (id == "private_message" || id == "ready_for_start" || id == "game_has_begun" || id == "turn_changed"));
180 }
181 
182 bool get_def_pref_lobby(const std::string & id) {
183  return (id == "private_message" || id == "server_message");
184 }
185 
186 
187 } // end namespace mp_ui_alerts
void ready_for_start()
void turn_changed(const std::string &player_name)
std::map< std::string, t_string > string_map
void game_has_begun()
std::string private_message
This namespace provides handlers which play the sounds / notificaitons for various mp server events...
std::string friend_message
const std::vector< std::string > items
std::string player_joins
void friend_message(bool is_lobby, const std::string &sender, const std::string &message)
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
std::string get(const std::string &key)
Definition: general.cpp:228
void private_message(bool is_lobby, const std::string &sender, const std::string &message)
std::string ready_for_start
bool UI_sound_on()
Definition: general.cpp:629
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 server_message
std::string public_message
void public_message(bool is_lobby, const std::string &sender, const std::string &message)
void server_message(bool is_lobby, const std::string &sender, const std::string &message)
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool get_def_pref_sound(const std::string &id)
void player_leaves(bool is_lobby)
std::string player_leaves
void play_bell(const std::string &files)
Definition: sound.cpp:1003
void player_joins(bool is_lobby)
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1019
bool get_def_pref_lobby(const std::string &id)
std::string game_has_begun
bool get_def_pref_notif(const std::string &id)