You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
5.5 KiB
199 lines
5.5 KiB
package de.srsoftware.belegscanner.gui; |
|
|
|
import de.srsoftware.belegscanner.Configuration; |
|
import de.srsoftware.belegscanner.Worker; |
|
import org.icepdf.ri.common.SwingController; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
import javax.swing.*; |
|
import java.awt.*; |
|
import java.io.ByteArrayOutputStream; |
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.nio.charset.StandardCharsets; |
|
import java.nio.file.Files; |
|
import java.util.Arrays; |
|
import java.util.Date; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.regex.Pattern; |
|
import java.util.stream.Collectors; |
|
|
|
import static de.srsoftware.belegscanner.Application.t; |
|
import static de.srsoftware.belegscanner.Constants.FILE_BROWSER; |
|
|
|
/** |
|
* The "guts" of the application: |
|
* Combines all panels and provides execution logic. |
|
* @author Stephan Richter <s.richter@srsoftware.de> |
|
* |
|
*/ |
|
public class MainFrame extends JFrame { |
|
private static final Logger LOG = LoggerFactory.getLogger(MainFrame.class); |
|
|
|
private static final long serialVersionUID = 210283601541223645L; |
|
|
|
private static final Pattern MARKEN = Pattern.compile("\\$([a-zäöüA-ZÄÖÜ]+)"); |
|
|
|
private static final String APP_WIDTH = "app.main.dimension.w"; |
|
|
|
private static final String APP_HEIGHT = "app.main.dimension.h"; |
|
|
|
private static final String APP_X = "app.main.position.x"; |
|
|
|
private static final String APP_Y = "app.main.position.y"; |
|
|
|
private static final String BAD_POLICY = "<policy domain=\"coder\" rights=\"none\" pattern=\"PDF\" />"; |
|
|
|
private static final String POLICY_FILE = "/etc/ImageMagick-6/policy.xml"; |
|
private final Worker worker; |
|
|
|
private StatusBar statusBar; |
|
private Toolbar toolbar; |
|
|
|
private HashMap<String,String> fields = new HashMap<>(); |
|
|
|
private String path = ""; |
|
|
|
private String category =""; |
|
|
|
private Date date = new Date(); |
|
|
|
private String patchedPath = ""; |
|
private String project = ""; |
|
|
|
private String mode = "Color"; |
|
|
|
private DocTable docTable; |
|
|
|
private Dimension dimension = new Dimension(209, 297); |
|
|
|
|
|
private JPanel pdfPreview; |
|
|
|
/** |
|
* Create application main frame. |
|
* @param config |
|
*/ |
|
public MainFrame(Configuration config) { |
|
super("BelegScanner"); |
|
setLayout(new BorderLayout()); |
|
worker = new Worker(config); |
|
|
|
add(toolbar = new Toolbar(worker),BorderLayout.EAST); |
|
add(statusBar = new StatusBar(worker),BorderLayout.SOUTH); |
|
add(docTable = new DocTable(worker),BorderLayout.NORTH); |
|
add(pdfPreview = new Preview(worker),BorderLayout.CENTER); |
|
|
|
int x = config.getOrCreate(APP_X, 20); |
|
int y = config.getOrCreate(APP_Y, 20); |
|
setLocation(new Point(x, y)); |
|
|
|
int width = config.getOrCreate(APP_WIDTH,800); |
|
int height = config.getOrCreate(APP_HEIGHT,600); |
|
setPreferredSize(new Dimension(width,height)); |
|
|
|
pack(); |
|
setVisible(true); |
|
} |
|
|
|
|
|
|
|
|
|
public String appMissing(String appTest){ |
|
List<String> cmd = Arrays.asList(appTest.split(" ")); |
|
|
|
try { |
|
Process process = new ProcessBuilder(cmd).start(); |
|
int errorCode = process.waitFor(); |
|
if (errorCode == 0) return null; |
|
} catch (InterruptedException | IOException e) { |
|
LOG.error(t("{} terminated: "),appTest,e); |
|
} |
|
return cmd.get(0); |
|
} |
|
|
|
public void checkDependencies() { |
|
List<String> missing = List.of("convert --version", "killall --version", "pdfsandwich -version", "pdftk --version", "scanimage --version",FILE_BROWSER+" --version") |
|
.stream().map(this::appMissing).filter(app -> app != null).collect(Collectors.toList()); |
|
if (!missing.isEmpty()) notifyAppsMissing(missing); |
|
checkImageMagickPolicy(); |
|
checkTesseractLang(); |
|
} |
|
|
|
private void checkTesseractLang() { |
|
List<String> cmd = List.of("tesseract","--list-langs"); |
|
|
|
try { |
|
Process process = new ProcessBuilder(cmd).start(); |
|
process.waitFor(); |
|
var bytes = new ByteArrayOutputStream(); |
|
process.getInputStream().transferTo(bytes); |
|
var out = bytes.toString(StandardCharsets.UTF_8); |
|
if (!out.contains("deu")) { |
|
StringBuilder message = new StringBuilder(); |
|
message.append("<html>") |
|
.append(t("German language support missing!")) |
|
.append("<br/>") |
|
.append(t("Please install the package")) |
|
.append("<br/><br/>tesseract-ocr-deu"); |
|
|
|
JOptionPane.showMessageDialog(this, |
|
message, |
|
t("Language support"), |
|
JOptionPane.WARNING_MESSAGE); |
|
System.exit(-1); |
|
|
|
} |
|
} catch (IOException | InterruptedException e) { |
|
} |
|
} |
|
|
|
|
|
|
|
private void checkImageMagickPolicy() { |
|
var file = new File(POLICY_FILE); |
|
if (file.exists()) |
|
try { |
|
var policy = Files.readString(file.toPath()); |
|
if (policy.contains(BAD_POLICY)) { |
|
StringBuilder message = new StringBuilder(); |
|
message.append("<html>") |
|
.append(t("Your ImageMagick configuration prohibits conversion of jpg to pdf!")) |
|
.append("<br/>") |
|
.append(t("Please remove the line<br/><br/>policy<br/><br/>from 'file'.").replace("policy",BAD_POLICY.replace("<", "<")).replace("file",POLICY_FILE)); |
|
|
|
JOptionPane.showMessageDialog(this, |
|
message, |
|
t("Configuration problem"), |
|
JOptionPane.WARNING_MESSAGE); |
|
System.exit(-1); |
|
|
|
} |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void notifyAppsMissing(List<String> missingApps) { |
|
StringBuilder message = new StringBuilder(); |
|
message.append("<html>"); |
|
message.append(t("This application requires the following helper commands to be installed:")); |
|
message.append("<ul>"); |
|
missingApps.forEach(app -> message.append("<li>").append(app).append("</li>")); |
|
message.append("</ul>"); |
|
|
|
JOptionPane.showMessageDialog(this, |
|
message, |
|
t("Apps missing"), |
|
JOptionPane.WARNING_MESSAGE); |
|
System.exit(-1); |
|
} |
|
}
|
|
|