From cf4e1960c2faedb8ed5a2c078bbcac2d04dacc35 Mon Sep 17 00:00:00 2001 From: JuanJakobo Date: Thu, 8 Sep 2022 20:53:45 +0200 Subject: [PATCH] Refactor FileBrowser to use Filesystem and static --- src/api/fileBrowser.cpp | 59 ++++++++++++------------------ src/api/fileBrowser.h | 13 +------ src/api/fileModel.h | 8 ++-- src/api/webDAV.cpp | 9 +++-- src/handler/eventHandler.cpp | 71 +++++++++++------------------------- 5 files changed, 57 insertions(+), 103 deletions(-) diff --git a/src/api/fileBrowser.cpp b/src/api/fileBrowser.cpp index b78ed6d..1aa0245 100644 --- a/src/api/fileBrowser.cpp +++ b/src/api/fileBrowser.cpp @@ -11,21 +11,15 @@ #include "inkview.h" #include +#include using std::string; using std::vector; +namespace fs = std::experimental::filesystem; -FileBrowser::FileBrowser(bool includeFiles) : _includeFiles(includeFiles) +std::vector FileBrowser::getFileStructure(const std::string &path, const bool includeFiles, const bool includeHeader) { -} - -std::vector FileBrowser::getFileStructure(const std::string &path) -{ - //get local files, https://stackoverflow.com/questions/306533/how-do-i-get-a-list-of-files-in-a-directory-in-c - DIR *dir; - class dirent *ent; - class stat st; string localPath = path; std::vector items; @@ -33,48 +27,41 @@ std::vector FileBrowser::getFileStructure(const std::string &path) localPath = localPath + '/'; FileItem temp; - temp.path = localPath.substr(0,localPath.find_last_of('/')); - temp.path = temp.path.substr(0,temp.path.find_last_of('/')); - if (temp.path.empty()) - temp.path = "/"; - temp.name = ".."; - temp.type = Type::FFOLDER; - items.push_back(temp); + if(includeHeader) + { + temp.path = localPath.substr(0,localPath.find_last_of('/')); + temp.path = temp.path.substr(0,temp.path.find_last_of('/')); + if (temp.path.empty()) + temp.path = "/"; + temp.name = ".."; + temp.type = Type::FFOLDER; + items.push_back(temp); + } if (iv_access(localPath.c_str(), R_OK) == 0) { - dir = opendir(localPath.c_str()); - while ((ent = readdir(dir)) != NULL) + for (const auto &entry : fs::directory_iterator(localPath)) { - const string fileName = ent->d_name; + //temp.size = fs::file_size(entry); + auto time = std::chrono::system_clock::to_time_t(fs::last_write_time(entry)); + temp.lastEditDate = *gmtime(&time); - if (fileName[0] == '.') - continue; - - const string fullFileName = localPath + fileName; - - if (stat(fullFileName.c_str(), &st) == -1) - continue; - - if ((st.st_mode & S_IFDIR) != 0) + if(is_directory(entry)) { - FileItem temp; - temp.path = fullFileName + '/'; - temp.name = fullFileName.substr(fullFileName.find_last_of("/") + 1, fullFileName.length()); + temp.path = entry.path(); + temp.name = temp.path.substr(temp.path.find_last_of('/') + 1, temp.path.length()); temp.type = Type::FFOLDER; items.push_back(temp); } - else if (_includeFiles) + else if (includeFiles) { - FileItem temp; - temp.path = fullFileName; - temp.name = fullFileName.substr(fullFileName.find_last_of("/") + 1, fullFileName.length()); + temp.path = entry.path(); + temp.name = temp.path.substr(temp.path.find_last_of('/') + 1, temp.path.length()); temp.type = Type::FFILE; items.push_back(temp); } } - closedir(dir); } return items; } diff --git a/src/api/fileBrowser.h b/src/api/fileBrowser.h index f9ba5f0..17bc954 100644 --- a/src/api/fileBrowser.h +++ b/src/api/fileBrowser.h @@ -18,19 +18,10 @@ class FileBrowser { public: - /** - * Creates a new FileBrowser object - * - */ - FileBrowser(bool includeFiles); - - std::vector getFileStructure(const std::string &path); - - void setIncludeFiles(bool includeFiles) { _includeFiles = includeFiles;}; + static std::vector getFileStructure(const std::string &path, const bool includeFiles, const bool includeHeader); private: - bool _includeFiles; - + FileBrowser(){}; }; #endif diff --git a/src/api/fileModel.h b/src/api/fileModel.h index 194cd5a..033de2c 100644 --- a/src/api/fileModel.h +++ b/src/api/fileModel.h @@ -20,9 +20,11 @@ enum class Type }; struct FileItem : Entry{ - std::string name; - std::string path; - Type type; + std::string name; + std::string path; + Type type; + //long long int size; + tm lastEditDate; }; #endif diff --git a/src/api/webDAV.cpp b/src/api/webDAV.cpp index 8479412..4071919 100644 --- a/src/api/webDAV.cpp +++ b/src/api/webDAV.cpp @@ -24,6 +24,7 @@ using std::ofstream; using std::string; using std::vector; +namespace fs = std::experimental::filesystem; WebDAV::WebDAV() { @@ -82,11 +83,11 @@ void WebDAV::logout(bool deleteFiles) { if (deleteFiles) { - std::experimental::filesystem::remove_all(Util::accessConfig(Action::IReadString, "storageLocation",{}) + "/" + Util::accessConfig(Action::IReadString,"UUID",{}) + '/'); + fs::remove_all(Util::accessConfig(Action::IReadString, "storageLocation",{}) + "/" + Util::accessConfig(Action::IReadString,"UUID",{}) + '/'); } - std::experimental::filesystem::remove(CONFIG_PATH.c_str()); - std::experimental::filesystem::remove((CONFIG_PATH + ".back.").c_str()); - std::experimental::filesystem::remove(DB_PATH.c_str()); + fs::remove(CONFIG_PATH.c_str()); + fs::remove((CONFIG_PATH + ".back.").c_str()); + fs::remove(DB_PATH.c_str()); _url = ""; _password = ""; _username = ""; diff --git a/src/handler/eventHandler.cpp b/src/handler/eventHandler.cpp index 07d5669..833c53a 100644 --- a/src/handler/eventHandler.cpp +++ b/src/handler/eventHandler.cpp @@ -1,4 +1,4 @@ -//------------------------------------------------------------------ +//----------------------------------------------------------------- // eventHandler.cpp // // Author: JuanJakobo @@ -26,6 +26,8 @@ using std::string; using std::vector; +namespace fs = std::experimental::filesystem; + std::unique_ptr EventHandler::_eventHandlerStatic; EventHandler::EventHandler() @@ -122,7 +124,6 @@ void EventHandler::mainMenuHandler(const int index) { UpdateProgressbar(("Upgrading " + path).c_str(), 0); currentWebDAVItems = _webDAV.getDataStructure(path); - Log::writeInfoLog("syncing"); } else { @@ -291,11 +292,11 @@ void EventHandler::contextMenuHandler(const int index) { if (_webDAVView->getCurrentEntry().type == Itemtype::IFOLDER) { - std::experimental::filesystem::remove_all(_webDAVView->getCurrentEntry().localPath); + fs::remove_all(_webDAVView->getCurrentEntry().localPath); } else { - std::experimental::filesystem::remove(_webDAVView->getCurrentEntry().localPath); + fs::remove(_webDAVView->getCurrentEntry().localPath); } vector currentWebDAVItems = _sqllite.getItemsChildren(_currentPath); updateItems(currentWebDAVItems); @@ -383,8 +384,7 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) case 1: { auto path = "/mnt/ext1"; - FileBrowser fileBrowser = FileBrowser(false); - vector currentFolder = fileBrowser.getFileStructure(path); + vector currentFolder = FileBrowser::getFileStructure(path,false,true); _currentPath = path; _loginView.reset(); FillAreaRect(&_menu->getContentRect(), WHITE); @@ -411,10 +411,8 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) if (_fileView->getCurrentEntry().type == Type::FFOLDER) { - FileBrowser fileBrowser = FileBrowser(false); _currentPath = _fileView->getCurrentEntry().path; - vector currentFolder = fileBrowser.getFileStructure(_currentPath); - + vector currentFolder = FileBrowser::getFileStructure(_currentPath,false,true); _fileView.reset(new FileView(_menu->getContentRect(), currentFolder,1)); } } @@ -552,51 +550,25 @@ int EventHandler::keyHandler(const int type, const int par1, const int par2) return 1; } - -//TODO use Filebrowser -void EventHandler::getLocalFileStructure(vector &items) +void EventHandler::getLocalFileStructure(std::vector &tempItems) { - //get local files, https://stackoverflow.com/questions/306533/how-do-i-get-a-list-of-files-in-a-directory-in-c - DIR *dir; - class dirent *ent; - class stat st; - - string localPath = items.at(0).localPath + '/'; - if (localPath.back() != '/') - localPath = localPath + '/'; + string localPath = tempItems.at(0).localPath + '/'; if (iv_access(localPath.c_str(), W_OK) == 0) { - dir = opendir(localPath.c_str()); - while ((ent = readdir(dir)) != NULL) + vector currentFolder = FileBrowser::getFileStructure(localPath,true,false); + + for(const FileItem &local : currentFolder) { - const string fileName = ent->d_name; - - if (fileName[0] == '.') - continue; - - const string fullFileName = localPath + fileName; - - if (stat(fullFileName.c_str(), &st) == -1) - continue; - - - bool found = false; - for (unsigned int i = 1; i < items.size(); i++) - { - if (items.at(i).localPath.compare(fullFileName) == 0) - { - found = true; - break; - } - } - if (!found) + auto p = find_if(tempItems.begin()+1, tempItems.end(), [&] (const WebDAVItem &item) {return item.localPath.compare(local.path) == 0;}); + if (p == tempItems.end()) { WebDAVItem temp; - temp.localPath = fullFileName; + temp.localPath = local.path; temp.state = FileState::ILOCAL; - temp.title = fullFileName.substr(fullFileName.find_last_of("/") + 1, fullFileName.length()); - Util::decodeUrl(temp.title); - if ((st.st_mode & S_IFDIR) != 0) + temp.title = temp.localPath.substr(temp.localPath.find_last_of('/') + 1, temp.localPath.length()); + //Log::writeInfoLog(std::to_string(fs::file_size(entry))); + temp.lastEditDate = local.lastEditDate; + if(local.type == Type::FFOLDER) { //create new dir in cloud temp.type = Itemtype::IFOLDER; @@ -604,12 +576,13 @@ void EventHandler::getLocalFileStructure(vector &items) else { //put to cloud + temp.fileType = "File"; temp.type = Itemtype::IFILE; } - items.push_back(temp); + tempItems.push_back(temp); } + } - closedir(dir); } }