Skip to content

Commit

Permalink
fix: disable single backup button during backup
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTurco committed Nov 7, 2024
1 parent 42b632d commit 14aaad9
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,11 @@ public void SingleBackup(String path1, String path2) {
try {
progressBar = new BackupProgressGUI(path1, path2);
progressBar.setVisible(true);
BackupOperations.zipDirectory(path1, path2+".zip", currentBackup, null, progressBar);

SingleBackup.setEnabled(false);
toggleAutoBackup.setEnabled(false);

BackupOperations.zipDirectory(path1, path2+".zip", currentBackup, null, progressBar, SingleBackup, toggleAutoBackup);

//if current_file_opened is null it means they are not in a backup but it is a backup with no associated json file
if (currentBackup.getBackupName() != null && !currentBackup.getBackupName().isEmpty()) {
Expand Down Expand Up @@ -1626,7 +1630,7 @@ private void RunBackupPopupItemActionPerformed(java.awt.event.ActionEvent evt) {

progressBar = new BackupProgressGUI(backup.getInitialPath(), backup.getDestinationPath());
progressBar.setVisible(true);
BackupOperations.SingleBackup(backup, null, progressBar);
BackupOperations.SingleBackup(backup, null, progressBar, SingleBackup, toggleAutoBackup);

// if the backup is currentBackup
if (currentBackup.getBackupName().equals(backup.getBackupName()))
Expand Down
51 changes: 40 additions & 11 deletions src/main/java/com/mycompany/autobackupprogram/BackupOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

public class BackupOperations{

private static final JSONAutoBackup JSON = new JSONAutoBackup();
private static Thread zipThread;

public static void SingleBackup(Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar) {
public static void SingleBackup(Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar, JButton singleBackupBtn, JToggleButton autoBackupBtn) {
if (backup == null) throw new IllegalArgumentException("Backup cannot be null!");

Logger.logMessage("Event --> automatic single backup started", Logger.LogLevel.INFO);

if (singleBackupBtn != null) singleBackupBtn.setEnabled(false);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(false);

System.out.println(singleBackupBtn.isEnabled());

String temp = "\\";
String path1 = backup.getInitialPath();
String path2 = backup.getDestinationPath();
Expand All @@ -51,22 +59,26 @@ public static void SingleBackup(Backup backup, TrayIcon trayIcon, BackupProgress
path2 = path2 + "\\" + name1 + " (Backup " + date + ")";

try {
zipDirectory(path1, path2+".zip", backup, trayIcon, progressBar);
zipDirectory(path1, path2+".zip", backup, trayIcon, progressBar, singleBackupBtn, autoBackupBtn);
} catch (IOException e) {
System.err.println("Exception (SingleBackup) --> " + e);
Logger.logMessage("Error during the backup operation: the initial path is incorrect!", Logger.LogLevel.WARN);
JOptionPane.showMessageDialog(null, "Error during the backup operation: the initial path is incorrect!", "Error", JOptionPane.ERROR_MESSAGE);
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
}
}

private static void updateAfterBackup(String path1, String path2, Backup backup, TrayIcon trayIcon) {
private static void updateAfterBackup(String path1, String path2, Backup backup, TrayIcon trayIcon, JButton singleBackupBtn, JToggleButton autoBackupBtn) {
if (backup == null) throw new IllegalArgumentException("Backup cannot be null!");
if (path1 == null) throw new IllegalArgumentException("Initial path cannot be null!");
if (path2 == null) throw new IllegalArgumentException("Destination path cannot be null!");

LocalDateTime dateNow = LocalDateTime.now();

Logger.logMessage("Backup completed!", Logger.LogLevel.INFO);

if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);

// next day backup update
if (backup.isAutoBackup() == true) {
Expand Down Expand Up @@ -128,10 +140,21 @@ public static boolean CheckInputCorrect(String backupName, String path1, String
return false;
}

if (path1.equals(path2)) {
Logger.logMessage("The initial path and destination path cannot be the same. Please choose different paths", Logger.LogLevel.WARN);

if (trayIcon != null) {
trayIcon.displayMessage("Backup Manager", "Backup: "+ backupName +"\nError during automatic backup.\nThe initial path and destination path cannot be the same. Please choose different paths!", TrayIcon.MessageType.ERROR);
} else {
JOptionPane.showMessageDialog(null, "The initial path and destination path cannot be the same. Please choose different paths!", "Error", JOptionPane.ERROR_MESSAGE);
}
return false;
}

return true;
}

public static void zipDirectory(String sourceDirectoryPath, String targetZipPath, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar) throws IOException { // Track copied files
public static void zipDirectory(String sourceDirectoryPath, String targetZipPath, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar, JButton singleBackupBtn, JToggleButton autoBackupBtn) throws IOException { // Track copied files
File file = new File(sourceDirectoryPath);
int totalFilesCount = file.isDirectory() ? countFilesInDirectory(file) : 1;

Expand All @@ -143,13 +166,15 @@ public static void zipDirectory(String sourceDirectoryPath, String targetZipPath

try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(targetZipPath))) {
if (file.isFile()) {
addFileToZip(sourceDirectoryPath, zipOut, file.toPath(), "", copiedFilesCount, totalFilesCount, backup, trayIcon, progressBar);
addFileToZip(sourceDirectoryPath, zipOut, file.toPath(), "", copiedFilesCount, totalFilesCount, backup, trayIcon, progressBar, singleBackupBtn, autoBackupBtn);
} else {
Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (Thread.currentThread().isInterrupted()) {
Logger.logMessage("Zipping process manually interrupted", Logger.LogLevel.INFO);
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
return FileVisitResult.TERMINATE; // Stop if interrupted
}

Expand All @@ -174,7 +199,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
// Update progress
int filesCopiedSoFar = copiedFilesCount.incrementAndGet();
int actualProgress = (int) (((double) filesCopiedSoFar / totalFilesCount) * 100);
UpdateProgressPercentage(actualProgress, sourceDirectoryPath, targetZipPath, backup, trayIcon, progressBar); // Update progress percentage
UpdateProgressPercentage(actualProgress, sourceDirectoryPath, targetZipPath, backup, trayIcon, progressBar, singleBackupBtn, autoBackupBtn); // Update progress percentage

return FileVisitResult.CONTINUE;
}
Expand All @@ -183,6 +208,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (Thread.currentThread().isInterrupted()) {
Logger.logMessage("Zipping process manually interrupted", Logger.LogLevel.INFO);
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
return FileVisitResult.TERMINATE; // Stop if interrupted
}

Expand All @@ -197,13 +224,15 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
} catch (IOException ex) {
Logger.logMessage("An error occurred", Logger.LogLevel.ERROR, ex);
ex.printStackTrace(); // Handle the exception as necessary
if (singleBackupBtn != null) singleBackupBtn.setEnabled(true);
if (autoBackupBtn != null) autoBackupBtn.setEnabled(true);
}
});

zipThread.start(); // Start the zipping thread
}

private static void addFileToZip(String sourceDirectoryPath, ZipOutputStream zipOut, Path file, String zipEntryName, AtomicInteger copiedFilesCount, int totalFilesCount, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar) throws IOException {
private static void addFileToZip(String sourceDirectoryPath, ZipOutputStream zipOut, Path file, String zipEntryName, AtomicInteger copiedFilesCount, int totalFilesCount, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar, JButton singleBackupBtn, JToggleButton autoBackupBtn) throws IOException {
if (zipEntryName.isEmpty()) {
zipEntryName = file.getFileName().toString();
}
Expand All @@ -222,7 +251,7 @@ private static void addFileToZip(String sourceDirectoryPath, ZipOutputStream zip

int filesCopiedSoFar = copiedFilesCount.incrementAndGet();
int actualProgress = (int) (((double) filesCopiedSoFar / totalFilesCount) * 100);
UpdateProgressPercentage(actualProgress, sourceDirectoryPath, zipOut.toString(), backup, trayIcon, progressBar);
UpdateProgressPercentage(actualProgress, sourceDirectoryPath, zipOut.toString(), backup, trayIcon, progressBar, singleBackupBtn, autoBackupBtn);
}

public static void updateBackupList(List<Backup> backups) {
Expand All @@ -243,7 +272,7 @@ public static void updateBackup(List<Backup> backups, Backup updatedBackup) {
updateTableWithNewBackupList(backups);
}

public static void UpdateProgressPercentage(int value, String path1, String path2, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar) {
public static void UpdateProgressPercentage(int value, String path1, String path2, Backup backup, TrayIcon trayIcon, BackupProgressGUI progressBar, JButton singleBackupBtn, JToggleButton autoBackupBtn) {

if (value == 0 || value == 25 || value == 50 || value == 75 || value == 100)
Logger.logMessage("Progress: " + value, Logger.LogLevel.INFO);
Expand All @@ -252,7 +281,7 @@ public static void UpdateProgressPercentage(int value, String path1, String path
progressBar.UpdateProgressBar(value);

if (value == 100) {
updateAfterBackup(path1, path2, backup, trayIcon);
updateAfterBackup(path1, path2, backup, trayIcon, singleBackupBtn, autoBackupBtn);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private List<Backup> getBackupsToDo(List<Backup> backups) {
private void executeBackups(List<Backup> backups) {
javax.swing.SwingUtilities.invokeLater(() -> {
for (Backup backup : backups) {
BackupOperations.SingleBackup(backup, trayIcon, null);
BackupOperations.SingleBackup(backup, trayIcon, null, null, null);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/res/backup_list.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"time_interval_backup":"0.0:1","destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":true,"backup_name":"test","notes":"","backup_count":41,"next_date_backup":"2024-11-06T21:15:10.867592100","start_path":"C:\\Users\\Utente\\Desktop\\AutoBackupProgram","creation_date":"2024-11-02T17:06:03.001492800","last_backup":"2024-11-06T21:14:10.867592100","last_update_date":"2024-11-04T23:06:18.579491400"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"test2","notes":"","backup_count":16,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-04T23:04:31.346029500","last_backup":null,"last_update_date":"2024-11-07T10:09:03.713405600"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"test3","notes":"","backup_count":37,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-04T23:04:33.154829","last_backup":"2024-11-06T15:23:06.306349900","last_update_date":"2024-11-06T15:18:15.215469900"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"test4","notes":"","backup_count":16,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\fg","creation_date":"2024-11-04T23:04:35.073470600","last_backup":null,"last_update_date":"2024-11-06T00:20:26.518897900"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"prova","notes":"","backup_count":9,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\Stalcraft1.mp4","creation_date":"2024-11-05T23:00:16.706667","last_backup":"2024-11-06T14:41:11.821238200","last_update_date":"2024-11-06T23:40:57.474653300"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"f","notes":"","backup_count":0,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-06T23:48:13.755548400","last_backup":null,"last_update_date":"2024-11-06T23:48:20.998591200"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"ff","notes":"","backup_count":0,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-07T09:49:49.571321","last_backup":null,"last_update_date":"2024-11-07T09:50:58.695252600"}]
[{"time_interval_backup":"0.0:1","destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":true,"backup_name":"test","notes":"","backup_count":45,"next_date_backup":"2024-11-07T21:09:44.302618500","start_path":"C:\\Users\\Utente\\Desktop\\AutoBackupProgram","creation_date":"2024-11-02T17:06:03.001492800","last_backup":"2024-11-07T21:08:52.047767900","last_update_date":"2024-11-04T23:06:18.579491400"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"test2","notes":"","backup_count":16,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-04T23:04:31.346029500","last_backup":null,"last_update_date":"2024-11-07T10:09:03.713405600"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"test3","notes":"","backup_count":37,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-04T23:04:33.154829","last_backup":"2024-11-06T15:23:06.306349900","last_update_date":"2024-11-06T15:18:15.215469900"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"test4","notes":"","backup_count":16,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\fg","creation_date":"2024-11-04T23:04:35.073470600","last_backup":null,"last_update_date":"2024-11-07T21:08:42.842286300"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"prova","notes":"","backup_count":9,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\Stalcraft1.mp4","creation_date":"2024-11-05T23:00:16.706667","last_backup":"2024-11-06T14:41:11.821238200","last_update_date":"2024-11-06T23:40:57.474653300"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"f","notes":"","backup_count":0,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-06T23:48:13.755548400","last_backup":null,"last_update_date":"2024-11-06T23:48:20.998591200"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"ff","notes":"","backup_count":0,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-07T09:49:49.571321","last_backup":null,"last_update_date":"2024-11-07T09:50:58.695252600"},{"time_interval_backup":null,"destination_path":"C:\\Users\\Utente\\Desktop","automatic_backup":false,"backup_name":"gg","notes":"","backup_count":3,"next_date_backup":null,"start_path":"C:\\Users\\Utente\\Desktop\\gg","creation_date":"2024-11-07T21:07:42.113672","last_backup":"2024-11-07T21:08:22.988730400","last_update_date":"2024-11-07T21:08:18.378356300"}]
Loading

0 comments on commit 14aaad9

Please sign in to comment.