/* * This file is part of DeskBin. * * Copyright (c) 2012, alta189 <http://github.com/alta189/DeskBin/> * DeskBin is licensed under the GNU Lesser General Public License. * * DeskBin is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * DeskBin is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.alta189.deskbin.util; import java.io.File; public class PlatformUtils { private static final File workingDirectory = getWorkingDirectory("deskbin"); public static File getWorkingDirectory() { return workingDirectory; } public static File getWorkingDirectory(String applicationName) { String userHome = System.getProperty("user.home", "."); File workingDirectory; switch (getOperatingSystem()) { case LINUX: case SOLARIS: workingDirectory = new File(userHome, '.' + applicationName + '/'); break; case WINDOWS: String applicationData = System.getenv("APPDATA"); if (applicationData != null) { workingDirectory = new File(applicationData, applicationName + '/'); } else { workingDirectory = new File(userHome, '.' + applicationName + '/'); } break; case MAC_OS: workingDirectory = new File(userHome, "Library/Application Support/" + applicationName); break; default: workingDirectory = new File(userHome, applicationName + '/'); } if ((!workingDirectory.exists()) && (!workingDirectory.mkdirs())) throw new RuntimeException("The working directory could not be created: " + workingDirectory); return workingDirectory; } public static OS getOperatingSystem() { String osName = System.getProperty("os.name").toLowerCase(); if (osName.contains("win")) return OS.WINDOWS; if (osName.contains("mac")) return OS.MAC_OS; if (osName.contains("solaris")) return OS.SOLARIS; if (osName.contains("sunos")) return OS.SOLARIS; if (osName.contains("linux")) return OS.LINUX; if (osName.contains("unix")) return OS.LINUX; return OS.UNKNOWN; } public enum OS { LINUX, SOLARIS, WINDOWS, MAC_OS, UNKNOWN; } }