]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
08cafc74f530d554663117f56d05106ee6c34d43
[crea.git] / lib / creaDevManagerLib / modelCDMProject.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  * modelCDMProject.cpp
30  *
31  *  Created on: 13/11/2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMProject.h"
36
37 #include <iostream>
38 #include <sstream>
39 #include <vector>
40 #include <algorithm>
41 #include <fstream>
42 #include <ctime>
43
44 #include "CDMUtilities.h"
45 #include "creaWx.h"
46 #include "wx/dir.h"
47
48 modelCDMProject::modelCDMProject()
49 {
50   std::cout << "in constructor1" << std::endl;
51   this->appli = NULL;
52   this->lib = NULL;
53   this->CMakeLists = NULL;
54 }
55
56 modelCDMProject::modelCDMProject(
57     modelCDMIProjectTreeNode* parent,
58     const std::string& path,
59     const std::string& name,
60     const std::string& buildPath
61 )
62 {
63   std::cout << "creating project: " + name + " in " + path + "\n";
64   this->parent = parent;
65   this->path = CDMUtilities::fixPath(path);
66   //open makelists file
67   std::string pathFixed(CDMUtilities::fixPath(path));
68
69   std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
70
71   std::ifstream confFile;
72   confFile.open((pathMakeLists).c_str());
73
74   std::string word;
75   while(confFile.is_open() && !confFile.eof())
76     {
77       //std::cout << "leyendo " << word << std::endl;
78       //get project name
79       std::getline(confFile,word,'(');
80       std::vector<std::string> wordBits;
81       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
82
83       if(wordBits[wordBits.size()-1] == "PROJECT")
84         {
85           std::getline(confFile,word,')');
86           std::vector<std::string> nameBits;
87           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
88
89           this->name = this->nameProject = "";
90           for (int i = 0; i < (int)(nameBits.size()); i++)
91             {
92               if(i != 0)
93                 this->name += " ";
94               this->name += nameBits[i];
95             }
96           this->nameProject = this->name;
97
98         }
99
100
101       if(wordBits[wordBits.size()-1] == "SET")
102         {
103           //get project version
104           std::getline(confFile,word,')');
105           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
106           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
107             {
108               version = wordBits[1];
109             }
110           if(wordBits[0] == "PROJECT_MINOR_VERSION")
111             {
112               version += "." + wordBits[1];
113             }
114           if(wordBits[0] == "PROJECT_BUILD_VERSION")
115             {
116               version += "." + wordBits[1];
117             }
118
119           //get project versionDate
120           if(wordBits[0] == "PROJECT_VERSION_DATE")
121             {
122               std::vector<std::string> versionBits;
123               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
124               versionDate = versionBits[0];
125             }
126           //get project buildPath
127
128           if (buildPath != "")
129             {
130               this->buildPath = buildPath;
131             }
132           else
133             {
134               this->buildPath = this->path + "Bin";
135             }
136         }
137     }
138   confFile.close();
139
140   this->type = wxDIR_DIRS;
141   this->level = 0;
142
143   this->children.clear();
144   this->appli = NULL;
145   this->lib = NULL;
146   this->packages.clear();
147
148
149   //check all folders
150   wxDir dir(crea::std2wx((pathFixed).c_str()));
151   if (dir.IsOpened())
152     {
153       wxString fileName;
154       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
155       while (cont)
156         {
157           std::string stdfileName = crea::wx2std(fileName);
158
159           //if appli, create appli
160           if(stdfileName == "appli")
161             {
162               this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
163               this->children.push_back(this->appli);
164             }
165           //if lib, create lib
166           else if(stdfileName == "lib")
167             {
168               this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
169               this->children.push_back(this->lib);
170             }
171           //if package , create package
172           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
173             {
174               modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
175               this->packages.push_back(package);
176               this->children.push_back(package);
177             }
178           //if is an unknown folder, create folder
179           else
180             {
181               this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
182             }
183
184           cont = dir.GetNext(&fileName);
185         }
186
187       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
188       while (cont)
189         {
190           std::string stdfileName = crea::wx2std(fileName);
191
192           //if CMakeLists, create CMakeLists
193           if(stdfileName == "CMakeLists.txt")
194             {
195               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
196               this->children.push_back(this->CMakeLists);
197             }
198           else
199             {
200               this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
201             }
202           //if is an unknown file, create file
203           cont = dir.GetNext(&fileName);
204         }
205     }
206
207   this->SortChildren();
208   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
209
210 }
211
212 modelCDMProject::~modelCDMProject()
213 {
214 }
215
216 const std::string& modelCDMProject::GetNameProject() const
217 {
218   return this->nameProject;
219 }
220
221 const std::string& modelCDMProject::GetVersion() const
222 {
223   return this->version;
224 }
225
226 const std::string& modelCDMProject::GetVersionDate() const
227 {
228   return this->versionDate;
229 }
230
231 const std::string& modelCDMProject::GetBuildPath() const
232 {
233   return this->buildPath;
234 }
235
236 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
237 {
238   return this->packages;
239 }
240
241 modelCDMAppli* modelCDMProject::GetAppli() const
242 {
243   return this->appli;
244 }
245
246 modelCDMLib* modelCDMProject::GetLib() const
247 {
248   return this->lib;
249 }
250
251 std::string modelCDMProject::GetBuildInstruction() const
252 {
253   std::string makeComm = "make -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
254   return makeComm;
255 }
256
257 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
258 {
259
260   std::vector<std::string> vers;
261   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
262
263   time_t now = time(0);
264
265   tm ltm;
266 #ifdef _WIN32
267   localtime_s(&ltm, &now);
268 #else
269   ltm = *(localtime(&now));
270 #endif
271
272   std::stringstream date;
273   date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
274
275   //set name of library in CMakeLists inside copied folder
276   std::string line;
277   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
278   if( !in.is_open())
279     {
280       result = new std::string("CMakeLists.txt file failed to open.");
281       return false;
282     }
283   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
284   if( !out.is_open())
285     {
286       result = new std::string("CMakeLists.txt.tmp file failed to open.");
287       return false;
288     }
289   while (getline(in, line))
290     {
291       if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
292         line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
293       else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
294         line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
295       else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
296         line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
297       else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
298         line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
299       out << line << std::endl;
300     }
301   in.close();
302   out.close();
303   //delete old file and rename new file
304 #ifdef _WIN32
305   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
306 #else
307   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
308 #endif
309   if(system(renameCommand.c_str()))
310     {
311       result = new std::string("An error occurred while running '" + renameCommand + "'.");
312       return false;
313     }
314
315   this->version = vers[0] + "." + vers[1] + "." + vers[2];
316   this->versionDate = date.str();
317   return true;
318 }
319
320 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
321 {
322   if(path == "")
323     {
324       result = new std::string("The path cannot be empty");
325       return false;
326     }
327   if(path == this->path)
328     {
329       result = new std::string("The path cannot be same as the project sources");
330       return false;
331     }
332   this->buildPath = path;
333   return true;
334 }
335
336 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
337     const std::string& name,
338     std::string*& result,
339     const std::string& authors,
340     const std::string& authorsEmail,
341     const std::string& description,
342     const std::string& version
343 )
344 {
345   //fixing input parameters
346   std::vector<std::string> words;
347
348   CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
349   std::string nameFixed = "";
350   for (int i = 0; i < (int)(words.size()); i++)
351     {
352       nameFixed += words[i];
353     }
354
355   words.clear();
356   CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
357   std::string authorFixed;
358   for (int i = 0; i < (int)(words.size()); i++)
359     {
360       authorFixed += words[i];
361     }
362
363   words.clear();
364   std::string descriptionFixed;
365   CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
366   for (int i = 0; i < (int)(words.size()); i++)
367     {
368       descriptionFixed += words[i] + "/";
369     }
370   words.clear();
371   CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
372   for (int i = 0; i < (int)(words.size()); i++)
373     {
374       descriptionFixed += "_" + words[i];
375     }
376
377   //call project to create package : use bbCreatePackage <path> <name> [author] [description]
378   std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
379   //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
380   bool resultCommand = 0 != system(creationCommand.c_str());
381 #ifdef _WIN32
382   resultCommand = false;
383 #endif
384   if(resultCommand)
385     {
386       result = new std::string("An error occurred while running '" + creationCommand + "'.");
387       return NULL;
388     }
389
390   //add library to project CMakeLists
391   std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
392   if (out1.is_open())
393     {
394       out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
395       out1.close();
396     }
397
398   //add library to model
399   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
400   this->packages.push_back(package);
401   this->children.push_back(package);
402
403   //TODO: set package version
404
405   this->SortChildren();
406
407   result = new std::string(this->path + CDMUtilities::SLASH + name);
408   return package;
409 }
410
411 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
412     const std::string& name,
413     std::string*& result,
414     const std::string& path
415 )
416 {
417   if(this->lib != NULL)
418     {
419       return this->lib->CreateLibrary(name, result);
420     }
421   result = new std::string("there is no lib folder in this project.");
422   return NULL;
423 }
424
425 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
426     const std::string& name,
427     const int& type,
428     std::string*& result,
429     const std::string& path
430 )
431 {
432   if(this->appli != NULL)
433     {
434       return this->appli->CreateApplication(name, type, result);
435     }
436   result = new std::string("there is no appli folder in this project.");
437   return NULL;
438 }
439
440 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
441     const std::string& name,
442     const std::string& package,
443     const std::string& authors,
444     const std::string& authorsEmail,
445     const std::string& categories,
446     const std::string& description
447 )
448 {
449   //TODO: implement method
450   return NULL;
451 }
452
453 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
454 {
455   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
456     return true;
457   else
458     {
459       result = new std::string("Couldn't open CMakeLists file.");
460       return false;
461     }
462 }
463
464 const bool modelCDMProject::Refresh(std::string*& result)
465 {
466   std::cout << "refreshing project" << std::endl;
467   //open makelists file
468   std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
469
470   std::ifstream confFile;
471   confFile.open((pathMakeLists).c_str());
472
473   std::string word;
474   while(confFile.is_open() && !confFile.eof())
475     {
476       //std::cout << "leyendo " << word << std::endl;
477       //get project name
478       std::getline(confFile,word,'(');
479       std::vector<std::string> wordBits;
480       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
481
482       if(wordBits[wordBits.size()-1] == "PROJECT")
483         {
484           std::getline(confFile,word,')');
485           std::vector<std::string> nameBits;
486           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
487
488           this->name = this->nameProject = "";
489           for (int i = 0; i < (int)(nameBits.size()); i++)
490             {
491               if(i != 0)
492                 this->name += " ";
493               this->name += nameBits[i];
494             }
495           this->nameProject = this->name;
496
497         }
498
499
500       if(wordBits[wordBits.size()-1] == "SET")
501         {
502           //get project version
503           std::getline(confFile,word,')');
504           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
505           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
506             {
507               version = wordBits[1];
508             }
509           if(wordBits[0] == "PROJECT_MINOR_VERSION")
510             {
511               version += "." + wordBits[1];
512             }
513           if(wordBits[0] == "PROJECT_BUILD_VERSION")
514             {
515               version += "." + wordBits[1];
516             }
517
518           //get project versionDate
519           if(wordBits[0] == "PROJECT_VERSION_DATE")
520             {
521               std::vector<std::string> versionBits;
522               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
523               versionDate = versionBits[0];
524             }
525         }
526     }
527   confFile.close();
528
529   this->type = wxDIR_DIRS;
530   this->level = 0;
531
532   std::vector<bool> checked(this->children.size(), false);
533   std::vector<bool> checkedPackages(this->packages.size(), false);
534
535   //check all folders
536   wxDir dir(crea::std2wx((this->path).c_str()));
537   if (dir.IsOpened())
538     {
539       wxString fileName;
540       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
541       while (cont)
542         {
543           std::string stdfileName = crea::wx2std(fileName);
544
545           //if appli, create appli
546           if(stdfileName == "appli")
547             {
548               if (this->appli == NULL)
549                 {
550                   this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
551                   this->children.push_back(this->appli);
552                 }
553               else
554                 {
555                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
556                   checked[pos] = true;
557                   if(!this->appli->Refresh(result))
558                     return false;
559                 }
560             }
561           //if lib, create lib
562           else if(stdfileName == "lib")
563             {
564               if (this->lib == NULL)
565                 {
566                   this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
567                   this->children.push_back(this->lib);
568                 }
569               else
570                 {
571                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
572                   checked[pos] = true;
573                   if(!this->lib->Refresh(result))
574                     return false;
575                 }
576
577             }
578           //if package , create package
579           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
580             {
581               bool found = false;
582               for (int i = 0; !found && i < (int)(this->packages.size()); i++)
583                 {
584                   if (this->packages[i]->GetName() == stdfileName)
585                     {
586                       found = true;
587                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
588                       checked[pos] = true;
589                       checkedPackages[i] = true;
590                       if(!this->packages[i]->Refresh(result))
591                         return false;
592                     }
593                 }
594               if(!found)
595                 {
596                   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
597                   this->packages.push_back(package);
598                   this->children.push_back(package);
599                 }
600
601             }
602           //if is an unknown folder, create folder
603           else
604             {
605               bool found = false;
606               for (int i = 0; !found && i < (int)(this->children.size()); i++)
607                 {
608                   if (this->children[i]->GetName() == stdfileName)
609                     {
610                       found = true;
611                       checked[i] = true;
612                       if(!this->children[i]->Refresh(result))
613                         return false;
614                     }
615                 }
616
617               if(!found)
618                 {
619                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
620                   this->children.push_back(folder);
621                 }
622             }
623
624           cont = dir.GetNext(&fileName);
625         }
626
627       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
628       while (cont)
629         {
630           std::string stdfileName = crea::wx2std(fileName);
631
632           //if CMakeLists, create CMakeLists
633           if(stdfileName == "CMakeLists.txt")
634             {
635               if (this->CMakeLists == NULL)
636                 {
637                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
638                   this->children.push_back(this->CMakeLists);
639                 }
640               else
641                 {
642                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
643                   checked[pos] = true;
644                   if(!this->CMakeLists->Refresh(result))
645                     return false;
646                 }
647             }
648           //if is an unknown file, create file
649           else
650             {
651               bool found = false;
652               for (int i = 0; !found && i < (int)(this->children.size()); i++)
653                 {
654                   if (this->children[i]->GetName() == stdfileName)
655                     {
656                       found = true;
657                       checked[i] = true;
658                       if(!this->children[i]->Refresh(result))
659                         return false;
660                     }
661                 }
662
663               if(!found)
664                 {
665                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
666                   this->children.push_back(file);
667                 }
668             }
669
670           cont = dir.GetNext(&fileName);
671         }
672     }
673
674   for (int i = 0; i < (int)(checkedPackages.size()); i++)
675     {
676       if(!checkedPackages[i])
677         {
678           this->packages.erase(this->packages.begin()+i);
679           checkedPackages.erase(checkedPackages.begin()+i);
680           i--;
681         }
682     }
683   for (int i = 0; i < (int)(checked.size()); i++)
684     {
685       if(!checked[i])
686         {
687           delete this->children[i];
688           this->children.erase(this->children.begin()+i);
689           checked.erase(checked.begin()+i);
690           i--;
691         }
692     }
693
694   this->SortChildren();
695   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
696   return true;
697 }
698
699 bool modelCDMProject::ConfigureBuild(std::string*& result)
700 {
701   //TODO: adjust for windows and mac
702 #ifdef _WIN32
703   // ------ Windows
704   if(0 == system("cmake-gui"))
705     return true;
706   else
707     {
708           result = new std::string("There was an error opening cmake-gui. Please make sure it's installed and that cmake's bin folder is in the system path.");
709       return false;
710     }
711 #elif __APPLE__
712   // ------ Apple
713 #else
714   // ------ Linux
715   //open binary folder
716   wxDir dir(crea::std2wx((this->buildPath).c_str()));
717
718   //if folder doesn't exist then create it
719   if (!dir.IsOpened())
720     {
721       //create command line to create folder
722       std::string createComm = "mkdir \"" + this->buildPath + "\"";
723       //execute creation command
724       if (system(createComm.c_str()))
725         {
726           //if there was an error then report it
727           result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
728           return false;
729         }
730     }
731   //create command line to execute ccmake
732   //TODO:: adjust for different Linux distributions
733   std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
734   //execute command
735   if(CDMUtilities::openTerminal(confComm))
736     {
737       //if there was an error then report it
738       result = new std::string("There was an error opening the configuration tool in the desired place.");
739       return false;
740     }
741 #endif
742   return true;
743 }
744
745 bool modelCDMProject::Build(std::string*& result, const std::string& line)
746 {
747   //TODO: adjust for windows and mac
748 #ifdef _WIN32
749   // ------ Windows
750         //\\..\\IDE\\VCExpress.exe \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"
751 //      std::string command = "\"" + std::string(getenv("VS90COMNTOOLS")) + "..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &";
752         std::string command = "\"\"%VS90COMNTOOLS%..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &\"";
753         command = "start cmd.exe /k " + command + " &";
754   if(0 == system(command.c_str()))
755     return true;
756   else
757     {
758       result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ express installed and to have the VS90COMNTOOLS environment variable set.");
759           return false;
760     }
761 #elif __APPLE__
762   // ------ Apple
763 #else
764   // ------ Linux
765   //open binary folder
766   wxDir dir(crea::std2wx((this->buildPath).c_str()));
767
768   //if binary folder can't be opened then return false
769   if (!dir.IsOpened())
770     {
771       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
772       return false;
773     }
774   //create make command
775   std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
776   for (int i = 0; i < line.size(); i++)
777     {
778       if(line[i] == '"')
779         {
780           makeComm+="\\\\\\\"";
781         }
782       else if(line[i] == '\\')
783         {
784           makeComm+="\\\\\\\\";
785         }
786       else
787         {
788           makeComm.push_back(line[i]);
789         }
790     }
791   makeComm += "; echo -e '\\a'; bash";
792   makeComm += "\\\"\"";
793
794   std::cout << "executing '" << makeComm << "'" << std::endl;
795   //execute make command
796   if(system(makeComm.c_str()))
797     {
798       //if there was an error then report it
799       result = new std::string("There was an error compiling the project, please check the \"building.log\" file located in the build folder to read more about the problem.");
800       return false;
801     }
802 #endif
803   return true;
804 }
805
806 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
807 {
808   //TODO: adjust for mac
809 #ifdef _WIN32
810   // ------ Windows
811   //open binary folder
812   wxDir dir(crea::std2wx(folder));
813
814   //if binary folder can't be opened then return false
815   if (!dir.IsOpened())
816     {
817       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
818       return false;
819     }
820   //create plug command
821   std::string plugComm = "bbPlugPackage \"" + folder + "\"";
822   std::cout << "executing '" << plugComm << "'" << std::endl;
823   //execute plug command
824   if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
825     {
826       //if there was an error then report it
827       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
828       return false;
829     }
830 #elif __APPLE__
831   // ------ Apple
832 #else
833   // ------ Linux
834   //open binary folder
835   wxDir dir(crea::std2wx(folder));
836
837   //if binary folder can't be opened then return false
838   if (!dir.IsOpened())
839     {
840       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
841       return false;
842     }
843   //create plug command
844   std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
845
846   std::string Comm = "gnome-terminal -e \"bash -c \\\"";
847     for (int i = 0; i < plugComm.size(); i++)
848       {
849         if(plugComm[i] == '"')
850           {
851             Comm+="\\\\\\\"";
852           }
853         else if(plugComm[i] == '\\')
854           {
855             Comm+="\\\\\\\\";
856           }
857         else
858           {
859             Comm.push_back(plugComm[i]);
860           }
861       }
862     Comm += "; echo -e '\\a'; bash";
863     Comm += "\\\"\"";
864
865
866   std::cout << "executing '" << Comm << "'" << std::endl;
867   //execute plug command
868   if(system(Comm.c_str()))
869     {
870       //if there was an error then report it
871       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
872       return false;
873     }
874 #endif
875   return true;
876 }
877
878 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
879 {
880   //check cmake exist
881   if(this->CMakeLists != NULL)
882     {
883       //set properties parameters based on model
884       properties["project add appli"] = this->appli != NULL;
885       properties["project add lib"] = this->lib != NULL;
886       for (int i = 0; i < (int)(this->packages.size()); i++)
887         properties["project add " + packages[i]->GetName()] = false;
888
889       //open cmakelists
890       std::ifstream confFile;
891       confFile.open((this->CMakeLists->GetPath()).c_str());
892
893       //take everything that is not commented
894       std::string fileContent;
895
896       std::string word;
897       std::vector<std::string> words;
898       while(confFile.is_open() && !confFile.eof())
899         {
900           std::getline(confFile,word, '\n');
901           if(word[0] != '#')
902             {
903               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
904               if (words.size() > 0)
905                 {
906                   word = words[0];
907                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
908                   for (int i = 0; i < (int)(words.size()); i++)
909                     {
910                       if(words[i].substr(0,2) == "//")
911                         break;
912                       fileContent += words[i] + " ";
913                     }
914                 }
915
916             }
917         }
918
919       //check every instruction
920       std::stringstream ss(fileContent);
921       while(!ss.eof())
922         {
923           std::getline(ss,word, '(');
924
925           //check instruction name
926           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
927
928           //set instructions
929           if (words.size() > 0 && words[words.size()-1] == "SET")
930             {
931               std::getline(ss,word, ')');
932
933               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
934               if (words.size() > 1)
935                 {
936                   if (words[0] == "USE_CREA" && words[1] == "ON")
937                     {
938                       properties["project set USE_CREA"] = true;
939                     }
940                   else if (words[0] == "USE_GDCM" && words[1] == "ON")
941                     {
942                       properties["project set USE_GDCM"] = true;
943                     }
944                   else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
945                     {
946                       properties["project set USE_GDCM_VTK"] = true;
947                     }
948                   else if (words[0] == "USE_GDCM2" && words[1] == "ON")
949                     {
950                       properties["project set USE_GDCM2"] = true;
951                     }
952                   else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
953                     {
954                       properties["project set USE_WXWIDGETS"] = true;
955                     }
956                   else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
957                     {
958                       properties["project set USE_KWWIDGETS"] = true;
959                     }
960                   else if (words[0] == "USE_VTK" && words[1] == "ON")
961                     {
962                       properties["project set USE_VTK"] = true;
963                     }
964                   else if (words[0] == "USE_ITK" && words[1] == "ON")
965                     {
966                       properties["project set USE_ITK"] = true;
967                     }
968                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
969                     {
970                       properties["project set USE_BOOST"] = true;
971                     }
972                 }
973             }
974           //add instructions
975           else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
976             {
977               std::getline(ss,word, ')');
978               //std::cout << word << std::endl;
979               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
980
981               if (words.size() > 0)
982                 {
983                   if(words[0] == "appli")
984                     {
985                       properties["project add "+words[0]] = this->appli != NULL;
986                     }
987                   else if(words[0] == "lib")
988                     {
989                       properties["project add "+words[0]] = this->lib != NULL;
990                     }
991                   else
992                     {
993                       properties["project add "+words[0]] = true;
994                     }
995                 }
996             }
997         }
998
999     }
1000
1001   //check appli's structure
1002   this->appli->CheckStructure(properties);
1003   //check lib's structure
1004   this->lib->CheckStructure(properties);
1005   //check packages' structure
1006   for (int i = 0; i < (int)(this->packages.size()); i++)
1007     {
1008       properties["package " + this->packages[i]->GetName()] = true;
1009       this->packages[i]->CheckStructure(properties);
1010     }
1011 }
1012
1013 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
1014 {
1015   if (this->HasCMakeLists())
1016     {
1017       CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1018       for (int i = 0; i < cmlFile.size(); ++i)
1019         {
1020           if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1021             {
1022               int pos = 1;
1023               while (pos < cmlFile[i].second.size())
1024                 {
1025                   if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1026                     {
1027                       if (package_name == cmlFile[i].second[pos])
1028                         return true;
1029                       break;
1030                     }
1031                   pos++;
1032                 }
1033             }
1034         }
1035     }
1036   return false;
1037
1038 }
1039
1040 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
1041 {
1042   if (this->HasCMakeLists())
1043     {
1044       CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1045
1046       bool found = false;
1047
1048       for (int i = 0; i < cmlFile.size(); ++i)
1049         {
1050           if(toInclude && cmlFile[i].first == "comment")
1051             {
1052               std::vector<std::string> segments;
1053               std::string line = cmlFile[i].second[0];
1054               while(line[0] == '#')
1055                 line.erase(0,1);
1056
1057               CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
1058               if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
1059                 {
1060                   found = true;
1061                   while(cmlFile[i].second[0][0] == '#')
1062                     cmlFile[i].second[0].erase(0,1);
1063                 }
1064             }
1065           else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1066             {
1067               int pos = 1;
1068               while (pos < cmlFile[i].second.size())
1069                 {
1070                   if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1071                     {
1072                       if (package_name == cmlFile[i].second[pos])
1073                         {
1074                           found = true;
1075                           if (!toInclude)
1076                             {
1077                               cmlFile[i].first = "comment";
1078                               cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
1079                               while (cmlFile[i].second.size() > 1)
1080                                 {
1081                                   cmlFile[i].second[0] += cmlFile[i].second[1];
1082                                   cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
1083                                 }
1084
1085                             }
1086                         }
1087                       break;
1088                     }
1089                   pos++;
1090                 }
1091             }
1092         }
1093       if (!found && toInclude)
1094         {
1095           CDMUtilities::syntaxElement element;
1096           element.first = "command";
1097           element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
1098           cmlFile.push_back(element);
1099         }
1100
1101       return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
1102     }
1103   return false;
1104 }