]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMFolder.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMFolder.cpp
index 8122ebb063103d94cc23c4bf16b8008ab9e828db..28d7a9581e7a96479c013b2dc444e60012393c25 100644 (file)
@@ -34,6 +34,9 @@
 
 #include "modelCDMFolder.h"
 
+#include <fstream>
+#include <algorithm>
+
 #include <creaWx.h>
 #include <wx/dir.h>
 
@@ -117,11 +120,20 @@ modelCDMFolder::~modelCDMFolder()
   this->children.clear();
 }
 
-bool modelCDMFolder::CreateFolder(const std::string& name, std::string*& result,
-    const std::string& path)
+modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
 {
-  //TODO: implement method
-  return true;
+  //TODO:: mkdir depending on OS
+  std::string command = "mkdir " + path + "/" + name;
+  if(system(command.c_str()))
+    {
+      result = new std::string("Error executing: " + command + ".");
+      return NULL;
+    }
+  modelCDMFolder* folder = new modelCDMFolder(path + "/" + name, level + 1);
+  this->folders.push_back(folder);
+  this->children.push_back(folder);
+
+  return folder;
 }
 
 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
@@ -142,7 +154,113 @@ bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
 
 const bool modelCDMFolder::Refresh(std::string*& result)
 {
-  //TODO: implement method
+  //set attributes
+  this->type = wxDIR_DIRS;
+
+  //check children
+  std::vector<bool> checked(this->children.size(), false);
+  std::vector<bool> checkedFolders(this->folders.size(), false);
+
+  //check all folders
+  wxDir dir(crea::std2wx((this->path).c_str()));
+  if (dir.IsOpened())
+    {
+      wxString fileName;
+      bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+      while (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+          std::string folderName = stdfileName;
+          //check if they already exist
+          bool found = false;
+          for (int i = 0;!found && i < this->folders.size(); i++)
+            {
+              if (this->folders[i]->GetName() == folderName)
+                {
+                  found = true;
+                  int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
+                  checked[pos] = true;
+                  checkedFolders[i] = true;
+                  if(!this->folders[i]->Refresh(result))
+                    return false;
+                }
+            }
+          if(!found)
+            {
+              modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
+              this->folders.push_back(folder);
+              this->children.push_back(folder);
+            }
+          cont = dir.GetNext(&fileName);
+        }
+
+      cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
+      while (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+
+          //if CMakeLists, create CMakeLists
+          if(stdfileName == "CMakeLists.txt")
+            {
+              if (this->CMakeLists == NULL)
+                {
+                  this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
+                  this->children.push_back(this->CMakeLists);
+                }
+              else
+                {
+                  int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
+                  checked[pos] = true;
+                  if(!this->CMakeLists->Refresh(result))
+                    return false;
+                }
+            }
+          //if is an unknown file, create file
+          else
+            {
+              bool found = false;
+              for (int i = 0; i <!found && this->children.size(); i++)
+                {
+                  if (this->children[i]->GetName() == stdfileName)
+                    {
+                      found = true;
+                      checked[i] = true;
+                      if(!this->children[i]->Refresh(result))
+                        return false;
+                    }
+                }
+
+              if(!found)
+                {
+                  modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
+                  this->children.push_back(file);
+                }
+            }
+
+          cont = dir.GetNext(&fileName);
+        }
+    }
+
+  for (int i = 0; i < checkedFolders.size(); i++)
+    {
+      if(!checkedFolders[i])
+        {
+          this->folders.erase(this->folders.begin()+i);
+          checkedFolders.erase(checkedFolders.begin()+i);
+          i--;
+        }
+    }
+  for (int i = 0; i < checked.size(); i++)
+    {
+      if(!checked[i])
+        {
+          delete this->children[i];
+          this->children.erase(this->children.begin()+i);
+          checked.erase(checked.begin()+i);
+          i--;
+        }
+    }
+  this->SortChildren();
   return true;
 }