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

Solid

  • sources
  • kde-4.14
  • kdelibs
  • solid
  • solid
xdgbasedirs.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007 by Kevin Krammer <kevin.krammer@gmx.at> *
3  * *
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. *
8  * *
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. *
13  * *
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  ***************************************************************************/
19 
20 /*
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.
23  */
24 
25 #include "xdgbasedirs_p.h"
26 
27 #include <QtCore/QDir>
28 #include <QtCore/QFileInfo>
29 
30 static QStringList splitPathList( const QString &pathList )
31 {
32  return pathList.split( QLatin1Char( ':' ) );
33 }
34 
35 class XdgBaseDirsSingleton
36 {
37  public:
38  QString homePath( const char *variable, const char *defaultSubDir );
39 
40  QStringList systemPathList( const char *variable, const char *defaultDirList );
41 
42  public:
43  QString mConfigHome;
44  QString mDataHome;
45 
46  QStringList mConfigDirs;
47  QStringList mDataDirs;
48 };
49 
50 Q_GLOBAL_STATIC( XdgBaseDirsSingleton, instance )
51 
52 QString Solid::XdgBaseDirs::homePath( const char *resource )
53 {
54  if ( qstrncmp( "data", resource, 4 ) == 0 ) {
55  if ( instance()->mDataHome.isEmpty() ) {
56  instance()->mDataHome = instance()->homePath( "XDG_DATA_HOME", ".local/share" );
57  }
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" );
62  }
63  return instance()->mConfigHome;
64  }
65 
66  return QString();
67 }
68 
69 QStringList Solid::XdgBaseDirs::systemPathList( const char *resource )
70 {
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" );
74  }
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" );
79  }
80  return instance()->mConfigDirs;
81  }
82 
83  return QStringList();
84 }
85 
86 QString Solid::XdgBaseDirs::findResourceFile( const char *resource, const QString &relPath )
87 {
88  const QString fullPath = homePath( resource ) + QLatin1Char( '/' ) + relPath;
89 
90  QFileInfo fileInfo( fullPath );
91  if ( fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() ) {
92  return fullPath;
93  }
94 
95  const QStringList pathList = systemPathList( resource );
96 
97  foreach ( const QString &path, pathList ) {
98  fileInfo = QFileInfo( path + QLatin1Char('/' ) + relPath );
99  if ( fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() ) {
100  return fileInfo.absoluteFilePath();
101  }
102  }
103 
104  return QString();
105 }
106 
107 QString XdgBaseDirsSingleton::homePath( const char *variable, const char *defaultSubDir )
108 {
109  const QByteArray env = qgetenv( variable );
110 
111  QString xdgPath;
112  if ( env.isEmpty() ) {
113  xdgPath = QDir::homePath() + QLatin1Char( '/' ) + QLatin1String( defaultSubDir );
114  } else if ( env.startsWith( '/' ) ) {
115  xdgPath = QString::fromLocal8Bit( env );
116  } else {
117  xdgPath = QDir::homePath() + QLatin1Char( '/' ) + QString::fromLocal8Bit( env );
118  }
119 
120  return xdgPath;
121 }
122 
123 QStringList XdgBaseDirsSingleton::systemPathList( const char *variable, const char *defaultDirList )
124 {
125  const QByteArray env = qgetenv( variable );
126 
127  QString xdgDirList;
128  if ( env.isEmpty() ) {
129  xdgDirList = QLatin1String( defaultDirList );
130  } else {
131  xdgDirList = QString::fromLocal8Bit( env );
132  }
133 
134  return splitPathList( xdgDirList );
135 }
QFileInfo::isReadable
bool isReadable() const
QByteArray
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QByteArray::isEmpty
bool isEmpty() const
QByteArray::startsWith
bool startsWith(const QByteArray &ba) const
QDir::homePath
QString homePath()
QFileInfo::isFile
bool isFile() const
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
xdgbasedirs_p.h
QFileInfo::absoluteFilePath
QString absoluteFilePath() const
splitPathList
static QStringList splitPathList(const QString &pathList)
Definition: xdgbasedirs.cpp:30
QString
QStringList
QFileInfo
QFileInfo::exists
bool exists() const
QLatin1Char
QLatin1String
Solid::XdgBaseDirs::findResourceFile
QString findResourceFile(const char *resource, const QString &relPath)
Searches the resource specific directories for a given file.
Definition: xdgbasedirs.cpp:86
Solid::XdgBaseDirs::homePath
QString homePath(const char *resource)
Returns the user specific directory for the given resource type.
Definition: xdgbasedirs.cpp:52
Solid::XdgBaseDirs::systemPathList
QStringList systemPathList(const char *resource)
Returns the list of system wide directories for a given resource type.
Definition: xdgbasedirs.cpp:69
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Solid

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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