Updated copyright
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / file / CommonPathResolver.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.file;
11
12 import java.io.File;
13
14 public class CommonPathResolver {
15
16     private static final String[] desktopNames = new String[]{"Tafelblad", "المكتب",
17             "Masa Üstü", "Сталец", "Десктоп", "ডেস্কটপ", "Desktop",
18             "Escriptori", "Prostředí pracovní plochy", "Penbwrdd",
19             "Skrivebord", "Επιφάνεια εργασίας", "Escritorio", "Töölaud",
20             "Mahaigaina", "Työpöytä", "Bureau", "Deasc", "ડૅસ્કટોપ",
21             "डेस्कटॉप", "Radna površina", "Munkaasztal", "デスクトップ", "დესკტოპი",
22             "데스크탑", "Darbastalis", "Дэлгэц", "Skrivebord", "Teseke", "ਵਿਹੜਾ",
23             "Pulpit", "Ambiente de Trabalho", "Área de Trabalho",
24             "Рабочий стол", "Pracovná plocha", "Hapësira e Punës",
25             "Радна површ", "Radna površ", "Skrivbord", "கணிமேசை", "Мизи корӣ",
26             "พื้นโต๊ะ", "Masaüstü", "Стільниця", "桌面", "Isiga-nyezi"};
27
28     private static File desktopFile;
29
30     /**
31      * This method tries to guess user desktop directory. Implementation is
32      * pretty lousy. Need to improve it some day.
33      *
34      * @return file that points to user desktop directory.
35      * @throws RuntimeException if user desktop directory is not found.
36      */
37     public static File getDesktopDirectory() {
38
39         if (desktopFile != null)
40             return desktopFile;
41
42         final String userHomePath = System.getProperty("user.home");
43
44         for (final String desktopName : desktopNames) {
45
46             final String desktopPath = userHomePath + "/" + desktopName;
47
48             final File possibleDesktopFile = new File(desktopPath);
49             if (possibleDesktopFile.exists()) {
50                 desktopFile = possibleDesktopFile;
51                 return desktopFile;
52             }
53         }
54
55         throw new RuntimeException("Cannot autodetect user desktop directory.");
56     }
57
58     public static File getHomeDirectory() {
59         return new File(System.getProperty("user.home"));
60     }
61
62 }