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

marble

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