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::size_t fileTypePos = stdfileName.find_last_of(".");
87 std::string fileType = stdfileName.substr(fileTypePos);
89 //if CMakeLists, create CMakeLists
90 if(stdfileName == "CMakeLists.txt")
92 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
93 this->children.push_back(this->CMakeLists);
95 //if is a code file, create code file
96 else if(fileType == ".c" ||
100 fileType == ".txx" ||
101 fileType == ".cmake" )
103 modelCDMCodeFile* file = new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
104 this->children.push_back(file);
106 //if is an unknown file, create file
109 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
112 cont = dir.GetNext(&fileName);
116 this->SortChildren();
117 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
120 modelCDMFolder::~modelCDMFolder()
122 this->folders.clear();
123 this->CMakeLists = NULL;
124 for (int i = 0; i < (int)(this->children.size()); i++)
126 if(this->children[i] != NULL)
128 delete this->children[i];
129 this->children[i] = NULL;
132 this->children.clear();
135 bool modelCDMFolder::CreateClass(const std::string& name)
137 if (!CDMUtilities::createEmptyClass(name, this->path))
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();
150 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
152 //TODO:: mkdir depending on OS
153 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
154 if(system(command.c_str()))
156 result = new std::string("Error executing: " + command + ".");
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);
166 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
168 if (this->CMakeLists == NULL)
170 result = new std::string("There's no CMakeLists file to open.");
173 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
177 result = new std::string("Couldn't open CMakeLists file.");
182 const bool modelCDMFolder::Refresh(std::string*& result)
184 //std::cout << "refreshing folder " << this->name << std::endl;
186 this->type = wxDIR_DIRS;
189 std::vector<bool> checked(this->children.size(), false);
190 std::vector<bool> checkedFolders(this->folders.size(), false);
193 wxDir dir(crea::std2wx((this->path).c_str()));
197 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
200 std::string stdfileName = crea::wx2std(fileName);
201 //check if they already exist
203 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
205 if (this->folders[i]->GetName() == stdfileName)
208 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
210 checkedFolders[i] = true;
211 if(!this->folders[i]->Refresh(result))
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);
221 cont = dir.GetNext(&fileName);
224 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
227 std::string stdfileName = crea::wx2std(fileName);
228 std::size_t fileTypePos = stdfileName.find_last_of(".");
229 std::string fileType = stdfileName.substr(fileTypePos);
231 //if CMakeLists, create CMakeLists
232 if(stdfileName == "CMakeLists.txt")
234 if (this->CMakeLists == NULL)
236 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
237 this->children.push_back(this->CMakeLists);
241 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
243 if(!this->CMakeLists->Refresh(result))
248 //if is an unknown file, create file
252 for (int i = 0;!found && i < (int)(this->children.size()); i++)
254 if (this->children[i]->GetName() == stdfileName)
258 if(!this->children[i]->Refresh(result))
265 //if is a code file, create modelCDMCodeFile
268 fileType == ".cxx" ||
270 fileType == ".cpp" ||
271 fileType == ".txx" ||
272 fileType == ".cmake" )
274 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
278 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
279 this->children.push_back(file);
284 cont = dir.GetNext(&fileName);
288 for (int i = 0; i < (int)(checkedFolders.size()); i++)
290 if(!checkedFolders[i])
292 this->folders.erase(this->folders.begin()+i);
293 checkedFolders.erase(checkedFolders.begin()+i);
297 for (int i = 0; i < (int)(checked.size()); i++)
301 delete this->children[i];
302 this->children.erase(this->children.begin()+i);
303 checked.erase(checked.begin()+i);
307 this->SortChildren();
308 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
312 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
314 return this->CMakeLists;
317 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
319 return this->folders;
323 modelCDMFolder::HasCMakeLists()
325 return this->CMakeLists != NULL;