269 lines
7.7 KiB
C++
269 lines
7.7 KiB
C++
/**
|
|
* @file tui.cpp
|
|
* @brief display user interface
|
|
* @author hendrik schutter
|
|
* @date 03.08.2020
|
|
*/
|
|
|
|
#include "../../include/reHDD.h"
|
|
|
|
|
|
TUI::TUI(void)
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* \brief wipe drive with shred
|
|
* \param pointer of Drive instance
|
|
* \return void
|
|
*/
|
|
|
|
void TUI::initTUI()
|
|
{
|
|
initscr();
|
|
raw();
|
|
keypad(stdscr,TRUE);
|
|
if(has_colors() == TRUE)
|
|
{
|
|
start_color();
|
|
}
|
|
else
|
|
{
|
|
printf("Your terminal does not support color\n");
|
|
exit(1);
|
|
}
|
|
clear();
|
|
curs_set(0);
|
|
noecho();
|
|
cbreak();
|
|
init_pair(COLOR_AREA_STDSCR,COLOR_WHITE, COLOR_BLUE);
|
|
wbkgd(stdscr, COLOR_PAIR(COLOR_AREA_STDSCR));
|
|
|
|
init_pair(COLOR_AREA_ENTRY, COLOR_BLACK, COLOR_WHITE);
|
|
init_pair(COLOR_AREA_ENTRY_SELECTED, COLOR_BLACK, COLOR_RED);
|
|
init_pair(COLOR_AREA_OVERVIEW, COLOR_BLACK, COLOR_WHITE);
|
|
init_pair(COLOR_AREA_DETAIL, COLOR_BLACK, COLOR_WHITE);
|
|
|
|
mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 ");
|
|
|
|
}
|
|
|
|
void TUI::updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry)
|
|
{
|
|
int stdscrX, stdscrY;
|
|
getmaxyx(stdscr, stdscrY, stdscrX);
|
|
|
|
init_pair(COLOR_AREA_STDSCR,COLOR_WHITE, COLOR_BLUE);
|
|
wbkgd(stdscr, COLOR_PAIR(COLOR_AREA_STDSCR));
|
|
|
|
refresh();
|
|
|
|
overview=createOverViewWindow((int)(stdscrX/3), (stdscrY-15));
|
|
wrefresh(overview);
|
|
|
|
systemview=createSystemStats((int)(stdscrX/3), 10, (stdscrY-11));
|
|
wrefresh(systemview);
|
|
|
|
vector <Drive>::iterator it;
|
|
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
|
|
{
|
|
string sModelFamily = it->getModelFamily();
|
|
string sModelName = it->getModelName();
|
|
string sCapacity = it->sCapacityToText();
|
|
|
|
bool bSelectedEntry = false;
|
|
|
|
if(i32SelectedEntry == (it - pvecDrives->begin()))
|
|
{
|
|
bSelectedEntry = true;
|
|
detailview=createDetailViewWindow(((stdscrX)-(int)(stdscrX/3)-7), (stdscrY-3), (int)(stdscrX/3)+5, pvecDrives->at((it - pvecDrives->begin())));
|
|
wrefresh(detailview);
|
|
}
|
|
|
|
WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity, bSelectedEntry);
|
|
wrefresh(tmp);
|
|
}
|
|
}
|
|
|
|
enum TUI::UserInput TUI::readUserInput()
|
|
{
|
|
int ch = wgetch(stdscr);
|
|
switch(ch)
|
|
{
|
|
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;
|
|
}
|
|
|
|
void TUI::centerTitle(WINDOW *pwin, const char * title)
|
|
{
|
|
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);
|
|
}
|
|
|
|
WINDOW* TUI::createOverViewWindow( int iXSize, int iYSize)
|
|
{
|
|
WINDOW *newWindow;
|
|
newWindow = newwin(iYSize, iXSize, 2, 2);
|
|
|
|
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW));
|
|
box(newWindow, ACS_VLINE, ACS_HLINE);
|
|
|
|
centerTitle(newWindow, "Detected Drives");
|
|
|
|
keypad(newWindow, TRUE);
|
|
return newWindow;
|
|
}
|
|
|
|
WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive drive)
|
|
{
|
|
WINDOW *newWindow;
|
|
newWindow = newwin(iYSize, iXSize, 2, iXStart);
|
|
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_DETAIL));
|
|
box(newWindow, ACS_VLINE, ACS_HLINE);
|
|
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();
|
|
|
|
uint16_t u16Line = 2;
|
|
|
|
mvwaddstr(newWindow,u16Line++, 3, sPath.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, sModelFamlily.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, sModelName.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, sCapacity.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, sSerial.c_str());
|
|
|
|
attroff(COLOR_PAIR(COLOR_AREA_DETAIL));
|
|
|
|
if(drive.getPowerOnHours() >= WORSE_HOURS)
|
|
{
|
|
mvwaddstr(newWindow,u16Line++, 3, "------------> WARNING: OPERATING HOURS <-----------");
|
|
mvwaddstr(newWindow,u16Line++, 3, sPowerOnHours.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, "---------------------------------------------------");
|
|
}
|
|
else
|
|
{
|
|
mvwaddstr(newWindow,u16Line++, 3, sPowerOnHours.c_str());
|
|
}
|
|
|
|
if(drive.getPowerCycles() >= WORSE_POWERUP)
|
|
{
|
|
mvwaddstr(newWindow,u16Line++, 3, "------------> WARNING: POWER-ON <------------------");
|
|
mvwaddstr(newWindow,u16Line++, 3, sPowerCycle.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, "---------------------------------------------------");
|
|
}
|
|
else
|
|
{
|
|
mvwaddstr(newWindow,u16Line++, 3, sPowerCycle.c_str());
|
|
}
|
|
|
|
if(drive.getErrorCount() > 0)
|
|
{
|
|
mvwaddstr(newWindow,u16Line++, 3, "------------> WARNING: S.M.A.R.T ERROR <-----------");
|
|
mvwaddstr(newWindow,u16Line++, 3, sErrorCount.c_str());
|
|
mvwaddstr(newWindow,u16Line++, 3, "---------------------------------------------------");
|
|
}
|
|
else
|
|
{
|
|
mvwaddstr(newWindow,u16Line++, 3, sErrorCount.c_str());
|
|
}
|
|
|
|
keypad(newWindow, TRUE);
|
|
return newWindow;
|
|
}
|
|
|
|
WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, bool bSelected)
|
|
{
|
|
WINDOW *newWindow;
|
|
newWindow = newwin(iYSize, iXSize, iYStart, iXStart);
|
|
|
|
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);
|
|
|
|
mvwaddstr(newWindow,1, 1, sModelFamily.c_str());
|
|
mvwaddstr(newWindow,2, 1, sModelName.c_str());
|
|
mvwaddstr(newWindow,3, 1, sCapacity.c_str());
|
|
|
|
keypad(newWindow, TRUE);
|
|
|
|
return newWindow;
|
|
}
|
|
|
|
WINDOW* TUI::createSystemStats(int iXSize, int iYSize, int iYStart){
|
|
|
|
WINDOW *newWindow;
|
|
newWindow = newwin(iYSize, iXSize, iYStart, 2);
|
|
|
|
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW));
|
|
box(newWindow, ACS_VLINE, ACS_HLINE);
|
|
|
|
centerTitle(newWindow, "System");
|
|
|
|
time_t rawtime;
|
|
struct tm * timeinfo;
|
|
char buffer[80];
|
|
time (&rawtime);
|
|
timeinfo = localtime(&rawtime);
|
|
strftime(buffer,sizeof(buffer),"Date: %d-%m-%Y Time: %H:%M",timeinfo);
|
|
string time(buffer);
|
|
|
|
mvwaddstr(newWindow,2, 2, time.c_str());
|
|
|
|
keypad(newWindow, TRUE);
|
|
return newWindow;
|
|
}
|