2008-07-30 18:37:01 -04:00
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot.ui.sfv;
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2008-10-06 15:13:58 -04:00
|
|
|
import java.io.PrintWriter;
|
2009-03-14 06:20:59 -04:00
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
import net.filebot.hash.HashType;
|
|
|
|
import net.filebot.hash.VerificationFileWriter;
|
|
|
|
import net.filebot.ui.transfer.TextFileExportHandler;
|
|
|
|
import net.filebot.util.FileUtilities;
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
|
2009-02-11 13:42:29 -05:00
|
|
|
class ChecksumTableExportHandler extends TextFileExportHandler {
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
private final ChecksumTableModel model;
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
public ChecksumTableExportHandler(ChecksumTableModel model) {
|
|
|
|
this.model = model;
|
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExport() {
|
2009-02-15 07:20:43 -05:00
|
|
|
return model.getRowCount() > 0 && defaultColumn() != null;
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
@Override
|
2009-05-16 16:16:13 -04:00
|
|
|
public void export(PrintWriter out) {
|
2011-09-13 14:16:38 -04:00
|
|
|
export(new VerificationFileWriter(out, model.getHashType().getFormat(), "UTF-8"), defaultColumn(), model.getHashType());
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDefaultFileName() {
|
2009-02-15 07:20:43 -05:00
|
|
|
return getDefaultFileName(defaultColumn());
|
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-02-15 07:20:43 -05:00
|
|
|
|
|
|
|
protected File defaultColumn() {
|
|
|
|
// select first column that is not a verification file column
|
2009-08-10 12:20:18 -04:00
|
|
|
for (File root : model.getChecksumColumns()) {
|
2009-02-15 07:20:43 -05:00
|
|
|
if (root.isDirectory())
|
|
|
|
return root;
|
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-02-15 07:20:43 -05:00
|
|
|
return null;
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
public void export(File file, File column) throws IOException {
|
2011-09-13 14:16:38 -04:00
|
|
|
VerificationFileWriter writer = new VerificationFileWriter(file, model.getHashType().getFormat(), "UTF-8");
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
try {
|
2011-09-13 14:16:38 -04:00
|
|
|
export(writer, column, model.getHashType());
|
2008-07-30 18:37:01 -04:00
|
|
|
} finally {
|
2011-09-13 14:16:38 -04:00
|
|
|
writer.close();
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
2011-09-13 14:16:38 -04:00
|
|
|
public void export(VerificationFileWriter out, File column, HashType type) {
|
2009-02-15 07:20:43 -05:00
|
|
|
for (ChecksumRow row : model.rows()) {
|
|
|
|
ChecksumCell cell = row.getChecksum(column);
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-02-15 07:20:43 -05:00
|
|
|
if (cell != null) {
|
2011-09-13 14:16:38 -04:00
|
|
|
String hash = cell.getChecksum(type);
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-02-15 07:20:43 -05:00
|
|
|
if (hash != null) {
|
2011-09-13 14:16:38 -04:00
|
|
|
out.write(cell.getName(), hash);
|
2009-02-15 07:20:43 -05:00
|
|
|
}
|
|
|
|
}
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
|
|
|
public String getDefaultFileName(File column) {
|
2009-02-15 07:20:43 -05:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
// append file name
|
|
|
|
sb.append(column != null ? FileUtilities.getName(column) : "name");
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
// append file extension
|
|
|
|
sb.append('.').append(model.getHashType().name().toLowerCase());
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
return sb.toString();
|
2008-07-30 18:37:01 -04:00
|
|
|
}
|
|
|
|
}
|