1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.ui;
20
21 import davmail.ui.tray.DavGatewayTray;
22 import davmail.util.IOUtil;
23 import org.apache.log4j.Logger;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30
31
32
33
34 public class OSXInfoPlist {
35 protected static final Logger LOGGER = Logger.getLogger(OSXInfoPlist.class);
36 protected static final String INFO_PLIST_PATH = "../Info.plist";
37
38 private OSXInfoPlist() {
39 }
40
41 protected static boolean isOSX() {
42 return System.getProperty("os.name").toLowerCase().startsWith("mac os x");
43 }
44
45 protected static String getInfoPlistContent() throws IOException {
46 try (FileInputStream fileInputStream = new FileInputStream(getInfoPlistPath())) {
47 return new String(IOUtil.readFully(fileInputStream), StandardCharsets.UTF_8);
48 }
49 }
50
51
52
53
54
55
56 public static boolean isHideFromDock() {
57 boolean result = false;
58 try {
59 result = isOSX() && getInfoPlistContent().contains("<key>LSUIElement</key><string>1</string>");
60 } catch (IOException e) {
61 LOGGER.warn("Unable to update Info.plist", e);
62 }
63 return result;
64 }
65
66
67
68
69
70
71 public static void setOSXHideFromDock(boolean hideFromDock) {
72 try {
73 if (isOSX()) {
74 boolean currentHideFromDock = isHideFromDock();
75 if (currentHideFromDock != hideFromDock) {
76 String content = getInfoPlistContent();
77 try (FileOutputStream fileOutputStream = new FileOutputStream(getInfoPlistPath())) {
78 fileOutputStream.write(content.replaceFirst(
79 "<key>LSUIElement</key><string>" + (currentHideFromDock ? "1" : "0") + "</string>",
80 "<key>LSUIElement</key><string>" + (hideFromDock ? "1" : "0") + "</string>").getBytes(StandardCharsets.UTF_8));
81 }
82 }
83 }
84 } catch (IOException e) {
85 LOGGER.warn("Unable to update Info.plist", e);
86 }
87 }
88
89 private static String getInfoPlistPath() throws IOException {
90
91 String codeLocation = DavGatewayTray.class.getProtectionDomain().getCodeSource().getLocation().getPath();
92 File file = new File(codeLocation);
93 if (file.exists()) {
94
95 return file.getParent() + File.separator + INFO_PLIST_PATH;
96 }
97 file = new File(INFO_PLIST_PATH);
98 if (file.exists()) {
99
100 return INFO_PLIST_PATH;
101 }
102 throw new IOException("Info.plist file not found");
103 }
104 }