Search code examples
javalibreofficeuno

How to store transient data per open document via UNO?


I have some configuration values (integers and strings) passed to a launcher program and later needed and changed in an extension. They can differ between documents but must not be saved along with them.

Can I store these directly in the LibreOffice process?
I have or can get a reference to the document as XStorable in either case.

So far, I tried to use XPropertyContainer.addProperty(...) on the document, but the values I pass in seem to be stored globally istead of per document:

XDocumentPropertiesSupplier xDocumentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, document);
XDocumentProperties xDocumentProperties = xDocumentPropertiesSupplier.getDocumentProperties();
XPropertyContainer xPropertyContainer = xDocumentProperties.getUserDefinedProperties();
xPropertyContainer.addProperty(propertyName, PropertyAttribute.TRANSIENT | PropertyAttribute.MAYBEDEFAULT, propertyValue);

Solution

  • In the following python code, the values are different for each document, and they are not stored.

    import uno
    from com.sun.star.beans.PropertyAttribute import TRANSIENT
    from com.sun.star.beans import UnknownPropertyException
    
    def temp_prop():
        DIR = "/path/to/dir"
        LOG1 = DIR + "log1.txt"
        LOG2 = DIR + "log2.txt"
        PROPNAME = "myPropName"
        oDesktop = XSCRIPTCONTEXT.getDesktop()
        oDoc1 = oDesktop.loadComponentFromURL(
            "file:///" + DIR + "1.odt", "_default", 0, ())
        oDoc2 = oDesktop.loadComponentFromURL(
            "file:///" + DIR + "2.odt", "_default", 0, ())
        oDoc1props = oDoc1.getDocumentProperties().getUserDefinedProperties();
        oDoc2props = oDoc2.getDocumentProperties().getUserDefinedProperties();
        try:
            appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
            appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
        except UnknownPropertyException:
            appendToFile(LOG1, "Unknown Property")
            appendToFile(LOG2, "Unknown Property")
        oDoc1props.addProperty(PROPNAME, TRANSIENT, "a")
        oDoc2props.addProperty(PROPNAME, TRANSIENT, "b")
        appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
        appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
        oDoc1.store()
        oDoc2.store()
        oDoc1props = oDoc1.getDocumentProperties().getUserDefinedProperties();
        oDoc2props = oDoc2.getDocumentProperties().getUserDefinedProperties();
        appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
        appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
    
    def appendToFile(fname, s):
        with open(fname, "a") as f: 
            f.write(s + ",")
    

    Results:

    logfile1: Unknown Property,a,a,
    logfile2: Unknown Property,b,b,
    

    When the documents are closed and then the code is run again, the exact same results occur, proving that the property is not stored.