Refactor FileBrowser to use Filesystem and static

master
JuanJakobo 2022-09-08 20:53:45 +02:00
parent e1b4f709fe
commit cf4e1960c2
5 changed files with 57 additions and 103 deletions

View File

@ -11,21 +11,15 @@
#include "inkview.h" #include "inkview.h"
#include <string> #include <string>
#include <experimental/filesystem>
using std::string; using std::string;
using std::vector; using std::vector;
namespace fs = std::experimental::filesystem;
FileBrowser::FileBrowser(bool includeFiles) : _includeFiles(includeFiles) std::vector<FileItem> FileBrowser::getFileStructure(const std::string &path, const bool includeFiles, const bool includeHeader)
{ {
}
std::vector<FileItem> 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; string localPath = path;
std::vector<FileItem> items; std::vector<FileItem> items;
@ -33,6 +27,8 @@ std::vector<FileItem> FileBrowser::getFileStructure(const std::string &path)
localPath = localPath + '/'; localPath = localPath + '/';
FileItem temp; FileItem temp;
if(includeHeader)
{
temp.path = localPath.substr(0,localPath.find_last_of('/')); temp.path = localPath.substr(0,localPath.find_last_of('/'));
temp.path = temp.path.substr(0,temp.path.find_last_of('/')); temp.path = temp.path.substr(0,temp.path.find_last_of('/'));
if (temp.path.empty()) if (temp.path.empty())
@ -40,41 +36,32 @@ std::vector<FileItem> FileBrowser::getFileStructure(const std::string &path)
temp.name = ".."; temp.name = "..";
temp.type = Type::FFOLDER; temp.type = Type::FFOLDER;
items.push_back(temp); items.push_back(temp);
}
if (iv_access(localPath.c_str(), R_OK) == 0) if (iv_access(localPath.c_str(), R_OK) == 0)
{ {
dir = opendir(localPath.c_str()); for (const auto &entry : fs::directory_iterator(localPath))
while ((ent = readdir(dir)) != NULL)
{ {
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] == '.') if(is_directory(entry))
continue;
const string fullFileName = localPath + fileName;
if (stat(fullFileName.c_str(), &st) == -1)
continue;
if ((st.st_mode & S_IFDIR) != 0)
{ {
FileItem temp; temp.path = entry.path();
temp.path = fullFileName + '/'; temp.name = temp.path.substr(temp.path.find_last_of('/') + 1, temp.path.length());
temp.name = fullFileName.substr(fullFileName.find_last_of("/") + 1, fullFileName.length());
temp.type = Type::FFOLDER; temp.type = Type::FFOLDER;
items.push_back(temp); items.push_back(temp);
} }
else if (_includeFiles) else if (includeFiles)
{ {
FileItem temp; temp.path = entry.path();
temp.path = fullFileName; temp.name = temp.path.substr(temp.path.find_last_of('/') + 1, temp.path.length());
temp.name = fullFileName.substr(fullFileName.find_last_of("/") + 1, fullFileName.length());
temp.type = Type::FFILE; temp.type = Type::FFILE;
items.push_back(temp); items.push_back(temp);
} }
} }
closedir(dir);
} }
return items; return items;
} }

View File

@ -18,19 +18,10 @@
class FileBrowser class FileBrowser
{ {
public: public:
/** static std::vector<FileItem> getFileStructure(const std::string &path, const bool includeFiles, const bool includeHeader);
* Creates a new FileBrowser object
*
*/
FileBrowser(bool includeFiles);
std::vector<FileItem> getFileStructure(const std::string &path);
void setIncludeFiles(bool includeFiles) { _includeFiles = includeFiles;};
private: private:
bool _includeFiles; FileBrowser(){};
}; };
#endif #endif

View File

@ -23,6 +23,8 @@ struct FileItem : Entry{
std::string name; std::string name;
std::string path; std::string path;
Type type; Type type;
//long long int size;
tm lastEditDate;
}; };
#endif #endif

View File

