Skip to content

Commit

Permalink
Java FX initial structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nedlir committed Mar 1, 2022
1 parent 7cbdb3e commit 5914a4c
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ test.xml
Tester.java
Testing.java
tempCodeRunnerFile.java
makefile
makefile
*.class
185 changes: 185 additions & 0 deletions Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package officerunlocker;

import java.awt.Desktop;
import java.io.File;
import javafx.scene.image.Image;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;

public class Controller {

private final String EXCEL = "xlsx";
private final String POWERPOINT = "pptx";
private final String WORD = "docx";

@FXML
private AnchorPane anchorPane;

@FXML
private Text filePathText;
private File file;
private FileChooser fileChooser;
private String filePath;
private String fileName;
private String fileType;

@FXML
private ImageView image;
private InputStream stream;
private Image wordImage;
private Image excelImage;
private Image powerpointImage;
private Image openingImage;

// opening screen instructions
@FXML
private Text textInstructions;

// opening screen title
@FXML
private Text textTitle;

@FXML
private Text invalidFileText;

@FXML
public void initialize() {
anchorPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
invalidFileText.setVisible(false);

textInstructions.setText(instructions());

try {
openingImage = new Image(getClass().getResourceAsStream("img/opening.png"));


wordImage = new Image(getClass().getResourceAsStream("img/word.png"));

powerpointImage = new Image(getClass().getResourceAsStream("img/powerpoint.png"));

excelImage = new Image(getClass().getResourceAsStream("img/excel.png"));

image.setImage(openingImage);
image.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}
}

@FXML
void browsePressed(ActionEvent event) {

// remove text from screen
textTitle.setVisible(false);
textInstructions.setVisible(false);
invalidFileText.setVisible(false);

fileChooser = new FileChooser();
fileChooser.setTitle("Please Select a File");
file = fileChooser.showOpenDialog(null);
filePath = file.toString();
fileName = file.getName();
filePathText.setText(filePath);
fileType = getFileType();

if (fileType != null) {
setImageByFileType();

}
}

@FXML
void removePasswordPressed(ActionEvent event) {
if (file == null) {
return;
}
if (!isFileSupported()) {
invalidFileText.setVisible(true);
return;
}
try {
FileManipulator manipulator = new FileManipulator(filePath, "./"); // create new object to manipulate the file

manipulator.extractFile(); // extract XML from file

XMLParser parser = new XMLParser(manipulator); // create new object to parse the XML

if (parser.isExistSecurityElement()) // if the file has a password
{
parser.removeElementOfSecurity(); // remove hash node from XML file

parser.writeToXMLFile(); // write to XML the change

manipulator.insertFile(); // write back to file

manipulator.removeXMLFile();

System.out.println("Finished");
}

} catch (Exception e) {
e.printStackTrace();
}

}

@FXML
void openCioccolatodorimaURL(ActionEvent event) {
try {
Desktop.getDesktop().browse(new URL("https://twitter.com/cioccolato_kun").toURI());
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean isFileSupported() {
if (fileType.equals(EXCEL) || fileType.equals(WORD) || fileType.equals(POWERPOINT)) {
return true;
} else {
return false;
}
}

private void setImageByFileType() {
switch (fileType) {
case EXCEL:
image.setImage(excelImage);
break;
case WORD:
image.setImage(wordImage);
break;
case POWERPOINT:
image.setImage(powerpointImage);
break;
default:
break;
}
}

private String getFileType() {
int indexOfLastDot = this.fileName.lastIndexOf(".");
if (indexOfLastDot == -1) // dot not found, invalid file
{
return null;
}
return this.fileName.substring(indexOfLastDot + 1);
}

private String instructions() {
return "Welcome to Office Breaker!\n" + "This tool is deisgned to help remove read-only protection from .pptx, .xlsx. .docx filetypes.";
}

}
14 changes: 7 additions & 7 deletions FileManipulator.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package officerunlocker;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FileManipulator {

public final static String EXCEL_FILE = "xlsx";
public final static String POWER_POINT_FILE = "pptx";
public final static String WORD_FILE = "pptx";
public final static String WORD_FILE = "docx";

public final static String EXCEL_XML = "workbook.xml";
public final static String POWER_POINT_XML = "presentation.xml";
Expand Down Expand Up @@ -103,6 +98,11 @@ public void locateTargetFileAndPath() throws Exception {
}
}

public void removeXMLFile() throws Exception {
File file = new File(this.XMLTargetFile);
Files.deleteIfExists(file.toPath());
}

public String getXMLTargetFile() {
return this.XMLTargetFile;
}
Expand Down
32 changes: 32 additions & 0 deletions OfficerBreaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package officerunlocker;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
*
* @author nedlir
*/
public class OfficerBreaker extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("View.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);
stage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}
34 changes: 34 additions & 0 deletions View.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane fx:id="anchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="501.0" prefWidth="675.0" xmlns="https://javafx.com/javafx/17" xmlns:fx="https://javafx.com/fxml/1" fx:controller="officerunlocker.Controller">
<children>
<Button layoutX="90.0" layoutY="401.0" mnemonicParsing="false" onAction="#browsePressed" text="Browse" />
<Button layoutX="89.0" layoutY="445.0" mnemonicParsing="false" onAction="#removePasswordPressed" text="Remove Password" />
<Text fx:id="filePathText" layoutX="158.0" layoutY="419.0" strokeType="OUTSIDE" strokeWidth="0.0">
<font>
<Font size="15.0" />
</font>
</Text>
<Text layoutX="260.0" layoutY="493.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Art by " />
<Hyperlink layoutX="293.0" layoutY="477.0" onAction="#openCioccolatodorimaURL" text="Cioccolatodorima" />
<ImageView fx:id="image" fitHeight="335.0" fitWidth="267.0" layoutX="394.0" layoutY="66.0" pickOnBounds="true" preserveRatio="true" />
<Text fx:id="invalidFileText" fill="RED" layoutX="220.0" layoutY="463.0" strokeType="OUTSIDE" strokeWidth="0.0" text="File not supported. Please use &quot;.pptx&quot;, &quot;.docx&quot; or &quot;.xlsx&quot;">
<font>
<Font size="14.0" />
</font>
</Text>
<Text fx:id="textTitle" fill="#000000bf" layoutX="77.0" layoutY="83.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Office Breaker">
<font>
<Font size="43.0" />
</font>
</Text>
<Text fx:id="textInstructions" fill="#000000c0" layoutX="57.0" layoutY="118.0" strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="326.13671875" />
</children>
</AnchorPane>
1 change: 1 addition & 0 deletions XMLParser.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package officerunlocker;

import java.io.File;
import java.io.FileInputStream;
Expand Down
Binary file added img/excel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/opening.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/powerpoint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/word.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5914a4c

Please sign in to comment.