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