Skip to content

Commit 16874d6

Browse files
kopporstefan-kolb
andcommitted
When JabRef finds a .sav file without changes, there is no dialog asking for acceptance of changes anymore.
Co-authored-by: Stefan Kolb <stefan-kolb@web.de>
1 parent 8b7c374 commit 16874d6

File tree

11 files changed

+125
-18
lines changed

11 files changed

+125
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
1313

1414
### Changed
1515

16+
- When JabRef finds a `.sav` file without changes, there is no dialog asking for acceptance of changes anymore.
17+
1618
### Fixed
1719

1820
- We fixed an issue where opening a library from the recent libraries menu was not possible [#5939](https://github.com/JabRef/jabref/issues/5939)

src/main/java/org/jabref/JabRefGUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void openLastEditedDatabases() {
247247
continue;
248248
}
249249

250-
if (BackupManager.checkForBackupFile(dbFile.toPath())) {
250+
if (BackupManager.backupFileDiffers(dbFile.toPath())) {
251251
BackupUIManager.showRestoreBackupDialog(mainFrame.getDialogService(), dbFile.toPath());
252252
}
253253

src/main/java/org/jabref/gui/JabRefFrame.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public boolean quit() {
380380
List<String> filenames = new ArrayList<>();
381381
for (int i = 0; i < tabbedPane.getTabs().size(); i++) {
382382
BasePanel panel = getBasePanelAt(i);
383-
BibDatabaseContext context = panel.getBibDatabaseContext();
383+
final BibDatabaseContext context = panel.getBibDatabaseContext();
384384

385385
if (panel.isModified() && (context.getLocation() == DatabaseLocation.LOCAL)) {
386386
tabbedPane.getSelectionModel().select(i);
@@ -1159,7 +1159,7 @@ private void closeTab(BasePanel panel) {
11591159
return;
11601160
}
11611161

1162-
BibDatabaseContext context = panel.getBibDatabaseContext();
1162+
final BibDatabaseContext context = panel.getBibDatabaseContext();
11631163

11641164
if (panel.isModified() && (context.getLocation() == DatabaseLocation.LOCAL)) {
11651165
if (confirmClose(panel)) {

src/main/java/org/jabref/gui/dialogs/BackupUIManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
public class BackupUIManager {
1414

1515
private BackupUIManager() {
16-
1716
}
1817

1918
public static void showRestoreBackupDialog(DialogService dialogService, Path originalPath) {

src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private ParserResult loadDatabase(Path file) throws Exception {
181181

182182
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, fileToLoad.getParent().toString());
183183

184-
if (BackupManager.checkForBackupFile(fileToLoad)) {
184+
if (BackupManager.backupFileDiffers(fileToLoad)) {
185185
BackupUIManager.showRestoreBackupDialog(dialogService, fileToLoad);
186186
}
187187

src/main/java/org/jabref/logic/autosaveandbackup/BackupManager.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
/**
3131
* Backups the given bib database file from {@link BibDatabaseContext} on every {@link BibDatabaseContextChangedEvent}.
3232
* An intelligent {@link ExecutorService} with a {@link BlockingQueue} prevents a high load while making backups and
33-
* rejects all redundant backup tasks.
34-
* This class does not manage the .bak file which is created when opening a database.
33+
* rejects all redundant backup tasks. This class does not manage the .bak file which is created when opening a
34+
* database.
3535
*/
3636
public class BackupManager {
3737

3838
private static final Logger LOGGER = LoggerFactory.getLogger(BackupManager.class);
3939

40-
private static final String BACKUP_EXTENSION = ".sav";
40+
// This differs from org.jabref.logic.exporter.AtomicFileOutputStream.BACKUP_EXTENSION, which is used for copying the .bib away before overwriting on save.
41+
private static final String AUTOSAVE_FILE_EXTENSION = ".sav";
4142

4243
private static Set<BackupManager> runningInstances = new HashSet<>();
4344

@@ -58,12 +59,12 @@ private BackupManager(BibDatabaseContext bibDatabaseContext, BibEntryTypesManage
5859
}
5960

6061
static Path getBackupPath(Path originalPath) {
61-
return FileUtil.addExtension(originalPath, BACKUP_EXTENSION);
62+
return FileUtil.addExtension(originalPath, AUTOSAVE_FILE_EXTENSION);
6263
}
6364

6465
/**
65-
* Starts the BackupManager which is associated with the given {@link BibDatabaseContext}.
66-
* As long as no database file is present in {@link BibDatabaseContext}, the {@link BackupManager} will do nothing.
66+
* Starts the BackupManager which is associated with the given {@link BibDatabaseContext}. As long as no database
67+
* file is present in {@link BibDatabaseContext}, the {@link BackupManager} will do nothing.
6768
*
6869
* @param bibDatabaseContext Associated {@link BibDatabaseContext}
6970
*/
@@ -86,13 +87,27 @@ public static void shutdown(BibDatabaseContext bibDatabaseContext) {
8687
}
8788

8889
/**
89-
* Checks whether a backup file exists for the given database file.
90+
* Checks whether a backup file exists for the given database file. If it exists, it is checked whether it is
91+
* different from the original.
9092
*
91-
* @param originalPath Path to the file a backup should be checked for.
93+
* @param originalPath Path to the file a backup should be checked for. Example: jabref.bib.
94+
* @return <code>true</code> if backup file exists AND differs from originalPath. <code>false</code> is the
95+
* "default" return value in the good case. In the case of an exception <code>true</code> is returned to ensure that
96+
* the user checks the output.
9297
*/
93-
public static boolean checkForBackupFile(Path originalPath) {
98+
public static boolean backupFileDiffers(Path originalPath) {
9499
Path backupPath = getBackupPath(originalPath);
95-
return Files.exists(backupPath) && !Files.isDirectory(backupPath);
100+
if (!Files.exists(backupPath) || Files.isDirectory(backupPath)) {
101+
return false;
102+
}
103+
104+
try {
105+
return !com.google.common.io.Files.equal(originalPath.toFile(), backupPath.toFile());
106+
} catch (IOException e) {
107+
LOGGER.debug("Could not compare original file and backup file.", e);
108+
// User has to investigate in this case
109+
return true;
110+
}
96111
}
97112

98113
/**
@@ -148,8 +163,8 @@ private void startBackupTask() {
148163
}
149164

150165
/**
151-
* Unregisters the BackupManager from the eventBus of {@link BibDatabaseContext} and deletes the backup file.
152-
* This method should only be used when closing a database/JabRef legally.
166+
* Unregisters the BackupManager from the eventBus of {@link BibDatabaseContext} and deletes the backup file. This
167+
* method should only be used when closing a database/JabRef legally.
153168
*/
154169
private void shutdown() {
155170
changeFilter.unregisterListener(this);

src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void initialize(String newStyle, CitationStyleOutputFormat newFormat) th
8484
*/
8585
private static class JabRefItemDataProvider implements ItemDataProvider {
8686

87-
private final ArrayList<BibEntry> data = new ArrayList<>();
87+
private final List<BibEntry> data = new ArrayList<>();
8888

8989
/**
9090
* Converts the {@link BibEntry} into {@link CSLItemData}.

src/test/java/org/jabref/logic/autosaveandbackup/BackupManagerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.junit.jupiter.api.Test;
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
911

1012
public class BackupManagerTest {
1113

@@ -15,4 +17,22 @@ public void backupFileNameIsCorrectlyGeneratedWithinTmpDirectory() {
1517
Path savPath = BackupManager.getBackupPath(bibPath);
1618
assertEquals(Paths.get("tmp", "test.bib.sav"), savPath);
1719
}
20+
21+
@Test
22+
public void backupFileIsEqualForNonExistingBackup() throws Exception {
23+
Path originalFile = Path.of(BackupManagerTest.class.getResource("no-backup.bib").toURI());
24+
assertFalse(BackupManager.backupFileDiffers(originalFile));
25+
}
26+
27+
@Test
28+
public void backupFileIsEqual() throws Exception {
29+
Path originalFile = Path.of(BackupManagerTest.class.getResource("no-changes.bib").toURI());
30+
assertFalse(BackupManager.backupFileDiffers(originalFile));
31+
}
32+
33+
@Test
34+
public void backupFileDiffers() throws Exception {
35+
Path originalFile = Path.of(BackupManagerTest.class.getResource("changes.bib").toURI());
36+
assertTrue(BackupManager.backupFileDiffers(originalFile));
37+
}
1838
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% Encoding: UTF-8
2+
3+
@Article{GarridoKallstromKummEtAl2016,
4+
author = {Mario Garrido and Petter Kallstrom and Martin Kumm and Oscar Gustafsson},
5+
title = {{CORDIC} {II:} {A} New Improved {CORDIC} Algorithm},
6+
journal = {{IEEE} Trans. on Circuits and Systems},
7+
year = {2016},
8+
volume = {63-II},
9+
number = {2},
10+
pages = {186--190},
11+
bibsource = {dblp computer science bibliography, http://dblp.org},
12+
biburl = {http://dblp.uni-trier.de/rec/bib/journals/tcas/GarridoKKG16},
13+
doi = {10.1109/TCSII.2015.2483422},
14+
timestamp = {Mon, 08 Feb 2016 00:00:00 +0100},
15+
}
16+
17+
@InProceedings{GustafssonJohansson2015,
18+
author = {Oscar Gustafsson and H{\aa}kan Johansson},
19+
title = {Decimation filters for high-speed delta-sigma modulators with passband constraints: General versus CIC-based {FIR} filters},
20+
booktitle = {2015 {IEEE} International Symposium on Circuits and Systems, {ISCAS} 2015, Lisbon, Portugal, May 24-27, 2015},
21+
year = {2015},
22+
pages = {2205--2208},
23+
publisher = {{IEEE}},
24+
bibsource = {dblp computer science bibliography, http://dblp.org},
25+
biburl = {http://dblp.uni-trier.de/rec/bib/conf/iscas/GustafssonJ15},
26+
doi = {10.1109/ISCAS.2015.7169119},
27+
timestamp = {Wed, 05 Aug 2015 09:08:53 +0200},
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% Encoding: UTF-8
2+
3+
@Article{GarridoKallstromKummEtAl2016,
4+
author = {Mario Garrido and Petter Kallstrom and Martin Kumm and Oscar Gustafsson},
5+
title = {{CORDIC} {II:} {A} New Improved {CORDIC} Algorithm},
6+
journal = {{IEEE} Trans. on Circuits and Systems},
7+
year = {2016},
8+
volume = {63-II},
9+
number = {2},
10+
pages = {186--190},
11+
bibsource = {dblp computer science bibliography, http://dblp.org},
12+
biburl = {http://dblp.uni-trier.de/rec/bib/journals/tcas/GarridoKKG16},
13+
doi = {10.1109/TCSII.2015.2483422},
14+
timestamp = {Mon, 08 Feb 2016 00:00:00 +0100},
15+
}
16+
17+
@InProceedings{GustafssonJohansson2015,
18+
author = {Oscar Gustafsson and H{\aa}kan Johansson},
19+
title = {Decimation filters for high-speed delta-sigma modulators with passband constraints: General versus CIC-based {FIR} filters},
20+
booktitle = {2015 {IEEE} International Symposium on Circuits and Systems, {ISCAS} 2015, Lisbon, Portugal, May 24-27, 2015},
21+
year = {2015},
22+
pages = {2205--2208},
23+
publisher = {{IEEE}},
24+
bibsource = {dblp computer science bibliography, http://dblp.org},
25+
biburl = {http://dblp.uni-trier.de/rec/bib/conf/iscas/GustafssonJ15},
26+
doi = {10.1109/ISCAS.2015.7169119},
27+
timestamp = {Wed, 05 Aug 2015 09:08:53 +0200},
28+
}

0 commit comments

Comments
 (0)