mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
fixed multi-digit numbers in terminal account selection
This commit is contained in:
parent
370d50b41a
commit
a778128147
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <cmath>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
#include <api/JsonStream.h>
|
#include <api/JsonStream.h>
|
||||||
|
|
||||||
@ -96,6 +98,13 @@ TerminalApiClient::~TerminalApiClient()
|
|||||||
fullstop();
|
fullstop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct AccountInfo
|
||||||
|
{
|
||||||
|
std::string name ;
|
||||||
|
std::string location ;
|
||||||
|
RsPeerId ssl_id ;
|
||||||
|
};
|
||||||
|
|
||||||
void TerminalApiClient::data_tick()
|
void TerminalApiClient::data_tick()
|
||||||
{
|
{
|
||||||
// values in milliseconds
|
// values in milliseconds
|
||||||
@ -109,11 +118,14 @@ void TerminalApiClient::data_tick()
|
|||||||
int last_char = 0;
|
int last_char = 0;
|
||||||
std::string inbuf;
|
std::string inbuf;
|
||||||
bool enter_was_pressed = false;
|
bool enter_was_pressed = false;
|
||||||
|
int account_number_size = 1 ;
|
||||||
|
int selected_account_number = 0 ;
|
||||||
|
int account_number_typed = 0 ;
|
||||||
|
|
||||||
StateToken runstate_state_token;
|
StateToken runstate_state_token;
|
||||||
std::string runstate;
|
std::string runstate;
|
||||||
|
|
||||||
std::vector<std::string> accounts;
|
std::vector<AccountInfo> accounts;
|
||||||
|
|
||||||
StateToken password_state_token;
|
StateToken password_state_token;
|
||||||
bool ask_for_password = false;
|
bool ask_for_password = false;
|
||||||
@ -224,7 +236,6 @@ void TerminalApiClient::data_tick()
|
|||||||
waitForResponse(id);
|
waitForResponse(id);
|
||||||
|
|
||||||
resps.switchToDeserialisation();
|
resps.switchToDeserialisation();
|
||||||
std::cout << "Type a number to select an account" << std::endl;
|
|
||||||
if(!resps.hasMore())
|
if(!resps.hasMore())
|
||||||
std::cout << "Error: No Accounts. Use the Qt-GUI or the webinterface to create an account." << std::endl;
|
std::cout << "Error: No Accounts. Use the Qt-GUI or the webinterface to create an account." << std::endl;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -234,37 +245,75 @@ void TerminalApiClient::data_tick()
|
|||||||
std::string id;
|
std::string id;
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string location;
|
std::string location;
|
||||||
|
|
||||||
resps.getStreamToMember()
|
resps.getStreamToMember()
|
||||||
<< makeKeyValueReference("id", id)
|
<< makeKeyValueReference("id", id)
|
||||||
<< makeKeyValueReference("name", name)
|
<< makeKeyValueReference("name", name)
|
||||||
<< makeKeyValueReference("location", location);
|
<< makeKeyValueReference("location", location);
|
||||||
std::cout << "[" << i << "] " << name << "(" << location << ")" << std::endl;
|
|
||||||
accounts.push_back(id);
|
AccountInfo info ;
|
||||||
|
info.location = location ;
|
||||||
|
info.name = name ;
|
||||||
|
info.ssl_id = RsPeerId(id) ;
|
||||||
|
|
||||||
|
accounts.push_back(info);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
account_number_size = (int)ceil(log(accounts.size())/log(10.0f)) ;
|
||||||
|
|
||||||
|
for(uint32_t i=0;i<accounts.size();++i)
|
||||||
|
std::cout << "[" << std::setw(account_number_size) << std::setfill('0') << i << "] " << accounts[i].name << " (" << accounts[i].location << ")" << std::endl;
|
||||||
|
|
||||||
|
std::cout << std::endl << "Type account number: " ;
|
||||||
|
std::cout.flush() ;
|
||||||
|
|
||||||
|
selected_account_number = 0 ;
|
||||||
|
account_number_typed = 0 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!ask_for_password && runstate == "waiting_account_select"
|
if(!ask_for_password && runstate == "waiting_account_select" && last_char >= '0' && last_char <= '9')
|
||||||
&& last_char >= '0' && last_char <= '9'
|
{
|
||||||
&& static_cast<uint32_t>(last_char-'0') < accounts.size())
|
std::cout.flush();
|
||||||
{
|
selected_account_number = 10*selected_account_number + last_char - '0' ;
|
||||||
std::string acc = accounts[last_char-'0'];
|
account_number_typed++ ;
|
||||||
JsonStream reqs;
|
|
||||||
JsonStream resps;
|
|
||||||
Request req(reqs);
|
|
||||||
std::stringstream ss;
|
|
||||||
Response resp(resps, ss);
|
|
||||||
|
|
||||||
req.mPath.push("login");
|
if(account_number_typed == account_number_size)
|
||||||
req.mPath.push("control");
|
{
|
||||||
reqs << makeKeyValueReference("id", acc);
|
if(selected_account_number < accounts.size())
|
||||||
reqs.switchToDeserialisation();
|
{
|
||||||
|
std::cout.flush();
|
||||||
|
std::cout << std::endl << "Selected account: " << accounts[selected_account_number].name << " (" << accounts[selected_account_number].location << ") SSL id: " << accounts[selected_account_number].ssl_id << std::endl;
|
||||||
|
|
||||||
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
|
std::string acc_ssl_id = accounts[selected_account_number].ssl_id.toStdString();
|
||||||
waitForResponse(id);
|
JsonStream reqs;
|
||||||
|
JsonStream resps;
|
||||||
|
Request req(reqs);
|
||||||
|
std::stringstream ss;
|
||||||
|
Response resp(resps, ss);
|
||||||
|
|
||||||
inbuf.clear();
|
req.mPath.push("login");
|
||||||
}
|
req.mPath.push("control");
|
||||||
|
reqs << makeKeyValueReference("id", acc_ssl_id);
|
||||||
|
reqs.switchToDeserialisation();
|
||||||
|
|
||||||
|
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
|
||||||
|
waitForResponse(id);
|
||||||
|
|
||||||
|
inbuf.clear();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cerr << ": invalid account number (should be between " << std::setw(account_number_size) << std::setfill('0')
|
||||||
|
<< 0 << " and " << std::setw(account_number_size) << std::setfill('0') << accounts.size()-1 << ")" << std::endl;
|
||||||
|
std::cout << std::endl << "Type account number: " ;
|
||||||
|
std::cout.flush() ;
|
||||||
|
}
|
||||||
|
|
||||||
|
account_number_typed = 0 ;
|
||||||
|
selected_account_number = 0 ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(edge && ask_for_password)
|
if(edge && ask_for_password)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user