NorMIT-nav  18.04
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxDataLocations.h"
12 
13 #include <iostream>
14 #include <QApplication>
15 #include <QDir>
16 #include "cxConfig.h"
17 #include "cxSettings.h"
18 #include "cxFileHelpers.h"
19 #include "cxTypeConversions.h"
20 #include "cxReporter.h"
21 #include "cxProfile.h"
22 
23 namespace cx
24 {
25 
26 //---------------------------------------------------------
27 bool DataLocations::mTestMode = false;
28 bool DataLocations::mRunFromBuildFolder = false;
29 bool DataLocations::mBuildFolderChecked = false;
30 QString DataLocations::mWebsiteUrl = "";
31 //---------------------------------------------------------
32 
34 {
35  mTestMode = true;
38 }
39 
41 {
42  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
43  QString dataRootConfigFile = settingsPath + "/data_root_location.txt";
44  if (QFileInfo(dataRootConfigFile).exists())
45  {
46  return readTestDataPathFromFile(dataRootConfigFile);
47  }
48  else
49  {
50  return CX_DATA_ROOT;
51  }
52 }
53 
55 {
56  QString settingsPath = cx::DataLocations::getRootConfigPath() + "/settings";
57  QString dataRootConfigFile = settingsPath + "/large_data_root_location.txt";
58  if (QFileInfo(dataRootConfigFile).exists())
59  {
60  return readTestDataPathFromFile(dataRootConfigFile);
61  }
62  else
63  {
64  return CX_LARGE_DATA_ROOT;
65  }
66 }
67 
68 QString DataLocations::getExistingTestData(QString pathRelativeToTestDataRoot, QString filename)
69 {
70  QString path;
71 
72  path = QString("%1/%2/%3").arg(getTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
73  if (QFileInfo(path).exists())
74  return path;
75 
76  path = QString("%1/%2/%3").arg(getLargeTestDataPath()).arg(pathRelativeToTestDataRoot).arg(filename);
77  if (QFileInfo(path).exists())
78  return path;
79 
80  return "";
81 }
82 
83 QString DataLocations::readTestDataPathFromFile(QString filename)
84 {
85  QFile file(filename);
86  file.open(QFile::ReadOnly);
87  QString cxDataRoot(file.readAll());
88  return cxDataRoot;
89 }
90 
92 {
93  QString homepath = QDir::homePath() + "/" + CX_SYSTEM_BASE_NAME + "_settings";
94 
95  if (mTestMode)
96  homepath = homepath + "/temp";
97 
98  return homepath;
99 }
100 
102 {
103  QString pathToDelete = DataLocations::getPersistentWritablePath();
104  QDir dir(pathToDelete);
105  CX_LOG_INFO() << "Going to delete:" << dir.absolutePath();
106  dir.removeRecursively();
107 }
108 
109 
111 {
112  // This method is becoming problematic (2015-11-30/CA):
113  // the APPLE case returns path to folder enclosing the bundle
114  // while the LINUX/WIN case returns path to bin folder.
115  // This is not symmetric - it should be. Try to migrate away from this
116  // method, using applicationDirPath instead, and remove it.
117  //
118 #ifdef __APPLE__
119  QString path(qApp->applicationDirPath()+"/../../..");
120  QString bundle = QDir(qApp->applicationDirPath()+"/../..").canonicalPath();
121  if (QFileInfo(bundle).isBundle())
122  return QDir(path).canonicalPath();
123  else
124  return qApp->applicationDirPath();
125 #else
126  QString path(qApp->applicationDirPath());
127  return path;
128 #endif
129 }
130 
132 {
133  QStringList retval;
134 
135  if(!isRunFromBuildFolder())
136  {
137  QString appPath(qApp->applicationDirPath());
138 
139  QString installLocation = appPath;
140 #ifndef CX_WINDOWS
141  installLocation = appPath + "/plugins";
142 #endif
143  if (QFile(installLocation).exists())
144  retval << installLocation;
145 
146  QString fallbackInstallLocation = appPath;
147  if (QFile(fallbackInstallLocation).exists())
148  retval << fallbackInstallLocation; }
149  else
150  {
151  QString bundlePath = DataLocations::getBundlePath();
152 
153  QString buildLocation = bundlePath;
154 #ifndef CX_WINDOWS
155  buildLocation = bundlePath + "/plugins";
156 #endif
157  if (QFile(buildLocation).exists())
158  retval << buildLocation;
159  }
160 
161  return retval;
162 }
163 
165 {
166  QStringList paths = getRootConfigPaths();
167  if (paths.empty())
168  return "";
169  // Those who ask for a single (legacy) config path need
170  // the default CX path, not the override.
171  return paths.back();
172 }
173 
175 {
176  if(!isRunFromBuildFolder())
177  {
178  // look for installed location
179  QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg(CX_CONFIG_ROOT_RELATIVE_INSTALLED);
180  if (QDir(path).exists())
181  return QStringList() << QDir(path).canonicalPath();
182  else
183  {
184  std::cout << "DataLocations::getRootConfigPaths(): Cannot find config root path: " << path << std::endl;
185  return QStringList();
186  }
187  }
188 
189  // add folders with the most important first: If the same file exists in both locations,
190  // the first should be prefered.
191  QStringList retval;
192  if (QDir(CX_OPTIONAL_CONFIG_ROOT).exists()) // look for override folder in source code
193  retval << QDir(CX_OPTIONAL_CONFIG_ROOT).canonicalPath();
194  if (QDir(CX_CONFIG_ROOT).exists()) // look for default folder in source code
195  retval << QDir(CX_CONFIG_ROOT).canonicalPath();
196 
197  return retval;
198 }
199 
201 {
202  if(!isRunFromBuildFolder())
203  {
204  QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg(CX_DOC_ROOT_RELATIVE_INSTALLED);
205 // QString path = getBundlePath() + "/" + CX_DOC_ROOT_RELATIVE_INSTALLED; // look for installed location
206  if (QDir(path).exists())
207  return QDir(path).canonicalPath();
208  else
209  {
210  CX_LOG_ERROR() << QString("Cannot find doc path: ") << path;
211  return "";
212  }
213  }
214 
215  if (QDir(CX_DOC_ROOT).exists()) // look for folder in source code
216  return QDir(CX_DOC_ROOT).canonicalPath();
217  else
218  {
219  CX_LOG_ERROR() << QString("Cannot find doc path: ") << CX_DOC_ROOT;
220  return "";
221  }
222 }
223 
224 QStringList DataLocations::appendStringToAllElements(QStringList root, QString suffix)
225 {
226  QStringList retval;
227  for (int i=0; i<root.size(); ++i)
228  retval << root[i] + suffix;
229  return retval;
230 }
231 
232 namespace
233 {
234 QString changeExtension(QString name, QString ext)
235 {
236  QStringList splitName = name.split(".");
237  splitName[splitName.size()-1] = ext;
238  return splitName.join(".");
239 }
240 } //namespace
241 
243 {
244  QString path(getPersistentWritablePath()+"/cache");
245  return path;
246 }
247 
248 QString DataLocations::findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
249 {
250  return findConfigPath("", pathRelativeToConfigRoot, alternativeAbsolutePath);
251 }
252 
253 QString DataLocations::findConfigPath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
254 {
255  QFileInfo filePath(findConfigFilePath(fileName, pathRelativeToConfigRoot, alternativeAbsolutePath));
256  return filePath.absolutePath() + "/";
257 }
258 
259 QString DataLocations::findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
260 {
261  QStringList paths;
262  foreach (QString root, getRootConfigPaths())
263  {
264  QString path = root + "/" + pathRelativeToConfigRoot + "/" + fileName;
265  paths << path;
266  if (QFileInfo(path).exists())
267  return path;
268  }
269 
270  QString path = QString(alternativeAbsolutePath + "/" + fileName);
271  paths << path;
272  if (QFileInfo(path).exists())
273  return path;
274 
275  reportWarning("DataLocations::findConfigFile. Error: Can't find " + fileName + " in any of\n" + paths.join(" \n"));
276  return "";
277 }
278 
279 QString DataLocations::checkExecutableExist(QString path, QString filename)
280 {
281  QStringList retval;
282  path = QDir::cleanPath(path);
283  if (QDir(path).exists(filename))
284  return QDir(DataLocations::getBundlePath()).absoluteFilePath(path + "/" + filename);
285  return "";
286 }
287 
289 {
290  QString result;
291 //#ifdef __APPLE__
292 // // run from installed folder on mac
293 // result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
294 // if (!result.isEmpty())
295 // return result;
296 //#endif
297  // run from installed or build bin folder
298  result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
299  if (!result.isEmpty())
300  return result;
301 
302  return result;
303 }
304 
305 void DataLocations::setWebsiteURL(QString websiteUrl)
306 {
307  mWebsiteUrl = websiteUrl;
308 }
309 
311 {
312  return mWebsiteUrl;
313 }
314 
316 {
317  return QString("http://custusx.org/uploads");
318 }
319 
321 {
322  QString version(CustusX_VERSION_STRING);
323  if (version.contains("dev"))
324  version = "nightly";
325  QString url = QString("%1/user_doc/%2")
327  .arg(version);
328  return url;
329 }
330 
332 {
333  if(!mBuildFolderChecked)
334  {
335  QString bundlePath = DataLocations::getBundlePath();
336 
337  //Check if cxConfig.h file exists relative to the run application
338  QString pathToConfigFile = bundlePath + "/../source/resource/core/settings/cxConfig.h";
339  if (QFile(pathToConfigFile).exists())
340  {
341  std::cout << "Using paths from build folder" << std::endl;
342  mRunFromBuildFolder = true;
343  }
344  else
345  mRunFromBuildFolder = false;
346  mBuildFolderChecked = true;
347  }
348 
349  return mRunFromBuildFolder;
350 }
351 
352 } // 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:96
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:70
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:99
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.