From 362ef2282c3efa0a2a8b807218f61c77697c6367 Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Wed, 10 Aug 2022 10:47:13 +0200 Subject: offer factory reset if startup fails, or config screen fails to load, cf. #114 --- .../gui/workflow/config/ConfigurationManager.java | 40 ++++++++++++++++++++++ .../gui/workflow/states/ConfigurationUIState.java | 31 ++++++++++++++--- .../workflow/states/PrepareConfigurationState.java | 16 +++++---- .../at/asit/pdfover/gui/messages.properties | 3 +- .../at/asit/pdfover/gui/messages_de.properties | 3 +- 5 files changed, 81 insertions(+), 12 deletions(-) (limited to 'pdf-over-gui/src/main') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java index 8d2800e7..eda1e59f 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java @@ -24,6 +24,8 @@ import java.util.Locale; import java.util.Properties; import at.asit.pdfover.commons.Profile; + +import org.apache.commons.io.FileUtils; import org.eclipse.swt.graphics.Point; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,6 +77,44 @@ public class ConfigurationManager { this.configurationOverlay = new ConfigurationDataInMemory(); } + static public void factoryResetPersistentConfig() { + // tell logback to close all file handles + ((ch.qos.logback.classic.LoggerContext)LoggerFactory.getILoggerFactory()).stop(); + + File configDirectory = new File(Constants.CONFIG_DIRECTORY); + File backupDirectory = new File(Constants.CONFIG_BACKUP_DIRECTORY); + + // delete existing backup, if any + FileUtils.deleteQuietly(backupDirectory); + + // attempt 1: try to move the old config directory to a backup location + try { + FileUtils.moveDirectory( + configDirectory, + backupDirectory + ); + } catch (Exception e) { + System.out.println("Failed move config directory to backup location:"); + e.printStackTrace(); + + // attempt 2: try to simply force delete the config directory + try { + FileUtils.forceDelete(configDirectory); + } catch (Exception e2) { + System.out.println("Failed to delete config directory:"); + e2.printStackTrace(); + + // attempt 3: try to schedule the config directory for force deletion on JVM exit + try { + FileUtils.forceDeleteOnExit(configDirectory); + } catch (Exception e3) { + System.out.println("Failed to schedule config directory for deletion:"); + e3.printStackTrace(); + } + } + } + } + /* load from disk */ public void loadFromDisk() throws IOException { if (loaded) diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/ConfigurationUIState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/ConfigurationUIState.java index 74b38410..2aad12c9 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/ConfigurationUIState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/ConfigurationUIState.java @@ -20,11 +20,15 @@ import org.eclipse.swt.SWT; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.asit.pdfover.commons.Messages; import at.asit.pdfover.gui.MainWindowBehavior; import at.asit.pdfover.gui.MainWindow.Buttons; import at.asit.pdfover.gui.composites.ConfigurationComposite; +import at.asit.pdfover.gui.controls.ErrorDialog; +import at.asit.pdfover.gui.controls.Dialog.BUTTONS; import at.asit.pdfover.gui.workflow.StateMachine; import at.asit.pdfover.gui.workflow.Status; +import at.asit.pdfover.gui.workflow.config.ConfigurationManager; /** * @@ -64,10 +68,29 @@ public class ConfigurationUIState extends State { @Override public void run() { Status status = getStateMachine().status; - - ConfigurationComposite config = this.getConfigurationComposite(); - - getStateMachine().display(config); + + ConfigurationComposite config; + try { + config = this.getConfigurationComposite(); + getStateMachine().display(config); + } catch (Exception e) { + log.error("Failed to initialize config UI", e); + ErrorDialog error = new ErrorDialog( + getStateMachine().getMainShell(), + Messages.getString("error.ConfigInitialization"), + BUTTONS.YES_NO + ); + + int selection = error.open(); + if (selection == SWT.YES) + { + ConfigurationManager.factoryResetPersistentConfig(); + getStateMachine().exit(); + return; + } + + throw e; + } if(config.isUserDone()) { diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java index 1a3fd8b5..b6ea9c0b 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java @@ -348,15 +348,19 @@ public class PrepareConfigurationState extends State { setNextState(new OpenState(stateMachine)); - } catch (InitializationException e) { + } catch (Exception e) { log.error("Failed to initialize: ", e); - ErrorDialog error = new ErrorDialog(getStateMachine() - .getMainShell(), - Messages.getString("error.Initialization"), - BUTTONS.OK); + ErrorDialog error = new ErrorDialog( + getStateMachine().getMainShell(), + Messages.getString("error.Initialization"), + BUTTONS.YES_NO + ); // error.setException(e); // setNextState(error); - error.open(); + int selection = error.open(); + if (selection == SWT.YES) + ConfigurationManager.factoryResetPersistentConfig(); + getStateMachine().exit(); } } diff --git a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties index da1cb68e..d8f0657e 100644 --- a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties +++ b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties @@ -142,7 +142,8 @@ error.FailedToOpenDocument=Failed to open signed document\: %s. error.FailedToSaveSettings=Failed to save configuration file\! error.FileNotExist=File %s does not exist\! error.IOError=Input/Output Error -error.Initialization=Initialization failed. Please check your configuration. +error.Initialization=PDF-Over failed to initialize.\nWe're very sorry for the inconvenience.\n\nIt may be possible to restore functionality by resetting PDF-Over's configuration to factory defaults.\nWould you like to try this now? +error.ConfigInitialization=The options screen failed to initialize.\nWe're very sorry for the inconvenience.\n\nIt may be possible to restore functionality by resetting PDF-Over's configuration to factory defaults.\nWould you like to try this now? error.InvalidBKU=Invalid CCE selection. Please check. error.InvalidLocale=Locale not valid error.InvalidPhoneNumber=Given phone number is invalid\! Example\:+43664123456789 diff --git a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties index b476be9d..ba82ea3e 100644 --- a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties +++ b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties @@ -130,7 +130,8 @@ error.FailedToOpenDocument=Konnte signiertes Dokument nicht öffnen\: %s. error.FailedToSaveSettings=Konnte Konfigurationsdatei nicht speichern\! error.FileNotExist=Datei %s existiert nicht\! error.IOError=Ein-/Ausgabe-Fehler -error.Initialization=Initialisierung fehlgeschlagen. Bitte überprüfen Sie Ihre Konfiguration. +error.Initialization=Initialisierung fehlgeschlagen.\nWir bittem un Verzeihung für die Unannehmlichkeiten.\n\nEventuell lässt sich Ihr Problem beheben, indem Sie PDF-Over auf Werkseinstellungen zurücksetzen.\nMöchten Sie dies nun versuchen? +error.Initialization=Der Einstellungsbildschirm konnte nicht geladen werden.\nWir bittem un Verzeihung für die Unannehmlichkeiten.\n\nEventuell lässt sich Ihr Problem beheben, indem Sie PDF-Over auf Werkseinstellungen zurücksetzen.\nMöchten Sie dies nun versuchen? error.InvalidBKU=Ungültige BKU-Auswahl. Bitte überprüfen. error.InvalidLocale=Ungültige Sprache error.InvalidPhoneNumber=Telefonnummer ungültig\! Beispiel\: +43664123456789 -- cgit v1.2.3