From 1002d6fe103173bf4fab4baca2a63a00a2842718 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sun, 21 Aug 2022 19:40:00 +0200 Subject: [PATCH 1/4] Close OO connection on exit Fixes #9075 JabRef exits now, LO document is kept open --- CHANGELOG.md | 1 + src/main/java/org/jabref/gui/JabRefMain.java | 2 + .../gui/openoffice/OOBibBaseConnect.java | 42 +++++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a869d7a09f0..4e20376a8b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed a bug where spaces are trimmed when highlighting differences in the Entries merge dialog. [koppor#371](https://github.com/koppor/jabref/issues/371) - We fixed several bugs regarding the manual and the autosave of library files that sometimes lead to exceptions or data loss. [#8448](https://github.com/JabRef/jabref/issues/8484), [#8746](https://github.com/JabRef/jabref/issues/8746), [#6684](https://github.com/JabRef/jabref/issues/6684), [#6644](https://github.com/JabRef/jabref/issues/6644), [#6102](https://github.com/JabRef/jabref/issues/6102), [#6002](https://github.com/JabRef/jabref/issues/6000) - We fixed an issue where applied save actions on saving the library file would lead to the dialog "The libary has been modified by another program" popping up [#4877](https://github.com/JabRef/jabref/issues/4877) +- We fixed an issue where JabRef would not exit when a connection to a LibreOffice document was established previously and the document is still open [#9075](https://github.com/JabRef/jabref/issues/9075) ### Removed diff --git a/src/main/java/org/jabref/gui/JabRefMain.java b/src/main/java/org/jabref/gui/JabRefMain.java index 777e35a514d..a9812e591ec 100644 --- a/src/main/java/org/jabref/gui/JabRefMain.java +++ b/src/main/java/org/jabref/gui/JabRefMain.java @@ -15,6 +15,7 @@ import org.jabref.cli.ArgumentProcessor; import org.jabref.cli.JabRefCLI; +import org.jabref.gui.openoffice.OOBibBaseConnect; import org.jabref.gui.remote.JabRefMessageHandler; import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.journals.JournalAbbreviationLoader; @@ -131,6 +132,7 @@ public void start(Stage mainStage) { @Override public void stop() { + OOBibBaseConnect.closeOfficeConnection(); Globals.stopBackgroundTasks(); Globals.shutdownThreadPools(); } diff --git a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java index 24660410fc3..03ea584094e 100644 --- a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java +++ b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java @@ -15,6 +15,8 @@ import org.jabref.model.openoffice.uno.UnoTextDocument; import org.jabref.model.openoffice.util.OOResult; +import com.sun.star.bridge.XBridge; +import com.sun.star.bridge.XBridgeFactory; import com.sun.star.comp.helper.BootstrapException; import com.sun.star.container.NoSuchElementException; import com.sun.star.container.XEnumeration; @@ -25,11 +27,16 @@ import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.text.XTextDocument; import com.sun.star.uno.XComponentContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import static com.sun.star.uno.UnoRuntime.queryInterface; /** * Establish connection to a document opened in OpenOffice or LibreOffice. */ -class OOBibBaseConnect { +public class OOBibBaseConnect { + + private static final Logger LOGGER = LoggerFactory.getLogger(OOBibBaseConnect.class); private final DialogService dialogService; private final XDesktop xDesktop; @@ -70,6 +77,27 @@ private XDesktop simpleBootstrap(Path loPath) return UnoCast.cast(XDesktop.class, desktop).get(); } + /** + * Close any open office connection, if none exists does nothing + */ + public static void closeOfficeConnection() { + try { + // get the bridge factory from the local service manager + XBridgeFactory bridgeFactory = queryInterface(XBridgeFactory.class, + org.jabref.gui.openoffice.Bootstrap.createSimpleServiceManager() + .createInstance("com.sun.star.bridge.BridgeFactory")); + + if (bridgeFactory != null) { + for (XBridge bridge : bridgeFactory.getExistingBridges()) { + // dispose of this bridge after closing its connection + queryInterface(XComponent.class, bridge).dispose(); + } + } + } catch (Exception ex) { + LOGGER.error("Exception disposing office process connection bridge:", ex); + } + } + private static List getTextDocuments(XDesktop desktop) throws NoSuchElementException, @@ -125,15 +153,15 @@ public String toString() { // auto-detecting instances, so we need to show dialog in FX // thread Optional selectedDocument = - (dialogService + dialogService .showChoiceDialogAndWait(Localization.lang("Select document"), Localization.lang("Found documents:"), Localization.lang("Use selected document"), - viewModel)); + viewModel); - return (selectedDocument + return selectedDocument .map(DocumentTitleViewModel::getXtextDocument) - .orElse(null)); + .orElse(null); } /** @@ -157,7 +185,7 @@ public void selectDocument(boolean autoSelectForSingle) List textDocumentList = getTextDocuments(this.xDesktop); if (textDocumentList.isEmpty()) { throw new NoDocumentFoundException("No Writer documents found"); - } else if (textDocumentList.size() == 1 && autoSelectForSingle) { + } else if ((textDocumentList.size() == 1) && autoSelectForSingle) { selected = textDocumentList.get(0); // Get the only one } else { // Bring up a dialog selected = OOBibBaseConnect.selectDocumentDialog(textDocumentList, @@ -233,4 +261,4 @@ public Optional getCurrentDocumentTitle() { return UnoTextDocument.getFrameTitle(this.xTextDocument); } } -} // end of OOBibBaseConnect +} From 78d9a710b6d8214f2388c07f2233a50e98676131 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sun, 21 Aug 2022 19:45:45 +0200 Subject: [PATCH 2/4] checkstyle --- src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java index 03ea584094e..03f83a86d42 100644 --- a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java +++ b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java @@ -31,6 +31,7 @@ import org.slf4j.LoggerFactory; import static com.sun.star.uno.UnoRuntime.queryInterface; + /** * Establish connection to a document opened in OpenOffice or LibreOffice. */ From 44eb7cf8c9ca867d83c82a03892f1f7fd3d3722d Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 22 Aug 2022 21:47:07 +0200 Subject: [PATCH 3/4] Update src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java Co-authored-by: Oliver Kopp --- src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java index 03f83a86d42..b77ec130448 100644 --- a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java +++ b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java @@ -79,7 +79,7 @@ private XDesktop simpleBootstrap(Path loPath) } /** - * Close any open office connection, if none exists does nothing + * Close any open office connection, if none exists does nothing */ public static void closeOfficeConnection() { try { From 3bb3ad27b0e70605976f46d03ccdcffa3958ba0c Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 22 Aug 2022 21:47:13 +0200 Subject: [PATCH 4/4] Update src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java Co-authored-by: Oliver Kopp --- src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java index b77ec130448..8a12722a17b 100644 --- a/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java +++ b/src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java @@ -95,7 +95,7 @@ public static void closeOfficeConnection() { } } } catch (Exception ex) { - LOGGER.error("Exception disposing office process connection bridge:", ex); + LOGGER.error("Exception disposing office process connection bridge", ex); } }