The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mp_ui_alerts.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
22 #include "global.hpp"
23 
25 #include "formula/string_utils.hpp"
26 #include "game_config.hpp"
27 #include "gettext.hpp"
28 #include "preferences.hpp"
29 #include "sound.hpp"
30 
31 #include <string>
32 #include <vector>
33 
34 namespace mp_ui_alerts {
35 
36 namespace {
37 
38 bool lobby_pref(std::string id)
39 {
40  return preferences::get(id + "_lobby", get_def_pref_lobby(id));
41 }
42 
43 bool sound_pref(std::string id)
44 {
45  return preferences::get(id + "_sound", get_def_pref_sound(id));
46 }
47 
48 bool notif_pref(std::string id)
49 {
50  return preferences::get(id + "_notif", get_def_pref_notif(id));
51 }
52 
53 } // end anonymous namespace
54 
55 // Note: This list must agree with data/gui/.../lobby_sound_options.cfg
56 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"};
57 
58 void player_joins(bool is_lobby)
59 {
60  std::string id = "player_enters";
61  if (is_lobby && !lobby_pref(id)) {
62  return ;
63  }
64  if (sound_pref(id)) {
66  }
67  if (notif_pref(id)) {
68  desktop::notifications::send(_("Wesnoth"), _("A player has joined"), desktop::notifications::OTHER);
69  }
70 }
71 
72 void player_leaves(bool is_lobby)
73 {
74  std::string id = "player_leaves";
75  if (is_lobby && !lobby_pref(id)) {
76  return ;
77  }
78  if (sound_pref(id)) {
80  }
81  if (notif_pref(id)) {
82  desktop::notifications::send(_("Wesnoth"), _("A player has left"), desktop::notifications::OTHER);
83  }
84 }
85 
86 void public_message(bool is_lobby, const std::string & sender, const std::string & message)
87 {
88  std::string id = "public_message";
89  if (is_lobby && !lobby_pref(id)) {
90  return ;
91  }
92  if (sound_pref(id)) {
94  }
95  if (notif_pref(id)) {
97  }
98 }
99 
100 void friend_message(bool is_lobby, const std::string & sender, const std::string & message)
101 {
102  std::string id = "friend_message";
103  if (is_lobby && !lobby_pref(id)) {
104  return ;
105  }
106  if (sound_pref(id)) {
108  }
109  if (notif_pref(id)) {
111  }
112 }
113 
114 void private_message(bool is_lobby, const std::string & sender, const std::string & message)
115 {
116  std::string id = "private_message";
117  if (is_lobby && !lobby_pref(id)) {
118  return ;
119  }
120  if (sound_pref(id)) {
122  }
123  if (notif_pref(id)) {
125  }
126 }
127 
128 void server_message(bool is_lobby, const std::string & sender, const std::string & message)
129 {
130  std::string id = "server_message";
131  if (is_lobby && !lobby_pref(id)) {
132  return ;
133  }
134  if (sound_pref(id)) {
136  }
137  if (notif_pref(id)) {
139  }
140 }
141 
143 {
144  std::string id = "ready_for_start";
145  if (sound_pref(id)) {
146  if (preferences::UI_sound_on()) {
147  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.
148  }
149  }
150  if (notif_pref(id)) {
151  desktop::notifications::send(_("Wesnoth"), _("Ready to start!"), desktop::notifications::OTHER);
152  }
153 }
154 
156 {
157  std::string id = "game_has_begun";
158  if (sound_pref(id)) {
160  }
161  if (notif_pref(id)) {
162  desktop::notifications::send(_("Wesnoth"), _ ("Game has begun!"), desktop::notifications::OTHER);
163  }
164 }
165 
166 void turn_changed(const std::string & player_name)
167 {
168  std::string id = "turn_changed";
169  if (notif_pref(id)) {
170  utils::string_map player;
171  player["name"] = player_name;
172  desktop::notifications::send(_("Turn changed"), vgettext("$name has taken control", player), desktop::notifications::TURN_CHANGED);
173  }
174 }
175 
176 bool get_def_pref_sound(const std::string & id) {
177  return (id != "public_message" && id != "friend_message");
178 }
179 
180 bool get_def_pref_notif(const std::string & id) {
181  return (desktop::notifications::available() && (id == "private_message" || id == "ready_for_start" || id == "game_has_begun" || id == "turn_changed"));
182 }
183 
184 bool get_def_pref_lobby(const std::string & id) {
185  return (id == "private_message" || id == "server_message");
186 }
187 
188 
189 } // end namespace mp_ui_alerts
void ready_for_start()
void turn_changed(const std::string &player_name)
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:82
std::string get(const std::string &key)
std::map< std::string, t_string > string_map
void private_message(bool is_lobby, const std::string &sender, const std::string &message)
std::string ready_for_start
bool UI_sound_on()
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)
bool get_def_pref_sound(const std::string &id)
std::string vgettext(const char *msgid, const utils::string_map &symbols)
void player_leaves(bool is_lobby)
std::string player_leaves
void play_bell(const std::string &files)
Definition: sound.cpp:826
void player_joins(bool is_lobby)
void play_UI_sound(const std::string &files)
Definition: sound.cpp:842
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
bool get_def_pref_lobby(const std::string &id)
std::string game_has_begun
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool get_def_pref_notif(const std::string &id)