Skip to content

Commit

Permalink
Fixes warning dialogs not working on MacOS. Closes #21
Browse files Browse the repository at this point in the history
Unknown consequences for errors during ReferenceMarks comparison.
I could not find any invocations of the call, but the function may be used by
LibreOffice itself. Worst case stuff will be very broken for people and errors
won't bubble up, but it doesn't look like there will be any actual consequences
  • Loading branch information
adomasven committed Jul 21, 2017
1 parent 7f77de7 commit 73c3639
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
57 changes: 55 additions & 2 deletions build/source/org/zotero/integration/ooo/comp/Document.java
Expand Up @@ -24,12 +24,16 @@

package org.zotero.integration.ooo.comp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.stream.Collectors;

import com.sun.jna.Platform;
import com.sun.star.awt.MessageBoxButtons;
import com.sun.star.awt.MessageBoxType;
import com.sun.star.awt.XMessageBox;
Expand Down Expand Up @@ -139,7 +143,8 @@ public void complete() throws InvalidStateException {
app.documentComplete(ID);
}

public int displayAlert(String text, int icon, int buttons) {
public int displayAlert(String text, int icon, int buttons) throws Exception {
if (Platform.isMac()) return displayAlertMacOS(text, icon, buttons);
// figure out appropriate buttons
int ooButtons = MessageBoxButtons.BUTTONS_OK;
if(buttons == 1) {
Expand Down Expand Up @@ -174,6 +179,54 @@ public int displayAlert(String text, int icon, int buttons) {
}
return result;
}

/**
* Solution for https://github.com/zotero/zotero-libreoffice-integration/issues/21
* https://bugs.documentfoundation.org/show_bug.cgi?id=106292
*/
public int displayAlertMacOS(String text, int icon, int buttons) throws Exception {
String script = "osascript -e \"tell app \\\"LibreOffice\\\" to display dialog \\\"" + text + "\\\"";
script += " with title \\\"Zotero Integration\\\" with icon " + icon + " buttons ";
switch (buttons) {
case 3:
script += "{\\\"Yes\\\", \\\"No\\\", \\\"Cancel\\\"} default button 1";
break;
case 2:
script += "{\\\"Yes\\\", \\\"No\\\"} default button 1";
break;
case 1:
script += "{\\\"OK\\\", \\\"Cancel\\\"}";
break;
default:
script += "{\\\"OK\\\"}";
break;
}
// prints to stderr on cancel which also returns 1
script += "\" 2>&1";
Process p = new ProcessBuilder("/bin/bash", "-c", script).start();
p.waitFor();
String output = new BufferedReader(new InputStreamReader(p.getInputStream()))
.lines().collect(Collectors.joining("\n"));

// ofc they spelt it 'canceled'
if (p.exitValue() != 0 && !output.contains("User canceled")) {
System.out.println(script);
System.out.println(output);
throw new RuntimeException();
}

if (output.contains("Yes")) {
if (buttons == 3) return 2;
else return 1;
} else if (output.contains("No")) {
if (buttons == 3) return 1;
else return 0;
} else if (output.contains("OK") && buttons == 1) {
return 1;
} else {
return 0;
}
}

public void activate() throws Exception {
if(System.getProperty("os.name").equals("Mac OS X")) {
Expand All @@ -182,7 +235,7 @@ public void activate() throws Exception {
}
}

public boolean canInsertField(String fieldType) {
public boolean canInsertField(String fieldType) throws Exception {
// first, check if cursor is in the bibliography (no sense offering to replace it)
XTextViewCursor selection = getSelection();
XTextSection section = (XTextSection) UnoRuntime.queryInterface(XTextSection.class, selection);
Expand Down
18 changes: 9 additions & 9 deletions build/source/org/zotero/integration/ooo/comp/ReferenceMark.java
Expand Up @@ -364,8 +364,9 @@ public int compareTo(ReferenceMark o) {
if(isNote && o.isNote) {
try {
cmp = textRangeCompare.compareRegionStarts(o.range, range);
} catch (com.sun.star.lang.IllegalArgumentException e) {
doc.displayAlert(Document.getErrorString(e), 0, 0);
} catch (Exception e) {
//doc.displayAlert(Document.getErrorString(e), 0, 0);
e.printStackTrace();
return 0;
}
} else if(table != null && o.table != null) {
Expand All @@ -378,20 +379,19 @@ public int compareTo(ReferenceMark o) {
try {
cell1Name = (String) cell1.getPropertyValue("CellName");
cell2Name = (String) cell2.getPropertyValue("CellName");
} catch (UnknownPropertyException e) {
doc.displayAlert(Document.getErrorString(e), 0, 0);
return 0;
} catch (WrappedTargetException e) {
doc.displayAlert(Document.getErrorString(e), 0, 0);
} catch (Exception e) {
//doc.displayAlert(Document.getErrorString(e), 0, 0);
e.printStackTrace();
return 0;
}

if(cell1Name.equals(cell2Name)) {
// should be in the same cell; compare ranges directly
try {
cmp = textRangeCompare.compareRegionStarts(o.range, range);
} catch (com.sun.star.lang.IllegalArgumentException e) {
doc.displayAlert(Document.getErrorString(e), 0, 0);
} catch (Exception e) {
//doc.displayAlert(Document.getErrorString(e), 0, 0);
e.printStackTrace();
return 0;
}
} else {
Expand Down

0 comments on commit 73c3639

Please sign in to comment.