]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMPackageSrc.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMPackageSrc.cpp
index 16e7e59b0b65cd67829cec8f41cabbd4eaad3d75..06d0c09d2da2c07a48323c63e314cbd1d1490cfb 100644 (file)
@@ -49,6 +49,7 @@ modelCDMPackageSrc::modelCDMPackageSrc()
 
 modelCDMPackageSrc::modelCDMPackageSrc(const std::string& path, const std::string& name, const int& level)
 {
+  std::cout << "creating package src: " + path + "\n";
   //set attributes
   this->children.clear();
   this->level = level;
@@ -109,6 +110,7 @@ modelCDMPackageSrc::modelCDMPackageSrc(const std::string& path, const std::strin
     }
 
   this->SortChildren();
+  std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
 }
 
 modelCDMPackageSrc::~modelCDMPackageSrc()
@@ -130,21 +132,151 @@ const std::vector<modelCDMBlackBox*>& modelCDMPackageSrc::GetBlackBoxes() const
 }
 
 modelCDMBlackBox* modelCDMPackageSrc::CreateBlackBox(
+    std::string*& result,
     const std::string& name,
     const std::string& package,
     const std::string& type,
     const std::string& format,
+    const std::string& categories,
     const std::string& authors,
     const std::string& authorsEmail,
-    const std::string& categories,
     const std::string& description)
 {
-  //TODO: implement method
-  return NULL;
+  //parse name
+  std::vector<std::string> words;
+  CDMUtilities::splitter::split(words, name, " \n\",/\\", CDMUtilities::splitter::no_empties);
+  std::string bbName;
+  for (int i = 0; i < words.size(); i++)
+    {
+      bbName += words[i];
+    }
+
+  //parse categories
+  CDMUtilities::splitter::split(words, categories, " \n\",/\\", CDMUtilities::splitter::no_empties);
+  std::string bbCategories;
+  if(words.size() > 0)
+    {
+      bbCategories = words[0];
+      for (int i = 1; i < words.size(); i++)
+        {
+          bbCategories += "," + words[i];
+        }
+    }
+  if(bbCategories == "")
+    bbCategories = "empty";
+
+  //parse authors
+  CDMUtilities::splitter::split(words, authors, "\n\",/\\", CDMUtilities::splitter::no_empties);
+  std::string bbAuthors;
+  if(words.size() > 0)
+    {
+      bbAuthors = words[0];
+      for (int i = 1; i < words.size(); i++)
+        {
+          bbAuthors += "," + words[i];
+        }
+    }
+  if(bbAuthors == "")
+    bbAuthors = "Unknown";
+
+  //parse description
+  CDMUtilities::splitter::split(words, authorsEmail, " \n\"/\\", CDMUtilities::splitter::no_empties);
+  std::string bbDescription;
+  if(words.size() > 0)
+    {
+      bbDescription = words[0];
+      for (int i = 1; i < words.size(); i++)
+        {
+          bbDescription += "," + words[i];
+        }
+      bbDescription += " - ";
+    }
+  CDMUtilities::splitter::split(words, description, "\n\"/\\", CDMUtilities::splitter::no_empties);
+  if(words.size() > 0)
+    {
+      bbDescription += words[0];
+      for (int i = 1; i < words.size(); i++)
+        {
+          bbDescription += words[i];
+        }
+    }
+
+  if(bbDescription == "")
+    bbDescription = "No Description.";
+
+
+  //create command
+  std::string command = "bbCreateBlackBox";
+  command += " \"" + this->path + "\"";
+  command += " \"" + package + "\"";
+  command += " \"" + bbName + "\"";
+  command += " \"" + type + "\"";
+  command += " \"" + format + "\"";
+  command += " \"" + bbAuthors + "\"";
+  command += " \"" + bbDescription + "\"";
+  command += " \"" + bbCategories + "\"";
+
+  //excecute command
+  if(system(command.c_str()))
+    {
+      result = new std::string("Error executing command '" + command + "'");
+      return NULL;
+    }
+
+  //if command succeed
+
+  //create header
+  //create source
+  modelCDMFile* header = NULL;
+  modelCDMFile* source = NULL;
+  wxDir dir(crea::std2wx(path));
+  if (dir.IsOpened())
+    {
+      wxString fileName;
+      bool cont = dir.GetFirst(&fileName, crea::std2wx("bb"+package+bbName+".h"), wxDIR_FILES);
+      if (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+          header = new modelCDMFile(this->path + stdfileName, stdfileName, this->level+1);
+        }
+      cont = dir.GetFirst(&fileName, crea::std2wx("bb"+package+bbName+".cxx"), wxDIR_FILES);
+      if (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+          source = new modelCDMFile(this->path + stdfileName, stdfileName, this->level+1);
+        }
+    }
+  //if source and header exist
+  if (header != NULL && source != NULL)
+    {
+      //create black box
+      modelCDMBlackBox* blackBox = new modelCDMBlackBox(this->path, package+bbName);
+
+      //associate header and source
+      blackBox->SetHeaderFile(header);
+      blackBox->SetSourceFile(source);
+
+      this->children.push_back(header);
+      this->children.push_back(source);
+
+      this->blackBoxes.push_back(blackBox);
+
+      //sort children
+      std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
+      this->SortChildren();
+
+      return blackBox;
+    }
+  else
+    {
+      result = new std::string("The header and source files were not found. Black box not created.");
+      return NULL;
+    }
 }
 
 const bool modelCDMPackageSrc::Refresh(std::string*& result)
 {
+  std::cout << "refreshing package src" << std::endl;
   //set attributes
   this->type = wxDIR_DIRS;
 
@@ -164,7 +296,7 @@ const bool modelCDMPackageSrc::Refresh(std::string*& result)
           std::string folderName = stdfileName;
           //check if they already exist
           bool found = false;
-          for (int i = 0;!found && i < this->children.size(); i++)
+          for (int i = 0; !found && i < this->children.size(); i++)
             {
               if (this->children[i]->GetName() == folderName)
                 {
@@ -207,7 +339,7 @@ const bool modelCDMPackageSrc::Refresh(std::string*& result)
           else
             {
               bool found = false;
-              for (int i = 0; i <!found && this->children.size(); i++)
+              for (int i = 0; !found && i < this->children.size(); i++)
                 {
                   if (this->children[i]->GetName() == stdfileName)
                     {
@@ -275,5 +407,6 @@ const bool modelCDMPackageSrc::Refresh(std::string*& result)
         }
     }
   this->SortChildren();
+  std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
   return true;
 }