2020-08-03 22:40:07 +02:00
/**
* @ file tui . cpp
* @ brief display user interface
* @ author hendrik schutter
* @ date 03.08 .2020
*/
2020-08-04 22:35:29 +02:00
# include "../../include/reHDD.h"
2020-08-07 11:38:00 +02:00
TUI : : TUI ( void )
{
2020-08-04 22:35:29 +02:00
}
2020-08-03 22:40:07 +02:00
/**
* \ brief wipe drive with shred
* \ param pointer of Drive instance
* \ return void
*/
2020-08-04 22:35:29 +02:00
2020-08-03 22:40:07 +02:00
void TUI : : initTUI ( )
{
initscr ( ) ;
raw ( ) ;
keypad ( stdscr , TRUE ) ;
2020-08-07 11:38:00 +02:00
if ( has_colors ( ) = = TRUE )
{
start_color ( ) ;
}
else
{
printf ( " Your terminal does not support color \n " ) ;
exit ( 1 ) ;
}
2020-08-03 22:40:07 +02:00
clear ( ) ;
curs_set ( 0 ) ;
2020-08-06 22:45:05 +02:00
noecho ( ) ;
cbreak ( ) ;
2020-08-03 22:40:07 +02:00
init_color ( COLOR_GRAY , 173 , 170 , 173 ) ;
2020-08-06 11:41:38 +02:00
init_pair ( COLOR_AREA_STDSCR , COLOR_WHITE , COLOR_BLUE ) ;
wbkgd ( stdscr , COLOR_PAIR ( COLOR_AREA_STDSCR ) ) ;
2020-08-07 11:38:00 +02:00
init_pair ( COLOR_AREA_ENTRY , COLOR_BLACK , COLOR_GRAY ) ;
init_pair ( COLOR_AREA_ENTRY_SELECTED , COLOR_BLACK , COLOR_WHITE ) ;
init_pair ( COLOR_AREA_OVERVIEW , COLOR_BLACK , COLOR_GRAY ) ;
init_pair ( COLOR_AREA_OVERVIEW , COLOR_BLACK , COLOR_GRAY ) ;
2020-08-06 22:45:05 +02:00
mvprintw ( 0 , 2 , " reHDD - HDD refurbishing tool - GPL 3.0 " ) ;
2020-08-06 11:41:38 +02:00
2020-08-03 22:40:07 +02:00
}
2020-08-07 11:38:00 +02:00
void TUI : : updateTUI ( vector < Drive > * pvecDrives , int32_t i32SelectedEntry )
{
2020-08-04 22:35:29 +02:00
int stdscrX , stdscrY ;
getmaxyx ( stdscr , stdscrY , stdscrX ) ;
init_pair ( COLOR_AREA_STDSCR , COLOR_WHITE , COLOR_BLUE ) ;
wbkgd ( stdscr , COLOR_PAIR ( COLOR_AREA_STDSCR ) ) ;
2020-08-06 11:41:38 +02:00
2020-08-04 22:35:29 +02:00
refresh ( ) ;
2020-08-03 22:40:07 +02:00
2020-08-04 22:35:29 +02:00
overview = createOverViewWindow ( ( int ) ( stdscrX / 3 ) , ( stdscrY - 15 ) ) ;
wrefresh ( overview ) ;
2020-08-03 22:40:07 +02:00
2020-08-04 22:35:29 +02:00
vector < Drive > : : iterator it ;
for ( it = pvecDrives - > begin ( ) ; it ! = pvecDrives - > end ( ) ; + + it )
2020-08-07 11:38:00 +02:00
{
string sModelFamily = it - > getModelFamily ( ) ;
string sModelName = it - > getModelName ( ) ;
string sCapacity = it - > sCapacityToText ( ) ;
bool bSelectedEntry = false ;
if ( i32SelectedEntry = = ( it - pvecDrives - > begin ( ) ) )
{
bSelectedEntry = true ;
2020-08-07 12:07:29 +02:00
detailview = createDetailViewWindow ( ( ( stdscrX ) - ( int ) ( stdscrX / 3 ) - 7 ) , ( stdscrY - 3 ) , ( int ) ( stdscrX / 3 ) + 5 , pvecDrives - > at ( ( it - pvecDrives - > begin ( ) ) ) ) ;
wrefresh ( detailview ) ;
2020-08-07 11:38:00 +02:00
}
WINDOW * tmp = createEntryWindow ( ( ( int ) ( stdscrX / 3 ) - 2 ) , 5 , 3 , ( 5 * ( it - pvecDrives - > begin ( ) ) ) + 3 , sModelFamily , sModelName , sCapacity , bSelectedEntry ) ;
wrefresh ( tmp ) ;
}
2020-08-06 22:45:05 +02:00
}
2020-08-04 22:35:29 +02:00
2020-08-07 11:38:00 +02:00
enum TUI : : UserInput TUI : : readUserInput ( )
{
int ch = wgetch ( stdscr ) ;
switch ( ch )
2020-08-06 22:45:05 +02:00
{
case KEY_UP :
return TUI : : UserInput : : UpKey ;
break ;
case KEY_DOWN :
return TUI : : UserInput : : DownKey ;
break ;
case 10 :
return TUI : : UserInput : : Enter ;
break ;
case 27 :
return TUI : : UserInput : : ESC ;
break ;
case ' a ' :
return TUI : : UserInput : : Abort ;
break ;
case ' d ' :
return TUI : : UserInput : : Delete ;
break ;
case ' s ' :
return TUI : : UserInput : : Shred ;
break ;
default :
return TUI : : UserInput : : Undefined ;
break ;
}
return TUI : : UserInput : : Undefined ;
2020-08-04 22:35:29 +02:00
}
2020-08-07 11:38:00 +02:00
void TUI : : centerTitle ( WINDOW * pwin , const char * title )
{
2020-08-04 22:35:29 +02:00
int x , maxX , stringSize ;
getmaxyx ( pwin , maxX , maxX ) ;
stringSize = 4 + strlen ( title ) ;
x = ( maxX - stringSize ) / 2 ;
mvwaddch ( pwin , 0 , x , ACS_RTEE ) ;
waddch ( pwin , ' ' ) ;
waddstr ( pwin , title ) ;
waddch ( pwin , ' ' ) ;
waddch ( pwin , ACS_LTEE ) ;
}
2020-08-07 11:38:00 +02:00
WINDOW * TUI : : createOverViewWindow ( int iXSize , int iYSize )
{
2020-08-04 22:35:29 +02:00
WINDOW * newWindow ;
newWindow = newwin ( iYSize , iXSize , 2 , 2 ) ;
2020-08-07 11:38:00 +02:00
2020-08-04 22:35:29 +02:00
wbkgd ( newWindow , COLOR_PAIR ( COLOR_AREA_OVERVIEW ) ) ;
box ( newWindow , ACS_VLINE , ACS_HLINE ) ;
time_t rawtime ;
2020-08-06 11:41:38 +02:00
struct tm * timeinfo ;
char buffer [ 80 ] ;
2020-08-04 22:35:29 +02:00
2020-08-06 11:41:38 +02:00
time ( & rawtime ) ;
timeinfo = localtime ( & rawtime ) ;
2020-08-04 22:35:29 +02:00
2020-08-06 11:41:38 +02:00
strftime ( buffer , sizeof ( buffer ) , " %d-%m-%Y %H:%M:%S " , timeinfo ) ;
std : : string str ( buffer ) ;
2020-08-04 22:35:29 +02:00
centerTitle ( newWindow , str . c_str ( ) ) ;
2020-08-06 22:45:05 +02:00
keypad ( newWindow , TRUE ) ;
2020-08-04 22:35:29 +02:00
return newWindow ;
}
2020-08-07 12:07:29 +02:00
WINDOW * TUI : : createDetailViewWindow ( int iXSize , int iYSize , int iXStart , Drive drive )
2020-08-07 11:38:00 +02:00
{
2020-08-04 22:35:29 +02:00
WINDOW * newWindow ;
newWindow = newwin ( iYSize , iXSize , 2 , iXStart ) ;
2020-08-07 11:38:00 +02:00
2020-08-04 22:35:29 +02:00
wbkgd ( newWindow , COLOR_PAIR ( COLOR_AREA_OVERVIEW ) ) ;
box ( newWindow , ACS_VLINE , ACS_HLINE ) ;
2020-08-07 12:07:29 +02:00
string title = " Selected Drive: " + drive . getModelName ( ) + " " + drive . sCapacityToText ( ) ;
centerTitle ( newWindow , title . c_str ( ) ) ;
string sPath = " Path: " + drive . getPath ( ) ;
string sModelFamlily = " ModelFamily: " + drive . getModelFamily ( ) ;
string sModelName = " ModelName: " + drive . getModelName ( ) ;
string sCapacity = " Capacity: " + drive . sCapacityToText ( ) ;
string sSerial = " Serial: " + drive . getSerial ( ) ;
string sPowerOnHours = " PowerOnHours: " + drive . sPowerOnHoursToText ( ) ;
string sPowerCycle = " PowerCycle: " + drive . sPowerCyclesToText ( ) ;
string sErrorCount = " ErrorCount: " + drive . sErrorCountToText ( ) ;
mvwaddstr ( newWindow , 2 , 3 , sPath . c_str ( ) ) ;
mvwaddstr ( newWindow , 3 , 3 , sModelFamlily . c_str ( ) ) ;
mvwaddstr ( newWindow , 4 , 3 , sModelName . c_str ( ) ) ;
mvwaddstr ( newWindow , 5 , 3 , sCapacity . c_str ( ) ) ;
mvwaddstr ( newWindow , 6 , 3 , sSerial . c_str ( ) ) ;
mvwaddstr ( newWindow , 7 , 3 , sPowerOnHours . c_str ( ) ) ;
mvwaddstr ( newWindow , 8 , 3 , sPowerCycle . c_str ( ) ) ;
mvwaddstr ( newWindow , 9 , 3 , sErrorCount . c_str ( ) ) ;
2020-08-04 22:35:29 +02:00
2020-08-06 22:45:05 +02:00
keypad ( newWindow , TRUE ) ;
2020-08-04 22:35:29 +02:00
return newWindow ;
}
2020-08-07 11:38:00 +02:00
WINDOW * TUI : : createEntryWindow ( int iXSize , int iYSize , int iXStart , int iYStart , string sModelFamily , string sModelName , string sCapacity , bool bSelected )
{
2020-08-04 22:35:29 +02:00
WINDOW * newWindow ;
newWindow = newwin ( iYSize , iXSize , iYStart , iXStart ) ;
2020-08-07 11:38:00 +02:00
if ( ! bSelected )
{
// entry is NOT selected
attron ( COLOR_PAIR ( COLOR_AREA_ENTRY ) ) ;
wbkgd ( newWindow , COLOR_PAIR ( COLOR_AREA_ENTRY ) ) ;
}
else
{
// entry IS selected
attron ( COLOR_PAIR ( COLOR_AREA_ENTRY ) ) ;
wbkgd ( newWindow , COLOR_PAIR ( COLOR_AREA_ENTRY_SELECTED ) ) ;
}
box ( newWindow , ACS_VLINE , ACS_HLINE ) ;
2020-08-06 22:45:05 +02:00
2020-08-06 11:41:38 +02:00
mvwaddstr ( newWindow , 1 , 1 , sModelFamily . c_str ( ) ) ;
mvwaddstr ( newWindow , 2 , 1 , sModelName . c_str ( ) ) ;
mvwaddstr ( newWindow , 3 , 1 , sCapacity . c_str ( ) ) ;
2020-08-04 22:35:29 +02:00
2020-08-06 22:45:05 +02:00
keypad ( newWindow , TRUE ) ;
2020-08-04 22:35:29 +02:00
return newWindow ;
}