source: trunk/src/org/expeditee/settings/UserSettings.java

Last change on this file was 1539, checked in by bnemhaus, 4 years ago

Moved the HomeFrame settings to be prior to the ProfileName and UserName setting because the HomeFrame setting CAN be dependent on having a profile name already set.

Added code to deal with legacy framesets that will still have HomeFrame setting appearing before ProfileName and UserName.

File size: 13.2 KB
Line 
1/**
2 * UserSettings.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (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, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.settings;
20
21import java.io.File;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.nio.file.Paths;
25import java.util.ArrayList;
26import java.util.LinkedList;
27import java.util.List;
28
29import org.expeditee.agents.SearchGreenstone;
30import org.expeditee.agents.mail.MailSession;
31import org.expeditee.agents.wordprocessing.JSpellChecker;
32import org.expeditee.auth.AuthenticatorBrowser;
33import org.expeditee.gui.Browser;
34import org.expeditee.gui.Frame;
35import org.expeditee.gui.FrameIO;
36import org.expeditee.gui.FrameUtils;
37import org.expeditee.gui.MessageBay;
38import org.expeditee.gui.management.ProfileManager;
39import org.expeditee.gui.management.ResourceUtil;
40import org.expeditee.items.Text;
41import org.expeditee.setting.BooleanSetting;
42import org.expeditee.setting.FloatSetting;
43import org.expeditee.setting.FrameSetting;
44import org.expeditee.setting.FunctionSetting;
45import org.expeditee.setting.IntegerSetting;
46import org.expeditee.setting.ListSetting;
47import org.expeditee.setting.StringSetting;
48import org.expeditee.settings.folders.FolderSettings;
49
50/**
51 * Central class to contain all values that can be set by the user on their
52 * profile frame. These values should be updated only by
53 * FrameUtils.ParseProfile.
54 */
55public abstract class UserSettings {
56
57 public static final String DEFAULT_PROFILE_NAME = "default";
58
59 public static Boolean PublicAndPrivateResources;
60
61 public static final IntegerSetting Gravity = new IntegerSetting("Distance the cursor has to be from a text item to select the text item", "Gravity", 3);
62
63 public static final StringSetting UserName = new StringSetting("User name", ProfileManager.USER_NAME_FLAG);
64
65 public static final StringSetting ProfileName = new StringSetting("Profile name", ProfileManager.PROFILE_NAME_FLAG);
66
67 public static final StringSetting StartFrame = new StringSetting("The frame to go to when Expeditee is started (defaults to the profile frame)", null);
68
69 /*
70 * Stuff that goes first
71 */
72 public static final StringSetting HomeFrame = new StringSetting("The home frame", null) {
73 @Override
74 public boolean setSetting(Text text) {
75 String profileName = UserSettings.ProfileName.get();
76
77 if (profileName == null) { profileName = Browser.getExpediteeUserProfileName(); }
78 else {
79 // Legacy case for if the order of the settings frame causes HomeFrame to be parsed before the UserName and ProfileName.
80 profileName = ResourceUtil.substitute(profileName, ProfileManager.PROFILE_NAME_FLAG, Browser.getExpediteeUserProfileName());
81 }
82
83 if(text.getText().indexOf(':') == -1 || !text.hasLink()) {
84 //if (!profileName.equals(ProfileManager.USER_NAME_FLAG)) {
85 text.setLink(profileName + "1");
86 //text.setLink(FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName());
87 }
88 String first = FrameUtils.getLink(text, UserSettings.HomeFrame.get());
89 // do not use non-existent frames as the first frame
90 if (FrameIO.isValidFrameName(first)) {
91 _value = first;
92 }
93 // warn the user
94 else {
95 // MessageBay.warningMessage("Home frame: " + first
96 // + " is not a valid frame.");
97 _value = profileName + "1";//FrameIO.LoadProfile(UserSettings.ProfileName.get()).getName();
98 }
99 return true;
100 }
101 };
102 public static final IntegerSetting InitialWidth = new IntegerSetting("Initial width of Expeditee window", "InitialWidth", 1024);
103
104 public static final IntegerSetting InitialHeight = new IntegerSetting("Initial height of Expeditee window", "InitialHeight", 768);
105
106 /*
107 * General settings (no setter functions)
108 */
109
110 public static final FloatSetting ScaleFactor = new FloatSetting("Scale Factor for drawing (TODO: does this even do anything?)", "ScaleFactor", 1F);
111
112 public static final FloatSetting FormatSpacingMin = new FloatSetting("Minimum spacing ratio", "FormatSpacingMin", null);
113
114 public static final FloatSetting FormatSpacingMax = new FloatSetting("Maximum spacing ratio", "FormatSpacingMax", null);
115
116 public static final IntegerSetting LineStraightenThreshold = new IntegerSetting("Threshold for straightening a line (TODO: does this even do anything?)", "LineStraightenThreshold", 15);
117
118 public static final IntegerSetting NoOpThreshold = new IntegerSetting("Distance the cursor may be dragged before Gestures must be reinterpreted. E.g. Copy becomes range.", "NoOpThreshold", 10);
119
120 public static final IntegerSetting TitlePosition = new IntegerSetting("Position of title item in frame (TODO: find whether this is x-offset or y-offset)", "TitlePosition", 150);
121
122 public static final BooleanSetting AntiAlias = new BooleanSetting("Whether anti-aliasing should be enabled", "AntiAlias", false);
123
124 public static final BooleanSetting LineHighlight = new BooleanSetting("Whether lines should be highlighted", "LineHighlight", false);
125
126 public static final BooleanSetting Logging = new BooleanSetting("Whether logging should be enabled", "Logging", false);
127
128 public static final BooleanSetting LogStats = new BooleanSetting("Whether stats should be logged", "LogStats", true);
129
130 public static final BooleanSetting Threading = new BooleanSetting("Whether threading should be enabled", "Threading", true);
131
132 /*
133 * Frames
134 */
135 public static final StringSetting StatisticsFrameset = new StringSetting("The statistics frameset", null);
136
137 public static final StringSetting MenuFrame = new StringSetting("The menu frame", null);
138
139 /*
140 * Other
141 */
142 public static final ListSetting<Text> Style = new ListSetting<Text>("Set the style (TODO: what does this do?)", "Style") {
143 @Override
144 public boolean setSetting(Text text) {
145 Frame child = text.getChild();
146 if (child == null) {
147 _value = new LinkedList<Text>();
148 return true;
149 }
150
151
152 List<Text> style = new ArrayList<Text>(8);
153 for (int i = 0; i < 10; i++) {
154 style.add(null);
155 }
156
157 for (Text t : child.getBodyTextItems(false)) {
158 String type = t.getText();
159 char lastChar = type.charAt(type.length() - 1);
160 if (Character.isDigit(lastChar)) {
161 style.set(lastChar - '0', t);
162 } else {
163 style.set(0, t);
164 }
165 }
166 _value = style;
167 return true;
168 }
169 };
170
171 public static final FunctionSetting SpellChecker = new FunctionSetting("Enables the dictionary with the default dictionary", "SpellChecker") {
172 @Override
173 public void run() {
174 try {
175 JSpellChecker.create();
176 } catch (FileNotFoundException e) {
177 MessageBay.errorMessage("Could not find dictionary: " + e.getMessage());
178 } catch (IOException e) {
179 e.printStackTrace();
180 }
181 }
182 };
183 public static final FrameSetting Spelling = new FrameSetting("Enables the dictionary and adds the items in the child frame to the dictionary", "Spelling") {
184 @Override
185 public void run(Frame frame) {
186 try {
187 JSpellChecker.create(frame);
188 } catch (Exception e) {
189 e.printStackTrace();
190 }
191 }
192 };
193
194 public static final FrameSetting GreenstoneSettings = new FrameSetting("Greenstone settings (TODO: What are these for?)", "GreenstoneSettings") {
195 @Override
196 public void run(Frame frame) {
197 SearchGreenstone.init(frame);
198 }
199 };
200
201 public static final FrameSetting Reminders = new FrameSetting("Reminders (TODO: What are these for?)", "Reminders") {
202 @Override
203 public void run(Frame frame) {
204 org.expeditee.gui.Reminders.init(frame);
205 }
206 };
207
208 public static final FrameSetting MailSettings = new FrameSetting("Mail Settings (TODO: How does this work?)", "MailSettings") {
209 @Override
210 public void run(Frame frame) {
211 MailSession.init(frame);
212 }
213 };
214
215 // add default values
216 static {
217 // Are we in the new regime or the old regime?
218 File resFile = Paths.get(getExpediteeHome()).resolve(".res").toFile();
219 File resourcesPrivateFile = Paths.get(getExpediteeHome() + "resources-private" + File.separator).toFile();
220 if (resourcesPrivateFile.exists()) {
221 // If resources-private exists then this is a good sign we have new regime available.
222 PublicAndPrivateResources = Boolean.TRUE;
223 } else if (resFile.exists()) {
224 // If we do not, but do have the .res file then this is a good sign we are in old regime.
225 PublicAndPrivateResources = Boolean.FALSE;
226 } else {
227 // If we have neither then we are unpacking and therefore will have new regime available once unpacked.
228 PublicAndPrivateResources = Boolean.TRUE;
229 }
230
231 setupDefaultFolders();
232 }
233
234 public static void setupDefaultFolders() {
235 String expeditee_home = getExpediteeHome();
236
237 FrameIO.changeParentAndSubFolders(expeditee_home);
238
239 FolderSettings.FrameDirs.get().clear();
240 FolderSettings.ImageDirs.get().clear();
241 FolderSettings.AudioDirs.get().clear();
242 appendDefaultFolders();
243
244 }
245
246 private static String getExpediteeHome() {
247 String home_property = System.getProperty("expeditee.home");
248 String expeditee_home = (home_property != null) ? home_property + File.separator : getSaveLocation();
249 return expeditee_home;
250 }
251
252 public static void appendDefaultFolders() {
253 if (PublicAndPrivateResources) {
254 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.HELP_USERNAME_PRIVATE_PATH);
255 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.HELP_PATH);
256 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.MESSAGES_PATH);
257 // TODO: This is not the ideal way of adding mail. Mail should only be available when authenticated.
258 // This would ideally be done on authenticated profile creation auth.account.Create.java
259 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.MAIL_PATH);
260 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.FRAME_USERNAME_PRIVATE_PATH);
261 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.FRAME_PATH);
262
263 if (AuthenticatorBrowser.isAuthenticated()) {
264 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.CONTACTS_PATH);
265 //FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.MAIL_PATH);
266 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.SHARED_FRAMESETS_PATH);
267 }
268
269 FolderSettings.ImageDirs.addAbsoluteDir("${CURRENT_FRAMESET}");
270 FolderSettings.ImageDirs.addAbsoluteDir(FrameIO.IMAGES_USERNAME_PRIVATE_PATH);
271 FolderSettings.ImageDirs.addAbsoluteDir(FrameIO.IMAGES_PATH);
272
273 FolderSettings.AudioDirs.addAbsoluteDir("${CURRENT_FRAMESET}");
274 FolderSettings.AudioDirs.addAbsoluteDir(FrameIO.AUDIO_PATH);
275 FolderSettings.AudioDirs.addAbsoluteDir(FrameIO.AUDIO_USERNAME_PRIVATE_PATH);
276
277 FolderSettings.FrameDirs.setDefault(FolderSettings.FrameDirs.get());
278 FolderSettings.ImageDirs.setDefault(FolderSettings.ImageDirs.get());
279 FolderSettings.AudioDirs.setDefault(FolderSettings.AudioDirs.get());
280 } else {
281 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.HELP_PATH);
282 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.FRAME_PATH);
283 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.MESSAGES_PATH);
284 if (AuthenticatorBrowser.isAuthenticated()) {
285 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.SHARED_FRAMESETS_PATH);
286 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.MAIL_PATH);
287 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.CONTACTS_PATH);
288 }
289
290 FolderSettings.ImageDirs.addAbsoluteDir(FrameIO.IMAGES_PATH);
291
292 FolderSettings.AudioDirs.addAbsoluteDir(FrameIO.AUDIO_PATH);
293 }
294
295 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.PUBLIC_PATH);
296 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.GROUP_PATH);
297 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.PROFILE_PATH);
298 FolderSettings.FrameDirs.addAbsoluteDir(FrameIO.TRASH_PATH);
299
300 FolderSettings.FrameDirs.setDefault(FolderSettings.FrameDirs.get());
301 FolderSettings.ImageDirs.setDefault(FolderSettings.ImageDirs.get());
302 FolderSettings.AudioDirs.setDefault(FolderSettings.AudioDirs.get());
303 }
304
305 /**
306 * Find the appropriate directory to store application settings in for
307 * the current OS.
308 * This has only been tested on Linux so far, so if it doesn't work it
309 * may need to be modified or reverted. Should return:
310 * Linux: ~/.expeditee
311 * Windows: %appdata%\.expeditee
312 * Mac: ~/Library/Application\ Support/.expeditee
313 * @return the path to store expeditee's settings
314 */
315 public static String getSaveLocation() {
316 String OS=System.getProperty("os.name").toLowerCase();
317 if(OS.indexOf("win")>=0) { //windoze
318 return System.getenv("APPDATA")+File.separator+".expeditee"+File.separator;
319 } else if(OS.indexOf("mac")>=0) { //mac
320 return System.getProperty("user.home")+File.separator+"Library"+File.separator+"Application Support"+File.separator+".expeditee"+File.separator;
321 } else { //linux or other
322 return System.getProperty("user.home")+File.separator+".expeditee"+File.separator;
323 }
324 }
325}
Note: See TracBrowser for help on using the repository browser.