Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RUPS open flags #86

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/itextpdf/rups/Rups.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void run() {
initApplication(frame, controller, onCloseOperation);
rups.setController(controller);
if (null != f && f.canRead()) {
rups.loadDocumentFromFile(f, false);
rups.loadDocumentFromFile(f, true);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This file is part of the iText (R) project.
import com.itextpdf.rups.event.*;
import com.itextpdf.rups.io.listeners.PdfTreeNavigationListener;
import com.itextpdf.rups.model.ObjectLoader;
import com.itextpdf.rups.model.PdfFile;
import com.itextpdf.rups.model.PdfSyntaxParser;
import com.itextpdf.rups.model.TreeNodeFactory;
import com.itextpdf.rups.view.DebugView;
Expand Down Expand Up @@ -273,8 +274,16 @@ public void update(Observable observable, Object obj) {
ObjectLoader loader = (ObjectLoader) event.getContent();
nodes = loader.getNodes();
PdfTrailerTreeNode root = pdfTree.getRoot();
root.setTrailer(loader.getFile().getPdfDocument().getTrailer());
root.setUserObject("PDF Object Tree (" + loader.getLoaderName() + ")");
PdfFile pdfFile = loader.getFile();
boolean readOnly = pdfFile.isReadOnly();
root.setTrailer(pdfFile.getPdfDocument().getTrailer());
String loaderName = loader.getLoaderName();
if (readOnly) {
loaderName += "; read-only";
}
root.setUserObject("PDF Object Tree (" + loaderName + ")");
// put the object panel into plugin mode to disable the editing UI as necessary
objectPanel.setPluginMode(readOnly);
nodes.expandNode(root);
navigationTabs.setSelectedIndex(0);
setChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public synchronized void drop(DropTargetDropEvent dtde) {
if (files == null || files.size() != 1) {
JOptionPane.showMessageDialog(masterComponent, "You can only open one file!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
loadFile(files.get(0), false);
// open drag & dropped files in read-only mode
loadFile(files.get(0), true);
}
} catch (HeadlessException | UnsupportedFlavorException | IOException e) {
JOptionPane.showMessageDialog(masterComponent, "Error opening file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
Expand Down Expand Up @@ -245,7 +246,7 @@ public void update(Observable o, Object arg) {
closeRoutine();
break;
case RupsEvent.OPEN_FILE_EVENT:
loadFile((File) event.getContent(), false);
loadFile((File) event.getContent(), ((OpenFileEvent) event).getOpenReadOnly());
break;
case RupsEvent.SAVE_TO_FILE_EVENT:
saveFile((File) event.getContent());
Expand Down Expand Up @@ -277,7 +278,7 @@ public void update(Observable o, Object arg) {
* @param file the file to load
* @param readOnly open the file read only or not
*/
public void loadFile(File file, boolean readOnly) {
public final void loadFile(File file, boolean readOnly) {
try {
byte[] contents = readFileToByteArray(file);
loadRawContent(contents, file.getName(), file.getParentFile(), readOnly);
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/itextpdf/rups/event/OpenFileEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ This file is part of the iText (R) project.

public class OpenFileEvent extends RupsEvent {

private File file;
private final boolean readOnly;
private final File file;

public OpenFileEvent(File file) {
public OpenFileEvent(File file, boolean readOnly) {
this.file = file;
this.readOnly = readOnly;
}

@Override
Expand All @@ -59,7 +61,12 @@ public int getType() {
}

@Override
public Object getContent() {
public File getContent() {
return file;
}

public boolean getOpenReadOnly() {
return readOnly;
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/itextpdf/rups/io/FileOpenAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ public FileOpenAction(Observer observer, FileFilter filter, Component parent) {

@Override
protected int showDialog() {
fileChooser.setAccessory(new PdfOpenFlagsPanel());
return fileChooser.showOpenDialog(parent);
}

@Override
protected RupsEvent getEvent() {
return new OpenFileEvent(getFile());
PdfOpenFlagsPanel pofp = (PdfOpenFlagsPanel) fileChooser.getAccessory();
return new OpenFileEvent(getFile(), pofp.getOpenReadOnly());
}
}
100 changes: 100 additions & 0 deletions src/main/java/com/itextpdf/rups/io/PdfOpenFlagsPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2021 iText Group NV
Authors: iText Software.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation with the addition of the
following permission added to Section 15 as permitted in Section 7(a):
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
OF THIRD PARTY RIGHTS

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, see http://www.gnu.org/licenses or write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA, 02110-1301 USA, or download the license from the following URL:
http://itextpdf.com/terms-of-use/

The interactive user interfaces in modified source and object code versions
of this program must display Appropriate Legal Notices, as required under
Section 5 of the GNU Affero General Public License.

In accordance with Section 7(b) of the GNU Affero General Public License,
a covered work must retain the producer line in every PDF that is created
or manipulated using iText.

You can be released from the requirements of the license by purchasing
a commercial license. Buying such a license is mandatory as soon as you
develop commercial activities involving the iText software without
disclosing the source code of your own applications.
These activities include: offering paid services to customers as an ASP,
serving PDFs on the fly in a web application, shipping iText with a closed
source product.

For more information, please contact iText Software Corp. at this
address: [email protected]
*/
package com.itextpdf.rups.io;

import javax.swing.*;

public final class PdfOpenFlagsPanel extends JPanel {

/**
* Panel title
*/
public static final String PANEL_LABEL = "Advanced options";

/**
* Label for read-only checkbox
*/
public static final String OPEN_READ_ONLY = "Open read-only";

/**
* Tooltip for read-only checkbox.
*/
public static final String OPEN_READ_ONLY_TOOLTIP = "If checked, the document will be opened read-only." +
"This disables modification and re-saving. If unchecked, the document will be opened in update mode. ";

/**
* Open PDF documents in read-only mode.
*/
private final JCheckBox openReadOnly;

/**
* Construct a flags panel with default initial states of 'true' for both checkboxes.
*/
public PdfOpenFlagsPanel() {
this(true);
}

/**
* Construct a flags panel for use as an accessory pane in a file chooser component.
*
* @param initReadOnly
* Initial flag state.
*/
public PdfOpenFlagsPanel(boolean initReadOnly) {
super();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JLabel(PANEL_LABEL));
this.openReadOnly = new JCheckBox(OPEN_READ_ONLY, initReadOnly);
this.openReadOnly.setToolTipText(OPEN_READ_ONLY_TOOLTIP);
this.add(this.openReadOnly);
}

/**
* @return the state of the "Read-only" checkbox.
*/
public boolean getOpenReadOnly() {
return this.openReadOnly.isSelected();
}


}
65 changes: 30 additions & 35 deletions src/main/java/com/itextpdf/rups/model/PdfFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ This file is part of the iText (R) project.

import com.ibm.icu.text.StringPrepParseException;
import com.ibm.icu.text.StringPrep;
import com.itextpdf.kernel.pdf.StampingProperties;

import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -61,6 +63,7 @@ This file is part of the iText (R) project.
*/
public class PdfFile {

public static final String IGNORE_PERMS_ENV_VAR = "RUPS_IGNORE_PERMS";
/**
* The directory where the file can be found (if the PDF was passed as a file).
*/
Expand All @@ -76,11 +79,6 @@ public class PdfFile {
*/
protected PdfDocument document = null;

/**
* The file permissions
*/
protected Permissions permissions = null;

/**
* Raw content
*/
Expand Down Expand Up @@ -121,6 +119,7 @@ public PdfFile(File file) throws IOException, PdfException {
*/
public PdfFile(byte[] file, boolean readOnly) throws IOException, PdfException {
rawContent = file;
this.readOnly = readOnly;

try {
readFile(new ByteArrayInputStream(file), false, readOnly);
Expand Down Expand Up @@ -164,6 +163,17 @@ public void selectInitialValue() {
return preparePasswordForOpen(passwordString);
}

private static boolean checkIgnorePermissions() {
// set unethical reading based on environment variable
final String ignorePermsEnv;
try {
ignorePermsEnv = System.getenv(IGNORE_PERMS_ENV_VAR);
} catch (SecurityException ex) {
return false;
}
return ignorePermsEnv != null && "1".equals(ignorePermsEnv.trim());
}

/**
* Does the actual reading of the file into PdfReader and PDFFile.
*
Expand All @@ -173,46 +183,27 @@ public void selectInitialValue() {
* @throws IOException an I/O exception
* @throws PdfException a PDF exception
*/
protected void readFile(InputStream fis, boolean checkPass, boolean readOnly) throws IOException, PdfException {
protected final void readFile(InputStream fis, boolean checkPass, boolean readOnly)
throws IOException, PdfException {
// reading the file into PdfReader
PdfReader reader;
PdfWriter writer;
permissions = new Permissions();
ReaderProperties readerProps = new ReaderProperties();
final byte[] password;
final ReaderProperties readerProps = new ReaderProperties();
if (checkPass) {
password = requestPassword();
readerProps.setPassword(password);
} else {
password = null;
readerProps.setPassword(requestPassword());
}
reader = new PdfReader(fis, readerProps);
final PdfReader reader = new PdfReader(fis, readerProps)
.setUnethicalReading(checkIgnorePermissions());
baos = new ByteArrayOutputStream();
if (readOnly) {
document = new PdfDocument(reader);
} else {
writer = new PdfWriter(baos);
document = new PdfDocument(reader, writer);
document = new PdfDocument(reader, new PdfWriter(baos), new StampingProperties().preserveEncryption());
}
// we have some extra work to do if the document was encrypted
if(reader.isEncrypted()) {
permissions.setEncrypted(true);
permissions.setCryptoMode(reader.getCryptoMode());
permissions.setPermissions((int) reader.getPermissions());
if(password != null) {
if (reader.isOpenedWithFullPermission()) {
permissions.setOwnerPassword(password);
permissions.setUserPassword(reader.computeUserPassword());
} else {
JOptionPane.showMessageDialog(
null,
"You opened the document using the user password instead of the owner password.");
}
}
} else {
permissions.setEncrypted(false);
if(reader.isEncrypted() && !reader.isOpenedWithFullPermission()) {
JOptionPane.showMessageDialog(
null,
"You opened the document using the user password instead of the owner password.");
}

}

/**
Expand Down Expand Up @@ -254,6 +245,10 @@ public void setFilename(String filename) {
this.filename = filename;
}

public boolean isReadOnly() {
return readOnly;
}

public ByteArrayOutputStream getByteArrayOutputStream() {
return baos;
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/itextpdf/rups/view/RupsMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ This file is part of the iText (R) project.
package com.itextpdf.rups.view;

import com.itextpdf.rups.controller.RupsController;
import com.itextpdf.rups.event.PostOpenDocumentEvent;
import com.itextpdf.rups.event.RupsEvent;
import com.itextpdf.rups.io.FileCloseAction;
import com.itextpdf.rups.io.FileCompareAction;
import com.itextpdf.rups.io.FileOpenAction;
import com.itextpdf.rups.io.FileSaveAction;
import com.itextpdf.rups.io.filters.PdfFilter;
import com.itextpdf.rups.model.ObjectLoader;
import com.itextpdf.rups.model.PdfFile;

import javax.swing.*;
Expand Down Expand Up @@ -178,7 +180,12 @@ public void update(Observable observable, Object obj) {
enableItems(false);
break;
case RupsEvent.OPEN_DOCUMENT_POST_EVENT:
enableItems(true);
ObjectLoader ol = (ObjectLoader) event.getContent();
if(ol.getFile().isReadOnly()) {
enableReadOnlyItems();
} else {
enableItems(true);
}
break;
case RupsEvent.ROOT_NODE_CLICKED_EVENT:
fileOpenAction.actionPerformed(null);
Expand Down Expand Up @@ -221,6 +228,14 @@ protected void enableItems(boolean enabled) {
enableItem(NEW_INDIRECT, enabled);
}

protected void enableReadOnlyItems() {
enableItem(CLOSE, true);
enableItem(SAVE_AS, false);
enableItem(OPENINVIEWER, true);
enableItem(COMPARE_WITH, true);
enableItem(NEW_INDIRECT, false);
}

/**
* Enables/disables a specific menu item
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ public void tableChanged(TableModelEvent e) {
}
}
}

public void setPluginMode(boolean pluginMode) {
this.pluginMode = pluginMode;
}
}