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.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   * Handle OSX Info.plist file access
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       * Test current LSUIElement (hide from dock) value
52       *
53       * @return true if application is hidden from dock
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       * Update LSUIElement (hide from dock) value
67       *
68       * @param hideFromDock new hide from dock value
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  }