@ -24,6 +24,7 @@ using std::ofstream;
using std::string; using std::string;
using std::vector; using std::vector;
namespace fs = std::experimental::filesystem;
WebDAV::WebDAV() WebDAV::WebDAV()
{ {
@ -82,11 +83,11 @@ void WebDAV::logout(bool deleteFiles)
{ {
if (deleteFiles) if (deleteFiles)
{ {
std::experimental::filesystem::remove_all(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + "/" + Util::accessConfig<string>(Action::IReadString,"UUID",{}) + '/'); fs::remove_all(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + "/" + Util::accessConfig<string>(Action::IReadString,"UUID",{}) + '/');
} }
std::experimental::filesystem::remove(CONFIG_PATH.c_str()); fs::remove(CONFIG_PATH.c_str());
std::experimental::filesystem::remove((CONFIG_PATH + ".back.").c_str()); fs::remove((CONFIG_PATH + ".back.").c_str());
std::experimental::filesystem::remove(DB_PATH.c_str()); fs::remove(DB_PATH.c_str());
_url = ""; _url = "";
_password = ""; _password = "";
_username = ""; _username = "";

View File

@ -1,4 +1,4 @@
//------------------------------------------------------------------ //-----------------------------------------------------------------
// eventHandler.cpp // eventHandler.cpp
// //
// Author: JuanJakobo // Author: JuanJakobo
@ -26,6 +26,8 @@
using std::string; using std::string;
using std::vector; using std::vector;
namespace fs = std::experimental::filesystem;
std::unique_ptr<EventHandler> EventHandler::_eventHandlerStatic; std::unique_ptr<EventHandler> EventHandler::_eventHandlerStatic;
EventHandler::EventHandler() EventHandler::EventHandler()
@ -122,7 +124,6 @@ void EventHandler::mainMenuHandler(const int index)
{ {
UpdateProgressbar(("Upgrading " + path).c_str(), 0); UpdateProgressbar(("Upgrading " + path).c_str(), 0);
currentWebDAVItems = _webDAV.getDataStructure(path); currentWebDAVItems = _webDAV.getDataStructure(path);
Log::writeInfoLog("syncing");
} }
else else
{ {
@ -291,11 +292,11 @@ void EventHandler::contextMenuHandler(const int index)
{ {
if (_webDAVView->getCurrentEntry().type == Itemtype::IFOLDER) if (_webDAVView->getCurrentEntry().type == Itemtype::IFOLDER)
{ {
std::experimental::filesystem::remove_all(_webDAVView->getCurrentEntry().localPath); fs::remove_all(_webDAVView->getCurrentEntry().localPath);
} }
else else
{ {
std::experimental::filesystem::remove(_webDAVView->getCurrentEntry().localPath); fs::remove(_webDAVView->getCurrentEntry().localPath);
} }
vector<WebDAVItem> currentWebDAVItems = _sqllite.getItemsChildren(_currentPath); vector<WebDAVItem> currentWebDAVItems = _sqllite.getItemsChildren(_currentPath);
updateItems(currentWebDAVItems); updateItems(currentWebDAVItems);
@ -383,8 +384,7 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
case 1: case 1:
{ {
auto path = "/mnt/ext1"; auto path = "/mnt/ext1";
FileBrowser fileBrowser = FileBrowser(false); vector<FileItem> currentFolder = FileBrowser::getFileStructure(path,false,true);
vector<FileItem> currentFolder = fileBrowser.getFileStructure(path);
_currentPath = path; _currentPath = path;
_loginView.reset(); _loginView.reset();
FillAreaRect(&_menu->getContentRect(), WHITE); 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) if (_fileView->getCurrentEntry().type == Type::FFOLDER)
{ {
FileBrowser fileBrowser = FileBrowser(false);
_currentPath = _fileView->getCurrentEntry().path; _currentPath = _fileView->getCurrentEntry().path;
vector<FileItem> currentFolder = fileBrowser.getFileStructure(_currentPath); vector<FileItem> currentFolder = FileBrowser::getFileStructure(_currentPath,false,true);
_fileView.reset(new FileView(_menu->getContentRect(), currentFolder,1)); _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; return 1;
} }
void EventHandler::getLocalFileStructure(std::vector<WebDAVItem> &tempItems)
//TODO use Filebrowser
void EventHandler::getLocalFileStructure(vector<WebDAVItem> &items)
{ {
//get local files, https://stackoverflow.com/questions/306533/how-do-i-get-a-list-of-files-in-a-directory-in-c string localPath = tempItems.at(0).localPath + '/';
DIR *dir;
class dirent *ent;
class stat st;
string localPath = items.at(0).localPath + '/';
if (localPath.back() != '/')
localPath = localPath + '/';
if (iv_access(localPath.c_str(), W_OK) == 0) if (iv_access(localPath.c_str(), W_OK) == 0)
{ {
dir = opendir(localPath.c_str()); vector<FileItem> currentFolder = FileBrowser::getFileStructure(localPath,true,false);
while ((ent = readdir(dir)) != NULL)
for(const FileItem &local : currentFolder)
{ {
const string fileName = ent->d_name; auto p = find_if(tempItems.begin()+1, tempItems.end(), [&] (const WebDAVItem &item) {return item.localPath.compare(local.path) == 0;});
if (p == tempItems.end())
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)
{ {
WebDAVItem temp; WebDAVItem temp;
temp.localPath = fullFileName; temp.localPath = local.path;
temp.state = FileState::ILOCAL; temp.state = FileState::ILOCAL;
temp.title = fullFileName.substr(fullFileName.find_last_of("/") + 1, fullFileName.length()); temp.title = temp.localPath.substr(temp.localPath.find_last_of('/') + 1, temp.localPath.length());
Util::decodeUrl(temp.title); //Log::writeInfoLog(std::to_string(fs::file_size(entry)));
if ((st.st_mode & S_IFDIR) != 0) temp.lastEditDate = local.lastEditDate;
if(local.type == Type::FFOLDER)
{ {
//create new dir in cloud //create new dir in cloud
temp.type = Itemtype::IFOLDER; temp.type = Itemtype::IFOLDER;
@ -604,12 +576,13 @@ void EventHandler::getLocalFileStructure(vector<WebDAVItem> &items)
else else
{ {
//put to cloud //put to cloud
temp.fileType = "File";
temp.type = Itemtype::IFILE; temp.type = Itemtype::IFILE;
} }
items.push_back(temp); tempItems.push_back(temp);
} }
} }
closedir(dir);
} }
} }