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 a bbs file, create bbs file
113 else if(fileType == ".bbs")
115 modelCDMBBSFile* file = new modelCDMBBSFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
116 this->children.push_back(file);
118 //if is a bbg file, create bbg file
119 else if(fileType == ".bbg")
121 modelCDMBBGFile* file = new modelCDMBBGFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
122 this->children.push_back(file);
124 //if is an unknown file, create file
127 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
130 cont = dir.GetNext(&fileName);
134 this->SortChildren();
135 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
138 modelCDMFolder::~modelCDMFolder()
140 this->folders.clear();
141 this->CMakeLists = NULL;
142 for (int i = 0; i < (int)(this->children.size()); i++)
144 if(this->children[i] != NULL)
146 delete this->children[i];
147 this->children[i] = NULL;
150 this->children.clear();
153 bool modelCDMFolder::CreateClass(const std::string& name)
155 if (!CDMUtilities::createEmptyClass(name, this->path))
161 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".h", name + ".h", this->level + 1));
162 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".cpp", name + ".cpp", this->level + 1));
163 this->SortChildren();
168 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
170 //TODO:: mkdir depending on OS
171 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
172 if(system(command.c_str()))
174 result = new std::string("Error executing: " + command + ".");
177 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
178 this->folders.push_back(folder);
179 this->children.push_back(folder);
184 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
186 if (this->CMakeLists == NULL)
188 result = new std::string("There's no CMakeLists file to open.");
191 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
195 result = new std::string("Couldn't open CMakeLists file.");
200 const bool modelCDMFolder::Refresh(std::string*& result)
202 //std::cout << "refreshing folder " << this->name << std::endl;
204 this->type = wxDIR_DIRS;
207 std::vector<bool> checked(this->children.size(), false);
208 std::vector<bool> checkedFolders(this->folders.size(), false);
211 wxDir dir(crea::std2wx((this->path).c_str()));
215 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
218 std::string stdfileName = crea::wx2std(fileName);
219 //check if they already exist
221 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
223 if (this->folders[i]->GetName() == stdfileName)
226 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
228 checkedFolders[i] = true;
229 if(!this->folders[i]->Refresh(result))
235 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
236 this->folders.push_back(folder);
237 this->children.push_back(folder);
239 cont = dir.GetNext(&fileName);
242 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
245 std::string stdfileName = crea::wx2std(fileName);
246 std::size_t fileTypePos = stdfileName.find_last_of(".");
247 std::string fileType;
248 if(fileTypePos != std::string::npos)
249 fileType = stdfileName.substr(fileTypePos);
253 //if CMakeLists, create CMakeLists
254 if(stdfileName == "CMakeLists.txt")
256 if (this->CMakeLists == NULL)
258 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
259 this->children.push_back(this->CMakeLists);
263 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
265 if(!this->CMakeLists->Refresh(result))
270 //if is an unknown file, create file
274 for (int i = 0;!found && i < (int)(this->children.size()); i++)
276 if (this->children[i]->GetName() == stdfileName)
280 if(!this->children[i]->Refresh(result))
287 //if is a code file, create modelCDMCodeFile
290 fileType == ".cxx" ||
292 fileType == ".cpp" ||
293 fileType == ".txx" ||
294 fileType == ".cmake" )
296 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
298 //if is a bbs file, create modelCDMBBSFile
299 else if(fileType == ".bbs")
301 this->children.push_back(new modelCDMBBSFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
303 //if is a bbg file, create modelCDMBBGFile
304 else if(fileType == ".bbg")
306 this->children.push_back(new modelCDMBBGFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
308 //if is an unknown file, create modelCDMFile
311 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
312 this->children.push_back(file);
317 cont = dir.GetNext(&fileName);
321 for (int i = 0; i < (int)(checkedFolders.size()); i++)
323 if(!checkedFolders[i])
325 this->folders.erase(this->folders.begin()+i);
326 checkedFolders.erase(checkedFolders.begin()+i);
330 for (int i = 0; i < (int)(checked.size()); i++)
334 delete this->children[i];
335 this->children.erase(this->children.begin()+i);
336 checked.erase(checked.begin()+i);
340 this->SortChildren();
341 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
345 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
347 return this->CMakeLists;
350 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
352 return this->folders;
356 modelCDMFolder::HasCMakeLists()
358 return this->CMakeLists != NULL;