[4] | 1 | package org.expeditee.gui;
|
---|
| 2 |
|
---|
[348] | 3 | import java.io.BufferedOutputStream;
|
---|
[4] | 4 | import java.io.BufferedReader;
|
---|
| 5 | import java.io.BufferedWriter;
|
---|
| 6 | import java.io.File;
|
---|
[7] | 7 | import java.io.FileInputStream;
|
---|
[162] | 8 | import java.io.FileNotFoundException;
|
---|
[7] | 9 | import java.io.FileOutputStream;
|
---|
[4] | 10 | import java.io.FileReader;
|
---|
| 11 | import java.io.FileWriter;
|
---|
| 12 | import java.io.IOException;
|
---|
[348] | 13 | import java.io.OutputStream;
|
---|
| 14 | import java.io.OutputStreamWriter;
|
---|
| 15 | import java.io.Writer;
|
---|
[105] | 16 | import java.sql.Time;
|
---|
[162] | 17 | import java.util.Collection;
|
---|
[4] | 18 | import java.util.HashMap;
|
---|
[162] | 19 | import java.util.LinkedList;
|
---|
[4] | 20 |
|
---|
[415] | 21 | import org.expeditee.actions.Actions;
|
---|
[8] | 22 | import org.expeditee.agents.ExistingFramesetException;
|
---|
[4] | 23 | import org.expeditee.io.Conversion;
|
---|
| 24 | import org.expeditee.io.ExpReader;
|
---|
| 25 | import org.expeditee.io.ExpWriter;
|
---|
| 26 | import org.expeditee.io.FrameReader;
|
---|
| 27 | import org.expeditee.io.FrameWriter;
|
---|
| 28 | import org.expeditee.io.KMSReader;
|
---|
| 29 | import org.expeditee.io.KMSWriter;
|
---|
| 30 | import org.expeditee.items.Item;
|
---|
| 31 | import org.expeditee.items.ItemUtils;
|
---|
[454] | 32 | import org.expeditee.items.PermissionPair;
|
---|
| 33 | import org.expeditee.items.UserAppliedPermission;
|
---|
[4] | 34 | import org.expeditee.items.Text;
|
---|
[298] | 35 | import org.expeditee.network.FrameShare;
|
---|
[570] | 36 | import org.expeditee.settings.UserSettings;
|
---|
[419] | 37 | import org.expeditee.stats.Formatter;
|
---|
| 38 | import org.expeditee.stats.Logger;
|
---|
[4] | 39 | import org.expeditee.stats.SessionStats;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * This class provides static methods for all saving and loading of Frames
|
---|
| 43 | * to\from disk. This class also handles any caching of previously loaded
|
---|
| 44 | * Frames.
|
---|
| 45 | *
|
---|
| 46 | * @author jdm18
|
---|
| 47 | *
|
---|
| 48 | */
|
---|
| 49 | public class FrameIO {
|
---|
| 50 |
|
---|
[24] | 51 | private static final char FRAME_NAME_LAST_CHAR = 'A';
|
---|
| 52 |
|
---|
[4] | 53 | public static void changeParentFolder(String newFolder) {
|
---|
| 54 | PARENT_FOLDER = newFolder;
|
---|
[286] | 55 | PUBLIC_PATH = PARENT_FOLDER + "public" + File.separator;
|
---|
[4] | 56 | FRAME_PATH = PARENT_FOLDER + "framesets" + File.separator;
|
---|
[298] | 57 | MESSAGES_PATH = PARENT_FOLDER + "messages" + File.separator;
|
---|
[64] | 58 | TRASH_PATH = PARENT_FOLDER + "trash" + File.separator;
|
---|
[80] | 59 | IMAGES_PATH = PARENT_FOLDER + IMAGES_FOLDER;
|
---|
[90] | 60 | HELP_PATH = PARENT_FOLDER + "documentation" + File.separator;
|
---|
[504] | 61 | DICT_PATH = PARENT_FOLDER + "dict" + File.separator;
|
---|
[4] | 62 | PROFILE_PATH = PARENT_FOLDER + "profiles" + File.separator;
|
---|
| 63 | EXPORTS_DIR = PARENT_FOLDER + "exports" + File.separator;
|
---|
| 64 | STATISTICS_DIR = PARENT_FOLDER + "statistics" + File.separator;
|
---|
| 65 | LOGS_DIR = PARENT_FOLDER + "logs" + File.separator;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * The default location for storing the framesets. Each frameset has its own
|
---|
| 70 | * subdirectory in this directory.
|
---|
| 71 | */
|
---|
[80] | 72 | public static String IMAGES_FOLDER = "images" + File.separator;
|
---|
[86] | 73 |
|
---|
[64] | 74 | public static String TRASH_PATH;
|
---|
| 75 |
|
---|
[4] | 76 | public static String PARENT_FOLDER;
|
---|
| 77 |
|
---|
| 78 | public static String FRAME_PATH;
|
---|
[298] | 79 |
|
---|
| 80 | public static String MESSAGES_PATH;
|
---|
| 81 |
|
---|
[286] | 82 | public static String PUBLIC_PATH;
|
---|
[4] | 83 |
|
---|
| 84 | public static String IMAGES_PATH;
|
---|
| 85 |
|
---|
| 86 | public static String HELP_PATH;
|
---|
[504] | 87 |
|
---|
[614] | 88 | public static String TEMPLATES_PATH;
|
---|
| 89 |
|
---|
[504] | 90 | public static String DICT_PATH;
|
---|
[4] | 91 |
|
---|
| 92 | public static String PROFILE_PATH;
|
---|
| 93 |
|
---|
| 94 | public static String EXPORTS_DIR;
|
---|
| 95 |
|
---|
| 96 | public static String STATISTICS_DIR;
|
---|
| 97 |
|
---|
| 98 | public static String LOGS_DIR;
|
---|
| 99 |
|
---|
| 100 | private static final String INF_FILENAME = "frame.inf";
|
---|
| 101 |
|
---|
| 102 | public static final String ILLEGAL_CHARS = ";:\\/?";
|
---|
| 103 |
|
---|
| 104 | public static final int MAX_NAME_LENGTH = 64;
|
---|
| 105 |
|
---|
[86] | 106 | public static final int MAX_CACHE = 100;
|
---|
[4] | 107 |
|
---|
[214] | 108 | private static HashMap<String, Frame> _Cache = new FrameCache();
|
---|
[4] | 109 |
|
---|
| 110 | // private static HashMap<String, String> _FramesetNameCache = new
|
---|
| 111 | // HashMap<String, String>();
|
---|
| 112 |
|
---|
| 113 | private static boolean ENABLE_CACHE = true;
|
---|
| 114 |
|
---|
| 115 | private static boolean _UseCache = true;
|
---|
| 116 |
|
---|
| 117 | private static boolean _SuspendedCache = false;
|
---|
| 118 |
|
---|
| 119 | // All methods are static, this should not be instantiated
|
---|
| 120 | private FrameIO() {
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public static boolean isCacheOn() {
|
---|
| 124 | return _UseCache && ENABLE_CACHE;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public static void Precache(String framename) {
|
---|
| 128 | // if the cache is turned off, do nothing
|
---|
| 129 | if (!isCacheOn())
|
---|
| 130 | return;
|
---|
| 131 |
|
---|
| 132 | // if the frame is already in the cache, do nothing
|
---|
| 133 | if (_Cache.containsKey(framename.toLowerCase()))
|
---|
| 134 | return;
|
---|
| 135 |
|
---|
| 136 | // otherwise, load the frame and put it in the cache
|
---|
| 137 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Precaching " + framename + ".");
|
---|
| 138 |
|
---|
| 139 | // do not display errors encountered to the user
|
---|
| 140 | // (they will be shown at load time)
|
---|
[121] | 141 | MessageBay.supressMessages(true);
|
---|
[4] | 142 | // loading automatically caches the frame is caching is turned on
|
---|
[307] | 143 | LoadFromDisk(framename, null, false);
|
---|
[121] | 144 | MessageBay.supressMessages(false);
|
---|
[4] | 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Checks if a string is a representation of a positive integer.
|
---|
| 149 | *
|
---|
| 150 | * @param s
|
---|
| 151 | * @return true if s is a positive integer
|
---|
| 152 | */
|
---|
| 153 | public static boolean isPositiveInteger(String s) {
|
---|
| 154 | if (s == null || s.length() == 0)
|
---|
| 155 | return false;
|
---|
| 156 |
|
---|
| 157 | for (int i = 0; i < s.length(); i++) {
|
---|
| 158 | if (!Character.isDigit(s.charAt(i)))
|
---|
| 159 | return false;
|
---|
| 160 | }
|
---|
| 161 | return true;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public static Frame LoadFrame(String frameName) {
|
---|
[307] | 165 | return LoadFrame(frameName, null, false);
|
---|
[282] | 166 | }
|
---|
[298] | 167 |
|
---|
[307] | 168 | public static Frame LoadFrame(String frameName, String path) {
|
---|
| 169 | return LoadFrame(frameName, path, false);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public static Frame LoadFrame(String frameName, String path,
|
---|
| 173 | boolean ignoreAnnotations) {
|
---|
[97] | 174 | if (!isValidFrameName(frameName))
|
---|
| 175 | return null;
|
---|
[4] | 176 |
|
---|
[108] | 177 | String frameNameLower = frameName.toLowerCase();
|
---|
[4] | 178 | // first try reading from cache
|
---|
[108] | 179 | if (isCacheOn() && _Cache.containsKey(frameNameLower)) {
|
---|
[4] | 180 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
|
---|
| 181 | + " from cache.");
|
---|
[247] | 182 | Frame frame = _Cache.get(frameNameLower);
|
---|
| 183 | return frame;
|
---|
[4] | 184 | }
|
---|
| 185 |
|
---|
| 186 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
|
---|
| 187 | + " from disk.");
|
---|
[307] | 188 |
|
---|
| 189 | return LoadFromDisk(frameName, path, ignoreAnnotations);
|
---|
[4] | 190 | }
|
---|
| 191 |
|
---|
[305] | 192 | public static BufferedReader LoadPublicFrame(String frameName) {
|
---|
| 193 | String fullPath = FrameIO.getFrameFullPathName(PUBLIC_PATH, frameName);
|
---|
[306] | 194 |
|
---|
[336] | 195 | if (fullPath == null)
|
---|
[307] | 196 | return null;
|
---|
[336] | 197 |
|
---|
[305] | 198 | File frameFile = new File(fullPath);
|
---|
| 199 | if (frameFile.exists() && frameFile.canRead()) {
|
---|
| 200 | try {
|
---|
| 201 | return new BufferedReader(new FileReader(frameFile));
|
---|
| 202 | } catch (FileNotFoundException e) {
|
---|
| 203 | e.printStackTrace();
|
---|
| 204 | }
|
---|
[298] | 205 | }
|
---|
[305] | 206 | return null;
|
---|
[298] | 207 | }
|
---|
| 208 |
|
---|
[307] | 209 | private static Frame LoadFromDisk(String framename, String knownPath,
|
---|
[298] | 210 | boolean ignoreAnnotations) {
|
---|
| 211 | Frame loaded = null;
|
---|
| 212 |
|
---|
[307] | 213 | if (knownPath != null) {
|
---|
| 214 | loaded = LoadKnowPath(knownPath, framename);
|
---|
| 215 | } else {
|
---|
| 216 |
|
---|
[655] | 217 | for (String path : UserSettings.FrameDirs.get()) {
|
---|
[307] | 218 | loaded = LoadKnowPath(path, framename);
|
---|
| 219 | if (loaded != null) {
|
---|
| 220 | break;
|
---|
| 221 | }
|
---|
[4] | 222 | }
|
---|
| 223 | }
|
---|
[336] | 224 |
|
---|
[305] | 225 | if (loaded == null && FrameShare.getInstance() != null) {
|
---|
[307] | 226 | loaded = FrameShare.getInstance().loadFrame(framename, knownPath);
|
---|
[298] | 227 | }
|
---|
| 228 |
|
---|
| 229 | if (loaded != null) {
|
---|
| 230 | FrameUtils.Parse(loaded, true, ignoreAnnotations);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[4] | 233 | return loaded;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
[7] | 237 | * Gets a list of all the framesets available to the user
|
---|
| 238 | *
|
---|
| 239 | * @return a string containing a list of all the available framesets on
|
---|
| 240 | * separate lines
|
---|
| 241 | */
|
---|
| 242 | public static String getFramesetList() {
|
---|
| 243 | StringBuffer list = new StringBuffer();
|
---|
| 244 |
|
---|
[655] | 245 | for (String path : UserSettings.FrameDirs.get()) {
|
---|
[97] | 246 | File files = new File(path);
|
---|
| 247 | if (!files.exists())
|
---|
| 248 | continue;
|
---|
[7] | 249 | for (File f : (new File(path)).listFiles()) {
|
---|
| 250 | if (f.isDirectory()) {
|
---|
| 251 | list.append(f.getName()).append('\n');
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | // remove the final new line char
|
---|
| 256 | list.deleteCharAt(list.length() - 1);
|
---|
| 257 | return list.toString();
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
[4] | 261 | * Gets the full path and file name of the frame.
|
---|
| 262 | *
|
---|
| 263 | * @param path-
|
---|
| 264 | * the directory in which to look for the frameset containing the
|
---|
| 265 | * frame.
|
---|
| 266 | * @param frameName-
|
---|
| 267 | * the name of the frame for which the path is being requested.
|
---|
| 268 | * @return null if the frame can not be located.
|
---|
| 269 | */
|
---|
[298] | 270 | public static synchronized String getFrameFullPathName(String path,
|
---|
| 271 | String frameName) {
|
---|
[97] | 272 | String source = path + Conversion.getFramesetName(frameName)
|
---|
[4] | 273 | + File.separator;
|
---|
| 274 |
|
---|
| 275 | File tester = new File(source);
|
---|
| 276 | if (!tester.exists())
|
---|
| 277 | return null;
|
---|
| 278 |
|
---|
[601] | 279 | // check for the new file name format
|
---|
[4] | 280 | String fullPath = source + Conversion.getFrameNumber(frameName)
|
---|
| 281 | + ExpReader.EXTENTION;
|
---|
| 282 | tester = new File(fullPath);
|
---|
| 283 |
|
---|
| 284 | if (tester.exists())
|
---|
| 285 | return fullPath;
|
---|
| 286 |
|
---|
[601] | 287 | // check for oldfile name format
|
---|
[97] | 288 | fullPath = source + Conversion.getFramesetName(frameName) + "."
|
---|
[4] | 289 | + Conversion.getFrameNumber(frameName);
|
---|
| 290 | tester = new File(fullPath);
|
---|
| 291 |
|
---|
| 292 | if (tester.exists())
|
---|
| 293 | return fullPath;
|
---|
| 294 |
|
---|
| 295 | return null;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[97] | 298 | public static boolean canAccessFrame(String frameName) {
|
---|
[294] | 299 | Frame current = DisplayIO.getCurrentFrame();
|
---|
[436] | 300 | // Just in case the current frame is not yet saved...
|
---|
[298] | 301 | if (frameName.equals(current.getName())) {
|
---|
| 302 | FrameIO.SaveFrame(current, false, false);
|
---|
[294] | 303 | current.change();
|
---|
| 304 | return true;
|
---|
| 305 | }
|
---|
[298] | 306 |
|
---|
[655] | 307 | for (String path : UserSettings.FrameDirs.get()) {
|
---|
[4] | 308 | if (getFrameFullPathName(path, frameName) != null)
|
---|
| 309 | return true;
|
---|
| 310 | }
|
---|
| 311 | return false;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[162] | 314 | public static Collection<String> searchFrame(String frameName,
|
---|
| 315 | String pattern, String path) {
|
---|
| 316 | String fullPath = null;
|
---|
| 317 | if (path == null) {
|
---|
[655] | 318 | for (String possiblePath : UserSettings.FrameDirs.get()) {
|
---|
[162] | 319 | fullPath = getFrameFullPathName(possiblePath, frameName);
|
---|
| 320 | if (fullPath != null)
|
---|
| 321 | break;
|
---|
| 322 | }
|
---|
| 323 | } else {
|
---|
| 324 | fullPath = getFrameFullPathName(path, frameName);
|
---|
| 325 | }
|
---|
| 326 | // If the frame was not located return null
|
---|
| 327 | if (fullPath == null)
|
---|
| 328 | return null;
|
---|
| 329 | Collection<String> results = new LinkedList<String>();
|
---|
| 330 | // Open the file and search the text items
|
---|
| 331 | try {
|
---|
| 332 | BufferedReader reader = new BufferedReader(new FileReader(fullPath));
|
---|
| 333 | String next;
|
---|
| 334 | while (reader.ready() && ((next = reader.readLine()) != null)) {
|
---|
| 335 | if (next.startsWith("T")) {
|
---|
| 336 | String toSearch = next.substring(2);
|
---|
[179] | 337 | if (toSearch.toLowerCase().contains(pattern))
|
---|
[162] | 338 | results.add(toSearch);
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | } catch (FileNotFoundException e) {
|
---|
| 342 | e.printStackTrace();
|
---|
| 343 | return null;
|
---|
| 344 | } catch (IOException e) {
|
---|
| 345 | e.printStackTrace();
|
---|
| 346 | }
|
---|
| 347 | return results;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[307] | 350 | private static Frame LoadKnowPath(String path, String frameName) {
|
---|
[4] | 351 | String fullPath = getFrameFullPathName(path, frameName);
|
---|
| 352 | if (fullPath == null)
|
---|
| 353 | return null;
|
---|
| 354 |
|
---|
| 355 | try {
|
---|
| 356 | FrameReader reader;
|
---|
| 357 |
|
---|
[601] | 358 | if (fullPath.endsWith(ExpReader.EXTENTION)) {
|
---|
[4] | 359 | reader = new ExpReader(frameName);
|
---|
| 360 | } else {
|
---|
| 361 | reader = new KMSReader();
|
---|
| 362 | }
|
---|
| 363 | Frame frame = reader.readFrame(fullPath);
|
---|
| 364 |
|
---|
| 365 | if (frame == null) {
|
---|
[121] | 366 | MessageBay.errorMessage("Error: " + frameName
|
---|
[4] | 367 | + " could not be successfully loaded.");
|
---|
| 368 | return null;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[298] | 371 | frame.setPath(path);
|
---|
[4] | 372 |
|
---|
| 373 | // do not put 0 frames or virtual frames into the cache
|
---|
[336] | 374 | // Why are zero frames not put in the cache
|
---|
[4] | 375 | if (_Cache.size() > MAX_CACHE)
|
---|
| 376 | _Cache.clear();
|
---|
| 377 |
|
---|
[80] | 378 | if (frame.getNumber() > 0 && isCacheOn())
|
---|
[4] | 379 | _Cache.put(frameName.toLowerCase(), frame);
|
---|
| 380 |
|
---|
| 381 | return frame;
|
---|
| 382 | } catch (IOException ioe) {
|
---|
| 383 | ioe.printStackTrace();
|
---|
| 384 | Logger.Log(ioe);
|
---|
| 385 | } catch (Exception e) {
|
---|
| 386 | e.printStackTrace();
|
---|
| 387 | Logger.Log(e);
|
---|
[121] | 388 | MessageBay.errorMessage("Error: " + frameName
|
---|
[4] | 389 | + " could not be successfully loaded.");
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | return null;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | public static void Reload() {
|
---|
| 396 | // disable cache
|
---|
| 397 | boolean cache = _UseCache;
|
---|
| 398 |
|
---|
| 399 | _UseCache = false;
|
---|
[86] | 400 | Frame fresh = FrameIO.LoadFrame(DisplayIO.getCurrentFrame().getName());
|
---|
[4] | 401 | _UseCache = cache;
|
---|
[80] | 402 | if (_Cache.containsKey(fresh.getName().toLowerCase()))
|
---|
[108] | 403 | addToCache(fresh);
|
---|
[154] | 404 | DisplayIO.setCurrentFrame(fresh, false);
|
---|
[4] | 405 | }
|
---|
| 406 |
|
---|
| 407 | public static Frame LoadPrevious(Frame current) {
|
---|
[7] | 408 | checkTDFC(current);
|
---|
| 409 |
|
---|
[4] | 410 | // the current name and number
|
---|
[24] | 411 | String name = current.getFramesetName();
|
---|
[80] | 412 | int num = current.getNumber() - 1;
|
---|
[4] | 413 |
|
---|
| 414 | // loop until a frame that exists is found
|
---|
| 415 | for (; num >= 0; num--) {
|
---|
[307] | 416 | Frame f = LoadFrame(name + num, current.getPath());
|
---|
[4] | 417 | if (f != null)
|
---|
| 418 | return f;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | // if we did not find another Frame then this one must be the last one
|
---|
| 422 | // in the frameset
|
---|
[121] | 423 | MessageBay
|
---|
| 424 | .displayMessageOnce("This is the first frame in the frameset");
|
---|
[4] | 425 | return null;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * Returns the next Frame in the current Frameset (The Frame with the next
|
---|
| 430 | * highest Frame number) If the current Frame is the last one in the
|
---|
| 431 | * Frameset, or an error occurs then null is returned.
|
---|
| 432 | *
|
---|
| 433 | * @return The Frame after this one in the current frameset, or null
|
---|
| 434 | */
|
---|
| 435 | public static Frame LoadNext(Frame current) {
|
---|
[7] | 436 | checkTDFC(current);
|
---|
[4] | 437 |
|
---|
| 438 | // the current name and number
|
---|
[80] | 439 | int num = current.getNumber() + 1;
|
---|
[4] | 440 | int max = num + 1;
|
---|
[24] | 441 | String name = current.getFramesetName();
|
---|
[4] | 442 |
|
---|
| 443 | // read the maximum from the INF file
|
---|
| 444 | try {
|
---|
[307] | 445 | max = ReadINF(current.getPath(), current.getFramesetName(), false);
|
---|
[4] | 446 | } catch (IOException ioe) {
|
---|
[121] | 447 | MessageBay.errorMessage("Error loading INF file for frameset '"
|
---|
[4] | 448 | + name + "'");
|
---|
| 449 | return null;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | // loop until a frame that exists is found
|
---|
| 453 | for (; num <= max; num++) {
|
---|
[307] | 454 | Frame f = LoadFrame(name + num, current.getPath());
|
---|
[4] | 455 | if (f != null)
|
---|
| 456 | return f;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | // if we did not find another Frame then this one must be the last one
|
---|
| 460 | // in the frameset
|
---|
[162] | 461 | MessageBay.displayMessageOnce("This is the last frame in the frameset");
|
---|
[4] | 462 | return null;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[7] | 465 | /**
|
---|
| 466 | * This method checks if the current frame has just been created with TDFC.
|
---|
| 467 | * If it has the frame is saved regardless of whether it has been edited or
|
---|
| 468 | * not and the TDFC item property is cleared. This is to ensure that the
|
---|
| 469 | * link is saved on the parent frame.
|
---|
| 470 | *
|
---|
| 471 | * @param current
|
---|
| 472 | */
|
---|
[86] | 473 | public static void checkTDFC(Frame current) {
|
---|
[70] | 474 | if (FrameUtils.getTdfcItem() != null) {
|
---|
| 475 | FrameUtils.setTdfcItem(null);
|
---|
[7] | 476 | current.change();
|
---|
| 477 | }
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[4] | 480 | public static Frame LoadLast(String framesetName, String path) {
|
---|
| 481 | // read the maximum from the INF file
|
---|
| 482 | int max;
|
---|
| 483 | try {
|
---|
[307] | 484 | max = ReadINF(path, framesetName, false);
|
---|
[4] | 485 | } catch (IOException ioe) {
|
---|
[121] | 486 | MessageBay.errorMessage("Error loading INF file for frameset '"
|
---|
[4] | 487 | + framesetName + "'");
|
---|
| 488 | return null;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | // loop backwards until a frame that exists is found
|
---|
| 492 | for (int num = max; num > 0; num--) {
|
---|
[307] | 493 | Frame f = LoadFromDisk(framesetName + num, path, false);
|
---|
[4] | 494 | if (f != null)
|
---|
| 495 | return f;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | // if we did not find another Frame then this one must be the last one
|
---|
| 499 | // in the frameset
|
---|
[121] | 500 | MessageBay.displayMessage("This is the last frame in the frameset");
|
---|
[4] | 501 | return null;
|
---|
| 502 | }
|
---|
[24] | 503 |
|
---|
[13] | 504 | public static Frame LoadZero(String framesetName, String path) {
|
---|
[24] | 505 | return LoadFrame(framesetName + 0);
|
---|
[13] | 506 | }
|
---|
[4] | 507 |
|
---|
[13] | 508 | public static Frame LoadZero() {
|
---|
| 509 | Frame current = DisplayIO.getCurrentFrame();
|
---|
[298] | 510 | return LoadZero(current.getFramesetName(), current.getPath());
|
---|
[13] | 511 | }
|
---|
[24] | 512 |
|
---|
[4] | 513 | public static Frame LoadLast() {
|
---|
| 514 | Frame current = DisplayIO.getCurrentFrame();
|
---|
[298] | 515 | return LoadLast(current.getFramesetName(), current.getPath());
|
---|
[4] | 516 | }
|
---|
| 517 |
|
---|
| 518 | public static Frame LoadNext() {
|
---|
| 519 | return LoadNext(DisplayIO.getCurrentFrame());
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | public static Frame LoadPrevious() {
|
---|
| 523 | return LoadPrevious(DisplayIO.getCurrentFrame());
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | /**
|
---|
| 527 | * Deletes the given Frame on disk and removes the cached Frame if there is
|
---|
| 528 | * one. Also adds the deleted frame into the deletedFrames frameset.
|
---|
| 529 | *
|
---|
| 530 | * @param toDelete
|
---|
| 531 | * The Frame to be deleted
|
---|
[162] | 532 | * @return The name the deleted frame was changed to, or null if the delete
|
---|
| 533 | * failed
|
---|
[4] | 534 | */
|
---|
[154] | 535 | public static String DeleteFrame(Frame toDelete) throws IOException,
|
---|
[97] | 536 | SecurityException {
|
---|
[7] | 537 | if (toDelete == null)
|
---|
[154] | 538 | return null;
|
---|
[7] | 539 |
|
---|
[72] | 540 | // Dont delete the zero frame
|
---|
[80] | 541 | if (toDelete.getNumber() == 0) {
|
---|
[97] | 542 | throw new SecurityException("Deleting a zero frame is illegal");
|
---|
[72] | 543 | }
|
---|
| 544 |
|
---|
[298] | 545 | // Dont delete the zero frame
|
---|
| 546 | if (!toDelete.isLocal()) {
|
---|
| 547 | throw new SecurityException("Attempted to delete remote frame");
|
---|
| 548 | }
|
---|
| 549 |
|
---|
[4] | 550 | SaveFrame(toDelete);
|
---|
| 551 |
|
---|
[97] | 552 | // Copy deleted frames to the DeletedFrames frameset
|
---|
[4] | 553 | // get the last used frame in the destination frameset
|
---|
| 554 | final String DELETED_FRAMES = "DeletedFrames";
|
---|
| 555 | int lastNumber = FrameIO.getLastNumber(DELETED_FRAMES);
|
---|
| 556 | String framePath;
|
---|
[8] | 557 | try {
|
---|
[4] | 558 | // create the new frameset
|
---|
[298] | 559 | Frame one = FrameIO.CreateFrameset(DELETED_FRAMES, toDelete
|
---|
| 560 | .getPath());
|
---|
| 561 | framePath = one.getPath();
|
---|
[4] | 562 | lastNumber = 0;
|
---|
[8] | 563 | } catch (Exception e) {
|
---|
[4] | 564 | Frame zero = FrameIO.LoadFrame(DELETED_FRAMES + "0");
|
---|
[298] | 565 | framePath = zero.getPath();
|
---|
[4] | 566 | }
|
---|
| 567 |
|
---|
| 568 | // get the fill path to determine which file version it is
|
---|
[298] | 569 | String source = getFrameFullPathName(toDelete.getPath(), toDelete
|
---|
| 570 | .getName());
|
---|
[4] | 571 |
|
---|
[80] | 572 | String oldFrameName = toDelete.getName().toLowerCase();
|
---|
[4] | 573 | // Now save the frame in the new location
|
---|
| 574 | toDelete.setFrameset(DELETED_FRAMES);
|
---|
| 575 | toDelete.setFrameNumber(lastNumber + 1);
|
---|
[298] | 576 | toDelete.setPath(framePath);
|
---|
[154] | 577 | ForceSaveFrame(toDelete);
|
---|
[4] | 578 |
|
---|
| 579 | if (_Cache.containsKey(oldFrameName))
|
---|
| 580 | _Cache.remove(oldFrameName);
|
---|
| 581 |
|
---|
[601] | 582 | File del = new File(source);
|
---|
| 583 |
|
---|
| 584 | java.io.FileInputStream ff = new java.io.FileInputStream(del);
|
---|
| 585 | ff.close();
|
---|
| 586 |
|
---|
| 587 | if (del.delete()) {
|
---|
[154] | 588 | return toDelete.getName();
|
---|
| 589 | }
|
---|
[162] | 590 |
|
---|
[154] | 591 | return null;
|
---|
[4] | 592 | }
|
---|
| 593 |
|
---|
| 594 | /**
|
---|
| 595 | * Creates a new Frame in the given frameset and assigns it the given Title,
|
---|
| 596 | * which can be null. The newly created Frame is a copy of the frameset's .0
|
---|
| 597 | * file with the number updated based on the last recorded Frame name in the
|
---|
| 598 | * frameset's INF file.
|
---|
| 599 | *
|
---|
| 600 | * @param frameset
|
---|
| 601 | * The frameset to create the new Frame in
|
---|
| 602 | * @param frameTitle
|
---|
| 603 | * The title to assign to the newly created Frame (can be NULL).
|
---|
| 604 | * @return The newly created Frame.
|
---|
| 605 | */
|
---|
[298] | 606 | public static synchronized Frame CreateFrame(String frameset,
|
---|
| 607 | String frameTitle, String templateFrame) throws RuntimeException {
|
---|
[4] | 608 |
|
---|
[72] | 609 | if (!FrameIO.isValidFramesetName(frameset)) {
|
---|
[86] | 610 | throw new RuntimeException(frameset
|
---|
| 611 | + " is not a valid frameset name");
|
---|
[72] | 612 | }
|
---|
[86] | 613 |
|
---|
[4] | 614 | int next = -1;
|
---|
| 615 |
|
---|
| 616 | // disable caching of 0 frames
|
---|
[336] | 617 | // Mike says: Why is caching of 0 frames being disabled?
|
---|
| 618 | /*
|
---|
| 619 | * Especially since 0 frames are not event put into the cache in the
|
---|
| 620 | * frist place
|
---|
| 621 | */
|
---|
| 622 | // SuspendCache();
|
---|
| 623 | /*
|
---|
| 624 | * Suspending the cache causes infinate loops when trying to load a zero
|
---|
| 625 | * frame which has a ao which contains an v or av which contains a link
|
---|
| 626 | * to the ao frame
|
---|
| 627 | */
|
---|
| 628 |
|
---|
[72] | 629 | String zeroFrameName = frameset + "0";
|
---|
| 630 | Frame destFramesetZero = LoadFrame(zeroFrameName);
|
---|
| 631 | if (destFramesetZero == null) {
|
---|
| 632 | throw new RuntimeException(zeroFrameName + " could not be found");
|
---|
| 633 | }
|
---|
[64] | 634 |
|
---|
[4] | 635 | Frame template = null;
|
---|
[72] | 636 | if (templateFrame == null) {
|
---|
[4] | 637 | // load in frame.0
|
---|
[72] | 638 | template = destFramesetZero;
|
---|
| 639 | } else {
|
---|
[4] | 640 | template = LoadFrame(templateFrame);
|
---|
[72] | 641 | if (template == null) {
|
---|
| 642 | throw new RuntimeException("LinkTemplate " + templateFrame
|
---|
| 643 | + " could not be found");
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
[4] | 646 |
|
---|
| 647 | ResumeCache();
|
---|
| 648 |
|
---|
| 649 | // read the next number from the INF file
|
---|
| 650 | try {
|
---|
[307] | 651 | next = ReadINF(destFramesetZero.getPath(), frameset, true);
|
---|
[4] | 652 | } catch (IOException ioe) {
|
---|
| 653 | ioe.printStackTrace();
|
---|
[72] | 654 | throw new RuntimeException("INF file could not be read");
|
---|
[4] | 655 | }
|
---|
| 656 |
|
---|
[86] | 657 | // Remove the old frame from the cashe then add the new one
|
---|
| 658 | // TODO figure out some way that we can put both in the cache
|
---|
| 659 | _Cache.remove(template.getName().toLowerCase());
|
---|
[4] | 660 | // set the number and title of the new frame
|
---|
[86] | 661 | template.setName(frameset, ++next);
|
---|
[4] | 662 | template.setTitle(frameTitle);
|
---|
[86] | 663 | // _Cache.put(template.getName().toLowerCase(), template);
|
---|
[4] | 664 |
|
---|
| 665 | Logger.Log(Logger.SYSTEM, Logger.TDFC, "Creating new frame: "
|
---|
[80] | 666 | + template.getName() + " from TDFC");
|
---|
[4] | 667 |
|
---|
[655] | 668 | template.setOwner(UserSettings.UserName.get());
|
---|
[424] | 669 | template.reset();
|
---|
[4] | 670 | template.resetDateCreated();
|
---|
[427] | 671 |
|
---|
[4] | 672 | for (Item i : template.getItems()) {
|
---|
[80] | 673 | if (ItemUtils.startsWithTag(i, ItemUtils.TAG_PARENT))
|
---|
[4] | 674 | i.setLink(null);
|
---|
| 675 | }
|
---|
[162] | 676 |
|
---|
[130] | 677 | // do auto shrinking of the title IF not in twin frames mode
|
---|
| 678 | Item titleItem = template.getTitleItem();
|
---|
[4] | 679 |
|
---|
[130] | 680 | if (!DisplayIO.isTwinFramesOn()) {
|
---|
| 681 | if ((titleItem.getX() + 1) < template.getNameItem().getX()) {
|
---|
[416] | 682 | while (titleItem.getSize() > Text.MINIMUM_FONT_SIZE
|
---|
| 683 | && titleItem.getBoundsWidth() + titleItem.getX() > template
|
---|
| 684 | .getNameItem().getX()) {
|
---|
[130] | 685 | titleItem.setSize(titleItem.getSize() - 1);
|
---|
[416] | 686 |
|
---|
[130] | 687 | }
|
---|
| 688 | } else {
|
---|
| 689 | System.out.println("Bad title x position: " + titleItem.getX());
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
[416] | 692 |
|
---|
[362] | 693 | // Assign a width to the title.
|
---|
[400] | 694 | titleItem.setRightMargin(template.getNameItem().getX(), true);
|
---|
[416] | 695 |
|
---|
[4] | 696 | return template;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | public static void DisableCache() {
|
---|
| 700 | _UseCache = false;
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | public static void EnableCache() {
|
---|
| 704 | _UseCache = true;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | public static void SuspendCache() {
|
---|
| 708 | if (_UseCache) {
|
---|
| 709 | DisableCache();
|
---|
| 710 | _SuspendedCache = true;
|
---|
| 711 | } else {
|
---|
| 712 | _SuspendedCache = false;
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | public static void ResumeCache() {
|
---|
| 717 | if (_SuspendedCache) {
|
---|
| 718 | EnableCache();
|
---|
| 719 | _SuspendedCache = false;
|
---|
| 720 | }
|
---|
| 721 | }
|
---|
| 722 |
|
---|
[67] | 723 | public static void RefreshCasheImages() {
|
---|
| 724 | SuspendCache();
|
---|
| 725 | for (Frame f : _Cache.values())
|
---|
| 726 | f.setBuffer(null);
|
---|
| 727 | ResumeCache();
|
---|
| 728 | }
|
---|
| 729 |
|
---|
[4] | 730 | /**
|
---|
| 731 | * Creates a new frameset using the given name. This includes creating a new
|
---|
| 732 | * subdirectory in the <code>FRAME_PATH</code> directory, Copying over the
|
---|
| 733 | * default.0 frame from the default frameset, copying the .0 Frame to make a
|
---|
| 734 | * .1 Frame, and creating the frameset's INF file.
|
---|
| 735 | *
|
---|
| 736 | * @param frameset
|
---|
| 737 | * The name of the Frameset to create
|
---|
| 738 | * @return The first Frame of the new Frameset (Frame.1)
|
---|
| 739 | */
|
---|
[8] | 740 | public static Frame CreateFrameset(String frameset, String path)
|
---|
| 741 | throws Exception {
|
---|
[7] | 742 | return CreateFrameset(frameset, path, false);
|
---|
[4] | 743 | }
|
---|
| 744 |
|
---|
| 745 | /**
|
---|
| 746 | * Tests if the given String is a 'proper' framename, that is, the String
|
---|
| 747 | * must begin with a character, end with a number with 0 or more letters and
|
---|
[24] | 748 | * numbers in between.
|
---|
[4] | 749 | *
|
---|
| 750 | * @param frameName
|
---|
| 751 | * The String to test for validity as a frame name
|
---|
| 752 | * @return True if the given framename is proper, false otherwise.
|
---|
| 753 | */
|
---|
| 754 | public static boolean isValidFrameName(String frameName) {
|
---|
| 755 |
|
---|
| 756 | if (frameName == null || frameName.length() < 2)
|
---|
| 757 | return false;
|
---|
| 758 |
|
---|
[24] | 759 | int lastCharIndex = frameName.length() - 1;
|
---|
[4] | 760 | // String must begin with a letter and end with a digit
|
---|
[24] | 761 | if (!Character.isLetter(frameName.charAt(0))
|
---|
| 762 | || !Character.isDigit(frameName.charAt(lastCharIndex)))
|
---|
[4] | 763 | return false;
|
---|
| 764 |
|
---|
[7] | 765 | // All the characters between first and last must be letters
|
---|
[24] | 766 | // or digits
|
---|
| 767 | for (int i = 1; i < lastCharIndex; i++) {
|
---|
[427] | 768 | if (!isValidFrameNameChar(frameName.charAt(i)))
|
---|
[24] | 769 | return false;
|
---|
[4] | 770 | }
|
---|
| 771 | return true;
|
---|
| 772 | }
|
---|
| 773 |
|
---|
[427] | 774 | private static boolean isValidFrameNameChar(char c) {
|
---|
| 775 | return Character.isLetterOrDigit(c) || c == '-';
|
---|
| 776 | }
|
---|
| 777 |
|
---|
[4] | 778 | /**
|
---|
| 779 | * Saves the given Frame to disk in the corresponding frameset directory.
|
---|
| 780 | * This is the same as calling SaveFrame(toSave, true)
|
---|
| 781 | *
|
---|
| 782 | * @param toSave
|
---|
[105] | 783 | * The Frame to save to disk
|
---|
[4] | 784 | */
|
---|
| 785 | public static String SaveFrame(Frame toSave) {
|
---|
| 786 | return SaveFrame(toSave, true);
|
---|
| 787 | }
|
---|
| 788 |
|
---|
[376] | 789 | /**
|
---|
| 790 | * Saves a frame.
|
---|
[416] | 791 | *
|
---|
| 792 | * @param toSave
|
---|
| 793 | * the frame to save
|
---|
| 794 | * @param inc
|
---|
| 795 | * true if the frames counter should be incremented
|
---|
[376] | 796 | * @return the text content of the frame
|
---|
| 797 | */
|
---|
[67] | 798 | public static String SaveFrame(Frame toSave, boolean inc) {
|
---|
| 799 | return SaveFrame(toSave, inc, true);
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[4] | 802 | /**
|
---|
| 803 | * Saves the given Frame to disk in the corresponding frameset directory, if
|
---|
| 804 | * inc is true then the saved frames counter is incremented, otherwise it is
|
---|
| 805 | * untouched.
|
---|
| 806 | *
|
---|
| 807 | * @param toSave
|
---|
[105] | 808 | * The Frame to save to disk
|
---|
[4] | 809 | * @param inc
|
---|
| 810 | * True if the saved frames counter should be incremented, false
|
---|
| 811 | * otherwise.
|
---|
[67] | 812 | * @param checkBackup
|
---|
| 813 | * True if the frame should be checked for the back up tag
|
---|
[4] | 814 | */
|
---|
[67] | 815 | public static String SaveFrame(Frame toSave, boolean inc,
|
---|
| 816 | boolean checkBackup) {
|
---|
[4] | 817 |
|
---|
[162] | 818 | // TODO When loading a frame maybe append onto the event history too-
|
---|
| 819 | // with a
|
---|
[105] | 820 | // break to indicate the end of a session
|
---|
[4] | 821 |
|
---|
[162] | 822 | if (toSave == null || !toSave.hasChanged() || toSave.isSaved()) {
|
---|
[7] | 823 | return "";
|
---|
[105] | 824 | }
|
---|
[4] | 825 |
|
---|
[86] | 826 | // Dont save if the frame is protected and it exists
|
---|
[162] | 827 | if (checkBackup && toSave.isReadOnly()) {
|
---|
[86] | 828 | _Cache.remove(toSave.getName().toLowerCase());
|
---|
| 829 | return "";
|
---|
| 830 | }
|
---|
| 831 |
|
---|
[416] | 832 | /* Dont save the frame if it has the noSave tag */
|
---|
| 833 | if (toSave.hasAnnotation("nosave")) {
|
---|
| 834 | Actions.PerformActionCatchErrors(toSave, null, "Restore");
|
---|
| 835 | return "";
|
---|
| 836 | }
|
---|
| 837 |
|
---|
[298] | 838 | // Save frame that is not local through the Networking classes
|
---|
| 839 | // TODO
|
---|
[306] | 840 | if (!toSave.isLocal()) {
|
---|
| 841 | return FrameShare.getInstance().saveFrame(toSave);
|
---|
| 842 | }
|
---|
[298] | 843 |
|
---|
[416] | 844 | /* Format the frame if it has the autoFormat tag */
|
---|
| 845 | if (toSave.hasAnnotation("autoformat")) {
|
---|
[415] | 846 | Actions.PerformActionCatchErrors(toSave, null, "Format");
|
---|
| 847 | }
|
---|
[416] | 848 |
|
---|
[298] | 849 | /**
|
---|
| 850 | * Get the full path only to determine which format to use for saving
|
---|
| 851 | * the frame. At this stage use Exp format for saving Exp frames only.
|
---|
| 852 | * Later this will be changed so that KMS frames will be updated to the
|
---|
| 853 | * Exp format.
|
---|
| 854 | */
|
---|
| 855 | String fullPath = getFrameFullPathName(toSave.getPath(), toSave
|
---|
| 856 | .getName());
|
---|
| 857 |
|
---|
[67] | 858 | // Check if the frame exists
|
---|
| 859 | if (checkBackup && fullPath == null) {
|
---|
| 860 | // The first time a frame with the backup tag is saved, dont back it
|
---|
| 861 | // up
|
---|
| 862 | checkBackup = false;
|
---|
| 863 | }
|
---|
[601] | 864 |
|
---|
| 865 | FrameWriter writer = null;
|
---|
[589] | 866 | int savedVersion;
|
---|
[4] | 867 | try {
|
---|
| 868 | // if its a new frame or an existing Exp frame...
|
---|
[601] | 869 | if (fullPath == null || fullPath.endsWith(ExpReader.EXTENTION)) {
|
---|
| 870 | writer = new ExpWriter();
|
---|
| 871 | savedVersion = ExpReader.getVersion(fullPath);
|
---|
| 872 | } else {
|
---|
| 873 | writer = new KMSWriter();
|
---|
| 874 | savedVersion = KMSReader.getVersion(fullPath);
|
---|
| 875 | }
|
---|
[348] | 876 |
|
---|
| 877 | // Check if the frame doesnt exist
|
---|
[416] | 878 | // if (savedVersion < 0) {
|
---|
| 879 | // /*
|
---|
| 880 | // * This will happen if the user has two Expeditee's running at
|
---|
| 881 | // * once and closes the first. When the second one closes the
|
---|
| 882 | // * messages directory will have been deleted.
|
---|
| 883 | // */
|
---|
| 884 | // MessageBay
|
---|
| 885 | // .errorMessage("Could not save frame that does not exist: "
|
---|
| 886 | // + toSave.getName());
|
---|
| 887 | // return null;
|
---|
| 888 | // }
|
---|
[348] | 889 |
|
---|
[67] | 890 | // Check if we are trying to save an out of date version
|
---|
[105] | 891 | if (savedVersion > toSave.getVersion()
|
---|
[67] | 892 | && !toSave.getFramesetName().equalsIgnoreCase(
|
---|
[121] | 893 | MessageBay.MESSAGES_FRAMESET_NAME)) {
|
---|
[67] | 894 | // remove this frame from the cache if it is there
|
---|
| 895 | // This will make sure links to the original are set correctly
|
---|
[80] | 896 | _Cache.remove(toSave.getName().toLowerCase());
|
---|
[298] | 897 | int nextnum = ReadINF(toSave.getPath(), toSave
|
---|
[307] | 898 | .getFramesetName(), false) + 1;
|
---|
[67] | 899 | SuspendCache();
|
---|
[80] | 900 | Frame original = LoadFrame(toSave.getName());
|
---|
[67] | 901 | toSave.setFrameNumber(nextnum);
|
---|
| 902 | ResumeCache();
|
---|
| 903 | // Put the modified version in the cache
|
---|
[108] | 904 | addToCache(toSave);
|
---|
[67] | 905 | // Show the messages alerting the user
|
---|
| 906 | Text originalMessage = new Text(-1);
|
---|
[121] | 907 | originalMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
[80] | 908 | originalMessage.setText(original.getName()
|
---|
[67] | 909 | + " was updated by another user.");
|
---|
[80] | 910 | originalMessage.setLink(original.getName());
|
---|
[67] | 911 | Text yourMessage = new Text(-1);
|
---|
[121] | 912 | yourMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
[67] | 913 | yourMessage.setText("Your version was renamed "
|
---|
[80] | 914 | + toSave.getName());
|
---|
| 915 | yourMessage.setLink(toSave.getName());
|
---|
[121] | 916 | MessageBay.displayMessage(originalMessage);
|
---|
| 917 | MessageBay.displayMessage(yourMessage);
|
---|
[67] | 918 | } else if (checkBackup
|
---|
[70] | 919 | && ItemUtils.ContainsExactTag(toSave.getItems(),
|
---|
[4] | 920 | ItemUtils.TAG_BACKUP)) {
|
---|
| 921 | SuspendCache();
|
---|
[427] | 922 | String oldFramesetName = toSave.getFramesetName() + "-old";
|
---|
| 923 |
|
---|
[80] | 924 | Frame original = LoadFrame(toSave.getName());
|
---|
[4] | 925 | if (original == null)
|
---|
| 926 | original = toSave;
|
---|
[80] | 927 | int orignum = original.getNumber();
|
---|
[4] | 928 |
|
---|
[427] | 929 | int nextnum = -1;
|
---|
| 930 | try {
|
---|
| 931 | nextnum = ReadINF(toSave.getPath(), oldFramesetName, false) + 1;
|
---|
| 932 | } catch (RuntimeException e) {
|
---|
| 933 | try {
|
---|
| 934 | CreateFrameset(oldFramesetName, toSave.getPath());
|
---|
| 935 | nextnum = 1;
|
---|
| 936 | } catch (Exception e1) {
|
---|
| 937 | e1.printStackTrace();
|
---|
| 938 | }
|
---|
[429] | 939 | // e.printStackTrace();
|
---|
[427] | 940 | }
|
---|
[429] | 941 |
|
---|
[427] | 942 | if (nextnum > 0) {
|
---|
| 943 | original.setFrameset(oldFramesetName);
|
---|
| 944 | original.setFrameNumber(nextnum);
|
---|
[454] | 945 | original.setPermission(new PermissionPair(UserAppliedPermission.copy));
|
---|
[427] | 946 | original.change();
|
---|
| 947 | SaveFrame(original, false, false);
|
---|
| 948 | }
|
---|
[4] | 949 |
|
---|
[70] | 950 | Item i = ItemUtils.FindExactTag(toSave.getItems(),
|
---|
[4] | 951 | ItemUtils.TAG_BACKUP);
|
---|
[80] | 952 | i.setLink(original.getName());
|
---|
[4] | 953 | toSave.setFrameNumber(orignum);
|
---|
| 954 | ResumeCache();
|
---|
| 955 | }
|
---|
[105] | 956 | // Update general stuff about frame
|
---|
[306] | 957 | setSavedProperties(toSave);
|
---|
[105] | 958 |
|
---|
[162] | 959 | // int oldMode = FrameGraphics.getMode();
|
---|
| 960 | // if (oldMode != FrameGraphics.MODE_XRAY)
|
---|
| 961 | // FrameGraphics.setMode(FrameGraphics.MODE_XRAY, true);
|
---|
[4] | 962 | writer.writeFrame(toSave);
|
---|
[162] | 963 | // FrameGraphics.setMode(oldMode, true);
|
---|
[4] | 964 | toSave.setSaved();
|
---|
[105] | 965 | if (inc) {
|
---|
[80] | 966 | SessionStats.SavedFrame(toSave.getName());
|
---|
[105] | 967 | }
|
---|
[4] | 968 |
|
---|
| 969 | // avoid out-of-sync frames (when in TwinFrames mode)
|
---|
[80] | 970 | if (_Cache.containsKey(toSave.getName().toLowerCase()))
|
---|
[108] | 971 | addToCache(toSave);
|
---|
[4] | 972 |
|
---|
[86] | 973 | Logger.Log(Logger.SYSTEM, Logger.SAVE, "Saving " + toSave.getName()
|
---|
| 974 | + " to disk.");
|
---|
[4] | 975 |
|
---|
| 976 | // check that the INF file is not out of date
|
---|
[307] | 977 | int last = ReadINF(toSave.getPath(), toSave.getFramesetName(),
|
---|
| 978 | false);
|
---|
[80] | 979 | if (last <= toSave.getNumber())
|
---|
[298] | 980 | WriteINF(toSave.getPath(), toSave.getFramesetName(), toSave
|
---|
[80] | 981 | .getName());
|
---|
[4] | 982 |
|
---|
| 983 | // check if this was the profile frame (and thus needs
|
---|
| 984 | // re-parsing)
|
---|
[95] | 985 | if (isProfileFrame(toSave)) {
|
---|
[97] | 986 | Frame profile = FrameIO.LoadFrame(toSave.getFramesetName()
|
---|
| 987 | + "1");
|
---|
| 988 | assert (profile != null);
|
---|
[95] | 989 | FrameUtils.ParseProfile(profile);
|
---|
[4] | 990 | }
|
---|
| 991 | } catch (IOException ioe) {
|
---|
[311] | 992 | ioe.printStackTrace();
|
---|
[95] | 993 | ioe.getStackTrace();
|
---|
[4] | 994 | Logger.Log(ioe);
|
---|
[7] | 995 | return null;
|
---|
[4] | 996 | }
|
---|
| 997 |
|
---|
| 998 | return writer.getFileContents();
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
[95] | 1001 | /**
|
---|
[108] | 1002 | * @param toAdd
|
---|
| 1003 | */
|
---|
| 1004 | public static void addToCache(Frame toAdd) {
|
---|
| 1005 | _Cache.put(toAdd.getName().toLowerCase(), toAdd);
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | /**
|
---|
[95] | 1009 | * Checks if a frame is in the current user profile frameset.
|
---|
[97] | 1010 | *
|
---|
| 1011 | * @param toCheck
|
---|
| 1012 | * the frame to check
|
---|
[95] | 1013 | * @return true if the frame is in the current user profile frameset
|
---|
| 1014 | */
|
---|
| 1015 | public static boolean isProfileFrame(Frame toCheck) {
|
---|
[97] | 1016 | if (toCheck.getNumber() == 0)
|
---|
[95] | 1017 | return false;
|
---|
[348] | 1018 | return toCheck.getPath().equals(PROFILE_PATH);
|
---|
[416] | 1019 | // return toCheck.getFramesetName()
|
---|
| 1020 | // .equalsIgnoreCase(UserSettings.ProfileName);
|
---|
[95] | 1021 | }
|
---|
| 1022 |
|
---|
[4] | 1023 | public static Frame LoadProfile(String userName) {
|
---|
[25] | 1024 | return LoadFrame(userName + "1");
|
---|
[4] | 1025 | }
|
---|
| 1026 |
|
---|
[8] | 1027 | public static Frame CreateNewProfile(String username) throws Exception {
|
---|
[23] | 1028 | Frame profile = CreateFrameset(username, PROFILE_PATH, true);
|
---|
[348] | 1029 | FrameUtils.CreateDefaultProfile(username, profile);
|
---|
[4] | 1030 | return profile;
|
---|
| 1031 | }
|
---|
| 1032 |
|
---|
| 1033 | /**
|
---|
| 1034 | * Reads the INF file that corresponds to the given Frame name
|
---|
| 1035 | *
|
---|
| 1036 | * @param framename
|
---|
| 1037 | * The Frame to lookup the INF file for
|
---|
| 1038 | * @throws IOException
|
---|
| 1039 | * Any exceptions encountered by the BufferedReader used to read
|
---|
| 1040 | * the INF.
|
---|
| 1041 | */
|
---|
[307] | 1042 | public static int ReadINF(String path, String frameset, boolean update)
|
---|
| 1043 | throws IOException {
|
---|
[4] | 1044 | assert (!frameset.endsWith("."));
|
---|
[307] | 1045 | try {
|
---|
| 1046 | // read INF
|
---|
| 1047 | BufferedReader reader;
|
---|
| 1048 | try {
|
---|
| 1049 | // Check on the local drive
|
---|
| 1050 | reader = new BufferedReader(new FileReader(path
|
---|
| 1051 | + frameset.toLowerCase() + File.separator
|
---|
| 1052 | + INF_FILENAME));
|
---|
| 1053 | } catch (Exception e) {
|
---|
| 1054 | reader = new BufferedReader(new FileReader(path
|
---|
| 1055 | + frameset.toLowerCase() + File.separator
|
---|
| 1056 | + frameset.toLowerCase() + ".inf"));
|
---|
| 1057 | }
|
---|
| 1058 | String inf = reader.readLine();
|
---|
| 1059 | reader.close();
|
---|
[4] | 1060 |
|
---|
[307] | 1061 | int next = Conversion.getFrameNumber(inf);
|
---|
| 1062 | // update INF file
|
---|
| 1063 | if (update) {
|
---|
| 1064 | try {
|
---|
| 1065 | WriteINF(path, frameset, frameset + (next + 1));
|
---|
| 1066 | } catch (IOException ioe) {
|
---|
| 1067 | ioe.printStackTrace();
|
---|
| 1068 | Logger.Log(ioe);
|
---|
| 1069 | }
|
---|
| 1070 | }
|
---|
| 1071 | return next;
|
---|
[4] | 1072 | } catch (Exception e) {
|
---|
| 1073 | }
|
---|
| 1074 |
|
---|
[307] | 1075 | // Check peers
|
---|
| 1076 | return FrameShare.getInstance().getInfNumber(path, frameset, update);
|
---|
[4] | 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | /**
|
---|
| 1080 | * Writes the given String out to the INF file corresponding to the current
|
---|
| 1081 | * frameset.
|
---|
| 1082 | *
|
---|
| 1083 | * @param toWrite
|
---|
| 1084 | * The String to write to the file.
|
---|
| 1085 | * @throws IOException
|
---|
| 1086 | * Any exception encountered by the BufferedWriter.
|
---|
| 1087 | */
|
---|
[307] | 1088 | public static void WriteINF(String path, String frameset, String frameName)
|
---|
[4] | 1089 | throws IOException {
|
---|
[307] | 1090 | try {
|
---|
| 1091 | assert (!frameset.endsWith("."));
|
---|
[4] | 1092 |
|
---|
[307] | 1093 | path += frameset.toLowerCase() + File.separator + INF_FILENAME;
|
---|
[4] | 1094 |
|
---|
[307] | 1095 | BufferedWriter writer = new BufferedWriter(new FileWriter(path));
|
---|
| 1096 | writer.write(frameName);
|
---|
| 1097 | writer.close();
|
---|
| 1098 | } catch (Exception e) {
|
---|
| 1099 |
|
---|
| 1100 | }
|
---|
[4] | 1101 | }
|
---|
| 1102 |
|
---|
| 1103 | public static boolean FrameIsCached(String name) {
|
---|
| 1104 | return _Cache.containsKey(name);
|
---|
| 1105 | }
|
---|
[298] | 1106 |
|
---|
[206] | 1107 | /**
|
---|
| 1108 | * Gets a frame from the cache.
|
---|
| 1109 | *
|
---|
| 1110 | * @param name
|
---|
[298] | 1111 | * The frame to get from the cache
|
---|
[206] | 1112 | *
|
---|
[298] | 1113 | * @return The frame from cache. Null if not cached.
|
---|
[206] | 1114 | */
|
---|
| 1115 | public static Frame FrameFromCache(String name) {
|
---|
| 1116 | return _Cache.get(name);
|
---|
| 1117 | }
|
---|
[4] | 1118 |
|
---|
[12] | 1119 | public static String ConvertToValidFramesetName(String toValidate) {
|
---|
[64] | 1120 | assert (toValidate != null && toValidate.length() > 0);
|
---|
| 1121 |
|
---|
[24] | 1122 | StringBuffer result = new StringBuffer();
|
---|
[4] | 1123 |
|
---|
[64] | 1124 | if (Character.isDigit(toValidate.charAt(0))) {
|
---|
[24] | 1125 | result.append(FRAME_NAME_LAST_CHAR);
|
---|
| 1126 | }
|
---|
[64] | 1127 |
|
---|
[24] | 1128 | boolean capital = false;
|
---|
[64] | 1129 | for (int i = 0; i < toValidate.length()
|
---|
| 1130 | && result.length() < MAX_NAME_LENGTH; i++) {
|
---|
[4] | 1131 | char cur = toValidate.charAt(i);
|
---|
| 1132 |
|
---|
| 1133 | // capitalize all characters after spaces
|
---|
[12] | 1134 | if (Character.isLetterOrDigit(cur)) {
|
---|
[4] | 1135 | if (capital) {
|
---|
| 1136 | capital = false;
|
---|
[24] | 1137 | result.append(Character.toUpperCase(cur));
|
---|
[4] | 1138 | } else
|
---|
[24] | 1139 | result.append(cur);
|
---|
[12] | 1140 | } else {
|
---|
| 1141 | capital = true;
|
---|
[4] | 1142 | }
|
---|
| 1143 | }
|
---|
[24] | 1144 | assert (result.length() > 0);
|
---|
| 1145 | int lastCharIndex = result.length() - 1;
|
---|
[64] | 1146 | if (!Character.isLetter(result.charAt(lastCharIndex))) {
|
---|
| 1147 | if (lastCharIndex == MAX_NAME_LENGTH - 1)
|
---|
| 1148 | result.setCharAt(lastCharIndex, FRAME_NAME_LAST_CHAR);
|
---|
[24] | 1149 | else
|
---|
| 1150 | result.append(FRAME_NAME_LAST_CHAR);
|
---|
| 1151 | }
|
---|
[64] | 1152 |
|
---|
[72] | 1153 | assert (isValidFramesetName(result.toString()));
|
---|
[24] | 1154 | return result.toString();
|
---|
[4] | 1155 | }
|
---|
| 1156 |
|
---|
[72] | 1157 | public static Frame CreateNewFrame(Item linker) throws RuntimeException {
|
---|
[80] | 1158 | String title = linker.getName();
|
---|
[4] | 1159 |
|
---|
[80] | 1160 | String templateLink = linker.getAbsoluteLinkTemplate();
|
---|
| 1161 | String framesetLink = linker.getAbsoluteLinkFrameset();
|
---|
[72] | 1162 | String frameset = (framesetLink != null ? framesetLink : DisplayIO
|
---|
| 1163 | .getCurrentFrame().getFramesetName());
|
---|
[4] | 1164 |
|
---|
| 1165 | Frame newFrame = FrameIO.CreateFrame(frameset, title, templateLink);
|
---|
| 1166 | return newFrame;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | /**
|
---|
| 1170 | * Creates a new Frameset on disk, including a .0, .1, and .inf files. The
|
---|
| 1171 | * Default.0 frame is copied to make the initial .0 and .1 Frames
|
---|
| 1172 | *
|
---|
| 1173 | * @param name
|
---|
| 1174 | * The Frameset name to use
|
---|
| 1175 | * @return The name of the first Frame in the newly created Frameset (the .1
|
---|
| 1176 | * frame)
|
---|
| 1177 | */
|
---|
[8] | 1178 | public static Frame CreateNewFrameset(String name) throws Exception {
|
---|
[298] | 1179 | String path = DisplayIO.getCurrentFrame().getPath();
|
---|
[4] | 1180 |
|
---|
| 1181 | // if current frameset is profile directory change it to framesets
|
---|
| 1182 | if (path.equals(FrameIO.PROFILE_PATH)) {
|
---|
| 1183 | path = FrameIO.FRAME_PATH;
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
[429] | 1186 | Frame newFrame = FrameIO.CreateFrameset(name, path);
|
---|
| 1187 |
|
---|
| 1188 | if (newFrame == null) {
|
---|
| 1189 | // Cant create directories if the path is readonly or there is no
|
---|
| 1190 | // space available
|
---|
| 1191 | newFrame = FrameIO.CreateFrameset(name, FrameIO.FRAME_PATH);
|
---|
| 1192 | }
|
---|
| 1193 |
|
---|
| 1194 | if (newFrame == null) {
|
---|
| 1195 | // TODO handle running out of disk space here
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | return newFrame;
|
---|
[4] | 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | /**
|
---|
| 1202 | *
|
---|
| 1203 | * @param frameset
|
---|
| 1204 | * @return
|
---|
| 1205 | */
|
---|
| 1206 | public static int getLastNumber(String frameset) { // Rob thinks it might
|
---|
| 1207 | // have been
|
---|
| 1208 | // GetHighestNumExFrame
|
---|
| 1209 | // TODO minimise the number of frames being read in!!
|
---|
| 1210 | int num = -1;
|
---|
| 1211 |
|
---|
| 1212 | Frame zero = LoadFrame(frameset + "0");
|
---|
| 1213 |
|
---|
| 1214 | // the frameset does not exist (or has no 0 frame)
|
---|
| 1215 | if (zero == null)
|
---|
| 1216 | return -1;
|
---|
| 1217 |
|
---|
| 1218 | try {
|
---|
[307] | 1219 | num = ReadINF(zero.getPath(), frameset, false);
|
---|
[4] | 1220 | } catch (IOException e) {
|
---|
| 1221 | // TODO Auto-generated catch block
|
---|
| 1222 | // e.printStackTrace();
|
---|
| 1223 | }
|
---|
| 1224 |
|
---|
| 1225 | /*
|
---|
| 1226 | * Michael doesnt think the code below is really needed... it will just
|
---|
| 1227 | * slow things down when we are reading frames over a network***** for (;
|
---|
| 1228 | * num >= 0; num--) { System.out.println("This code is loading frames to
|
---|
| 1229 | * find the highest existing frame..."); if (LoadFrame(frameset + num) !=
|
---|
| 1230 | * null) break; }
|
---|
| 1231 | */
|
---|
| 1232 |
|
---|
| 1233 | return num;
|
---|
| 1234 | }
|
---|
| 1235 |
|
---|
[97] | 1236 | /**
|
---|
| 1237 | * Checks if a given frameset is accessable.
|
---|
| 1238 | *
|
---|
| 1239 | * @param framesetName
|
---|
| 1240 | * @return
|
---|
| 1241 | */
|
---|
| 1242 | public static Boolean canAccessFrameset(String framesetName) {
|
---|
| 1243 | framesetName = framesetName.toLowerCase();
|
---|
[655] | 1244 | for (String path : UserSettings.FrameDirs.get()) {
|
---|
[97] | 1245 | if ((new File(path + framesetName)).exists())
|
---|
| 1246 | return true;
|
---|
| 1247 | }
|
---|
| 1248 | return false;
|
---|
[7] | 1249 | }
|
---|
| 1250 |
|
---|
| 1251 | public static Frame CreateFrameset(String frameset, String path,
|
---|
[8] | 1252 | boolean recreate) throws Exception {
|
---|
[7] | 1253 | String conversion = frameset + " --> ";
|
---|
| 1254 |
|
---|
[72] | 1255 | if (!isValidFramesetName(frameset)) {
|
---|
[7] | 1256 | throw new Exception("Invalid frameset name");
|
---|
[13] | 1257 | }
|
---|
[24] | 1258 |
|
---|
[97] | 1259 | if (!recreate && FrameIO.canAccessFrameset(frameset)) {
|
---|
[8] | 1260 | throw new ExistingFramesetException(frameset);
|
---|
[7] | 1261 | }
|
---|
| 1262 |
|
---|
| 1263 | conversion += frameset;
|
---|
| 1264 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Frameset Name: "
|
---|
| 1265 | + conversion);
|
---|
| 1266 | conversion = frameset;
|
---|
| 1267 |
|
---|
| 1268 | /**
|
---|
| 1269 | * TODO: Update this to exclude any\all invalid filename characters
|
---|
| 1270 | */
|
---|
| 1271 | // ignore annotation character
|
---|
| 1272 | if (frameset.startsWith("@"))
|
---|
| 1273 | frameset = frameset.substring(1);
|
---|
| 1274 |
|
---|
| 1275 | conversion += " --> " + frameset;
|
---|
| 1276 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Name: " + conversion);
|
---|
| 1277 |
|
---|
| 1278 | // create the new Frameset directory
|
---|
| 1279 | File dir = new File(path + frameset.toLowerCase() + File.separator);
|
---|
| 1280 |
|
---|
[429] | 1281 | // If the directory doesnt already exist then create it...
|
---|
| 1282 | if (!dir.exists()) {
|
---|
| 1283 | // If the directory couldnt be created, then there is something
|
---|
| 1284 | // wrong... ie. The disk is full.
|
---|
| 1285 | if (!dir.mkdirs()) {
|
---|
| 1286 | return null;
|
---|
| 1287 | }
|
---|
| 1288 | }
|
---|
[7] | 1289 |
|
---|
| 1290 | // create the new INF file
|
---|
| 1291 | try {
|
---|
[25] | 1292 | WriteINF(path, frameset, frameset + '1');
|
---|
[7] | 1293 | } catch (IOException ioe) {
|
---|
| 1294 | ioe.printStackTrace();
|
---|
| 1295 | Logger.Log(ioe);
|
---|
| 1296 | }
|
---|
| 1297 |
|
---|
| 1298 | SuspendCache();
|
---|
| 1299 | // copy the default .0 and .1 files
|
---|
[98] | 1300 | Frame base = null;
|
---|
[7] | 1301 | try {
|
---|
[655] | 1302 | base = LoadFrame(UserSettings.DefaultFrame.get());
|
---|
[7] | 1303 | } catch (Exception e) {
|
---|
[98] | 1304 | }
|
---|
| 1305 | // The frame may not be accessed for various reasons... in all these
|
---|
| 1306 | // cases just create a new one
|
---|
| 1307 | if (base == null) {
|
---|
[7] | 1308 | base = new Frame();
|
---|
| 1309 | }
|
---|
[98] | 1310 |
|
---|
[7] | 1311 | ResumeCache();
|
---|
| 1312 |
|
---|
[424] | 1313 | base.reset();
|
---|
[7] | 1314 | base.resetDateCreated();
|
---|
| 1315 | base.setFrameset(frameset);
|
---|
| 1316 | base.setFrameNumber(0);
|
---|
[24] | 1317 | base.setTitle(base.getFramesetName() + "0");
|
---|
[298] | 1318 | base.setPath(path);
|
---|
[7] | 1319 | base.change();
|
---|
[655] | 1320 | base.setOwner(UserSettings.UserName.get());
|
---|
[7] | 1321 | SaveFrame(base, false);
|
---|
| 1322 |
|
---|
[424] | 1323 | base.reset();
|
---|
[7] | 1324 | base.resetDateCreated();
|
---|
| 1325 | base.setFrameNumber(1);
|
---|
| 1326 | base.setTitle(frameset);
|
---|
| 1327 | base.change();
|
---|
[655] | 1328 | base.setOwner(UserSettings.UserName.get());
|
---|
[7] | 1329 | SaveFrame(base, true);
|
---|
| 1330 |
|
---|
| 1331 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Created new frameset: "
|
---|
| 1332 | + frameset);
|
---|
| 1333 |
|
---|
| 1334 | return base;
|
---|
[24] | 1335 | }
|
---|
| 1336 |
|
---|
| 1337 | /**
|
---|
| 1338 | * Tests if a frameset name is valid. That is it must begin and end with a
|
---|
| 1339 | * letter and contain only letters and digits in between.
|
---|
| 1340 | *
|
---|
[64] | 1341 | * @param frameset
|
---|
| 1342 | * the name to be tested
|
---|
[24] | 1343 | * @return true if the frameset name is valid
|
---|
| 1344 | */
|
---|
[72] | 1345 | public static boolean isValidFramesetName(String frameset) {
|
---|
[24] | 1346 | if (frameset == null) {
|
---|
| 1347 | return false;
|
---|
| 1348 | }
|
---|
[64] | 1349 |
|
---|
[24] | 1350 | int nameLength = frameset.length();
|
---|
[64] | 1351 | if (frameset.length() <= 0 || nameLength > MAX_NAME_LENGTH) {
|
---|
[24] | 1352 | return false;
|
---|
| 1353 | }
|
---|
[64] | 1354 |
|
---|
| 1355 | int lastCharIndex = nameLength - 1;
|
---|
| 1356 |
|
---|
| 1357 | if (!Character.isLetter(frameset.charAt(0))
|
---|
| 1358 | || !Character.isLetter(frameset.charAt(lastCharIndex)))
|
---|
[24] | 1359 | return false;
|
---|
| 1360 |
|
---|
| 1361 | for (int i = 1; i < lastCharIndex; i++) {
|
---|
[427] | 1362 | if (!isValidFrameNameChar(frameset.charAt(i))) {
|
---|
[13] | 1363 | return false;
|
---|
| 1364 | }
|
---|
| 1365 | }
|
---|
| 1366 | return true;
|
---|
[7] | 1367 | }
|
---|
| 1368 |
|
---|
[298] | 1369 | public static boolean deleteFrameset(String framesetName) {
|
---|
| 1370 | return moveFrameset(framesetName, FrameIO.TRASH_PATH);
|
---|
| 1371 | }
|
---|
| 1372 |
|
---|
| 1373 | public static boolean moveFrameset(String framesetName,
|
---|
| 1374 | String destinationFolder) {
|
---|
[97] | 1375 | if (!FrameIO.canAccessFrameset(framesetName))
|
---|
[7] | 1376 | return false;
|
---|
[416] | 1377 | // Clear the cache
|
---|
[362] | 1378 | _Cache.clear();
|
---|
[416] | 1379 |
|
---|
[64] | 1380 | // Search all the available directories for the directory
|
---|
[655] | 1381 | for (String path : UserSettings.FrameDirs.get()) {
|
---|
[7] | 1382 | String source = path + framesetName.toLowerCase() + File.separator;
|
---|
| 1383 | File framesetDirectory = new File(source);
|
---|
[362] | 1384 | // Once we have found the directory move it
|
---|
[7] | 1385 | if (framesetDirectory.exists()) {
|
---|
[298] | 1386 | String destPath = destinationFolder
|
---|
[64] | 1387 | + framesetName.toLowerCase();
|
---|
| 1388 | int copyNumber = 1;
|
---|
| 1389 | File dest = new File(destPath + File.separator);
|
---|
[362] | 1390 | // Create the destination folder if it doesnt already exist
|
---|
[64] | 1391 | if (!dest.getParentFile().exists())
|
---|
| 1392 | dest.mkdirs();
|
---|
[416] | 1393 | // If a frameset with the same name is already in the
|
---|
| 1394 | // destination add
|
---|
[64] | 1395 | // a number to the end
|
---|
| 1396 | while (dest.exists()) {
|
---|
[67] | 1397 | dest = new File(destPath + ++copyNumber + File.separator);
|
---|
[64] | 1398 | }
|
---|
[98] | 1399 | if (!framesetDirectory.renameTo(dest)) {
|
---|
| 1400 | for (File f : framesetDirectory.listFiles()) {
|
---|
| 1401 | if (!f.delete())
|
---|
| 1402 | return false;
|
---|
| 1403 | }
|
---|
| 1404 | if (!framesetDirectory.delete())
|
---|
| 1405 | return false;
|
---|
| 1406 | }
|
---|
| 1407 | return true;
|
---|
[7] | 1408 | }
|
---|
| 1409 | }
|
---|
| 1410 | return false;
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
| 1413 | public static boolean CopyFrameset(String framesetToCopy,
|
---|
| 1414 | String copiedFrameset) throws Exception {
|
---|
[97] | 1415 | if (!FrameIO.canAccessFrameset(framesetToCopy))
|
---|
[7] | 1416 | return false;
|
---|
[97] | 1417 | if (FrameIO.canAccessFrameset(copiedFrameset))
|
---|
[7] | 1418 | return false;
|
---|
| 1419 | // search through all the directories to find the frameset we are
|
---|
| 1420 | // copying
|
---|
[655] | 1421 | for (String path : UserSettings.FrameDirs.get()) {
|
---|
[7] | 1422 | String source = path + framesetToCopy.toLowerCase()
|
---|
| 1423 | + File.separator;
|
---|
| 1424 | File framesetDirectory = new File(source);
|
---|
| 1425 | if (framesetDirectory.exists()) {
|
---|
| 1426 | // copy the frameset
|
---|
| 1427 | File copyFramesetDirectory = new File(path
|
---|
| 1428 | + copiedFrameset.toLowerCase() + File.separator);
|
---|
| 1429 | if (!copyFramesetDirectory.mkdirs())
|
---|
| 1430 | return false;
|
---|
| 1431 | // copy each of the frames
|
---|
| 1432 | for (File f : framesetDirectory.listFiles()) {
|
---|
[97] | 1433 | // Ignore hidden files
|
---|
| 1434 | if (f.getName().charAt(0) == '.')
|
---|
| 1435 | continue;
|
---|
[7] | 1436 | String copyPath = copyFramesetDirectory.getAbsolutePath()
|
---|
| 1437 | + File.separator + f.getName();
|
---|
| 1438 | FrameIO.copyFile(f.getAbsolutePath(), copyPath);
|
---|
| 1439 | }
|
---|
| 1440 | return true;
|
---|
| 1441 | }
|
---|
| 1442 | }
|
---|
| 1443 | return false;
|
---|
| 1444 | }
|
---|
| 1445 |
|
---|
[80] | 1446 | /**
|
---|
| 1447 | * Copies a file from one location to another.
|
---|
[86] | 1448 | *
|
---|
[80] | 1449 | * @param existingFile
|
---|
| 1450 | * @param newFileName
|
---|
| 1451 | * @throws Exception
|
---|
| 1452 | */
|
---|
[7] | 1453 | public static void copyFile(String existingFile, String newFileName)
|
---|
| 1454 | throws Exception {
|
---|
| 1455 | FileInputStream is = new FileInputStream(existingFile);
|
---|
| 1456 | FileOutputStream os = new FileOutputStream(newFileName, false);
|
---|
| 1457 | int data;
|
---|
| 1458 | while ((data = is.read()) != -1) {
|
---|
| 1459 | os.write(data);
|
---|
| 1460 | }
|
---|
| 1461 | os.flush();
|
---|
| 1462 | os.close();
|
---|
| 1463 | is.close();
|
---|
| 1464 | }
|
---|
| 1465 |
|
---|
| 1466 | /**
|
---|
| 1467 | * Saves a frame regardless of whether or not the frame is marked as having
|
---|
| 1468 | * been changed.
|
---|
| 1469 | *
|
---|
| 1470 | * @param frame
|
---|
| 1471 | * the frame to save
|
---|
| 1472 | * @return the contents of the frame or null if it could not be saved
|
---|
| 1473 | */
|
---|
| 1474 | public static String ForceSaveFrame(Frame frame) {
|
---|
| 1475 | frame.change();
|
---|
| 1476 | return SaveFrame(frame, false);
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
[72] | 1479 | public static boolean isValidLink(String frameName) {
|
---|
[86] | 1480 | return frameName == null || isPositiveInteger(frameName)
|
---|
| 1481 | || isValidFrameName(frameName);
|
---|
[72] | 1482 | }
|
---|
| 1483 |
|
---|
[306] | 1484 | public static void SavePublicFrame(String peerName, String frameName,
|
---|
| 1485 | int version, BufferedReader packetContents) {
|
---|
| 1486 | // TODO handle versioning - add version to the header
|
---|
| 1487 | // Remote user uploads version based on an old version
|
---|
| 1488 |
|
---|
| 1489 | // Remove it from the cache so that next time it is loaded we get the up
|
---|
| 1490 | // todate version
|
---|
| 1491 | _Cache.remove(frameName.toLowerCase());
|
---|
| 1492 |
|
---|
| 1493 | // Save to file
|
---|
| 1494 | String filename = PUBLIC_PATH + Conversion.getFramesetName(frameName)
|
---|
| 1495 | + File.separator + Conversion.getFrameNumber(frameName)
|
---|
[601] | 1496 | + ExpReader.EXTENTION;
|
---|
[306] | 1497 |
|
---|
| 1498 | File file = new File(filename);
|
---|
| 1499 | // Ensure the file exists
|
---|
| 1500 | if (file.exists()) {
|
---|
| 1501 | // Check the versions
|
---|
[601] | 1502 | int savedVersion = ExpReader.getVersion(filename);
|
---|
[306] | 1503 |
|
---|
| 1504 | if (savedVersion > version) {
|
---|
| 1505 | // remove this frame from the cache if it is there
|
---|
| 1506 | // This will make sure links to the original are set correctly
|
---|
| 1507 | // _Cache.remove(frameName.toLowerCase());
|
---|
| 1508 |
|
---|
| 1509 | int nextNum = 0;
|
---|
| 1510 | try {
|
---|
| 1511 | nextNum = ReadINF(PUBLIC_PATH, Conversion
|
---|
[307] | 1512 | .getFramesetName(frameName), false) + 1;
|
---|
[306] | 1513 | } catch (IOException e) {
|
---|
| 1514 | e.printStackTrace();
|
---|
| 1515 | }
|
---|
| 1516 |
|
---|
| 1517 | String newName = Conversion.getFramesetName(frameName)
|
---|
| 1518 | + nextNum;
|
---|
| 1519 | filename = PUBLIC_PATH + Conversion.getFramesetName(frameName)
|
---|
[601] | 1520 | + File.separator + nextNum + ExpReader.EXTENTION;
|
---|
[306] | 1521 |
|
---|
| 1522 | // Show the messages alerting the user
|
---|
| 1523 | Text originalMessage = new Text(-1);
|
---|
| 1524 | originalMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
| 1525 | originalMessage.setText(frameName + " was edited by "
|
---|
| 1526 | + peerName);
|
---|
| 1527 | originalMessage.setLink(frameName);
|
---|
| 1528 | Text yourMessage = new Text(-1);
|
---|
| 1529 | yourMessage.setColor(MessageBay.ERROR_COLOR);
|
---|
| 1530 | yourMessage.setText("Their version was renamed " + newName);
|
---|
| 1531 | yourMessage.setLink(newName);
|
---|
| 1532 | MessageBay.displayMessage(originalMessage);
|
---|
| 1533 | MessageBay.displayMessage(yourMessage);
|
---|
| 1534 |
|
---|
| 1535 | Frame editedFrame = FrameIO.LoadFrame(frameName);
|
---|
| 1536 |
|
---|
| 1537 | FrameShare.getInstance().sendMessage(
|
---|
| 1538 | frameName + " was recently edited by "
|
---|
| 1539 | + editedFrame.getLastModifyUser(), peerName);
|
---|
| 1540 | FrameShare.getInstance().sendMessage(
|
---|
| 1541 | "Your version was renamed " + newName, peerName);
|
---|
| 1542 | }
|
---|
[307] | 1543 | }
|
---|
[306] | 1544 |
|
---|
[307] | 1545 | // Save the new version
|
---|
| 1546 | try {
|
---|
[348] | 1547 | // FileWriter fw = new FileWriter(file);
|
---|
| 1548 |
|
---|
| 1549 | // Open an Output Stream Writer to set encoding
|
---|
| 1550 | OutputStream fout = new FileOutputStream(file);
|
---|
| 1551 | OutputStream bout = new BufferedOutputStream(fout);
|
---|
| 1552 | Writer fw = new OutputStreamWriter(bout, "UTF-8");
|
---|
| 1553 |
|
---|
[307] | 1554 | String nextLine = null;
|
---|
| 1555 | while ((nextLine = packetContents.readLine()) != null) {
|
---|
| 1556 | fw.write(nextLine + '\n');
|
---|
[306] | 1557 | }
|
---|
[307] | 1558 | fw.flush();
|
---|
| 1559 | fw.close();
|
---|
| 1560 | MessageBay.displayMessage("Saved remote frame: " + frameName);
|
---|
| 1561 | } catch (IOException e) {
|
---|
| 1562 | MessageBay.errorMessage("Error remote saving " + frameName + ": "
|
---|
| 1563 | + e.getMessage());
|
---|
| 1564 | e.printStackTrace();
|
---|
[306] | 1565 | }
|
---|
[307] | 1566 | // } else {
|
---|
| 1567 | //
|
---|
| 1568 | //
|
---|
| 1569 | //
|
---|
| 1570 | // MessageBay
|
---|
| 1571 | // .errorMessage("Recieved save request for unknown public frame: "
|
---|
| 1572 | // + frameName);
|
---|
| 1573 | // }
|
---|
[306] | 1574 | }
|
---|
| 1575 |
|
---|
| 1576 | public static void setSavedProperties(Frame toSave) {
|
---|
[419] | 1577 | toSave.setLastModifyDate(Formatter.getDateTime());
|
---|
[655] | 1578 | toSave.setLastModifyUser(UserSettings.UserName.get());
|
---|
[306] | 1579 | toSave.setVersion(toSave.getVersion() + 1);
|
---|
| 1580 | Time darkTime = new Time(SessionStats.getFrameDarkTime().getTime()
|
---|
| 1581 | + toSave.getDarkTime().getTime());
|
---|
| 1582 | Time activeTime = new Time(SessionStats.getFrameActiveTime().getTime()
|
---|
| 1583 | + toSave.getActiveTime().getTime());
|
---|
| 1584 | toSave.setDarkTime(darkTime);
|
---|
| 1585 | toSave.setActiveTime(activeTime);
|
---|
| 1586 | }
|
---|
| 1587 |
|
---|
[4] | 1588 | } |
---|