Fraxinus  17.12-rc2
An IGT application
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 QString DataLocations::mWebsiteUrl = "";
52 //---------------------------------------------------------
53 
55 {
56  mTestMode = true;
59 }
60 
62 {
63  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
64  QString dataRootConfigFile = settingsPath + "/data_root_location.txt";
65  if (QFileInfo(dataRootConfigFile).exists())
66  {
67  return readTestDataPathFromFile(dataRootConfigFile);
68  }
69  else
70  {
71  return CX_DATA_ROOT;
72  }
73 }
74 
76 {
77  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
78  QString dataRootConfigFile = settingsPath + "/large_data_root_location.txt";
79  if (QFileInfo(dataRootConfigFile).exists())
80  {
81  return readTestDataPathFromFile(dataRootConfigFile);
82  }
83  else
84  {
85  return CX_LARGE_DATA_ROOT;
86  }
87 }
88 
89 QString DataLocations::getExistingTestData(QString pathRelativeToTestDataRoot, QString filename)
90 {
91  QString path;
92 
93  path = QString("%1/%2/%3").arg(getTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
94  if (QFileInfo(path).exists())
95  return path;
96 
97  path = QString("%1/%2/%3").arg(getLargeTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
98  if (QFileInfo(path).exists())
99  return path;
100 
101  return "";
102 }
103 
104 QString DataLocations::readTestDataPathFromFile(QString filename)
105 {
106  QFile file(filename);
107  file.open(QFile::ReadOnly);
108  QString cxDataRoot(file.readAll());
109  return cxDataRoot;
110 }
111 
113 {
114  QString homepath = QDir::homePath() + "/" + CX_SYSTEM_BASE_NAME + "_settings";
115 
116  if (mTestMode)
117  homepath = homepath + "/temp";
118 
119  return homepath;
120 }
121 
123 {
124  QString pathToDelete = DataLocations::getPersistentWritablePath();
125  QDir dir(pathToDelete);
126  CX_LOG_INFO() << "Going to delete:" << dir.absolutePath();
127  dir.removeRecursively();
128 }
129 
130 
132 {
133  // This method is becoming problematic (2015-11-30/CA):
134  // the APPLE case returns path to folder enclosing the bundle
135  // while the LINUX/WIN case returns path to bin folder.
136  // This is not symmetric - it should be. Try to migrate away from this
137  // method, using applicationDirPath instead, and remove it.
138  //
139 #ifdef __APPLE__
140  QString path(qApp->applicationDirPath()+"/../../..");
141  QString bundle = QDir(qApp->applicationDirPath()+"/../..").canonicalPath();
142  if (QFileInfo(bundle).isBundle())
143  return QDir(path).canonicalPath();
144  else
145  return qApp->applicationDirPath();
146 #else
147  QString path(qApp->applicationDirPath());
148  return path;
149 #endif
150 }
151 
153 {
154  QStringList retval;
155 
156  if(!isRunFromBuildFolder())
157  {
158  QString appPath(qApp->applicationDirPath());
159 
160  QString installLocation = appPath;
161 #ifndef CX_WINDOWS
162  installLocation = appPath + "/plugins";
163 #endif
164  if (QFile(installLocation).exists())
165  retval << installLocation;
166 
167  QString fallbackInstallLocation = appPath;
168  if (QFile(fallbackInstallLocation).exists())
169  retval << fallbackInstallLocation; }
170  else
171  {
172  QString bundlePath = DataLocations::getBundlePath();
173 
174  QString buildLocation = bundlePath;
175 #ifndef CX_WINDOWS
176  buildLocation = bundlePath + "/plugins";
177 #endif
178  if (QFile(buildLocation).exists())
179  retval << buildLocation;
180  }
181 
182  return retval;
183 }
184 
186 {
187  QStringList paths = getRootConfigPaths();
188  if (paths.empty())
189  return "";
190  // Those who ask for a single (legacy) config path need
191  // the default CX path, not the override.
192  return paths.back();
193 }
194 
196 {
197  if(!isRunFromBuildFolder())
198  {
199  // look for installed location
200  QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg(CX_CONFIG_ROOT_RELATIVE_INSTALLED);
201  if (QDir(path).exists())
202  return QStringList() << QDir(path).canonicalPath();
203  else
204  {
205  std::cout << "DataLocations::getRootConfigPaths(): Cannot find config root path: " << path << std::endl;
206  return QStringList();
207  }
208  }
209 
210  // add folders with the most important first: If the same file exists in both locations,
211  // the first should be prefered.
212  QStringList retval;
213  if (QDir(CX_OPTIONAL_CONFIG_ROOT).exists()) // look for override folder in source code
214  retval << QDir(CX_OPTIONAL_CONFIG_ROOT).canonicalPath();
215  if (QDir(CX_CONFIG_ROOT).exists()) // look for default folder in source code
216  retval << QDir(CX_CONFIG_ROOT).canonicalPath();
217 
218  return retval;
219 }
220 
222 {
223  if(!isRunFromBuildFolder())
224  {
225  QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg(CX_DOC_ROOT_RELATIVE_INSTALLED);
226 // QString path = getBundlePath() + "/" + CX_DOC_ROOT_RELATIVE_INSTALLED; // look for installed location
227  if (QDir(path).exists())
228  return QDir(path).canonicalPath();
229  else
230  {
231  CX_LOG_ERROR() << QString("Cannot find doc path: ") << path;
232  return "";
233  }
234  }
235 
236  if (QDir(CX_DOC_ROOT).exists()) // look for folder in source code
237  return QDir(CX_DOC_ROOT).canonicalPath();
238  else
239  {
240  CX_LOG_ERROR() << QString("Cannot find doc path: ") << CX_DOC_ROOT;
241  return "";
242  }
243 }
244 
245 QStringList DataLocations::appendStringToAllElements(QStringList root, QString suffix)
246 {
247  QStringList retval;
248  for (int i=0; i<root.size(); ++i)
249  retval << root[i] + suffix;
250  return retval;
251 }
252 
253 namespace
254 {
255 QString changeExtension(QString name, QString ext)
256 {
257  QStringList splitName = name.split(".");
258  splitName[splitName.size()-1] = ext;
259  return splitName.join(".");
260 }
261 } //namespace
262 
264 {
265  QString path(getPersistentWritablePath()+"/cache");
266  return path;
267 }
268 
269 QString DataLocations::findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
270 {
271  return findConfigPath("", pathRelativeToConfigRoot, alternativeAbsolutePath);
272 }
273 
274 QString DataLocations::findConfigPath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
275 {
276  QFileInfo filePath(findConfigFilePath(fileName, pathRelativeToConfigRoot, alternativeAbsolutePath));
277  return filePath.absolutePath() + "/";
278 }
279 
280 QString DataLocations::findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
281 {
282  QStringList paths;
283  foreach (QString root, getRootConfigPaths())
284  {
285  QString path = root + "/" + pathRelativeToConfigRoot + "/" + fileName;
286  paths << path;
287  if (QFileInfo(path).exists())
288  return path;
289  }
290 
291  QString path = QString(alternativeAbsolutePath + "/" + fileName);
292  paths << path;
293  if (QFileInfo(path).exists())
294  return path;
295 
296  reportWarning("DataLocations::findConfigFile. Error: Can't find " + fileName + " in any of\n" + paths.join(" \n"));
297  return "";
298 }
299 
300 QString DataLocations::checkExecutableExist(QString path, QString filename)
301 {
302  QStringList retval;
303  path = QDir::cleanPath(path);
304  if (QDir(path).exists(filename))
305  return QDir(DataLocations::getBundlePath()).absoluteFilePath(path + "/" + filename);
306  return "";
307 }
308 
310 {
311  QString result;
312 //#ifdef __APPLE__
313 // // run from installed folder on mac
314 // result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
315 // if (!result.isEmpty())
316 // return result;
317 //#endif
318  // run from installed or build bin folder
319  result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
320  if (!result.isEmpty())
321  return result;
322 
323  return result;
324 }
325 
326 void DataLocations::setWebsiteURL(QString websiteUrl)
327 {
328  mWebsiteUrl = websiteUrl;
329 }
330 
332 {
333  return mWebsiteUrl;
334 }
335 
337 {
338  return QString("http://custusx.org/uploads");
339 }
340 
342 {
343  QString version(CustusX_VERSION_STRING);
344  if (version.contains("dev"))
345  version = "nightly";
346  QString url = QString("%1/user_doc/%2")
348  .arg(version);
349  return url;
350 }
351 
353 {
354  if(!mBuildFolderChecked)
355  {
356  QString bundlePath = DataLocations::getBundlePath();
357 
358  //Check if cxConfig.h file exists relative to the run application
359  QString pathToConfigFile = bundlePath + "/../source/resource/core/settings/cxConfig.h";
360  if (QFile(pathToConfigFile).exists())
361  {
362  std::cout << "Using paths from build folder" << std::endl;
363  mRunFromBuildFolder = true;
364  }
365  else
366  mRunFromBuildFolder = false;
367  mBuildFolderChecked = true;
368  }
369 
370  return mRunFromBuildFolder;
371 }
372 
373 } // 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)
#define CX_LOG_INFO
Definition: cxLogger.h:117
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 void setWebsiteURL(QString websiteUrl)
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:120
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 void deletePersistentWritablePath()
Deletes the folder called *_settings.
static QString getDocPath()
return path to folder containing documentation files
static QString getBundlePath()
return the folder where the bundle or executable are located.
static QString getUploadsUrl()
Namespace for all CustusX production code.