forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenFile.java
More file actions
executable file
·145 lines (137 loc) · 4.19 KB
/
Copy pathOpenFile.java
File metadata and controls
executable file
·145 lines (137 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package javaforce.utils;
/**
* Created : Apr 16, 2012
*
* @author pquiring
*/
import java.io.*;
import javaforce.*;
import javaforce.linux.*;
public class OpenFile {
public static void main(String[] args) {
//opens arg[0]
if ((args.length < 1) || (args[0].length() == 0)) {
System.out.println("usage : jfopen file\ndesc : open program associated with file type");
return;
}
String action = "open";
String file;
if (args[0].equals("--edit")) {
action = "edit";
file = args[1];
} else {
file = args[0];
}
try {
openFile(file, action);
} catch (Exception e) {
System.out.println("" + e);
}
}
public static void openFile(String file, String action) throws Exception {
int idx = file.lastIndexOf(".");
if (idx == -1) {
throw new Exception("file has no extension");
}
String ext = file.substring(idx + 1);
//open /etc/mime.types to convert extension to mime type
String mime = getMimeType(ext);
if (mime == null) {
throw new Exception("mime-type not found");
}
//open /usr/share/applications/*.desktop
String desktop = getHandler(mime, action);
if (desktop == null) {
if (!action.equals("open")) {
//try with just "open"
desktop = getHandler(mime, "open"); //TODO : don't re-read files
}
if (desktop == null) {
throw new Exception("handler not found");
}
}
Linux.executeDesktop(desktop, new String[] {file});
}
private static String mimetypes;
public static String getMimeType(String ext) throws Exception {
if (mimetypes == null) {
FileInputStream fis = new FileInputStream("/etc/mime.types");
byte[] data = JF.readAll(fis);
fis.close();
mimetypes = new String(data);
}
String[] lns = mimetypes.split("\n");
for (int a = 0; a < lns.length; a++) {
String[] f = lns[a].split("\t+"); //greedy tabs
if (f.length < 2) {
continue;
}
String[] exts = f[1].split(" ");
for (int b = 0; b < exts.length; b++) {
if (exts[b].equalsIgnoreCase(ext)) {
return f[0];
}
}
}
return null;
}
public static String getHandler(String mime, String action) throws Exception {
String desktop = getDesktop(mime, "/usr/share/applications", action);
if (desktop != null) return desktop;
return getDesktop(mime, JF.getUserPath() + "/.local/share/applications", action);
}
public static String getIcon(String mime, String action) throws Exception {
String desktop = getDesktop(mime, "/usr/share/applications", action);
if (desktop == null) {
desktop = getDesktop(mime, JF.getUserPath() + "/.local/share/applications", action);
if (desktop == null) {
return null;
}
}
FileInputStream fis = new FileInputStream(desktop);
byte[] data = JF.readAll(fis);
fis.close();
String str = new String(data);
String[] lns = str.split("\n");
for (int a = 0; a < lns.length; a++) {
if (lns[a].startsWith("Icon=")) {
return lns[a].substring(5).trim();
}
}
return null;
}
public static String getDesktop(String mime, String folder, String action) throws Exception {
File file = new File(folder + "/mimeinfo.cache");
if (!file.exists()) {
return null;
}
FileInputStream fis = new FileInputStream(file);
byte[] data = JF.readAll(fis);
fis.close();
String str = new String(data);
String[] lns = str.split("\n");
String cacheAction = "open"; //assume open in cause cache is not generated from JF
for (int a = 0; a < lns.length; a++) {
String ln = lns[a].trim();
if (ln.startsWith("[") || ln.endsWith("]")) {
cacheAction = ln.substring(1, ln.length() - 1);
continue;
}
if (!cacheAction.equals(action)) {
continue;
}
int idx = lns[a].indexOf("=");
if (idx == -1) {
continue;
}
if (lns[a].substring(0, idx).equals(mime)) {
String[] desktops = lns[a].substring(idx + 1).trim().split(";");
if ((desktops == null) || (desktops.length == 0)) {
continue;
}
return folder + "/" + desktops[0];
}
}
return null;
}
}