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