CustusX  15.8
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 #include "cxProfile.h"
43 
44 namespace cx
45 {
46 
47 //---------------------------------------------------------
48 bool DataLocations::mTestMode = false;
49 bool DataLocations::mRunFromBuildFolder = false;
50 bool DataLocations::mBuildFolderChecked = false;
51 //---------------------------------------------------------
52 
54 {
55  mTestMode = true;
57 }
58 
60 {
61  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
62  QString dataRootConfigFile = settingsPath + "/data_root_location.txt";
63  if (QFileInfo(dataRootConfigFile).exists())
64  {
65  return readTestDataPathFromFile(dataRootConfigFile);
66  }
67  else
68  {
69  return CX_DATA_ROOT;
70  }
71 }
72 
74 {
75  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
76  QString dataRootConfigFile = settingsPath + "/large_data_root_location.txt";
77  if (QFileInfo(dataRootConfigFile).exists())
78  {
79  return readTestDataPathFromFile(dataRootConfigFile);
80  }
81  else
82  {
83  return CX_LARGE_DATA_ROOT;
84  }
85 }
86 
87 QString DataLocations::getExistingTestData(QString pathRelativeToTestDataRoot, QString filename)
88 {
89  QString path;
90 
91  path = QString("%1/%2/%3").arg(getTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
92  if (QFileInfo(path).exists())
93  return path;
94 
95  path = QString("%1/%2/%3").arg(getLargeTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
96  if (QFileInfo(path).exists())
97  return path;
98 
99  return "";
100 }
101 
102 QString DataLocations::readTestDataPathFromFile(QString filename)
103 {
104  QFile file(filename);
105  file.open(QFile::ReadOnly);
106  QString cxDataRoot(file.readAll());
107  return cxDataRoot;
108 }
109 
111 {
112  QString homepath = QDir::homePath() + "/cx_settings";
113 
114  if (mTestMode)
115  homepath = getTestDataPath() + "/temp";
116 
117  return homepath;
118 }
119 
120 
122 {
123 #ifdef __APPLE__
124  QString path(qApp->applicationDirPath()+"/../../..");
125  QString bundle = QDir(qApp->applicationDirPath()+"/../..").canonicalPath();
126  if (QFileInfo(bundle).isBundle())
127  return path;
128  else
129  return qApp->applicationDirPath();
130 #else
131  QString path(qApp->applicationDirPath());
132  return path;
133 #endif
134 }
135 
137 {
138  QStringList retval;
139 
140  if(!isRunFromBuildFolder())
141  {
142  QString appPath(qApp->applicationDirPath());
143 
144  QString installLocation = appPath;
145 #ifndef CX_WINDOWS
146  installLocation = appPath + "/plugins";
147 #endif
148  if (QFile(installLocation).exists())
149  retval << installLocation;
150 
151  QString fallbackInstallLocation = appPath;
152  if (QFile(fallbackInstallLocation).exists())
153  retval << fallbackInstallLocation; }
154  else
155  {
156  QString bundlePath = DataLocations::getBundlePath();
157 
158  QString buildLocation = bundlePath;
159 #ifndef CX_WINDOWS
160  buildLocation = bundlePath + "/plugins";
161 #endif
162  if (QFile(buildLocation).exists())
163  retval << buildLocation;
164  }
165 
166  return retval;
167 }
168 
170 {
171  QStringList paths = getRootConfigPaths();
172  if (paths.empty())
173  return "";
174  return paths.front();
175 }
176 
178 {
179  if(!isRunFromBuildFolder())
180  {
181  QString path = getBundlePath() + "/" + CX_CONFIG_ROOT_RELATIVE_INSTALLED; // look for installed location
182  if (QDir(path).exists())
183  return QStringList() << QDir(path).canonicalPath();
184  else
185  {
186  std::cout << "DataLocations::getRootConfigPaths(): Cannot find config root path: " << path << std::endl;
187  return QStringList();
188  }
189  }
190 
191  QStringList retval;
192  if (QDir(CX_CONFIG_ROOT).exists()) // look for folder in source code
193  retval << QDir(CX_CONFIG_ROOT).canonicalPath();
194  if (QDir(CX_OPTIONAL_CONFIG_ROOT).exists()) // look for folder in source code
195  retval << QDir(CX_OPTIONAL_CONFIG_ROOT).canonicalPath();
196 
197  return retval;
198 }
199 
201 {
202  if(!isRunFromBuildFolder())
203  {
204  QString path = getBundlePath() + "/" + CX_DOC_ROOT_RELATIVE_INSTALLED; // look for installed location
205  if (QDir(path).exists())
206  return QDir(path).canonicalPath();
207  else
208  {
209  CX_LOG_ERROR() << QString("Cannot find doc path: ") << path;
210  return "";
211  }
212  }
213 
214  if (QDir(CX_DOC_ROOT).exists()) // look for folder in source code
215  return QDir(CX_DOC_ROOT).canonicalPath();
216  else
217  {
218  CX_LOG_ERROR() << QString("Cannot find doc path: ") << CX_DOC_ROOT;
219  return "";
220  }
221 }
222 
223 QStringList DataLocations::appendStringToAllElements(QStringList root, QString suffix)
224 {
225  QStringList retval;
226  for (int i=0; i<root.size(); ++i)
227  retval << root[i] + suffix;
228  return retval;
229 }
230 
231 namespace
232 {
233 QString changeExtension(QString name, QString ext)
234 {
235  QStringList splitName = name.split(".");
236  splitName[splitName.size()-1] = ext;
237  return splitName.join(".");
238 }
239 } //namespace
240 
242 {
243  QString path(getPersistentWritablePath()+"/cache/");
244  return path;
245 }
246 
247 QString DataLocations::findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
248 {
249  return findConfigPath("", pathRelativeToConfigRoot, alternativeAbsolutePath);
250 }
251 
252 QString DataLocations::findConfigPath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
253 {
254  QFileInfo filePath(findConfigFilePath(fileName, pathRelativeToConfigRoot, alternativeAbsolutePath));
255  return filePath.absolutePath() + "/";
256 }
257 
258 QString DataLocations::findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
259 {
260  QStringList paths;
261  foreach (QString root, getRootConfigPaths())
262  {
263  QString path = root + "/" + pathRelativeToConfigRoot + "/" + fileName;
264  paths << path;
265  if (QFileInfo(path).exists())
266  return path;
267  }
268 
269  QString path = QString(alternativeAbsolutePath + "/" + fileName);
270  paths << path;
271  if (QFileInfo(path).exists())
272  return path;
273 
274  reportWarning("DataLocations::findConfigFile. Error: Can't find " + fileName + " in any of\n" + paths.join(" \n"));
275  return "";
276 }
277 
278 QString DataLocations::checkExecutableExist(QString path, QString filename)
279 {
280  QStringList retval;
281  path = QDir::cleanPath(path);
282  if (QDir(path).exists(filename))
283  return QDir(DataLocations::getBundlePath()).absoluteFilePath(path + "/" + filename);
284  return "";
285 }
286 
288 {
289  QString result;
290 #ifdef __APPLE__
291  // run from installed folder on mac
292  result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
293  if (!result.isEmpty())
294  return result;
295 #endif
296  // run from installed or build bin folder
297  result = DataLocations::checkExecutableExist(DataLocations::getBundlePath(), filename);
298  if (!result.isEmpty())
299  return result;
300 
301  return result;
302 }
303 
305 {
306  QString url("http://custusx.org");
307  return url;
308 
309 }
310 
312 {
313  QString version(CustusX_VERSION_STRING);
314  if (version.contains("dev"))
315  version = "nightly";
316  QString url = QString("%1/uploads/user_doc/%2")
318  .arg(version);
319  return url;
320 }
321 
323 {
324  if(!mBuildFolderChecked)
325  {
326  QString bundlePath = DataLocations::getBundlePath();
327 
328  //Check if cxConfig.h file exists relative to the run application
329  QString pathToConfigFile = bundlePath + "/../source/resource/core/settings/cxConfig.h";
330  if (QFile(pathToConfigFile).exists())
331  {
332  std::cout << "Using paths from build folder" << std::endl;
333  mRunFromBuildFolder = true;
334  }
335  else
336  mRunFromBuildFolder = false;
337  mBuildFolderChecked = true;
338  }
339 
340  return mRunFromBuildFolder;
341 }
342 
343 } // 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 bool isRunFromBuildFolder()
static QString getWebsiteUserDocumentationURL()
static QString getLargeTestDataPath()
return path to test data folder containing large data sets
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...
#define CX_LOG_ERROR
Definition: cxLogger.h:113
static QString getWebsiteURL()
static QString findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
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 getDocPath()
return path to folder containing documentation files
static QString getBundlePath()
return the folder where the bundle or executable are located.