2 * ModelCreaDevManagerTree.cpp
4 * Created on: 22/10/2012
8 #include "ModelCreaDevManagerTree.h"
19 ModelCreaDevManagerTree::ModelCreaDevManagerTree()
23 ModelCreaDevManagerTree::~ModelCreaDevManagerTree()
27 bool ModelCreaDevManagerTree::CompareNodeItem(ModelCreaDevManagerTreeNode x, ModelCreaDevManagerTreeNode y)
32 std::string xName = x.GetName();
33 std::string yName = y.GetName();
34 unsigned char xType = x.GetType();
35 unsigned char yType = y.GetType();
37 while ((i < xName.length()) && (i < yName.length()))
39 if (tolower (xName[i]) < tolower (yName[i]))
45 else if (tolower (xName[i]) > tolower (yName[i]))
56 if (xName.length() < yName.length())
73 void ModelCreaDevManagerTree::addRoot(std::string path)
75 std::stringstream p(path);
76 std::vector<std::string> breadcrumbs;
83 breadcrumbs.push_back(name);
87 for (int i = 0; i < breadcrumbs.size()-1; i++)
89 path += breadcrumbs[i] + "/";
91 name = breadcrumbs[breadcrumbs.size()-1];
93 bool projectFound = false;
94 for (int i = 0; i < this->projectRoots.size(); i++)
96 if(this->projectRoots[i].GetName() == name)
102 this->projectRoots.push_back(ModelCreaDevManagerTreeNode(path,name,DT_DIR,0));
104 std::cout << "already existing ";
107 std::cout << "project root added: " << name << " in " << path << std::endl;
110 void ModelCreaDevManagerTree::populateNode(ModelCreaDevManagerTreeNode& node)
112 //std::cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << std::endl;
113 std::vector <ModelCreaDevManagerTreeNode>* nodes = new std::vector <ModelCreaDevManagerTreeNode>;
115 std::string path = node.GetPath()+node.GetName()+"/";
117 wxDir dir(wxString(path.c_str()));
120 std::cerr << "Couldn't open the directory" << std::endl;
125 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
128 ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, crea::wx2std(fileName), wxDIR_DIRS, node.GetLevel()+1);
129 this->populateNode(innerNode);
130 nodes->push_back(innerNode);
131 cont = dir.GetNext(&fileName);
134 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
137 ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, crea::wx2std(fileName), wxDIR_FILES, node.GetLevel()+1);
138 nodes->push_back(innerNode);
139 cont = dir.GetNext(&fileName);
142 sort (nodes->begin(), nodes->end(), CompareNodeItem);
143 node.SetChildren(*nodes);
150 std::string path = node.GetPath()+node.GetName()+"/";
152 dp = opendir(path.c_str());
155 while ((ep = readdir (dp)) != NULL)
157 //std::cout << ep->d_name << std::endl;
158 if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
160 ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
161 if (ep->d_type == DT_DIR)
163 this->populateNode(innerNode);
166 nodes->push_back(innerNode);
171 (void) closedir (dp);
173 sort (nodes->begin(), nodes->end(), CompareNodeItem);
175 node.SetChildren(*nodes);
179 std::cerr << "Couldn't open the directory" << std::endl;
183 void ModelCreaDevManagerTree::populateNode(std::string path)
185 if(path[path.size()-1] != '/')
187 std::cout << "searching " << path << std::endl;
188 for (int i = 0; i < this->projectRoots.size(); i++)
190 //std::cout << (this->projectRoots[i].GetPath() + this->projectRoots[i].GetName() + "/") << "..." << std::endl;
191 if(this->projectRoots[i].GetPath()+this->projectRoots[i].GetName()+"/" == path)
193 std::cout << "Populating Project: " << path << "..." << std::endl;
194 this->populateNode(this->projectRoots[i]);