CustusX  18.04-rc5
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxProfile.h"
13 
14 #include "cxDataLocations.h"
15 
16 #include <iostream>
17 #include <QApplication>
18 #include <QDir>
19 #include "cxConfig.h"
20 #include "cxSettings.h"
21 #include "cxFileHelpers.h"
22 #include "cxTypeConversions.h"
23 #include "cxXmlOptionItem.h"
24 #include "cxStringProperty.h"
25 #include "cxLogger.h"
26 
27 namespace cx
28 {
29 
30 //---------------------------------------------------------
31 //---------------------------------------------------------
32 //---------------------------------------------------------
33 
34 
36 {
37  mPath = path;
38  mSettings = settings;
39 }
40 
42 {
43  this->getXmlSettings().save();
44 }
45 
47 {
48  // this will trigger lots of change signals. Do after Profile object is in place.
49  mSettings->resetFile(this->getSettingsFile());
50 }
51 
53 {
54  QString filename = this->getSettingsPath() + "/settings.xml";
55  return XmlOptionFile(filename);
56 }
57 
59 {
60  return mSettings.get();
61 }
62 
63 QString Profile::getSettingsFile()
64 {
65  QString filename = this->getSettingsPath() + "/settings.ini";
66  return filename;
67 }
68 
69 QString Profile::getName() const
70 {
71  return this->getUid();
72 }
73 
74 QString Profile::getUid() const
75 {
76  return QFileInfo(mPath).fileName();
77 }
78 
79 QString Profile::getPath() const
80 {
81  return mPath;
82 }
83 
85 {
86  return QStringList() << this->getPath() + "/tool/";
87 }
88 
90 {
91  QString expectedPath = this->getApplicationToolConfigPaths().front();
92 
93  QFileInfo info(path);
94  if (info.absolutePath() != expectedPath)
95  CX_LOG_WARNING() << "Set ref to file " << path << ", should be in folder " << expectedPath;
96 
97  settings()->setValue("toolConfigFile", info.fileName());
98 }
99 
101 {
102  QString path = this->getApplicationToolConfigPaths().front();
103  QString filename = this->getSettings()->value("toolConfigFile").toString();
104  if (filename.isEmpty())
105  return "";
106  return path + "/" + filename;
107 }
108 
110 {
111  return this->getPath() + "/settings";
112 }
113 
115 {
116  return DataLocations::getRootConfigPath() + "/patient_templates";
117 }
118 
120 {
121  QStringList retval = DataLocations::getRootConfigPaths();
122  retval << this->getPath();
123  return retval;
124 }
125 
126 
127 QString Profile::getDefaultSessionRootFolder() const
128 {
129  QStringList path;
130  path << QDir::homePath() << "Patients" << this->getName();
131  return path.join("/");
132 }
133 
135 {
136  QString folder = this->getSettings()->value("globalPatientDataFolder",
137  this->getDefaultSessionRootFolder()).toString();
138 
139  // Create folders
140  if (!QDir().exists(folder))
141  {
142  if(QDir().mkpath(folder))
143  report("Made a new patient folder: " + folder);
144  else
145  reportWarning("Cannot make new patient folder: " + folder);
146  }
147 
148  return folder;
149 }
150 
152 {
153  this->getSettings()->setValueIfNotDefault("globalPatientDataFolder", path, this->getDefaultSessionRootFolder());
154 }
155 
156 //---------------------------------------------------------
157 //---------------------------------------------------------
158 //---------------------------------------------------------
159 
160 cxResource_EXPORT ProfilePtr profile()
161 {
163 }
164 
165 ProfileManager *ProfileManager::mInstance = NULL;
166 
168 {
169  if (mInstance == NULL)
170  {
171  mInstance = new ProfileManager(defaultProfile);
172  }
173  return mInstance;
174 }
175 
177 {
179 }
180 
182 {
183  delete mInstance;
184  mInstance = NULL;
185 }
186 
187 ProfileManager::ProfileManager(QString defaultProfile)
188 {
189  QString profileUid = this->getDefaultProfileUid(defaultProfile);
190  profileUid = this->getGenericSettings()->value("profile", profileUid).toString();
191 
192  mSettings.reset(new Settings());
193 
194  this->setActiveProfile(profileUid);
195 }
196 
197 ProfileManager::~ProfileManager()
198 {
199 
200 }
201 
202 QString ProfileManager::getDefaultProfileUid(QString defaultProfile)
203 {
204  QStringList installed = this->getInstalledProfiles();
205  if (installed.contains(defaultProfile))
206  {
207  return defaultProfile;
208  }
209  else if (!installed.isEmpty())
210  {
211  return installed.front();
212  }
213  else
214  {
215  return "default";
216  }
217 }
218 
220 {
221  return DataLocations::getPersistentWritablePath()+"/settings";
222 }
223 
224 QSettingsPtr ProfileManager::getGenericSettings()
225 {
226  QString filename = this->getCustomPath() + "/generic_settings.ini";
227  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
228 }
229 
230 QStringList ProfileManager::getInstalledProfiles()
231 {
232  QStringList configPaths = DataLocations::getRootConfigPaths();
233  QStringList profiles;
234  for (int i=0; i< configPaths.size(); ++i)
235  profiles << getProfilesInFolder(configPaths[i]+"/profiles");
236  return profiles;
237 }
238 QStringList ProfileManager::getCustomProfiles()
239 {
240  QStringList profiles;
241  profiles << getProfilesInFolder(this->getCustomPath());
242  return profiles;
243 }
244 
245 QString ProfileManager::getCustomPath()
246 {
247  return DataLocations::getPersistentWritablePath() + "/profiles";
248 }
249 
250 QStringList ProfileManager::getProfilesInFolder(QString folder)
251 {
252  QDir dir(folder);
253  return dir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
254 }
255 
257 {
258  QStringList profiles = this->getInstalledProfiles();
259  profiles << getProfilesInFolder(this->getCustomPath());
260  profiles.removeDuplicates();
261  return profiles;
262 }
263 
264 void ProfileManager::newProfile(QString uid)
265 {
266  QString path = this->getPathForCustom(uid);
267 
268  QDir dir(path);
269  dir.mkpath(".");
270  dir.mkpath("tool");
271 
272  this->profilesChanged();
273 }
274 
275 void ProfileManager::copyProfile(QString base, QString uid)
276 {
277  QString newPath = this->getPathForCustom(uid);
278 
279  if (!copyRecursively(base, newPath, true))
280  CX_LOG_WARNING() << "Failed to copy profile " << base << " to " << newPath;
281 
282  this->profilesChanged();
283 }
284 
286 {
287  return mActive;
288 }
289 
290 QString ProfileManager::getPathForInstalled(QString uid)
291 {
292  QStringList configPaths = DataLocations::getRootConfigPaths();
293  QStringList profiles;
294  for (int i=0; i< configPaths.size(); ++i)
295  {
296  QFileInfo info(configPaths[i]+"/profiles/"+uid);
297  if (info.exists())
298  return info.canonicalFilePath();
299  }
300  return "";
301 }
302 
303 QString ProfileManager::getPathForCustom(QString uid)
304 {
305  return this->getCustomPath() + "/" + uid;
306 }
307 
309 {
310  if (mActive && mActive->getUid()==uid)
311  return;
312 
313  if (!this->getCustomProfiles().contains(uid))
314  {
315  this->createCustomProfile(uid);
316  }
317 
318  // uid now is guaranteed to exist in the custom folder
319 
320  mActive.reset(new Profile(this->getPathForCustom(uid), mSettings));
321  this->getGenericSettings()->setValue("profile", mActive->getUid());
322  mActive->activate();
323  emit activeProfileChanged();
324 // CX_LOG_INFO() << "Set profile " << mActive->getName();
325 }
326 
327 void ProfileManager::createCustomProfile(QString uid)
328 {
329  if (this->getInstalledProfiles().contains(uid))
330  {
331  QString path = this->getPathForInstalled(uid);
332  this->copyProfile(path, uid);
333  }
334  else
335  {
336  if (mActive)
337  {
338  this->copyProfile(mActive->getPath(), uid);
339  }
340  else if (!this->getInstalledProfiles().isEmpty())
341  {
342  QString base = this->getInstalledProfiles().front();
343  QString basePath = this->getPathForInstalled(base);
344  this->copyProfile(basePath, uid);
345  }
346  else
347  {
348  this->newProfile(uid);
349  }
350  }
351 }
352 
353 void ProfileManager::profilesChanged()
354 {
355 }
356 
357 
358 
359 } // namespace cx
QStringList getApplicationToolConfigPaths()
Definition: cxProfile.cpp:84
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
ProfilePtr activeProfile()
Definition: cxProfile.cpp:285
void activate()
Definition: cxProfile.cpp:46
Settings * getSettings() const
Definition: cxProfile.cpp:58
static void initialize()
Definition: cxProfile.cpp:176
QString getSettingsPath()
Definition: cxProfile.cpp:109
boost::shared_ptr< class Settings > SettingsPtr
Definition: cxProfile.h:25
static void shutdown()
Definition: cxProfile.cpp:181
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
static QStringList getRootConfigPaths()
QStringList getProfiles()
Definition: cxProfile.cpp:256
void setValueIfNotDefault(const QString &key, const QVariant &value, const QVariant &defaultValue)
Definition: cxSettings.cpp:51
void setToolConfigFilePath(QString path)
Definition: cxProfile.cpp:89
bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:308
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
QString getUid() const
Definition: cxProfile.cpp:74
Profile(QString path, SettingsPtr settings)
Definition: cxProfile.cpp:35
Customized interface for setting values in QSettings.
Definition: cxSettings.h:37
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
boost::shared_ptr< class QSettings > QSettingsPtr
Definition: cxProfile.h:24
XmlOptionFile getXmlSettings()
internal use
Definition: cxProfile.cpp:52
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
static QString getPersistentWritablePath()
Path to location usable for persistent and temporary storage of config. Do not use directly...
QString getToolConfigFilePath()
Definition: cxProfile.cpp:100
QStringList getAllRootConfigPaths()
Definition: cxProfile.cpp:119
QString getPath() const
Definition: cxProfile.cpp:79
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
void report(QString msg)
Definition: cxLogger.cpp:69
#define CX_LOG_WARNING
Definition: cxLogger.h:98
void save()
save entire document.
QString getSettingsPath()
Definition: cxProfile.cpp:219
void setSessionRootFolder(QString path)
Definition: cxProfile.cpp:151
boost::shared_ptr< class Profile > ProfilePtr
Definition: cxProfile.h:29
bool contains(std::string const &s, std::string const &infix)
Definition: catch.hpp:168
QString getSessionRootFolder() const
Definition: cxProfile.cpp:134
Helper class for xml files used to store ssc/cx data.
QString getPatientTemplatePath()
Definition: cxProfile.cpp:114
QString getName() const
Definition: cxProfile.cpp:69
Namespace for all CustusX production code.