[919] | 1 | /**
|
---|
| 2 | * FrameIO.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 |
|
---|
[4] | 19 | package org.expeditee.gui;
|
---|
| 20 |
|
---|
[348] | 21 | import java.io.BufferedOutputStream;
|
---|
[4] | 22 | import java.io.BufferedReader;
|
---|
| 23 | import java.io.BufferedWriter;
|
---|
| 24 | import java.io.File;
|
---|
[7] | 25 | import java.io.FileInputStream;
|
---|
[162] | 26 | import java.io.FileNotFoundException;
|
---|
[7] | 27 | import java.io.FileOutputStream;
|
---|
[4] | 28 | import java.io.FileReader;
|
---|
| 29 | import java.io.FileWriter;
|
---|
| 30 | import java.io.IOException;
|
---|
[348] | 31 | import java.io.OutputStream;
|
---|
| 32 | import java.io.OutputStreamWriter;
|
---|
| 33 | import java.io.Writer;
|
---|
[997] | 34 | import java.nio.channels.FileChannel;
|
---|
[1244] | 35 | import java.nio.file.Files;
|
---|
| 36 | import java.nio.file.Path;
|
---|
[1242] | 37 | import java.nio.file.Paths;
|
---|
[1293] | 38 | import java.nio.file.StandardCopyOption;
|
---|
[105] | 39 | import java.sql.Time;
|
---|
[1317] | 40 | import java.util.ArrayList;
|
---|
[1242] | 41 | import java.util.Arrays;
|
---|
[162] | 42 | import java.util.Collection;
|
---|
[4] | 43 | import java.util.HashMap;
|
---|
[162] | 44 | import java.util.LinkedList;
|
---|
[1219] | 45 | import java.util.List;
|
---|
[1242] | 46 | import java.util.Map;
|
---|
[1270] | 47 | import java.util.function.Consumer;
|
---|
[1242] | 48 | import java.util.stream.Collectors;
|
---|
[4] | 49 |
|
---|
[1244] | 50 | import org.apollo.io.AudioPathManager;
|
---|
[415] | 51 | import org.expeditee.actions.Actions;
|
---|
[8] | 52 | import org.expeditee.agents.ExistingFramesetException;
|
---|
[1242] | 53 | import org.expeditee.agents.InvalidFramesetNameException;
|
---|
[1283] | 54 | import org.expeditee.auth.AuthenticatorBrowser;
|
---|
[1219] | 55 | import org.expeditee.auth.EncryptedExpReader;
|
---|
[1227] | 56 | import org.expeditee.auth.EncryptedExpWriter;
|
---|
[1219] | 57 | import org.expeditee.auth.gui.MailBay;
|
---|
[1242] | 58 | import org.expeditee.gio.EcosystemManager;
|
---|
[4] | 59 | import org.expeditee.io.Conversion;
|
---|
| 60 | import org.expeditee.io.ExpReader;
|
---|
| 61 | import org.expeditee.io.ExpWriter;
|
---|
| 62 | import org.expeditee.io.FrameReader;
|
---|
| 63 | import org.expeditee.io.FrameWriter;
|
---|
| 64 | import org.expeditee.io.KMSReader;
|
---|
| 65 | import org.expeditee.io.KMSWriter;
|
---|
| 66 | import org.expeditee.items.Item;
|
---|
| 67 | import org.expeditee.items.ItemUtils;
|
---|
[924] | 68 | import org.expeditee.items.Justification;
|
---|
[454] | 69 | import org.expeditee.items.PermissionPair;
|
---|
[778] | 70 | import org.expeditee.items.Text;
|
---|
[454] | 71 | import org.expeditee.items.UserAppliedPermission;
|
---|
[298] | 72 | import org.expeditee.network.FrameShare;
|
---|
[1242] | 73 | import org.expeditee.setting.Setting;
|
---|
[570] | 74 | import org.expeditee.settings.UserSettings;
|
---|
[778] | 75 | import org.expeditee.settings.folders.FolderSettings;
|
---|
| 76 | import org.expeditee.settings.templates.TemplateSettings;
|
---|
[419] | 77 | import org.expeditee.stats.Formatter;
|
---|
| 78 | import org.expeditee.stats.Logger;
|
---|
[4] | 79 | import org.expeditee.stats.SessionStats;
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * This class provides static methods for all saving and loading of Frames
|
---|
| 83 | * to\from disk. This class also handles any caching of previously loaded
|
---|
| 84 | * Frames.
|
---|
| 85 | *
|
---|
| 86 | * @author jdm18
|
---|
| 87 | *
|
---|
| 88 | */
|
---|
[1244] | 89 |
|
---|
| 90 |
|
---|
[4] | 91 | public class FrameIO {
|
---|
| 92 |
|
---|
[24] | 93 | private static final char FRAME_NAME_LAST_CHAR = 'A';
|
---|
| 94 |
|
---|
[1242] | 95 | // The parent path that all others are relative to. Also referred to as Expeditee Home.
|
---|
[4] | 96 | public static String PARENT_FOLDER;
|
---|
[1274] | 97 |
|
---|
[1242] | 98 | public static String PROFILE_PATH;
|
---|
[4] | 99 | public static String FRAME_PATH;
|
---|
[1242] | 100 | public static String IMAGES_PATH;
|
---|
[1244] | 101 | public static String AUDIO_PATH;
|
---|
[286] | 102 | public static String PUBLIC_PATH;
|
---|
[1242] | 103 | public static String TRASH_PATH;
|
---|
[761] | 104 | public static String FONT_PATH;
|
---|
[504] | 105 | public static String DICT_PATH;
|
---|
[1274] | 106 | public static String EXPORTS_PATH;
|
---|
| 107 | public static String STATISTICS_PATH;
|
---|
| 108 | public static String LOGS_PATH;
|
---|
| 109 | public static String MESSAGES_PATH;
|
---|
| 110 | public static String MAIL_PATH;
|
---|
[1242] | 111 | public static String SHARED_FRAMESETS_PATH;
|
---|
| 112 | public static String CONTACTS_PATH;
|
---|
[1274] | 113 | public static String RESOURCES_PRIVATE_PATH;
|
---|
[1242] | 114 | public static String RESOURCES_PATH;
|
---|
| 115 | public static String FRAME_PRIVATE_PATH;
|
---|
| 116 | public static String IMAGES_PRIVATE_PATH;
|
---|
[1244] | 117 | public static String AUDIO_PRIVATE_PATH;
|
---|
[1257] | 118 | public static String HELP_PRIVATE_PATH;
|
---|
[1242] | 119 | public static String HELP_PATH;
|
---|
[1277] | 120 | public static String DEAD_DROPS_PATH;
|
---|
[1274] | 121 |
|
---|
[1242] | 122 | // Paths that appear to be unused.
|
---|
| 123 | public static String TEMPLATES_PATH;
|
---|
| 124 |
|
---|
| 125 | // Variables for controlling cache functionality.
|
---|
[4] | 126 | public static final int MAX_NAME_LENGTH = 64;
|
---|
[86] | 127 | public static final int MAX_CACHE = 100;
|
---|
[214] | 128 | private static HashMap<String, Frame> _Cache = new FrameCache();
|
---|
[4] | 129 | private static boolean ENABLE_CACHE = true;
|
---|
| 130 | private static boolean _UseCache = true;
|
---|
| 131 | private static boolean _SuspendedCache = false;
|
---|
[1242] | 132 |
|
---|
| 133 | private static final String INF_FILENAME = "frame.inf";
|
---|
| 134 |
|
---|
[1244] | 135 | public static void changeParentAndSubFolders(String newFolder) {
|
---|
[1328] | 136 | // Partial Paths
|
---|
| 137 | PARENT_FOLDER = newFolder;
|
---|
[1244] | 138 | PUBLIC_PATH = PARENT_FOLDER + "public" + File.separator;
|
---|
| 139 | TRASH_PATH = PARENT_FOLDER + "trash" + File.separator;
|
---|
| 140 | PROFILE_PATH = PARENT_FOLDER + "profiles" + File.separator;
|
---|
[1274] | 141 | EXPORTS_PATH = PARENT_FOLDER + "exports" + File.separator;
|
---|
| 142 | STATISTICS_PATH = PARENT_FOLDER + "statistics" + File.separator;
|
---|
| 143 | LOGS_PATH = PARENT_FOLDER + "logs" + File.separator;
|
---|
[1244] | 144 |
|
---|
[1328] | 145 | String resourcesPublicPath = PARENT_FOLDER + "resources-public" + File.separator;
|
---|
| 146 | String resourcesPrivateIndividualPath = PARENT_FOLDER + "resources-" + UserSettings.UserName.get() + File.separator;
|
---|
| 147 |
|
---|
[1244] | 148 | if (UserSettings.PublicAndPrivateResources) {
|
---|
[1328] | 149 | // Paths for the new regime
|
---|
[1244] | 150 | FONT_PATH = resourcesPublicPath + "fonts" + File.separator;
|
---|
| 151 | DICT_PATH = resourcesPublicPath + "dict" + File.separator;
|
---|
[1328] | 152 | HELP_PATH = resourcesPublicPath + "documentation" + File.separator;
|
---|
| 153 | HELP_PRIVATE_PATH = resourcesPrivateIndividualPath + "documentation" + File.separator;
|
---|
| 154 | FRAME_PATH = resourcesPublicPath + "framesets" + File.separator;
|
---|
| 155 | FRAME_PRIVATE_PATH = resourcesPrivateIndividualPath + "framesets" + File.separator;
|
---|
| 156 | MESSAGES_PATH = resourcesPrivateIndividualPath + "messages" + File.separator;
|
---|
[1244] | 157 | IMAGES_PATH = resourcesPublicPath + "images" + File.separator;
|
---|
[1328] | 158 | IMAGES_PRIVATE_PATH = resourcesPrivateIndividualPath + "images" + File.separator;
|
---|
[1244] | 159 | AUDIO_PATH = resourcesPublicPath + "audio" + File.separator;
|
---|
[1328] | 160 | AUDIO_PRIVATE_PATH = resourcesPrivateIndividualPath + "audio" + File.separator;
|
---|
| 161 |
|
---|
| 162 | // Used only when extracting resources (when expeditee is run for first time)
|
---|
| 163 | RESOURCES_PRIVATE_PATH = PARENT_FOLDER + "resources-private" + File.separator;
|
---|
| 164 |
|
---|
| 165 | if (AuthenticatorBrowser.isAuthenticated()) {
|
---|
| 166 | // Paths for the new regime while authenticated
|
---|
| 167 | SHARED_FRAMESETS_PATH = resourcesPrivateIndividualPath + "framesets-shared" + File.separator;
|
---|
| 168 | DEAD_DROPS_PATH = resourcesPrivateIndividualPath + "deaddrops" + File.separator;
|
---|
| 169 | CONTACTS_PATH = resourcesPrivateIndividualPath + "contacts" + File.separator;
|
---|
| 170 | MAIL_PATH = resourcesPrivateIndividualPath + "mail" + File.separator;
|
---|
| 171 | } else {
|
---|
| 172 | SHARED_FRAMESETS_PATH = null;
|
---|
| 173 | DEAD_DROPS_PATH = null;
|
---|
| 174 | CONTACTS_PATH = null;
|
---|
| 175 | MAIL_PATH = null;
|
---|
| 176 | }
|
---|
[1251] | 177 | } else {
|
---|
[1328] | 178 | // Paths for the old regime
|
---|
[1244] | 179 | FONT_PATH = PARENT_FOLDER + "fonts" + File.separator;
|
---|
| 180 | DICT_PATH = PARENT_FOLDER + "dict" + File.separator;
|
---|
[1328] | 181 | HELP_PATH = PARENT_FOLDER + "documentation" + File.separator;
|
---|
| 182 | FRAME_PATH = PARENT_FOLDER + "framesets" + File.separator;
|
---|
| 183 | MESSAGES_PATH = PARENT_FOLDER + "messages" + File.separator;
|
---|
[1244] | 184 | IMAGES_PATH = PARENT_FOLDER + "images" + File.separator;
|
---|
| 185 | AUDIO_PATH = PARENT_FOLDER + "audio" + File.separator;
|
---|
| 186 |
|
---|
[1328] | 187 | if (AuthenticatorBrowser.isAuthenticated()) {
|
---|
| 188 | // Paths for the old regime while authenticated
|
---|
| 189 | SHARED_FRAMESETS_PATH = PARENT_FOLDER + "framesets-shared" + File.separator;
|
---|
| 190 | DEAD_DROPS_PATH = PARENT_FOLDER + "deaddrops" + File.separator;
|
---|
| 191 | CONTACTS_PATH = PARENT_FOLDER + "contacts" + File.separator;
|
---|
| 192 | MAIL_PATH = PARENT_FOLDER + "mail" + File.separator;
|
---|
[1274] | 193 | } else {
|
---|
[1328] | 194 | SHARED_FRAMESETS_PATH = null;
|
---|
| 195 | DEAD_DROPS_PATH = null;
|
---|
| 196 | CONTACTS_PATH = null;
|
---|
| 197 | MAIL_PATH = null;
|
---|
[1274] | 198 | }
|
---|
[1328] | 199 | }
|
---|
[1244] | 200 |
|
---|
| 201 | System.err.println("**** FrameIO::changeParentAndSubFolder(): Calling AudioPathManger.changeParentAndSubFolder()");
|
---|
| 202 | AudioPathManager.changeParentAndSubFolders(newFolder);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[1328] | 205 | // public static void changeParentAndSubFolders(String newFolder) {
|
---|
| 206 | // // Partial Paths.
|
---|
| 207 | // PARENT_FOLDER = newFolder;
|
---|
| 208 | // String resourcesPublicPath = PARENT_FOLDER + "resources-public" + File.separator;
|
---|
| 209 | // String resourcesPrivateIndividualPath = PARENT_FOLDER + "resources-" + UserSettings.UserName.get() + File.separator;
|
---|
| 210 | //
|
---|
| 211 | // // Standard paths.
|
---|
| 212 | // PUBLIC_PATH = PARENT_FOLDER + "public" + File.separator;
|
---|
| 213 | // TRASH_PATH = PARENT_FOLDER + "trash" + File.separator;
|
---|
| 214 | // HELP_PATH = PARENT_FOLDER + "documentation" + File.separator;
|
---|
| 215 | // PROFILE_PATH = PARENT_FOLDER + "profiles" + File.separator;
|
---|
| 216 | // EXPORTS_PATH = PARENT_FOLDER + "exports" + File.separator;
|
---|
| 217 | // STATISTICS_PATH = PARENT_FOLDER + "statistics" + File.separator;
|
---|
| 218 | // LOGS_PATH = PARENT_FOLDER + "logs" + File.separator;
|
---|
| 219 | //
|
---|
| 220 | //
|
---|
| 221 | // // Conditional paths
|
---|
| 222 | // if (UserSettings.PublicAndPrivateResources) {
|
---|
| 223 | // // Work with a system of public and private folders
|
---|
| 224 | //
|
---|
| 225 | // FONT_PATH = resourcesPublicPath + "fonts" + File.separator;
|
---|
| 226 | // DICT_PATH = resourcesPublicPath + "dict" + File.separator;
|
---|
| 227 | // IMAGES_PATH = resourcesPublicPath + "images" + File.separator;
|
---|
| 228 | // AUDIO_PATH = resourcesPublicPath + "audio" + File.separator;
|
---|
| 229 | // FRAME_PATH = resourcesPublicPath + "framesets" + File.separator;
|
---|
| 230 | // } else {
|
---|
| 231 | // FONT_PATH = PARENT_FOLDER + "fonts" + File.separator;
|
---|
| 232 | // DICT_PATH = PARENT_FOLDER + "dict" + File.separator;
|
---|
| 233 | // IMAGES_PATH = PARENT_FOLDER + "images" + File.separator;
|
---|
| 234 | // AUDIO_PATH = PARENT_FOLDER + "audio" + File.separator;
|
---|
| 235 | // FRAME_PATH = PARENT_FOLDER + "framesets" + File.separator;
|
---|
| 236 | // DEAD_DROPS_PATH = PARENT_FOLDER + "deaddrops" + File.separator;
|
---|
| 237 | // }
|
---|
| 238 | //
|
---|
| 239 | // if (!UserSettings.PublicAndPrivateResources || (AuthenticatorBrowser.isAuthenticationRequired() && !AuthenticatorBrowser.isAuthenticated())) {
|
---|
| 240 | //
|
---|
| 241 | // if (UserSettings.UserName.get().equals(AuthenticatorBrowser.USER_NOBODY)) {
|
---|
| 242 | // System.err.println("**** FrameIO::changeParentAndSubFolders(): Not setting subfolders for user '"+AuthenticatorBrowser.USER_NOBODY+"'");
|
---|
| 243 | // }
|
---|
| 244 | //
|
---|
| 245 | // // If we are using the old regime, or user.name set to Browser.USER_NOBODY
|
---|
| 246 | // // => then these paths should not be used.
|
---|
| 247 | // RESOURCES_PATH = null;
|
---|
| 248 | // SHARED_FRAMESETS_PATH = null;
|
---|
| 249 | // RESOURCES_PRIVATE_PATH = null;
|
---|
| 250 | // FRAME_PRIVATE_PATH = null;
|
---|
| 251 | // IMAGES_PRIVATE_PATH = null;
|
---|
| 252 | // AUDIO_PRIVATE_PATH = null;
|
---|
| 253 | // CONTACTS_PATH = null;
|
---|
| 254 | // HELP_PRIVATE_PATH = null;
|
---|
| 255 | //
|
---|
| 256 | // if (!UserSettings.PublicAndPrivateResources) {
|
---|
| 257 | // MESSAGES_PATH = PARENT_FOLDER + "messages" + File.separator;
|
---|
| 258 | // } else {
|
---|
| 259 | // MESSAGES_PATH = resourcesPrivateIndividualPath + "messages" + File.separator;
|
---|
| 260 | // }
|
---|
| 261 | //
|
---|
| 262 | // } else {
|
---|
| 263 | // RESOURCES_PATH = resourcesPublicPath + "documentation" + File.separator;
|
---|
| 264 | // SHARED_FRAMESETS_PATH = resourcesPrivateIndividualPath + "framesets-shared" + File.separator;
|
---|
| 265 | //
|
---|
| 266 | // RESOURCES_PRIVATE_PATH = PARENT_FOLDER + "resources-private" + File.separator;
|
---|
| 267 | // FRAME_PRIVATE_PATH = resourcesPrivateIndividualPath + "framesets" + File.separator;
|
---|
| 268 | // IMAGES_PRIVATE_PATH = resourcesPrivateIndividualPath + "images" + File.separator;
|
---|
| 269 | // AUDIO_PRIVATE_PATH = resourcesPrivateIndividualPath + "audio" + File.separator;
|
---|
| 270 | // CONTACTS_PATH = resourcesPrivateIndividualPath + "contacts" + File.separator;
|
---|
| 271 | // HELP_PRIVATE_PATH = resourcesPrivateIndividualPath + "documentation" + File.separator;
|
---|
| 272 | // MESSAGES_PATH = resourcesPrivateIndividualPath + "messages" + File.separator;
|
---|
| 273 | // MAIL_PATH = resourcesPrivateIndividualPath + "mail" + File.separator;
|
---|
| 274 | // DEAD_DROPS_PATH = resourcesPrivateIndividualPath + "deaddrops" + File.separator;
|
---|
| 275 | // }
|
---|
| 276 | //
|
---|
| 277 | //
|
---|
| 278 | // System.err.println("**** FrameIO::changeParentAndSubFolder(): Calling AudioPathManger.changeParentAndSubFolder()");
|
---|
| 279 | // AudioPathManager.changeParentAndSubFolders(newFolder);
|
---|
| 280 | // }
|
---|
| 281 |
|
---|
[4] | 282 | // All methods are static, this should not be instantiated
|
---|
| 283 | private FrameIO() {
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | public static boolean isCacheOn() {
|
---|
| 287 | return _UseCache && ENABLE_CACHE;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | public static void Precache(String framename) {
|
---|
| 291 | // if the cache is turned off, do nothing
|
---|
[1188] | 292 | if (!isCacheOn()) {
|
---|
[4] | 293 | return;
|
---|
[1188] | 294 | }
|
---|
[4] | 295 |
|
---|
| 296 | // if the frame is already in the cache, do nothing
|
---|
[1188] | 297 | if (_Cache.containsKey(framename.toLowerCase())) {
|
---|
[4] | 298 | return;
|
---|
[1188] | 299 | }
|
---|
[4] | 300 |
|
---|
| 301 | // otherwise, load the frame and put it in the cache
|
---|
| 302 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Precaching " + framename + ".");
|
---|
| 303 |
|
---|
| 304 | // do not display errors encountered to the user
|
---|
| 305 | // (they will be shown at load time)
|
---|
[689] | 306 | MessageBay.suppressMessages(true);
|
---|
[4] | 307 | // loading automatically caches the frame is caching is turned on
|
---|
[307] | 308 | LoadFromDisk(framename, null, false);
|
---|
[689] | 309 | MessageBay.suppressMessages(false);
|
---|
[4] | 310 | }
|
---|
| 311 |
|
---|
| 312 | /**
|
---|
| 313 | * Checks if a string is a representation of a positive integer.
|
---|
| 314 | *
|
---|
| 315 | * @param s
|
---|
| 316 | * @return true if s is a positive integer
|
---|
| 317 | */
|
---|
| 318 | public static boolean isPositiveInteger(String s) {
|
---|
[1188] | 319 | if (s == null || s.length() == 0) {
|
---|
[4] | 320 | return false;
|
---|
[1188] | 321 | }
|
---|
[4] | 322 |
|
---|
| 323 | for (int i = 0; i < s.length(); i++) {
|
---|
[1188] | 324 | if (!Character.isDigit(s.charAt(i))) {
|
---|
[4] | 325 | return false;
|
---|
[1188] | 326 | }
|
---|
[4] | 327 | }
|
---|
| 328 | return true;
|
---|
| 329 | }
|
---|
[1188] | 330 |
|
---|
[1242] | 331 | /**
|
---|
| 332 | * Loads a frame with the specified name.
|
---|
| 333 | * By using a dot separated framename, users are able to specify the path to find the frameset in.
|
---|
| 334 | * @param frameName The frame to load.
|
---|
| 335 | * @return the loaded frame
|
---|
| 336 | */
|
---|
[4] | 337 | public static Frame LoadFrame(String frameName) {
|
---|
[1242] | 338 | if (frameName.contains(".")) {
|
---|
| 339 | String[] split = frameName.split("\\.");
|
---|
| 340 | String[] pathSplit = Arrays.copyOfRange(split, 0, split.length - 1);
|
---|
| 341 | String name = split[split.length - 1];
|
---|
| 342 | String path = Arrays.asList(pathSplit).stream().collect(Collectors.joining(File.separator));
|
---|
| 343 | return LoadFrame(name, Paths.get(FrameIO.PARENT_FOLDER).resolve(path).toString() + File.separator, false);
|
---|
| 344 | } else {
|
---|
[1284] | 345 | return LoadFrame(frameName, null, false);
|
---|
[1242] | 346 | }
|
---|
[282] | 347 | }
|
---|
[298] | 348 |
|
---|
[307] | 349 | public static Frame LoadFrame(String frameName, String path) {
|
---|
| 350 | return LoadFrame(frameName, path, false);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[1219] | 353 | public static Frame LoadFrame(String frameName, String path, boolean ignoreAnnotations) {
|
---|
[1188] | 354 | if (!isValidFrameName(frameName)) {
|
---|
| 355 | return null;
|
---|
| 356 | }
|
---|
[1242] | 357 |
|
---|
[108] | 358 | String frameNameLower = frameName.toLowerCase();
|
---|
[4] | 359 | // first try reading from cache
|
---|
[108] | 360 | if (isCacheOn() && _Cache.containsKey(frameNameLower)) {
|
---|
[1102] | 361 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName + " from cache.");
|
---|
[247] | 362 | Frame frame = _Cache.get(frameNameLower);
|
---|
[1229] | 363 |
|
---|
[1242] | 364 | // if frame in cache is older than the one on disk then don't use the cached one
|
---|
| 365 | File file = new File(frame.getFramePathReal());
|
---|
| 366 | long lastModified = file.lastModified();
|
---|
| 367 | if (lastModified <= frame.getLastModifyPrecise()) {
|
---|
[1229] | 368 | return frame;
|
---|
| 369 | }
|
---|
[4] | 370 | }
|
---|
| 371 |
|
---|
| 372 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
|
---|
| 373 | + " from disk.");
|
---|
[307] | 374 |
|
---|
[1242] | 375 | Frame fromDisk = LoadFromDisk(frameName, path, ignoreAnnotations);
|
---|
| 376 | return fromDisk;
|
---|
[4] | 377 | }
|
---|
| 378 |
|
---|
[997] | 379 | //Loads the 'restore' version of a frame if there is one
|
---|
[1229] | 380 | public static Frame LoadRestoreFrame(Frame frameToRestore) {
|
---|
[997] | 381 |
|
---|
[1229] | 382 | String fullPath = getFrameFullPathName(frameToRestore.getPath(), frameToRestore
|
---|
| 383 | .getName());
|
---|
| 384 | //System.out.println("fullpath: " + fullPath);
|
---|
| 385 | String restoreVersion = fullPath + ".restore";
|
---|
| 386 | //System.out.println("restoreversion" + restoreVersion);
|
---|
| 387 | File source = new File(restoreVersion);
|
---|
| 388 | File dest = new File(fullPath);
|
---|
| 389 |
|
---|
| 390 | FileChannel inputChannel = null;
|
---|
| 391 | FileChannel outputChannel = null;
|
---|
| 392 |
|
---|
| 393 | try{
|
---|
[1242] | 394 | FileInputStream source_fis = new FileInputStream(source);
|
---|
| 395 | inputChannel = source_fis.getChannel();
|
---|
| 396 |
|
---|
| 397 | FileOutputStream dest_fos = new FileOutputStream(dest);
|
---|
| 398 | outputChannel = dest_fos.getChannel();
|
---|
[997] | 399 |
|
---|
[1242] | 400 | outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
|
---|
| 401 | inputChannel.close();
|
---|
| 402 | outputChannel.close();
|
---|
| 403 | source_fis.close();
|
---|
| 404 | dest_fos.close();
|
---|
[1229] | 405 | }
|
---|
| 406 | catch(Exception e){
|
---|
| 407 | System.err.println("No restore point detected.");
|
---|
| 408 | }
|
---|
| 409 | String frameName = frameToRestore.getName();
|
---|
| 410 | String frameNameLower = frameName.toLowerCase();
|
---|
[997] | 411 |
|
---|
[1229] | 412 | // first try reading from cache
|
---|
| 413 | if (isCacheOn() && _Cache.containsKey(frameNameLower)) {
|
---|
| 414 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Clearing " + frameName
|
---|
| 415 | + " from cache.");
|
---|
| 416 | _Cache.remove(frameNameLower);
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | return LoadFrame(frameName, frameToRestore.getPath(), true);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[305] | 422 | public static BufferedReader LoadPublicFrame(String frameName) {
|
---|
| 423 | String fullPath = FrameIO.getFrameFullPathName(PUBLIC_PATH, frameName);
|
---|
[306] | 424 |
|
---|
[1188] | 425 | if (fullPath == null) {
|
---|
[307] | 426 | return null;
|
---|
[1188] | 427 | }
|
---|
[336] | 428 |
|
---|
[305] | 429 | File frameFile = new File(fullPath);
|
---|
| 430 | if (frameFile.exists() && frameFile.canRead()) {
|
---|
| 431 | try {
|
---|
| 432 | return new BufferedReader(new FileReader(frameFile));
|
---|
| 433 | } catch (FileNotFoundException e) {
|
---|
| 434 | e.printStackTrace();
|
---|
| 435 | }
|
---|
[298] | 436 | }
|
---|
[305] | 437 | return null;
|
---|
[298] | 438 | }
|
---|
| 439 |
|
---|
[307] | 440 | private static Frame LoadFromDisk(String framename, String knownPath,
|
---|
[298] | 441 | boolean ignoreAnnotations) {
|
---|
| 442 | Frame loaded = null;
|
---|
| 443 |
|
---|
[307] | 444 | if (knownPath != null) {
|
---|
[1242] | 445 | loaded = LoadKnownPath(knownPath, framename);
|
---|
[307] | 446 | } else {
|
---|
[1244] | 447 | List<String> directoriesToSearch = FolderSettings.FrameDirs.getAbsoluteDirs();
|
---|
[1219] | 448 |
|
---|
[1316] | 449 | // if (AuthenticatorBrowser.Authenticated) {
|
---|
| 450 | // // if we are running Expeditee Authenticated, consult user profile as location for framesets first
|
---|
| 451 | // String profilePath = FrameIO.PROFILE_PATH + UserSettings.UserName.get() + File.separator;
|
---|
| 452 | // directoriesToSearch.add(0, profilePath);
|
---|
| 453 | // }
|
---|
[1219] | 454 |
|
---|
| 455 | for (String path : directoriesToSearch) {
|
---|
[1242] | 456 | loaded = LoadKnownPath(path, framename);
|
---|
[307] | 457 | if (loaded != null) {
|
---|
| 458 | break;
|
---|
| 459 | }
|
---|
[4] | 460 | }
|
---|
| 461 | }
|
---|
[336] | 462 |
|
---|
[305] | 463 | if (loaded == null && FrameShare.getInstance() != null) {
|
---|
[307] | 464 | loaded = FrameShare.getInstance().loadFrame(framename, knownPath);
|
---|
[298] | 465 | }
|
---|
| 466 |
|
---|
| 467 | if (loaded != null) {
|
---|
| 468 | FrameUtils.Parse(loaded, true, ignoreAnnotations);
|
---|
[1242] | 469 | FrameIO.setSavedProperties(loaded);
|
---|
[298] | 470 | }
|
---|
| 471 |
|
---|
[4] | 472 | return loaded;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | /**
|
---|
[7] | 476 | * Gets a list of all the framesets available to the user
|
---|
| 477 | *
|
---|
| 478 | * @return a string containing a list of all the available framesets on
|
---|
| 479 | * separate lines
|
---|
| 480 | */
|
---|
| 481 | public static String getFramesetList() {
|
---|
| 482 | StringBuffer list = new StringBuffer();
|
---|
| 483 |
|
---|
[1244] | 484 | for (String path : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
[97] | 485 | File files = new File(path);
|
---|
[1188] | 486 | if (!files.exists()) {
|
---|
[97] | 487 | continue;
|
---|
[1188] | 488 | }
|
---|
[7] | 489 | for (File f : (new File(path)).listFiles()) {
|
---|
| 490 | if (f.isDirectory()) {
|
---|
| 491 | list.append(f.getName()).append('\n');
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | // remove the final new line char
|
---|
| 496 | list.deleteCharAt(list.length() - 1);
|
---|
| 497 | return list.toString();
|
---|
| 498 | }
|
---|
[1316] | 499 |
|
---|
| 500 | /**
|
---|
| 501 | * Gets a list of all the profiles available to the user
|
---|
| 502 | *
|
---|
| 503 | * @return a list containing all the available framesets on separate lines
|
---|
| 504 | */
|
---|
| 505 | public static List<String> getProfilesList() {
|
---|
[1317] | 506 | File[] listFiles = new File(FrameIO.PROFILE_PATH).listFiles();
|
---|
| 507 | if (listFiles == null) return new ArrayList<String>();
|
---|
| 508 | List<File> potentialProfiles = Arrays.asList(listFiles);
|
---|
[1316] | 509 | potentialProfiles.removeIf(file -> !file.isDirectory());
|
---|
| 510 | return potentialProfiles.stream().map(dir -> dir.getName()).collect(Collectors.toList());
|
---|
| 511 | }
|
---|
[7] | 512 |
|
---|
| 513 | /**
|
---|
[4] | 514 | * Gets the full path and file name of the frame.
|
---|
[1242] | 515 | * This is a alias for Frame::getFramePathLogical
|
---|
[4] | 516 | * @param path-
|
---|
| 517 | * the directory in which to look for the frameset containing the
|
---|
| 518 | * frame.
|
---|
| 519 | * @param frameName-
|
---|
| 520 | * the name of the frame for which the path is being requested.
|
---|
| 521 | * @return null if the frame can not be located.
|
---|
| 522 | */
|
---|
[298] | 523 | public static synchronized String getFrameFullPathName(String path,
|
---|
| 524 | String frameName) {
|
---|
[997] | 525 |
|
---|
| 526 | String source;
|
---|
| 527 | String fileName = null;
|
---|
| 528 | if(frameName.contains("restore")){
|
---|
| 529 | source = path + File.separator;// + frameName;
|
---|
| 530 | fileName = path + File.separator + frameName + ExpReader.EXTENTION;
|
---|
| 531 |
|
---|
| 532 | }
|
---|
| 533 | else
|
---|
| 534 | {
|
---|
| 535 | source = path + Conversion.getFramesetName(frameName)
|
---|
| 536 | + File.separator;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
[4] | 539 |
|
---|
| 540 | File tester = new File(source);
|
---|
[1188] | 541 | if (!tester.exists()) {
|
---|
[4] | 542 | return null;
|
---|
[1188] | 543 | }
|
---|
[4] | 544 |
|
---|
[997] | 545 | String fullPath;
|
---|
| 546 |
|
---|
| 547 | if(frameName.contains("restore")){
|
---|
| 548 |
|
---|
| 549 | fullPath = fileName;
|
---|
| 550 | }
|
---|
| 551 | else
|
---|
| 552 | {
|
---|
| 553 | // check for the new file name format
|
---|
| 554 | fullPath = source + Conversion.getFrameNumber(frameName)
|
---|
| 555 | + ExpReader.EXTENTION;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[4] | 558 | tester = new File(fullPath);
|
---|
| 559 |
|
---|
[1188] | 560 | if (tester.exists()) {
|
---|
[4] | 561 | return fullPath;
|
---|
[1188] | 562 | }
|
---|
[4] | 563 |
|
---|
[601] | 564 | // check for oldfile name format
|
---|
[97] | 565 | fullPath = source + Conversion.getFramesetName(frameName) + "."
|
---|
[4] | 566 | + Conversion.getFrameNumber(frameName);
|
---|
| 567 | tester = new File(fullPath);
|
---|
| 568 |
|
---|
[1188] | 569 | if (tester.exists()) {
|
---|
[4] | 570 | return fullPath;
|
---|
[1188] | 571 | }
|
---|
[4] | 572 |
|
---|
| 573 | return null;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[97] | 576 | public static boolean canAccessFrame(String frameName) {
|
---|
[1102] | 577 | Frame current = DisplayController.getCurrentFrame();
|
---|
[436] | 578 | // Just in case the current frame is not yet saved...
|
---|
[298] | 579 | if (frameName.equals(current.getName())) {
|
---|
| 580 | FrameIO.SaveFrame(current, false, false);
|
---|
[294] | 581 | current.change();
|
---|
| 582 | return true;
|
---|
| 583 | }
|
---|
[298] | 584 |
|
---|
[1244] | 585 | for (String path : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
[1188] | 586 | if (getFrameFullPathName(path, frameName) != null) {
|
---|
[4] | 587 | return true;
|
---|
[1188] | 588 | }
|
---|
[4] | 589 | }
|
---|
| 590 | return false;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[162] | 593 | public static Collection<String> searchFrame(String frameName,
|
---|
| 594 | String pattern, String path) {
|
---|
| 595 | String fullPath = null;
|
---|
| 596 | if (path == null) {
|
---|
[1244] | 597 | for (String possiblePath : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
[162] | 598 | fullPath = getFrameFullPathName(possiblePath, frameName);
|
---|
[1188] | 599 | if (fullPath != null) {
|
---|
[162] | 600 | break;
|
---|
[1188] | 601 | }
|
---|
[162] | 602 | }
|
---|
| 603 | } else {
|
---|
| 604 | fullPath = getFrameFullPathName(path, frameName);
|
---|
| 605 | }
|
---|
| 606 | // If the frame was not located return null
|
---|
[1188] | 607 | if (fullPath == null) {
|
---|
[162] | 608 | return null;
|
---|
[1188] | 609 | }
|
---|
[162] | 610 | Collection<String> results = new LinkedList<String>();
|
---|
| 611 | // Open the file and search the text items
|
---|
| 612 | try {
|
---|
| 613 | BufferedReader reader = new BufferedReader(new FileReader(fullPath));
|
---|
| 614 | String next;
|
---|
| 615 | while (reader.ready() && ((next = reader.readLine()) != null)) {
|
---|
| 616 | if (next.startsWith("T")) {
|
---|
| 617 | String toSearch = next.substring(2);
|
---|
[1188] | 618 | if (toSearch.toLowerCase().contains(pattern)) {
|
---|
[162] | 619 | results.add(toSearch);
|
---|
[1188] | 620 | }
|
---|
[1289] | 621 | } else if (next.startsWith("+T+")) {
|
---|
| 622 | String toSearch = next.substring(4);
|
---|
| 623 | if (toSearch.toLowerCase().contains(pattern)) {
|
---|
| 624 | results.add(toSearch);
|
---|
| 625 | }
|
---|
[162] | 626 | }
|
---|
| 627 | }
|
---|
[1219] | 628 | reader.close();
|
---|
[162] | 629 | } catch (FileNotFoundException e) {
|
---|
| 630 | e.printStackTrace();
|
---|
| 631 | return null;
|
---|
| 632 | } catch (IOException e) {
|
---|
| 633 | e.printStackTrace();
|
---|
| 634 | }
|
---|
| 635 | return results;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
[1242] | 638 | private static Frame LoadKnownPath(String path, String frameName) {
|
---|
[4] | 639 | String fullPath = getFrameFullPathName(path, frameName);
|
---|
[1188] | 640 | if (fullPath == null) {
|
---|
[4] | 641 | return null;
|
---|
[1188] | 642 | }
|
---|
[4] | 643 |
|
---|
| 644 | try {
|
---|
| 645 | FrameReader reader;
|
---|
[1242] | 646 |
|
---|
| 647 | // Get the frameset name.
|
---|
| 648 | int i = frameName.length() - 1;
|
---|
| 649 | for (; i >= 0; i--) {
|
---|
| 650 | if (!Character.isDigit(frameName.charAt(i))) {
|
---|
| 651 | break;
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 | if (i < 0) {
|
---|
| 655 | System.err.println("LoadKnownFrame was provided with a invalid Frame name: " + frameName);
|
---|
| 656 | return null;
|
---|
| 657 | }
|
---|
| 658 | String framesetName = frameName.substring(0, i + 1);
|
---|
| 659 |
|
---|
| 660 | String redirectTo = ExpReader.redirectTo(fullPath);
|
---|
| 661 | while (redirectTo != null) {
|
---|
| 662 | fullPath = path + framesetName + File.separator + redirectTo;
|
---|
| 663 | redirectTo = ExpReader.redirectTo(fullPath);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
[601] | 666 | if (fullPath.endsWith(ExpReader.EXTENTION)) {
|
---|
[1200] | 667 | if (EncryptedExpReader.isEncryptedExpediteeFile(fullPath)) {
|
---|
[1227] | 668 | reader = new EncryptedExpReader(frameName);
|
---|
[1200] | 669 | } else {
|
---|
| 670 | reader = new ExpReader(frameName);
|
---|
| 671 | }
|
---|
[4] | 672 | } else {
|
---|
| 673 | reader = new KMSReader();
|
---|
| 674 | }
|
---|
| 675 | Frame frame = reader.readFrame(fullPath);
|
---|
| 676 |
|
---|
| 677 | if (frame == null) {
|
---|
[121] | 678 | MessageBay.errorMessage("Error: " + frameName
|
---|
[4] | 679 | + " could not be successfully loaded.");
|
---|
| 680 | return null;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[298] | 683 | frame.setPath(path);
|
---|
[4] | 684 |
|
---|
| 685 | // do not put 0 frames or virtual frames into the cache
|
---|
[336] | 686 | // Why are zero frames not put in the cache
|
---|
[1188] | 687 | if (_Cache.size() > MAX_CACHE) {
|
---|
[4] | 688 | _Cache.clear();
|
---|
[1188] | 689 | }
|
---|
[4] | 690 |
|
---|
[1188] | 691 | if (frame.getNumber() > 0 && isCacheOn()) {
|
---|
[4] | 692 | _Cache.put(frameName.toLowerCase(), frame);
|
---|
[1188] | 693 | }
|
---|
[4] | 694 |
|
---|
| 695 | return frame;
|
---|
| 696 | } catch (IOException ioe) {
|
---|
| 697 | ioe.printStackTrace();
|
---|
| 698 | Logger.Log(ioe);
|
---|
| 699 | } catch (Exception e) {
|
---|
| 700 | e.printStackTrace();
|
---|
| 701 | Logger.Log(e);
|
---|
[121] | 702 | MessageBay.errorMessage("Error: " + frameName
|
---|
[4] | 703 | + " could not be successfully loaded.");
|
---|
| 704 | }
|
---|
| 705 |
|
---|
| 706 | return null;
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | public static void Reload() {
|
---|
| 710 | // disable cache
|
---|
| 711 | boolean cache = _UseCache;
|
---|
| 712 |
|
---|
| 713 | _UseCache = false;
|
---|
[1102] | 714 | Frame fresh = FrameIO.LoadFrame(DisplayController.getCurrentFrame().getName());
|
---|
[4] | 715 | _UseCache = cache;
|
---|
[1188] | 716 | if (_Cache.containsKey(fresh.getName().toLowerCase())) {
|
---|
[108] | 717 | addToCache(fresh);
|
---|
[1188] | 718 | }
|
---|
[1102] | 719 | DisplayController.setCurrentFrame(fresh, false);
|
---|
[4] | 720 | }
|
---|
| 721 |
|
---|
| 722 | public static Frame LoadPrevious(Frame current) {
|
---|
[7] | 723 | checkTDFC(current);
|
---|
| 724 |
|
---|
[4] | 725 | // the current name and number
|
---|
[24] | 726 | String name = current.getFramesetName();
|
---|
[80] | 727 | int num = current.getNumber() - 1;
|
---|
[4] | 728 |
|
---|
| 729 | // loop until a frame that exists is found
|
---|
| 730 | for (; num >= 0; num--) {
|
---|
[307] | 731 | Frame f = LoadFrame(name + num, current.getPath());
|
---|
[1188] | 732 | if (f != null) {
|
---|
[4] | 733 | return f;
|
---|
[1188] | 734 | }
|
---|
[4] | 735 | }
|
---|
| 736 |
|
---|
| 737 | // if we did not find another Frame then this one must be the last one
|
---|
| 738 | // in the frameset
|
---|
[121] | 739 | MessageBay
|
---|
| 740 | .displayMessageOnce("This is the first frame in the frameset");
|
---|
[4] | 741 | return null;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | /**
|
---|
| 745 | * Returns the next Frame in the current Frameset (The Frame with the next
|
---|
| 746 | * highest Frame number) If the current Frame is the last one in the
|
---|
| 747 | * Frameset, or an error occurs then null is returned.
|
---|
| 748 | *
|
---|
| 749 | * @return The Frame after this one in the current frameset, or null
|
---|
| 750 | */
|
---|
| 751 | public static Frame LoadNext(Frame current) {
|
---|
[7] | 752 | checkTDFC(current);
|
---|
[4] | 753 |
|
---|
| 754 | // the current name and number
|
---|
[80] | 755 | int num = current.getNumber() + 1;
|
---|
[4] | 756 | int max = num + 1;
|
---|
[24] | 757 | String name = current.getFramesetName();
|
---|
[4] | 758 |
|
---|
| 759 | // read the maximum from the INF file
|
---|
| 760 | try {
|
---|
[307] | 761 | max = ReadINF(current.getPath(), current.getFramesetName(), false);
|
---|
[4] | 762 | } catch (IOException ioe) {
|
---|
[121] | 763 | MessageBay.errorMessage("Error loading INF file for frameset '"
|
---|
[4] | 764 | + name + "'");
|
---|
| 765 | return null;
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | // loop until a frame that exists is found
|
---|
| 769 | for (; num <= max; num++) {
|
---|
[307] | 770 | Frame f = LoadFrame(name + num, current.getPath());
|
---|
[1188] | 771 | if (f != null) {
|
---|
[4] | 772 | return f;
|
---|
[1188] | 773 | }
|
---|
[4] | 774 | }
|
---|
| 775 |
|
---|
| 776 | // if we did not find another Frame then this one must be the last one
|
---|
| 777 | // in the frameset
|
---|
[162] | 778 | MessageBay.displayMessageOnce("This is the last frame in the frameset");
|
---|
[4] | 779 | return null;
|
---|
| 780 | }
|
---|
| 781 |
|
---|
[7] | 782 | /**
|
---|
| 783 | * This method checks if the current frame has just been created with TDFC.
|
---|
| 784 | * If it has the frame is saved regardless of whether it has been edited or
|
---|
| 785 | * not and the TDFC item property is cleared. This is to ensure that the
|
---|
| 786 | * link is saved on the parent frame.
|
---|
| 787 | *
|
---|
| 788 | * @param current
|
---|
| 789 | */
|
---|
[86] | 790 | public static void checkTDFC(Frame current) {
|
---|
[70] | 791 | if (FrameUtils.getTdfcItem() != null) {
|
---|
| 792 | FrameUtils.setTdfcItem(null);
|
---|
[7] | 793 | current.change();
|
---|
| 794 | }
|
---|
| 795 | }
|
---|
| 796 |
|
---|
[4] | 797 | public static Frame LoadLast(String framesetName, String path) {
|
---|
| 798 | // read the maximum from the INF file
|
---|
| 799 | int max;
|
---|
| 800 | try {
|
---|
[307] | 801 | max = ReadINF(path, framesetName, false);
|
---|
[4] | 802 | } catch (IOException ioe) {
|
---|
[121] | 803 | MessageBay.errorMessage("Error loading INF file for frameset '"
|
---|
[4] | 804 | + framesetName + "'");
|
---|
| 805 | return null;
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 | // loop backwards until a frame that exists is found
|
---|
| 809 | for (int num = max; num > 0; num--) {
|
---|
[307] | 810 | Frame f = LoadFromDisk(framesetName + num, path, false);
|
---|
[1188] | 811 | if (f != null) {
|
---|
[4] | 812 | return f;
|
---|
[1188] | 813 | }
|
---|
[4] | 814 | }
|
---|
| 815 |
|
---|
| 816 | // if we did not find another Frame then this one must be the last one
|
---|
| 817 | // in the frameset
|
---|
[121] | 818 | MessageBay.displayMessage("This is the last frame in the frameset");
|
---|
[4] | 819 | return null;
|
---|
| 820 | }
|
---|
[24] | 821 |
|
---|
[13] | 822 | public static Frame LoadZero(String framesetName, String path) {
|
---|
[24] | 823 | return LoadFrame(framesetName + 0);
|
---|
[13] | 824 | }
|
---|
[4] | 825 |
|
---|
[13] | 826 | public static Frame LoadZero() {
|
---|
[1102] | 827 | Frame current = DisplayController.getCurrentFrame();
|
---|
[298] | 828 | return LoadZero(current.getFramesetName(), current.getPath());
|
---|
[13] | 829 | }
|
---|
[24] | 830 |
|
---|
[4] | 831 | public static Frame LoadLast() {
|
---|
[1102] | 832 | Frame current = DisplayController.getCurrentFrame();
|
---|
[298] | 833 | return LoadLast(current.getFramesetName(), current.getPath());
|
---|
[4] | 834 | }
|
---|
| 835 |
|
---|
| 836 | public static Frame LoadNext() {
|
---|
[1102] | 837 | return LoadNext(DisplayController.getCurrentFrame());
|
---|
[4] | 838 | }
|
---|
| 839 |
|
---|
| 840 | public static Frame LoadPrevious() {
|
---|
[1102] | 841 | return LoadPrevious(DisplayController.getCurrentFrame());
|
---|
[4] | 842 | }
|
---|
| 843 |
|
---|
| 844 | /**
|
---|
| 845 | * Deletes the given Frame on disk and removes the cached Frame if there is
|
---|
| 846 | * one. Also adds the deleted frame into the deletedFrames frameset.
|
---|
| 847 | *
|
---|
| 848 | * @param toDelete
|
---|
| 849 | * The Frame to be deleted
|
---|
[162] | 850 | * @return The name the deleted frame was changed to, or null if the delete
|
---|
| 851 | * failed
|
---|
[4] | 852 | */
|
---|
[154] | 853 | public static String DeleteFrame(Frame toDelete) throws IOException,
|
---|
[97] | 854 | SecurityException {
|
---|
[1188] | 855 | if (toDelete == null) {
|
---|
[154] | 856 | return null;
|
---|
[1188] | 857 | }
|
---|
[7] | 858 |
|
---|
[72] | 859 | // Dont delete the zero frame
|
---|
[80] | 860 | if (toDelete.getNumber() == 0) {
|
---|
[97] | 861 | throw new SecurityException("Deleting a zero frame is illegal");
|
---|
[72] | 862 | }
|
---|
| 863 |
|
---|
[298] | 864 | // Dont delete the zero frame
|
---|
| 865 | if (!toDelete.isLocal()) {
|
---|
| 866 | throw new SecurityException("Attempted to delete remote frame");
|
---|
| 867 | }
|
---|
| 868 |
|
---|
[4] | 869 | SaveFrame(toDelete);
|
---|
| 870 |
|
---|
[97] | 871 | // Copy deleted frames to the DeletedFrames frameset
|
---|
[4] | 872 | // get the last used frame in the destination frameset
|
---|
| 873 | final String DELETED_FRAMES = "DeletedFrames";
|
---|
| 874 | int lastNumber = FrameIO.getLastNumber(DELETED_FRAMES);
|
---|
| 875 | String framePath;
|
---|
[8] | 876 | try {
|
---|
[4] | 877 | // create the new frameset
|
---|
[298] | 878 | Frame one = FrameIO.CreateFrameset(DELETED_FRAMES, toDelete
|
---|
| 879 | .getPath());
|
---|
| 880 | framePath = one.getPath();
|
---|
[4] | 881 | lastNumber = 0;
|
---|
[8] | 882 | } catch (Exception e) {
|
---|
[4] | 883 | Frame zero = FrameIO.LoadFrame(DELETED_FRAMES + "0");
|
---|
[298] | 884 | framePath = zero.getPath();
|
---|
[4] | 885 | }
|
---|
| 886 |
|
---|
| 887 | // get the fill path to determine which file version it is
|
---|
[298] | 888 | String source = getFrameFullPathName(toDelete.getPath(), toDelete
|
---|
| 889 | .getName());
|
---|
[4] | 890 |
|
---|
[80] | 891 | String oldFrameName = toDelete.getName().toLowerCase();
|
---|
[4] | 892 | // Now save the frame in the new location
|
---|
| 893 | toDelete.setFrameset(DELETED_FRAMES);
|
---|
| 894 | toDelete.setFrameNumber(lastNumber + 1);
|
---|
[298] | 895 | toDelete.setPath(framePath);
|
---|
[154] | 896 | ForceSaveFrame(toDelete);
|
---|
[4] | 897 |
|
---|
[1188] | 898 | if (_Cache.containsKey(oldFrameName)) {
|
---|
[4] | 899 | _Cache.remove(oldFrameName);
|
---|
[1188] | 900 | }
|
---|
[4] | 901 |
|
---|
[601] | 902 | File del = new File(source);
|
---|
| 903 |
|
---|
| 904 | java.io.FileInputStream ff = new java.io.FileInputStream(del);
|
---|
| 905 | ff.close();
|
---|
| 906 |
|
---|
| 907 | if (del.delete()) {
|
---|
[154] | 908 | return toDelete.getName();
|
---|
| 909 | }
|
---|
[162] | 910 |
|
---|
[154] | 911 | return null;
|
---|
[4] | 912 | }
|
---|
| 913 |
|
---|
| 914 | /**
|
---|
| 915 | * Creates a new Frame in the given frameset and assigns it the given Title,
|
---|
| 916 | * which can be null. The newly created Frame is a copy of the frameset's .0
|
---|
| 917 | * file with the number updated based on the last recorded Frame name in the
|
---|
| 918 | * frameset's INF file.
|
---|
| 919 | *
|
---|
| 920 | * @param frameset
|
---|
| 921 | * The frameset to create the new Frame in
|
---|
| 922 | * @param frameTitle
|
---|
| 923 | * The title to assign to the newly created Frame (can be NULL).
|
---|
| 924 | * @return The newly created Frame.
|
---|
| 925 | */
|
---|
[298] | 926 | public static synchronized Frame CreateFrame(String frameset,
|
---|
| 927 | String frameTitle, String templateFrame) throws RuntimeException {
|
---|
[4] | 928 |
|
---|
[72] | 929 | if (!FrameIO.isValidFramesetName(frameset)) {
|
---|
[86] | 930 | throw new RuntimeException(frameset
|
---|
| 931 | + " is not a valid frameset name");
|
---|
[72] | 932 | }
|
---|
[86] | 933 |
|
---|
[4] | 934 | int next = -1;
|
---|
| 935 |
|
---|
| 936 | // disable caching of 0 frames
|
---|
[336] | 937 | // Mike says: Why is caching of 0 frames being disabled?
|
---|
| 938 | /*
|
---|
| 939 | * Especially since 0 frames are not event put into the cache in the
|
---|
| 940 | * frist place
|
---|
| 941 | */
|
---|
| 942 | // SuspendCache();
|
---|
| 943 | /*
|
---|
| 944 | * Suspending the cache causes infinate loops when trying to load a zero
|
---|
| 945 | * frame which has a ao which contains an v or av which contains a link
|
---|
| 946 | * to the ao frame
|
---|
| 947 | */
|
---|
| 948 |
|
---|
[72] | 949 | String zeroFrameName = frameset + "0";
|
---|
| 950 | Frame destFramesetZero = LoadFrame(zeroFrameName);
|
---|
| 951 | if (destFramesetZero == null) {
|
---|
| 952 | throw new RuntimeException(zeroFrameName + " could not be found");
|
---|
| 953 | }
|
---|
[64] | 954 |
|
---|
[4] | 955 | Frame template = null;
|
---|
[72] | 956 | if (templateFrame == null) {
|
---|
[4] | 957 | // load in frame.0
|
---|
[72] | 958 | template = destFramesetZero;
|
---|
| 959 | } else {
|
---|
[4] | 960 | template = LoadFrame(templateFrame);
|
---|
[72] | 961 | if (template == null) {
|
---|
| 962 | throw new RuntimeException("LinkTemplate " + templateFrame
|
---|
| 963 | + " could not be found");
|
---|
| 964 | }
|
---|
| 965 | }
|
---|
[4] | 966 |
|
---|
| 967 | ResumeCache();
|
---|
| 968 |
|
---|
| 969 | // read the next number from the INF file
|
---|
| 970 | try {
|
---|
[307] | 971 | next = ReadINF(destFramesetZero.getPath(), frameset, true);
|
---|
[4] | 972 | } catch (IOException ioe) {
|
---|
| 973 | ioe.printStackTrace();
|
---|
[72] | 974 | throw new RuntimeException("INF file could not be read");
|
---|
[4] | 975 | }
|
---|
| 976 |
|
---|
[1102] | 977 | // Remove the old frame from the cache then add the new one
|
---|
[86] | 978 | // TODO figure out some way that we can put both in the cache
|
---|
| 979 | _Cache.remove(template.getName().toLowerCase());
|
---|
[4] | 980 | // set the number and title of the new frame
|
---|
[86] | 981 | template.setName(frameset, ++next);
|
---|
[4] | 982 | template.setTitle(frameTitle);
|
---|
[86] | 983 | // _Cache.put(template.getName().toLowerCase(), template);
|
---|
[4] | 984 |
|
---|
| 985 | Logger.Log(Logger.SYSTEM, Logger.TDFC, "Creating new frame: "
|
---|
[80] | 986 | + template.getName() + " from TDFC");
|
---|
[4] | 987 |
|
---|
[655] | 988 | template.setOwner(UserSettings.UserName.get());
|
---|
[424] | 989 | template.reset();
|
---|
[4] | 990 | template.resetDateCreated();
|
---|
[427] | 991 |
|
---|
[4] | 992 | for (Item i : template.getItems()) {
|
---|
[1188] | 993 | if (ItemUtils.startsWithTag(i, ItemUtils.TAG_PARENT)) {
|
---|
[4] | 994 | i.setLink(null);
|
---|
[1188] | 995 | }
|
---|
[4] | 996 | }
|
---|
[162] | 997 |
|
---|
[924] | 998 | // do auto shrinking of the title IF not in twin frames mode and the title is not centred
|
---|
[130] | 999 | Item titleItem = template.getTitleItem();
|
---|
[4] | 1000 |
|
---|
[1102] | 1001 | if (!DisplayController.isTwinFramesOn() && !Justification.center.equals(((Text)titleItem).getJustification())) {
|
---|
[130] | 1002 | if ((titleItem.getX() + 1) < template.getNameItem().getX()) {
|
---|
[1244] | 1003 | int title_item_xr = titleItem.getX() + titleItem.getBoundsWidth(); // should really be '... -1'
|
---|
| 1004 | int frame_name_xl = template.getNameItem().getX();
|
---|
| 1005 | if (frame_name_xl < DisplayController.MINIMUM_FRAME_WIDTH) {
|
---|
| 1006 | frame_name_xl = DisplayController.MINIMUM_FRAME_WIDTH;
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
| 1009 | while ((titleItem.getSize() > Text.MINIMUM_FONT_SIZE) && title_item_xr > frame_name_xl) {
|
---|
[130] | 1010 | titleItem.setSize(titleItem.getSize() - 1);
|
---|
[1244] | 1011 | System.err.println("**** shrunk titleItem: " + titleItem + " to font size: " + titleItem.getSize());
|
---|
[130] | 1012 | }
|
---|
| 1013 | } else {
|
---|
| 1014 | System.out.println("Bad title x position: " + titleItem.getX());
|
---|
| 1015 | }
|
---|
| 1016 | }
|
---|
[362] | 1017 | // Assign a width to the title.
|
---|
[400] | 1018 | titleItem.setRightMargin(template.getNameItem().getX(), true);
|
---|
[416] | 1019 |
|
---|
[4] | 1020 | return template;
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
| 1023 | public static void DisableCache() {
|
---|
| 1024 | _UseCache = false;
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 | public static void EnableCache() {
|
---|
| 1028 | _UseCache = true;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 | public static void SuspendCache() {
|
---|
| 1032 | if (_UseCache) {
|
---|
| 1033 | DisableCache();
|
---|
| 1034 | _SuspendedCache = true;
|
---|
| 1035 | } else {
|
---|
| 1036 | _SuspendedCache = false;
|
---|
| 1037 | }
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | public static void ResumeCache() {
|
---|
| 1041 | if (_SuspendedCache) {
|
---|
| 1042 | EnableCache();
|
---|
| 1043 | _SuspendedCache = false;
|
---|
| 1044 | }
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
[1102] | 1047 | public static void RefreshCacheImages()
|
---|
| 1048 | {
|
---|
[67] | 1049 | SuspendCache();
|
---|
[1188] | 1050 | for (Frame frame : _Cache.values()) {
|
---|
| 1051 | frame.setBuffer(null);
|
---|
| 1052 | }
|
---|
[67] | 1053 | ResumeCache();
|
---|
| 1054 | }
|
---|
| 1055 |
|
---|
[4] | 1056 | /**
|
---|
| 1057 | * Creates a new frameset using the given name. This includes creating a new
|
---|
| 1058 | * subdirectory in the <code>FRAME_PATH</code> directory, Copying over the
|
---|
| 1059 | * default.0 frame from the default frameset, copying the .0 Frame to make a
|
---|
| 1060 | * .1 Frame, and creating the frameset's INF file.
|
---|
| 1061 | *
|
---|
| 1062 | * @param frameset
|
---|
| 1063 | * The name of the Frameset to create
|
---|
| 1064 | * @return The first Frame of the new Frameset (Frame.1)
|
---|
| 1065 | */
|
---|
[8] | 1066 | public static Frame CreateFrameset(String frameset, String path)
|
---|
| 1067 | throws Exception {
|
---|
[7] | 1068 | return CreateFrameset(frameset, path, false);
|
---|
[4] | 1069 | }
|
---|
| 1070 |
|
---|
| 1071 | /**
|
---|
| 1072 | * Tests if the given String is a 'proper' framename, that is, the String
|
---|
| 1073 | * must begin with a character, end with a number with 0 or more letters and
|
---|
[24] | 1074 | * numbers in between.
|
---|
[4] | 1075 | *
|
---|
| 1076 | * @param frameName
|
---|
| 1077 | * The String to test for validity as a frame name
|
---|
| 1078 | * @return True if the given framename is proper, false otherwise.
|
---|
| 1079 | */
|
---|
| 1080 | public static boolean isValidFrameName(String frameName) {
|
---|
| 1081 |
|
---|
[1188] | 1082 | if (frameName == null || frameName.length() < 2) {
|
---|
[4] | 1083 | return false;
|
---|
[1188] | 1084 | }
|
---|
[4] | 1085 |
|
---|
[24] | 1086 | int lastCharIndex = frameName.length() - 1;
|
---|
[4] | 1087 | // String must begin with a letter and end with a digit
|
---|
[24] | 1088 | if (!Character.isLetter(frameName.charAt(0))
|
---|
[1188] | 1089 | || !Character.isDigit(frameName.charAt(lastCharIndex))) {
|
---|
[4] | 1090 | return false;
|
---|
[1188] | 1091 | }
|
---|
[4] | 1092 |
|
---|
[7] | 1093 | // All the characters between first and last must be letters
|
---|
[24] | 1094 | // or digits
|
---|
| 1095 | for (int i = 1; i < lastCharIndex; i++) {
|
---|
[1188] | 1096 | if (!isValidFrameNameChar(frameName.charAt(i))) {
|
---|
[24] | 1097 | return false;
|
---|
[1188] | 1098 | }
|
---|
[4] | 1099 | }
|
---|
| 1100 | return true;
|
---|
| 1101 | }
|
---|
| 1102 |
|
---|
[427] | 1103 | private static boolean isValidFrameNameChar(char c) {
|
---|
[1242] | 1104 | return c == '-' || c == '.' || Character.isLetterOrDigit(c);
|
---|
[427] | 1105 | }
|
---|
| 1106 |
|
---|
[4] | 1107 | /**
|
---|
| 1108 | * Saves the given Frame to disk in the corresponding frameset directory.
|
---|
| 1109 | * This is the same as calling SaveFrame(toSave, true)
|
---|
| 1110 | *
|
---|
| 1111 | * @param toSave
|
---|
[105] | 1112 | * The Frame to save to disk
|
---|
[4] | 1113 | */
|
---|
| 1114 | public static String SaveFrame(Frame toSave) {
|
---|
| 1115 | return SaveFrame(toSave, true);
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
[376] | 1118 | /**
|
---|
| 1119 | * Saves a frame.
|
---|
[416] | 1120 | *
|
---|
| 1121 | * @param toSave
|
---|
| 1122 | * the frame to save
|
---|
| 1123 | * @param inc
|
---|
| 1124 | * true if the frames counter should be incremented
|
---|
[376] | 1125 | * @return the text content of the frame
|
---|
| 1126 | */
|
---|
[67] | 1127 | public static String SaveFrame(Frame toSave, boolean inc) {
|
---|
| 1128 | return SaveFrame(toSave, inc, true);
|
---|
| 1129 | }
|
---|
| 1130 |
|
---|
[4] | 1131 | /**
|
---|
| 1132 | * Saves the given Frame to disk in the corresponding frameset directory, if
|
---|
| 1133 | * inc is true then the saved frames counter is incremented, otherwise it is
|
---|
| 1134 | * untouched.
|
---|
| 1135 | *
|
---|
| 1136 | * @param toSave
|
---|
[1242] | 1137 | * The Frame to save to disk
|
---|
[4] | 1138 | * @param inc
|
---|
[1242] | 1139 | * True if the saved frames counter should be incremented, false otherwise.
|
---|
[67] | 1140 | * @param checkBackup
|
---|
[1242] | 1141 | * True if the frame should be checked for the back up tag
|
---|
[4] | 1142 | */
|
---|
[1242] | 1143 | public static String SaveFrame(Frame toSave, boolean inc, boolean checkBackup) {
|
---|
[162] | 1144 | // TODO When loading a frame maybe append onto the event history too-
|
---|
[1232] | 1145 | // with a break to indicate the end of a session
|
---|
[1242] | 1146 |
|
---|
[162] | 1147 | if (toSave == null || !toSave.hasChanged() || toSave.isSaved()) {
|
---|
[7] | 1148 | return "";
|
---|
[105] | 1149 | }
|
---|
[4] | 1150 |
|
---|
[86] | 1151 | // Dont save if the frame is protected and it exists
|
---|
[162] | 1152 | if (checkBackup && toSave.isReadOnly()) {
|
---|
[86] | 1153 | _Cache.remove(toSave.getName().toLowerCase());
|
---|
| 1154 | return "";
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
[416] | 1157 | /* Dont save the frame if it has the noSave tag */
|
---|
| 1158 | if (toSave.hasAnnotation("nosave")) {
|
---|
[1200] | 1159 | Actions.LegacyPerformActionCatchErrors(toSave, null, "Restore");
|
---|
[416] | 1160 | return "";
|
---|
| 1161 | }
|
---|
| 1162 |
|
---|
[298] | 1163 | // Save frame that is not local through the Networking classes
|
---|
[306] | 1164 | if (!toSave.isLocal()) {
|
---|
| 1165 | return FrameShare.getInstance().saveFrame(toSave);
|
---|
| 1166 | }
|
---|
[298] | 1167 |
|
---|
[416] | 1168 | /* Format the frame if it has the autoFormat tag */
|
---|
| 1169 | if (toSave.hasAnnotation("autoformat")) {
|
---|
[1200] | 1170 | Actions.LegacyPerformActionCatchErrors(toSave, null, "Format");
|
---|
[415] | 1171 | }
|
---|
[416] | 1172 |
|
---|
[298] | 1173 | /**
|
---|
| 1174 | * Get the full path only to determine which format to use for saving
|
---|
| 1175 | * the frame. At this stage use Exp format for saving Exp frames only.
|
---|
| 1176 | * Later this will be changed so that KMS frames will be updated to the
|
---|
| 1177 | * Exp format.
|
---|
| 1178 | */
|
---|
| 1179 | String fullPath = getFrameFullPathName(toSave.getPath(), toSave
|
---|
| 1180 | .getName());
|
---|
| 1181 |
|
---|
[67] | 1182 | // Check if the frame exists
|
---|
| 1183 | if (checkBackup && fullPath == null) {
|
---|
| 1184 | // The first time a frame with the backup tag is saved, dont back it
|
---|
| 1185 | // up
|
---|
| 1186 | checkBackup = false;
|
---|
| 1187 | }
|
---|
[601] | 1188 |
|
---|
| 1189 | FrameWriter writer = null;
|
---|
[589] | 1190 | int savedVersion;
|
---|
[4] | 1191 | try {
|
---|
| 1192 | // if its a new frame or an existing Exp frame...
|
---|
[1242] | 1193 | if (fullPath == null || fullPath.endsWith(ExpReader.EXTENTION)) {
|
---|
[1283] | 1194 | if (toSave.getNumber() != AuthenticatorBrowser.CREDENTIALS_FRAME &&
|
---|
[1227] | 1195 | toSave.getEncryptionLabel() != null) {
|
---|
| 1196 | writer = new EncryptedExpWriter(toSave.getEncryptionLabel());
|
---|
[1219] | 1197 | savedVersion = EncryptedExpReader.getVersion(fullPath);
|
---|
| 1198 | } else {
|
---|
| 1199 | writer = new ExpWriter();
|
---|
| 1200 | savedVersion = ExpReader.getVersion(fullPath);
|
---|
| 1201 | }
|
---|
[1242] | 1202 |
|
---|
| 1203 | // Is the file this would be saved to a redirect?
|
---|
| 1204 | String redirectTo = ExpReader.redirectTo(fullPath);
|
---|
| 1205 | if (redirectTo != null) {
|
---|
| 1206 | String redirectedPath = toSave.getFramePathReal();
|
---|
| 1207 | writer.setOutputLocation(redirectedPath);
|
---|
| 1208 | }
|
---|
| 1209 |
|
---|
[601] | 1210 | } else {
|
---|
| 1211 | writer = new KMSWriter();
|
---|
| 1212 | savedVersion = KMSReader.getVersion(fullPath);
|
---|
| 1213 | }
|
---|
[1232] | 1214 |
|
---|
[348] | 1215 | // Check if the frame doesnt exist
|
---|
[416] | 1216 | // if (savedVersion < 0) {
|
---|
| 1217 | // /*
|
---|
| 1218 | // * This will happen if the user has two Expeditee's running at
|
---|
| 1219 | // * once and closes the first. When the second one closes the
|
---|
| 1220 | // * messages directory will have been deleted.
|
---|
| 1221 | // */
|
---|
| 1222 | // MessageBay
|
---|
| 1223 | // .errorMessage("Could not save frame that does not exist: "
|
---|
| 1224 | // + toSave.getName());
|
---|
| 1225 | // return null;
|
---|
| 1226 | // }
|
---|
[348] | 1227 |
|
---|
[67] | 1228 | // Check if we are trying to save an out of date version
|
---|
[1219] | 1229 | String framesetName = toSave.getFramesetName();
|
---|
| 1230 | boolean isBayFrameset =
|
---|
| 1231 | framesetName.equalsIgnoreCase(MessageBay.MESSAGES_FRAMESET_NAME) ||
|
---|
| 1232 | framesetName.equalsIgnoreCase(MailBay.EXPEDITEE_MAIL_FRAMESET_NAME);
|
---|
[1242] | 1233 | long frameLastModify = toSave.getLastModifyPrecise();
|
---|
| 1234 | long fileLastModify = new File(toSave.getFramePathReal()).lastModified();
|
---|
| 1235 | boolean fileModifyConflict = fileLastModify > frameLastModify && !isBayFrameset;
|
---|
| 1236 | boolean versionConflict = savedVersion > toSave.getVersion() && !isBayFrameset;
|
---|
| 1237 | if (fileModifyConflict || versionConflict) {
|
---|
[67] | 1238 | // remove this frame from the cache if it is there
|
---|
| 1239 | // This will make sure links to the original are set correctly
|
---|
[80] | 1240 | _Cache.remove(toSave.getName().toLowerCase());
|
---|
[298] | 1241 | int nextnum = ReadINF(toSave.getPath(), toSave
|
---|
[307] | 1242 | .getFramesetName(), false) + 1;
|
---|
[67] | 1243 | SuspendCache();
|
---|
[80] | 1244 | Frame original = LoadFrame(toSave.getName());
|
---|
[67] | 1245 | toSave.setFrameNumber(nextnum);
|
---|
| 1246 | ResumeCache();
|
---|
| 1247 | // Put the modified version in the cache
|
---|
[108] | 1248 | addToCache(toSave);
|
---|
[67] | 1249 | // Show the messages alerting the user
|
---|
| 1250 | Text originalMessage = new Text(-1);
|
---|
[121] | 1251 | originalMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
[1242] | 1252 | StringBuilder message = new StringBuilder(original.getName()
|
---|
| 1253 | + " was updated by another user. ");
|
---|
| 1254 | if (fileModifyConflict) {
|
---|
| 1255 | message.append("{ File modify conflict }");
|
---|
[1325] | 1256 | System.err.println("Thread name: " + Thread.currentThread().getName());
|
---|
| 1257 | StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
---|
| 1258 | for (StackTraceElement ste: stackTrace) {
|
---|
| 1259 | System.err.println(ste.toString());
|
---|
| 1260 | }
|
---|
[1242] | 1261 | }
|
---|
| 1262 | if (versionConflict) {
|
---|
| 1263 | message.append("{ Version conflict }");
|
---|
| 1264 | }
|
---|
| 1265 | originalMessage.setText(message.toString());
|
---|
[80] | 1266 | originalMessage.setLink(original.getName());
|
---|
[67] | 1267 | Text yourMessage = new Text(-1);
|
---|
[121] | 1268 | yourMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
[67] | 1269 | yourMessage.setText("Your version was renamed "
|
---|
[80] | 1270 | + toSave.getName());
|
---|
| 1271 | yourMessage.setLink(toSave.getName());
|
---|
[121] | 1272 | MessageBay.displayMessage(originalMessage);
|
---|
| 1273 | MessageBay.displayMessage(yourMessage);
|
---|
[1242] | 1274 | EcosystemManager.getMiscManager().beep();
|
---|
[67] | 1275 | } else if (checkBackup
|
---|
[70] | 1276 | && ItemUtils.ContainsExactTag(toSave.getItems(),
|
---|
[4] | 1277 | ItemUtils.TAG_BACKUP)) {
|
---|
| 1278 | SuspendCache();
|
---|
[427] | 1279 | String oldFramesetName = toSave.getFramesetName() + "-old";
|
---|
| 1280 |
|
---|
[80] | 1281 | Frame original = LoadFrame(toSave.getName());
|
---|
[1188] | 1282 | if (original == null) {
|
---|
[4] | 1283 | original = toSave;
|
---|
[1188] | 1284 | }
|
---|
[80] | 1285 | int orignum = original.getNumber();
|
---|
[4] | 1286 |
|
---|
[427] | 1287 | int nextnum = -1;
|
---|
| 1288 | try {
|
---|
| 1289 | nextnum = ReadINF(toSave.getPath(), oldFramesetName, false) + 1;
|
---|
| 1290 | } catch (RuntimeException e) {
|
---|
| 1291 | try {
|
---|
| 1292 | CreateFrameset(oldFramesetName, toSave.getPath());
|
---|
| 1293 | nextnum = 1;
|
---|
| 1294 | } catch (Exception e1) {
|
---|
| 1295 | e1.printStackTrace();
|
---|
| 1296 | }
|
---|
[429] | 1297 | // e.printStackTrace();
|
---|
[427] | 1298 | }
|
---|
[429] | 1299 |
|
---|
[427] | 1300 | if (nextnum > 0) {
|
---|
| 1301 | original.setFrameset(oldFramesetName);
|
---|
| 1302 | original.setFrameNumber(nextnum);
|
---|
[454] | 1303 | original.setPermission(new PermissionPair(UserAppliedPermission.copy));
|
---|
[427] | 1304 | original.change();
|
---|
| 1305 | SaveFrame(original, false, false);
|
---|
| 1306 | }
|
---|
[4] | 1307 |
|
---|
[70] | 1308 | Item i = ItemUtils.FindExactTag(toSave.getItems(),
|
---|
[4] | 1309 | ItemUtils.TAG_BACKUP);
|
---|
[80] | 1310 | i.setLink(original.getName());
|
---|
[4] | 1311 | toSave.setFrameNumber(orignum);
|
---|
| 1312 | ResumeCache();
|
---|
| 1313 | }
|
---|
[105] | 1314 |
|
---|
[162] | 1315 | // int oldMode = FrameGraphics.getMode();
|
---|
| 1316 | // if (oldMode != FrameGraphics.MODE_XRAY)
|
---|
| 1317 | // FrameGraphics.setMode(FrameGraphics.MODE_XRAY, true);
|
---|
[1242] | 1318 |
|
---|
[4] | 1319 | writer.writeFrame(toSave);
|
---|
[162] | 1320 | // FrameGraphics.setMode(oldMode, true);
|
---|
[4] | 1321 | toSave.setSaved();
|
---|
[1242] | 1322 |
|
---|
| 1323 | // Update general stuff about frame
|
---|
| 1324 | setSavedProperties(toSave);
|
---|
| 1325 |
|
---|
[105] | 1326 | if (inc) {
|
---|
[80] | 1327 | SessionStats.SavedFrame(toSave.getName());
|
---|
[105] | 1328 | }
|
---|
[4] | 1329 |
|
---|
| 1330 | // avoid out-of-sync frames (when in TwinFrames mode)
|
---|
[1188] | 1331 | if (_Cache.containsKey(toSave.getName().toLowerCase())) {
|
---|
[108] | 1332 | addToCache(toSave);
|
---|
[1188] | 1333 | }
|
---|
[4] | 1334 |
|
---|
[86] | 1335 | Logger.Log(Logger.SYSTEM, Logger.SAVE, "Saving " + toSave.getName()
|
---|
| 1336 | + " to disk.");
|
---|
[4] | 1337 |
|
---|
| 1338 | // check that the INF file is not out of date
|
---|
[307] | 1339 | int last = ReadINF(toSave.getPath(), toSave.getFramesetName(),
|
---|
| 1340 | false);
|
---|
[1188] | 1341 | if (last <= toSave.getNumber()) {
|
---|
[298] | 1342 | WriteINF(toSave.getPath(), toSave.getFramesetName(), toSave
|
---|
[80] | 1343 | .getName());
|
---|
[1188] | 1344 | }
|
---|
[4] | 1345 |
|
---|
| 1346 | // check if this was the profile frame (and thus needs
|
---|
| 1347 | // re-parsing)
|
---|
[95] | 1348 | if (isProfileFrame(toSave)) {
|
---|
[1242] | 1349 | Frame profile = FrameIO.LoadFrame(toSave.getFramesetName() + "1");
|
---|
[97] | 1350 | assert (profile != null);
|
---|
[95] | 1351 | FrameUtils.ParseProfile(profile);
|
---|
[4] | 1352 | }
|
---|
| 1353 | } catch (IOException ioe) {
|
---|
[311] | 1354 | ioe.printStackTrace();
|
---|
[95] | 1355 | ioe.getStackTrace();
|
---|
[4] | 1356 | Logger.Log(ioe);
|
---|
[7] | 1357 | return null;
|
---|
[4] | 1358 | }
|
---|
[1242] | 1359 | toSave.notifyObservers(false);
|
---|
[4] | 1360 |
|
---|
| 1361 | return writer.getFileContents();
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
[95] | 1364 | /**
|
---|
[997] | 1365 | * Saves the given Frame to disk in the corresponding frameset directory as a RESTORE, if
|
---|
| 1366 | * inc is true then the saved frames counter is incremented, otherwise it is
|
---|
| 1367 | * untouched.
|
---|
| 1368 | *
|
---|
| 1369 | * @param toSave
|
---|
| 1370 | * The Frame to save to disk as the DEFAULT COPY
|
---|
| 1371 | * @param inc
|
---|
| 1372 | * True if the saved frames counter should be incremented, false
|
---|
| 1373 | * otherwise.
|
---|
| 1374 | * @param checkBackup
|
---|
| 1375 | * True if the frame should be checked for the back up tag
|
---|
| 1376 | */
|
---|
| 1377 | public static String SaveFrameAsRestore(Frame toSave, boolean inc,
|
---|
| 1378 | boolean checkBackup) {
|
---|
| 1379 |
|
---|
| 1380 | String sf = SaveFrame(toSave, inc, checkBackup);
|
---|
| 1381 | String fullPath = getFrameFullPathName(toSave.getPath(), toSave
|
---|
| 1382 | .getName());
|
---|
| 1383 | //System.out.println(fullPath);
|
---|
| 1384 | String restoreVersion = fullPath + ".restore";
|
---|
| 1385 | File source = new File(fullPath);
|
---|
| 1386 | File dest = new File(restoreVersion);
|
---|
| 1387 |
|
---|
| 1388 | FileChannel inputChannel = null;
|
---|
| 1389 | FileChannel outputChannel = null;
|
---|
| 1390 |
|
---|
| 1391 | try{
|
---|
| 1392 | FileInputStream source_fis = new FileInputStream(source);
|
---|
| 1393 | inputChannel = source_fis.getChannel();
|
---|
| 1394 |
|
---|
| 1395 | FileOutputStream dest_fos = new FileOutputStream(dest);
|
---|
| 1396 | outputChannel = dest_fos.getChannel();
|
---|
| 1397 |
|
---|
| 1398 | outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
|
---|
| 1399 | inputChannel.close();
|
---|
| 1400 | outputChannel.close();
|
---|
| 1401 | source_fis.close();
|
---|
| 1402 | dest_fos.close();
|
---|
| 1403 | }
|
---|
| 1404 | catch(Exception e){
|
---|
| 1405 | e.printStackTrace();
|
---|
| 1406 | }
|
---|
| 1407 |
|
---|
| 1408 | return sf;
|
---|
| 1409 | }
|
---|
| 1410 |
|
---|
| 1411 | /**
|
---|
[108] | 1412 | * @param toAdd
|
---|
| 1413 | */
|
---|
| 1414 | public static void addToCache(Frame toAdd) {
|
---|
| 1415 | _Cache.put(toAdd.getName().toLowerCase(), toAdd);
|
---|
| 1416 | }
|
---|
[1242] | 1417 |
|
---|
| 1418 | public static void ClearCache() {
|
---|
| 1419 | _Cache.clear();
|
---|
| 1420 | }
|
---|
[108] | 1421 |
|
---|
| 1422 | /**
|
---|
[95] | 1423 | * Checks if a frame is in the current user profile frameset.
|
---|
[97] | 1424 | *
|
---|
| 1425 | * @param toCheck
|
---|
| 1426 | * the frame to check
|
---|
[95] | 1427 | * @return true if the frame is in the current user profile frameset
|
---|
| 1428 | */
|
---|
[1102] | 1429 | public static boolean isProfileFrame(Frame toCheck)
|
---|
| 1430 | {
|
---|
[1188] | 1431 | if (toCheck.getNumber() == 0) {
|
---|
| 1432 | return false;
|
---|
| 1433 | }
|
---|
[1102] | 1434 |
|
---|
[348] | 1435 | return toCheck.getPath().equals(PROFILE_PATH);
|
---|
[416] | 1436 | // return toCheck.getFramesetName()
|
---|
| 1437 | // .equalsIgnoreCase(UserSettings.ProfileName);
|
---|
[95] | 1438 | }
|
---|
| 1439 |
|
---|
[1102] | 1440 | public static Frame LoadProfile(String userName)
|
---|
| 1441 | {
|
---|
[1200] | 1442 | final String profilesLoc = System.getProperty("profiles.loc");
|
---|
| 1443 | if (profilesLoc != null) {
|
---|
| 1444 | return LoadFrame(userName + "1", profilesLoc);
|
---|
| 1445 | } else {
|
---|
| 1446 | return LoadFrame(userName + "1");
|
---|
| 1447 | }
|
---|
[4] | 1448 | }
|
---|
| 1449 |
|
---|
[1270] | 1450 | public static Frame CreateNewProfile(String username, Map<String, Setting> initialSettings, Map<String, Consumer<Frame>> toNotifyOnSet) throws InvalidFramesetNameException, ExistingFramesetException {
|
---|
[23] | 1451 | Frame profile = CreateFrameset(username, PROFILE_PATH, true);
|
---|
[1270] | 1452 | FrameUtils.CreateDefaultProfile(username, profile, initialSettings, toNotifyOnSet);
|
---|
[4] | 1453 | return profile;
|
---|
| 1454 | }
|
---|
| 1455 |
|
---|
| 1456 | /**
|
---|
| 1457 | * Reads the INF file that corresponds to the given Frame name
|
---|
| 1458 | *
|
---|
| 1459 | * @param framename
|
---|
| 1460 | * The Frame to lookup the INF file for
|
---|
| 1461 | * @throws IOException
|
---|
| 1462 | * Any exceptions encountered by the BufferedReader used to read
|
---|
| 1463 | * the INF.
|
---|
| 1464 | */
|
---|
[307] | 1465 | public static int ReadINF(String path, String frameset, boolean update)
|
---|
| 1466 | throws IOException {
|
---|
[4] | 1467 | assert (!frameset.endsWith("."));
|
---|
[307] | 1468 | try {
|
---|
| 1469 | // read INF
|
---|
| 1470 | BufferedReader reader;
|
---|
| 1471 | try {
|
---|
| 1472 | // Check on the local drive
|
---|
| 1473 | reader = new BufferedReader(new FileReader(path
|
---|
| 1474 | + frameset.toLowerCase() + File.separator
|
---|
| 1475 | + INF_FILENAME));
|
---|
| 1476 | } catch (Exception e) {
|
---|
| 1477 | reader = new BufferedReader(new FileReader(path
|
---|
| 1478 | + frameset.toLowerCase() + File.separator
|
---|
| 1479 | + frameset.toLowerCase() + ".inf"));
|
---|
| 1480 | }
|
---|
| 1481 | String inf = reader.readLine();
|
---|
| 1482 | reader.close();
|
---|
[4] | 1483 |
|
---|
[307] | 1484 | int next = Conversion.getFrameNumber(inf);
|
---|
| 1485 | // update INF file
|
---|
| 1486 | if (update) {
|
---|
| 1487 | try {
|
---|
| 1488 | WriteINF(path, frameset, frameset + (next + 1));
|
---|
| 1489 | } catch (IOException ioe) {
|
---|
| 1490 | ioe.printStackTrace();
|
---|
| 1491 | Logger.Log(ioe);
|
---|
| 1492 | }
|
---|
| 1493 | }
|
---|
| 1494 | return next;
|
---|
[4] | 1495 | } catch (Exception e) {
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
[307] | 1498 | // Check peers
|
---|
| 1499 | return FrameShare.getInstance().getInfNumber(path, frameset, update);
|
---|
[4] | 1500 | }
|
---|
| 1501 |
|
---|
| 1502 | /**
|
---|
| 1503 | * Writes the given String out to the INF file corresponding to the current
|
---|
| 1504 | * frameset.
|
---|
| 1505 | *
|
---|
| 1506 | * @param toWrite
|
---|
| 1507 | * The String to write to the file.
|
---|
| 1508 | * @throws IOException
|
---|
| 1509 | * Any exception encountered by the BufferedWriter.
|
---|
| 1510 | */
|
---|
[307] | 1511 | public static void WriteINF(String path, String frameset, String frameName)
|
---|
[4] | 1512 | throws IOException {
|
---|
[307] | 1513 | try {
|
---|
| 1514 | assert (!frameset.endsWith("."));
|
---|
[4] | 1515 |
|
---|
[307] | 1516 | path += frameset.toLowerCase() + File.separator + INF_FILENAME;
|
---|
[4] | 1517 |
|
---|
[307] | 1518 | BufferedWriter writer = new BufferedWriter(new FileWriter(path));
|
---|
| 1519 | writer.write(frameName);
|
---|
| 1520 | writer.close();
|
---|
| 1521 | } catch (Exception e) {
|
---|
| 1522 |
|
---|
| 1523 | }
|
---|
[4] | 1524 | }
|
---|
| 1525 |
|
---|
| 1526 | public static boolean FrameIsCached(String name) {
|
---|
| 1527 | return _Cache.containsKey(name);
|
---|
| 1528 | }
|
---|
[298] | 1529 |
|
---|
[206] | 1530 | /**
|
---|
| 1531 | * Gets a frame from the cache.
|
---|
| 1532 | *
|
---|
| 1533 | * @param name
|
---|
[298] | 1534 | * The frame to get from the cache
|
---|
[206] | 1535 | *
|
---|
[298] | 1536 | * @return The frame from cache. Null if not cached.
|
---|
[206] | 1537 | */
|
---|
| 1538 | public static Frame FrameFromCache(String name) {
|
---|
| 1539 | return _Cache.get(name);
|
---|
| 1540 | }
|
---|
[4] | 1541 |
|
---|
[12] | 1542 | public static String ConvertToValidFramesetName(String toValidate) {
|
---|
[64] | 1543 | assert (toValidate != null && toValidate.length() > 0);
|
---|
| 1544 |
|
---|
[24] | 1545 | StringBuffer result = new StringBuffer();
|
---|
[4] | 1546 |
|
---|
[64] | 1547 | if (Character.isDigit(toValidate.charAt(0))) {
|
---|
[24] | 1548 | result.append(FRAME_NAME_LAST_CHAR);
|
---|
| 1549 | }
|
---|
[64] | 1550 |
|
---|
[24] | 1551 | boolean capital = false;
|
---|
[64] | 1552 | for (int i = 0; i < toValidate.length()
|
---|
| 1553 | && result.length() < MAX_NAME_LENGTH; i++) {
|
---|
[4] | 1554 | char cur = toValidate.charAt(i);
|
---|
| 1555 |
|
---|
| 1556 | // capitalize all characters after spaces
|
---|
[12] | 1557 | if (Character.isLetterOrDigit(cur)) {
|
---|
[4] | 1558 | if (capital) {
|
---|
| 1559 | capital = false;
|
---|
[24] | 1560 | result.append(Character.toUpperCase(cur));
|
---|
[1188] | 1561 | } else {
|
---|
[24] | 1562 | result.append(cur);
|
---|
[1188] | 1563 | }
|
---|
[12] | 1564 | } else {
|
---|
| 1565 | capital = true;
|
---|
[4] | 1566 | }
|
---|
| 1567 | }
|
---|
[24] | 1568 | assert (result.length() > 0);
|
---|
| 1569 | int lastCharIndex = result.length() - 1;
|
---|
[64] | 1570 | if (!Character.isLetter(result.charAt(lastCharIndex))) {
|
---|
[1188] | 1571 | if (lastCharIndex == MAX_NAME_LENGTH - 1) {
|
---|
[64] | 1572 | result.setCharAt(lastCharIndex, FRAME_NAME_LAST_CHAR);
|
---|
[1188] | 1573 | } else {
|
---|
[24] | 1574 | result.append(FRAME_NAME_LAST_CHAR);
|
---|
[1188] | 1575 | }
|
---|
[24] | 1576 | }
|
---|
[64] | 1577 |
|
---|
[72] | 1578 | assert (isValidFramesetName(result.toString()));
|
---|
[24] | 1579 | return result.toString();
|
---|
[4] | 1580 | }
|
---|
| 1581 |
|
---|
[72] | 1582 | public static Frame CreateNewFrame(Item linker) throws RuntimeException {
|
---|
[80] | 1583 | String title = linker.getName();
|
---|
[4] | 1584 |
|
---|
[80] | 1585 | String templateLink = linker.getAbsoluteLinkTemplate();
|
---|
| 1586 | String framesetLink = linker.getAbsoluteLinkFrameset();
|
---|
[1102] | 1587 | String frameset = (framesetLink != null ? framesetLink : DisplayController
|
---|
[72] | 1588 | .getCurrentFrame().getFramesetName());
|
---|
[4] | 1589 |
|
---|
| 1590 | Frame newFrame = FrameIO.CreateFrame(frameset, title, templateLink);
|
---|
| 1591 | return newFrame;
|
---|
| 1592 | }
|
---|
[734] | 1593 |
|
---|
| 1594 | public static Frame CreateNewFrame(Item linker, OnNewFrameAction action) throws RuntimeException {
|
---|
| 1595 | Frame newFrame = FrameIO.CreateNewFrame(linker);
|
---|
[1188] | 1596 | if(action != null) {
|
---|
| 1597 | action.exec(linker, newFrame);
|
---|
| 1598 | }
|
---|
[734] | 1599 | return newFrame;
|
---|
| 1600 | }
|
---|
[4] | 1601 |
|
---|
| 1602 | /**
|
---|
| 1603 | * Creates a new Frameset on disk, including a .0, .1, and .inf files. The
|
---|
| 1604 | * Default.0 frame is copied to make the initial .0 and .1 Frames
|
---|
| 1605 | *
|
---|
| 1606 | * @param name
|
---|
| 1607 | * The Frameset name to use
|
---|
| 1608 | * @return The name of the first Frame in the newly created Frameset (the .1
|
---|
| 1609 | * frame)
|
---|
| 1610 | */
|
---|
[8] | 1611 | public static Frame CreateNewFrameset(String name) throws Exception {
|
---|
[1102] | 1612 | String path = DisplayController.getCurrentFrame().getPath();
|
---|
[4] | 1613 |
|
---|
| 1614 | // if current frameset is profile directory change it to framesets
|
---|
| 1615 | if (path.equals(FrameIO.PROFILE_PATH)) {
|
---|
| 1616 | path = FrameIO.FRAME_PATH;
|
---|
| 1617 | }
|
---|
| 1618 |
|
---|
[429] | 1619 | Frame newFrame = FrameIO.CreateFrameset(name, path);
|
---|
| 1620 |
|
---|
| 1621 | if (newFrame == null) {
|
---|
| 1622 | // Cant create directories if the path is readonly or there is no
|
---|
| 1623 | // space available
|
---|
| 1624 | newFrame = FrameIO.CreateFrameset(name, FrameIO.FRAME_PATH);
|
---|
| 1625 | }
|
---|
| 1626 |
|
---|
| 1627 | if (newFrame == null) {
|
---|
| 1628 | // TODO handle running out of disk space here
|
---|
| 1629 | }
|
---|
| 1630 |
|
---|
| 1631 | return newFrame;
|
---|
[4] | 1632 | }
|
---|
| 1633 |
|
---|
| 1634 | /**
|
---|
| 1635 | *
|
---|
| 1636 | * @param frameset
|
---|
| 1637 | * @return
|
---|
| 1638 | */
|
---|
| 1639 | public static int getLastNumber(String frameset) { // Rob thinks it might
|
---|
| 1640 | // have been
|
---|
| 1641 | // GetHighestNumExFrame
|
---|
| 1642 | // TODO minimise the number of frames being read in!!
|
---|
| 1643 | int num = -1;
|
---|
| 1644 |
|
---|
| 1645 | Frame zero = LoadFrame(frameset + "0");
|
---|
| 1646 |
|
---|
| 1647 | // the frameset does not exist (or has no 0 frame)
|
---|
[1188] | 1648 | if (zero == null) {
|
---|
[4] | 1649 | return -1;
|
---|
[1188] | 1650 | }
|
---|
[4] | 1651 |
|
---|
| 1652 | try {
|
---|
[307] | 1653 | num = ReadINF(zero.getPath(), frameset, false);
|
---|
[4] | 1654 | } catch (IOException e) {
|
---|
| 1655 | // TODO Auto-generated catch block
|
---|
| 1656 | // e.printStackTrace();
|
---|
| 1657 | }
|
---|
| 1658 |
|
---|
| 1659 | /*
|
---|
| 1660 | * Michael doesnt think the code below is really needed... it will just
|
---|
| 1661 | * slow things down when we are reading frames over a network***** for (;
|
---|
| 1662 | * num >= 0; num--) { System.out.println("This code is loading frames to
|
---|
| 1663 | * find the highest existing frame..."); if (LoadFrame(frameset + num) !=
|
---|
| 1664 | * null) break; }
|
---|
| 1665 | */
|
---|
| 1666 |
|
---|
| 1667 | return num;
|
---|
| 1668 | }
|
---|
| 1669 |
|
---|
[97] | 1670 | /**
|
---|
| 1671 | * Checks if a given frameset is accessable.
|
---|
| 1672 | *
|
---|
| 1673 | * @param framesetName
|
---|
| 1674 | * @return
|
---|
| 1675 | */
|
---|
[1280] | 1676 | public static boolean canAccessFrameset(String framesetName) {
|
---|
[97] | 1677 | framesetName = framesetName.toLowerCase();
|
---|
[1244] | 1678 | for (String path : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
[1280] | 1679 | if (canAccessFrameset(framesetName, Paths.get(path))) {
|
---|
[97] | 1680 | return true;
|
---|
[1188] | 1681 | }
|
---|
[97] | 1682 | }
|
---|
| 1683 | return false;
|
---|
[7] | 1684 | }
|
---|
[1280] | 1685 |
|
---|
| 1686 | public static boolean canAccessFrameset(String framesetName, Path path) {
|
---|
| 1687 | File framesetDir = path.resolve(framesetName).toFile();
|
---|
| 1688 | if (framesetDir.exists() && framesetDir.isDirectory()) {
|
---|
| 1689 | return true;
|
---|
| 1690 | } else {
|
---|
| 1691 | return false;
|
---|
| 1692 | }
|
---|
| 1693 | }
|
---|
[7] | 1694 |
|
---|
[1242] | 1695 | public static Frame CreateFrameset(String frameset, String path, boolean recreate) throws InvalidFramesetNameException, ExistingFramesetException {
|
---|
[7] | 1696 | String conversion = frameset + " --> ";
|
---|
| 1697 |
|
---|
[1188] | 1698 | if (!isValidFramesetName(frameset)) {
|
---|
[1242] | 1699 | throw new InvalidFramesetNameException(frameset);
|
---|
[1188] | 1700 | }
|
---|
[24] | 1701 |
|
---|
[97] | 1702 | if (!recreate && FrameIO.canAccessFrameset(frameset)) {
|
---|
[8] | 1703 | throw new ExistingFramesetException(frameset);
|
---|
[7] | 1704 | }
|
---|
| 1705 |
|
---|
| 1706 | conversion += frameset;
|
---|
| 1707 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Frameset Name: "
|
---|
| 1708 | + conversion);
|
---|
| 1709 | conversion = frameset;
|
---|
| 1710 |
|
---|
| 1711 | /**
|
---|
| 1712 | * TODO: Update this to exclude any\all invalid filename characters
|
---|
| 1713 | */
|
---|
| 1714 | // ignore annotation character
|
---|
[1188] | 1715 | if (frameset.startsWith("@")) {
|
---|
[7] | 1716 | frameset = frameset.substring(1);
|
---|
[1188] | 1717 | }
|
---|
[7] | 1718 |
|
---|
| 1719 | conversion += " --> " + frameset;
|
---|
| 1720 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Name: " + conversion);
|
---|
| 1721 |
|
---|
| 1722 | // create the new Frameset directory
|
---|
| 1723 | File dir = new File(path + frameset.toLowerCase() + File.separator);
|
---|
| 1724 |
|
---|
[429] | 1725 | // If the directory doesnt already exist then create it...
|
---|
| 1726 | if (!dir.exists()) {
|
---|
| 1727 | // If the directory couldnt be created, then there is something
|
---|
| 1728 | // wrong... ie. The disk is full.
|
---|
| 1729 | if (!dir.mkdirs()) {
|
---|
| 1730 | return null;
|
---|
| 1731 | }
|
---|
| 1732 | }
|
---|
[7] | 1733 |
|
---|
| 1734 | // create the new INF file
|
---|
| 1735 | try {
|
---|
[25] | 1736 | WriteINF(path, frameset, frameset + '1');
|
---|
[7] | 1737 | } catch (IOException ioe) {
|
---|
| 1738 | ioe.printStackTrace();
|
---|
| 1739 | Logger.Log(ioe);
|
---|
| 1740 | }
|
---|
| 1741 |
|
---|
| 1742 | SuspendCache();
|
---|
| 1743 | // copy the default .0 and .1 files
|
---|
[98] | 1744 | Frame base = null;
|
---|
[7] | 1745 | try {
|
---|
[778] | 1746 | base = LoadFrame(TemplateSettings.DefaultFrame.get());
|
---|
[7] | 1747 | } catch (Exception e) {
|
---|
[98] | 1748 | }
|
---|
| 1749 | // The frame may not be accessed for various reasons... in all these
|
---|
| 1750 | // cases just create a new one
|
---|
| 1751 | if (base == null) {
|
---|
[7] | 1752 | base = new Frame();
|
---|
| 1753 | }
|
---|
[98] | 1754 |
|
---|
[7] | 1755 | ResumeCache();
|
---|
| 1756 |
|
---|
[424] | 1757 | base.reset();
|
---|
[7] | 1758 | base.resetDateCreated();
|
---|
| 1759 | base.setFrameset(frameset);
|
---|
| 1760 | base.setFrameNumber(0);
|
---|
[24] | 1761 | base.setTitle(base.getFramesetName() + "0");
|
---|
[298] | 1762 | base.setPath(path);
|
---|
[7] | 1763 | base.change();
|
---|
[655] | 1764 | base.setOwner(UserSettings.UserName.get());
|
---|
[7] | 1765 | SaveFrame(base, false);
|
---|
| 1766 |
|
---|
[424] | 1767 | base.reset();
|
---|
[7] | 1768 | base.resetDateCreated();
|
---|
| 1769 | base.setFrameNumber(1);
|
---|
| 1770 | base.setTitle(frameset);
|
---|
| 1771 | base.change();
|
---|
[655] | 1772 | base.setOwner(UserSettings.UserName.get());
|
---|
[7] | 1773 | SaveFrame(base, true);
|
---|
[1242] | 1774 |
|
---|
| 1775 | FrameIO.setSavedProperties(base);
|
---|
[7] | 1776 |
|
---|
[1102] | 1777 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Created new frameset: " + frameset);
|
---|
[7] | 1778 |
|
---|
| 1779 | return base;
|
---|
[24] | 1780 | }
|
---|
| 1781 |
|
---|
| 1782 | /**
|
---|
| 1783 | * Tests if a frameset name is valid. That is it must begin and end with a
|
---|
| 1784 | * letter and contain only letters and digits in between.
|
---|
| 1785 | *
|
---|
[64] | 1786 | * @param frameset
|
---|
| 1787 | * the name to be tested
|
---|
[24] | 1788 | * @return true if the frameset name is valid
|
---|
| 1789 | */
|
---|
[72] | 1790 | public static boolean isValidFramesetName(String frameset) {
|
---|
[24] | 1791 | if (frameset == null) {
|
---|
| 1792 | return false;
|
---|
| 1793 | }
|
---|
[64] | 1794 |
|
---|
[24] | 1795 | int nameLength = frameset.length();
|
---|
[64] | 1796 | if (frameset.length() <= 0 || nameLength > MAX_NAME_LENGTH) {
|
---|
[24] | 1797 | return false;
|
---|
| 1798 | }
|
---|
[64] | 1799 |
|
---|
| 1800 | int lastCharIndex = nameLength - 1;
|
---|
| 1801 |
|
---|
| 1802 | if (!Character.isLetter(frameset.charAt(0))
|
---|
[1188] | 1803 | || !Character.isLetter(frameset.charAt(lastCharIndex))) {
|
---|
[24] | 1804 | return false;
|
---|
[1188] | 1805 | }
|
---|
[24] | 1806 |
|
---|
| 1807 | for (int i = 1; i < lastCharIndex; i++) {
|
---|
[427] | 1808 | if (!isValidFrameNameChar(frameset.charAt(i))) {
|
---|
[13] | 1809 | return false;
|
---|
| 1810 | }
|
---|
| 1811 | }
|
---|
| 1812 | return true;
|
---|
[7] | 1813 | }
|
---|
| 1814 |
|
---|
[298] | 1815 | public static boolean deleteFrameset(String framesetName) {
|
---|
[1293] | 1816 | return moveFrameset(framesetName, FrameIO.TRASH_PATH, true);
|
---|
[298] | 1817 | }
|
---|
| 1818 |
|
---|
[1293] | 1819 | public static boolean moveFrameset(String framesetName, String destinationFolder, boolean override) {
|
---|
[1188] | 1820 | if (!FrameIO.canAccessFrameset(framesetName)) {
|
---|
[7] | 1821 | return false;
|
---|
[1188] | 1822 | }
|
---|
[416] | 1823 | // Clear the cache
|
---|
[362] | 1824 | _Cache.clear();
|
---|
[416] | 1825 |
|
---|
[64] | 1826 | // Search all the available directories for the directory
|
---|
[1244] | 1827 | for (String path : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
[1293] | 1828 | return moveFrameset(framesetName, path, destinationFolder, override);
|
---|
[1280] | 1829 | }
|
---|
| 1830 | return false;
|
---|
| 1831 | }
|
---|
| 1832 |
|
---|
[1293] | 1833 | public static boolean moveFrameset(String framesetName, String path, String destinationFolder, boolean override) {
|
---|
[1280] | 1834 | String source = path + framesetName.toLowerCase() + File.separator;
|
---|
| 1835 | File framesetDirectory = new File(source);
|
---|
| 1836 | // Once we have found the directory move it
|
---|
| 1837 | if (framesetDirectory.exists()) {
|
---|
| 1838 | String destPath = destinationFolder
|
---|
| 1839 | + framesetName.toLowerCase();
|
---|
| 1840 | int copyNumber = 1;
|
---|
| 1841 | File dest = new File(destPath + File.separator);
|
---|
| 1842 | // Create the destination folder if it doesnt already exist
|
---|
| 1843 | if (!dest.getParentFile().exists()) {
|
---|
| 1844 | dest.mkdirs();
|
---|
| 1845 | }
|
---|
| 1846 | // If a frameset with the same name is already in the
|
---|
| 1847 | // destination add
|
---|
| 1848 | // a number to the end
|
---|
[1293] | 1849 | while (dest.exists() && !override) {
|
---|
[1280] | 1850 | dest = new File(destPath + ++copyNumber + File.separator);
|
---|
| 1851 | }
|
---|
| 1852 | try {
|
---|
[1293] | 1853 | moveFileTree(framesetDirectory.toPath(), dest.toPath());
|
---|
[1280] | 1854 | } catch (IOException e) {
|
---|
| 1855 | e.printStackTrace();
|
---|
| 1856 | return false;
|
---|
| 1857 | }
|
---|
| 1858 |
|
---|
| 1859 | for (File f : framesetDirectory.listFiles()) {
|
---|
| 1860 | if (!f.delete()) {
|
---|
| 1861 | return false;
|
---|
[1188] | 1862 | }
|
---|
[7] | 1863 | }
|
---|
[1280] | 1864 | if (!framesetDirectory.delete()) {
|
---|
| 1865 | return false;
|
---|
| 1866 | }
|
---|
| 1867 | return true;
|
---|
| 1868 | } else {
|
---|
| 1869 | return false;
|
---|
[7] | 1870 | }
|
---|
| 1871 | }
|
---|
| 1872 |
|
---|
| 1873 | public static boolean CopyFrameset(String framesetToCopy,
|
---|
| 1874 | String copiedFrameset) throws Exception {
|
---|
[1188] | 1875 | if (!FrameIO.canAccessFrameset(framesetToCopy)) {
|
---|
[7] | 1876 | return false;
|
---|
[1188] | 1877 | }
|
---|
| 1878 | if (FrameIO.canAccessFrameset(copiedFrameset)) {
|
---|
[7] | 1879 | return false;
|
---|
[1188] | 1880 | }
|
---|
[7] | 1881 | // search through all the directories to find the frameset we are
|
---|
| 1882 | // copying
|
---|
[1244] | 1883 | for (String path : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
[7] | 1884 | String source = path + framesetToCopy.toLowerCase()
|
---|
| 1885 | + File.separator;
|
---|
| 1886 | File framesetDirectory = new File(source);
|
---|
| 1887 | if (framesetDirectory.exists()) {
|
---|
| 1888 | // copy the frameset
|
---|
| 1889 | File copyFramesetDirectory = new File(path
|
---|
| 1890 | + copiedFrameset.toLowerCase() + File.separator);
|
---|
[1188] | 1891 | if (!copyFramesetDirectory.mkdirs()) {
|
---|
[7] | 1892 | return false;
|
---|
[1188] | 1893 | }
|
---|
[7] | 1894 | // copy each of the frames
|
---|
| 1895 | for (File f : framesetDirectory.listFiles()) {
|
---|
[97] | 1896 | // Ignore hidden files
|
---|
[1188] | 1897 | if (f.getName().charAt(0) == '.') {
|
---|
[97] | 1898 | continue;
|
---|
[1188] | 1899 | }
|
---|
[7] | 1900 | String copyPath = copyFramesetDirectory.getAbsolutePath()
|
---|
| 1901 | + File.separator + f.getName();
|
---|
| 1902 | FrameIO.copyFile(f.getAbsolutePath(), copyPath);
|
---|
| 1903 | }
|
---|
| 1904 | return true;
|
---|
| 1905 | }
|
---|
| 1906 | }
|
---|
| 1907 | return false;
|
---|
| 1908 | }
|
---|
| 1909 |
|
---|
[80] | 1910 | /**
|
---|
| 1911 | * Copies a file from one location to another.
|
---|
[86] | 1912 | *
|
---|
[80] | 1913 | * @param existingFile
|
---|
| 1914 | * @param newFileName
|
---|
| 1915 | * @throws Exception
|
---|
| 1916 | */
|
---|
[7] | 1917 | public static void copyFile(String existingFile, String newFileName)
|
---|
[1105] | 1918 | throws IOException {
|
---|
[7] | 1919 | FileInputStream is = new FileInputStream(existingFile);
|
---|
| 1920 | FileOutputStream os = new FileOutputStream(newFileName, false);
|
---|
| 1921 | int data;
|
---|
| 1922 | while ((data = is.read()) != -1) {
|
---|
| 1923 | os.write(data);
|
---|
| 1924 | }
|
---|
| 1925 | os.flush();
|
---|
| 1926 | os.close();
|
---|
| 1927 | is.close();
|
---|
| 1928 | }
|
---|
| 1929 |
|
---|
| 1930 | /**
|
---|
| 1931 | * Saves a frame regardless of whether or not the frame is marked as having
|
---|
| 1932 | * been changed.
|
---|
| 1933 | *
|
---|
| 1934 | * @param frame
|
---|
| 1935 | * the frame to save
|
---|
| 1936 | * @return the contents of the frame or null if it could not be saved
|
---|
| 1937 | */
|
---|
| 1938 | public static String ForceSaveFrame(Frame frame) {
|
---|
| 1939 | frame.change();
|
---|
| 1940 | return SaveFrame(frame, false);
|
---|
| 1941 | }
|
---|
| 1942 |
|
---|
[72] | 1943 | public static boolean isValidLink(String frameName) {
|
---|
[86] | 1944 | return frameName == null || isPositiveInteger(frameName)
|
---|
| 1945 | || isValidFrameName(frameName);
|
---|
[72] | 1946 | }
|
---|
| 1947 |
|
---|
[306] | 1948 | public static void SavePublicFrame(String peerName, String frameName,
|
---|
| 1949 | int version, BufferedReader packetContents) {
|
---|
| 1950 | // TODO handle versioning - add version to the header
|
---|
| 1951 | // Remote user uploads version based on an old version
|
---|
| 1952 |
|
---|
| 1953 | // Remove it from the cache so that next time it is loaded we get the up
|
---|
| 1954 | // todate version
|
---|
| 1955 | _Cache.remove(frameName.toLowerCase());
|
---|
| 1956 |
|
---|
| 1957 | // Save to file
|
---|
| 1958 | String filename = PUBLIC_PATH + Conversion.getFramesetName(frameName)
|
---|
| 1959 | + File.separator + Conversion.getFrameNumber(frameName)
|
---|
[601] | 1960 | + ExpReader.EXTENTION;
|
---|
[306] | 1961 |
|
---|
| 1962 | File file = new File(filename);
|
---|
| 1963 | // Ensure the file exists
|
---|
| 1964 | if (file.exists()) {
|
---|
| 1965 | // Check the versions
|
---|
[601] | 1966 | int savedVersion = ExpReader.getVersion(filename);
|
---|
[306] | 1967 |
|
---|
| 1968 | if (savedVersion > version) {
|
---|
| 1969 | // remove this frame from the cache if it is there
|
---|
| 1970 | // This will make sure links to the original are set correctly
|
---|
| 1971 | // _Cache.remove(frameName.toLowerCase());
|
---|
| 1972 |
|
---|
| 1973 | int nextNum = 0;
|
---|
| 1974 | try {
|
---|
| 1975 | nextNum = ReadINF(PUBLIC_PATH, Conversion
|
---|
[307] | 1976 | .getFramesetName(frameName), false) + 1;
|
---|
[306] | 1977 | } catch (IOException e) {
|
---|
| 1978 | e.printStackTrace();
|
---|
| 1979 | }
|
---|
| 1980 |
|
---|
| 1981 | String newName = Conversion.getFramesetName(frameName)
|
---|
| 1982 | + nextNum;
|
---|
| 1983 | filename = PUBLIC_PATH + Conversion.getFramesetName(frameName)
|
---|
[601] | 1984 | + File.separator + nextNum + ExpReader.EXTENTION;
|
---|
[306] | 1985 |
|
---|
| 1986 | // Show the messages alerting the user
|
---|
| 1987 | Text originalMessage = new Text(-1);
|
---|
| 1988 | originalMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
| 1989 | originalMessage.setText(frameName + " was edited by "
|
---|
| 1990 | + peerName);
|
---|
| 1991 | originalMessage.setLink(frameName);
|
---|
| 1992 | Text yourMessage = new Text(-1);
|
---|
| 1993 | yourMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
| 1994 | yourMessage.setText("Their version was renamed " + newName);
|
---|
| 1995 | yourMessage.setLink(newName);
|
---|
| 1996 | MessageBay.displayMessage(originalMessage);
|
---|
| 1997 | MessageBay.displayMessage(yourMessage);
|
---|
| 1998 |
|
---|
| 1999 | Frame editedFrame = FrameIO.LoadFrame(frameName);
|
---|
| 2000 |
|
---|
| 2001 | FrameShare.getInstance().sendMessage(
|
---|
| 2002 | frameName + " was recently edited by "
|
---|
| 2003 | + editedFrame.getLastModifyUser(), peerName);
|
---|
| 2004 | FrameShare.getInstance().sendMessage(
|
---|
| 2005 | "Your version was renamed " + newName, peerName);
|
---|
| 2006 | }
|
---|
[307] | 2007 | }
|
---|
[306] | 2008 |
|
---|
[307] | 2009 | // Save the new version
|
---|
| 2010 | try {
|
---|
[348] | 2011 | // FileWriter fw = new FileWriter(file);
|
---|
| 2012 |
|
---|
| 2013 | // Open an Output Stream Writer to set encoding
|
---|
| 2014 | OutputStream fout = new FileOutputStream(file);
|
---|
| 2015 | OutputStream bout = new BufferedOutputStream(fout);
|
---|
| 2016 | Writer fw = new OutputStreamWriter(bout, "UTF-8");
|
---|
| 2017 |
|
---|
[307] | 2018 | String nextLine = null;
|
---|
| 2019 | while ((nextLine = packetContents.readLine()) != null) {
|
---|
| 2020 | fw.write(nextLine + '\n');
|
---|
[306] | 2021 | }
|
---|
[307] | 2022 | fw.flush();
|
---|
| 2023 | fw.close();
|
---|
| 2024 | MessageBay.displayMessage("Saved remote frame: " + frameName);
|
---|
| 2025 | } catch (IOException e) {
|
---|
| 2026 | MessageBay.errorMessage("Error remote saving " + frameName + ": "
|
---|
| 2027 | + e.getMessage());
|
---|
| 2028 | e.printStackTrace();
|
---|
[306] | 2029 | }
|
---|
| 2030 | }
|
---|
| 2031 |
|
---|
| 2032 | public static void setSavedProperties(Frame toSave) {
|
---|
[1229] | 2033 | toSave.setLastModifyDate(Formatter.getDateTime(), System.currentTimeMillis());
|
---|
[655] | 2034 | toSave.setLastModifyUser(UserSettings.UserName.get());
|
---|
[306] | 2035 | toSave.setVersion(toSave.getVersion() + 1);
|
---|
| 2036 | Time darkTime = new Time(SessionStats.getFrameDarkTime().getTime()
|
---|
| 2037 | + toSave.getDarkTime().getTime());
|
---|
| 2038 | Time activeTime = new Time(SessionStats.getFrameActiveTime().getTime()
|
---|
| 2039 | + toSave.getActiveTime().getTime());
|
---|
| 2040 | toSave.setDarkTime(darkTime);
|
---|
| 2041 | toSave.setActiveTime(activeTime);
|
---|
| 2042 | }
|
---|
[1244] | 2043 |
|
---|
[1270] | 2044 | public static Path setupPersonalResources(String username) {
|
---|
[1244] | 2045 | Path personalResources = Paths.get(FrameIO.PARENT_FOLDER).resolve("resources-" + username);
|
---|
| 2046 | personalResources.toFile().mkdir();
|
---|
[1274] | 2047 | File[] globalResourcesToCopy = Paths.get(FrameIO.RESOURCES_PRIVATE_PATH).toFile().listFiles();
|
---|
[1244] | 2048 |
|
---|
| 2049 | try {
|
---|
| 2050 | for (File toCopy: globalResourcesToCopy) {
|
---|
| 2051 | Path p = Paths.get(toCopy.getAbsolutePath());
|
---|
| 2052 | if (!p.getFileName().toString().equals(".res") && !p.getFileName().toString().equals("about")) {
|
---|
[1293] | 2053 | moveFileTree(p.toAbsolutePath(), personalResources.resolve(p.getFileName()));
|
---|
[1244] | 2054 | }
|
---|
| 2055 | }
|
---|
[1330] | 2056 | } catch (IOException e) {
|
---|
[1244] | 2057 | e.printStackTrace();
|
---|
| 2058 | personalResources = null;
|
---|
| 2059 | }
|
---|
| 2060 |
|
---|
| 2061 | return personalResources;
|
---|
| 2062 | }
|
---|
| 2063 |
|
---|
[1270] | 2064 | public static void migrateFrame(Frame toMigrate, Path destinationDirectory) {
|
---|
| 2065 | Path source = Paths.get(toMigrate.getFramePathReal());
|
---|
| 2066 | String destination = source.relativize(destinationDirectory).toString().substring(3).replace(File.separator, "/");
|
---|
| 2067 | try {
|
---|
[1271] | 2068 | Files.move(source, destinationDirectory);
|
---|
[1270] | 2069 | } catch (IOException e) {
|
---|
| 2070 | System.err.println("FrameIO::migrateFrame: failed to migrate from to new location. Message: " + e.getMessage());
|
---|
| 2071 | return;
|
---|
| 2072 | }
|
---|
| 2073 | try {
|
---|
| 2074 | FileWriter out = new FileWriter(source.toFile());
|
---|
| 2075 | out.write("REDIRECT:" + destination);
|
---|
| 2076 | out.flush();
|
---|
| 2077 | out.close();
|
---|
| 2078 | } catch (IOException e) {
|
---|
| 2079 | System.err.println("FrameIO::migrateFrame: failed to update file [" + source + "] to redirect to [" + destination + "] following migration. Message: " + e.getMessage());
|
---|
| 2080 | }
|
---|
| 2081 | }
|
---|
| 2082 |
|
---|
[1293] | 2083 | private static void moveFileTree(Path source, Path target) throws IOException {
|
---|
[1244] | 2084 | if (source.toFile().isDirectory()) {
|
---|
[1293] | 2085 | if (!target.toFile().exists()) {
|
---|
| 2086 | Files.copy(source, target);
|
---|
| 2087 | }
|
---|
[1244] | 2088 | File[] files = source.toFile().listFiles();
|
---|
| 2089 | for (File file: files) {
|
---|
| 2090 | Path asPath = Paths.get(file.getAbsolutePath());
|
---|
[1293] | 2091 | moveFileTree(asPath, target.resolve(asPath.getFileName()));
|
---|
[1244] | 2092 | }
|
---|
[1293] | 2093 | } else {
|
---|
| 2094 | Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
|
---|
[1244] | 2095 | }
|
---|
| 2096 | }
|
---|
[4] | 2097 | } |
---|