1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-06-30 21:17:18 +03: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)
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 += "/";
}

View File

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

View File

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

View File

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

View File

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

View File

@ -26,14 +26,14 @@
class FileLister {
private:
std::string path, filter;
std::string filter;
bool showDirectories, showFiles;
std::vector<std::string> 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<std::string> &getDirectories() { return directories; }
const std::vector<std::string> &getFiles() { return files; }
void insertFile(const std::string &file);

View File

@ -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"];

View File

@ -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; x<fl.size() && !found; 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) {
FileLister fl(dir, link->getSelectorBrowser());
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] != '/') {

View File

@ -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);
}
}