From 3822d3197c0204410fd253c5d9569c38523f8ffe Mon Sep 17 00:00:00 2001 From: JuanJakobo Date: Fri, 29 Jul 2022 17:34:22 +0200 Subject: [PATCH] Implement FilePicker in other classes --- src/api/webDAV.cpp | 16 ++-- src/api/webDAV.h | 2 - src/handler/eventHandler.cpp | 144 ++++++++++++++++++++++++++++------- src/handler/eventHandler.h | 3 +- src/handler/mainMenu.cpp | 8 +- src/handler/mainMenu.h | 4 +- 6 files changed, 136 insertions(+), 41 deletions(-) diff --git a/src/api/webDAV.cpp b/src/api/webDAV.cpp index 9112704..2a6099c 100644 --- a/src/api/webDAV.cpp +++ b/src/api/webDAV.cpp @@ -29,9 +29,6 @@ WebDAV::WebDAV() if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0) iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777); - if (iv_access(NEXTCLOUD_FILE_PATH.c_str(), W_OK) != 0) - iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(), 0777); - if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) { _username = Util::accessConfig(CONFIG_PATH,Action::IReadString,"username"); @@ -59,6 +56,7 @@ std::vector WebDAV::login(const string &Url, const string &Username, uuid = Username; } auto tempPath = NEXTCLOUD_ROOT_PATH + uuid + "/"; + Util::accessConfig(CONFIG_PATH, Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); std::vector tempItems = getDataStructure(tempPath); if (!tempItems.empty()) { @@ -82,8 +80,8 @@ void WebDAV::logout(bool deleteFiles) { if (deleteFiles) { - //string cmd = "rm -rf " + NEXTCLOUD_FILE_PATH + "/" + getUUID() + "/"; - //system(cmd.c_str()); + string cmd = "rm -rf " + Util::accessConfig(CONFIG_PATH, Action::IReadString, "storageLocation") + "/" + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/'; + system(cmd.c_str()); } remove(CONFIG_PATH.c_str()); remove((CONFIG_PATH + ".back.").c_str()); @@ -151,7 +149,13 @@ vector WebDAV::getDataStructure(const string &pathUrl) tempItem.path.erase(0,tempItem.path.find(NEXTCLOUD_START_PATH)); tempItem.title = tempItem.path; - tempItem.localPath = getLocalPath(tempItem.path); + //TODO make lambda? + tempItem.localPath = tempItem.path; + Util::decodeUrl(tempItem.localPath); + if (tempItem.localPath.find(NEXTCLOUD_ROOT_PATH) != string::npos) + tempItem.localPath = tempItem.localPath.substr(NEXTCLOUD_ROOT_PATH.length()); + tempItem.localPath = Util::accessConfig(CONFIG_PATH, Action::IReadString, "storageLocation") + "/" + tempItem.localPath; + if (tempItem.path.back() == '/') { diff --git a/src/api/webDAV.h b/src/api/webDAV.h index 29c53c8..76b62e2 100644 --- a/src/api/webDAV.h +++ b/src/api/webDAV.h @@ -15,8 +15,6 @@ #include #include -//TODO write into config -const std::string NEXTCLOUD_FILE_PATH = "/mnt/ext1/nextcloud"; const std::string NEXTCLOUD_ROOT_PATH = "/remote.php/dav/files/"; const std::string NEXTCLOUD_START_PATH = "/remote.php/"; const std::string CACERT_PATH = "/mnt/ext1/applications/cacert.pem"; diff --git a/src/handler/eventHandler.cpp b/src/handler/eventHandler.cpp index dc8a5f9..45ef7bb 100644 --- a/src/handler/eventHandler.cpp +++ b/src/handler/eventHandler.cpp @@ -15,6 +15,9 @@ #include "log.h" #include "webDAV.h" #include "webDAVModel.h" +#include "fileBrowser.h" +#include "fileView.h" +#include "fileModel.h" #include #include @@ -168,18 +171,44 @@ void EventHandler::mainMenuHandler(const int index) break; } _webDAVView.release(); - _loginView = std::make_unique(LoginView(_menu->getContentRect())); - FullUpdate(); + _loginView = std::unique_ptr(new LoginView(_menu->getContentRect())); + break; + } + //Select folder + case 103: + { + + if(_currentPath.back() != '/') + _currentPath = _currentPath + "/nextcloud"; + else + _currentPath = _currentPath + "nextcloud"; + + if(iv_mkdir(_currentPath.c_str(), 0777) != 0) + Message(ICON_ERROR, "Error", "The permissions are not sufficient.", 1000); + else + { + Util::accessConfig(CONFIG_PATH, Action::IWriteString, "storageLocation", _currentPath); + std::vector currentWebDAVItems = _webDAV.getDataStructure(NEXTCLOUD_ROOT_PATH + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/'); + if(currentWebDAVItems.empty()) + { + Message(ICON_ERROR, "Error", "Failed to get items. Please try again.", 1000); + } + else + { + updateItems(currentWebDAVItems); + drawWebDAVItems(currentWebDAVItems); + } + } break; } //Info - case 103: + case 104: { - Message(ICON_INFORMATION, "Information", "Version 0.98 \n For support please open a ticket at https://github.com/JuanJakobo/Pocketbook-Nextcloud-Client/issues", 1200); + Message(ICON_INFORMATION, "Info", "Version 0.98 \n For support please open a ticket at https://github.com/JuanJakobo/Pocketbook-Nextcloud-Client/issues", 1200); break; } //Exit - case 104: + case 105: CloseApp(); break; default: @@ -268,7 +297,7 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) { if (IsInRect(par1, par2, &_menu->getMenuButtonRect()) == 1) { - return _menu->createMenu((_webDAVView != nullptr), EventHandler::mainMenuHandlerStatic); + return _menu->createMenu((_fileView != nullptr), (_webDAVView != nullptr), EventHandler::mainMenuHandlerStatic); } else if (_webDAVView != nullptr) { @@ -305,18 +334,55 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) std::vector currentWebDAVItems = _webDAV.login(_loginView->getURL(), _loginView->getUsername(), _loginView->getPassword()); if(currentWebDAVItems.empty()) { + Message(ICON_ERROR, "Error", "Login failed.", 1000); HideHourglass(); - Log::writeErrorLog("login failed."); } else { - //TODO storagelocation picker - drawWebDAVItems(currentWebDAVItems); - _loginView.reset(); + int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "Do you want to choose your own storage path or use the default one. \n (/mnt/ext1/nextcloud/)", "Choose my own path", "Choose standard path", NULL); + auto path = "/mnt/ext1"; + switch (dialogResult) + { + case 1: + { + FileBrowser fileBrowser = FileBrowser(); + vector currentFolder = fileBrowser.getFileStructure(path); + _currentPath = path; + _loginView.reset(); + FillAreaRect(&_menu->getContentRect(), WHITE); + _fileView = std::unique_ptr(new FileView(_menu->getContentRect(), currentFolder,1)); + } + break; + default: + updateItems(currentWebDAVItems); + drawWebDAVItems(currentWebDAVItems); + break; + } } return 0; } } + else if(_fileView != nullptr) + { + if(_fileView->checkIfEntryClicked(par1, par2)) + { + _fileView->invertCurrentEntryColor(); + + if (_fileView->getCurrentEntry().type == Type::FFOLDER) + { + FileBrowser fileBrowser = FileBrowser(); + _currentPath = _fileView->getCurrentEntry().path; + vector currentFolder = fileBrowser.getFileStructure(_currentPath); + + //TODO use other method + _fileView.reset(); + _fileView = std::unique_ptr(new FileView(_menu->getContentRect(), currentFolder,1)); + } + } + + return 0; + } + } return 1; } @@ -377,31 +443,53 @@ void EventHandler::openFolder() int EventHandler::keyHandler(const int type, const int par1, const int par2) { - if (type == EVT_KEYPRESS) + if(_webDAVView != nullptr) { - //menu button - if (par1 == 23) + if (type == EVT_KEYPRESS) { - _webDAVView->firstPage(); - } - else if (_webDAVView != nullptr) - { - //left button - if (par1 == 24) + switch(par1) { - _webDAVView->prevPage(); - } - //right button - else if (par1 == 25) - { - _webDAVView->nextPage(); + //menu button + case 23: + _webDAVView->firstPage(); + break; + //left button + case 24: + _webDAVView->prevPage(); + break; + //right button + case 25: + _webDAVView->nextPage(); + break; + default: + return 1; } + return 0; } - else + } + else if(_fileView != nullptr) + { + if (type == EVT_KEYPRESS) { - return 1; + switch(par1) + { + //menu button + case 23: + _fileView->firstPage(); + break; + //left button + case 24: + _fileView->prevPage(); + break; + //right button + case 25: + _fileView->nextPage(); + break; + default: + return 1; + } + return 0; } - return 0; } return 1; diff --git a/src/handler/eventHandler.h b/src/handler/eventHandler.h index 2485c5d..45f809a 100644 --- a/src/handler/eventHandler.h +++ b/src/handler/eventHandler.h @@ -14,13 +14,13 @@ #include "webDAV.h" #include "webDAVView.h" #include "loginView.h" +#include "fileView.h" #include "sqliteConnector.h" #include const std::string CONFIG_FOLDER = "/mnt/ext1/system/config/nextcloud"; const std::string CONFIG_PATH = CONFIG_FOLDER + "/nextcloud.cfg"; -//TODO use folder of nextcloud conifg temp const std::string DB_PATH = CONFIG_FOLDER + "/data.db"; class EventHandler @@ -45,6 +45,7 @@ private: static std::unique_ptr _eventHandlerStatic; std::unique_ptr _webDAVView; std::unique_ptr _loginView; + std::unique_ptr _fileView; std::unique_ptr _contextMenu; std::unique_ptr _menu; diff --git a/src/handler/mainMenu.cpp b/src/handler/mainMenu.cpp index 8e7c279..9c1c7eb 100644 --- a/src/handler/mainMenu.cpp +++ b/src/handler/mainMenu.cpp @@ -43,6 +43,7 @@ MainMenu::~MainMenu() free(_logout); free(_info); free(_exit); + free(_chooseFolder); } void MainMenu::panelHandlerStatic() @@ -51,7 +52,7 @@ void MainMenu::panelHandlerStatic() SetHardTimer("PANELUPDATE", panelHandlerStatic, 110000); } -int MainMenu::createMenu(bool loggedIn, iv_menuhandler handler) +int MainMenu::createMenu(bool filePicker, bool loggedIn, iv_menuhandler handler) { imenu mainMenu[] = { @@ -59,9 +60,10 @@ int MainMenu::createMenu(bool loggedIn, iv_menuhandler handler) //show logged in {loggedIn ? (short)ITEM_ACTIVE : (short)ITEM_HIDDEN, 101, _syncFolder, NULL}, {loggedIn ? (short)ITEM_ACTIVE : (short)ITEM_HIDDEN, 102, _logout, NULL}, + {filePicker ? (short)ITEM_ACTIVE : (short)ITEM_HIDDEN, 103, _chooseFolder, NULL}, //show always - {ITEM_ACTIVE, 103, _info, NULL}, - {ITEM_ACTIVE, 104, _exit, NULL}, + {ITEM_ACTIVE, 104, _info, NULL}, + {ITEM_ACTIVE, 105, _exit, NULL}, {0, 0, NULL, NULL}}; OpenMenu(mainMenu, 0, _panelMenuBeginX, _panelMenuBeginY, handler); diff --git a/src/handler/mainMenu.h b/src/handler/mainMenu.h index b5ba01f..16896b2 100644 --- a/src/handler/mainMenu.h +++ b/src/handler/mainMenu.h @@ -31,11 +31,12 @@ public: /** * Shows the menu on the screen, lets the user choose menu options and then redirects the handler to the caller * + * @param filePicker true if the filepicker is shown * @param loogedIn the status if the user is logged in * @param handler handles the clicks on the menu * @return int returns if the event was handled */ - int createMenu(bool loggedIn, iv_menuhandler handler); + int createMenu(bool filePicker, bool loggedIn, iv_menuhandler handler); private: ifont *_menuFont; @@ -52,6 +53,7 @@ private: char *_menu = strdup("Menu"); char *_syncFolder = strdup("Actualize folder"); char *_logout = strdup("Logout"); + char *_chooseFolder = strdup("Create here"); char *_info = strdup("Info"); char *_exit = strdup("Close App");