| 1 |
#include "dclient/query_handler.h" |
| 2 |
#include "dclient/widgets.h" |
| 3 |
#include "dclient/widgets/map.h" |
| 4 |
#include "dclient/widgets/query.h" |
| 5 |
|
| 6 |
#include "adt/string.h" |
| 7 |
|
| 8 |
#include <ndk/application.hh> |
| 9 |
|
| 10 |
#include <boost/algorithm/string/split.hpp> |
| 11 |
|
| 12 |
query_handler::query_handler ( communicator &comm |
| 13 |
, widgets &ui |
| 14 |
, std::string const &username |
| 15 |
, std::string const &password |
| 16 |
) |
| 17 |
: sender (comm) |
| 18 |
, ui (ui) |
| 19 |
, username (username) |
| 20 |
, password (password) |
| 21 |
{ |
| 22 |
ui.query_input->on_commit () = [this] (std::string const &answer) { |
| 23 |
this->send ("reply", answer); |
| 24 |
this->ui.map->focus (); |
| 25 |
this->ui.query_input->set_text (""); |
| 26 |
this->ui.query->hide (); |
| 27 |
ndk::app->cursor_off (); |
| 28 |
}; |
| 29 |
} |
| 30 |
|
| 31 |
void |
| 32 |
query_handler::query (int flags, std::string const &prompt) |
| 33 |
{ |
| 34 |
if (!username.empty ()) |
| 35 |
{ |
| 36 |
if (flags == 0 && !strncmp (prompt, "What is your name?")) |
| 37 |
{ |
| 38 |
send ("reply", username); |
| 39 |
return; |
| 40 |
} |
| 41 |
else if (flags == 4 && !strncmp (prompt, "What is your password?")) |
| 42 |
{ |
| 43 |
send ("reply", password); |
| 44 |
return; |
| 45 |
} |
| 46 |
else if (flags == 4 && !strncmp (prompt, "Please type your password again.")) |
| 47 |
{ |
| 48 |
send ("reply", password); |
| 49 |
return; |
| 50 |
} |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
if (flags == 4 && !strncmp (prompt, "What is your password?")) |
| 55 |
ui.query_input->set_mode (ndk::edit::password); |
| 56 |
else if (flags == 4 && !strncmp (prompt, "Please type your password again.")) |
| 57 |
ui.query_input->set_mode (ndk::edit::password); |
| 58 |
else |
| 59 |
ui.query_input->set_mode (ndk::edit::normal); |
| 60 |
} |
| 61 |
|
| 62 |
std::vector<std::string> lines; |
| 63 |
boost::split (lines, prompt, [](char c) { return c == '\n'; }); |
| 64 |
|
| 65 |
ui.query->update (std::move (lines)); |
| 66 |
ui.query->visualize (); |
| 67 |
ui.repack (); |
| 68 |
|
| 69 |
ui.query_input->focus (); |
| 70 |
|
| 71 |
ndk::app->update (); |
| 72 |
} |