a4323ce85c50401bd3aca6168c198bc018d70b98
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / FileAssociationManager.java
1 package eu.svjatoslav.instantlauncher;
2
3 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.HashMap;
8
9 public class FileAssociationManager {
10
11     private static final String FILE_INDICATOR = "<FILE>";
12
13     private static final String[] TEXT_EDITOR_PATTERN = new String[]{"emc", FILE_INDICATOR};
14
15     private static final String DOCUMENT_EDITOR = "lowriter";
16
17     private static final String SPREADSHEET_EDITOR = "localc";
18
19     private static final String MUSIC_PLAYER = "clementine";
20
21     private static final HashMap<String, String[]> extensionToApplication = new HashMap<>();
22
23     public FileAssociationManager() {
24
25         extensionToApplication.put("txt", TEXT_EDITOR_PATTERN);
26         extensionToApplication.put("xml", TEXT_EDITOR_PATTERN);
27         extensionToApplication.put("org", TEXT_EDITOR_PATTERN);
28         extensionToApplication.put("properties", TEXT_EDITOR_PATTERN);
29         extensionToApplication.put("ini", TEXT_EDITOR_PATTERN);
30         extensionToApplication.put("", TEXT_EDITOR_PATTERN);
31
32         extensionToApplication.put("jpg", new String[]{"eog", FILE_INDICATOR});
33         extensionToApplication.put("jpeg", new String[]{"eog", FILE_INDICATOR});
34         extensionToApplication.put("png", new String[]{"eog", FILE_INDICATOR});
35
36         extensionToApplication.put("pdf", new String[]{"evince", FILE_INDICATOR});
37
38         extensionToApplication.put("mp3", new String[]{"banshee", FILE_INDICATOR});
39         extensionToApplication.put("ogg", new String[]{"banshee", FILE_INDICATOR});
40
41         extensionToApplication.put("avi", new String[]{"vlc", FILE_INDICATOR});
42         extensionToApplication.put("mkv", new String[]{"vlc", FILE_INDICATOR});
43         extensionToApplication.put("ogv", new String[]{"vlc", FILE_INDICATOR});
44         extensionToApplication.put("mp4", new String[]{"vlc", FILE_INDICATOR});
45         extensionToApplication.put("flv", new String[]{"vlc", FILE_INDICATOR});
46         extensionToApplication.put("wmv", new String[]{"vlc", FILE_INDICATOR});
47
48         extensionToApplication.put("flac", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
49         extensionToApplication.put("mp3", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
50         extensionToApplication.put("ogg", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
51         extensionToApplication.put("wav", new String[]{MUSIC_PLAYER, FILE_INDICATOR});
52
53         extensionToApplication.put("ods", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
54         extensionToApplication.put("xls", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
55         extensionToApplication.put("xlsx", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR});
56
57         extensionToApplication.put("docx", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR});
58         extensionToApplication.put("doc", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR});
59
60         extensionToApplication.put("desktop", new String[]{"run-desktop", FILE_INDICATOR});
61
62         extensionToApplication.put("log", new String[]{"glogg", FILE_INDICATOR});
63         extensionToApplication.put("out", new String[]{"glogg", FILE_INDICATOR});
64
65         extensionToApplication.put("html", new String[]{"firefox", FILE_INDICATOR});
66
67         extensionToApplication.put("htm", new String[]{"firefox", FILE_INDICATOR});
68
69     }
70
71     private static boolean isLogFile(final String fileName) {
72         if (fileName.endsWith(".out"))
73             return true;
74         if (fileName.endsWith(".log"))
75             return true;
76         return fileName.contains(".log.");
77
78     }
79
80     /**
81      * @return <code>true</code> if file was opened. <code>false</code> if
82      * unknown file type.
83      */
84     public static boolean openFile(final File file) {
85
86         if (isLogFile(file.getName())) {
87             runOpeningApplication(file, new String[]{"glogg", FILE_INDICATOR});
88             return true;
89         }
90
91         final String fileExtension = Utils.getFileExtension(file);
92
93         if (extensionToApplication.containsKey(fileExtension)) {
94             final String[] commands = extensionToApplication.get(fileExtension);
95             runOpeningApplication(file, commands);
96             return true;
97         } else
98             return false;
99
100     }
101
102     private static void runOpeningApplication(final File file, final String[] commands) {
103         for (int i = 0; i < commands.length; i++)
104             if (commands[i].equals(FILE_INDICATOR))
105                 commands[i] = file.getAbsolutePath();
106
107         try {
108             Runtime.getRuntime().exec(commands);
109             InstantLauncher.exitProgram();
110
111         } catch (final IOException e) {
112             new ExceptionDialog(e);
113         }
114     }
115
116 }