CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 will trigger lots of change signals. Do after Profile object is in place.
65  mSettings->resetFile(this->getSettingsFile());
66 }
67 
69 {
70  QString filename = this->getSettingsPath() + "/settings.xml";
71  return XmlOptionFile(filename);
72 }
73 
75 {
76  return mSettings.get();
77 }
78 
79 QString Profile::getSettingsFile()
80 {
81  QString filename = this->getSettingsPath() + "/settings.ini";
82  return filename;
83 }
84 
85 QString Profile::getName() const
86 {
87  return this->getUid();
88 }
89 
90 QString Profile::getUid() const
91 {
92  return QFileInfo(mPath).fileName();
93 }
94 
95 QString Profile::getPath() const
96 {
97  return mPath;
98 }
99 
101 {
102  return QStringList() << this->getPath() + "/tool/";
103 }
104 
106 {
107  QString expectedPath = this->getApplicationToolConfigPaths().front();
108 
109  QFileInfo info(path);
110  if (info.absolutePath() != expectedPath)
111  CX_LOG_WARNING() << "Set ref to file " << path << ", should be in folder " << expectedPath;
112 
113  settings()->setValue("toolConfigFile", info.fileName());
114 }
115 
117 {
118  QString path = this->getApplicationToolConfigPaths().front();
119  QString filename = this->getSettings()->value("toolConfigFile").toString();
120  if (filename.isEmpty())
121  return "";
122  return path + "/" + filename;
123 }
124 
126 {
127  return this->getPath() + "/settings";
128 }
129 
131 {
132  QStringList retval = DataLocations::getRootConfigPaths();
133  retval << this->getPath();
134  return retval;
135 }
136 
137 
138 QString Profile::getDefaultSessionRootFolder() const
139 {
140  QStringList path;
141  path << QDir::homePath() << "Patients" << this->getName();
142  return path.join("/");
143 }
144 
146 {
147  QString folder = this->getSettings()->value("globalPatientDataFolder",
148  this->getDefaultSessionRootFolder()).toString();
149 
150  // Create folders
151  if (!QDir().exists(folder))
152  {
153  QDir().mkdir(folder);
154  report("Made a new patient folder: " + folder);
155  }
156 
157  return folder;
158 }
159 
161 {
162  this->getSettings()->setValueIfNotDefault("globalPatientDataFolder", path, this->getDefaultSessionRootFolder());
163 }
164 
165 //---------------------------------------------------------
166 //---------------------------------------------------------
167 //---------------------------------------------------------
168 
169 cxResource_EXPORT ProfilePtr profile()
170 {
172 }
173 
174 ProfileManager *ProfileManager::mInstance = NULL;
175 
177 {
178  if (mInstance == NULL)
179  {
180  mInstance = new ProfileManager();
181  }
182  return mInstance;
183 }
184 
186 {
188 }
189 
191 {
192  delete mInstance;
193  mInstance = NULL;
194 }
195 
196 ProfileManager::ProfileManager()
197 {
198  QString defaultProfile = this->getDefaultProfileUid();
199  defaultProfile = this->getGenericSettings()->value("profile", defaultProfile).toString();
200 
201  mSettings.reset(new Settings());
202 
203  this->setActiveProfile(defaultProfile);
204 }
205 
206 ProfileManager::~ProfileManager()
207 {
208 
209 }
210 
211 QString ProfileManager::getDefaultProfileUid()
212 {
213  QStringList installed = this->getInstalledProfiles();
214  if (installed.contains("Laboratory"))
215  {
216  return "Laboratory";
217  }
218  else if (!installed.isEmpty())
219  {
220  return installed.front();
221  }
222  else
223  {
224  return "default";
225  }
226 }
227 
229 {
230  return DataLocations::getPersistentWritablePath()+"/settings";
231 }
232 
233 QSettingsPtr ProfileManager::getGenericSettings()
234 {
235  QString filename = this->getCustomPath() + "/generic_settings.ini";
236  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
237 }
238 
239 QStringList ProfileManager::getInstalledProfiles()
240 {
241  QStringList configPaths = DataLocations::getRootConfigPaths();
242  QStringList profiles;
243  for (int i=0; i< configPaths.size(); ++i)
244  profiles << getProfilesInFolder(configPaths[i]+"/profiles");
245  return profiles;
246 }
247 QStringList ProfileManager::getCustomProfiles()
248 {
249  QStringList profiles;
250  profiles << getProfilesInFolder(this->getCustomPath());
251  return profiles;
252 }
253 
254 QString ProfileManager::getCustomPath()
255 {
256  return DataLocations::getPersistentWritablePath() + "/profiles";
257 }
258 
259 QStringList ProfileManager::getProfilesInFolder(QString folder)
260 {
261  QDir dir(folder);
262  return dir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
263 }
264 
266 {
267  QStringList profiles = this->getInstalledProfiles();
268  profiles << getProfilesInFolder(this->getCustomPath());
269  profiles.removeDuplicates();
270  return profiles;
271 }
272 
273 void ProfileManager::newProfile(QString uid)
274 {
275  QString path = this->getPathForCustom(uid);
276 
277  QDir dir(path);
278  dir.mkpath(".");
279  dir.mkpath("tool");
280 
281  this->profilesChanged();
282 }
283 
284 void ProfileManager::copyProfile(QString base, QString uid)
285 {
286  QString newPath = this->getPathForCustom(uid);
287 
288  if (!copyRecursively(base, newPath))
289  CX_LOG_WARNING() << "Failed to copy profile " << base << " to " << newPath;
290 
291  this->profilesChanged();
292 }
293 
295 {
296  return mActive;
297 }
298 
299 QString ProfileManager::getPathForInstalled(QString uid)
300 {
301  QStringList configPaths = DataLocations::getRootConfigPaths();
302  QStringList profiles;
303  for (int i=0; i< configPaths.size(); ++i)
304  {
305  QFileInfo info(configPaths[i]+"/profiles/"+uid);
306  if (info.exists())
307  return info.canonicalFilePath();
308  }
309  return "";
310 }
311 
312 QString ProfileManager::getPathForCustom(QString uid)
313 {
314  return this->getCustomPath() + "/" + uid;
315 }
316 
318 {
319  if (mActive && mActive->getUid()==uid)
320  return;
321 
322  if (!this->getCustomProfiles().contains(uid))
323  {
324  this->createCustomProfile(uid);
325  }
326 
327  // uid now is guaranteed to exist in the custom folder
328 
329  mActive.reset(new Profile(this->getPathForCustom(uid), mSettings));
330  this->getGenericSettings()->setValue("profile", mActive->getUid());
331  mActive->activate();
332  emit activeProfileChanged();
333 // CX_LOG_INFO() << "Set profile " << mActive->getName();
334 }
335 
336 void ProfileManager::createCustomProfile(QString uid)
337 {
338  if (this->getInstalledProfiles().contains(uid))
339  {
340  QString path = this->getPathForInstalled(uid);
341  this->copyProfile(path, uid);
342  }
343  else
344  {
345  if (mActive)
346  {
347  this->copyProfile(mActive->getPath(), uid);
348  }
349  else if (!this->getInstalledProfiles().isEmpty())
350  {
351  QString base = this->getInstalledProfiles().front();
352  QString basePath = this->getPathForInstalled(base);
353  this->copyProfile(basePath, uid);
354  }
355  else
356  {
357  this->newProfile(uid);
358  }
359  }
360 }
361 
362 void ProfileManager::profilesChanged()
363 {
364 }
365 
366 
367 
368 } // namespace cx
QStringList getApplicationToolConfigPaths()
Definition: cxProfile.cpp:100
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:169
bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath)
void activeProfileChanged()
ProfilePtr activeProfile()
Definition: cxProfile.cpp:294
void activate()
Definition: cxProfile.cpp:62
Settings * getSettings() const
Definition: cxProfile.cpp:74
static void initialize()
Definition: cxProfile.cpp:185
QString getSettingsPath()
Definition: cxProfile.cpp:125
boost::shared_ptr< class Settings > SettingsPtr
Definition: cxProfile.h:46
static void shutdown()
Definition: cxProfile.cpp:190
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:87
static QStringList getRootConfigPaths()
QStringList getProfiles()
Definition: cxProfile.cpp:265
void setValueIfNotDefault(const QString &key, const QVariant &value, const QVariant &defaultValue)
Definition: cxSettings.cpp:72
void setToolConfigFilePath(QString path)
Definition: cxProfile.cpp:105
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:317
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:79
QString getUid() const
Definition: cxProfile.cpp:90
Profile(QString path, SettingsPtr settings)
Definition: cxProfile.cpp:56
Customized interface for setting values in QSettings.
Definition: cxSettings.h:58
static ProfileManager * getInstance()
returns the only instance of this class
Definition: cxProfile.cpp:176
boost::shared_ptr< class QSettings > QSettingsPtr
Definition: cxProfile.h:45
XmlOptionFile getXmlSettings()
internal use
Definition: cxProfile.cpp:68
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:116
QStringList getAllRootConfigPaths()
Definition: cxProfile.cpp:130
QString getPath() const
Definition: cxProfile.cpp:95
void report(QString msg)
Definition: cxLogger.cpp:90
#define CX_LOG_WARNING
Definition: cxLogger.h:112
QString getSettingsPath()
Definition: cxProfile.cpp:228
void setSessionRootFolder(QString path)
Definition: cxProfile.cpp:160
boost::shared_ptr< class Profile > ProfilePtr
Definition: cxProfile.h:50
QString getSessionRootFolder() const
Definition: cxProfile.cpp:145
Helper class for xml files used to store ssc/cx data.
QString getName() const
Definition: cxProfile.cpp:85