• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
MarbleDirs.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2004-2007 Torsten Rahn <tackat@kde.org>
9 // Copyright 2007 Inge Wallin <ingwa@kde.org>
10 //
11 
12 
13 #include "MarbleDirs.h"
14 #include "MarbleDebug.h"
15 
16 #include <QDir>
17 #include <QFile>
18 #include <QString>
19 #include <QStringList>
20 #include <QApplication>
21 
22 #include <stdlib.h>
23 
24 #if QT_VERSION >= 0x050000
25 #include <QStandardPaths>
26 #else
27 #include <QDesktopServices>
28 #endif
29 
30 #ifdef Q_OS_WIN
31 //for getting appdata path
32 //mingw-w64 Internet Explorer 5.01
33 #define _WIN32_IE 0x0501
34 #include <shlobj.h>
35 #endif
36 
37 #ifdef Q_OS_MACX
38 //for getting app bundle path
39 #include <ApplicationServices/ApplicationServices.h>
40 #endif
41 
42 #include <config-marble.h>
43 
44 using namespace Marble;
45 
46 namespace
47 {
48  QString runTimeMarbleDataPath = "";
49 
50  QString runTimeMarblePluginPath = "";
51 }
52 
53 MarbleDirs::MarbleDirs()
54  : d( 0 )
55 {
56 }
57 
58 
59 QString MarbleDirs::path( const QString& relativePath )
60 {
61  QString localpath = localPath() + '/' + relativePath; // local path
62  QString systempath = systemPath() + '/' + relativePath; // system path
63 
64 
65  QString fullpath = systempath;
66  if ( QFile::exists( localpath ) ) {
67  fullpath = localpath;
68  }
69  return QDir( fullpath ).canonicalPath();
70 }
71 
72 
73 QString MarbleDirs::pluginPath( const QString& relativePath )
74 {
75  QString localpath = pluginLocalPath() + QDir::separator() + relativePath; // local path
76  QString systempath = pluginSystemPath() + QDir::separator() + relativePath; // system path
77 
78 
79  QString fullpath = systempath;
80  if ( QFile::exists( localpath ) ) {
81  fullpath = localpath;
82  }
83 
84  return QDir( fullpath ).canonicalPath();
85 }
86 
87 QStringList MarbleDirs::entryList( const QString& relativePath, QDir::Filters filters )
88 {
89  QStringList filesLocal = QDir( MarbleDirs::localPath() + '/' + relativePath ).entryList(filters);
90  QStringList filesSystem = QDir( MarbleDirs::systemPath() + '/' + relativePath ).entryList(filters);
91  QStringList allFiles( filesLocal );
92  allFiles << filesSystem;
93 
94  // remove duplicate entries
95  allFiles.sort();
96  for ( int i = 1; i < allFiles.size(); ++i ) {
97  if ( allFiles.at(i) == allFiles.at( i - 1 ) ) {
98  allFiles.removeAt(i);
99  --i;
100  }
101  }
102 
103  return allFiles;
104 }
105 
106 QStringList MarbleDirs::pluginEntryList( const QString& relativePath, QDir::Filters filters )
107 {
108  QStringList filesLocal = QDir( MarbleDirs::pluginLocalPath() + '/' + relativePath ).entryList(filters);
109  QStringList filesSystem = QDir( MarbleDirs::pluginSystemPath() + '/' + relativePath ).entryList(filters);
110  QStringList allFiles( filesLocal );
111  allFiles << filesSystem;
112 
113  // remove duplicate entries
114  allFiles.sort();
115  for ( int i = 1; i < allFiles.size(); ++i ) {
116  if ( allFiles.at(i) == allFiles.at( i - 1 ) ) {
117  allFiles.removeAt(i);
118  --i;
119  }
120  }
121 
122  return allFiles;
123 }
124 
125 QString MarbleDirs::systemPath()
126 {
127  QString systempath;
128 
129 #ifdef Q_OS_MACX
130  //
131  // On OSX lets try to find any file first in the bundle
132  // before branching out to home and sys dirs
133  //
134  CFURLRef myBundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
135  CFStringRef myMacPath = CFURLCopyFileSystemPath(myBundleRef, kCFURLPOSIXPathStyle);
136  const char *mypPathPtr = CFStringGetCStringPtr(myMacPath,CFStringGetSystemEncoding());
137  CFRelease(myBundleRef);
138  QString myPath(mypPathPtr);
139  CFRelease(myMacPath);
140  //do some magick so that we can still find data dir if
141  //marble was not built as a bundle
142  if (myPath.contains(".app")) //its a bundle!
143  {
144  systempath = myPath + "/Contents/Resources/data";
145  }
146 
147  if ( QFile::exists( systempath ) ){
148  return systempath;
149  }
150 #endif // mac bundle
151 
152 // Should this happen before the Mac bundle already?
153 if ( !runTimeMarbleDataPath.isEmpty() )
154  return runTimeMarbleDataPath;
155 
156 #ifdef MARBLE_DATA_PATH
157  //MARBLE_DATA_PATH is a compiler define set by cmake
158  QString compileTimeMarbleDataPath(MARBLE_DATA_PATH);
159 
160  if(QDir(compileTimeMarbleDataPath).exists())
161  return compileTimeMarbleDataPath;
162 #endif // MARBLE_DATA_PATH
163 
164  return QDir( QCoreApplication::applicationDirPath()
165 
166 #if defined(QTONLY)
167  + QLatin1String( "/data" )
168 #else
169  + QLatin1String( "/../share/apps/marble/data" )
170 #endif
171  ).canonicalPath();
172 }
173 
174 QString MarbleDirs::pluginSystemPath()
175 {
176  QString systempath;
177 
178 #ifdef Q_OS_MACX
179  //
180  // On OSX lets try to find any file first in the bundle
181  // before branching out to home and sys dirs
182  //
183  CFURLRef myBundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
184  CFStringRef myMacPath = CFURLCopyFileSystemPath(myBundleRef, kCFURLPOSIXPathStyle);
185  const char *mypPathPtr = CFStringGetCStringPtr(myMacPath,CFStringGetSystemEncoding());
186  CFRelease(myBundleRef);
187  CFRelease(myMacPath);
188  QString myPath(mypPathPtr);
189  //do some magick so that we can still find data dir if
190  //marble was not built as a bundle
191  if (myPath.contains(".app")) //its a bundle!
192  {
193  systempath = myPath + "/Contents/Resources/plugins";
194  }
195 
196  if ( QFile::exists( systempath ) ){
197  return systempath;
198  }
199 #endif // mac bundle
200 
201 // Should this happen before the Mac bundle already?
202 if ( !runTimeMarblePluginPath.isEmpty() )
203  return runTimeMarblePluginPath;
204 
205 #ifdef MARBLE_PLUGIN_PATH
206  //MARBLE_PLUGIN_PATH is a compiler define set by cmake
207  QString compileTimeMarblePluginPath(MARBLE_PLUGIN_PATH);
208 
209  if(QDir(compileTimeMarblePluginPath).exists())
210  return compileTimeMarblePluginPath;
211 #endif // MARBLE_PLUGIN_PATH
212 
213  return QDir( QCoreApplication::applicationDirPath()
214 
215 #if defined(QTONLY)
216  + QLatin1String( "/plugins" )
217 #else
218  + QLatin1String( "/../lib/kde4/plugins/marble" )
219 #endif
220  ).canonicalPath();
221 }
222 
223 QString MarbleDirs::localPath()
224 {
225 #ifndef Q_OS_WIN
226  QString dataHome = getenv( "XDG_DATA_HOME" );
227  if( dataHome.isEmpty() )
228  dataHome = QDir::homePath() + "/.local/share";
229 
230  return dataHome + "/marble"; // local path
231 #else
232 #if QT_VERSION >= 0x050000
233  return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/.marble/data";
234 #else
235  return QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/.marble/data";
236 #endif
237 #endif
238 }
239 
240 QStringList MarbleDirs::oldLocalPaths()
241 {
242  QStringList possibleOldPaths;
243 
244 #ifndef Q_OS_WIN
245  QString oldDefault = QDir::homePath() + "/.marble/data";
246  possibleOldPaths.append( oldDefault );
247 
248  QString xdgDefault = QDir::homePath() + "/.local/share/marble";
249  possibleOldPaths.append( xdgDefault );
250 
251  QString xdg = getenv( "XDG_DATA_HOME" );
252  xdg += "/marble/";
253  possibleOldPaths.append( xdg );
254 #endif
255 
256 #ifdef Q_OS_WIN
257  HWND hwnd = 0;
258  WCHAR *appdata_path = new WCHAR[MAX_PATH + 1];
259 
260  SHGetSpecialFolderPathW(hwnd, appdata_path, CSIDL_APPDATA, 0);
261  QString appdata = QString::fromUtf16(reinterpret_cast<ushort*>(appdata_path));
262  delete[] appdata_path;
263  possibleOldPaths << QString(QDir::fromNativeSeparators(appdata) + "/.marble/data"); // local path
264 #endif
265 
266  QString currentLocalPath = QDir( MarbleDirs::localPath() ).canonicalPath();
267  QStringList oldPaths;
268  foreach( const QString& possibleOldPath, possibleOldPaths ) {
269  if( !QDir().exists( possibleOldPath ) ) {
270  continue;
271  }
272 
273  QString canonicalPossibleOldPath = QDir( possibleOldPath ).canonicalPath();
274  if( canonicalPossibleOldPath == currentLocalPath ) {
275  continue;
276  }
277 
278  oldPaths.append( canonicalPossibleOldPath );
279  }
280 
281  return oldPaths;
282 }
283 
284 QString MarbleDirs::pluginLocalPath()
285 {
286 #ifndef Q_OS_WIN
287  return QString( QDir::homePath() + "/.marble/plugins" ); // local path
288 #else
289 #if QT_VERSION >= 0x050000
290  return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/.marble/plugins";
291 #else
292  return QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/.marble/plugins";
293 #endif
294 #endif
295 }
296 
297 QString MarbleDirs::marbleDataPath()
298 {
299  return runTimeMarbleDataPath;
300 }
301 
302 QString MarbleDirs::marblePluginPath()
303 {
304  return runTimeMarblePluginPath;
305 }
306 
307 void MarbleDirs::setMarbleDataPath( const QString& adaptedPath )
308 {
309  if ( !QDir::root().exists( adaptedPath ) )
310  {
311  qWarning() << QString( "Invalid MarbleDataPath \"%1\". Using \"%2\" instead." ).arg( adaptedPath ).arg( systemPath() );
312  return;
313  }
314 
315  runTimeMarbleDataPath = adaptedPath;
316 }
317 
318 void MarbleDirs::setMarblePluginPath( const QString& adaptedPath )
319 {
320  if ( !QDir::root().exists( adaptedPath ) )
321  {
322  qWarning() << QString( "Invalid MarblePluginPath \"%1\". Using \"%2\" instead." ).arg( adaptedPath ).arg( pluginSystemPath() );
323  return;
324  }
325 
326  runTimeMarblePluginPath = adaptedPath;
327 }
328 
329 
330 void MarbleDirs::debug()
331 {
332  mDebug() << "=== MarbleDirs: ===";
333  mDebug() << "Local Path:" << localPath();
334  mDebug() << "Plugin Local Path:" << pluginLocalPath();
335  mDebug() << "";
336  mDebug() << "Marble Data Path (Run Time) :" << runTimeMarbleDataPath;
337  mDebug() << "Marble Data Path (Compile Time):" << QString(MARBLE_DATA_PATH);
338  mDebug() << "";
339  mDebug() << "Marble Plugin Path (Run Time) :" << runTimeMarblePluginPath;
340  mDebug() << "Marble Plugin Path (Compile Time):" << QString(MARBLE_PLUGIN_PATH);
341  mDebug() << "";
342  mDebug() << "System Path:" << systemPath();
343  mDebug() << "Plugin System Path:" << pluginSystemPath();
344  mDebug() << "===================";
345 }
Marble::MarbleDirs::marbleDataPath
static QString marbleDataPath()
Definition: MarbleDirs.cpp:297
Marble::MarbleDirs::pluginPath
static QString pluginPath(const QString &relativePath)
Definition: MarbleDirs.cpp:73
Marble::MarbleDirs::setMarbleDataPath
static void setMarbleDataPath(const QString &adaptedPath)
Definition: MarbleDirs.cpp:307
Marble::MarbleDirs::path
static QString path(const QString &relativePath)
Definition: MarbleDirs.cpp:59
QDir::fromNativeSeparators
QString fromNativeSeparators(const QString &pathName)
Marble::MarbleDirs::setMarblePluginPath
static void setMarblePluginPath(const QString &adaptedPath)
Definition: MarbleDirs.cpp:318
QList::at
const T & at(int i) const
QList::removeAt
void removeAt(int i)
Marble::MarbleDirs::pluginSystemPath
static QString pluginSystemPath()
Definition: MarbleDirs.cpp:174
Marble::MarbleDirs::localPath
static QString localPath()
Definition: MarbleDirs.cpp:223
QFile::exists
bool exists() const
QDir::root
QDir root()
QDir::homePath
QString homePath()
MarbleDebug.h
QDir::separator
QChar separator()
QList::size
int size() const
QList::append
void append(const T &value)
QString::fromUtf16
QString fromUtf16(const ushort *unicode, int size)
Marble::MarbleDirs::MarbleDirs
MarbleDirs()
Definition: MarbleDirs.cpp:53
QDesktopServices::storageLocation
QString storageLocation(StandardLocation type)
QDir::Filters
typedef Filters
MarbleDirs.h
QString::isEmpty
bool isEmpty() const
QString
QStringList
Marble::MarbleDirs::debug
static void debug()
Definition: MarbleDirs.cpp:330
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QDir
Marble::MarbleDirs::marblePluginPath
static QString marblePluginPath()
Definition: MarbleDirs.cpp:302
QLatin1String
QDir::entryList
QStringList entryList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
Marble::MarbleDirs::pluginEntryList
static QStringList pluginEntryList(const QString &relativePath, QDir::Filters filters=QDir::NoFilter)
Definition: MarbleDirs.cpp:106
Marble::MarbleDirs::entryList
static QStringList entryList(const QString &relativePath, QDir::Filters filters=QDir::NoFilter)
Definition: MarbleDirs.cpp:87
QStringList::sort
void sort()
Marble::MarbleDirs::systemPath
static QString systemPath()
Definition: MarbleDirs.cpp:125
QDir::canonicalPath
QString canonicalPath() const
QCoreApplication::applicationDirPath
QString applicationDirPath()
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::MarbleDirs::pluginLocalPath
static QString pluginLocalPath()
Definition: MarbleDirs.cpp:284
Marble::MarbleDirs::oldLocalPaths
static QStringList oldLocalPaths()
Definition: MarbleDirs.cpp:240
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal