| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.model.swing; |
| 5 | |
|
| 6 | |
import java.io.File; |
| 7 | |
import java.io.FileFilter; |
| 8 | |
import java.io.FileInputStream; |
| 9 | |
import java.io.IOException; |
| 10 | |
import java.util.Collections; |
| 11 | |
import java.util.Comparator; |
| 12 | |
import java.util.Date; |
| 13 | |
import java.util.HashMap; |
| 14 | |
import java.util.LinkedList; |
| 15 | |
import java.util.List; |
| 16 | |
import java.util.Map; |
| 17 | |
import java.util.logging.Level; |
| 18 | |
import java.util.logging.Logger; |
| 19 | |
|
| 20 | |
import javax.swing.AbstractListModel; |
| 21 | |
|
| 22 | |
import org.homeunix.thecave.buddi.Const; |
| 23 | |
import org.homeunix.thecave.buddi.model.Document; |
| 24 | |
import org.homeunix.thecave.buddi.util.BuddiCryptoFactory; |
| 25 | |
|
| 26 | |
import ca.digitalcave.moss.crypto.CipherException; |
| 27 | |
import ca.digitalcave.moss.crypto.IncorrectDocumentFormatException; |
| 28 | |
|
| 29 | 0 | public class BackupManagerListModel extends AbstractListModel { |
| 30 | |
public static final long serialVersionUID = 0; |
| 31 | |
|
| 32 | |
private final List<File> backupDocuments; |
| 33 | |
private final Map<File, Date> backupFileMap; |
| 34 | |
|
| 35 | 0 | public BackupManagerListModel(Document document) { |
| 36 | 0 | backupFileMap = getAssociatedBackups(document.getFile()); |
| 37 | 0 | backupDocuments = new LinkedList<File>(backupFileMap.keySet()); |
| 38 | 0 | Collections.sort(backupDocuments, new Comparator<File>(){ |
| 39 | |
public int compare(File o1, File o2) { |
| 40 | 0 | Date d1 = backupFileMap.get(o1); |
| 41 | 0 | Date d2 = backupFileMap.get(o2); |
| 42 | |
|
| 43 | 0 | if (d1 == null || d2 == null || d1.equals(d2)) |
| 44 | 0 | return o1.compareTo(o2); |
| 45 | 0 | return d1.compareTo(d2) * -1; |
| 46 | |
} |
| 47 | |
}); |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
public File getElementAt(int index) { |
| 51 | 0 | return backupDocuments.get(index); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public int getSize() { |
| 55 | 0 | return backupDocuments.size(); |
| 56 | |
} |
| 57 | |
|
| 58 | |
public Date getDate(File f){ |
| 59 | 0 | return backupFileMap.get(f); |
| 60 | |
} |
| 61 | |
|
| 62 | |
private Map<File, Date> getAssociatedBackups(final File dataFile){ |
| 63 | 0 | Map<File, Date> backups = new HashMap<File, Date>(); |
| 64 | |
|
| 65 | 0 | if (dataFile != null){ |
| 66 | 0 | File folder = dataFile.getParentFile(); |
| 67 | |
try { |
| 68 | 0 | BuddiCryptoFactory bcf = new BuddiCryptoFactory(); |
| 69 | 0 | for (File f : folder.listFiles(new FileFilter(){ |
| 70 | |
public boolean accept(File pathname) { |
| 71 | 0 | if (pathname.getName().matches( |
| 72 | |
dataFile.getName().replaceAll(Const.DATA_FILE_EXTENSION + "$", "") |
| 73 | |
+ "_\\d+\\" |
| 74 | |
+ Const.BACKUP_FILE_EXTENSION)) |
| 75 | 0 | return true; |
| 76 | 0 | return false; |
| 77 | |
} |
| 78 | |
})) { |
| 79 | |
try { |
| 80 | 0 | File newFile = new File(f.getAbsolutePath()); |
| 81 | 0 | backups.put(f, bcf.getTimestamp(new FileInputStream(newFile))); |
| 82 | |
} |
| 83 | 0 | catch (IOException ioe){ |
| 84 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "IOException for file " + f, ioe); |
| 85 | |
} |
| 86 | 0 | catch (IncorrectDocumentFormatException idfe){ |
| 87 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Incorrect document format for file " + f, idfe); |
| 88 | 0 | } |
| 89 | |
} |
| 90 | |
} |
| 91 | 0 | catch (CipherException ce){ |
| 92 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cipher Exception", ce); |
| 93 | 0 | } |
| 94 | |
} |
| 95 | |
|
| 96 | 0 | return backups; |
| 97 | |
} |
| 98 | |
} |