]> Creatis software - crea.git/blob - lib/creaDevManagerLib/ModelCreaDevManagerTree.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / ModelCreaDevManagerTree.cpp
1 /*
2  * ModelCreaDevManagerTree.cpp
3  *
4  *  Created on: 22/10/2012
5  *      Author: daniel
6  */
7
8 #include "ModelCreaDevManagerTree.h"
9
10 #include <iostream>
11 #include <sstream>
12 #include <algorithm>
13 #include <string>
14 #include <cstring>
15 #include <creaWx.h>
16 #include <wx/dir.h>
17
18
19 ModelCreaDevManagerTree::ModelCreaDevManagerTree()
20 {
21 }
22
23 ModelCreaDevManagerTree::~ModelCreaDevManagerTree()
24 {
25 }
26
27 bool ModelCreaDevManagerTree::CompareNodeItem(ModelCreaDevManagerTreeNode x, ModelCreaDevManagerTreeNode y)
28 {
29   bool returnValue;
30   bool noWinner = true;
31   unsigned int i = 0;
32   std::string xName = x.GetName();
33   std::string yName = y.GetName();
34   unsigned char xType = x.GetType();
35   unsigned char yType = y.GetType();
36
37   while ((i < xName.length()) && (i < yName.length()))
38   {
39     if (tolower (xName[i]) < tolower (yName[i]))
40     {
41       noWinner = false;
42       returnValue = true;
43       break;
44     }
45     else if (tolower (xName[i]) > tolower (yName[i]))
46     {
47       noWinner = false;
48       returnValue = false;
49       break;
50     }
51     i++;
52   }
53
54   if(noWinner)
55   {
56     if (xName.length() < yName.length())
57       returnValue = true;
58     else
59       returnValue = false;
60   }
61
62   if(xType != yType)
63   {
64     if(xType == DT_DIR)
65       returnValue = true;
66     else
67       returnValue = false;
68   }
69
70   return returnValue;
71 }
72
73 void ModelCreaDevManagerTree::addRoot(std::string path)
74 {
75   std::stringstream p(path);
76   std::vector<std::string> breadcrumbs;
77   std::string name;
78
79   while(!p.eof())
80   {
81     getline(p,name,'/');
82     if(name != "")
83       breadcrumbs.push_back(name);
84   }
85
86   path = "/";
87   for (int i = 0; i < breadcrumbs.size()-1; i++)
88   {
89     path += breadcrumbs[i] + "/";
90   }
91   name = breadcrumbs[breadcrumbs.size()-1];
92   this->projectRoots.push_back(ModelCreaDevManagerTreeNode(path,name,DT_DIR,0));
93
94   std::cout << "project root added: " << name << " in " << path << std::endl;
95 }
96
97 void ModelCreaDevManagerTree::populateNode(ModelCreaDevManagerTreeNode& node)
98 {
99   //std::cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << std::endl;
100   std::vector <ModelCreaDevManagerTreeNode>*  nodes = new std::vector <ModelCreaDevManagerTreeNode>;
101
102   std::string path = node.GetPath()+node.GetName()+"/";
103
104   wxDir dir(wxString(path.c_str()));
105   if (!dir.IsOpened())
106   {
107     std::cerr << "Couldn't open the directory" << std::endl;
108     return;
109   }
110
111   wxString fileName;
112   bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
113   while (cont)
114   {
115     ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, crea::wx2std(fileName), wxDIR_DIRS, node.GetLevel()+1);
116     this->populateNode(innerNode);
117     nodes->push_back(innerNode);
118     cont = dir.GetNext(&fileName);
119   }
120
121   cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
122   while (cont)
123   {
124     ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, crea::wx2std(fileName), wxDIR_FILES, node.GetLevel()+1);
125     nodes->push_back(innerNode);
126     cont = dir.GetNext(&fileName);
127   }
128
129   sort (nodes->begin(), nodes->end(), CompareNodeItem);
130   node.SetChildren(*nodes);
131
132   /*
133
134   DIR *dp;
135   struct dirent *ep;
136
137   std::string path = node.GetPath()+node.GetName()+"/";
138
139   dp = opendir(path.c_str());
140   if (dp != NULL)
141   {
142     while ((ep = readdir (dp)) != NULL)
143     {
144       //std::cout << ep->d_name << std::endl;
145       if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
146       {
147         ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
148         if (ep->d_type == DT_DIR)
149         {
150           this->populateNode(innerNode);
151         }
152
153         nodes->push_back(innerNode);
154       }
155
156     }
157
158     (void) closedir (dp);
159
160     sort (nodes->begin(), nodes->end(), CompareNodeItem);
161
162     node.SetChildren(*nodes);
163   }
164   else
165   {
166     std::cerr << "Couldn't open the directory" << std::endl;
167   }*/
168 }
169
170 void ModelCreaDevManagerTree::populateNode(std::string path)
171 {
172   if(path[path.size()-1] != '/')
173     path+="/";
174   std::cout << "searching " << path << std::endl;
175   for (int i = 0; i < this->projectRoots.size(); i++)
176   {
177     //std::cout << (this->projectRoots[i].GetPath() + this->projectRoots[i].GetName() + "/") << "..." << std::endl;
178     if(this->projectRoots[i].GetPath()+this->projectRoots[i].GetName()+"/" == path)
179     {
180         std::cout << "Populating Project: " << path << "..." << std::endl;
181       this->populateNode(this->projectRoots[i]);
182       break;
183     }
184   }
185 }