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