CustusX  15.3.4-beta
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 
86 {
87  return this->getUid();
88 }
89 
90 QString Profile::getUid()
91 {
92  return QFileInfo(mPath).fileName();
93 }
94 
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 //---------------------------------------------------------
139 //---------------------------------------------------------
140 //---------------------------------------------------------
141 
142 cxResource_EXPORT ProfilePtr profile()
143 {
145 }
146 
147 ProfileManager *ProfileManager::mInstance = NULL;
148 
150 {
151  if (mInstance == NULL)
152  {
153  mInstance = new ProfileManager();
154  }
155  return mInstance;
156 }
157 
159 {
161 }
162 
164 {
165  delete mInstance;
166  mInstance = NULL;
167 }
168 
169 ProfileManager::ProfileManager()
170 {
171  QString defaultProfile = this->getDefaultProfileUid();
172  defaultProfile = this->getGenericSettings()->value("profile", defaultProfile).toString();
173 
174  mSettings.reset(new Settings());
175 
176  this->setActiveProfile(defaultProfile);
177 }
178 
179 ProfileManager::~ProfileManager()
180 {
181 
182 }
183 
184 QString ProfileManager::getDefaultProfileUid()
185 {
186  QStringList installed = this->getInstalledProfiles();
187  if (installed.contains("Laboratory"))
188  {
189  return "Laboratory";
190  }
191  else if (!installed.isEmpty())
192  {
193  return installed.front();
194  }
195  else
196  {
197  return "default";
198  }
199 }
200 
202 {
203  return DataLocations::getPersistentWritablePath()+"/settings";
204 }
205 
206 QSettingsPtr ProfileManager::getGenericSettings()
207 {
208  QString filename = this->getCustomPath() + "/generic_settings.ini";
209  return QSettingsPtr(new QSettings(filename, QSettings::IniFormat));
210 }
211 
212 QStringList ProfileManager::getInstalledProfiles()
213 {
214  QStringList configPaths = DataLocations::getRootConfigPaths();
215  QStringList profiles;
216  for (int i=0; i< configPaths.size(); ++i)
217  profiles << getProfilesInFolder(configPaths[i]+"/profiles");
218  return profiles;
219 }
220 QStringList ProfileManager::getCustomProfiles()
221 {
222  QStringList profiles;
223  profiles << getProfilesInFolder(this->getCustomPath());
224  return profiles;
225 }
226 
227 QString ProfileManager::getCustomPath()
228 {
229  return DataLocations::getPersistentWritablePath() + "/profiles";
230 }
231 
232 QStringList ProfileManager::getProfilesInFolder(QString folder)
233 {
234  QDir dir(folder);
235  return dir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
236 }
237 
239 {
240  QStringList profiles = this->getInstalledProfiles();
241  profiles << getProfilesInFolder(this->getCustomPath());
242  profiles.removeDuplicates();
243  return profiles;
244 }
245 
246 void ProfileManager::newProfile(QString uid)
247 {
248  QString path = this->getPathForCustom(uid);
249 
250  QDir dir(path);
251  dir.mkpath(".");
252  dir.mkpath("tool");
253 
254  this->profilesChanged();
255 }
256 
257 void ProfileManager::copyProfile(QString base, QString uid)
258 {
259  QString newPath = this->getPathForCustom(uid);
260 
261  if (!copyRecursively(base, newPath))
262  CX_LOG_WARNING() << "Failed to copy profile " << base << " to " << newPath;
263 
264  this->profilesChanged();
265 }
266 
268 {
269  return mActive;
270 }
271 
272 QString ProfileManager::getPathForInstalled(QString uid)
273 {
274  QStringList configPaths = DataLocations::getRootConfigPaths();
275  QStringList profiles;
276  for (int i=0; i< configPaths.size(); ++i)
277  {
278  QFileInfo info(configPaths[i]+"/profiles/"+uid);
279  if (info.exists())
280  return info.canonicalFilePath();
281  }
282  return "";
283 }
284 
285 QString ProfileManager::getPathForCustom(QString uid)
286 {
287  return this->getCustomPath() + "/" + uid;
288 }
289 
291 {
292  if (mActive && mActive->getUid()==uid)
293  return;
294 
295  if (!this->getCustomProfiles().contains(uid))
296  {
297  this->createCustomProfile(uid);
298  }
299 
300  // uid now is guaranteed to exist in the custom folder
301 
302  mActive.reset(new Profile(this->getPathForCustom(uid), mSettings));
303  this->getGenericSettings()->setValue("profile", mActive->getUid());
304 // if (mSelector)
305 // mSelector->setValue(mActive->getUid());
306  mActive->activate();
307  emit activeProfileChanged();
308 // CX_LOG_INFO() << "Set profile " << mActive->getName();
309 }
310 
311 void ProfileManager::createCustomProfile(QString uid)
312 {
313  if (this->getInstalledProfiles().contains(uid))
314  {
315  QString path = this->getPathForInstalled(uid);
316  this->copyProfile(path, uid);
317  }
318  else
319  {
320  if (mActive)
321  {
322  this->copyProfile(mActive->getPath(), uid);
323  }
324  else if (!this->getInstalledProfiles().isEmpty())
325  {
326  QString base = this->getInstalledProfiles().front();
327  QString basePath = this->getPathForInstalled(base);
328  this->copyProfile(basePath, uid);
329  }
330  else
331  {
332  this->newProfile(uid);
333  }
334  }
335 }
336 
337 void ProfileManager::profilesChanged()
338 {
339 // if (mSelector)
340 // mSelector->setValueRange(this->getProfiles());
341 }
342 
343 
344 
345 } // namespace cx
QStringList getApplicationToolConfigPaths()
Definition: cxProfile.cpp:100
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath)
void activeProfileChanged()
ProfilePtr activeProfile()
Definition: cxProfile.cpp:267
void activate()
Definition: cxProfile.cpp:62
QString getName()
Definition: cxProfile.cpp:85
static void initialize()
Definition: cxProfile.cpp:158
QString getUid()
Definition: cxProfile.cpp:90
QString getSettingsPath()
Definition: cxProfile.cpp:125
boost::shared_ptr< class Settings > SettingsPtr
Definition: cxProfile.h:46
static void shutdown()
Definition: cxProfile.cpp:163
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:99
static QStringList getRootConfigPaths()
QStringList getProfiles()
Definition: cxProfile.cpp:238
void setToolConfigFilePath(QString path)
Definition: cxProfile.cpp:105
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:290
QString getPath()
Definition: cxProfile.cpp:95
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:91
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:149
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
#define CX_LOG_WARNING
Definition: cxLogger.h:112
QString getSettingsPath()
Definition: cxProfile.cpp:201
boost::shared_ptr< class Profile > ProfilePtr
Definition: cxProfile.h:50
Settings * getSettings()
Definition: cxProfile.cpp:74
Helper class for xml files used to store ssc/cx data.