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