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