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)
53 this->children.clear();
55 this->CMakeLists = NULL;
59 this->type = wxDIR_DIRS;
61 std::string pathFixed(CDMUtilities::fixPath(path));
63 wxDir dir(crea::std2wx((pathFixed).c_str()));
67 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
70 std::string stdfileName = crea::wx2std(fileName);
72 //if is an unknown folder, create folder
73 this->children.push_back(new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
75 cont = dir.GetNext(&fileName);
78 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
81 std::string stdfileName = crea::wx2std(fileName);
83 //if CMakeLists, create CMakeLists
84 if(stdfileName == "CMakeLists.txt")
86 this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
87 this->children.push_back(this->CMakeLists);
91 this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
93 //if is an unknown file, create file
94 cont = dir.GetNext(&fileName);
101 modelCDMFolder::~modelCDMFolder()
103 this->folders.clear();
104 this->CMakeLists = NULL;
105 for (int i = 0; i < this->children.size(); i++)
107 if(this->children[i] != NULL)
109 delete this->children[i];
110 this->children[i] = NULL;
113 this->children.clear();
116 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
118 //TODO:: mkdir depending on OS
119 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
120 if(system(command.c_str()))
122 result = new std::string("Error executing: " + command + ".");
125 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
126 this->folders.push_back(folder);
127 this->children.push_back(folder);
132 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
134 if (this->CMakeLists == NULL)
136 result = new std::string("There's no CMakeLists file to open.");
139 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
143 result = new std::string("Couldn't open CMakeLists file.");
148 const bool modelCDMFolder::Refresh(std::string*& result)
151 this->type = wxDIR_DIRS;
154 std::vector<bool> checked(this->children.size(), false);
155 std::vector<bool> checkedFolders(this->folders.size(), false);
158 wxDir dir(crea::std2wx((this->path).c_str()));
162 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
165 std::string stdfileName = crea::wx2std(fileName);
166 std::string folderName = stdfileName;
167 //check if they already exist
169 for (int i = 0;!found && i < this->folders.size(); i++)
171 if (this->folders[i]->GetName() == folderName)
174 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
176 checkedFolders[i] = true;
177 if(!this->folders[i]->Refresh(result))
183 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
184 this->folders.push_back(folder);
185 this->children.push_back(folder);
187 cont = dir.GetNext(&fileName);
190 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
193 std::string stdfileName = crea::wx2std(fileName);
195 //if CMakeLists, create CMakeLists
196 if(stdfileName == "CMakeLists.txt")
198 if (this->CMakeLists == NULL)
200 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
201 this->children.push_back(this->CMakeLists);
205 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
207 if(!this->CMakeLists->Refresh(result))
212 //if is an unknown file, create file
216 for (int i = 0; i <!found && this->children.size(); i++)
218 if (this->children[i]->GetName() == stdfileName)
222 if(!this->children[i]->Refresh(result))
229 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
230 this->children.push_back(file);
234 cont = dir.GetNext(&fileName);
238 for (int i = 0; i < checkedFolders.size(); i++)
240 if(!checkedFolders[i])
242 this->folders.erase(this->folders.begin()+i);
243 checkedFolders.erase(checkedFolders.begin()+i);
247 for (int i = 0; i < checked.size(); i++)
251 delete this->children[i];
252 this->children.erase(this->children.begin()+i);
253 checked.erase(checked.begin()+i);
257 this->SortChildren();
261 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
263 return this->CMakeLists;
266 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
268 return this->folders;
272 modelCDMFolder::HasCMakeLists()
274 return this->CMakeLists != NULL;