OfficerBreaker/src/officerbreaker/Controller.java

235 lines
7.0 KiB
Java
Raw Normal View History

2022-03-11 21:27:08 -03:00
package officerbreaker;
2022-03-01 20:24:28 -03:00
import java.awt.Desktop;
import java.io.File;
import javafx.scene.image.Image;
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
2022-03-11 21:27:08 -03:00
private ImageView titleImageView;
@FXML
private ImageView fileImageView;
2022-03-01 20:24:28 -03:00
private Image wordImage;
private Image excelImage;
private Image powerpointImage;
2022-03-31 10:59:18 -03:00
private Image progressOnGoing;
private Image progressFinished;
2022-04-21 19:45:05 -03:00
2022-03-01 20:24:28 -03:00
@FXML
2022-03-11 21:27:08 -03:00
private Text fileText;
2022-03-01 20:24:28 -03:00
@FXML
private Text invalidFileText;
2022-04-21 19:45:05 -03:00
2022-03-31 10:59:18 -03:00
@FXML
private ImageView progressBarBrowse;
@FXML
private ImageView progressBarRemove;
2022-03-01 20:24:28 -03:00
@FXML
public void initialize() {
anchorPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
2022-04-21 19:45:05 -03:00
invalidFileText.setVisible(false);
2022-03-31 10:59:18 -03:00
progressBarBrowse.setVisible(false);
progressBarRemove.setVisible(false);
2022-04-21 19:45:05 -03:00
2022-03-01 20:24:28 -03:00
try {
wordImage = new Image(getClass().getResourceAsStream("img/word.png"));
powerpointImage = new Image(getClass().getResourceAsStream("img/powerpoint.png"));
excelImage = new Image(getClass().getResourceAsStream("img/excel.png"));
2022-04-21 19:45:05 -03:00
2022-03-31 10:59:18 -03:00
progressOnGoing = new Image(getClass().getResourceAsStream("img/prog-bar-ongoing.gif"));
2022-04-21 19:45:05 -03:00
2022-03-31 10:59:18 -03:00
progressFinished = new Image(getClass().getResourceAsStream("img/prog-bar-complete.png"));
2022-03-01 20:24:28 -03:00
} catch (Exception e) {
e.printStackTrace();
}
}
@FXML
void browsePressed(ActionEvent event) {
invalidFileText.setVisible(false);
2022-04-21 19:45:05 -03:00
progressBarRemove.setVisible(false);
2022-03-31 10:59:18 -03:00
progressBarBrowse.setVisible(true);
progressBarBrowse.setImage(progressOnGoing);
2022-04-21 19:45:05 -03:00
filePathText.setVisible(false);
2022-03-11 21:27:08 -03:00
try {
fileChooser = new FileChooser();
fileChooser.setTitle("Please Select a File");
file = fileChooser.showOpenDialog(null);
filePath = file.toString();
fileName = file.getName();
2022-04-21 19:45:05 -03:00
filePathText.setVisible(true);
2022-03-11 21:27:08 -03:00
filePathText.setText(filePath);
fileType = getFileType();
2022-03-01 20:24:28 -03:00
2022-03-11 21:27:08 -03:00
} catch (Exception e) {
e.printStackTrace();
}
2022-03-01 20:24:28 -03:00
2022-03-11 21:27:08 -03:00
if (fileType != null && isFileSupported()) {
setScreenByFileType();
2022-03-01 20:24:28 -03:00
}
}
@FXML
void removePasswordPressed(ActionEvent event) {
2022-04-21 19:45:05 -03:00
2022-03-01 20:24:28 -03:00
if (file == null) {
return;
}
if (!isFileSupported()) {
invalidFileText.setVisible(true);
return;
}
2022-03-20 19:36:24 -03:00
2022-03-01 20:24:28 -03:00
try {
// show progress
2022-03-31 10:59:18 -03:00
progressBarRemove.setVisible(true);
progressBarRemove.setImage(progressOnGoing);
2022-04-21 19:45:05 -03:00
2022-03-01 20:24:28 -03:00
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
2022-04-21 19:45:05 -03:00
if (parser.isExistSecurityElement()) // if the file has a password protection as a read-only file
2022-03-01 20:24:28 -03:00
{
parser.removeElementOfSecurity(); // remove hash node from XML file
parser.writeToXMLFile(); // write to XML the change
manipulator.insertFile(); // write back to file
2022-03-11 21:27:08 -03:00
fileText.setText(fileFinishedMessage());
2022-04-21 19:45:05 -03:00
} else // if there is no security element in the xml file, there is no read-only protection on it
{
2022-04-21 19:45:05 -03:00
fileText.setText(notReadOnlyMessage());
}
2022-03-01 20:24:28 -03:00
2022-04-21 19:45:05 -03:00
manipulator.removeXMLFile(); // cleans the xml extracted from origin file
progressBarRemove.setImage(progressFinished);
2022-03-01 20:24:28 -03:00
} 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();
}
}
2022-03-20 19:36:24 -03:00
@FXML
void openNedlirURL(ActionEvent event) {
try {
Desktop.getDesktop().browse(new URL("https://github.com/nedlir").toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
2022-03-01 20:24:28 -03:00
private boolean isFileSupported() {
if (fileType.equals(EXCEL) || fileType.equals(WORD) || fileType.equals(POWERPOINT)) {
return true;
} else {
return false;
}
}
2022-03-11 21:27:08 -03:00
private void setScreenByFileType() {
titleImageView.setVisible(false);
2022-03-31 10:59:18 -03:00
progressBarBrowse.setVisible(false);
2022-03-20 19:36:24 -03:00
2022-03-01 20:24:28 -03:00
switch (fileType) {
case EXCEL:
2022-03-11 21:27:08 -03:00
fileImageView.setImage(excelImage);
fileText.setText(fileRecognizedMessage("Excel"));
2022-03-01 20:24:28 -03:00
break;
case WORD:
2022-03-11 21:27:08 -03:00
fileImageView.setImage(wordImage);
fileText.setText(fileRecognizedMessage("Word"));
2022-03-01 20:24:28 -03:00
break;
case POWERPOINT:
2022-03-11 21:27:08 -03:00
fileImageView.setImage(powerpointImage);
fileText.setText(fileRecognizedMessage("PowerPoint"));
2022-03-01 20:24:28 -03:00
break;
default:
break;
}
}
private String getFileType() {
2022-03-11 21:27:08 -03:00
int indexOfLastDot = fileName.lastIndexOf(".");
2022-03-01 20:24:28 -03:00
if (indexOfLastDot == -1) // dot not found, invalid file
{
return null;
}
2022-03-11 21:27:08 -03:00
return fileName.substring(indexOfLastDot + 1);
2022-03-01 20:24:28 -03:00
}
2022-03-20 19:36:24 -03:00
2022-03-11 21:27:08 -03:00
// Messages on screen
2022-03-20 19:36:24 -03:00
private String fileRecognizedMessage(String fileType) {
String messageOnScreen = "Officer Breaker recognized " + "\"" + fileName + "\"" + " as " + fileType + " file.\n\n";
2022-03-11 21:27:08 -03:00
messageOnScreen = messageOnScreen + "Press \"Remove Password\" to unprotect the file.";
return messageOnScreen;
2022-03-01 20:24:28 -03:00
}
2022-03-20 19:36:24 -03:00
private String fileFinishedMessage() {
2022-03-11 21:27:08 -03:00
String messageOnScreen = "Yay! Officer Breaker removed the protection from your file!";
return messageOnScreen;
}
2022-03-20 19:36:24 -03:00
2022-04-21 19:45:05 -03:00
private String notReadOnlyMessage() {
String messageOnScreen = "Officer Breaker couldn't find any \"read-only\" restriction on the file. \n \nTry another file?";
return messageOnScreen;
}
2022-03-01 20:24:28 -03:00
}