2 * ModelCreaDevManagerTree.cpp
4 * Created on: 22/10/2012
8 #include "ModelCreaDevManagerTree.h"
12 #include <sys/types.h>
20 ModelCreaDevManagerTree::ModelCreaDevManagerTree()
24 ModelCreaDevManagerTree::~ModelCreaDevManagerTree()
28 bool ModelCreaDevManagerTree::CompareNodeItem(ModelCreaDevManagerTreeNode x, ModelCreaDevManagerTreeNode y)
33 std::string xName = x.GetName();
34 std::string yName = y.GetName();
35 unsigned char xType = x.GetType();
36 unsigned char yType = y.GetType();
38 while ((i < xName.length()) && (i < yName.length()))
40 if (tolower (xName[i]) < tolower (yName[i]))
46 else if (tolower (xName[i]) > tolower (yName[i]))
57 if (xName.length() < yName.length())
74 void ModelCreaDevManagerTree::addRoot(std::string path)
76 std::stringstream p(path);
77 std::vector<std::string> breadcrumbs;
84 breadcrumbs.push_back(name);
88 for (int i = 0; i < breadcrumbs.size()-1; i++)
90 path += breadcrumbs[i] + "/";
92 name = breadcrumbs[breadcrumbs.size()-1];
93 this->projectRoots.push_back(ModelCreaDevManagerTreeNode(path,name,DT_DIR,0));
95 std::cout << "project root added: " << name << " in " << path << std::endl;
98 void ModelCreaDevManagerTree::populateNode(ModelCreaDevManagerTreeNode& node)
100 //std::cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << std::endl;
101 std::vector <ModelCreaDevManagerTreeNode>* nodes = new std::vector <ModelCreaDevManagerTreeNode>;
106 std::string path = node.GetPath()+node.GetName()+"/";
108 dp = opendir(path.c_str());
111 while ((ep = readdir (dp)) != NULL)
113 //std::cout << ep->d_name << std::endl;
114 if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
116 ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
117 if (ep->d_type == DT_DIR)
119 this->populateNode(innerNode);
122 nodes->push_back(innerNode);
127 (void) closedir (dp);
129 sort (nodes->begin(), nodes->end(), CompareNodeItem);
131 node.SetChildren(*nodes);
135 std::cerr << "Couldn't open the directory" << std::endl;
139 void ModelCreaDevManagerTree::populateNode(std::string path)
141 if(path[path.size()-1] != '/')
143 std::cout << "searching " << path << std::endl;
144 for (int i = 0; i < this->projectRoots.size(); i++)
146 //std::cout << (this->projectRoots[i].GetPath() + this->projectRoots[i].GetName() + "/") << "..." << std::endl;
147 if(this->projectRoots[i].GetPath()+this->projectRoots[i].GetName()+"/" == path)
149 std::cout << "Populating Project: " << path << "..." << std::endl;
150 this->populateNode(this->projectRoots[i]);