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 <string>
#include <experimental/filesystem>
using std::string;
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;
std::vector<FileItem> items;
@ -33,48 +27,41 @@ std::vector<FileItem> 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;
}

View File

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

View File

@ -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

View File

@ -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<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());
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 = "";

View File

@ -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> 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<WebDAVItem> 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<FileItem> currentFolder = fileBrowser.getFileStructure(path);
vector<FileItem> 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<FileItem> currentFolder = fileBrowser.getFileStructure(_currentPath);
vector<FileItem> 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<WebDAVItem> &items)
void EventHandler::getLocalFileStructure(std::vector<WebDAVItem> &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<FileItem> 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<WebDAVItem> &items)
else
{
//put to cloud
temp.fileType = "File";
temp.type = Itemtype::IFILE;
}
items.push_back(temp);
tempItems.push_back(temp);
}
}
closedir(dir);
}
}