From a255311924fbfc529205ad507bb31021cc687f72 Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 22 Aug 2022 21:58:24 +0200 Subject: [PATCH] Close OO connection on JabRef exit (#9076) * Close OO connection on exit Fixes #9075 JabRef exits now, LO document is kept open * checkstyle * Update src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java * Update src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java --- CHANGELOG.md | 1 + src/main/java/org/jabref/gui/JabRefMain.java | 2 + .../gui/openoffice/OOBibBaseConnect.java | 43 ++++++++++++++++--- 3 files changed, 39 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..8a12722a17b 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,17 @@ 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 +78,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 +154,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 +186,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 +262,4 @@ public Optional getCurrentDocumentTitle() { return UnoTextDocument.getFrameTitle(this.xTextDocument); } } -} // end of OOBibBaseConnect +}