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

Solid

  • 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
30static QStringList splitPathList( const QString &pathList )
31{
32 return pathList.split( QLatin1Char( ':' ) );
33}
34
35class 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
50Q_GLOBAL_STATIC( XdgBaseDirsSingleton, instance )
51
52QString 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
69QStringList 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
86QString 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
107QString 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
123QStringList 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}
Solid::XdgBaseDirs::homePath
QString homePath(const char *resource)
Returns the user specific directory for the given resource type.
Definition: xdgbasedirs.cpp:52
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::systemPathList
QStringList systemPathList(const char *resource)
Returns the list of system wide directories for a given resource type.
Definition: xdgbasedirs.cpp:69
Solid
Definition: acadapter.h:29
splitPathList
static QStringList splitPathList(const QString &pathList)
Definition: xdgbasedirs.cpp:30
xdgbasedirs_p.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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