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  
29  /**
30   * Handle OSX Info.plist file access
31   */
32  public class OSXInfoPlist {
33      protected static final Logger LOGGER = Logger.getLogger(OSXInfoPlist.class);
34      protected static final String INFO_PLIST_PATH = "Contents/Info.plist";
35  
36      private OSXInfoPlist() {
37      }
38  
39      protected static boolean isOSX() {
40          return System.getProperty("os.name").toLowerCase().startsWith("mac os x");
41      }
42  
43      protected static String getInfoPlistContent() throws IOException {
44          FileInputStream fileInputStream = null;
45          try {
46              fileInputStream = new FileInputStream(getInfoPlistPath());
47              return new String(IOUtil.readFully(fileInputStream), "UTF-8");
48          } finally {
49              if (fileInputStream != null) {
50                  try {
51                      fileInputStream.close();
52                  } catch (IOException e) {
53                      // ignore
54                  }
55              }
56          }
57      }
58  
59      /**
60       * Test current LSUIElement (hide from dock) value
61       *
62       * @return true if application is hidden from dock
63       */
64      public static boolean isHideFromDock() {
65          boolean result = false;
66          try {
67              result = isOSX() && getInfoPlistContent().contains("<key>LSUIElement</key><string>1</string>");
68          } catch (IOException e) {
69              LOGGER.warn("Unable to update Info.plist", e);
70          }
71          return result;
72      }
73  
74      /**
75       * Update LSUIElement (hide from dock) value
76       *
77       * @param hideFromDock new hide from dock value
78       */
79      public static void setOSXHideFromDock(boolean hideFromDock) {
80          try {
81              if (isOSX()) {
82                  boolean currentHideFromDock = isHideFromDock();
83                  if (currentHideFromDock != hideFromDock) {
84                      String content = getInfoPlistContent();
85                      FileOutputStream fileOutputStream = new FileOutputStream(getInfoPlistPath());
86                      try {
87                          fileOutputStream.write(content.replaceFirst(
88                                  "<key>LSUIElement</key><string>" + (currentHideFromDock ? "1" : "0") + "</string>",
89                                  "<key>LSUIElement</key><string>" + (hideFromDock ? "1" : "0") + "</string>").getBytes("UTF-8"));
90                      } finally {
91                          fileOutputStream.close();
92                      }
93                      fileOutputStream.close();
94                  }
95              }
96          } catch (IOException e) {
97              LOGGER.warn("Unable to update Info.plist", e);
98          }
99      }
100 
101     private static String getInfoPlistPath() throws IOException {
102         File file = new File(INFO_PLIST_PATH);
103         if (file.exists()) {
104             return INFO_PLIST_PATH;
105         }
106         // failover for Java7
107         String libraryPath = System.getProperty("java.library.path");
108         if (libraryPath != null && libraryPath.endsWith("Contents/MacOS")) {
109             file = new File(libraryPath.replace("Contents/MacOS", INFO_PLIST_PATH));
110             if (file.exists()) {
111                 return INFO_PLIST_PATH;
112             }
113         }
114         throw new IOException("Info.plist file not found");
115     }
116 }