1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2007
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
21   */
22  package org.crosswire.jsword.util;
23  
24  import java.io.IOException;
25  import java.net.URI;
26  
27  import org.crosswire.common.util.CWProject;
28  import org.crosswire.common.util.FileUtil;
29  import org.crosswire.common.util.Logger;
30  import org.crosswire.common.util.NetUtil;
31  import org.crosswire.common.util.PropertyMap;
32  import org.crosswire.common.util.ResourceUtil;
33  import org.crosswire.jsword.JSMsg;
34  
35  /**
36   * Provide a configurable warning that the Internet is going to be accessed.
37   * This is important in places where Internet activity may be monitored and
38   * Christians may be persecuted.
39   * 
40   * @see gnu.lgpl.License for license details.<br>
41   *      The copyright to this program is held by it's authors.
42   * @author DM Smith [dmsmith555 at yahoo dot com]
43   */
44  public class WebWarning {
45      /**
46       * This is a utility class, thus it's constructor is private.
47       */
48      private WebWarning() {
49          try {
50              PropertyMap props = ResourceUtil.getProperties(getClass().getName());
51              shown = Boolean.valueOf(props.get(SHOWN_KEY, Boolean.valueOf(DEFAULT_SHOWN).toString())).booleanValue();
52          } catch (IOException e) {
53              shown = DEFAULT_SHOWN;
54          }
55      }
56  
57      /**
58       * All access to WebWarning is through this single instance.
59       * 
60       * @return the singleton instance
61       */
62      public static WebWarning instance() {
63          return instance;
64      }
65  
66      /**
67       * @param newShown
68       *            Whether this WebWarning should be shown.
69       */
70      public void setShown(boolean newShown) {
71          try {
72              shown = newShown;
73              PropertyMap props = new PropertyMap();
74              props.put(SHOWN_KEY, Boolean.valueOf(shown).toString());
75              URI outputURI = CWProject.instance().getWritableURI(getClass().getName(), FileUtil.EXTENSION_PROPERTIES);
76              NetUtil.storeProperties(props, outputURI, "JSword WebWarning");
77          } catch (IOException ex) {
78              log.error("Failed to save JSword WebWarning", ex);
79          }
80      }
81  
82      /**
83       * @return Whether this WebWarning should be shown.
84       */
85      public boolean isShown() {
86          return shown;
87      }
88  
89      /**
90       * From configuration set the state.
91       * 
92       * @param newShown
93       *            Whether this WebWarning should be shown.
94       */
95      public static void setWarningShown(boolean newShown) {
96          WebWarning.instance().setShown(newShown);
97      }
98  
99      /**
100      * @return Whether this WebWarning should be shown.
101      */
102     public static boolean isWarningShown() {
103         return WebWarning.instance().isShown();
104     }
105 
106     /**
107      * @return a warning that the Internet is about to be accessed
108      */
109     public String getWarning() {
110         // TRANSLATOR: Warn the user that the program is about to access the Internet.
111         // In some countries, this warning may be too bland. It might be better to warn the user that this might
112         // put them at risk of persecution.
113         return JSMsg.gettext("You are about to access the Internet. Are you sure you want to do this?");
114     }
115 
116     /**
117      * @return indicate that the warning will be shown again
118      */
119     public String getShownWarningLabel() {
120         // TRANSLATOR: This labels a checkbox, which is checked by default.
121         // Unchecking it allows the user to not see the message again but the Internet will be accessed.
122         return JSMsg.gettext("Show this warning every time the Internet is accessed.");
123     }
124 
125     private static WebWarning instance = new WebWarning();
126 
127     private static final String SHOWN_KEY = "shown";
128     private static final boolean DEFAULT_SHOWN = true;
129     private boolean shown;
130 
131     /**
132      * The log stream
133      */
134     private static final Logger log = Logger.getLogger(WebWarning.class);
135 }
136