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(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
52 std::cout << "creating folder: " + path + "\n";
54 this->parent = parent;
55 this->children.clear();
57 this->CMakeLists = NULL;
61 this->type = wxDIR_DIRS;
63 std::string pathFixed(CDMUtilities::fixPath(path));
65 wxDir dir(crea::std2wx((pathFixed).c_str()));
69 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
72 std::string stdfileName = crea::wx2std(fileName);
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);
79 cont = dir.GetNext(&fileName);
82 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
85 std::string stdfileName = crea::wx2std(fileName);
86 std::cout << "analyzing " << stdfileName << std::endl;
87 std::size_t fileTypePos = stdfileName.find_last_of(".");
89 if(fileTypePos != std::string::npos)
90 fileType = stdfileName.substr(fileTypePos);
93 std::cout << "fileType: " << fileType <<std::endl;
95 //if CMakeLists, create CMakeLists
96 if(stdfileName == "CMakeLists.txt")
98 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
99 this->children.push_back(this->CMakeLists);
101 //if is a code file, create code file
102 else if(fileType == ".c" ||
103 fileType == ".cxx" ||
105 fileType == ".cpp" ||
106 fileType == ".txx" ||
107 fileType == ".cmake" )
109 modelCDMCodeFile* file = new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
110 this->children.push_back(file);
112 //if is an unknown file, create file
115 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
118 cont = dir.GetNext(&fileName);
122 this->SortChildren();
123 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
126 modelCDMFolder::~modelCDMFolder()
128 this->folders.clear();
129 this->CMakeLists = NULL;
130 for (int i = 0; i < (int)(this->children.size()); i++)
132 if(this->children[i] != NULL)
134 delete this->children[i];
135 this->children[i] = NULL;
138 this->children.clear();
141 bool modelCDMFolder::CreateClass(const std::string& name)
143 if (!CDMUtilities::createEmptyClass(name, this->path))
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();
156 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
158 //TODO:: mkdir depending on OS
159 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
160 if(system(command.c_str()))
162 result = new std::string("Error executing: " + command + ".");
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);
172 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
174 if (this->CMakeLists == NULL)
176 result = new std::string("There's no CMakeLists file to open.");
179 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
183 result = new std::string("Couldn't open CMakeLists file.");
188 const bool modelCDMFolder::Refresh(std::string*& result)
190 //std::cout << "refreshing folder " << this->name << std::endl;
192 this->type = wxDIR_DIRS;
195 std::vector<bool> checked(this->children.size(), false);
196 std::vector<bool> checkedFolders(this->folders.size(), false);
199 wxDir dir(crea::std2wx((this->path).c_str()));
203 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
206 std::string stdfileName = crea::wx2std(fileName);
207 //check if they already exist
209 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
211 if (this->folders[i]->GetName() == stdfileName)
214 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
216 checkedFolders[i] = true;
217 if(!this->folders[i]->Refresh(result))
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);
227 cont = dir.GetNext(&fileName);
230 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
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);
241 //if CMakeLists, create CMakeLists
242 if(stdfileName == "CMakeLists.txt")
244 if (this->CMakeLists == NULL)
246 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
247 this->children.push_back(this->CMakeLists);
251 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
253 if(!this->CMakeLists->Refresh(result))
258 //if is an unknown file, create file
262 for (int i = 0;!found && i < (int)(this->children.size()); i++)
264 if (this->children[i]->GetName() == stdfileName)
268 if(!this->children[i]->Refresh(result))
275 //if is a code file, create modelCDMCodeFile
278 fileType == ".cxx" ||
280 fileType == ".cpp" ||
281 fileType == ".txx" ||
282 fileType == ".cmake" )
284 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
288 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
289 this->children.push_back(file);
294 cont = dir.GetNext(&fileName);
298 for (int i = 0; i < (int)(checkedFolders.size()); i++)
300 if(!checkedFolders[i])
302 this->folders.erase(this->folders.begin()+i);
303 checkedFolders.erase(checkedFolders.begin()+i);
307 for (int i = 0; i < (int)(checked.size()); i++)
311 delete this->children[i];
312 this->children.erase(this->children.begin()+i);
313 checked.erase(checked.begin()+i);
317 this->SortChildren();
318 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
322 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
324 return this->CMakeLists;
327 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
329 return this->folders;
333 modelCDMFolder::HasCMakeLists()
335 return this->CMakeLists != NULL;