]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMAppli.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMAppli.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  * modelCDMAppli.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMAppli.h"
36
37 #include <iostream>
38 #include <fstream>
39 #include <algorithm>
40
41 #include "CDMUtilities.h"
42 #include "creaWx.h"
43 #include "wx/dir.h"
44
45 modelCDMAppli::modelCDMAppli()
46 {
47 }
48
49 modelCDMAppli::modelCDMAppli(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
50 {
51   std::cout << "creating appli\n";
52   this->parent = parent;
53   this->type = wxDIR_DIRS;
54   this->name = name;
55   this->level = level;
56   this->path = path;
57
58
59
60   this->path = CDMUtilities::fixPath(path);
61   std::string pathFixed(CDMUtilities::fixPath(path));
62
63   this->applications.clear();
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(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
74             {
75               modelCDMApplication* application = new modelCDMApplication(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
76               this->applications.push_back(application);
77               this->children.push_back(application);
78             }
79           else
80             {
81               modelCDMFolder* folder = new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
82               this->children.push_back(folder);
83             }
84
85           cont = dir.GetNext(&fileName);
86         }
87       //files
88       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
89       while (cont)
90         {
91           std::string stdfileName = crea::wx2std(fileName);
92
93           //if CMakeLists, create CMakeLists
94           if(stdfileName == "CMakeLists.txt")
95             {
96               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
97               this->children.push_back(this->CMakeLists);
98             }
99           //if is an unknown file, create file
100           else
101             {
102               this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
103             }
104
105           cont = dir.GetNext(&fileName);
106         }
107
108     }
109   this->SortChildren();
110   std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
111 }
112
113 modelCDMAppli::~modelCDMAppli()
114 {
115 }
116
117 const std::vector<modelCDMApplication*>& modelCDMAppli::GetApplications() const
118 {
119   return this->applications;
120 }
121
122 modelCDMApplication* modelCDMAppli::CreateApplication(
123     const std::string& name,
124     std::string*& result
125 )
126 {
127   //copy template application folder with new name
128   std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
129   if(system(copyCommand.c_str()))
130     {
131       result = new std::string("An error occurred while running '" + copyCommand + "'.");
132       return NULL;
133     }
134   //set name of library in CMakeLists inside copied folder
135   std::string line;
136   std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
137   if( !in.is_open())
138     {
139       result = new std::string("CMakeLists.txt file failed to open.");
140       return NULL;
141     }
142   std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
143   while (getline(in, line))
144     {
145       if(line == "SET ( EXE_NAME   MyExe  )")
146         line = "SET ( EXE_NAME   " + name + "  )";
147       out << line << std::endl;
148     }
149   in.close();
150   out.close();
151   //delete old file and rename new file
152   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
153   if(system(renameCommand.c_str()))
154     {
155       result = new std::string("An error occurred while running '" + renameCommand + "'.");
156       return NULL;
157     }
158
159   //add application to model
160   modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
161   this->applications.push_back(application);
162   this->children.push_back(application);
163
164   this->SortChildren();
165
166   result = new std::string(this->path + CDMUtilities::SLASH + name);
167   return application;
168 }
169
170 const bool modelCDMAppli::Refresh(std::string*& result)
171 {
172   std::cout << "refreshing appli" << std::endl;
173   this->type = wxDIR_DIRS;
174
175   std::vector<bool> checked(this->children.size(), false);
176   std::vector<bool> checkedApplications(this->applications.size(), false);
177
178   //check all folders
179   wxDir dir(crea::std2wx((this->path).c_str()));
180   if (dir.IsOpened())
181     {
182       wxString fileName;
183       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
184       while (cont)
185         {
186           std::string stdfileName = crea::wx2std(fileName);
187
188           if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
189             {
190               std::string applicationName = stdfileName;
191               //check if application already exist
192               bool found = false;
193               for (int i = 0; !found && i < this->applications.size(); i++)
194                 {
195                   if (this->applications[i]->GetName() == applicationName)
196                     {
197                       found = true;
198                       int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
199                       checked[pos] = true;
200                       checkedApplications[i] = true;
201                       if(!this->applications[i]->Refresh(result))
202                         return false;
203                     }
204                 }
205               if(!found)
206                 {
207                   modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
208                   this->applications.push_back(application);
209                   this->children.push_back(application);
210                 }
211             }
212           else
213             {
214               //check if folder already exist
215               bool found = false;
216               for (int i = 0; !found && i < this->children.size(); i++)
217                 {
218                   if (this->children[i]->GetName() == stdfileName)
219                     {
220                       found = true;
221                       checked[i] = true;
222
223                       if(!this->children[i]->Refresh(result))
224                         return false;
225                     }
226                 }
227               if(!found)
228                 {
229                   modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
230                   this->children.push_back(folder);
231                 }
232             }
233           cont = dir.GetNext(&fileName);
234         }
235
236       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
237       while (cont)
238         {
239           std::string stdfileName = crea::wx2std(fileName);
240
241           //if CMakeLists, create CMakeLists
242           if(stdfileName == "CMakeLists.txt")
243             {
244               if (this->CMakeLists == NULL)
245                 {
246                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
247                   this->children.push_back(this->CMakeLists);
248                 }
249               else
250                 {
251                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
252                   checked[pos] = true;
253                   if(!this->CMakeLists->Refresh(result))
254                     return false;
255                 }
256             }
257           //if is an unknown file, create file
258           else
259             {
260               bool found = false;
261               for (int i = 0; !found && i < this->children.size(); i++)
262                 {
263                   if (this->children[i]->GetName() == stdfileName)
264                     {
265                       found = true;
266                       checked[i] = true;
267                       if(!this->children[i]->Refresh(result))
268                         return false;
269                     }
270                 }
271
272               if(!found)
273                 {
274                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
275                   this->children.push_back(file);
276                 }
277             }
278
279           cont = dir.GetNext(&fileName);
280         }
281     }
282
283   for (int i = 0; i < checkedApplications.size(); i++)
284     {
285       if(!checkedApplications[i])
286         {
287           this->applications.erase(this->applications.begin()+i);
288           checkedApplications.erase(checkedApplications.begin()+i);
289           i--;
290         }
291     }
292   for (int i = 0; i < checked.size(); i++)
293     {
294       if(!checked[i])
295         {
296           delete this->children[i];
297           this->children.erase(this->children.begin()+i);
298           checked.erase(checked.begin()+i);
299           i--;
300         }
301     }
302   this->SortChildren();
303   std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
304   return true;
305 }