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.
33 // make sure directory names end with a slash
34 static std::string
terminateDir(const std::string
&string
)
36 std::string returnValue
= string
;
37 std::string::const_iterator it
= returnValue
.end() - 1;
43 static std::string
readEnvDir(const char *name
, const char *defaultValue
)
45 const char * const env
= getenv(name
);
46 std::string returnValue
= std::string(env
? env
: defaultValue
);
47 returnValue
= bt::expandTilde(returnValue
);
48 return terminateDir(returnValue
);
51 static std::list
<std::string
> readEnvDirList(const char *name
,
52 const char *defaultValue
)
54 const char * const env
= getenv(name
);
56 std::string str
= env
? env
: defaultValue
;
57 // if the environment variable ends with a ':', append the
59 std::string::const_iterator last
= str
.end() - 1;
63 std::list
<std::string
> returnValue
;
64 const std::string::const_iterator end
= str
.end();
65 std::string::const_iterator begin
= str
.begin();
67 std::string::const_iterator it
= std::find(begin
, end
, ':');
68 std::string dir
= std::string(begin
, it
);
69 dir
= bt::expandTilde(dir
);
70 dir
= terminateDir(dir
);
71 returnValue
.push_back(dir
);
75 } while (begin
!= end
);
81 std::string
bt::XDG::BaseDir::dataHome()
83 static std::string XDG_DATA_HOME
=
84 readEnvDir("XDG_DATA_HOME", "~/.local/share/");
88 std::string
bt::XDG::BaseDir::configHome()
90 static std::string XDG_CONFIG_HOME
=
91 readEnvDir("XDG_CONFIG_HOME", "~/.config/");
92 return XDG_CONFIG_HOME
;
95 std::list
<std::string
> bt::XDG::BaseDir::dataDirs()
97 static std::list
<std::string
> XDG_DATA_DIRS
=
98 readEnvDirList("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/");
102 std::list
<std::string
> bt::XDG::BaseDir::configDirs()
104 static std::list
<std::string
> XDG_CONFIG_DIRS
=
105 readEnvDirList("XDG_CONFIG_DIRS", "/etc/xdg/");
106 return XDG_CONFIG_DIRS
;
109 std::string
bt::XDG::BaseDir::cacheHome()
111 static std::string XDG_CACHE_HOME
=
112 readEnvDir("XDG_CACHE_HOME", "~/.cache/");
113 return XDG_CACHE_HOME
;
116 std::string
bt::XDG::BaseDir::writeDataFile(const std::string
&filename
)
118 std::string path
= dataHome() + filename
;
119 std::string directoryName
= dirname(path
);
120 if (!mkdirhier(directoryName
, 0700))
121 return std::string();
125 std::string
bt::XDG::BaseDir::writeConfigFile(const std::string
&filename
)
127 std::string path
= configHome() + filename
;
128 std::string directoryName
= dirname(path
);
129 if (!mkdirhier(directoryName
, 0700))
130 return std::string();
134 std::string
bt::XDG::BaseDir::writeCacheFile(const std::string
&filename
)
136 std::string path
= cacheHome() + filename
;
137 std::string directoryName
= dirname(path
);
138 if (!mkdirhier(directoryName
, 0700))
139 return std::string();