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);
87 //if CMakeLists, create CMakeLists
88 if(stdfileName == "CMakeLists.txt")
90 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
91 this->children.push_back(this->CMakeLists);
95 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
97 //if is an unknown file, create file
98 cont = dir.GetNext(&fileName);
102 this->SortChildren();
103 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
106 modelCDMFolder::~modelCDMFolder()
108 this->folders.clear();
109 this->CMakeLists = NULL;
110 for (int i = 0; i < (int)(this->children.size()); i++)
112 if(this->children[i] != NULL)
114 delete this->children[i];
115 this->children[i] = NULL;
118 this->children.clear();
121 bool modelCDMFolder::CreateClass(const std::string& name)
123 if (!CDMUtilities::createEmptyClass(name, this->path))
129 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".h", name + ".h", this->level + 1));
130 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + name + ".cpp", name + ".cpp", this->level + 1));
131 this->SortChildren();
136 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
138 //TODO:: mkdir depending on OS
139 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
140 if(system(command.c_str()))
142 result = new std::string("Error executing: " + command + ".");
145 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
146 this->folders.push_back(folder);
147 this->children.push_back(folder);
152 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
154 if (this->CMakeLists == NULL)
156 result = new std::string("There's no CMakeLists file to open.");
159 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
163 result = new std::string("Couldn't open CMakeLists file.");
168 const bool modelCDMFolder::Refresh(std::string*& result)
170 //std::cout << "refreshing folder " << this->name << std::endl;
172 this->type = wxDIR_DIRS;
175 std::vector<bool> checked(this->children.size(), false);
176 std::vector<bool> checkedFolders(this->folders.size(), false);
179 wxDir dir(crea::std2wx((this->path).c_str()));
183 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
186 std::string stdfileName = crea::wx2std(fileName);
187 //check if they already exist
189 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
191 if (this->folders[i]->GetName() == stdfileName)
194 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
196 checkedFolders[i] = true;
197 if(!this->folders[i]->Refresh(result))
203 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
204 this->folders.push_back(folder);
205 this->children.push_back(folder);
207 cont = dir.GetNext(&fileName);
210 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
213 std::string stdfileName = crea::wx2std(fileName);
215 //if CMakeLists, create CMakeLists
216 if(stdfileName == "CMakeLists.txt")
218 if (this->CMakeLists == NULL)
220 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
221 this->children.push_back(this->CMakeLists);
225 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
227 if(!this->CMakeLists->Refresh(result))
232 //if is an unknown file, create file
236 for (int i = 0;!found && i < (int)(this->children.size()); i++)
238 if (this->children[i]->GetName() == stdfileName)
242 if(!this->children[i]->Refresh(result))
249 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
250 this->children.push_back(file);
254 cont = dir.GetNext(&fileName);
258 for (int i = 0; i < (int)(checkedFolders.size()); i++)
260 if(!checkedFolders[i])
262 this->folders.erase(this->folders.begin()+i);
263 checkedFolders.erase(checkedFolders.begin()+i);
267 for (int i = 0; i < (int)(checked.size()); i++)
271 delete this->children[i];
272 this->children.erase(this->children.begin()+i);
273 checked.erase(checked.begin()+i);
277 this->SortChildren();
278 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
282 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
284 return this->CMakeLists;
287 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
289 return this->folders;
293 modelCDMFolder::HasCMakeLists()
295 return this->CMakeLists != NULL;