]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMFolder.cpp
28d7a9581e7a96479c013b2dc444e60012393c25
[crea.git] / lib / creaDevManagerLib / modelCDMFolder.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  * modelCDMFolder.cpp
30  *
31  *  Created on: Nov 28, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMFolder.h"
36
37 #include <fstream>
38 #include <algorithm>
39
40 #include <creaWx.h>
41 #include <wx/dir.h>
42
43 #include "CDMUtilities.h"
44
45 modelCDMFolder::modelCDMFolder()
46 {
47   this->CMakeLists = NULL;
48 }
49
50 modelCDMFolder::modelCDMFolder(const std::string& path, const int& level)
51 {
52   //set attributes
53   this->children.clear();
54   this->level = level;
55   this->CMakeLists = NULL;
56   this->length = 0;
57
58   std::vector<std::string> words;
59   std::string delimiters;
60   //TODO::fix for windows
61   delimiters = "/";
62   CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
63   this->name = words[words.size()-1];
64
65   this->path = path;
66   this->type = wxDIR_DIRS;
67
68   std::string pathFixed(CDMUtilities::fixPath(path));
69   //check all folders
70   wxDir dir(crea::std2wx((pathFixed).c_str()));
71   if (dir.IsOpened())
72     {
73       wxString fileName;
74       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
75       while (cont)
76         {
77           std::string stdfileName = crea::wx2std(fileName);
78
79           //if is an unknown folder, create folder
80           this->children.push_back(new modelCDMFolder(pathFixed + "/" + stdfileName, this->level + 1));
81
82           cont = dir.GetNext(&fileName);
83         }
84
85       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
86       while (cont)
87         {
88           std::string stdfileName = crea::wx2std(fileName);
89
90           //if CMakeLists, create CMakeLists
91           if(stdfileName == "CMakeLists.txt")
92             {
93               this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + "/" + stdfileName, this->level + 1);
94               this->children.push_back(this->CMakeLists);
95             }
96           else
97             {
98               this->children.push_back(new modelCDMFile(pathFixed + "/" + stdfileName, this->level + 1));
99             }
100           //if is an unknown file, create file
101           cont = dir.GetNext(&fileName);
102         }
103     }
104
105   this->SortChildren();
106 }
107
108 modelCDMFolder::~modelCDMFolder()
109 {
110   this->folders.clear();
111   this->CMakeLists = NULL;
112   for (int i = 0; i < this->children.size(); i++)
113     {
114       if(this->children[i] != NULL)
115         {
116           delete this->children[i];
117           this->children[i] = NULL;
118         }
119     }
120   this->children.clear();
121 }
122
123 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
124 {
125   //TODO:: mkdir depending on OS
126   std::string command = "mkdir " + path + "/" + name;
127   if(system(command.c_str()))
128     {
129       result = new std::string("Error executing: " + command + ".");
130       return NULL;
131     }
132   modelCDMFolder* folder = new modelCDMFolder(path + "/" + name, level + 1);
133   this->folders.push_back(folder);
134   this->children.push_back(folder);
135
136   return folder;
137 }
138
139 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
140 {
141   if (this->CMakeLists == NULL)
142     {
143       result = new std::string("There's no CMakeLists file to open.");
144       return false;
145     }
146   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
147     return true;
148   else
149     {
150       result = new std::string("Couldn't open CMakeLists file.");
151       return false;
152     }
153 }
154
155 const bool modelCDMFolder::Refresh(std::string*& result)
156 {
157   //set attributes
158   this->type = wxDIR_DIRS;
159
160   //check children
161   std::vector<bool> checked(this->children.size(), false);
162   std::vector<bool> checkedFolders(this->folders.size(), false);
163
164   //check all folders
165   wxDir dir(crea::std2wx((this->path).c_str()));
166   if (dir.IsOpened())
167     {
168       wxString fileName;
169       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
170       while (cont)
171         {
172           std::string stdfileName = crea::wx2std(fileName);
173           std::string folderName = stdfileName;
174           //check if they already exist
175           bool found = false;
176           for (int i = 0;!found && i < this->folders.size(); i++)
177             {
178               if (this->folders[i]->GetName() == folderName)
179                 {
180                   found = true;
181                   int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
182                   checked[pos] = true;
183                   checkedFolders[i] = true;
184                   if(!this->folders[i]->Refresh(result))
185                     return false;
186                 }
187             }
188           if(!found)
189             {
190               modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
191               this->folders.push_back(folder);
192               this->children.push_back(folder);
193             }
194           cont = dir.GetNext(&fileName);
195         }
196
197       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
198       while (cont)
199         {
200           std::string stdfileName = crea::wx2std(fileName);
201
202           //if CMakeLists, create CMakeLists
203           if(stdfileName == "CMakeLists.txt")
204             {
205               if (this->CMakeLists == NULL)
206                 {
207                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
208                   this->children.push_back(this->CMakeLists);
209                 }
210               else
211                 {
212                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
213                   checked[pos] = true;
214                   if(!this->CMakeLists->Refresh(result))
215                     return false;
216                 }
217             }
218           //if is an unknown file, create file
219           else
220             {
221               bool found = false;
222               for (int i = 0; i <!found && this->children.size(); i++)
223                 {
224                   if (this->children[i]->GetName() == stdfileName)
225                     {
226                       found = true;
227                       checked[i] = true;
228                       if(!this->children[i]->Refresh(result))
229                         return false;
230                     }
231                 }
232
233               if(!found)
234                 {
235                   modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
236                   this->children.push_back(file);
237                 }
238             }
239
240           cont = dir.GetNext(&fileName);
241         }
242     }
243
244   for (int i = 0; i < checkedFolders.size(); i++)
245     {
246       if(!checkedFolders[i])
247         {
248           this->folders.erase(this->folders.begin()+i);
249           checkedFolders.erase(checkedFolders.begin()+i);
250           i--;
251         }
252     }
253   for (int i = 0; i < checked.size(); i++)
254     {
255       if(!checked[i])
256         {
257           delete this->children[i];
258           this->children.erase(this->children.begin()+i);
259           checked.erase(checked.begin()+i);
260           i--;
261         }
262     }
263   this->SortChildren();
264   return true;
265 }
266
267 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
268 {
269   return this->CMakeLists;
270 }
271
272 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
273 {
274   return this->folders;
275 }
276
277 bool
278 modelCDMFolder::HasCMakeLists()
279 {
280   return this->CMakeLists != NULL;
281 }