Skip to content

Commit

Permalink
Close OO connection on JabRef exit (#9076)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Siedlerchr authored Aug 22, 2022
1 parent 8836cf4 commit a255311
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,6 +132,7 @@ public void start(Stage mainStage) {

@Override
public void stop() {
OOBibBaseConnect.closeOfficeConnection();
Globals.stopBackgroundTasks();
Globals.shutdownThreadPools();
}
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/org/jabref/gui/openoffice/OOBibBaseConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<XTextDocument> getTextDocuments(XDesktop desktop)
throws
NoSuchElementException,
Expand Down Expand Up @@ -125,15 +154,15 @@ public String toString() {
// auto-detecting instances, so we need to show dialog in FX
// thread
Optional<DocumentTitleViewModel> 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);
}

/**
Expand All @@ -157,7 +186,7 @@ public void selectDocument(boolean autoSelectForSingle)
List<XTextDocument> 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,
Expand Down Expand Up @@ -233,4 +262,4 @@ public Optional<String> getCurrentDocumentTitle() {
return UnoTextDocument.getFrameTitle(this.xTextDocument);
}
}
} // end of OOBibBaseConnect
}

0 comments on commit a255311

Please sign in to comment.