From e0c56549d404efdee32874c4ea8ee73e9b654a83 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 6 May 2013 18:57:36 +0200 Subject: Remove drive list from filebrowser in win32. Generally clean up code. --- plugingui/directory.cc | 113 ++++++++++++------------ plugingui/filebrowser.cc | 222 ++++------------------------------------------- plugingui/filebrowser.h | 7 -- 3 files changed, 70 insertions(+), 272 deletions(-) diff --git a/plugingui/directory.cc b/plugingui/directory.cc index 6e4a414..eefbd93 100644 --- a/plugingui/directory.cc +++ b/plugingui/directory.cc @@ -45,30 +45,34 @@ #define SEP "/" #endif - - -Directory::Directory(std::string path) { +Directory::Directory(std::string path) +{ setPath(path); } -Directory::~Directory() { +Directory::~Directory() +{ } -void Directory::setPath(std::string path) { +void Directory::setPath(std::string path) +{ DEBUG(directory, "Setting path to '%s'\n", path.c_str()); this->_path = path; refresh(); } -size_t Directory::count() { +size_t Directory::count() +{ return _files.size(); } -void Directory::refresh() { +void Directory::refresh() +{ _files = listFiles(_path); } -bool Directory::cd(std::string dir) { +bool Directory::cd(std::string dir) +{ //TODO: Should this return true or false? if(dir.empty() || dir == ".") return true; @@ -78,20 +82,24 @@ bool Directory::cd(std::string dir) { setPath(path); refresh(); return true; + } else { + return false; } - else return false; } -bool Directory::cdUp() { +bool Directory::cdUp() +{ return this->cd(".."); } -std::string Directory::path() { +std::string Directory::path() +{ setPath(cleanPath(_path)); return _path; } -Directory::EntryList Directory::entryList() { +Directory::EntryList Directory::entryList() +{ return _files; } @@ -104,15 +112,16 @@ std::string Directory::cwd() { else return ""; } - -std::string Directory::cleanPath(std::string path) { +std::string Directory::cleanPath(std::string path) +{ DEBUG(directory, "Cleaning path '%s'\n", path.c_str()); Directory::Path pathlst = parsePath(path); return Directory::pathToStr(pathlst); } -Directory::EntryList Directory::listFiles(std::string path) { +Directory::EntryList Directory::listFiles(std::string path) +{ DEBUG(directory, "Listing files in '%s'\n", path.c_str()); Directory::EntryList entries; @@ -127,7 +136,7 @@ Directory::EntryList Directory::listFiles(std::string path) { std::string name = entry->d_name; if(name == ".") continue; - if(Directory::isRoot(path) && name == "..") continue; + if(Directory::isRoot(path) && name == "..") continue; entries.push_back(entry->d_name); } @@ -141,8 +150,8 @@ if(Directory::isRoot(path)) entries.push_back(".."); return entries; } -bool Directory::isRoot(std::string path) { -// DEBUG(directory, "Is root %s\n", path.c_str()); +bool Directory::isRoot(std::string path) +{ #ifdef WIN32 std::transform(path.begin(), path.end(), path.begin(), ::tolower); std::string root_str = Directory::root(path); @@ -151,13 +160,10 @@ bool Directory::isRoot(std::string path) { if(path.size() == 2) { if(path == root_str) return true; else return false; - } - else if (path.size() == 3) { -// DEBUG(directory, "Comparing %s and %s\n", path.c_str(), (root_str + SEP).c_str()); + } else if (path.size() == 3) { if(path == root_str + SEP) return true; return false; - } - else { + } else { return false; } #else @@ -166,17 +172,17 @@ bool Directory::isRoot(std::string path) { #endif } -std::string Directory::root() { +std::string Directory::root() +{ return root(cwd()); } -// TODO: Handle windows root -std::string Directory::root(std::string path) { +std::string Directory::root(std::string path) +{ #ifdef WIN32 if(path.size() < 2) { return "c:"; // just something default when input is bad - } - else { + } else { return path.substr(0, 2); } #else @@ -184,7 +190,8 @@ std::string Directory::root(std::string path) { #endif } -Directory::DriveList Directory::drives() { +Directory::DriveList Directory::drives() +{ Directory::DriveList drives; #ifdef WIN32 unsigned int d = GetLogicalDrives(); @@ -208,19 +215,23 @@ bool Directory::isDir() return isDir(_path); } -bool Directory::fileExists(std::string filename) { +bool Directory::fileExists(std::string filename) +{ return !isDir(_path + SEP + filename); } -bool Directory::exists(std::string path) { +bool Directory::exists(std::string path) +{ struct stat st; if(stat(path.c_str(), &st) == 0) { return true; + } else { + return false; } - else return false; } -bool Directory::isDir(std::string path) { +bool Directory::isDir(std::string path) +{ DEBUG(directory, "Is '%s' dir?\n", path.c_str()); struct stat st; if(stat(path.c_str(), &st) == 0) { @@ -233,7 +244,8 @@ bool Directory::isDir(std::string path) { return false; } -Directory::Path Directory::parsePath(std::string path_str) { +Directory::Path Directory::parsePath(std::string path_str) +{ //TODO: Handle "." input and propably other special cases DEBUG(directory, "Parsing path '%s'", path_str.c_str()); @@ -250,8 +262,7 @@ Directory::Path Directory::parsePath(std::string path_str) { dir.clear(); prev_char = current_char; continue; - } - else if(prev_char == ".") { + } else if(prev_char == ".") { prev_char = current_char; continue; } @@ -259,8 +270,7 @@ Directory::Path Directory::parsePath(std::string path_str) { if(!dir.empty()) path.push_back(dir); dir.clear(); continue; - } - else if(current_char == ".") { + } else if(current_char == ".") { if(prev_char == ".") { dir.clear(); if(!path.empty()) path.pop_back(); @@ -277,7 +287,8 @@ Directory::Path Directory::parsePath(std::string path_str) { return path; } -std::string Directory::pathToStr(Directory::Path& path) { +std::string Directory::pathToStr(Directory::Path& path) +{ std::string cleaned_path; DEBUG(directory, "Number of directories in path is %d\n", path.size()); @@ -298,9 +309,9 @@ std::string Directory::pathToStr(Directory::Path& path) { if(cleaned_path.empty()) { cleaned_path = Directory::root(); #ifdef WIN32 - cleaned_path += SEP; + cleaned_path += SEP; #endif -} + } #ifdef WIN32 if(cleaned_path.size() == 2) cleaned_path += SEP; @@ -309,7 +320,8 @@ std::string Directory::pathToStr(Directory::Path& path) { return cleaned_path; } -std::string Directory::pathDirectory(std::string filepath) { +std::string Directory::pathDirectory(std::string filepath) +{ if(Directory::isDir(filepath)) return filepath; Directory::Path path = parsePath(filepath); @@ -317,20 +329,3 @@ std::string Directory::pathDirectory(std::string filepath) { return Directory::pathToStr(path); } - -#ifdef TEST_DIRECTORY -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; - -#endif/*TEST_DIRECTORY*/ diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index d4acdd6..6eaca8a 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -48,7 +48,6 @@ struct GUI::FileBrowser::private_data { GUI::LineEdit *lineedit; GUI::ListBox *listbox; - GUI::ComboBox *drives; void (*filesel_handler)(void *, std::string); void *ptr; Directory *dir; @@ -64,7 +63,8 @@ static void cancel(void *ptr) fp->hide(); } -static void changeDir(void *ptr) { +static void changeDir(void *ptr) +{ struct GUI::FileBrowser::private_data *prv = (struct GUI::FileBrowser::private_data *) ptr; @@ -76,12 +76,12 @@ static void changeDir(void *ptr) { lb->clear(); - INFO(filebrowser, "Changing path to '%s'\n", (dir->path() + "/" + value).c_str()); + INFO(filebrowser, "Changing path to '%s'\n", + (dir->path() + "/" + value).c_str()); #ifdef WIN32 if(prv->above_root && !value.empty()) { -DEBUG(filebrowser, "AAAA"); - dir->setPath(value+"\\"); + dir->setPath(value+"\\"); value.clear(); prv->above_root = false; } @@ -94,7 +94,6 @@ DEBUG(filebrowser, "AAAA"); return; } - std::vector items; #ifdef WIN32 @@ -110,20 +109,20 @@ DEBUG(filebrowser, "AAAA"); items.push_back(item); } prv->above_root = true; - } - else { + } else { #endif - if(!value.empty() && !dir->cd(value)) { - DEBUG(filebrowser, "Error changing to '%s'\n", + if(!value.empty() && !dir->cd(value)) { + DEBUG(filebrowser, "Error changing to '%s'\n", (dir->path() + "/" + value).c_str()); - return; - } - - DEBUG(filebrowser, "Setting path of lineedit to %s\n", dir->path().c_str()); - le->setText(dir->path()); + return; + } + + DEBUG(filebrowser, "Setting path of lineedit to %s\n", + dir->path().c_str()); + le->setText(dir->path()); -Directory::EntryList entries = dir->entryList(); + Directory::EntryList entries = dir->entryList(); for(Directory::EntryList::iterator it = entries.begin(); it != entries.end(); it++) { GUI::ListBoxBasic::Item item; @@ -138,94 +137,10 @@ Directory::EntryList entries = dir->entryList(); lb->addItems(items); } -#if 0 -static void changeDir(void *ptr) -{ - struct GUI::FileBrowser::private_data *prv = - (struct GUI::FileBrowser::private_data *)ptr; - - GUI::ListBox *lb = prv->listbox; - GUI::LineEdit *le = prv->lineedit; - std::string value = lb->selectedValue(); - -#ifdef WIN32 - std::string drive = prv->drives->selectedValue(); - int drvidx = atoi(drive.c_str()); - /*if(prv->drvidx != drvidx)*/ _chdrive(drvidx + 1); // one based... sigh - //printf("DRV: [%d %s]\n", drvidx, drive.c_str()); -#endif - - char filename[1024]; - char *c = getcwd(filename, sizeof(filename)); - (void)c; - - DEBUG(cwd, "CWD1: [%s]\n", filename); - - if(value != "") { -#ifdef WIN32 - if(prv->drvidx == drvidx) { - strcat(filename, "\\"); - strcat(filename, value.c_str()); - } - prv->drvidx = drvidx; -#else - strcat(filename, "/"); - strcat(filename, value.c_str()); -#endif - } - - struct stat st; - if(stat(filename, &st) == 0) { - if((st.st_mode & S_IFDIR) != 0) { - DEBUG(cwd, "'%s' is present and is a directory\n", filename); - } - if((st.st_mode & S_IFREG) != 0) { - DEBUG(cwd, "'%s' is present and is a file\n", filename); - if(prv->filesel_handler) prv->filesel_handler(prv->ptr, filename); - return; - } - } else { - DEBUG(cwd, "'%s' is not present or unreadable\n", filename); - //perror("!"); - return; - } - - lb->clear(); - int i = chdir(value.c_str()); - (void)i; - - c = getcwd(filename, sizeof(filename)); - le->setText(filename); - - DEBUG(cwd, "CWD2: [%s]\n", filename); - - DIR *dir = opendir("."); - if(!dir) { - lb->addItem("[ Could not open dir ]", ""); - return; - } - - std::vector items; - struct dirent *entry; - while((entry = readdir(dir)) != NULL) { - GUI::ListBoxBasic::Item item; - item.name = entry->d_name; - item.value = entry->d_name; - items.push_back(item); - } - lb->addItems(items); - - closedir(dir); -} -#endif/*0*/ - GUI::FileBrowser::FileBrowser(GUI::Widget *parent) : GUI::Widget(parent), lbl_path(this), lineedit(this), listbox(this), btn_sel(this), btn_esc(this), back(":bg.png") -#ifdef WIN32 - , drv(this), lbl_drive(this) -#endif { prv = new struct GUI::FileBrowser::private_data(); prv->filesel_handler = NULL; @@ -250,30 +165,6 @@ GUI::FileBrowser::FileBrowser(GUI::Widget *parent) btn_esc.registerClickHandler(cancel, this); changeDir(prv); -/* -#ifdef WIN32 - lbl_drive.setText("Drive:"); - - drv.registerValueChangedHandler(changeDir, prv); - - unsigned int d = GetLogicalDrives(); - for(int i = 0; i < 32; i++) { - if(d & (1 << i)) { - - char name[] = "X:"; - name[0] = i + 'A'; - - char num[32]; - sprintf(num, "%d", i); - - drv.addItem(name, num); - } - } - prv->drives = &drv; -#endif -*/ - -// changeDir(prv); resize(200, 190); } @@ -284,82 +175,15 @@ GUI::FileBrowser::~FileBrowser() delete prv; } -#if 0 -#include - -static bool isDir(std::string d) -{ - DEBUG(dir, "Is '%s' a dir?\n", d.c_str()); - struct stat st; - if(stat(d.c_str(), &st) == 0) { - if((st.st_mode & S_IFDIR) != 0) { - DEBUG(dir, "Yes\n"); - return true; - } - } - - DEBUG(dir, "No\n"); - return false; -} -#endif - void GUI::FileBrowser::setPath(std::string path) { -// WARN(filebrowser, "Not implemented yet!"); - // TODO: Remove this check to directoy.cc INFO(filebrowser, "Setting path to '%s'\n", path.c_str()); if(path.empty()) path = Directory::cwd(); - // TODO: Strip path to set path to a directory prv->dir->setPath(Directory::pathDirectory(path)); prv->listbox->clear(); changeDir(prv); -/* - std::string dir; - if(prv->dir->isDir()) { - dir = path; - } else { - char *d = strdup(path.c_str()); - std::string _dirname = dirname(d); - free(d); - if(prv->dir->isDir(_dirname)) dir = _dirname; - else return; - } -*/ -// if(chdir(dir.c_str()) == -1) return; -// prv->listbox->clear(); -// changeDir(prv); - - /* - std::string dirname = path; - - while(dirname != "") { - - DEBUG(filebrowser, "dirname: %s\n", dirname.c_str()); - - struct stat st; - if(stat(dirname.c_str(), &st) == 0) { - if((st.st_mode & S_IFDIR) != 0) { - dirname += "/."; - int i = chdir(dirname.c_str()); - (void)i; - changeDir(prv); - - DEBUG(filebrowser, "chdir to: %s\n", dirname.c_str()); - - return; - } - } - - dirname = dirname.substr(0, dirname.length() - 1); - while(dirname[dirname.length() - 1] != '/' && - dirname[dirname.length() - 1] != '\\' && - dirname != "") { - dirname = dirname.substr(0, dirname.length() - 1); - } - } - */ } void GUI::FileBrowser::resize(int w, int h) @@ -370,17 +194,6 @@ void GUI::FileBrowser::resize(int w, int h) int brd = 5; // border int btn_h = 30; -#ifdef WIN32 - offset += brd; - - lbl_drive.move(0, offset); - drv.move(60, offset); - - offset += btn_h; - - lbl_drive.resize(60, btn_h); - drv.resize(w - 60 - brd, btn_h); -#endif offset += brd; lbl_path.move(0, offset); @@ -401,13 +214,10 @@ void GUI::FileBrowser::resize(int w, int h) btn_sel.move(brd + w / 2 - brd / 2, h - btn_h - brd); btn_sel.resize((w - 1 - 2*brd) / 2, btn_h); - - } - void GUI::FileBrowser::registerFileSelectHandler(void (*handler)(void *, - std::string), + std::string), void *ptr) { prv->filesel_handler = handler; diff --git a/plugingui/filebrowser.h b/plugingui/filebrowser.h index cd389be..6583b53 100644 --- a/plugingui/filebrowser.h +++ b/plugingui/filebrowser.h @@ -31,7 +31,6 @@ #include "button.h" #include "listbox.h" -#include "combobox.h" #include "lineedit.h" #include "label.h" #include "image.h" @@ -69,12 +68,6 @@ private: GUI::Button btn_esc; Image back; - -#ifdef WIN32 - // Only used on win32 - GUI::ComboBox drv; - GUI::Label lbl_drive; -#endif }; }; -- cgit v1.2.3