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

Configure backup path in prefs #9809

Merged
merged 22 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public boolean quit() {
context.clearDBMSSynchronizer();
}
AutosaveManager.shutdown(context);
BackupManager.shutdown(context);
BackupManager.shutdown(context, prefs);
context.getDatabasePath().map(Path::toAbsolutePath).map(Path::toString).ifPresent(filenames::add);
}

Expand Down Expand Up @@ -1312,7 +1312,7 @@ private void closeTab(LibraryTab libraryTab) {
removeTab(libraryTab);
}
AutosaveManager.shutdown(context);
BackupManager.shutdown(context);
BackupManager.shutdown(context, prefs);
}

private void removeTab(LibraryTab libraryTab) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ private void saveDividerLocation(Number position) {
public void cleanUp() {
changeMonitor.ifPresent(DatabaseChangeMonitor::unregister);
AutosaveManager.shutdown(bibDatabaseContext);
BackupManager.shutdown(bibDatabaseContext);
BackupManager.shutdown(bibDatabaseContext, preferencesService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public BackupResolverDialog(Path originalPath) {
getDialogPane().setMinHeight(180);
getDialogPane().getButtonTypes().setAll(RESTORE_FROM_BACKUP, REVIEW_BACKUP, IGNORE_BACKUP);

Optional<Path> backupPathOpt = BackupFileUtil.getPathOfLatestExistingBackupFile(originalPath, BackupFileType.BACKUP);
Optional<Path> backupPathOpt = BackupFileUtil.yyyyyyyyyy(originalPath, BackupFileType.BACKUP);
String backupFilename = backupPathOpt.map(Path::getFileName).map(Path::toString).orElse(Localization.lang("File not found"));
String content = new StringBuilder()
.append(Localization.lang("A backup file for '%0' was found at [%1]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void execute() {
checkOverwriteKeysChosen();

if (!this.isCanceled) {
BackgroundTask backgroundTask = this.generateKeysInBackground();
BackgroundTask<Void> backgroundTask = this.generateKeysInBackground();
backgroundTask.showToUser(true);
backgroundTask.titleProperty().set(Localization.lang("Autogenerate citation keys"));
backgroundTask.messageProperty().set(Localization.lang("%0/%1 entries", 0, entries.size()));
Expand Down Expand Up @@ -93,8 +93,8 @@ private void checkOverwriteKeysChosen() {
}
}

private BackgroundTask generateKeysInBackground() {
return new BackgroundTask<Void>() {
private BackgroundTask<Void> generateKeysInBackground() {
return new BackgroundTask<>() {
private NamedCompound compound;

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/dialogs/BackupUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Optional<ParserResult> showRestoreBackupDialog(DialogService dialo
var actionOpt = showBackupResolverDialog(dialogService, originalPath);
return actionOpt.flatMap(action -> {
if (action == BackupResolverDialog.RESTORE_FROM_BACKUP) {
BackupManager.restoreBackup(originalPath);
BackupManager.restoreBackup(originalPath, preferencesService.getFilePreferences().getBackupDirectory());
return Optional.empty();
} else if (action == BackupResolverDialog.REVIEW_BACKUP) {
return showReviewBackupDialog(dialogService, originalPath, preferencesService);
Expand All @@ -63,7 +63,7 @@ private static Optional<ParserResult> showReviewBackupDialog(DialogService dialo
// This will be modified by using the `DatabaseChangesResolverDialog`.
BibDatabaseContext originalDatabase = originalParserResult.getDatabaseContext();

Path backupPath = BackupFileUtil.getPathOfLatestExistingBackupFile(originalPath, BackupFileType.BACKUP).orElseThrow();
Path backupPath = BackupFileUtil.yyyyyyyyyy(originalPath, BackupFileType.BACKUP).orElseThrow();
BibDatabaseContext backupDatabase = OpenDatabase.loadDatabase(backupPath, importFormatPreferences, new DummyFileUpdateMonitor()).getDatabaseContext();

DatabaseChangeResolverFactory changeResolverFactory = new DatabaseChangeResolverFactory(dialogService, originalDatabase, preferencesService.getBibEntryPreferences());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ boolean saveAs(Path file, SaveDatabaseMode mode) {
final Path oldFile = databasePath.get();
context.setDatabasePath(oldFile);
AutosaveManager.shutdown(context);
BackupManager.shutdown(context);
BackupManager.shutdown(context, this.preferences);
}

// Set new location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ private ParserResult loadDatabase(Path file) throws Exception {
dialogService.notify(Localization.lang("Opening") + ": '" + file + "'");

preferencesService.getFilePreferences().setWorkingDirectory(fileToLoad.getParent());
Path backupDir = preferencesService.getFilePreferences().getBackupDirectory();

ParserResult parserResult = null;
if (BackupManager.backupFileDiffers(fileToLoad)) {
if (BackupManager.backupFileDiffers(fileToLoad, backupDir)) {
// In case the backup differs, ask the user what to do.
// In case the user opted for restoring a backup, the content of the backup is contained in parserResult.
parserResult = BackupUIManager.showRestoreBackupDialog(dialogService, fileToLoad, preferencesService).orElse(null);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/preferences/file/FileTab.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>
<?import org.jabref.gui.icon.JabRefIconView?>

<fx:root spacing="10.0" type="VBox"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="org.jabref.gui.preferences.file.FileTab">
Expand Down Expand Up @@ -42,4 +46,19 @@
</HBox>

<CheckBox fx:id="alwaysReformatBib" text="%Always reformat BIB file on save and export"/>
<HBox alignment="CENTER_LEFT" spacing="10.0">
<CheckBox fx:id="createBackup" text="%Create backup"></CheckBox>
<TextField fx:id="backupDirectory" HBox.hgrow="ALWAYS"/>
<Button fx:id="browseDirectory" onAction="#backupFileDirBrowse"
styleClass="icon-button,narrow"
prefHeight="20.0" prefWidth="20.0" GridPane.columnIndex="2">
<graphic>
<JabRefIconView glyph="OPEN"/>
</graphic>
<tooltip>
<Tooltip text="%Browse"/>
</tooltip>
</Button>
</HBox>

</fx:root>
15 changes: 12 additions & 3 deletions src/main/java/org/jabref/gui/preferences/file/FileTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;

import org.jabref.gui.Globals;
import org.jabref.gui.DialogService;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.help.HelpAction;
Expand All @@ -16,6 +16,7 @@
import org.jabref.logic.l10n.Localization;

import com.airhacks.afterburner.views.ViewLoader;
import jakarta.inject.Inject;

public class FileTab extends AbstractPreferenceTabView<FileTabViewModel> implements PreferencesTab {

Expand All @@ -25,18 +26,22 @@ public class FileTab extends AbstractPreferenceTabView<FileTabViewModel> impleme
@FXML private RadioButton resolveStrings;
@FXML private TextField resolveStringsForFields;
@FXML private CheckBox alwaysReformatBib;
@FXML private CheckBox createBackup;
@FXML private TextField backupDirectory;

@FXML private CheckBox autosaveLocalLibraries;
@FXML private Button autosaveLocalLibrariesHelp;

@Inject private DialogService dialogService;

public FileTab() {
ViewLoader.view(this)
.root(this)
.load();
}

public void initialize() {
this.viewModel = new FileTabViewModel(preferencesService.getImportExportPreferences());
this.viewModel = new FileTabViewModel(preferencesService.getImportExportPreferences(), preferencesService.getFilePreferences(), dialogService);

openLastStartup.selectedProperty().bindBidirectional(viewModel.openLastStartupProperty());
noWrapFiles.textProperty().bindBidirectional(viewModel.noWrapFilesProperty());
Expand All @@ -49,12 +54,16 @@ public void initialize() {
alwaysReformatBib.selectedProperty().bindBidirectional(viewModel.alwaysReformatBibProperty());
autosaveLocalLibraries.selectedProperty().bindBidirectional(viewModel.autosaveLocalLibrariesProperty());

ActionFactory actionFactory = new ActionFactory(Globals.getKeyPrefs());
ActionFactory actionFactory = new ActionFactory(preferencesService.getKeyBindingRepository());
actionFactory.configureIconButton(StandardActions.HELP, new HelpAction(HelpFile.AUTOSAVE, dialogService), autosaveLocalLibrariesHelp);
}

@Override
public String getTabName() {
return Localization.lang("File");
}

public void backupFileDirBrowse() {
viewModel.backupFileDirBrowse();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.jabref.gui.preferences.file;

import java.nio.file.Path;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.gui.DialogService;
import org.jabref.gui.preferences.PreferenceTabViewModel;
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.ImportExportPreferences;

public class FileTabViewModel implements PreferenceTabViewModel {
Expand All @@ -18,10 +23,17 @@ public class FileTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty alwaysReformatBibProperty = new SimpleBooleanProperty();
private final BooleanProperty autosaveLocalLibraries = new SimpleBooleanProperty();

private final BooleanProperty createBackupProperty = new SimpleBooleanProperty();
private final StringProperty backupDirectoryProperty = new SimpleStringProperty("");

private final ImportExportPreferences importExportPreferences;
private final FilePreferences filePreferences;
private final DialogService dialogService;

FileTabViewModel(ImportExportPreferences importExportPreferences) {
FileTabViewModel(ImportExportPreferences importExportPreferences, FilePreferences filePreferences, DialogService dialogService) {
this.importExportPreferences = importExportPreferences;
this.filePreferences = filePreferences;
this.dialogService = dialogService;
}

@Override
Expand All @@ -34,6 +46,9 @@ public void setValues() {
resolveStringsForFieldsProperty.setValue(importExportPreferences.getResolvableFields());
alwaysReformatBibProperty.setValue(importExportPreferences.shouldAlwaysReformatOnSave());
autosaveLocalLibraries.setValue(importExportPreferences.shouldAutoSave());

createBackupProperty.setValue(filePreferences.shouldCreateBackup());
backupDirectoryProperty.setValue(filePreferences.getBackupDirectory().toString());
}

@Override
Expand All @@ -44,6 +59,9 @@ public void storeSettings() {
importExportPreferences.setResolvableFields(resolveStringsForFieldsProperty.getValue().trim());
importExportPreferences.setAlwaysReformatOnSave(alwaysReformatBibProperty.getValue());
importExportPreferences.setAutoSave(autosaveLocalLibraries.getValue());

filePreferences.createBackupProperty().setValue(createBackupProperty.getValue());
filePreferences.backupDirectoryProperty().setValue(Path.of(backupDirectoryProperty.getValue()));
}

// General
Expand Down Expand Up @@ -78,4 +96,20 @@ public BooleanProperty alwaysReformatBibProperty() {
public BooleanProperty autosaveLocalLibrariesProperty() {
return autosaveLocalLibraries;
}

public BooleanProperty createBackupProperty() {
return this.createBackupProperty;
}

public StringProperty backupDirectoryProperty() {
return this.backupDirectoryProperty;
}

public void backupFileDirBrowse() {
DirectoryDialogConfiguration dirDialogConfiguration =
new DirectoryDialogConfiguration.Builder().withInitialDirectory(Path.of(backupDirectoryProperty().getValue())).build();
dialogService.showDirectorySelectionDialog(dirDialogConfiguration)
.ifPresent(dir -> backupDirectoryProperty.setValue(dir.toString()));
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved

}
}
Loading