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.util.IOUtil;
22 import org.apache.log4j.Logger;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29
30
31
32
33 public class OSXInfoPlist {
34 protected static final Logger LOGGER = Logger.getLogger(OSXInfoPlist.class);
35 protected static final String INFO_PLIST_PATH = "../Info.plist";
36
37 private OSXInfoPlist() {
38 }
39
40 protected static boolean isOSX() {
41 return System.getProperty("os.name").toLowerCase().startsWith("mac os x");
42 }
43
44 protected static String getInfoPlistContent() throws IOException {
45 try (FileInputStream fileInputStream = new FileInputStream(getInfoPlistPath())) {
46 return new String(IOUtil.readFully(fileInputStream), StandardCharsets.UTF_8);
47 }
48 }
49
50
51
52
53
54
55 public static boolean isHideFromDock() {
56 boolean result = false;
57 try {
58 result = isOSX() && getInfoPlistContent().contains("<key>LSUIElement</key><string>1</string>");
59 } catch (IOException e) {
60 LOGGER.warn("Unable to update Info.plist", e);
61 }
62 return result;
63 }
64
65
66
67
68
69
70 public static void setOSXHideFromDock(boolean hideFromDock) {
71 try {
72 if (isOSX()) {
73 boolean currentHideFromDock = isHideFromDock();
74 if (currentHideFromDock != hideFromDock) {
75 String content = getInfoPlistContent();
76 try (FileOutputStream fileOutputStream = new FileOutputStream(getInfoPlistPath())) {
77 fileOutputStream.write(content.replaceFirst(
78 "<key>LSUIElement</key><string>" + (currentHideFromDock ? "1" : "0") + "</string>",
79 "<key>LSUIElement</key><string>" + (hideFromDock ? "1" : "0") + "</string>").getBytes(StandardCharsets.UTF_8));
80 }
81 }
82 }
83 } catch (IOException e) {
84 LOGGER.warn("Unable to update Info.plist", e);
85 }
86 }
87
88 private static String getInfoPlistPath() throws IOException {
89 File file = new File(INFO_PLIST_PATH);
90 if (file.exists()) {
91 return INFO_PLIST_PATH;
92 }
93 throw new IOException("Info.plist file not found");
94 }
95 }