Babel  1
The voip software that only works on your local network
Loading...
Searching...
No Matches
UserControls.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** epitech-ratrappage-babel (Workspace)
4** File description:
5** Controls.cpp
6*/
7
14#include <mutex>
15
16std::mutex logMutex;
17
19 : _playing(false), _recording(false), _looping(true), _echo(false), _help(false)
20{
21}
22
26
33{
34 _echo = echo;
35}
36
43{
44 _help = help;
45}
46
52void Controls::UserControls::setPlaying(const bool playing)
53{
54 _playing = playing;
55}
56
62void Controls::UserControls::setLooping(const bool looping)
63{
64 _looping = looping;
65}
66
72void Controls::UserControls::setRecording(const bool recording)
73{
74 _recording = recording;
75}
76
82void Controls::UserControls::setUserChoice(const std::string &userChoice)
83{
84 if (userChoice.empty()) {
85 const std::string errMsg = "User choice cannot be empty";
86 PRETTY_DEBUG << errMsg << std::endl;
88 }
89
90 const bool logStatus = Logging::Log::getInstance().getLogEnabled();
92
93 // if (userChoice == "r") {
94 // toggleRecording();
95 // if (isRecording()) {
96 // COLOUR_INFO << "Recording is enabled" << std::endl;
97 // } else {
98 // COLOUR_INFO << "Recording is disabled" << std::endl;
99 // }
100 // } else if (userChoice == "p") {
101 // togglePlaying();
102 // if (isPlaying()) {
103 // COLOUR_INFO << "Playing is enabled" << std::endl;
104 // } else {
105 // COLOUR_INFO << "Playing is disabled" << std::endl;
106 // }
107 // } else
108 if (userChoice == "l" || userChoice == "q" || userChoice == "esc") {
109 toggleLooping();
110 _hangUp = true;
111 if (isLooping()) {
112 COLOUR_INFO << "Looping is enabled" << std::endl;
113 } else {
114 COLOUR_INFO << "Looping is disabled" << std::endl;
115 }
116 } else if (userChoice == "e") {
117 toggleEcho();
118 if (isEcho()) {
119 COLOUR_INFO << "Echo is enabled" << std::endl;
120 } else {
121 COLOUR_INFO << "Echo is disabled" << std::endl;
122 }
123 } else if (userChoice == "h") {
124 toggleHelp();
125 } else if (userChoice == "hu") {
126 if (isEcho()) {
127 COLOUR_INFO << "Hanging up the call" << std::endl;
128 }
129 _hangUp = true;
130 toggleLooping();
131 } else if (userChoice == "i") {
132 COLOUR_INFO << getInfo() << std::endl;
133 } else {
135 throw CustomExceptions::InvalidChoice("Invalid user choice");
136 }
137 if (isHelp()) {
138 _displayHelp();
139 setHelp(false);
140 }
142}
143
150{
151 return _echo;
152}
153
160{
161 return _help;
162}
163
170{
171 return _playing;
172}
173
180{
181 return _looping;
182}
183
190{
191 return _recording;
192}
193
200{
201 return _hangUp;
202}
203
208{
209 _echo = !_echo;
210}
211
216{
217 _help = !_help;
218}
219
224{
225 _playing = !_playing;
226}
227
232{
233 _looping = !_looping;
234}
235
240{
241 _recording = !_recording;
242}
243
250{
251 return _echo;
252}
253
260{
261 return _help;
262}
263
270{
271 return _playing;
272}
273
280{
281 return _looping;
282}
283
290{
291 return _recording;
292}
293
297void Controls::UserControls::_displayHelp() const
298{
299 std::cout << RESET_COL << BACKGROUND_COL << INFO_COL << "Help:\n" << RESET_COL << BACKGROUND_COL << DEFAULT_FG;
300 // std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << "r:" << DEBUG_COL << " Record" << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Start a recording of a specified duration\n";
301 // std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << "p:" << DEBUG_COL << " Play " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Play the recording (if any are present)\n";
302 std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << " l:" << DEBUG_COL << " Loop " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Stop this loop (will stop the whole program)\n";
303 std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << "hu:" << DEBUG_COL << " Hang Up " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Hang the phone up, close the connection between both concerned parties\n";
304 std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << " e:" << DEBUG_COL << " Echo " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Toggle if to display or not the command that was entered\n";
305 std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << " h:" << DEBUG_COL << " Help " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Display this help.\n";
306 std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << " i:" << DEBUG_COL << " Info " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Display the current state of the user command prompt configuration\n";
307 std::cout << RESET_COL << BACKGROUND_COL << "\t" << SUCCESS_COL << " q:" << DEBUG_COL << " Quit " << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "\t Exit the program\n";
308 std::cout << RESET_COL << std::flush;
309}
310
316const std::string Controls::UserControls::_gatherResponse() const
317{
318 std::string response;
319 char c;
320 while (std::cin.get(c)) {
321 if (c == '\n') {
322 break;
323 }
324 response += c;
325 }
326 return response;
327}
328
333{
334 // std::cout << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "Enter your choice ([r]ecord, [p]lay, [l]oop, [e]cho, [h]elp, [i]nfo, [q]uit): " << std::flush;
335 std::cout << RESET_COL << BACKGROUND_COL << DEFAULT_FG << "Enter your choice ([hu] Hang Up, [l]oop, [e]cho, [h]elp, [i]nfo, [q]uit): " << std::flush;
336}
337
343const std::string Controls::UserControls::_getUserChoice() const
344{
345 std::string choice;
346 showPrompt();
347 choice = _gatherResponse();
348 return choice;
349}
350
355{
356 while (_looping == true) {
357 try {
358 std::string resp = _getUserChoice();
359 if (_echo) {
360 bool logStatus = Logging::Log::getInstance().getLogEnabled();
362 COLOUR_INFO << "User choice: '" << resp << "'" << std::endl;
364 }
365 setUserChoice(resp);
366 }
367 catch (const std::exception &e) {
368 bool logStatus = Logging::Log::getInstance().getLogEnabled();
370 COLOUR_ERROR << e.what() << std::endl;
372 }
373 }
374}
375
382const std::string Controls::UserControls::getInfo(const unsigned int indent) const
383{
384 std::string indentation = "";
385 for (unsigned int i = 0; i < indent; ++i) {
386 indentation += "\t";
387 }
388 std::string result = indentation + "User Controls:\n";
389 result += indentation + "- Echo: '" + Recoded::myToString(_echo) + "'\n";
390 result += indentation + "- Help: '" + Recoded::myToString(_help) + "'\n";
391 result += indentation + "- Playing: '" + Recoded::myToString(_playing) + "'\n";
392 result += indentation + "- Looping: '" + Recoded::myToString(_looping) + "'\n";
393 result += indentation + "- Recording: '" + Recoded::myToString(_recording) + "'\n";
394 return result;
395}
396
404std::ostream &Controls::operator<<(std::ostream &os, const Controls::UserControls &network)
405{
406 os << network.getInfo();
407 return os;
408}
#define SUCCESS_COL
Definition LogMacros.hpp:78
#define PRETTY_DEBUG
Debug log with details and colour.
#define RESET_COL
Definition LogMacros.hpp:76
#define COLOUR_INFO
Info log with colour.
#define INFO_COL
Definition LogMacros.hpp:79
#define COLOUR_ERROR
Error log with colour.
#define BACKGROUND_COL
Empty colour codes for unsupported platforms.
Definition LogMacros.hpp:74
#define DEFAULT_FG
Definition LogMacros.hpp:75
#define DEBUG_COL
Definition LogMacros.hpp:77
std::mutex logMutex
This file contains the definition of the UserControls class, which manages user input and control sta...
Manages user input and control states.
void spamUserChoice()
Continuously prompts the user for input and processes it.
const bool getEcho() const
Gets the echo state.
void togglePlaying()
Toggles the playing state.
void setUserChoice(const std::string &userChoice)
Processes the user choice.
const bool getRecording() const
Gets the recording state.
const bool isRecording() const
Checks if recording is enabled.
const bool isPlaying() const
Checks if playing is enabled.
void setRecording(const bool recording)
Sets the recording state.
void toggleLooping()
Toggles the looping state.
const bool getHelp() const
Gets the help state.
void setHelp(const bool help)
Sets the help state.
void setPlaying(const bool playing)
Sets the playing state.
const bool getLooping() const
Gets the looping state.
void setLooping(const bool looping)
Sets the looping state.
const bool hangUpTheCall() const
Checks if the call should be hung up.
void toggleHelp()
Toggles the help state.
const bool isHelp() const
Checks if help is enabled.
const bool getPlaying() const
Gets the playing state.
void setEcho(const bool echo)
Sets the echo state.
void toggleRecording()
Toggles the recording state.
const bool isEcho() const
Checks if echo is enabled.
void toggleEcho()
Toggles the echo state.
const bool isLooping() const
Checks if looping is enabled.
void showPrompt() const
Displays the prompt to the user.
const std::string getInfo(const unsigned int indent=0) const
Dumps the current state of the variables for debugging purposes.
Exception class for invalid operations.
Definition Invalid.hpp:159
void setLogEnabled(bool enabled)
Enables or disables logging.
Definition Log.cpp:56
static Log & getInstance(const bool debug=false)
Provides access to the singleton instance of the Log class.
Definition Log.cpp:22
const bool getLogEnabled() const
Checks if logging is enabled.
Definition Log.cpp:76
std::ostream & operator<<(std::ostream &os, const ThreadCapsule &network)
Overloads the stream insertion operator for ThreadCapsule.
const std::string myToString(bool value)
Converts a boolean value to its string representation.
Definition ToString.cpp:22