Store user preferences backup only once every ~3 months

This commit is contained in:
Reinhard Pointner 2019-03-11 17:35:14 +07:00
parent 569575db0d
commit 7b971fd34c
2 changed files with 26 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -81,7 +82,7 @@ public class Main {
Settings.forPackage(Main.class).clear();
// restore preferences on start if empty (TODO: remove after a few releases)
ApplicationFolder.AppData.resolve("preferences.backup.xml").delete();
getPreferencesBackupFile().delete();
}
// clear caches
@ -143,7 +144,7 @@ public class Main {
// restore preferences on start if empty (TODO: remove after a few releases)
try {
if (Preferences.userNodeForPackage(Main.class).keys().length == 0) {
File f = ApplicationFolder.AppData.resolve("preferences.backup.xml");
File f = getPreferencesBackupFile();
if (f.exists()) {
log.fine("Restore user preferences: " + f);
Settings.restore(f);
@ -242,7 +243,15 @@ public class Main {
}
// restore preferences on start if empty (TODO: remove after a few releases)
Settings.store(ApplicationFolder.AppData.resolve("preferences.backup.xml"));
try {
File f = getPreferencesBackupFile();
if (!f.exists() || !lastModifiedWithin(f, Duration.ofDays(90))) {
log.fine("Store user preferences: " + f);
Settings.store(f);
}
} catch (Exception e) {
debug.log(Level.WARNING, "Failed to store preferences", e);
}
System.exit(0);
}));
@ -271,6 +280,10 @@ public class Main {
frame.setVisible(true);
}
private static File getPreferencesBackupFile() {
return ApplicationFolder.AppData.resolve("preferences.backup.xml");
}
/**
* Show update notifications if updates are available
*/

View File

@ -33,6 +33,8 @@ import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
@ -431,6 +433,14 @@ public final class FileUtilities {
return true;
}
public static boolean lastModifiedWithin(File f, Duration d) throws IOException {
Instant now = Instant.now();
Instant lastModifiedTime = Files.getLastModifiedTime(f.toPath()).toInstant();
Duration age = Duration.between(lastModifiedTime, now);
return d.compareTo(age) > 0;
}
public static List<File> sortByUniquePath(Collection<File> files) {
TreeSet<File> sortedSet = new TreeSet<File>(CASE_INSENSITIVE_PATH_ORDER); // sort by unique lower-case paths
sortedSet.addAll(files);