2020-01-08 16:43:04 +00:00
|
|
|
#include <ncurses.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <string>
|
2020-01-08 19:33:16 +00:00
|
|
|
#include <iostream>
|
2020-01-08 16:43:04 +00:00
|
|
|
|
|
|
|
#include "loginscreen.h"
|
2020-01-08 19:47:35 +00:00
|
|
|
#include "ui_utils.h"
|
2020-01-08 16:43:04 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
LoginScreen::LoginScreen()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
enum CursorPos {
|
|
|
|
EMAIL = 0,
|
|
|
|
PASSWORD = 1,
|
|
|
|
SUBMIT = 2
|
|
|
|
};
|
|
|
|
|
2020-01-08 16:43:04 +00:00
|
|
|
bool LoginScreen::exec()
|
|
|
|
{
|
|
|
|
initscr(); /* Start curses mode */
|
|
|
|
keypad(stdscr, TRUE); // Listen to function keys
|
2020-01-08 19:33:16 +00:00
|
|
|
noecho();
|
|
|
|
cbreak(); /* Line buffering disabled. pass on everything */
|
2020-01-08 16:43:04 +00:00
|
|
|
|
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
string email, password;
|
|
|
|
getCredentials(email, password);
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
mvprintw(3,0, "Done!");
|
2020-01-08 16:43:04 +00:00
|
|
|
|
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
getch(); /* Wait for user input */
|
|
|
|
endwin(); /* End curses mode */
|
|
|
|
}
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
void LoginScreen::getCredentials(string &email, string &pass)
|
|
|
|
{
|
|
|
|
int currPos = 0;
|
|
|
|
int currX = 0, currY = 0;
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
bool stop = false;
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
do {
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
erase();
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:47:35 +00:00
|
|
|
ui_utils::print_base_screen(stdscr);
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
int row, col;
|
|
|
|
getmaxyx(stdscr, row, col);
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
int startX = (row-4*2)/2;
|
|
|
|
int startY = (col-50)/2;
|
2020-01-08 16:43:04 +00:00
|
|
|
|
2020-01-08 19:33:16 +00:00
|
|
|
mvprintw(startX, startY, "Please login to your Comunic account:");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ask for email
|
|
|
|
if(currPos == EMAIL) wattron(stdscr, A_REVERSE);
|
|
|
|
mvprintw(startX+2, startY, "Email address:");
|
|
|
|
wattroff(stdscr, A_REVERSE);
|
|
|
|
printw(" ");
|
|
|
|
printw(email.c_str());
|
|
|
|
if(currPos == EMAIL) getyx(stdscr, currY, currX);
|
|
|
|
|
|
|
|
|
|
|
|
// Ask for password
|
|
|
|
if(currPos == PASSWORD) wattron(stdscr, A_REVERSE);
|
|
|
|
mvprintw(startX+4, startY, "Password:");
|
|
|
|
wattroff(stdscr, A_REVERSE);
|
|
|
|
printw(" ");
|
2020-01-08 19:36:58 +00:00
|
|
|
for(size_t i = 0; i < pass.length(); i++) printw("*");
|
2020-01-08 19:33:16 +00:00
|
|
|
if(currPos == PASSWORD) getyx(stdscr, currY, currX);
|
|
|
|
|
|
|
|
// Validate button
|
|
|
|
if(currPos == SUBMIT) wattron(stdscr, A_REVERSE);
|
|
|
|
mvprintw(startX+6, startY, "Login");
|
|
|
|
wattroff(stdscr, A_REVERSE);
|
|
|
|
|
2020-01-08 19:37:49 +00:00
|
|
|
if(currPos == SUBMIT) {curs_set(0);} else {curs_set(1);}
|
2020-01-08 19:33:16 +00:00
|
|
|
|
|
|
|
move(currY, currX);
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
int c = wgetch(stdscr);
|
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case KEY_UP:
|
|
|
|
currPos--;
|
|
|
|
if(currPos < 0) currPos = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DOWN:
|
2020-01-08 19:36:58 +00:00
|
|
|
case 9: // TAB
|
2020-01-08 19:33:16 +00:00
|
|
|
currPos++;
|
|
|
|
if(currPos > 2) currPos = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 10: // ENTER
|
|
|
|
if(currPos == EMAIL)
|
|
|
|
currPos++;
|
2020-01-08 20:06:14 +00:00
|
|
|
else {
|
|
|
|
if(email.length() < 3 || pass.length() < 3)
|
|
|
|
ui_utils::alert(stdscr, "Please complete all the field before submitting form!");
|
|
|
|
else
|
|
|
|
stop = true;
|
|
|
|
}
|
2020-01-08 19:33:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 263: // Erase
|
|
|
|
if(currPos == EMAIL && email.length() > 0)
|
|
|
|
email.pop_back();
|
|
|
|
else if(currPos == PASSWORD && pass.length() > 0)
|
|
|
|
pass.pop_back();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if(c > 126) break; // We can not process this value
|
|
|
|
|
|
|
|
if(currPos == EMAIL)
|
|
|
|
email += static_cast<char>(c);
|
|
|
|
else if(currPos == PASSWORD)
|
|
|
|
pass += static_cast<char>(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(!stop);
|
2020-01-08 16:43:04 +00:00
|
|
|
}
|