1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // XDG.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
31 // make sure directory names end with a slash
32 static std::string
terminateDir(const std::string
&string
)
34 std::string returnValue
= string
;
35 std::string::const_iterator it
= returnValue
.end() - 1;
41 static std::string
readEnvDir(const char *name
, const char *defaultValue
)
43 const char * const env
= getenv(name
);
44 std::string returnValue
= std::string(env
? env
: defaultValue
);
45 returnValue
= bt::expandTilde(returnValue
);
46 return terminateDir(returnValue
);
49 static std::list
<std::string
> readEnvDirList(const char *name
,
50 const char *defaultValue
)
52 const char * const env
= getenv(name
);
54 std::string str
= env
? env
: defaultValue
;
55 // if the environment variable ends with a ':', append the
57 std::string::const_iterator last
= str
.end() - 1;
61 std::list
<std::string
> returnValue
;
62 const std::string::const_iterator end
= str
.end();
63 std::string::const_iterator begin
= str
.begin();
65 std::string::const_iterator it
= std::find(begin
, end
, ':');
66 std::string dir
= std::string(begin
, it
);
67 dir
= bt::expandTilde(dir
);
68 dir
= terminateDir(dir
);
69 returnValue
.push_back(dir
);
73 } while (begin
!= end
);
79 std::string
bt::XDG::BaseDir::dataHome()
81 static std::string XDG_DATA_HOME
=
82 readEnvDir("XDG_DATA_HOME", "~/.local/share/");
86 std::string
bt::XDG::BaseDir::configHome()
88 static std::string XDG_CONFIG_HOME
=
89 readEnvDir("XDG_CONFIG_HOME", "~/.config/");
90 return XDG_CONFIG_HOME
;
93 std::list
<std::string
> bt::XDG::BaseDir::dataDirs()
95 static std::list
<std::string
> XDG_DATA_DIRS
=
96 readEnvDirList("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/");
100 std::list
<std::string
> bt::XDG::BaseDir::configDirs()
102 static std::list
<std::string
> XDG_CONFIG_DIRS
=
103 readEnvDirList("XDG_CONFIG_DIRS", "/etc/xdg/");
104 return XDG_CONFIG_DIRS
;
107 std::string
bt::XDG::BaseDir::cacheHome()
109 static std::string XDG_CACHE_HOME
=
110 readEnvDir("XDG_CACHE_HOME", "~/.cache/");
111 return XDG_CACHE_HOME
;
114 std::string
bt::XDG::BaseDir::writeDataFile(const std::string
&filename
)
116 std::string path
= dataHome() + filename
;
117 std::string directoryName
= dirname(path
);
118 if (!mkdirhier(directoryName
, 0700))
119 return std::string();
123 std::string
bt::XDG::BaseDir::writeConfigFile(const std::string
&filename
)
125 std::string path
= configHome() + filename
;
126 std::string directoryName
= dirname(path
);
127 if (!mkdirhier(directoryName
, 0700))
128 return std::string();
132 std::string
bt::XDG::BaseDir::writeCacheFile(const std::string
&filename
)
134 std::string path
= cacheHome() + filename
;
135 std::string directoryName
= dirname(path
);
136 if (!mkdirhier(directoryName
, 0700))
137 return std::string();