]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProjectsTree.cpp
338c90cca069c07234cefe7a9680f73286bd3050
[crea.git] / lib / creaDevManagerLib / modelCDMProjectsTree.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */ 
24
25
26 /*
27  * ModelCreaDevManagerTree.cpp
28  *
29  *  Created on: 22/10/2012
30  *      Author: daniel
31  */
32
33 #include "modelCDMProjectsTree.h"
34
35 #include <creaWx.h>
36 #include <wx/dir.h>
37
38 #include <sstream>
39 #include <algorithm>
40
41 modelCDMProjectsTree::modelCDMProjectsTree()
42 {
43 }
44
45 modelCDMProjectsTree::~modelCDMProjectsTree()
46 {
47 }
48
49 bool modelCDMProjectsTree::CompareNodeItem(modelCDMProjectsTreeNode x, modelCDMProjectsTreeNode y)
50 {
51   bool returnValue;
52   bool noWinner = true;
53   unsigned int i = 0;
54   std::string xName = x.GetName();
55   std::string yName = y.GetName();
56   unsigned char xType = x.GetType();
57   unsigned char yType = y.GetType();
58
59   while ((i < xName.length()) && (i < yName.length()))
60   {
61     if (tolower (xName[i]) < tolower (yName[i]))
62     {
63       noWinner = false;
64       returnValue = true;
65       break;
66     }
67     else if (tolower (xName[i]) > tolower (yName[i]))
68     {
69       noWinner = false;
70       returnValue = false;
71       break;
72     }
73     i++;
74   }
75
76   if(noWinner)
77   {
78     if (xName.length() < yName.length())
79       returnValue = true;
80     else
81       returnValue = false;
82   }
83
84   if(xType != yType)
85   {
86     if(xType == DT_DIR)
87       returnValue = true;
88     else
89       returnValue = false;
90   }
91
92   return returnValue;
93 }
94
95 void modelCDMProjectsTree::SetRoot(std::string path)
96 {
97   std::stringstream p(path);
98   std::vector<std::string> breadcrumbs;
99   std::string name;
100
101   while(!p.eof())
102   {
103     getline(p,name,'/');
104     if(name != "")
105       breadcrumbs.push_back(name);
106   }
107
108   path = "/";
109   for (int i = 0; i < breadcrumbs.size()-1; i++)
110   {
111     path += breadcrumbs[i] + "/";
112   }
113   name = breadcrumbs[breadcrumbs.size()-1];
114
115   bool projectFound = false;
116   if(projectRoot.GetName() == name)
117     projectFound = true;
118
119   if(!projectFound)
120   {
121     this->projectRoot = modelCDMProjectsTreeNode(path,name,DT_DIR,0);
122   }else{
123     std::cout << "already existing ";
124   }
125
126   std::cout << "project root set: " << name << " in " << path << std::endl;
127 }
128
129 void modelCDMProjectsTree::populateNode(modelCDMProjectsTreeNode& node)
130 {
131   //std::cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << std::endl;
132   std::vector <modelCDMProjectsTreeNode>*  nodes = new std::vector <modelCDMProjectsTreeNode>;
133
134   std::string path = node.GetPath()+node.GetName()+"/";
135
136   wxDir dir(wxString(path.c_str()));
137   if (!dir.IsOpened())
138   {
139     std::cerr << "Couldn't open the directory" << std::endl;
140     return;
141   }
142
143   wxString fileName;
144   bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
145   while (cont)
146   {
147       modelCDMProjectsTreeNode innerNode = modelCDMProjectsTreeNode(path, crea::wx2std(fileName), wxDIR_DIRS, node.GetLevel()+1);
148     this->populateNode(innerNode);
149     nodes->push_back(innerNode);
150     cont = dir.GetNext(&fileName);
151   }
152
153   cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
154   while (cont)
155   {
156     modelCDMProjectsTreeNode innerNode = modelCDMProjectsTreeNode(path, crea::wx2std(fileName), wxDIR_FILES, node.GetLevel()+1);
157     nodes->push_back(innerNode);
158     cont = dir.GetNext(&fileName);
159   }
160
161   sort (nodes->begin(), nodes->end(), CompareNodeItem);
162   node.SetChildren(*nodes);
163
164   /*
165
166   DIR *dp;
167   struct dirent *ep;
168
169   std::string path = node.GetPath()+node.GetName()+"/";
170
171   dp = opendir(path.c_str());
172   if (dp != NULL)
173   {
174     while ((ep = readdir (dp)) != NULL)
175     {
176       //std::cout << ep->d_name << std::endl;
177       if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
178       {
179         ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
180         if (ep->d_type == DT_DIR)
181         {
182           this->populateNode(innerNode);
183         }
184
185         nodes->push_back(innerNode);
186       }
187
188     }
189
190     (void) closedir (dp);
191
192     sort (nodes->begin(), nodes->end(), CompareNodeItem);
193
194     node.SetChildren(*nodes);
195   }
196   else
197   {
198     std::cerr << "Couldn't open the directory" << std::endl;
199   }*/
200 }
201
202 void modelCDMProjectsTree::populateNode(std::string path)
203 {
204   if(path[path.size()-1] != '/')
205     path+="/";
206   std::cout << "searching " << path << std::endl;
207   if(this->projectRoot.GetPath()+this->projectRoot.GetName()+"/" == path)
208   {
209       std::cout << "Populating Project: " << path << "..." << std::endl;
210     this->populateNode(this->projectRoot);
211   }
212 }