Fraxinus  17.12-rc4
An IGT application
cxProfile.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxProfile.h"
34 
35 #include "cxDataLocations.h"
36 
37 #include <iostream>
38 #include <QApplication>
39 #include <QDir>
40 #include "cxConfig.h"
41 #include "cxSettings.h"
42 #include "cxFileHelpers.h"
43 #include "cxTypeConversions.h"
44 #include "cxXmlOptionItem.h"
45 #include "cxStringProperty.h"
46 #include "cxLogger.h"
47 
48 namespace cx
49 {
50 
51 //---------------------------------------------------------
52 //---------------------------------------------------------
53 //---------------------------------------------------------
54 
55 
57 {
58  mPath = path;
59  mSettings = settings;
60 }
61 
63 {
64  this->getXmlSettings().save();
65 }
66 
68 {
69  // this will trigger lots of change signals. Do after Profile object is in place.
70  mSettings->resetFile(this->getSettingsFile());
71 }
72 
74 {
75  QString filename = this->getSettingsPath() + "/settings.xml";
76  return XmlOptionFile(filename);
77 }
78 
80 {
81  return mSettings.get();
82 }
83 
84 QString Profile::getSettingsFile()
85 {
86  QString filename = this->getSettingsPath() + "/settings.ini";
87  return filename;
88 }
89 
90 QString Profile::getName() const
91 {
92  return this->getUid();
93 }
94 
95 QString Profile::getUid() const
96 {
97  return QFileInfo(mPath).fileName();
98 }
99 
100 QString Profile::getPath() const
101 {
102  return mPath;
103 }
104 
106 {
107  return QStringList() << this->getPath() + "/tool/";
108 }
109 
111 {
112  QString expectedPath = this->getApplicationToolConfigPaths().front();
113 
114  QFileInfo info(path);
115  if (info.absolutePath() != expectedPath)
116  CX_LOG_WARNING() << "Set ref to file " << path << ", should be in folder " << expectedPath;
117 
118  settings()->setValue("toolConfigFile", info.fileName());
119 }
120 
122 {
123  QString path = this->getApplicationToolConfigPaths().front();
124  QString filename = this->getSettings()->value("toolConfigFile").toString();
125  if (filename.isEmpty())
126  return "";
127  return path + "/" + filename;
128 }
129 
131 {
132  return this->getPath() + "/settings";
133 }
134 
136 {
137  return DataLocations::getRootConfigPath() + "/patient_templates";
138 }
139 
141 {
142  QStringList retval = DataLocations::getRootConfigPaths();
143  retval << this->getPath();
144  return retval;
145 }
146 
147 
148 QString Profile::getDefaultSessionRootFolder() const
149 {
150  QStringList path;
151  path << QDir::homePath() << "Patients" << this->getName();
152  return path.join("/");
153 }
154 
156 {
157  QString folder = this->getSettings()->value("globalPatientDataFolder",
158  this->getDefaultSessionRootFolder()).toString();
159 
160  // Create folders
161  if (!QDir().exists(folder))
162  {
163  if(QDir().mkpath(folder))
164  report("Made a new patient folder: " + folder);
165  else
166  reportWarning("Cannot make new patient folder: " + folder);
167  }
168 
169  return folder;
170 }
171 
173 {
174  this->getSettings()->setValueIfNotDefault("globalPatientDataFolder", path, this->getDefaultSessionRootFolder());
175 }
176 
177 //---------------------------------------------------------
178 //---------------------------------------------------------
179 //---------------------------------------------------------
180 
181 cxResource_EXPORT ProfilePtr profile()
182 {
184 }
185 
186 ProfileManager *ProfileManager::mInstance = NULL;
187 
189 {
190  if (mInstance == NULL)
191  {
192  mInstance = new ProfileManager(defaultProfile);
193  }
194  return mInstance;
195 }
196 
198 {
200 }
201 
203 {
204  delete mInstance;
205  mInstance = NULL;
206 }
207 
208 ProfileManager::ProfileManager(QString defaultProfile)
209 {
210  QString profileUid = this->getDefaultProfileUid(defaultProfile);
211  profileUid = this->getGenericSettings()->value("profile", profileUid).toString();
212 
213  mSettings.reset(new Settings());
214 
215  this->setActiveProfile(profileUid);
216 }
217 
218 ProfileManager::~ProfileManager()
219 {
220 
221 }
222 
223 QString ProfileManager::getDefaultProfileUid(QString defaultProfile)
224 {
225  QStringList installed = this->getInstalledProfiles();
226  if (installed.contains(defaultProfile))
227  {
228  return defaultProfile;
229  }
230  else if (!installed.isEmpty())
231  {
232  return installed.front();
233  }
234  else
235  {
236  return "default";
237  }
238 }
239 
241 {
242  return DataLocations::getPersistentWritablePath()+"/settings";
243 }
244 
245 QSettingsPtr ProfileManager::getGenericSettings()
246 {
247  QString filename = this->getCustomPath() + "/generic_settings.ini";
248  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
249 }
250 
251 QStringList ProfileManager::getInstalledProfiles()
252 {
253  QStringList configPaths = DataLocations::getRootConfigPaths();
254  QStringList profiles;
255  for (int i=0; i< configPaths.size(); ++i)
256  profiles << getProfilesInFolder(configPaths[i]+"/profiles");
257  return profiles;
258 }
259 QStringList ProfileManager::getCustomProfiles()
260 {
261  QStringList profiles;
262  profiles << getProfilesInFolder(this->getCustomPath());
263  return profiles;
264 }
265 
266 QString ProfileManager::getCustomPath()
267 {
268  return DataLocations::getPersistentWritablePath() + "/profiles";
269 }
270 
271 QStringList ProfileManager::getProfilesInFolder(QString folder)
272 {
273  QDir dir(folder);
274  return dir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
275 }
276 
278 {
279  QStringList profiles = this->getInstalledProfiles();
280  profiles << getProfilesInFolder(this->getCustomPath());
281  profiles.removeDuplicates();
282  return profiles;
283 }
284 
285 void ProfileManager::newProfile(QString uid)
286 {
287  QString path = this->getPathForCustom(uid);
288 
289  QDir dir(path);
290  dir.mkpath(".");
291  dir.mkpath("tool");
292 
293  this->profilesChanged();
294 }
295 
296 void ProfileManager::copyProfile(QString base, QString uid)
297 {
298  QString newPath = this->getPathForCustom(uid);
299 
300  if (!copyRecursively(base, newPath, true))
301  CX_LOG_WARNING() << "Failed to copy profile " << base << " to " << newPath;
302 
303  this->profilesChanged();
304 }
305 
307 {
308  return mActive;
309 }
310 
311 QString ProfileManager::getPathForInstalled(QString uid)
312 {
313  QStringList configPaths = DataLocations::getRootConfigPaths();
314  QStringList profiles;
315  for (int i=0; i< configPaths.size(); ++i)
316  {
317  QFileInfo info(configPaths[i]+"/profiles/"+uid);
318  if (info.exists())
319  return info.canonicalFilePath();
320  }
321  return "";
322 }
323 
324 QString ProfileManager::getPathForCustom(QString uid)
325 {
326  return this->getCustomPath() + "/" + uid;
327 }
328 
330 {
331  if (mActive && mActive->getUid()==uid)
332  return;
333 
334  if (!this->getCustomProfiles().contains(uid))
335  {
336  this->createCustomProfile(uid);
337  }
338 
339  // uid now is guaranteed to exist in the custom folder
340 
341  mActive.reset(new Profile(this->getPathForCustom(uid), mSettings));
342  this->getGenericSettings()->setValue("profile", mActive->getUid());
343  mActive->activate();
344  emit activeProfileChanged();
345 // CX_LOG_INFO() << "Set profile " << mActive->getName();
346 }
347 
348 void ProfileManager::createCustomProfile(QString uid)
349 {
350  if (this->getInstalledProfiles().contains(uid))
351  {
352  QString path = this->getPathForInstalled(uid);
353  this->copyProfile(path, uid);
354  }
355  else
356  {
357  if (mActive)
358  {
359  this->copyProfile(mActive->getPath(), uid);
360  }
361  else if (!this->getInstalledProfiles().isEmpty())
362  {
363  QString base = this->getInstalledProfiles().front();
364  QString basePath = this->getPathForInstalled(base);
365  this->copyProfile(basePath, uid);
366  }
367  else
368  {
369  this->newProfile(uid);
370  }
371  }
372 }
373 
374 void ProfileManager::profilesChanged()
375 {
376 }
377 
378 
379 
380 } // namespace cx
QStringList getApplicationToolConfigPaths()
Definition: cxProfile.cpp:105
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:181
ProfilePtr activeProfile()
Definition: cxProfile.cpp:306
void activate()
Definition: cxProfile.cpp:67
Settings * getSettings() const
Definition: cxProfile.cpp:79
static void initialize()
Definition: cxProfile.cpp:197
QString getSettingsPath()
Definition: cxProfile.cpp:130
boost::shared_ptr< class Settings > SettingsPtr
Definition: cxProfile.h:46
static void shutdown()
Definition: cxProfile.cpp:202
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:188
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:87
static QStringList getRootConfigPaths()
QStringList getProfiles()
Definition: cxProfile.cpp:277
void setValueIfNotDefault(const QString &key, const QVariant &value, const QVariant &defaultValue)
Definition: cxSettings.cpp:72
void setToolConfigFilePath(QString path)
Definition: cxProfile.cpp:110
bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:329
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:79
QString getUid() const
Definition: cxProfile.cpp:95
Profile(QString path, SettingsPtr settings)
Definition: cxProfile.cpp:56
Customized interface for setting values in QSettings.
Definition: cxSettings.h:58
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
boost::shared_ptr< class QSettings > QSettingsPtr
Definition: cxProfile.h:45
XmlOptionFile getXmlSettings()
internal use
Definition: cxProfile.cpp:73
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
static QString getPersistentWritablePath()
Path to location usable for persistent and temporary storage of config. Do not use directly...
QString getToolConfigFilePath()
Definition: cxProfile.cpp:121
QStringList getAllRootConfigPaths()
Definition: cxProfile.cpp:140
QString getPath() const
Definition: cxProfile.cpp:100
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
void report(QString msg)
Definition: cxLogger.cpp:90
#define CX_LOG_WARNING
Definition: cxLogger.h:119
void save()
save entire document.
QString getSettingsPath()
Definition: cxProfile.cpp:240
void setSessionRootFolder(QString path)
Definition: cxProfile.cpp:172
boost::shared_ptr< class Profile > ProfilePtr
Definition: cxProfile.h:50
bool contains(std::string const &s, std::string const &infix)
Definition: catch.hpp:168
QString getSessionRootFolder() const
Definition: cxProfile.cpp:155
Helper class for xml files used to store ssc/cx data.
QString getPatientTemplatePath()
Definition: cxProfile.cpp:135
QString getName() const
Definition: cxProfile.cpp:90
Namespace for all CustusX production code.