From c2ab7c1c7f9e82c0d6f8428b8e66d2b25335e7a8 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sun, 9 Aug 2020 21:41:28 +0200 Subject: [PATCH] added states for tasks --- include/drive.h | 38 ++++++++++++++------ include/reHDD.h | 8 ++++- include/tui.h | 21 +++++++---- src/TUI/tui.cpp | 60 +++++++++++++++++++++++++++---- src/drive.cpp | 13 +++++++ src/reHDD.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 203 insertions(+), 32 deletions(-) diff --git a/include/drive.h b/include/drive.h index f1377c9..b7b1423 100644 --- a/include/drive.h +++ b/include/drive.h @@ -12,6 +12,29 @@ class Drive { + +public: + enum TaskState {NONE, + SHRED_SELECTED, + SHRED_ACTIVE, + SHRED_FINISHED, + DELETE_SELECTED, + DELETE_ACTIVE, + DELETE_FINISHED + } state; + +private: + string sPath; + string sModelFamily; + string sModelName; + string sSerial; + uint64_t u64Capacity = 0U; //in byte + uint32_t u32ErrorCount = 0U; + uint32_t u32PowerOnHours = 0U; //in hours + uint32_t u32PowerCycles = 0U; + + uint8_t u8TaskPercentage = 0U; //in percent for Shred (1 to 100) and Delete (1 OR 100) + protected: public: @@ -42,18 +65,11 @@ public: string sPowerOnHoursToText(); string sPowerCyclesToText(); + void setTaskPercentage(uint8_t u8TaskPercentage); + uint8_t getTaskPercentage(void); + + private: - string sPath; - string sModelFamily; - string sModelName; - string sSerial; - uint64_t u64Capacity = 0U; //in byte - uint32_t u32ErrorCount = 0U; - uint32_t u32PowerOnHours = 0U; //in hours - uint32_t u32PowerCycles = 0U; - uint32_t u32ShredPercentage = 0U; //in percent - - }; diff --git a/include/reHDD.h b/include/reHDD.h index 7ac6bfd..81aabc6 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -13,6 +13,8 @@ #define WORSE_HOURS 19200 //mark drive if at this limit or beyond #define WORSE_POWERUP 4000 //mark drive if at this limit or beyond +#define SELECTED_DRIVE vecDrives.at(i32SelectedEntry) + #include #include #include @@ -58,11 +60,15 @@ private: static void searchDrives(vector * pvecDrives); static void printDrives(vector * pvecDrives); static void filterIgnoredDrives(vector * pvecDrives); + static void filterNewDrives(vector * pvecOldDrives, vector * pvecNewDrives); static void addSMARTData(vector * pvecDrives); static void ThreadScannDevices(); static void ThreadUserInput(); static void handleArrowKey(TUI::UserInput userInput); - static void filterNewDrives(vector * pvecOldDrives, vector * pvecNewDrives); + static void handleEnter(); + static void handleESC(); + static void handleAbort(); + }; diff --git a/include/tui.h b/include/tui.h index 99a1e4c..5092ada 100644 --- a/include/tui.h +++ b/include/tui.h @@ -23,12 +23,21 @@ protected: public: enum UserInput { UpKey, DownKey, Abort, Shred, Delete, Enter, ESC, Undefined}; + struct MenuState + { + bool bAbort; + bool bShred; + bool bDelete; + bool bConfirmAbort; + bool bConfirmShred; + bool bConfirmDelete; + }; TUI(void); static void initTUI(); - void updateTUI(vector * pvecDrives, int32_t i32SelectedEntry); + void updateTUI(vector * pvecDrives, int32_t i32SelectedEntry, struct MenuState menustate); static enum UserInput readUserInput(); @@ -37,17 +46,17 @@ private: static string sRamUsage; static string sLocalTime; - WINDOW *detailview; - WINDOW *overview; - WINDOW *systemview; - + WINDOW* detailview; + WINDOW* overview; + WINDOW* systemview; + WINDOW* menuview; static void centerTitle(WINDOW *pwin, const char * title); static WINDOW *createOverViewWindow( int iXSize, int iYSize); static WINDOW *createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive drive); static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart,string sModelFamily, string sModelName, string sCapacity, bool bSelected); static WINDOW *createSystemStats(int iXSize, int iYSize, int iYStart); - + static WINDOW *createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate); }; #endif // TUI_H_ \ No newline at end of file diff --git a/src/TUI/tui.cpp b/src/TUI/tui.cpp index 2db6682..0d8d52d 100644 --- a/src/TUI/tui.cpp +++ b/src/TUI/tui.cpp @@ -49,10 +49,9 @@ void TUI::initTUI() init_pair(COLOR_AREA_DETAIL, COLOR_BLACK, COLOR_WHITE); mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 "); - } -void TUI::updateTUI(vector * pvecDrives, int32_t i32SelectedEntry) +void TUI::updateTUI(vector * pvecDrives, int32_t i32SelectedEntry, struct MenuState menustate) { int stdscrX, stdscrY; getmaxyx(stdscr, stdscrY, stdscrX); @@ -68,6 +67,9 @@ void TUI::updateTUI(vector * pvecDrives, int32_t i32SelectedEntry) systemview=createSystemStats((int)(stdscrX/3), 10, (stdscrY-11)); wrefresh(systemview); + menuview=createMenuView(((stdscrX)-(int)(stdscrX/3)-7), 10, (int)(stdscrX/3)+5,(stdscrY-11), menustate); + wrefresh(menuview); + vector ::iterator it; for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) { @@ -80,7 +82,7 @@ void TUI::updateTUI(vector * pvecDrives, int32_t i32SelectedEntry) 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()))); + detailview=createDetailViewWindow(((stdscrX)-(int)(stdscrX/3)-7), (stdscrY-15), (int)(stdscrX/3)+5, pvecDrives->at((it - pvecDrives->begin()))); wrefresh(detailview); } @@ -209,7 +211,7 @@ WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive { mvwaddstr(newWindow,u16Line++, 3, sErrorCount.c_str()); } - + keypad(newWindow, TRUE); return newWindow; } @@ -243,9 +245,10 @@ WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, return newWindow; } -WINDOW* TUI::createSystemStats(int iXSize, int iYSize, int iYStart){ +WINDOW* TUI::createSystemStats(int iXSize, int iYSize, int iYStart) +{ -WINDOW *newWindow; + WINDOW *newWindow; newWindow = newwin(iYSize, iXSize, iYStart, 2); wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW)); @@ -262,7 +265,50 @@ WINDOW *newWindow; string time(buffer); mvwaddstr(newWindow,2, 2, time.c_str()); - + + keypad(newWindow, TRUE); + return newWindow; +} + +WINDOW* TUI::createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate) +{ + + WINDOW *newWindow; + newWindow = newwin(iYSize, iXSize, iYStart, iXStart); + + wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW)); + box(newWindow, ACS_VLINE, ACS_HLINE); + + centerTitle(newWindow, "Controls"); + + + uint16_t u16Line = 2; + + if(menustate.bAbort) + { + mvwaddstr(newWindow,u16Line++, 3, "Press A for Abort"); + } + if(menustate.bShred) + { + mvwaddstr(newWindow,u16Line++, 3, "Press S for Shred"); + } + if(menustate.bDelete) + { + mvwaddstr(newWindow,u16Line++, 3, "Press D for Delete"); + } + if (menustate.bConfirmAbort) + { + mvwaddstr(newWindow,u16Line++, 3, "Press Enter to confirm Abort or ESC to return"); + } + if (menustate.bConfirmShred) + { + mvwaddstr(newWindow,u16Line++, 3, "Press Enter to confirm Shred or ESC to return"); + } + if (menustate.bConfirmDelete) + { + mvwaddstr(newWindow,u16Line++, 3, "Press Enter to confirm Delete or ESC to return"); + } + keypad(newWindow, TRUE); return newWindow; } diff --git a/src/drive.cpp b/src/drive.cpp index 1b7b5c3..e078325 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -86,6 +86,19 @@ string Drive::sPowerCyclesToText() return to_string(getPowerCycles()); } +void Drive::setTaskPercentage(uint8_t u8TaskPercentage) +{ + if((u8TaskPercentage >= 0) && (u8TaskPercentage <= 100)) + { + this->u8TaskPercentage = u8TaskPercentage; + } +} +uint8_t Drive::getTaskPercentage(void) +{ + return this->u8TaskPercentage; +} + + /** * \brief set S.M.A.R.T. values in model * \param string modelFamily diff --git a/src/reHDD.cpp b/src/reHDD.cpp index 551f256..292183e 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -26,6 +26,8 @@ static int32_t i32SelectedEntry; static fd_set selectSet; +static struct TUI::MenuState menustate; + /** * \brief app constructor * \param void @@ -60,7 +62,6 @@ void reHDD::app_logic(void) while(1) { - select(FD_SETSIZE, &selectSet, NULL, NULL, NULL); if( FD_ISSET(fdSearchDrives[0], &selectSet)) @@ -74,9 +75,7 @@ void reHDD::app_logic(void) //printDrives(&vecDrives); //TODO update UI - ui->updateTUI(&vecDrives, i32SelectedEntry); - - + ui->updateTUI(&vecDrives, i32SelectedEntry, menustate); } else if (FD_ISSET(fdWhipe[0], &selectSet)) { @@ -115,27 +114,45 @@ void reHDD::ThreadUserInput() case TUI::UserInput::DownKey: //cout << "Down" << endl; handleArrowKey(TUI::UserInput::DownKey); - ui->updateTUI(&vecDrives, i32SelectedEntry); + ui->updateTUI(&vecDrives, i32SelectedEntry, menustate); break; case TUI::UserInput::UpKey: //cout << "Up" << endl; handleArrowKey(TUI::UserInput::UpKey); - ui->updateTUI(&vecDrives, i32SelectedEntry); + ui->updateTUI(&vecDrives, i32SelectedEntry, menustate); break; case TUI::UserInput::Undefined: //cout << "Undefined" << endl; break; case TUI::UserInput::Abort: //cout << "Abort" << endl; + + handleAbort(); + break; case TUI::UserInput::Delete: //cout << "Delete" << endl; + + if(SELECTED_DRIVE.state == Drive::NONE) + { + + SELECTED_DRIVE.state = Drive::DELETE_SELECTED; + } + break; case TUI::UserInput::Shred: //cout << "Shred" << endl; + + if(SELECTED_DRIVE.state == Drive::NONE) + { + + SELECTED_DRIVE.state = Drive::SHRED_SELECTED; + } + break; case TUI::UserInput::Enter: //cout << "Enter" << endl; + handleEnter(); break; case TUI::UserInput::ESC: //cout << "ESC" << endl; @@ -161,13 +178,15 @@ void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecN { bOldDriveIsOffline = false; // cout << "already online drive found: " << itOld->getPath() << endl; + itNew->state = itOld->state; + itNew->setTaskPercentage(itOld->getTaskPercentage()); } } if(bOldDriveIsOffline == true) { //cout << "offline drive found: " << itOld->getPath() << endl; - //TODO kill wipe thread + //TODO kill task thread if running } } @@ -356,4 +375,66 @@ void reHDD::handleArrowKey(TUI::UserInput userInput) i32SelectedEntry = 0; break; } + + + if(SELECTED_DRIVE.state == Drive::TaskState::SHRED_ACTIVE || SELECTED_DRIVE.state == Drive::TaskState::DELETE_ACTIVE) + { + //task for drive is running --> donĀ“t show more task options + menustate.bAbort = true; //activate abort + menustate.bConfirmAbort = false; + menustate.bDelete = false; + menustate.bShred = false; + menustate.bConfirmDelete = false; + menustate.bConfirmShred = false; + + } + else + { + //no task for drive is running --> show more task options + menustate.bAbort = false; //deactivate abort + menustate.bConfirmAbort = false; + menustate.bDelete = true; + menustate.bShred = true; + menustate.bConfirmDelete = false; + menustate.bConfirmShred = false; + + } +} + +void reHDD::handleEnter() +{ + if(SELECTED_DRIVE.state == Drive::TaskState::SHRED_SELECTED) + { + SELECTED_DRIVE.state = Drive::TaskState::SHRED_ACTIVE; + //TODO start shredding + } + + if(SELECTED_DRIVE.state == Drive::TaskState::DELETE_SELECTED) + { + SELECTED_DRIVE.state = Drive::TaskState::DELETE_ACTIVE; + //TODO start deleting + } +} + + +void reHDD::handleESC() +{ + if(SELECTED_DRIVE.state == Drive::TaskState::SHRED_SELECTED) + { + SELECTED_DRIVE.state = Drive::TaskState::NONE; + } + + if(SELECTED_DRIVE.state == Drive::TaskState::DELETE_SELECTED) + { + SELECTED_DRIVE.state = Drive::TaskState::NONE; + } +} + +void reHDD::handleAbort() +{ + if(SELECTED_DRIVE.state == Drive::SHRED_ACTIVE || SELECTED_DRIVE.state == Drive::DELETE_ACTIVE ) + { + // TODO cancle shred or delete + SELECTED_DRIVE.state = Drive::NONE; + } } \ No newline at end of file