Skip to content

Commit

Permalink
Backup clip database on startup every X days.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Feb 2, 2018
1 parent 1011deb commit 81d039e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/com/esotericsoftware/clippy/Clippy.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
import com.esotericsoftware.clippy.Win.POINT;
import com.esotericsoftware.clippy.util.MultiplexOutputStream;
import com.esotericsoftware.clippy.util.TextItem;
import com.esotericsoftware.clippy.util.Util;
import com.esotericsoftware.minlog.Log;
import com.esotericsoftware.minlog.Log.Logger;
import com.sun.jna.WString;

// BOZO - Favorites that always show up before others when searching.
// BOZO - Don't auto paste if alt is down.

/** @author Nathan Sweet */
public class Clippy {
Expand Down Expand Up @@ -136,6 +138,24 @@ public void uncaughtException (Thread thread, Throwable ex) {

TextItem.font = Font.decode(config.font);

// Backup clip database on startup every X days.
File backupDir = new File(System.getProperty("user.home"), ".clippy/db-backup2");
File oldestDir = new File(System.getProperty("user.home"), ".clippy/db-backup1");
if (backupDir.lastModified() < oldestDir.lastModified()) {
File temp = oldestDir;
oldestDir = backupDir;
backupDir = temp;
}
if (System.currentTimeMillis() - backupDir.lastModified() > 1000l * 60 * 60 * 24 * config.clipBackupDays) {
try {
if (INFO) info("Backing up clip database: " + oldestDir.getAbsolutePath());
Util.delete(oldestDir);
Util.copy(new File(System.getProperty("user.home"), ".clippy/db"), oldestDir);
} catch (IOException ex) {
if (ERROR) error("Error backing up clip database.", ex);
}
}

try {
db = new ClipDataStore();
} catch (SQLException ex) {
Expand Down
2 changes: 2 additions & 0 deletions src/com/esotericsoftware/clippy/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class Config {
public boolean popupPastes = true;
public String font = "Consolas-14";

public int clipBackupDays = 7;

public String screenshotHotkey = null;
public String screenshotAppHotkey = "ctrl alt shift BACK_SLASH";
public String screenshotRegionHotkey = "ctrl alt BACK_SLASH";
Expand Down
42 changes: 42 additions & 0 deletions src/com/esotericsoftware/clippy/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Robot;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand Down Expand Up @@ -195,6 +198,45 @@ static private boolean canWrite (File file) {
}
}

static public void delete (File file) {
if (file.isDirectory()) {
for (File child : file.listFiles())
delete(child);
}
file.delete();
}

static public void copy (File src, File dest) throws IOException {
if (src.isDirectory()) {
dest.mkdirs();
for (File child : src.listFiles())
copy(child, new File(dest, child.getName()));
return;
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
while (true) {
int count = in.read(buffer);
if (count == -1) break;
out.write(buffer, 0, count);
}
} finally {
close(in);
close(out);
}
}

static public void close (Closeable c) {
try {
if (c != null) c.close();
} catch (Throwable ignored) {
}
}

static public String id (int length) {
int alphabetLength = alphabet.length();
StringBuilder buffer = new StringBuilder();
Expand Down

0 comments on commit 81d039e

Please sign in to comment.