Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FileBrowserControl.cc

Go to the documentation of this file.
00001 #include "FileBrowserControl.h"
00002 #include <sys/types.h>
00003 #include <sys/stat.h>
00004 #include <unistd.h>
00005 #include <dirent.h>
00006 
00007 using namespace std;
00008 
00009 ControlBase * FileBrowserControl::activate(MotionManager::MC_ID display, Socket * gui) {
00010   rebuildmenu();
00011   return ControlBase::activate(display,gui);
00012 }
00013 
00014 ControlBase* FileBrowserControl::doSelect() {
00015   for(unsigned int i=0; i<hilights.size(); i++) {
00016     unsigned int cur=hilights[i];
00017     if(cur>=options.size() || options[cur]==NULL)
00018       continue;
00019     ControlBase::doSelect();
00020     std::string nm(options[cur]->getName());
00021     if(nm[nm.size()-1]=='/' || nm=="..") {
00022       if(hilights.size()>1)
00023         continue;
00024       if(nm=="..")
00025         paths.pop_back();
00026       else
00027         paths.push_back(nm.substr(0,nm.size()-1));
00028       rebuildmenu();
00029       refresh();
00030       return this;
00031     } else {
00032       ControlBase * ret=selectedFile(makePath(nm));
00033       if(ret!=this)
00034         return ret;
00035     }
00036   }
00037   refresh();
00038   return this;
00039 }
00040 
00041 ControlBase * FileBrowserControl::takeInput(const std::string& msg) {
00042   if(options.size()==1 && options.front()==NULL)
00043     rebuildmenu();
00044   return ControlBase::takeInput(msg);
00045 }
00046 
00047 void FileBrowserControl::setRoot(const std::string& path) {
00048   root=path;
00049   if(root[root.size()-1]=='/')
00050     root.erase(root.size()-1);
00051   paths.clear();
00052 }
00053 
00054 
00055 void FileBrowserControl::setPath(const std::string& path) {
00056   for(unsigned int i=0; i<path.size(); i++)
00057     if(path[i]!='/')
00058       paths.back().append(1,path[i]);
00059     else if(paths.back().size()!=0)
00060       paths.push_back(std::string());
00061 }
00062 
00063 
00064 std::string FileBrowserControl::makePath() {
00065   std::string path=root;
00066   for(unsigned int i=0; i<paths.size(); i++) {
00067     path+="/";
00068     path+=paths[i];
00069   }
00070   return path;
00071 }
00072 
00073 //! returns the path from root as a string, appends filename
00074 std::string FileBrowserControl::makePath(const std::string& filename) {
00075   std::string path=makePath();
00076   path.append("/");
00077   path.append(filename);
00078   return path;
00079 }
00080   
00081 bool FileBrowserControl::match(const std::string& file, const std::string& filt) {
00082   unsigned int i=0;
00083   if(i==filt.size() && i==file.size())
00084     return true;
00085   if(i==filt.size() || i==file.size())
00086     return false;
00087   while(filt[i]!='*') {
00088     if(toupper(filt[i])!=toupper(file[i]))
00089       return false;
00090     i++;
00091     if(i==filt.size() && i==file.size())
00092       return true;
00093     if(i==filt.size() || i==file.size())
00094       return false;
00095   }
00096   i=filt.size()-1;
00097   unsigned int j=file.size()-1;
00098   while(filt[i]!='*') {
00099     if(toupper(filt[i])!=toupper(file[j]))
00100       return false;
00101     i--; j--;
00102   }
00103   return true;
00104 }
00105 
00106 void FileBrowserControl::rebuildmenu() {
00107   clearSlots();
00108   DIR* dir=opendir(makePath().c_str());
00109   if(dir==NULL) {
00110     pushSlot(new ControlBase("Bad Path"));
00111     cout << "bad path: " << makePath() << endl;
00112     return;
00113   }
00114   if(paths.size()!=0 && recurse) {
00115     struct stat s;
00116     std::string path=makePath("..");
00117     int err=stat(path.c_str(),&s);
00118     if(err==0 && s.st_mode&S_IFDIR)
00119       pushSlot(new ControlBase(".."));
00120   }
00121   struct dirent * ent=readdir(dir);
00122   while(ent!=NULL) {
00123     if(strcmp(ent->d_name,".")!=0 && strcmp(ent->d_name,"..")!=0) {
00124       struct stat s;
00125       std::string path=(makePath(ent->d_name));
00126       int err=stat(path.c_str(),&s);
00127       if(err!=0) {
00128         cout << "File disappeared: " << path << endl;
00129         return;
00130       }
00131       if(s.st_mode&S_IFDIR) {
00132         if(recurse)
00133           pushSlot(new ControlBase(std::string(ent->d_name).append(1,'/')));
00134       } else {
00135         std::string nm=(makePath(ent->d_name));
00136         if(match(nm,filter))
00137           pushSlot(new ControlBase(ent->d_name));
00138       }
00139     }
00140     ent=readdir(dir);
00141   }
00142   closedir(dir);
00143   if(options.size()==0)
00144     pushSlot(new ControlBase("[empty directory]"));
00145   else {
00146     hilights.push_back(0);
00147     /*  for(unsigned int i=0; i<hilights.size(); i++) {
00148         if(hilights[i]>=options.size()) {
00149         hilights.resize(i);
00150         cout << "hilights resized at " << i << endl;
00151         break;
00152         }
00153         }*/
00154   }
00155 }
00156 
00157 
00158 /*! @file
00159  * @brief Implements FileBrowserControl, which displays the contents of a directory
00160  * @author ejt (Creator)
00161  *
00162  * $Author: ejt $
00163  * $Name: tekkotsu-2_4_1 $
00164  * $Revision: 1.10 $
00165  * $State: Exp $
00166  * $Date: 2005/02/02 18:20:27 $
00167  */
00168 

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:47 2005 by Doxygen 1.4.4