Fraxinus  16.5.0-fx-rc9
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->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  QStringList retval = DataLocations::getRootConfigPaths();
138  retval << this->getPath();
139  return retval;
140 }
141 
142 
143 QString Profile::getDefaultSessionRootFolder() const
144 {
145  QStringList path;
146  path << QDir::homePath() << "Patients" << this->getName();
147  return path.join("/");
148 }
149 
151 {
152  QString folder = this->getSettings()->value("globalPatientDataFolder",
153  this->getDefaultSessionRootFolder()).toString();
154 
155  // Create folders
156  if (!QDir().exists(folder))
157  {
158  if(QDir().mkpath(folder))
159  report("Made a new patient folder: " + folder);
160  else
161  reportWarning("Cannot make new patient folder: " + folder);
162  }
163 
164  return folder;
165 }
166 
168 {
169  this->getSettings()->setValueIfNotDefault("globalPatientDataFolder", path, this->getDefaultSessionRootFolder());
170 }
171 
172 //---------------------------------------------------------
173 //---------------------------------------------------------
174 //---------------------------------------------------------
175 
176 cxResource_EXPORT ProfilePtr profile()
177 {
179 }
180 
181 ProfileManager *ProfileManager::mInstance = NULL;
182 
184 {
185  if (mInstance == NULL)
186  {
187  mInstance = new ProfileManager(defaultProfile);
188  }
189  return mInstance;
190 }
191 
193 {
195 }
196 
198 {
199  delete mInstance;
200  mInstance = NULL;
201 }
202 
203 ProfileManager::ProfileManager(QString defaultProfile)
204 {
205  QString profileUid = this->getDefaultProfileUid(defaultProfile);
206  profileUid = this->getGenericSettings()->value("profile", profileUid).toString();
207 
208  mSettings.reset(new Settings());
209 
210  this->setActiveProfile(profileUid);
211 }
212 
213 ProfileManager::~ProfileManager()
214 {
215 
216 }
217 
218 QString ProfileManager::getDefaultProfileUid(QString defaultProfile)
219 {
220  QStringList installed = this->getInstalledProfiles();
221  if (installed.contains(defaultProfile))
222  {
223  return defaultProfile;
224  }
225  else if (!installed.isEmpty())
226  {
227  return installed.front();
228  }
229  else
230  {
231  return "default";
232  }
233 }
234 
236 {
237  return DataLocations::getPersistentWritablePath()+"/settings";
238 }
239 
240 QSettingsPtr ProfileManager::getGenericSettings()
241 {
242  QString filename = this->getCustomPath() + "/generic_settings.ini";
243  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
244 }
245 
246 QStringList ProfileManager::getInstalledProfiles()
247 {
248  QStringList configPaths = DataLocations::getRootConfigPaths();
249  QStringList profiles;
250  for (int i=0; i< configPaths.size(); ++i)
251  profiles << getProfilesInFolder(configPaths[i]+"/profiles");
252  return profiles;
253 }
254 QStringList ProfileManager::getCustomProfiles()
255 {
256  QStringList profiles;
257  profiles << getProfilesInFolder(this->getCustomPath());
258  return profiles;
259 }
260 
261 QString ProfileManager::getCustomPath()
262 {
263  return DataLocations::getPersistentWritablePath() + "/profiles";
264 }
265 
266 QStringList ProfileManager::getProfilesInFolder(QString folder)
267 {
268  QDir dir(folder);
269  return dir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
270 }
271 
273 {
274  QStringList profiles = this->getInstalledProfiles();
275  profiles << getProfilesInFolder(this->getCustomPath());
276  profiles.removeDuplicates();
277  return profiles;
278 }
279 
280 void ProfileManager::newProfile(QString uid)
281 {
282  QString path = this->getPathForCustom(uid);
283 
284  QDir dir(path);
285  dir.mkpath(".");
286  dir.mkpath("tool");
287 
288  this->profilesChanged();
289 }
290 
291 void ProfileManager::copyProfile(QString base, QString uid)
292 {
293  QString newPath = this->getPathForCustom(uid);
294 
295  if (!copyRecursively(base, newPath))
296  CX_LOG_WARNING() << "Failed to copy profile " << base << " to " << newPath;
297 
298  this->profilesChanged();
299 }
300 
302 {
303  return mActive;
304 }
305 
306 QString ProfileManager::getPathForInstalled(QString uid)
307 {
308  QStringList configPaths = DataLocations::getRootConfigPaths();
309  QStringList profiles;
310  for (int i=0; i< configPaths.size(); ++i)
311  {
312  QFileInfo info(configPaths[i]+"/profiles/"+uid);
313  if (info.exists())
314  return info.canonicalFilePath();
315  }
316  return "";
317 }
318 
319 QString ProfileManager::getPathForCustom(QString uid)
320 {
321  return this->getCustomPath() + "/" + uid;
322 }
323 
325 {
326  if (mActive && mActive->getUid()==uid)
327  return;
328 
329  if (!this->getCustomProfiles().contains(uid))
330  {
331  this->createCustomProfile(uid);
332  }
333 
334  // uid now is guaranteed to exist in the custom folder
335 
336  mActive.reset(new Profile(this->getPathForCustom(uid), mSettings));
337  this->getGenericSettings()->setValue("profile", mActive->getUid());
338  mActive->activate();
339  emit activeProfileChanged();
340 // CX_LOG_INFO() << "Set profile " << mActive->getName();
341 }
342 
343 void ProfileManager::createCustomProfile(QString uid)
344 {
345  if (this->getInstalledProfiles().contains(uid))
346  {
347  QString path = this->getPathForInstalled(uid);
348  this->copyProfile(path, uid);
349  }
350  else
351  {
352  if (mActive)
353  {
354  this->copyProfile(mActive->getPath(), uid);
355  }
356  else if (!this->getInstalledProfiles().isEmpty())
357  {
358  QString base = this->getInstalledProfiles().front();
359  QString basePath = this->getPathForInstalled(base);
360  this->copyProfile(basePath, uid);
361  }
362  else
363  {
364  this->newProfile(uid);
365  }
366  }
367 }
368 
369 void ProfileManager::profilesChanged()
370 {
371 }
372 
373 
374 
375 } // namespace cx
QStringList getApplicationToolConfigPaths()
Definition: cxProfile.cpp:105
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath)
void activeProfileChanged()
ProfilePtr activeProfile()
Definition: cxProfile.cpp:301
void activate()
Definition: cxProfile.cpp:67
Settings * getSettings() const
Definition: cxProfile.cpp:79
static void initialize()
Definition: cxProfile.cpp:192
QString getSettingsPath()
Definition: cxProfile.cpp:130
boost::shared_ptr< class Settings > SettingsPtr
Definition: cxProfile.h:46
static void shutdown()
Definition: cxProfile.cpp:197
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:183
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:87
static QStringList getRootConfigPaths()
QStringList getProfiles()
Definition: cxProfile.cpp:272
void setValueIfNotDefault(const QString &key, const QVariant &value, const QVariant &defaultValue)
Definition: cxSettings.cpp:72
void setToolConfigFilePath(QString path)
Definition: cxProfile.cpp:110
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:324
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:135
QString getPath() const
Definition: cxProfile.cpp:100
void report(QString msg)
Definition: cxLogger.cpp:90
#define CX_LOG_WARNING
Definition: cxLogger.h:113
void save()
save entire document.
QString getSettingsPath()
Definition: cxProfile.cpp:235
void setSessionRootFolder(QString path)
Definition: cxProfile.cpp:167
boost::shared_ptr< class Profile > ProfilePtr
Definition: cxProfile.h:50
QString getSessionRootFolder() const
Definition: cxProfile.cpp:150
Helper class for xml files used to store ssc/cx data.
QString getName() const
Definition: cxProfile.cpp:90