From 1dec6f6f119b397809abb909035eaefe8f7be61f Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Wed, 13 Aug 2014 00:24:18 +0000 Subject: [PATCH] Split creation, configuration and use of FileLister objects The constructor now has zero arguments. showDirectories and showFiles are now set using setter methods. FileLister::browse is now the sole way to start a scan for files and directories, replacing the initial path in the constructor and the setPath method. It allows merging results from multiple directories as before. This is all to make explicit how many times the costly task of browsing a directory is actually carried out. --- src/browsedialog.cpp | 12 +++++------ src/browsedialog.h | 6 ++++-- src/dirdialog.cpp | 4 +++- src/filedialog.cpp | 3 ++- src/filelister.cpp | 44 ++++++++++++++++++++--------------------- src/filelister.h | 11 ++++++----- src/gmenu2x.cpp | 15 +++++++------- src/menu.cpp | 5 +++-- src/selector.cpp | 9 ++++----- src/wallpaperdialog.cpp | 18 ++++++++--------- 10 files changed, 63 insertions(+), 64 deletions(-) diff --git a/src/browsedialog.cpp b/src/browsedialog.cpp index f0fc906..174517b 100644 --- a/src/browsedialog.cpp +++ b/src/browsedialog.cpp @@ -51,13 +51,11 @@ bool BrowseDialog::exec() if (!fl) return false; - string path = fl->getPath(); - if (path.empty() || !fileExists(path) || path.compare(0, - strlen(CARD_ROOT), CARD_ROOT) != 0) + string path = getPath(); + if (path.empty() || !fileExists(path) + || path.compare(0, strlen(CARD_ROOT), CARD_ROOT) != 0) setPath(CARD_ROOT); - fl->browse(); - const int topBarHeight = gmenu2x->skinConfInt["topBarHeight"]; rowHeight = gmenu2x->font->getLineSpacing() + 1; // gp2x=15+1 / pandora=19+1 rowHeight = constrain(rowHeight, 20, 40); @@ -183,7 +181,7 @@ void BrowseDialog::handleInput() void BrowseDialog::directoryUp() { - string path = fl->getPath(); + string path = getPath(); string::size_type p = path.rfind("/"); if (p == path.size() - 1) { @@ -200,7 +198,7 @@ void BrowseDialog::directoryUp() void BrowseDialog::directoryEnter() { - string path = fl->getPath(); + string path = getPath(); if (path[path.size()-1] != '/') { path += "/"; } diff --git a/src/browsedialog.h b/src/browsedialog.h index 61d2657..56d04d3 100644 --- a/src/browsedialog.h +++ b/src/browsedialog.h @@ -40,7 +40,8 @@ protected: virtual ~BrowseDialog(); void setPath(const std::string &path) { - fl->setPath(path); + this->path = path; + fl->browse(path); } FileLister *fl; @@ -64,6 +65,7 @@ private: std::string title; std::string subtitle; + std::string path; SDL_Rect clipRect; SDL_Rect touchRect; @@ -93,7 +95,7 @@ public: bool exec(); const std::string &getPath() { - return fl->getPath(); + return path; } std::string getFile() { return (*fl)[selected]; diff --git a/src/dirdialog.cpp b/src/dirdialog.cpp index 6d0397d..bababb3 100644 --- a/src/dirdialog.cpp +++ b/src/dirdialog.cpp @@ -29,7 +29,9 @@ DirDialog::DirDialog( const string &text, const string &dir) : BrowseDialog(gmenu2x, ts, "Directory Browser", text) { - fl = new FileLister(dir, true, false); + fl = new FileLister(); + fl->setShowFiles(false); + setPath(dir); } DirDialog::~DirDialog() diff --git a/src/filedialog.cpp b/src/filedialog.cpp index fef76d6..23e2f77 100644 --- a/src/filedialog.cpp +++ b/src/filedialog.cpp @@ -37,8 +37,9 @@ FileDialog::FileDialog( path = file.substr(0, pos); } - fl = new FileLister(path); + fl = new FileLister(); fl->setFilter(filter); + setPath(path); } FileDialog::~FileDialog() diff --git a/src/filelister.cpp b/src/filelister.cpp index d417b6e..fc3c6f0 100644 --- a/src/filelister.cpp +++ b/src/filelister.cpp @@ -34,26 +34,10 @@ using namespace std; -FileLister::FileLister(const string &startPath, bool showDirectories, - bool showFiles) : showDirectories(showDirectories), showFiles(showFiles) +FileLister::FileLister() + : showDirectories(true) + , showFiles(true) { - setPath(startPath, false); -} - -const string &FileLister::getPath() -{ - return path; -} - -void FileLister::setPath(const string &path, bool doBrowse) -{ - this->path = path; - - if (this->path[path.length() - 1]!='/') - this->path += "/"; - - if (doBrowse) - browse(); } const string &FileLister::getFilter() @@ -66,7 +50,17 @@ void FileLister::setFilter(const string &filter) this->filter = filter; } -void FileLister::browse(bool clean) +void FileLister::setShowDirectories(bool showDirectories) +{ + this->showDirectories = showDirectories; +} + +void FileLister::setShowFiles(bool showFiles) +{ + this->showFiles = showFiles; +} + +void FileLister::browse(const string& path, bool clean) { if (clean) { directories.clear(); @@ -75,8 +69,12 @@ void FileLister::browse(bool clean) if (showDirectories || showFiles) { DIR *dirp; - if ((dirp = opendir(path.c_str())) == NULL) { - ERROR("Unable to open directory: %s\n", path.c_str()); + string slashedPath = path; + if (path[path.length() - 1] != '/') + slashedPath.push_back('/'); + + if ((dirp = opendir(slashedPath.c_str())) == NULL) { + ERROR("Unable to open directory: %s\n", slashedPath.c_str()); return; } @@ -90,7 +88,7 @@ void FileLister::browse(bool clean) if (file[0] == '.' && file != "..") continue; - filepath = path + file; + filepath = slashedPath + file; int statRet = stat(filepath.c_str(), &st); if (statRet == -1) { ERROR("Stat failed on '%s' with error '%s'\n", filepath.c_str(), strerror(errno)); diff --git a/src/filelister.h b/src/filelister.h index 2497d29..8da00db 100644 --- a/src/filelister.h +++ b/src/filelister.h @@ -26,14 +26,14 @@ class FileLister { private: - std::string path, filter; + std::string filter; bool showDirectories, showFiles; std::vector directories, files, excludes; public: - FileLister(const std::string &startPath = "/boot/local", bool showDirectories = true, bool showFiles = true); - void browse(bool clean = true); + FileLister(); + void browse(const std::string& path, bool clean = true); unsigned int size(); unsigned int dirCount(); @@ -43,11 +43,12 @@ public: bool isFile(unsigned int); bool isDirectory(unsigned int); - const std::string &getPath(); - void setPath(const std::string &path, bool doBrowse=true); const std::string &getFilter(); void setFilter(const std::string &filter); + void setShowDirectories(bool); + void setShowFiles(bool); + const std::vector &getDirectories() { return directories; } const std::vector &getFiles() { return files; } void insertFile(const std::string &file); diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 4ca4d59..69a1554 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -673,13 +673,12 @@ void GMenu2X::showSettings() { int curMenuClock = confInt["menuClock"]; #endif - FileLister fl_tr(GMENU2X_SYSTEM_DIR "/translations"); - fl_tr.browse(); + FileLister fl_tr; + fl_tr.browse(GMENU2X_SYSTEM_DIR "/translations"); string tr_path = getHome() + "/translations"; if (fileExists(tr_path)) { - fl_tr.setPath(tr_path, false); - fl_tr.browse(false); + fl_tr.browse(tr_path, false); } fl_tr.insertFile("English"); @@ -725,11 +724,11 @@ void GMenu2X::showSettings() { } void GMenu2X::skinMenu() { - FileLister fl_sk(getHome() + "/skins", true, false); + FileLister fl_sk; + fl_sk.setShowFiles(false); fl_sk.addExclude(".."); - fl_sk.browse(); - fl_sk.setPath(GMENU2X_SYSTEM_DIR "/skins", false); - fl_sk.browse(false); + fl_sk.browse(getHome() + "/skins"); + fl_sk.browse(GMENU2X_SYSTEM_DIR "/skins", false); string curSkin = confStr["skin"]; diff --git a/src/menu.cpp b/src/menu.cpp index f094201..6822546 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -464,9 +464,10 @@ bool Menu::addLink(string path, string file, string section) { manual = exename+".man.txt"; } else { //scan directory for a file like *readme* - FileLister fl(path, false); + FileLister fl; + fl.setShowDirectories(false); fl.setFilter(".txt"); - fl.browse(); + fl.browse(path); bool found = false; for (uint x=0; xgetSelectorBrowser()); + FileLister fl; + fl.setShowDirectories(link->getSelectorBrowser()); fl.setFilter(link->getSelectorFilter()); - fl.browse(); + prepare(fl); OffscreenSurface bg(*gmenu2x->bg); drawTitleIcon(bg, link->getIconPath(), true); @@ -85,8 +86,6 @@ int Selector::exec(int startSelection) { unsigned int firstElement = 0; unsigned int selected = constrain(startSelection, 0, fl.size() - 1); - prepare(fl); - auto folderIcon = gmenu2x->sc.skinRes("imgs/folder.png"); if (!folderIcon) { folderIcon = gmenu2x->sc.addSkinRes("imgs/folder.png"); @@ -214,7 +213,7 @@ int Selector::exec(int startSelection) { } void Selector::prepare(FileLister& fl) { - fl.setPath(dir); + fl.browse(dir); screendir = dir; if (!screendir.empty() && screendir[screendir.length() - 1] != '/') { diff --git a/src/wallpaperdialog.cpp b/src/wallpaperdialog.cpp index 2234c0a..67c7159 100644 --- a/src/wallpaperdialog.cpp +++ b/src/wallpaperdialog.cpp @@ -46,28 +46,26 @@ bool WallpaperDialog::exec() fl.setFilter("png"); string filepath = GMenu2X::getHome() + "/skins/" - + gmenu2x->confStr["skin"] + "/wallpapers"; - if (fileExists(filepath)) - fl.setPath(filepath, true); + + gmenu2x->confStr["skin"] + "/wallpapers"; + if (fileExists(filepath)) { + fl.browse(filepath, true); + } filepath = GMENU2X_SYSTEM_DIR "/skins/" - + gmenu2x->confStr["skin"] + "/wallpapers"; + + gmenu2x->confStr["skin"] + "/wallpapers"; if (fileExists(filepath)) { - fl.setPath(filepath, false); - fl.browse(false); + fl.browse(filepath, false); } if (gmenu2x->confStr["skin"] != "Default") { filepath = GMenu2X::getHome() + "/skins/Default/wallpapers"; if (fileExists(filepath)) { - fl.setPath(filepath, false); - fl.browse(false); + fl.browse(filepath, false); } filepath = GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers"; if (fileExists(filepath)) { - fl.setPath(filepath, false); - fl.browse(false); + fl.browse(filepath, false); } }