NorMIT-nav  2023.01.05-dev+develop.0da12
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->getGenericSettingsFromInstaller()->value("profile", profileUid).toString();
191  profileUid = this->getGenericSettings()->value("profile", profileUid).toString();
192 
193  mSettings.reset(new Settings());
194 
195  this->setActiveProfile(profileUid);
196 }
197 
198 ProfileManager::~ProfileManager()
199 {
200 
201 }
202 
203 QString ProfileManager::getDefaultProfileUid(QString defaultProfile)
204 {
205  QStringList installed = this->getInstalledProfiles();
206  if (installed.contains(defaultProfile))
207  {
208  return defaultProfile;
209  }
210  else if (!installed.isEmpty())
211  {
212  return installed.front();
213  }
214  else
215  {
216  return "default";
217  }
218 }
219 
221 {
222  return DataLocations::getPersistentWritablePath()+"/settings";
223 }
224 
225 QSettingsPtr ProfileManager::getGenericSettings()
226 {
227  QString filename = this->getCustomPath() + "/generic_settings.ini";
228  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
229 }
230 
231 QSettingsPtr ProfileManager::getGenericSettingsFromInstaller()
232 {
233  QString configPath = DataLocations::getRootConfigPath();
234  QString filename = configPath + "/profiles/generic_settings.ini";
235  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
236 }
237 
238 QStringList ProfileManager::getInstalledProfiles()
239 {
240  QStringList configPaths = DataLocations::getRootConfigPaths();
241  QStringList profiles;
242  for (int i=0; i< configPaths.size(); ++i)
243  profiles << getProfilesInFolder(configPaths[i]+"/profiles");
244  return profiles;
245 }
246 QStringList ProfileManager::getCustomProfiles()
247 {
248  QStringList profiles;
249  profiles << getProfilesInFolder(this->getCustomPath());
250  return profiles;
251 }
252 
253 QString ProfileManager::getCustomPath()
254 {
255  return DataLocations::getPersistentWritablePath() + "/profiles";
256 }
257 
258 QStringList ProfileManager::getProfilesInFolder(QString folder)
259 {
260  QDir dir(folder);
261  return dir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
262 }
263 
265 {
266  QStringList profiles = this->getInstalledProfiles();
267  profiles << getProfilesInFolder(this->getCustomPath());
268  profiles.removeDuplicates();
269  return profiles;
270 }
271 
272 void ProfileManager::newProfile(QString uid)
273 {
274  QString path = this->getPathForCustom(uid);
275 
276  QDir dir(path);
277  dir.mkpath(".");
278  dir.mkpath("tool");
279 
280  this->profilesChanged();
281 }
282 
283 void ProfileManager::copyProfile(QString base, QString uid)
284 {
285  QString newPath = this->getPathForCustom(uid);
286 
287  if (!copyRecursively(base, newPath, true))
288  CX_LOG_WARNING() << "Failed to copy profile " << base << " to " << newPath;
289 
290  this->profilesChanged();
291 }
292 
294 {
295  return mActive;
296 }
297 
298 QString ProfileManager::getPathForInstalled(QString uid)
299 {
300  QStringList configPaths = DataLocations::getRootConfigPaths();
301  QStringList profiles;
302  for (int i=0; i< configPaths.size(); ++i)
303  {
304  QFileInfo info(configPaths[i]+"/profiles/"+uid);
305  if (info.exists())
306  return info.canonicalFilePath();
307  }
308  return "";
309 }
310 
311 QString ProfileManager::getPathForCustom(QString uid)
312 {
313  return this->getCustomPath() + "/" + uid;
314 }
315 
317 {
318  if (mActive && mActive->getUid()==uid)
319  return;
320 
321  if (!this->getCustomProfiles().contains(uid))
322  {
323  this->createCustomProfile(uid);
324  }
325 
326  // uid now is guaranteed to exist in the custom folder
327 
328  mActive.reset(new Profile(this->getPathForCustom(uid), mSettings));
329  this->getGenericSettings()->setValue("profile", mActive->getUid());
330  mActive->activate();
331  emit activeProfileChanged();
332 // CX_LOG_INFO() << "Set profile " << mActive->getName();
333 }
334 
335 void ProfileManager::createCustomProfile(QString uid)
336 {
337  if (this->getInstalledProfiles().contains(uid))
338  {
339  QString path = this->getPathForInstalled(uid);
340  this->copyProfile(path, uid);
341  }
342  else
343  {
344  if (mActive)
345  {
346  this->copyProfile(mActive->getPath(), uid);
347  }
348  else if (!this->getInstalledProfiles().isEmpty())
349  {
350  QString base = this->getInstalledProfiles().front();
351  QString basePath = this->getPathForInstalled(base);
352  this->copyProfile(basePath, uid);
353  }
354  else
355  {
356  this->newProfile(uid);
357  }
358  }
359 }
360 
361 void ProfileManager::profilesChanged()
362 {
363 }
364 
365 
366 
367 } // namespace cx
cx::ProfileManager::getProfiles
QStringList getProfiles()
Definition: cxProfile.cpp:264
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:220
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:316
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:293
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