Skip to content

Commit 81d039e

Browse files
committed
Backup clip database on startup every X days.
1 parent 1011deb commit 81d039e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/com/esotericsoftware/clippy/Clippy.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@
4747
import com.esotericsoftware.clippy.Win.POINT;
4848
import com.esotericsoftware.clippy.util.MultiplexOutputStream;
4949
import com.esotericsoftware.clippy.util.TextItem;
50+
import com.esotericsoftware.clippy.util.Util;
5051
import com.esotericsoftware.minlog.Log;
5152
import com.esotericsoftware.minlog.Log.Logger;
5253
import com.sun.jna.WString;
5354

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

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

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

141+
// Backup clip database on startup every X days.
142+
File backupDir = new File(System.getProperty("user.home"), ".clippy/db-backup2");
143+
File oldestDir = new File(System.getProperty("user.home"), ".clippy/db-backup1");
144+
if (backupDir.lastModified() < oldestDir.lastModified()) {
145+
File temp = oldestDir;
146+
oldestDir = backupDir;
147+
backupDir = temp;
148+
}
149+
if (System.currentTimeMillis() - backupDir.lastModified() > 1000l * 60 * 60 * 24 * config.clipBackupDays) {
150+
try {
151+
if (INFO) info("Backing up clip database: " + oldestDir.getAbsolutePath());
152+
Util.delete(oldestDir);
153+
Util.copy(new File(System.getProperty("user.home"), ".clippy/db"), oldestDir);
154+
} catch (IOException ex) {
155+
if (ERROR) error("Error backing up clip database.", ex);
156+
}
157+
}
158+
139159
try {
140160
db = new ClipDataStore();
141161
} catch (SQLException ex) {

src/com/esotericsoftware/clippy/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class Config {
6363
public boolean popupPastes = true;
6464
public String font = "Consolas-14";
6565

66+
public int clipBackupDays = 7;
67+
6668
public String screenshotHotkey = null;
6769
public String screenshotAppHotkey = "ctrl alt shift BACK_SLASH";
6870
public String screenshotRegionHotkey = "ctrl alt BACK_SLASH";

src/com/esotericsoftware/clippy/util/Util.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
import java.awt.GraphicsEnvironment;
2727
import java.awt.Point;
2828
import java.awt.Robot;
29+
import java.io.Closeable;
2930
import java.io.File;
31+
import java.io.FileInputStream;
3032
import java.io.FileOutputStream;
3133
import java.io.IOException;
3234
import java.io.InputStream;
35+
import java.io.OutputStream;
3336
import java.io.OutputStreamWriter;
3437
import java.io.UnsupportedEncodingException;
3538
import java.net.URLEncoder;
@@ -195,6 +198,45 @@ static private boolean canWrite (File file) {
195198
}
196199
}
197200

201+
static public void delete (File file) {
202+
if (file.isDirectory()) {
203+
for (File child : file.listFiles())
204+
delete(child);
205+
}
206+
file.delete();
207+
}
208+
209+
static public void copy (File src, File dest) throws IOException {
210+
if (src.isDirectory()) {
211+
dest.mkdirs();
212+
for (File child : src.listFiles())
213+
copy(child, new File(dest, child.getName()));
214+
return;
215+
}
216+
InputStream in = null;
217+
OutputStream out = null;
218+
try {
219+
in = new FileInputStream(src);
220+
out = new FileOutputStream(dest);
221+
byte[] buffer = new byte[1024];
222+
while (true) {
223+
int count = in.read(buffer);
224+
if (count == -1) break;
225+
out.write(buffer, 0, count);
226+
}
227+
} finally {
228+
close(in);
229+
close(out);
230+
}
231+
}
232+
233+
static public void close (Closeable c) {
234+
try {
235+
if (c != null) c.close();
236+
} catch (Throwable ignored) {
237+
}
238+
}
239+
198240
static public String id (int length) {
199241
int alphabetLength = alphabet.length();
200242
StringBuilder buffer = new StringBuilder();

0 commit comments

Comments
 (0)