1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-26 03:06:15 +02:00

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.
This commit is contained in:
Nebuleon Fumika 2014-08-13 00:24:18 +00:00 committed by Maarten ter Huurne
parent 1508ac87ac
commit 1dec6f6f11
10 changed files with 63 additions and 64 deletions

View File

@ -51,13 +51,11 @@ bool BrowseDialog::exec()
if (!fl) if (!fl)
return false; return false;
string path = fl->getPath(); string path = getPath();
if (path.empty() || !fileExists(path) || path.compare(0, if (path.empty() || !fileExists(path)
strlen(CARD_ROOT), CARD_ROOT) != 0) || path.compare(0, strlen(CARD_ROOT), CARD_ROOT) != 0)
setPath(CARD_ROOT); setPath(CARD_ROOT);
fl->browse();
const int topBarHeight = gmenu2x->skinConfInt["topBarHeight"]; const int topBarHeight = gmenu2x->skinConfInt["topBarHeight"];
rowHeight = gmenu2x->font->getLineSpacing() + 1; // gp2x=15+1 / pandora=19+1 rowHeight = gmenu2x->font->getLineSpacing() + 1; // gp2x=15+1 / pandora=19+1
rowHeight = constrain(rowHeight, 20, 40); rowHeight = constrain(rowHeight, 20, 40);
@ -183,7 +181,7 @@ void BrowseDialog::handleInput()
void BrowseDialog::directoryUp() void BrowseDialog::directoryUp()
{ {
string path = fl->getPath(); string path = getPath();
string::size_type p = path.rfind("/"); string::size_type p = path.rfind("/");
if (p == path.size() - 1) { if (p == path.size() - 1) {
@ -200,7 +198,7 @@ void BrowseDialog::directoryUp()
void BrowseDialog::directoryEnter() void BrowseDialog::directoryEnter()
{ {
string path = fl->getPath(); string path = getPath();
if (path[path.size()-1] != '/') { if (path[path.size()-1] != '/') {
path += "/"; path += "/";
} }

View File

@ -40,7 +40,8 @@ protected:
virtual ~BrowseDialog(); virtual ~BrowseDialog();
void setPath(const std::string &path) { void setPath(const std::string &path) {
fl->setPath(path); this->path = path;
fl->browse(path);
} }
FileLister *fl; FileLister *fl;
@ -64,6 +65,7 @@ private:
std::string title; std::string title;
std::string subtitle; std::string subtitle;
std::string path;
SDL_Rect clipRect; SDL_Rect clipRect;
SDL_Rect touchRect; SDL_Rect touchRect;
@ -93,7 +95,7 @@ public:
bool exec(); bool exec();
const std::string &getPath() { const std::string &getPath() {
return fl->getPath(); return path;
} }
std::string getFile() { std::string getFile() {
return (*fl)[selected]; return (*fl)[selected];

View File

@ -29,7 +29,9 @@ DirDialog::DirDialog(
const string &text, const string &dir) const string &text, const string &dir)
: BrowseDialog(gmenu2x, ts, "Directory Browser", text) : BrowseDialog(gmenu2x, ts, "Directory Browser", text)
{ {
fl = new FileLister(dir, true, false); fl = new FileLister();
fl->setShowFiles(false);
setPath(dir);
} }
DirDialog::~DirDialog() DirDialog::~DirDialog()

View File

@ -37,8 +37,9 @@ FileDialog::FileDialog(
path = file.substr(0, pos); path = file.substr(0, pos);
} }
fl = new FileLister(path); fl = new FileLister();
fl->setFilter(filter); fl->setFilter(filter);
setPath(path);
} }
FileDialog::~FileDialog() FileDialog::~FileDialog()

View File

@ -34,26 +34,10 @@
using namespace std; using namespace std;
FileLister::FileLister(const string &startPath, bool showDirectories, FileLister::FileLister()
bool showFiles) : showDirectories(showDirectories), showFiles(showFiles) : 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() const string &FileLister::getFilter()
@ -66,7 +50,17 @@ void FileLister::setFilter(const string &filter)
this->filter = 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) { if (clean) {
directories.clear(); directories.clear();
@ -75,8 +69,12 @@ void FileLister::browse(bool clean)
if (showDirectories || showFiles) { if (showDirectories || showFiles) {
DIR *dirp; DIR *dirp;
if ((dirp = opendir(path.c_str())) == NULL) { string slashedPath = path;
ERROR("Unable to open directory: %s\n", path.c_str()); 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; return;
} }
@ -90,7 +88,7 @@ void FileLister::browse(bool clean)
if (file[0] == '.' && file != "..") if (file[0] == '.' && file != "..")
continue; continue;
filepath = path + file; filepath = slashedPath + file;
int statRet = stat(filepath.c_str(), &st); int statRet = stat(filepath.c_str(), &st);
if (statRet == -1) { if (statRet == -1) {
ERROR("Stat failed on '%s' with error '%s'\n", filepath.c_str(), strerror(errno)); ERROR("Stat failed on '%s' with error '%s'\n", filepath.c_str(), strerror(errno));

View File

@ -26,14 +26,14 @@
class FileLister { class FileLister {
private: private:
std::string path, filter; std::string filter;
bool showDirectories, showFiles; bool showDirectories, showFiles;
std::vector<std::string> directories, files, excludes; std::vector<std::string> directories, files, excludes;
public: public:
FileLister(const std::string &startPath = "/boot/local", bool showDirectories = true, bool showFiles = true); FileLister();
void browse(bool clean = true); void browse(const std::string& path, bool clean = true);
unsigned int size(); unsigned int size();
unsigned int dirCount(); unsigned int dirCount();
@ -43,11 +43,12 @@ public:
bool isFile(unsigned int); bool isFile(unsigned int);
bool isDirectory(unsigned int); bool isDirectory(unsigned int);
const std::string &getPath();
void setPath(const std::string &path, bool doBrowse=true);
const std::string &getFilter(); const std::string &getFilter();
void setFilter(const std::string &filter); void setFilter(const std::string &filter);
void setShowDirectories(bool);
void setShowFiles(bool);
const std::vector<std::string> &getDirectories() { return directories; } const std::vector<std::string> &getDirectories() { return directories; }
const std::vector<std::string> &getFiles() { return files; } const std::vector<std::string> &getFiles() { return files; }
void insertFile(const std::string &file); void insertFile(const std::string &file);

View File

@ -673,13 +673,12 @@ void GMenu2X::showSettings() {
int curMenuClock = confInt["menuClock"]; int curMenuClock = confInt["menuClock"];
#endif #endif
FileLister fl_tr(GMENU2X_SYSTEM_DIR "/translations"); FileLister fl_tr;
fl_tr.browse(); fl_tr.browse(GMENU2X_SYSTEM_DIR "/translations");
string tr_path = getHome() + "/translations"; string tr_path = getHome() + "/translations";
if (fileExists(tr_path)) { if (fileExists(tr_path)) {
fl_tr.setPath(tr_path, false); fl_tr.browse(tr_path, false);
fl_tr.browse(false);
} }
fl_tr.insertFile("English"); fl_tr.insertFile("English");
@ -725,11 +724,11 @@ void GMenu2X::showSettings() {
} }
void GMenu2X::skinMenu() { void GMenu2X::skinMenu() {
FileLister fl_sk(getHome() + "/skins", true, false); FileLister fl_sk;
fl_sk.setShowFiles(false);
fl_sk.addExclude(".."); fl_sk.addExclude("..");
fl_sk.browse(); fl_sk.browse(getHome() + "/skins");
fl_sk.setPath(GMENU2X_SYSTEM_DIR "/skins", false); fl_sk.browse(GMENU2X_SYSTEM_DIR "/skins", false);
fl_sk.browse(false);
string curSkin = confStr["skin"]; string curSkin = confStr["skin"];

View File

@ -464,9 +464,10 @@ bool Menu::addLink(string path, string file, string section) {
manual = exename+".man.txt"; manual = exename+".man.txt";
} else { } else {
//scan directory for a file like *readme* //scan directory for a file like *readme*
FileLister fl(path, false); FileLister fl;
fl.setShowDirectories(false);
fl.setFilter(".txt"); fl.setFilter(".txt");
fl.browse(); fl.browse(path);
bool found = false; bool found = false;
for (uint x=0; x<fl.size() && !found; x++) { for (uint x=0; x<fl.size() && !found; x++) {
string lcfilename = fl[x]; string lcfilename = fl[x];

View File

@ -52,9 +52,10 @@ Selector::Selector(GMenu2X *gmenu2x, LinkApp *link, const string &selectorDir) :
} }
int Selector::exec(int startSelection) { int Selector::exec(int startSelection) {
FileLister fl(dir, link->getSelectorBrowser()); FileLister fl;
fl.setShowDirectories(link->getSelectorBrowser());
fl.setFilter(link->getSelectorFilter()); fl.setFilter(link->getSelectorFilter());
fl.browse(); prepare(fl);
OffscreenSurface bg(*gmenu2x->bg); OffscreenSurface bg(*gmenu2x->bg);
drawTitleIcon(bg, link->getIconPath(), true); drawTitleIcon(bg, link->getIconPath(), true);
@ -85,8 +86,6 @@ int Selector::exec(int startSelection) {
unsigned int firstElement = 0; unsigned int firstElement = 0;
unsigned int selected = constrain(startSelection, 0, fl.size() - 1); unsigned int selected = constrain(startSelection, 0, fl.size() - 1);
prepare(fl);
auto folderIcon = gmenu2x->sc.skinRes("imgs/folder.png"); auto folderIcon = gmenu2x->sc.skinRes("imgs/folder.png");
if (!folderIcon) { if (!folderIcon) {
folderIcon = gmenu2x->sc.addSkinRes("imgs/folder.png"); folderIcon = gmenu2x->sc.addSkinRes("imgs/folder.png");
@ -214,7 +213,7 @@ int Selector::exec(int startSelection) {
} }
void Selector::prepare(FileLister& fl) { void Selector::prepare(FileLister& fl) {
fl.setPath(dir); fl.browse(dir);
screendir = dir; screendir = dir;
if (!screendir.empty() && screendir[screendir.length() - 1] != '/') { if (!screendir.empty() && screendir[screendir.length() - 1] != '/') {

View File

@ -47,27 +47,25 @@ bool WallpaperDialog::exec()
string filepath = GMenu2X::getHome() + "/skins/" string filepath = GMenu2X::getHome() + "/skins/"
+ gmenu2x->confStr["skin"] + "/wallpapers"; + gmenu2x->confStr["skin"] + "/wallpapers";
if (fileExists(filepath)) if (fileExists(filepath)) {
fl.setPath(filepath, true); fl.browse(filepath, true);
}
filepath = GMENU2X_SYSTEM_DIR "/skins/" filepath = GMENU2X_SYSTEM_DIR "/skins/"
+ gmenu2x->confStr["skin"] + "/wallpapers"; + gmenu2x->confStr["skin"] + "/wallpapers";
if (fileExists(filepath)) { if (fileExists(filepath)) {
fl.setPath(filepath, false); fl.browse(filepath, false);
fl.browse(false);
} }
if (gmenu2x->confStr["skin"] != "Default") { if (gmenu2x->confStr["skin"] != "Default") {
filepath = GMenu2X::getHome() + "/skins/Default/wallpapers"; filepath = GMenu2X::getHome() + "/skins/Default/wallpapers";
if (fileExists(filepath)) { if (fileExists(filepath)) {
fl.setPath(filepath, false); fl.browse(filepath, false);
fl.browse(false);
} }
filepath = GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers"; filepath = GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers";
if (fileExists(filepath)) { if (fileExists(filepath)) {
fl.setPath(filepath, false); fl.browse(filepath, false);
fl.browse(false);
} }
} }