2 * ModelCreaDevManagerTree.cpp
4 * Created on: 22/10/2012
8 #include "modelCDMProjectsTree.h"
16 modelCDMProjectsTree::modelCDMProjectsTree()
20 modelCDMProjectsTree::~modelCDMProjectsTree()
24 bool modelCDMProjectsTree::CompareNodeItem(modelCDMProjectsTreeNode x, modelCDMProjectsTreeNode y)
29 std::string xName = x.GetName();
30 std::string yName = y.GetName();
31 unsigned char xType = x.GetType();
32 unsigned char yType = y.GetType();
34 while ((i < xName.length()) && (i < yName.length()))
36 if (tolower (xName[i]) < tolower (yName[i]))
42 else if (tolower (xName[i]) > tolower (yName[i]))
53 if (xName.length() < yName.length())
70 void modelCDMProjectsTree::SetRoot(std::string path)
72 std::stringstream p(path);
73 std::vector<std::string> breadcrumbs;
80 breadcrumbs.push_back(name);
84 for (int i = 0; i < breadcrumbs.size()-1; i++)
86 path += breadcrumbs[i] + "/";
88 name = breadcrumbs[breadcrumbs.size()-1];
90 bool projectFound = false;
91 if(projectRoot.GetName() == name)
96 this->projectRoot = modelCDMProjectsTreeNode(path,name,DT_DIR,0);
98 std::cout << "already existing ";
101 std::cout << "project root set: " << name << " in " << path << std::endl;
104 void modelCDMProjectsTree::populateNode(modelCDMProjectsTreeNode& node)
106 //std::cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << std::endl;
107 std::vector <modelCDMProjectsTreeNode>* nodes = new std::vector <modelCDMProjectsTreeNode>;
109 std::string path = node.GetPath()+node.GetName()+"/";
111 wxDir dir(wxString(path.c_str()));
114 std::cerr << "Couldn't open the directory" << std::endl;
119 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
122 modelCDMProjectsTreeNode innerNode = modelCDMProjectsTreeNode(path, crea::wx2std(fileName), wxDIR_DIRS, node.GetLevel()+1);
123 this->populateNode(innerNode);
124 nodes->push_back(innerNode);
125 cont = dir.GetNext(&fileName);
128 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
131 modelCDMProjectsTreeNode innerNode = modelCDMProjectsTreeNode(path, crea::wx2std(fileName), wxDIR_FILES, node.GetLevel()+1);
132 nodes->push_back(innerNode);
133 cont = dir.GetNext(&fileName);
136 sort (nodes->begin(), nodes->end(), CompareNodeItem);
137 node.SetChildren(*nodes);
144 std::string path = node.GetPath()+node.GetName()+"/";
146 dp = opendir(path.c_str());
149 while ((ep = readdir (dp)) != NULL)
151 //std::cout << ep->d_name << std::endl;
152 if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
154 ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
155 if (ep->d_type == DT_DIR)
157 this->populateNode(innerNode);
160 nodes->push_back(innerNode);
165 (void) closedir (dp);
167 sort (nodes->begin(), nodes->end(), CompareNodeItem);
169 node.SetChildren(*nodes);
173 std::cerr << "Couldn't open the directory" << std::endl;
177 void modelCDMProjectsTree::populateNode(std::string path)
179 if(path[path.size()-1] != '/')
181 std::cout << "searching " << path << std::endl;
182 if(this->projectRoot.GetPath()+this->projectRoot.GetName()+"/" == path)
184 std::cout << "Populating Project: " << path << "..." << std::endl;
185 this->populateNode(this->projectRoot);