~ubuntu-branches/ubuntu/utopic/kde4libs/utopic

« back to all changes in this revision

Viewing changes to solid/solid/xdgbasedirs.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-05-13 15:42:30 UTC
  • mfrom: (1.14.17)
  • Revision ID: package-import@ubuntu.com-20120513154230-2xv2bsi52w6x5lue
Tags: 4:4.8.3-0ubuntu1
* New upstream release
  - Drop kubuntu_fix_nepomuk_utils_crash.diff, applied upstream
  - Add new symbols to libkdecore5.symbols and libkdewebkit5.symbols
  - Update kdelibs5-dev.install and kdelibs5-experimental-dev.install
  - update symbol files for gcc 4.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}