/* * ModelCreaDevManagerTree.cpp * * Created on: 22/10/2012 * Author: daniel */ #include "ModelCreaDevManagerTree.h" #include #include #include #include #include #include #include ModelCreaDevManagerTree::ModelCreaDevManagerTree() { } ModelCreaDevManagerTree::~ModelCreaDevManagerTree() { } bool ModelCreaDevManagerTree::CompareNodeItem(ModelCreaDevManagerTreeNode x, ModelCreaDevManagerTreeNode 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 ModelCreaDevManagerTree::addRoot(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; for (int i = 0; i < this->projectRoots.size(); i++) { if(this->projectRoots[i].GetName() == name) projectFound = true; } if(!projectFound) { this->projectRoots.push_back(ModelCreaDevManagerTreeNode(path,name,DT_DIR,0)); }else{ std::cout << "already existing "; } std::cout << "project root added: " << name << " in " << path << std::endl; } void ModelCreaDevManagerTree::populateNode(ModelCreaDevManagerTreeNode& 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) { ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(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) { ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(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 ModelCreaDevManagerTree::populateNode(std::string path) { if(path[path.size()-1] != '/') path+="/"; std::cout << "searching " << path << std::endl; for (int i = 0; i < this->projectRoots.size(); i++) { //std::cout << (this->projectRoots[i].GetPath() + this->projectRoots[i].GetName() + "/") << "..." << std::endl; if(this->projectRoots[i].GetPath()+this->projectRoots[i].GetName()+"/" == path) { std::cout << "Populating Project: " << path << "..." << std::endl; this->populateNode(this->projectRoots[i]); break; } } }