ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/query_handler.cpp
Revision: 1.1
Committed: Sun Oct 17 08:15:11 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Log Message:
initial import

File Contents

# User Rev Content
1 sf-pippijn 1.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 ( interface::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     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     send ("reply", username);
38     else if (flags == 4 && !strncmp (prompt, "What is your password?"))
39     send ("reply", password);
40     else if (flags == 4 && !strncmp (prompt, "Please type your password again."))
41     send ("reply", password);
42     return;
43     }
44     else
45     {
46     if (flags == 4 && !strncmp (prompt, "What is your password?"))
47     ui.query_input.set_mode (ndk::edit::password);
48     else if (flags == 4 && !strncmp (prompt, "Please type your password again."))
49     ui.query_input.set_mode (ndk::edit::password);
50     else
51     ui.query_input.set_mode (ndk::edit::normal);
52     }
53    
54     std::vector<std::string> lines;
55     boost::split (lines, prompt, [](char c) { return c == '\n'; });
56    
57     ui.query->update (std::move (lines));
58     ui.query->visualize ();
59     ui.repack ();
60    
61     ui.query_input.focus ();
62    
63     ndk::app ().update ();
64     }