]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMFolder.cpp
cdd10068eacfdcaf1ad9357cd8dc3d268a804a59
[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           std::cout << "analyzing " << stdfileName << std::endl;
87           std::size_t fileTypePos = stdfileName.find_last_of(".");
88           std::string fileType;
89           if(fileTypePos != std::string::npos)
90             fileType = stdfileName.substr(fileTypePos);
91           else
92             fileType = "";
93           std::cout << "fileType: " << fileType <<std::endl;
94
95           //if CMakeLists, create CMakeLists
96           if(stdfileName == "CMakeLists.txt")
97             {
98               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
99               this->children.push_back(this->CMakeLists);
100             }
101           //if is a code file, create code file
102           else if(fileType == ".c" ||
103               fileType == ".cxx" ||
104               fileType == ".h" ||
105               fileType == ".cpp" ||
106               fileType == ".txx" ||
107               fileType == ".cmake" )
108             {
109               modelCDMCodeFile* file = new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
110               this->children.push_back(file);
111             }
112           else if(fileType == ".bbs")
113             {
114               modelCDMBBSFile* file = new modelCDMBBSFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
115               this->children.push_back(file);
116             }
117           //if is an unknown file, create file
118           else
119             {
120               this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
121             }
122
123           cont = dir.GetNext(&fileName);
124         }
125     }
126
127   this->SortChildren();
128   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
129 }
130
131 modelCDMFolder::~modelCDMFolder()
132 {
133   this->folders.clear();
134   this->CMakeLists = NULL;
135   for (int i = 0; i < (int)(this->children.size()); i++)
136     {
137       if(this->children[i] != NULL)
138         {
139           delete this->children[i];
140           this->children[i] = NULL;
141         }
142     }
143   this->children.clear();
144 }
145
146 bool modelCDMFolder::CreateClass(const std::string& name)
147 {
148   if (!CDMUtilities::createEmptyClass(name, this->path))
149       {
150         return false;
151       }
152     else
153       {
154         this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".h", name + ".h", this->level + 1));
155         this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".cpp", name + ".cpp", this->level + 1));
156         this->SortChildren();
157         return true;
158       }
159 }
160
161 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
162 {
163   //TODO:: mkdir depending on OS
164   std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
165   if(system(command.c_str()))
166     {
167       result = new std::string("Error executing: " + command + ".");
168       return NULL;
169     }
170   modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
171   this->folders.push_back(folder);
172   this->children.push_back(folder);
173
174   return folder;
175 }
176
177 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
178 {
179   if (this->CMakeLists == NULL)
180     {
181       result = new std::string("There's no CMakeLists file to open.");
182       return false;
183     }
184   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
185     return true;
186   else
187     {
188       result = new std::string("Couldn't open CMakeLists file.");
189       return false;
190     }
191 }
192
193 const bool modelCDMFolder::Refresh(std::string*& result)
194 {
195   //std::cout << "refreshing folder " << this->name << std::endl;
196   //set attributes
197   this->type = wxDIR_DIRS;
198
199   //check children
200   std::vector<bool> checked(this->children.size(), false);
201   std::vector<bool> checkedFolders(this->folders.size(), false);
202
203   //check all folders
204   wxDir dir(crea::std2wx((this->path).c_str()));
205   if (dir.IsOpened())
206     {
207       wxString fileName;
208       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
209       while (cont)
210         {
211           std::string stdfileName = crea::wx2std(fileName);
212           //check if they already exist
213           bool found = false;
214           for (int i = 0; !found && i < (int)(this->folders.size()); i++)
215             {
216               if (this->folders[i]->GetName() == stdfileName)
217                 {
218                   found = true;
219                   int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
220                   checked[pos] = true;
221                   checkedFolders[i] = true;
222                   if(!this->folders[i]->Refresh(result))
223                     return false;
224                 }
225             }
226           if(!found)
227             {
228               modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
229               this->folders.push_back(folder);
230               this->children.push_back(folder);
231             }
232           cont = dir.GetNext(&fileName);
233         }
234
235       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
236       while (cont)
237         {
238           std::string stdfileName = crea::wx2std(fileName);
239           std::size_t fileTypePos = stdfileName.find_last_of(".");
240           std::string fileType;
241           if(fileTypePos != std::string::npos)
242             fileType = stdfileName.substr(fileTypePos);
243           else
244             fileType = "";
245
246           //if CMakeLists, create CMakeLists
247           if(stdfileName == "CMakeLists.txt")
248             {
249               if (this->CMakeLists == NULL)
250                 {
251                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
252                   this->children.push_back(this->CMakeLists);
253                 }
254               else
255                 {
256                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
257                   checked[pos] = true;
258                   if(!this->CMakeLists->Refresh(result))
259                     return false;
260                 }
261             }
262
263           //if is an unknown file, create file
264           else
265             {
266               bool found = false;
267               for (int i = 0;!found && i < (int)(this->children.size()); i++)
268                 {
269                   if (this->children[i]->GetName() == stdfileName)
270                     {
271                       found = true;
272                       checked[i] = true;
273                       if(!this->children[i]->Refresh(result))
274                         return false;
275                     }
276                 }
277
278               if(!found)
279                 {
280                   //if is a code file, create modelCDMCodeFile
281                   if(
282                       fileType == ".c" ||
283                       fileType == ".cxx" ||
284                       fileType == ".h" ||
285                       fileType == ".cpp" ||
286                       fileType == ".txx" ||
287                       fileType == ".cmake" )
288                     {
289                       this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
290                     }
291                   else if(fileType == ".bbs")
292                     {
293                       this->children.push_back(new modelCDMBBSFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
294                     }
295                   else
296                     {
297                       modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
298                       this->children.push_back(file);
299                     }
300                 }
301             }
302
303           cont = dir.GetNext(&fileName);
304         }
305     }
306
307   for (int i = 0; i < (int)(checkedFolders.size()); i++)
308     {
309       if(!checkedFolders[i])
310         {
311           this->folders.erase(this->folders.begin()+i);
312           checkedFolders.erase(checkedFolders.begin()+i);
313           i--;
314         }
315     }
316   for (int i = 0; i < (int)(checked.size()); i++)
317     {
318       if(!checked[i])
319         {
320           delete this->children[i];
321           this->children.erase(this->children.begin()+i);
322           checked.erase(checked.begin()+i);
323           i--;
324         }
325     }
326   this->SortChildren();
327   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
328   return true;
329 }
330
331 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
332 {
333   return this->CMakeLists;
334 }
335
336 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
337 {
338   return this->folders;
339 }
340
341 bool
342 modelCDMFolder::HasCMakeLists()
343 {
344   return this->CMakeLists != NULL;
345 }