1
/***************************************************************************
2
* Copyright (C) 2007 by Kevin Krammer <kevin.krammer@gmx.at> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU Library General Public License as *
6
* published by the Free Software Foundation; either version 2 of the *
7
* License, or (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU Library General Public *
15
* License along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18
***************************************************************************/
21
* This file was copied to solid from [akonadi]/libs/xdgbasedirs.cpp and slimmed down.
22
* See xdgbasedirs_p.h for notes about slimming down.
25
#include "xdgbasedirs_p.h"
27
#include <QtCore/QDir>
28
#include <QtCore/QFileInfo>
30
static QStringList splitPathList( const QString &pathList )
32
return pathList.split( QLatin1Char( ':' ) );
35
class XdgBaseDirsSingleton
38
QString homePath( const char *variable, const char *defaultSubDir );
40
QStringList systemPathList( const char *variable, const char *defaultDirList );
46
QStringList mConfigDirs;
47
QStringList mDataDirs;
50
Q_GLOBAL_STATIC( XdgBaseDirsSingleton, instance )
52
QString Solid::XdgBaseDirs::homePath( const char *resource )
54
if ( qstrncmp( "data", resource, 4 ) == 0 ) {
55
if ( instance()->mDataHome.isEmpty() ) {
56
instance()->mDataHome = instance()->homePath( "XDG_DATA_HOME", ".local/share" );
58
return instance()->mDataHome;
59
} else if ( qstrncmp( "config", resource, 6 ) == 0 ) {
60
if ( instance()->mConfigHome.isEmpty() ) {
61
instance()->mConfigHome = instance()->homePath( "XDG_CONFIG_HOME", ".config" );
63
return instance()->mConfigHome;
69
QStringList Solid::XdgBaseDirs::systemPathList( const char *resource )
71
if ( qstrncmp( "data", resource, 4 ) == 0 ) {
72
if ( instance()->mDataDirs.isEmpty() ) {
73
instance()->mDataDirs = instance()->systemPathList( "XDG_DATA_DIRS", "/usr/local/share:/usr/share" );
75
return instance()->mDataDirs;
76
} else if ( qstrncmp( "config", resource, 6 ) == 0 ) {
77
if ( instance()->mConfigDirs.isEmpty() ) {
78
instance()->mConfigDirs = instance()->systemPathList( "XDG_CONFIG_DIRS", "/etc/xdg" );
80
return instance()->mConfigDirs;
86
QString Solid::XdgBaseDirs::findResourceFile( const char *resource, const QString &relPath )
88
const QString fullPath = homePath( resource ) + QLatin1Char( '/' ) + relPath;
90
QFileInfo fileInfo( fullPath );
91
if ( fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() ) {
95
const QStringList pathList = systemPathList( resource );
97
foreach ( const QString &path, pathList ) {
98
fileInfo = QFileInfo( path + QLatin1Char('/' ) + relPath );
99
if ( fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() ) {
100
return fileInfo.absoluteFilePath();
107
QString XdgBaseDirsSingleton::homePath( const char *variable, const char *defaultSubDir )
109
const QByteArray env = qgetenv( variable );
112
if ( env.isEmpty() ) {
113
xdgPath = QDir::homePath() + QLatin1Char( '/' ) + QLatin1String( defaultSubDir );
114
} else if ( env.startsWith( '/' ) ) {
115
xdgPath = QString::fromLocal8Bit( env );
117
xdgPath = QDir::homePath() + QLatin1Char( '/' ) + QString::fromLocal8Bit( env );
123
QStringList XdgBaseDirsSingleton::systemPathList( const char *variable, const char *defaultDirList )
125
const QByteArray env = qgetenv( variable );
128
if ( env.isEmpty() ) {
129
xdgDirList = QLatin1String( defaultDirList );
131
xdgDirList = QString::fromLocal8Bit( env );
134
return splitPathList( xdgDirList );