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