001/*
002 * Copyright 2013 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.moonshine.directories;
017
018import java.nio.file.Path;
019import java.nio.file.Paths;
020import java.util.ArrayList;
021import java.util.List;
022
023/**
024 * Specifies directory layout based on XDG Base Directory Specification.
025 * @see <a href="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html">XDG Base Directory Specification</a>
026 */
027public class XdgDirectoryLayout implements WriteableDirectoryLayout {
028    private final Path homeDirectory;
029    private final Path configHome;
030    private final Path dataHome;
031    private final Path cacheHome;
032    private final List<Path> configDirs = new ArrayList<>();
033    private final List<Path> dataDirs = new ArrayList<>();
034
035    public XdgDirectoryLayout(String applicationName) {
036        homeDirectory = Paths.get(System.getProperty("user.home"));
037
038        String xdgConfigHome = System.getenv("XDG_CONFIG_HOME");
039        if (xdgConfigHome != null) {
040            configHome = Paths.get(xdgConfigHome, applicationName);
041        } else {
042            configHome = homeDirectory.resolve(".config").resolve(applicationName);
043        }
044
045        String xdgDataHome = System.getenv("XDG_DATA_HOME");
046        if (xdgDataHome != null) {
047            dataHome = Paths.get(xdgDataHome, applicationName);
048        } else {
049            dataHome = homeDirectory.resolve(".local/share").resolve(applicationName);
050        }
051
052        String xdgCacheHome = System.getenv("XDG_CACHE_HOME");
053        if (xdgCacheHome != null) {
054            cacheHome = Paths.get(xdgCacheHome, applicationName);
055        } else {
056            cacheHome = homeDirectory.resolve(".cache").resolve(applicationName);
057        }
058
059        String xdgConfigDirs = System.getenv("XDG_CONFIG_DIRS");
060        if (xdgConfigDirs == null) {
061            xdgConfigDirs = "/etc/xdg";
062        }
063        for (String dir : xdgConfigDirs.split(":")) {
064            configDirs.add(Paths.get(dir));
065        }
066        String xdgDataDirs = System.getenv("XDG_CONFIG_DIRS");
067        if (xdgDataDirs == null) {
068            xdgDataDirs = "/usr/local/share/:/usr/share/";
069        }
070        for (String dir : xdgDataDirs.split(":")) {
071            dataDirs.add(Paths.get(dir));
072        }
073    }
074
075    @Override
076    public Path getConfigHome() {
077        return configHome;
078    }
079
080    @Override
081    public Path getDataHome() {
082        return dataHome;
083    }
084
085    @Override
086    public Path getCacheHome() {
087        return cacheHome;
088    }
089
090    @Override
091    public Path getLogHome() {
092        return dataHome.resolve("logs");
093    }
094
095    @Override
096    public Iterable<Path> getConfigDirs() {
097        return configDirs;
098    }
099
100    @Override
101    public Iterable<Path> getDataDirs() {
102        return dataDirs;
103    }
104}