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