VPTissue Reference Manual
Workspace.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "workspace/Workspace.h"
21 
22 #include "common/InstallDirs.h"
23 #include "workspace/CliWorkspace.h"
24 #include "workspace/GuiWorkspace.h"
27 
28 using namespace std;
29 using namespace boost::property_tree;
30 using namespace boost::property_tree::xml_parser;
31 using namespace SimPT_Sim::Util;
32 
33 namespace SimShell { namespace Ws {
34  template class Workspace<SimPT_Shell::Ws::Project,
35  SimPT_Shell::Ws::WorkspaceFactory::g_workspace_index_file>;
36 }}
37 
38 namespace SimPT_Shell {
39 namespace Ws {
40 
41 using namespace SimShell;
42 using namespace SimShell::Ws;
43 
44 Workspace::Workspace(const string& path,
45  const string& prefs_file)
46  : SimShell::Ws::Workspace<Project, WorkspaceFactory::g_workspace_index_file>(path, prefs_file)
47 {
48  string prefs_path = path + '/' + prefs_file;
49 
50  if (!QFile::exists(QString::fromStdString(prefs_path))) {
51  ptree default_prefs;
52 
53  WorkspaceFactory f;
54  const string default_prefs_file = f.GetWorkspaceTemplatePath() + '/' + prefs_file;
55  read_xml(default_prefs_file, default_prefs, trim_whitespace);
56 
57  SetPreferences(default_prefs.get_child("preferences"));
58  }
59 }
60 
61 void Workspace::Init(const string& p)
62 {
63  string path = p;
64 
65  // make sure path doesn't end with a '/'
66  if (path.length() > 0 && path.rfind("/") == path.length() - 1) {
67  path = path.erase(path.length() - 1);
68  }
69 
70  // Overwrite preferences files
71  string cli_prefs_filename = path + '/' + CliWorkspace::g_preferences_file;
72  string gui_prefs_filename = path + '/' + GuiWorkspace::g_preferences_file;
73 
74  if (QFile::exists(QString::fromStdString(cli_prefs_filename))) {
75  if (!QFile::remove(QString::fromStdString(cli_prefs_filename))) {
76  throw Exception("Unable to overwrite \"" + cli_prefs_filename + '"');
77  }
78  }
79  if (QFile::exists(QString::fromStdString(gui_prefs_filename))) {
80  if (!QFile::remove(QString::fromStdString(gui_prefs_filename))) {
81  throw Exception("Unable to overwrite \"" + gui_prefs_filename + '"');
82  }
83  }
84 
85  WorkspaceFactory f;
86  string default_cli_prefs_filename = f.GetWorkspaceTemplatePath()
87  + '/' + CliWorkspace::g_preferences_file;
88  string default_gui_prefs_filename = f.GetWorkspaceTemplatePath()
89  + '/' + GuiWorkspace::g_preferences_file;
90 
91  if (!QFile::copy(QString::fromStdString(default_cli_prefs_filename),
92  QString::fromStdString(cli_prefs_filename))) {
93  throw Exception("Unable to copy file \"" + default_cli_prefs_filename
94  + "\". Your installation could be corrupted.");
95  }
96 
97  if (!QFile::copy(QString::fromStdString(default_gui_prefs_filename),
98  QString::fromStdString(gui_prefs_filename))) {
99  throw Exception("Unable to copy file \"" + default_gui_prefs_filename
100  + "\". Your installation could be corrupted.");
101  }
102 
103  // Create empty workspace index file
104  string index_filename = path + '/' + WorkspaceFactory::g_workspace_index_file;
105  if (!QFile::exists(QString::fromStdString(index_filename))) {
106  ptree pt_workspace;
107  pt_workspace.put("workspace.projects", "");
108 
109  try {
110  write_xml(index_filename, pt_workspace, std::locale(), XmlWriterSettings::GetTab());
111  }
112  catch (xml_parser_error & e) {
113  throw Exception("Could not initialize workspace \"" + path + '"');
114  }
115  }
116 }
117 
118 } // namespace
119 } // namespace
Abstraction of project info on filesystem, as well as a maintainer of a work session.
STL namespace.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Namespace for SimPT shell package.
Definition: Client.cpp:50
Abstraction of project in workspace on file system.
Interface for workspace.
Extremely simple Exception root class.
Definition: Exception.h:28
Namespace for generic workspace classes.
Definition: SimSession.h:32
Interface for workspace.
Xml writer settings class.
Implementation for Workspace.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
Interface for FileSys.