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 else if(fileType == ".bbs")
114 modelCDMBBSFile* file = new modelCDMBBSFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
115 this->children.push_back(file);
117 //if is an unknown file, create file
120 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
123 cont = dir.GetNext(&fileName);
127 this->SortChildren();
128 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
131 modelCDMFolder::~modelCDMFolder()
133 this->folders.clear();
134 this->CMakeLists = NULL;
135 for (int i = 0; i < (int)(this->children.size()); i++)
137 if(this->children[i] != NULL)
139 delete this->children[i];
140 this->children[i] = NULL;
143 this->children.clear();
146 bool modelCDMFolder::CreateClass(const std::string& name)
148 if (!CDMUtilities::createEmptyClass(name, this->path))
154 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".h", name + ".h", this->level + 1));
155 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".cpp", name + ".cpp", this->level + 1));
156 this->SortChildren();
161 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
163 //TODO:: mkdir depending on OS
164 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
165 if(system(command.c_str()))
167 result = new std::string("Error executing: " + command + ".");
170 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
171 this->folders.push_back(folder);
172 this->children.push_back(folder);
177 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
179 if (this->CMakeLists == NULL)
181 result = new std::string("There's no CMakeLists file to open.");
184 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
188 result = new std::string("Couldn't open CMakeLists file.");
193 const bool modelCDMFolder::Refresh(std::string*& result)
195 //std::cout << "refreshing folder " << this->name << std::endl;
197 this->type = wxDIR_DIRS;
200 std::vector<bool> checked(this->children.size(), false);
201 std::vector<bool> checkedFolders(this->folders.size(), false);
204 wxDir dir(crea::std2wx((this->path).c_str()));
208 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
211 std::string stdfileName = crea::wx2std(fileName);
212 //check if they already exist
214 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
216 if (this->folders[i]->GetName() == stdfileName)
219 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
221 checkedFolders[i] = true;
222 if(!this->folders[i]->Refresh(result))
228 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
229 this->folders.push_back(folder);
230 this->children.push_back(folder);
232 cont = dir.GetNext(&fileName);
235 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
238 std::string stdfileName = crea::wx2std(fileName);
239 std::size_t fileTypePos = stdfileName.find_last_of(".");
240 std::string fileType;
241 if(fileTypePos != std::string::npos)
242 fileType = stdfileName.substr(fileTypePos);
246 //if CMakeLists, create CMakeLists
247 if(stdfileName == "CMakeLists.txt")
249 if (this->CMakeLists == NULL)
251 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
252 this->children.push_back(this->CMakeLists);
256 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
258 if(!this->CMakeLists->Refresh(result))
263 //if is an unknown file, create file
267 for (int i = 0;!found && i < (int)(this->children.size()); i++)
269 if (this->children[i]->GetName() == stdfileName)
273 if(!this->children[i]->Refresh(result))
280 //if is a code file, create modelCDMCodeFile
283 fileType == ".cxx" ||
285 fileType == ".cpp" ||
286 fileType == ".txx" ||
287 fileType == ".cmake" )
289 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
291 else if(fileType == ".bbs")
293 this->children.push_back(new modelCDMBBSFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
297 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
298 this->children.push_back(file);
303 cont = dir.GetNext(&fileName);
307 for (int i = 0; i < (int)(checkedFolders.size()); i++)
309 if(!checkedFolders[i])
311 this->folders.erase(this->folders.begin()+i);
312 checkedFolders.erase(checkedFolders.begin()+i);
316 for (int i = 0; i < (int)(checked.size()); i++)
320 delete this->children[i];
321 this->children.erase(this->children.begin()+i);
322 checked.erase(checked.begin()+i);
326 this->SortChildren();
327 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
331 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
333 return this->CMakeLists;
336 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
338 return this->folders;
342 modelCDMFolder::HasCMakeLists()
344 return this->CMakeLists != NULL;