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