NorMIT-nav  22.09
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
cx::ProfileManager::getProfiles
QStringList getProfiles()
Definition: cxProfile.cpp:256
cx::XmlOptionFile
Helper class for xml files used to store ssc/cx data.
Definition: cxXmlOptionItem.h:78
cxLogger.h
cx::Profile::getPatientTemplatePath
QString getPatientTemplatePath()
Definition: cxProfile.cpp:114
cx::Profile::getPath
QString getPath() const
Definition: cxProfile.cpp:79
cx::Settings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::XmlOptionFile::save
void save()
save entire document.
Definition: cxXmlOptionItem.cpp:396
cx::Profile::activate
void activate()
Definition: cxProfile.cpp:46
cx::report
void report(QString msg)
Definition: cxLogger.cpp:69
cxXmlOptionItem.h
cx::Settings::setValueIfNotDefault
void setValueIfNotDefault(const QString &key, const QVariant &value, const QVariant &defaultValue)
Definition: cxSettings.cpp:51
cx::DataLocations::getRootConfigPath
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
Definition: cxDataLocations.cpp:169
cx::ProfileManager
Definition: cxProfile.h:115
cx::Profile::setToolConfigFilePath
void setToolConfigFilePath(QString path)
Definition: cxProfile.cpp:89
cx::DataLocations::getRootConfigPaths
static QStringList getRootConfigPaths()
Definition: cxDataLocations.cpp:179
cx::Settings
Customized interface for setting values in QSettings.
Definition: cxSettings.h:37
cx::Profile::getSessionRootFolder
QString getSessionRootFolder() const
Definition: cxProfile.cpp:134
cx::DataLocations::getPersistentWritablePath
static QString getPersistentWritablePath()
Path to location usable for persistent and temporary storage of config. Do not use directly,...
Definition: cxDataLocations.cpp:96
cx::Profile::getUid
QString getUid() const
Definition: cxProfile.cpp:74
cxProfile.h
cx::Profile::getName
QString getName() const
Definition: cxProfile.cpp:69
cx::copyRecursively
bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
Definition: cxFileHelpers.cpp:23
cx::Profile::getSettingsPath
QString getSettingsPath()
Definition: cxProfile.cpp:109
cx::Profile::getAllRootConfigPaths
QStringList getAllRootConfigPaths()
Definition: cxProfile.cpp:119
cxTypeConversions.h
cx::Profile::~Profile
~Profile()
Definition: cxProfile.cpp:41
cx::ProfileManager::getSettingsPath
QString getSettingsPath()
Definition: cxProfile.cpp:219
cxSettings.h
cx::Profile::getXmlSettings
XmlOptionFile getXmlSettings()
internal use
Definition: cxProfile.cpp:52
cx::profile
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
cx::ProfileManager::setActiveProfile
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:308
cx::Profile
Definition: cxProfile.h:41
cx::Profile::getToolConfigFilePath
QString getToolConfigFilePath()
Definition: cxProfile.cpp:100
cx::Profile::setSessionRootFolder
void setSessionRootFolder(QString path)
Definition: cxProfile.cpp:151
cxFileHelpers.h
cxDataLocations.h
cx::ProfileManager::activeProfileChanged
void activeProfileChanged()
cx::ProfilePtr
boost::shared_ptr< class Profile > ProfilePtr
Definition: cxProfile.h:29
cx::Settings::setValue
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
cx::ProfileManager::getInstance
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
cx::Profile::Profile
Profile(QString path, SettingsPtr settings)
Definition: cxProfile.cpp:35
cxStringProperty.h
CX_LOG_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::ProfileManager::activeProfile
ProfilePtr activeProfile()
Definition: cxProfile.cpp:285
cx::SettingsPtr
boost::shared_ptr< class Settings > SettingsPtr
Definition: cxProfile.h:25
cx::ProfileManager::initialize
static void initialize()
Definition: cxProfile.cpp:176
cx::Profile::getApplicationToolConfigPaths
QStringList getApplicationToolConfigPaths()
Definition: cxProfile.cpp:84
cx::QSettingsPtr
boost::shared_ptr< class QSettings > QSettingsPtr
Definition: cxProfile.h:24
cx::ProfileManager::shutdown
static void shutdown()
Definition: cxProfile.cpp:181
cx::Profile::getSettings
Settings * getSettings() const
Definition: cxProfile.cpp:58
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cx::settings
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21