From 1dd3e9d86fec1a676fe532084806f31c06d8636f Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Thu, 28 Nov 2019 14:32:11 +0000 Subject: [PATCH] Add purge backup files menu entry --- .../audiobookrecorder/AudiobookRecorder.java | 9 +++++++++ src/uk/co/majenko/audiobookrecorder/Book.java | 10 ++++++++++ src/uk/co/majenko/audiobookrecorder/Chapter.java | 10 ++++++++++ src/uk/co/majenko/audiobookrecorder/Sentence.java | 13 +++++++++++++ 4 files changed, 42 insertions(+) diff --git a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java index 8025bb1..04dd422 100644 --- a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java +++ b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java @@ -58,6 +58,7 @@ public class AudiobookRecorder extends JFrame { JMenuItem bookNewChapter; JMenuItem bookExportAudio; + JMenuItem bookPurgeBackups; JMenu bookVisitACX; JMenuItem bookVisitTitle; JMenuItem bookVisitAudition; @@ -212,8 +213,16 @@ public class AudiobookRecorder extends JFrame { } }); + bookPurgeBackups = new JMenuItem("Purge Backups"); + bookPurgeBackups.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + book.purgeBackups(); + } + }); + bookMenu.add(bookNewChapter); bookMenu.add(bookExportAudio); + bookMenu.add(bookPurgeBackups); bookVisitACX = new JMenu("Visit ACX"); bookMenu.add(bookVisitACX); diff --git a/src/uk/co/majenko/audiobookrecorder/Book.java b/src/uk/co/majenko/audiobookrecorder/Book.java index 39399e7..3a576db 100644 --- a/src/uk/co/majenko/audiobookrecorder/Book.java +++ b/src/uk/co/majenko/audiobookrecorder/Book.java @@ -206,4 +206,14 @@ public class Book extends DefaultMutableTreeNode { return out; } + public void purgeBackups() { + for (Enumeration o = children(); o.hasMoreElements();) { + Object ob = (Object)o.nextElement(); + if (ob instanceof Chapter) { + Chapter c = (Chapter)ob; + c.purgeBackups(); + } + } + } + } diff --git a/src/uk/co/majenko/audiobookrecorder/Chapter.java b/src/uk/co/majenko/audiobookrecorder/Chapter.java index ef0f5f8..12cb98b 100644 --- a/src/uk/co/majenko/audiobookrecorder/Chapter.java +++ b/src/uk/co/majenko/audiobookrecorder/Chapter.java @@ -239,4 +239,14 @@ public class Chapter extends DefaultMutableTreeNode { } } + public void purgeBackups() { + for (Enumeration o = children(); o.hasMoreElements();) { + Object ob = (Object)o.nextElement(); + if (ob instanceof Sentence) { + Sentence s = (Sentence)ob; + s.purgeBackups(); + } + } + } + } diff --git a/src/uk/co/majenko/audiobookrecorder/Sentence.java b/src/uk/co/majenko/audiobookrecorder/Sentence.java index 8b8d33d..40da263 100644 --- a/src/uk/co/majenko/audiobookrecorder/Sentence.java +++ b/src/uk/co/majenko/audiobookrecorder/Sentence.java @@ -1328,4 +1328,17 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { return out; } + + public void purgeBackups() { + File whereto = getFile().getParentFile(); + String name = getFile().getName(); + + File[] files = whereto.listFiles(); + for (File f : files) { + String fn = f.getName(); + if (fn.startsWith("backup-") && fn.endsWith("-" + name)) { + f.delete(); + } + } + } }