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