NorMIT-nav  16.5
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 
121 
123 {
124  // This method is becoming problematic (2015-11-30/CA):
125  // the APPLE case returns path to folder enclosing the bundle
126  // while the LINUX/WIN case returns path to bin folder.
127  // This is not symmetric - it should be. Try to migrate away from this
128  // method, using applicationDirPath instead, and remove it.
129  //
130 #ifdef __APPLE__
131  QString path(qApp->applicationDirPath()+"/../../..");
132  QString bundle = QDir(qApp->applicationDirPath()+"/../..").canonicalPath();
133  if (QFileInfo(bundle).isBundle())
134  return QDir(path).canonicalPath();
135  else
136  return qApp->applicationDirPath();
137 #else
138  QString path(qApp->applicationDirPath());
139  return path;
140 #endif
141 }
142 
144 {
145  QStringList retval;
146 
147  if(!isRunFromBuildFolder())
148  {
149  QString appPath(qApp->applicationDirPath());
150 
151  QString installLocation = appPath;
152 #ifndef CX_WINDOWS
153  installLocation = appPath + "/plugins";
154 #endif
155  if (QFile(installLocation).exists())
156  retval << installLocation;
157 
158  QString fallbackInstallLocation = appPath;
159  if (QFile(fallbackInstallLocation).exists())
160  retval << fallbackInstallLocation; }
161  else
162  {
163  QString bundlePath = DataLocations::getBundlePath();
164 
165  QString buildLocation = bundlePath;
166 #ifndef CX_WINDOWS
167  buildLocation = bundlePath + "/plugins";
168 #endif
169  if (QFile(buildLocation).exists())
170  retval << buildLocation;
171  }
172 
173  return retval;
174 }
175 
177 {
178  QStringList paths = getRootConfigPaths();
179  if (paths.empty())
180  return "";
181  // Those who ask for a single (legacy) config path need
182  // the default CX path, not the override.
183  return paths.back();
184 }
185 
187 {
188  if(!isRunFromBuildFolder())
189  {
190  // look for installed location
191  QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg(CX_CONFIG_ROOT_RELATIVE_INSTALLED);
192  if (QDir(path).exists())
193  return QStringList() << QDir(path).canonicalPath();
194  else
195  {
196  std::cout << "DataLocations::getRootConfigPaths(): Cannot find config root path: " << path << std::endl;
197  return QStringList();
198  }
199  }
200 
201  // add folders with the most important first: If the same file exists in both locations,
202  // the first should be prefered.
203  QStringList retval;
204  if (QDir(CX_OPTIONAL_CONFIG_ROOT).exists()) // look for override folder in source code
205  retval << QDir(CX_OPTIONAL_CONFIG_ROOT).canonicalPath();
206  if (QDir(CX_CONFIG_ROOT).exists()) // look for default folder in source code
207  retval << QDir(CX_CONFIG_ROOT).canonicalPath();
208 
209  return retval;
210 }
211 
213 {
214  if(!isRunFromBuildFolder())
215  {
216  QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg(CX_DOC_ROOT_RELATIVE_INSTALLED);
217 // QString path = getBundlePath() + "/" + CX_DOC_ROOT_RELATIVE_INSTALLED; // look for installed location
218  if (QDir(path).exists())
219  return QDir(path).canonicalPath();
220  else
221  {
222  CX_LOG_ERROR() << QString("Cannot find doc path: ") << path;
223  return "";
224  }
225  }
226 
227  if (QDir(CX_DOC_ROOT).exists()) // look for folder in source code
228  return QDir(CX_DOC_ROOT).canonicalPath();
229  else
230  {
231  CX_LOG_ERROR() << QString("Cannot find doc path: ") << CX_DOC_ROOT;
232  return "";
233  }
234 }
235 
236 QStringList DataLocations::appendStringToAllElements(QStringList root, QString suffix)
237 {
238  QStringList retval;
239  for (int i=0; i<root.size(); ++i)
240  retval << root[i] + suffix;
241  return retval;
242 }
243 
244 namespace
245 {
246 QString changeExtension(QString name, QString ext)
247 {
248  QStringList splitName = name.split(".");
249  splitName[splitName.size()-1] = ext;
250  return splitName.join(".");
251 }
252 } //namespace
253 
255 {
256  QString path(getPersistentWritablePath()+"/cache/");
257  return path;
258 }
259 
260 QString DataLocations::findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
261 {
262  return findConfigPath("", pathRelativeToConfigRoot, alternativeAbsolutePath);
263 }
264 
265 QString DataLocations::findConfigPath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
266 {
267  QFileInfo filePath(findConfigFilePath(fileName, pathRelativeToConfigRoot, alternativeAbsolutePath));
268  return filePath.absolutePath() + "/";
269 }
270 
271 QString DataLocations::findConfigFilePath(QString fileName, QString pathRelativeToConfigRoot, QString alternativeAbsolutePath)
272 {
273  QStringList paths;
274  foreach (QString root, getRootConfigPaths())
275  {
276  QString path = root + "/" + pathRelativeToConfigRoot + "/" + fileName;
277  paths << path;
278  if (QFileInfo(path).exists())
279  return path;
280  }
281 
282  QString path = QString(alternativeAbsolutePath + "/" + fileName);
283  paths << path;
284  if (QFileInfo(path).exists())
285  return path;
286 
287  reportWarning("DataLocations::findConfigFile. Error: Can't find " + fileName + " in any of\n" + paths.join(" \n"));
288  return "";
289 }
290 
291 QString DataLocations::checkExecutableExist(QString path, QString filename)
292 {
293  QStringList retval;
294  path = QDir::cleanPath(path);
295  if (QDir(path).exists(filename))
296  return QDir(DataLocations::getBundlePath()).absoluteFilePath(path + "/" + filename);
297  return "";
298 }
299 
301 {
302  QString result;
303 //#ifdef __APPLE__
304 // // run from installed folder on mac
305 // result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
306 // if (!result.isEmpty())
307 // return result;
308 //#endif
309  // run from installed or build bin folder
310  result = DataLocations::checkExecutableExist(qApp->applicationDirPath(), filename);
311  if (!result.isEmpty())
312  return result;
313 
314  return result;
315 }
316 
317 void DataLocations::setWebsiteURL(QString websiteUrl)
318 {
319  mWebsiteUrl = websiteUrl;
320 }
321 
323 {
324  return mWebsiteUrl;
325 }
326 
328 {
329  return QString("http://custusx.org/uploads");
330 }
331 
333 {
334  QString version(CustusX_VERSION_STRING);
335  if (version.contains("dev"))
336  version = "nightly";
337  QString url = QString("%1/user_doc/%2")
339  .arg(version);
340  return url;
341 }
342 
344 {
345  if(!mBuildFolderChecked)
346  {
347  QString bundlePath = DataLocations::getBundlePath();
348 
349  //Check if cxConfig.h file exists relative to the run application
350  QString pathToConfigFile = bundlePath + "/../source/resource/core/settings/cxConfig.h";
351  if (QFile(pathToConfigFile).exists())
352  {
353  std::cout << "Using paths from build folder" << std::endl;
354  mRunFromBuildFolder = true;
355  }
356  else
357  mRunFromBuildFolder = false;
358  mBuildFolderChecked = true;
359  }
360 
361  return mRunFromBuildFolder;
362 }
363 
364 } // 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 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 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()