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