Solid
xdgbasedirs.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2007 by Kevin Krammer <kevin.krammer@gmx.at> * 00003 * * 00004 * This program is free software; you can redistribute it and/or modify * 00005 * it under the terms of the GNU Library General Public License as * 00006 * published by the Free Software Foundation; either version 2 of the * 00007 * License, or (at your option) any later version. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU Library General Public * 00015 * License along with this program; if not, write to the * 00016 * Free Software Foundation, Inc., * 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 00018 ***************************************************************************/ 00019 00020 /* 00021 * This file was copied to solid from [akonadi]/libs/xdgbasedirs.cpp and slimmed down. 00022 * See xdgbasedirs_p.h for notes about slimming down. 00023 */ 00024 00025 #include "xdgbasedirs_p.h" 00026 00027 #include <QtCore/QDir> 00028 #include <QtCore/QFileInfo> 00029 00030 static QStringList splitPathList( const QString &pathList ) 00031 { 00032 return pathList.split( QLatin1Char( ':' ) ); 00033 } 00034 00035 class XdgBaseDirsSingleton 00036 { 00037 public: 00038 QString homePath( const char *variable, const char *defaultSubDir ); 00039 00040 QStringList systemPathList( const char *variable, const char *defaultDirList ); 00041 00042 public: 00043 QString mConfigHome; 00044 QString mDataHome; 00045 00046 QStringList mConfigDirs; 00047 QStringList mDataDirs; 00048 }; 00049 00050 Q_GLOBAL_STATIC( XdgBaseDirsSingleton, instance ) 00051 00052 QString Solid::XdgBaseDirs::homePath( const char *resource ) 00053 { 00054 if ( qstrncmp( "data", resource, 4 ) == 0 ) { 00055 if ( instance()->mDataHome.isEmpty() ) { 00056 instance()->mDataHome = instance()->homePath( "XDG_DATA_HOME", ".local/share" ); 00057 } 00058 return instance()->mDataHome; 00059 } else if ( qstrncmp( "config", resource, 6 ) == 0 ) { 00060 if ( instance()->mConfigHome.isEmpty() ) { 00061 instance()->mConfigHome = instance()->homePath( "XDG_CONFIG_HOME", ".config" ); 00062 } 00063 return instance()->mConfigHome; 00064 } 00065 00066 return QString(); 00067 } 00068 00069 QStringList Solid::XdgBaseDirs::systemPathList( const char *resource ) 00070 { 00071 if ( qstrncmp( "data", resource, 4 ) == 0 ) { 00072 if ( instance()->mDataDirs.isEmpty() ) { 00073 instance()->mDataDirs = instance()->systemPathList( "XDG_DATA_DIRS", "/usr/local/share:/usr/share" ); 00074 } 00075 return instance()->mDataDirs; 00076 } else if ( qstrncmp( "config", resource, 6 ) == 0 ) { 00077 if ( instance()->mConfigDirs.isEmpty() ) { 00078 instance()->mConfigDirs = instance()->systemPathList( "XDG_CONFIG_DIRS", "/etc/xdg" ); 00079 } 00080 return instance()->mConfigDirs; 00081 } 00082 00083 return QStringList(); 00084 } 00085 00086 QString Solid::XdgBaseDirs::findResourceFile( const char *resource, const QString &relPath ) 00087 { 00088 const QString fullPath = homePath( resource ) + QLatin1Char( '/' ) + relPath; 00089 00090 QFileInfo fileInfo( fullPath ); 00091 if ( fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() ) { 00092 return fullPath; 00093 } 00094 00095 const QStringList pathList = systemPathList( resource ); 00096 00097 foreach ( const QString &path, pathList ) { 00098 fileInfo = QFileInfo( path + QLatin1Char('/' ) + relPath ); 00099 if ( fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() ) { 00100 return fileInfo.absoluteFilePath(); 00101 } 00102 } 00103 00104 return QString(); 00105 } 00106 00107 QString XdgBaseDirsSingleton::homePath( const char *variable, const char *defaultSubDir ) 00108 { 00109 const QByteArray env = qgetenv( variable ); 00110 00111 QString xdgPath; 00112 if ( env.isEmpty() ) { 00113 xdgPath = QDir::homePath() + QLatin1Char( '/' ) + QLatin1String( defaultSubDir ); 00114 } else if ( env.startsWith( '/' ) ) { 00115 xdgPath = QString::fromLocal8Bit( env ); 00116 } else { 00117 xdgPath = QDir::homePath() + QLatin1Char( '/' ) + QString::fromLocal8Bit( env ); 00118 } 00119 00120 return xdgPath; 00121 } 00122 00123 QStringList XdgBaseDirsSingleton::systemPathList( const char *variable, const char *defaultDirList ) 00124 { 00125 const QByteArray env = qgetenv( variable ); 00126 00127 QString xdgDirList; 00128 if ( env.isEmpty() ) { 00129 xdgDirList = QLatin1String( defaultDirList ); 00130 } else { 00131 xdgDirList = QString::fromLocal8Bit( env ); 00132 } 00133 00134 return splitPathList( xdgDirList ); 00135 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:52:05 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:52:05 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.