2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
31 * Created on: Nov 28, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMFolder.h"
43 #include "CDMUtilities.h"
45 modelCDMFolder::modelCDMFolder()
47 this->CMakeLists = NULL;
50 modelCDMFolder::modelCDMFolder(const std::string& path, const std::string& name, const int& level)
52 std::cout << "creating folder: " + path + "\n";
54 this->children.clear();
56 this->CMakeLists = NULL;
60 this->type = wxDIR_DIRS;
62 std::string pathFixed(CDMUtilities::fixPath(path));
64 wxDir dir(crea::std2wx((pathFixed).c_str()));
68 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
71 std::string stdfileName = crea::wx2std(fileName);
73 //if is an unknown folder, create folder
74 modelCDMFolder* folder = new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
75 this->children.push_back(folder);
76 this->folders.push_back(folder);
78 cont = dir.GetNext(&fileName);
81 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
84 std::string stdfileName = crea::wx2std(fileName);
86 //if CMakeLists, create CMakeLists
87 if(stdfileName == "CMakeLists.txt")
89 this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
90 this->children.push_back(this->CMakeLists);
94 this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
96 //if is an unknown file, create file
97 cont = dir.GetNext(&fileName);
101 this->SortChildren();
102 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
105 modelCDMFolder::~modelCDMFolder()
107 this->folders.clear();
108 this->CMakeLists = NULL;
109 for (int i = 0; i < this->children.size(); i++)
111 if(this->children[i] != NULL)
113 delete this->children[i];
114 this->children[i] = NULL;
117 this->children.clear();
120 bool modelCDMFolder::CreateClass(const std::string& name)
122 if (!CDMUtilities::createEmptyClass(name, this->path))
128 this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + name + ".h", name + ".h", this->level + 1));
129 this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + name + ".cpp", name + ".cpp", this->level + 1));
130 this->SortChildren();
135 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
137 //TODO:: mkdir depending on OS
138 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
139 if(system(command.c_str()))
141 result = new std::string("Error executing: " + command + ".");
144 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
145 this->folders.push_back(folder);
146 this->children.push_back(folder);
151 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
153 if (this->CMakeLists == NULL)
155 result = new std::string("There's no CMakeLists file to open.");
158 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
162 result = new std::string("Couldn't open CMakeLists file.");
167 const bool modelCDMFolder::Refresh(std::string*& result)
169 //std::cout << "refreshing folder " << this->name << std::endl;
171 this->type = wxDIR_DIRS;
174 std::vector<bool> checked(this->children.size(), false);
175 std::vector<bool> checkedFolders(this->folders.size(), false);
178 wxDir dir(crea::std2wx((this->path).c_str()));
182 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
185 std::string stdfileName = crea::wx2std(fileName);
186 //check if they already exist
188 for (int i = 0; !found && i < this->folders.size(); i++)
190 if (this->folders[i]->GetName() == stdfileName)
193 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
195 checkedFolders[i] = true;
196 if(!this->folders[i]->Refresh(result))
202 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
203 this->folders.push_back(folder);
204 this->children.push_back(folder);
206 cont = dir.GetNext(&fileName);
209 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
212 std::string stdfileName = crea::wx2std(fileName);
214 //if CMakeLists, create CMakeLists
215 if(stdfileName == "CMakeLists.txt")
217 if (this->CMakeLists == NULL)
219 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
220 this->children.push_back(this->CMakeLists);
224 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
226 if(!this->CMakeLists->Refresh(result))
231 //if is an unknown file, create file
235 for (int i = 0;!found && i < this->children.size(); i++)
237 if (this->children[i]->GetName() == stdfileName)
241 if(!this->children[i]->Refresh(result))
248 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
249 this->children.push_back(file);
253 cont = dir.GetNext(&fileName);
257 for (int i = 0; i < checkedFolders.size(); i++)
259 if(!checkedFolders[i])
261 this->folders.erase(this->folders.begin()+i);
262 checkedFolders.erase(checkedFolders.begin()+i);
266 for (int i = 0; i < checked.size(); i++)
270 delete this->children[i];
271 this->children.erase(this->children.begin()+i);
272 checked.erase(checked.begin()+i);
276 this->SortChildren();
277 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
281 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
283 return this->CMakeLists;
286 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
288 return this->folders;
292 modelCDMFolder::HasCMakeLists()
294 return this->CMakeLists != NULL;