]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMFolder.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMFolder.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sant�)
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
9 #
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.
16 #
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
21 #  liability.
22 #
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 # ------------------------------------------------------------------------
26  */
27
28 /*
29  * modelCDMFolder.cpp
30  *
31  *  Created on: Nov 28, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMFolder.h"
36
37 #include <fstream>
38 #include <algorithm>
39
40 #include <creaWx.h>
41 #include <wx/dir.h>
42
43 #include "CDMUtilities.h"
44
45 modelCDMFolder::modelCDMFolder()
46 {
47   this->CMakeLists = NULL;
48 }
49
50 modelCDMFolder::modelCDMFolder(const std::string& path, const std::string& name, const int& level)
51 {
52   std::cout << "creating folder: " + path + "\n";
53   //set attributes
54   this->children.clear();
55   this->level = level;
56   this->CMakeLists = NULL;
57   this->length = 0;
58   this->name = name;
59   this->path = path;
60   this->type = wxDIR_DIRS;
61
62   std::string pathFixed(CDMUtilities::fixPath(path));
63   //check all folders
64   wxDir dir(crea::std2wx((pathFixed).c_str()));
65   if (dir.IsOpened())
66     {
67       wxString fileName;
68       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
69       while (cont)
70         {
71           std::string stdfileName = crea::wx2std(fileName);
72
73           //if is an unknown folder, create folder
74           modelCDMFolder* folder = new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
75           this->children.push_back(folder);
76           this->folders.push_back(folder);
77
78           cont = dir.GetNext(&fileName);
79         }
80
81       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
82       while (cont)
83         {
84           std::string stdfileName = crea::wx2std(fileName);
85
86           //if CMakeLists, create CMakeLists
87           if(stdfileName == "CMakeLists.txt")
88             {
89               this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
90               this->children.push_back(this->CMakeLists);
91             }
92           else
93             {
94               this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
95             }
96           //if is an unknown file, create file
97           cont = dir.GetNext(&fileName);
98         }
99     }
100
101   this->SortChildren();
102   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
103 }
104
105 modelCDMFolder::~modelCDMFolder()
106 {
107   this->folders.clear();
108   this->CMakeLists = NULL;
109   for (int i = 0; i < this->children.size(); i++)
110     {
111       if(this->children[i] != NULL)
112         {
113           delete this->children[i];
114           this->children[i] = NULL;
115         }
116     }
117   this->children.clear();
118 }
119
120 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
121 {
122   //TODO:: mkdir depending on OS
123   std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
124   if(system(command.c_str()))
125     {
126       result = new std::string("Error executing: " + command + ".");
127       return NULL;
128     }
129   modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
130   this->folders.push_back(folder);
131   this->children.push_back(folder);
132
133   return folder;
134 }
135
136 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
137 {
138   if (this->CMakeLists == NULL)
139     {
140       result = new std::string("There's no CMakeLists file to open.");
141       return false;
142     }
143   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
144     return true;
145   else
146     {
147       result = new std::string("Couldn't open CMakeLists file.");
148       return false;
149     }
150 }
151
152 const bool modelCDMFolder::Refresh(std::string*& result)
153 {
154   //std::cout << "refreshing folder " << this->name << std::endl;
155   //set attributes
156   this->type = wxDIR_DIRS;
157
158   //check children
159   std::vector<bool> checked(this->children.size(), false);
160   std::vector<bool> checkedFolders(this->folders.size(), false);
161
162   //check all folders
163   wxDir dir(crea::std2wx((this->path).c_str()));
164   if (dir.IsOpened())
165     {
166       wxString fileName;
167       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
168       while (cont)
169         {
170           std::string stdfileName = crea::wx2std(fileName);
171           //check if they already exist
172           bool found = false;
173           for (int i = 0; !found && i < this->folders.size(); i++)
174             {
175               if (this->folders[i]->GetName() == stdfileName)
176                 {
177                   found = true;
178                   int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
179                   checked[pos] = true;
180                   checkedFolders[i] = true;
181                   if(!this->folders[i]->Refresh(result))
182                     return false;
183                 }
184             }
185           if(!found)
186             {
187               modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
188               this->folders.push_back(folder);
189               this->children.push_back(folder);
190             }
191           cont = dir.GetNext(&fileName);
192         }
193
194       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
195       while (cont)
196         {
197           std::string stdfileName = crea::wx2std(fileName);
198
199           //if CMakeLists, create CMakeLists
200           if(stdfileName == "CMakeLists.txt")
201             {
202               if (this->CMakeLists == NULL)
203                 {
204                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
205                   this->children.push_back(this->CMakeLists);
206                 }
207               else
208                 {
209                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
210                   checked[pos] = true;
211                   if(!this->CMakeLists->Refresh(result))
212                     return false;
213                 }
214             }
215
216           //if is an unknown file, create file
217           else
218             {
219               bool found = false;
220               for (int i = 0;!found && i < this->children.size(); i++)
221                 {
222                   if (this->children[i]->GetName() == stdfileName)
223                     {
224                       found = true;
225                       checked[i] = true;
226                       if(!this->children[i]->Refresh(result))
227                         return false;
228                     }
229                 }
230
231               if(!found)
232                 {
233                   modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
234                   this->children.push_back(file);
235                 }
236             }
237
238           cont = dir.GetNext(&fileName);
239         }
240     }
241
242   for (int i = 0; i < checkedFolders.size(); i++)
243     {
244       if(!checkedFolders[i])
245         {
246           this->folders.erase(this->folders.begin()+i);
247           checkedFolders.erase(checkedFolders.begin()+i);
248           i--;
249         }
250     }
251   for (int i = 0; i < checked.size(); i++)
252     {
253       if(!checked[i])
254         {
255           delete this->children[i];
256           this->children.erase(this->children.begin()+i);
257           checked.erase(checked.begin()+i);
258           i--;
259         }
260     }
261   this->SortChildren();
262   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
263   return true;
264 }
265
266 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
267 {
268   return this->CMakeLists;
269 }
270
271 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
272 {
273   return this->folders;
274 }
275
276 bool
277 modelCDMFolder::HasCMakeLists()
278 {
279   return this->CMakeLists != NULL;
280 }