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