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