]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
8b93e189a2f3decab2cad2de0d9906fe3332d5bb
[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 model
391   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
392   this->packages.push_back(package);
393   this->children.push_back(package);
394
395   //TODO: set package version
396
397   this->SortChildren();
398
399   result = new std::string(this->path + CDMUtilities::SLASH + name);
400   return package;
401 }
402
403 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
404     const std::string& name,
405     std::string*& result,
406     const std::string& path
407 )
408 {
409   if(this->lib != NULL)
410     {
411       return this->lib->CreateLibrary(name, result);
412     }
413   result = new std::string("there is no lib folder in this project.");
414   return NULL;
415 }
416
417 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
418     const std::string& name,
419     std::string*& result,
420     const std::string& path
421 )
422 {
423   if(this->appli != NULL)
424     {
425       return this->appli->CreateApplication(name, result);
426     }
427   result = new std::string("there is no appli folder in this project.");
428   return NULL;
429 }
430
431 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
432     const std::string& name,
433     const std::string& package,
434     const std::string& authors,
435     const std::string& authorsEmail,
436     const std::string& categories,
437     const std::string& description
438 )
439 {
440   //TODO: implement method
441   return NULL;
442 }
443
444 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
445 {
446   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
447     return true;
448   else
449     {
450       result = new std::string("Couldn't open CMakeLists file.");
451       return false;
452     }
453 }
454
455 const bool modelCDMProject::Refresh(std::string*& result)
456 {
457   std::cout << "refreshing project" << std::endl;
458   //open makelists file
459   std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
460
461   std::ifstream confFile;
462   confFile.open((pathMakeLists).c_str());
463
464   std::string word;
465   while(confFile.is_open() && !confFile.eof())
466     {
467       //std::cout << "leyendo " << word << std::endl;
468       //get project name
469       std::getline(confFile,word,'(');
470       std::vector<std::string> wordBits;
471       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
472
473       if(wordBits[wordBits.size()-1] == "PROJECT")
474         {
475           std::getline(confFile,word,')');
476           std::vector<std::string> nameBits;
477           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
478
479           this->name = this->nameProject = "";
480           for (int i = 0; i < (int)(nameBits.size()); i++)
481             {
482               if(i != 0)
483                 this->name += " ";
484               this->name += nameBits[i];
485             }
486           this->nameProject = this->name;
487
488         }
489
490
491       if(wordBits[wordBits.size()-1] == "SET")
492         {
493           //get project version
494           std::getline(confFile,word,')');
495           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
496           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
497             {
498               version = wordBits[1];
499             }
500           if(wordBits[0] == "PROJECT_MINOR_VERSION")
501             {
502               version += "." + wordBits[1];
503             }
504           if(wordBits[0] == "PROJECT_BUILD_VERSION")
505             {
506               version += "." + wordBits[1];
507             }
508
509           //get project versionDate
510           if(wordBits[0] == "PROJECT_VERSION_DATE")
511             {
512               std::vector<std::string> versionBits;
513               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
514               versionDate = versionBits[0];
515             }
516         }
517     }
518   confFile.close();
519
520   this->type = wxDIR_DIRS;
521   this->level = 0;
522
523   std::vector<bool> checked(this->children.size(), false);
524   std::vector<bool> checkedPackages(this->packages.size(), false);
525
526   //check all folders
527   wxDir dir(crea::std2wx((this->path).c_str()));
528   if (dir.IsOpened())
529     {
530       wxString fileName;
531       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
532       while (cont)
533         {
534           std::string stdfileName = crea::wx2std(fileName);
535
536           //if appli, create appli
537           if(stdfileName == "appli")
538             {
539               if (this->appli == NULL)
540                 {
541                   this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
542                   this->children.push_back(this->appli);
543                 }
544               else
545                 {
546                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
547                   checked[pos] = true;
548                   if(!this->appli->Refresh(result))
549                     return false;
550                 }
551             }
552           //if lib, create lib
553           else if(stdfileName == "lib")
554             {
555               if (this->lib == NULL)
556                 {
557                   this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
558                   this->children.push_back(this->lib);
559                 }
560               else
561                 {
562                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
563                   checked[pos] = true;
564                   if(!this->lib->Refresh(result))
565                     return false;
566                 }
567
568             }
569           //if package , create package
570           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
571             {
572               bool found = false;
573               for (int i = 0; !found && i < (int)(this->packages.size()); i++)
574                 {
575                   if (this->packages[i]->GetName() == stdfileName)
576                     {
577                       found = true;
578                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
579                       checked[pos] = true;
580                       checkedPackages[i] = true;
581                       if(!this->packages[i]->Refresh(result))
582                         return false;
583                     }
584                 }
585               if(!found)
586                 {
587                   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
588                   this->packages.push_back(package);
589                   this->children.push_back(package);
590                 }
591
592             }
593           //if is an unknown folder, create folder
594           else
595             {
596               bool found = false;
597               for (int i = 0; !found && i < (int)(this->children.size()); i++)
598                 {
599                   if (this->children[i]->GetName() == stdfileName)
600                     {
601                       found = true;
602                       checked[i] = true;
603                       if(!this->children[i]->Refresh(result))
604                         return false;
605                     }
606                 }
607
608               if(!found)
609                 {
610                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
611                   this->children.push_back(folder);
612                 }
613             }
614
615           cont = dir.GetNext(&fileName);
616         }
617
618       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
619       while (cont)
620         {
621           std::string stdfileName = crea::wx2std(fileName);
622
623           //if CMakeLists, create CMakeLists
624           if(stdfileName == "CMakeLists.txt")
625             {
626               if (this->CMakeLists == NULL)
627                 {
628                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
629                   this->children.push_back(this->CMakeLists);
630                 }
631               else
632                 {
633                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
634                   checked[pos] = true;
635                   if(!this->CMakeLists->Refresh(result))
636                     return false;
637                 }
638             }
639           //if is an unknown file, create file
640           else
641             {
642               bool found = false;
643               for (int i = 0; !found && i < (int)(this->children.size()); i++)
644                 {
645                   if (this->children[i]->GetName() == stdfileName)
646                     {
647                       found = true;
648                       checked[i] = true;
649                       if(!this->children[i]->Refresh(result))
650                         return false;
651                     }
652                 }
653
654               if(!found)
655                 {
656                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
657                   this->children.push_back(file);
658                 }
659             }
660
661           cont = dir.GetNext(&fileName);
662         }
663     }
664
665   for (int i = 0; i < (int)(checkedPackages.size()); i++)
666     {
667       if(!checkedPackages[i])
668         {
669           this->packages.erase(this->packages.begin()+i);
670           checkedPackages.erase(checkedPackages.begin()+i);
671           i--;
672         }
673     }
674   for (int i = 0; i < (int)(checked.size()); i++)
675     {
676       if(!checked[i])
677         {
678           delete this->children[i];
679           this->children.erase(this->children.begin()+i);
680           checked.erase(checked.begin()+i);
681           i--;
682         }
683     }
684
685   this->SortChildren();
686   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
687   return true;
688 }
689
690 bool modelCDMProject::ConfigureBuild(std::string*& result)
691 {
692   //TODO: adjust for windows and mac
693 #ifdef _WIN32
694   // ------ Windows
695   if(0 == system("cmake-gui"))
696     return true;
697   else
698     {
699           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.");
700       return false;
701     }
702 #elif __APPLE__
703   // ------ Apple
704 #else
705   // ------ Linux
706   //open binary folder
707   wxDir dir(crea::std2wx((this->buildPath).c_str()));
708
709   //if folder doesn't exist then create it
710   if (!dir.IsOpened())
711     {
712       //create command line to create folder
713       std::string createComm = "mkdir \"" + this->buildPath + "\"";
714       //execute creation command
715       if (system(createComm.c_str()))
716         {
717           //if there was an error then report it
718           result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
719           return false;
720         }
721     }
722   //create command line to execute ccmake
723   //TODO:: adjust for different Linux distributions
724   std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
725   //execute command
726   if(CDMUtilities::openTerminal(confComm))
727     {
728       //if there was an error then report it
729       result = new std::string("There was an error opening the configuration tool in the desired place.");
730       return false;
731     }
732 #endif
733   return true;
734 }
735
736 bool modelCDMProject::Build(std::string*& result, const std::string& line)
737 {
738   //TODO: adjust for windows and mac
739 #ifdef _WIN32
740   // ------ Windows
741         //\\..\\IDE\\VCExpress.exe \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"
742 //      std::string command = "\"" + std::string(getenv("VS90COMNTOOLS")) + "..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &";
743         std::string command = "\"\"%VS90COMNTOOLS%..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &\"";
744         command = "start cmd.exe /k " + command + " &";
745   if(0 == system(command.c_str()))
746     return true;
747   else
748     {
749       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.");
750           return false;
751     }
752 #elif __APPLE__
753   // ------ Apple
754 #else
755   // ------ Linux
756   //open binary folder
757   wxDir dir(crea::std2wx((this->buildPath).c_str()));
758
759   //if binary folder can't be opened then return false
760   if (!dir.IsOpened())
761     {
762       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
763       return false;
764     }
765   //create make command
766   std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
767   for (int i = 0; i < line.size(); i++)
768     {
769       if(line[i] == '"')
770         {
771           makeComm+="\\\\\\\"";
772         }
773       else if(line[i] == '\\')
774         {
775           makeComm+="\\\\\\\\";
776         }
777       else
778         {
779           makeComm.push_back(line[i]);
780         }
781     }
782   makeComm += "; echo -e '\\a'; bash";
783   makeComm += "\\\"\"";
784
785   std::cout << "executing '" << makeComm << "'" << std::endl;
786   //execute make command
787   if(system(makeComm.c_str()))
788     {
789       //if there was an error then report it
790       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.");
791       return false;
792     }
793 #endif
794   return true;
795 }
796
797 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
798 {
799   //TODO: adjust for mac
800 #ifdef _WIN32
801   // ------ Windows
802   //open binary folder
803   wxDir dir(crea::std2wx(folder));
804
805   //if binary folder can't be opened then return false
806   if (!dir.IsOpened())
807     {
808       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
809       return false;
810     }
811   //create plug command
812   std::string plugComm = "bbPlugPackage \"" + folder + "\"";
813   std::cout << "executing '" << plugComm << "'" << std::endl;
814   //execute plug command
815   if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
816     {
817       //if there was an error then report it
818       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
819       return false;
820     }
821 #elif __APPLE__
822   // ------ Apple
823 #else
824   // ------ Linux
825   //open binary folder
826   wxDir dir(crea::std2wx(folder));
827
828   //if binary folder can't be opened then return false
829   if (!dir.IsOpened())
830     {
831       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
832       return false;
833     }
834   //create plug command
835   std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
836   std::cout << "executing '" << plugComm << "'" << std::endl;
837   //execute plug command
838   if(system(plugComm.c_str()))
839     {
840       //if there was an error then report it
841       result = new std::string("There was an error plugging the packages of the project, please check the plugging.log in the build folder file to read more about the problem.");
842       return false;
843     }
844 #endif
845   return true;
846 }
847
848 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
849 {
850   //check cmake exist
851   if(this->CMakeLists != NULL)
852     {
853       //set properties parameters based on model
854       properties["project add appli"] = this->appli != NULL;
855       properties["project add lib"] = this->lib != NULL;
856       for (int i = 0; i < (int)(this->packages.size()); i++)
857         properties["project add " + packages[i]->GetName()] = false;
858
859       //open cmakelists
860       std::ifstream confFile;
861       confFile.open((this->CMakeLists->GetPath()).c_str());
862
863       //take everything that is not commented
864       std::string fileContent;
865
866       std::string word;
867       std::vector<std::string> words;
868       while(confFile.is_open() && !confFile.eof())
869         {
870           std::getline(confFile,word, '\n');
871           if(word[0] != '#')
872             {
873               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
874               if (words.size() > 0)
875                 {
876                   word = words[0];
877                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
878                   for (int i = 0; i < (int)(words.size()); i++)
879                     {
880                       if(words[i].substr(0,2) == "//")
881                         break;
882                       fileContent += words[i] + " ";
883                     }
884                 }
885
886             }
887         }
888
889       //check every instruction
890       std::stringstream ss(fileContent);
891       while(!ss.eof())
892         {
893           std::getline(ss,word, '(');
894
895           //check instruction name
896           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
897
898           //set instructions
899           if (words.size() > 0 && words[words.size()-1] == "SET")
900             {
901               std::getline(ss,word, ')');
902
903               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
904               if (words.size() > 1)
905                 {
906                   if (words[0] == "USE_CREA" && words[1] == "ON")
907                     {
908                       properties["project set USE_CREA"] = true;
909                     }
910                   else if (words[0] == "USE_GDCM" && words[1] == "ON")
911                     {
912                       properties["project set USE_GDCM"] = true;
913                     }
914                   else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
915                     {
916                       properties["project set USE_GDCM_VTK"] = true;
917                     }
918                   else if (words[0] == "USE_GDCM2" && words[1] == "ON")
919                     {
920                       properties["project set USE_GDCM2"] = true;
921                     }
922                   else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
923                     {
924                       properties["project set USE_WXWIDGETS"] = true;
925                     }
926                   else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
927                     {
928                       properties["project set USE_KWWIDGETS"] = true;
929                     }
930                   else if (words[0] == "USE_VTK" && words[1] == "ON")
931                     {
932                       properties["project set USE_VTK"] = true;
933                     }
934                   else if (words[0] == "USE_ITK" && words[1] == "ON")
935                     {
936                       properties["project set USE_ITK"] = true;
937                     }
938                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
939                     {
940                       properties["project set USE_BOOST"] = true;
941                     }
942                 }
943             }
944           //add instructions
945           else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
946             {
947               std::getline(ss,word, ')');
948               //std::cout << word << std::endl;
949               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
950
951               if (words.size() > 0)
952                 {
953                   if(words[0] == "appli")
954                     {
955                       properties["project add "+words[0]] = this->appli != NULL;
956                     }
957                   else if(words[0] == "lib")
958                     {
959                       properties["project add "+words[0]] = this->lib != NULL;
960                     }
961                   else
962                     {
963                       properties["project add "+words[0]] = true;
964                     }
965                 }
966             }
967         }
968
969     }
970
971   //check appli's structure
972   this->appli->CheckStructure(properties);
973   //check lib's structure
974   this->lib->CheckStructure(properties);
975   //check packages' structure
976   for (int i = 0; i < (int)(this->packages.size()); i++)
977     {
978       properties["package " + this->packages[i]->GetName()] = true;
979       this->packages[i]->CheckStructure(properties);
980     }
981 }