View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2012  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * Handle OSX Info.plist file access
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       * Test current LSUIElement (hide from dock) value
53       *
54       * @return true if application is hidden from dock
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       * Update LSUIElement (hide from dock) value
68       *
69       * @param hideFromDock new hide from dock value
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          // code location points to davmail.jar
91          String codeLocation = DavGatewayTray.class.getProtectionDomain().getCodeSource().getLocation().getPath();
92          File file = new File(codeLocation);
93          if (file.exists()) {
94              // code location is absolute path
95              return file.getParent() + File.separator + INFO_PLIST_PATH;
96          }
97          file = new File(INFO_PLIST_PATH);
98          if (file.exists()) {
99              // fallback to relative path
100             return INFO_PLIST_PATH;
101         }
102         throw new IOException("Info.plist file not found");
103     }
104 }