CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxDataLocations.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 #include "cxDataLocations.h"
33 
34 #include <iostream>
35 #include <QApplication>
36 #include <QDir>
37 #include "cxConfig.h"
38 #include "cxSettings.h"
39 #include "cxFileHelpers.h"
40 #include "cxTypeConversions.h"
41 #include "cxReporter.h"
42 
43 namespace cx
44 {
45 
46 //---------------------------------------------------------
47 bool DataLocations::mTestMode = false;
48 //---------------------------------------------------------
49 
51 {
52  mTestMode = true;
55 }
56 
58 {
59  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
60  QString dataRootConfigFile = settingsPath + "/data_root_location.txt";
61  if (QFileInfo(dataRootConfigFile).exists())
62  {
63  return readTestDataPathFromFile(dataRootConfigFile);
64  }
65  else
66  {
67  return CX_DATA_ROOT;
68  }
69 }
70 
72 {
73  QString patientDatafolder = settings()->value("globalPatientDataFolder").toString();
74  return patientDatafolder + "/NoPatient";
75 }
76 
78 {
79  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
80  QString dataRootConfigFile = settingsPath + "/large_data_root_location.txt";
81  if (QFileInfo(dataRootConfigFile).exists())
82  {
83  return readTestDataPathFromFile(dataRootConfigFile);
84  }
85  else
86  {
87  return CX_LARGE_DATA_ROOT;
88  }
89 }
90 
91 QString DataLocations::getExistingTestData(QString pathRelativeToTestDataRoot, QString filename)
92 {
93  QString path;
94 
95  path = QString("%1/%2/%3").arg(getTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
96  if (QFileInfo(path).exists())
97  return path;
98 
99  path = QString("%1/%2/%3").arg(getLargeTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
100  if (QFileInfo(path).exists())
101  return path;
102 
103  return "";
104 }
105 
106 QString DataLocations::readTestDataPathFromFile(QString filename)
107 {
108  QFile file(filename);
109  file.open(QFile::ReadOnly);
110  QString cxDataRoot(file.readAll());
111  return cxDataRoot;
112 }
113 
115 {
116  QString homepath = QDir::homePath() + "/cx_settings";
117 
118  if (mTestMode)
119  {
120 // QString bundlepath = getBundlePath();
121 // bundlepath.replace(":","_");
122 // if (bundlepath.startsWith("/"))
123 // bundlepath.remove(0, 1);
124 // homepath += "/test/" + bundlepath;
125  homepath = getTestDataPath() + "/temp";
126  }
127 
128  return homepath;
129 }
130 
131 
133 {
134 #ifdef __APPLE__
135  QString path(qApp->applicationDirPath()+"/../../..");
136  QString bundle = QDir(qApp->applicationDirPath()+"/../..").canonicalPath();
137  if (QFileInfo(bundle).isBundle())
138  return path;
139  else
140  return qApp->applicationDirPath();
141 #else
142  QString path(qApp->applicationDirPath());
143  return path;
144 #endif
145 }
146 
148 {
149  QStringList retval;
150  QString bundlePath = DataLocations::getBundlePath();
151  QString appPath(qApp->applicationDirPath());
152 
153  QString buildLocation = bundlePath + "/plugins";
154  if (QFile(buildLocation).exists())
155  retval << buildLocation;
156 
157  QString installLocation = appPath + "/plugins";
158  if (QFile(installLocation).exists())
159  retval << installLocation;
160 
161  QString fallbackInstallLocation = appPath;
162  if (QFile(fallbackInstallLocation).exists())
163  retval << fallbackInstallLocation;
164 
165  return retval;
166 }
167 
169 {
170  QStringList paths = getRootConfigPaths();
171  if (paths.empty())
172  return "";
173  return paths.front();
174 }
175 
177 {
178  QString path = getBundlePath() + "/" + CX_CONFIG_ROOT_RELATIVE_INSTALLED; // look for installed location
179  if (QDir(path).exists())
180  return QStringList() << QDir(path).canonicalPath();
181 
182  QStringList retval;
183  if (QDir(CX_CONFIG_ROOT).exists()) // look for folder in source code
184  retval << QDir(CX_CONFIG_ROOT).canonicalPath();
185  if (QDir(CX_OPTIONAL_CONFIG_ROOT).exists()) // look for folder in source code
186  retval << QDir(CX_OPTIONAL_CONFIG_ROOT).canonicalPath();
187 
188  return retval;
189 }
190 
192 {
193  QString path = getBundlePath() + "/" + CX_DOC_ROOT_RELATIVE_INSTALLED; // look for installed location
194  if (QDir(path).exists())
195  return QDir(path).canonicalPath();
196 
197  if (QDir(CX_DOC_ROOT).exists()) // look for folder in source code
198  return QDir(CX_DOC_ROOT).canonicalPath();
199 
200  return "";
201 }
202 
203 QStringList DataLocations::appendStringToAllElements(QStringList root, QString suffix)
204 {
205  QStringList retval;
206  for (int i=0; i<root.size(); ++i)
207  retval << root[i] + suffix;
208  return retval;
209 }
210 
211 namespace
212 {
213 QString changeExtension(QString name, QString ext)
214 {
215  QStringList splitName = name.split(".");
216  splitName[splitName.size()-1] = ext;
217  return splitName.join(".");
218 }
219 }
220 
222 {
223  QString path(getPersistentWritablePath()+"/cache/");
224  return path;
225 }
226 
227 QString DataLocations::findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
228 {
229  return findConfigPath("", pathRelativeToConfigRoot, alternativeAbsolutePath);
230 }
231 
232 QString DataLocations::findConfigPath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
233 {
234  QFileInfo filePath(findConfigFilePath(fileName, pathRelativeToConfigRoot, alternativeAbsolutePath));
235  return filePath.absolutePath() + "/";
236 }
237 
238 QString DataLocations::findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
239 {
240  QStringList paths;
241  foreach (QString root, getRootConfigPaths())
242  {
243  QString path = root + "/" + pathRelativeToConfigRoot + "/" + fileName;
244  paths << path;
245  if (QFileInfo(path).exists())
246  return path;
247  }
248 
249  QString path = QString(alternativeAbsolutePath + "/" + fileName);
250  paths << path;
251  if (QFileInfo(path).exists())
252  return path;
253 
254  reportWarning("DataLocations::findConfigFile. Error: Can't find " + fileName + " in any of\n" + paths.join(" \n"));
255  return "";
256 }
257 
258 QString DataLocations::checkExecutableExist(QString path, QString filename)
259 {
260  QStringList retval;
261  path = QDir::cleanPath(path);
262  if (QDir(path).exists(filename))
263  return QDir(DataLocations::getBundlePath()).absoluteFilePath(path + "/" + filename);
264  return "";
265 }
266 
268 {
269  QString result;
270 #ifdef __APPLE__
271  // run from installed folder on mac
272  result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
273  if (!result.isEmpty())
274  return result;
275 #endif
276  // run from installed or build bin folder
277  result = DataLocations::checkExecutableExist(DataLocations::getBundlePath(), filename);
278  if (!result.isEmpty())
279  return result;
280 
281  return result;
282 }
283 
284 } // namespace cx
static QString getExistingTestData(QString pathRelativeToTestDataRoot, QString filename="")
Return full path to test data, both normal and large repositories are searched.
bool removeNonemptyDirRecursively(const QString &dirName)
static QString findConfigPath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
static QString getLargeTestDataPath()
return path to test data folder containing large data sets
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:99
static QStringList getRootConfigPaths()
static QString findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
static QString getCachePath()
return path to a folder that is used during execution, will be cleared at start and stop...
static QString findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
static QString getTestDataPath()
return path to test data folder
static QString getPersistentWritablePath()
Path to location usable for persistent and temporary storage of config. Do not use directly...
static QStringList getDefaultPluginsPath()
return the folder where plugins should be located, by default.
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
QString changeExtension(QString name, QString ext)
static QString findExecutableInStandardLocations(QString filename)
look for an exe in the same folder as the executable or bundle.
static QStringList appendStringToAllElements(QStringList root, QString suffix)
static void setTestMode()
set a testing mode that changes location of settings files to a temp folder.
static QString getNoPatientFolder()
static QString getDocPath()
return path to folder containing documentation files
static QString getBundlePath()
return the folder where the bundle or executable are located.