X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcreaDevManagerLib%2FmodelCDMProjectsTree.cpp;fp=lib%2FcreaDevManagerLib%2FmodelCDMProjectsTree.cpp;h=cc09a9fb147336c241821ee097d96e46771fb976;hb=f887a0013d53146a6a280a0f88514df95ea6bfda;hp=0000000000000000000000000000000000000000;hpb=c2aff0c6d14e7c5d05459d92184c2a29a71c73ac;p=crea.git diff --git a/lib/creaDevManagerLib/modelCDMProjectsTree.cpp b/lib/creaDevManagerLib/modelCDMProjectsTree.cpp new file mode 100755 index 0000000..cc09a9f --- /dev/null +++ b/lib/creaDevManagerLib/modelCDMProjectsTree.cpp @@ -0,0 +1,187 @@ +/* + * ModelCreaDevManagerTree.cpp + * + * Created on: 22/10/2012 + * Author: daniel + */ + +#include "modelCDMProjectsTree.h" + +#include +#include + +#include +#include + +modelCDMProjectsTree::modelCDMProjectsTree() +{ +} + +modelCDMProjectsTree::~modelCDMProjectsTree() +{ +} + +bool modelCDMProjectsTree::CompareNodeItem(modelCDMProjectsTreeNode x, modelCDMProjectsTreeNode y) +{ + bool returnValue; + bool noWinner = true; + unsigned int i = 0; + std::string xName = x.GetName(); + std::string yName = y.GetName(); + unsigned char xType = x.GetType(); + unsigned char yType = y.GetType(); + + while ((i < xName.length()) && (i < yName.length())) + { + if (tolower (xName[i]) < tolower (yName[i])) + { + noWinner = false; + returnValue = true; + break; + } + else if (tolower (xName[i]) > tolower (yName[i])) + { + noWinner = false; + returnValue = false; + break; + } + i++; + } + + if(noWinner) + { + if (xName.length() < yName.length()) + returnValue = true; + else + returnValue = false; + } + + if(xType != yType) + { + if(xType == DT_DIR) + returnValue = true; + else + returnValue = false; + } + + return returnValue; +} + +void modelCDMProjectsTree::SetRoot(std::string path) +{ + std::stringstream p(path); + std::vector breadcrumbs; + std::string name; + + while(!p.eof()) + { + getline(p,name,'/'); + if(name != "") + breadcrumbs.push_back(name); + } + + path = "/"; + for (int i = 0; i < breadcrumbs.size()-1; i++) + { + path += breadcrumbs[i] + "/"; + } + name = breadcrumbs[breadcrumbs.size()-1]; + + bool projectFound = false; + if(projectRoot.GetName() == name) + projectFound = true; + + if(!projectFound) + { + this->projectRoot = modelCDMProjectsTreeNode(path,name,DT_DIR,0); + }else{ + std::cout << "already existing "; + } + + std::cout << "project root set: " << name << " in " << path << std::endl; +} + +void modelCDMProjectsTree::populateNode(modelCDMProjectsTreeNode& node) +{ + //std::cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << std::endl; + std::vector * nodes = new std::vector ; + + std::string path = node.GetPath()+node.GetName()+"/"; + + wxDir dir(wxString(path.c_str())); + if (!dir.IsOpened()) + { + std::cerr << "Couldn't open the directory" << std::endl; + return; + } + + wxString fileName; + bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS); + while (cont) + { + modelCDMProjectsTreeNode innerNode = modelCDMProjectsTreeNode(path, crea::wx2std(fileName), wxDIR_DIRS, node.GetLevel()+1); + this->populateNode(innerNode); + nodes->push_back(innerNode); + cont = dir.GetNext(&fileName); + } + + cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES); + while (cont) + { + modelCDMProjectsTreeNode innerNode = modelCDMProjectsTreeNode(path, crea::wx2std(fileName), wxDIR_FILES, node.GetLevel()+1); + nodes->push_back(innerNode); + cont = dir.GetNext(&fileName); + } + + sort (nodes->begin(), nodes->end(), CompareNodeItem); + node.SetChildren(*nodes); + + /* + + DIR *dp; + struct dirent *ep; + + std::string path = node.GetPath()+node.GetName()+"/"; + + dp = opendir(path.c_str()); + if (dp != NULL) + { + while ((ep = readdir (dp)) != NULL) + { + //std::cout << ep->d_name << std::endl; + if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 ) + { + ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1); + if (ep->d_type == DT_DIR) + { + this->populateNode(innerNode); + } + + nodes->push_back(innerNode); + } + + } + + (void) closedir (dp); + + sort (nodes->begin(), nodes->end(), CompareNodeItem); + + node.SetChildren(*nodes); + } + else + { + std::cerr << "Couldn't open the directory" << std::endl; + }*/ +} + +void modelCDMProjectsTree::populateNode(std::string path) +{ + if(path[path.size()-1] != '/') + path+="/"; + std::cout << "searching " << path << std::endl; + if(this->projectRoot.GetPath()+this->projectRoot.GetName()+"/" == path) + { + std::cout << "Populating Project: " << path << "..." << std::endl; + this->populateNode(this->projectRoot); + } +}