Recent

Author Topic: LibreOffice events listener  (Read 5739 times)

chris_laz

  • New member
  • *
  • Posts: 7
LibreOffice events listener
« on: October 07, 2024, 10:13:55 am »
Hi all,
I am attempting to port a Delphi app which runs LibreOffice by automation and listens for document save and close events, but I can't quite get it to work in Lazarus.

I am using Lazarus 3.6/FPC 3.22 on Windows 10, with LibreOffice 7.4 (all 64bit).

The Delphi code implements an event listener for LO by implementing a TAutoObject with an IConnectionPointContainer, but I can't get this to work in Lazarus, as FPC does not appear to include any implementations of the IConnectionPointContainer interface (Delphi has a TConnectionPoints class).
I have tried reimplementing this, but with no success so far.

I can run LO and open a doc in Calc successfully, but my event listener does nothing (but doesn't crash).

Has anyone managed to implement an event listener for LO in Lazarus?
I can share the code I've tried so far if that's any help, or if there is a pre-built event listener somewhere, that would also be a great help!

Thanks
« Last Edit: October 07, 2024, 06:34:49 pm by chris_laz »

cdbc

  • Hero Member
  • *****
  • Posts: 1774
    • http://www.cdbc.dk
Re: LibreOffice events listener
« Reply #1 on: October 07, 2024, 10:20:07 am »
Hi
Yup, let's see the code you have so far...  ;)
Crystal balls are hard to program with  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

egsuh

  • Hero Member
  • *****
  • Posts: 1520
Re: LibreOffice events listener
« Reply #2 on: October 07, 2024, 10:33:01 am »
Yes, I'd like to see your codes as well... (to learn more... ^^)

chris_laz

  • New member
  • *
  • Posts: 7
Re: LibreOffice events listener
« Reply #3 on: October 07, 2024, 11:10:26 am »
OK here goes -

In main unit

Code: Pascal  [Select][+][-]
  1. var
  2.       libreMgr, libreDsk, libreDoc: OleVariant;
  3.       libreEvents: ILibreOfficeEvents;
  4.  
  5. ...
  6.  
  7.       libreMgr := CreateOleObject('com.sun.star.ServiceManager');
  8.       libreDsk := LibreMgr.createInstance('com.sun.star.frame.Desktop');
  9.  
  10.       libreDoc := libreDsk.loadComponentFromFile(filename etc...);
  11.  
  12.       libreEvents := TLibreOfficeEvents.Create;
  13.       libreEvents.OnPrepareUnload := event handler method
  14.  
  15.       libreDoc.addEventListener(libreEvents);
  16.  

See attached units
LibreOfficeEventListener - implementation of TLibreOfficeEvents with internal ILibreOfficeEvents declaration
LibreOfficeEventListener_TLB - imported LO type library, declaration of LO ILibreOfficeEventListener interface
ActiveXConnectionPoints - custom implementation of IConnectionPointContainer
ComServNoRegisterLaz - override default FPC TComServer implementation to remove registry updates

I am not sure where my original Delphi implementation came from (back in 2011), I must have found some hints online, as I am pretty sure I did not work this all out for myself.
The LO type library must be loaded as a resource in the exe, but does not need to be added to the registry. This allows the app to run without admin privileges, hence the overrides of the default TComServer and TAutoObjectFactory to remove registry updates.

This all compiles OK in Lazarus, but the events do not fire at all after the LO document is opened and the event listener is added, and there are no runtime errors.

chris_laz

  • New member
  • *
  • Posts: 7
Re: LibreOffice events listener
« Reply #4 on: November 12, 2024, 03:31:45 pm »
No suggestions?
Has anyone else managed to hook LibreOffice automation events in a Lazarus app?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11986
  • FPC developer.
Re: LibreOffice events listener
« Reply #5 on: November 12, 2024, 04:16:36 pm »
Could you also add a small project to see how you test? I can at least test it on development versions of FPC.

chris_laz

  • New member
  • *
  • Posts: 7
Re: LibreOffice events listener
« Reply #6 on: November 12, 2024, 05:18:32 pm »
OK, here's a very simple test project, supplied as a zip. It should compile as-is in Lazarus, all required units are included (I think), plus a sample CSV file.

A form with one button and a memo.
Click the button to open a CSV file in Calc.
Start editing the CSV, and Calc's OnModified events should be logged in the memo - but nothing happens here, so obviously my plumbing is wrong.

I have Lazarus 3.6, FPC 3.2.2



Curt Carpenter

  • Hero Member
  • *****
  • Posts: 576
Re: LibreOffice events listener
« Reply #7 on: November 12, 2024, 06:18:05 pm »
I am not sure where my original Delphi implementation came from (back in 2011), I must have found some hints online, as I am pretty sure I did not work this all out for myself.

I've been wrestling with some old code issues for the past two weeks too -- wonderfully frustrating isn't it?  So many things have changed in the past thirteen years (across so many different interfaces in your case -- mine is much simpler   :)).   

chris_laz

  • New member
  • *
  • Posts: 7
Re: LibreOffice events listener
« Reply #8 on: December 31, 2024, 03:25:45 pm »
Just a thread bump, does anyone have any experience automating LibreOffice with Lazarus to share?
Thanks

rvk

  • Hero Member
  • *****
  • Posts: 6641
Re: LibreOffice events listener
« Reply #9 on: January 02, 2025, 11:28:57 am »
I'm not familiar with OO and LO automation via TLB... but when diving into this...

I see you used XEventListener.
If I look at the documentation of XEventListener I see it is deprecated:
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1document_1_1XEventListener.html

XDocumentEventListener would be the new one:
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1document_1_1XDocumentEventListener.html

It also states it uses XDocumentEventBroadcaster to be notified:
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1document_1_1XDocumentEventBroadcaster.html

And XDocumentEventBroadcaster has a addDocumentEventListener, removeDocumentEventListener and notifyDocumentEvent.

Shouldn't those be used???

And more... you are now using the COM style code to create a LibreOffice document, but are using the TLB style code to interact with it. I'm not sure if you can mix those.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11986
  • FPC developer.
Re: LibreOffice events listener
« Reply #10 on: January 02, 2025, 02:11:52 pm »
I traced through it yesterday on 32-bit to see where it goes wrong. I don't see calls failing, but the notifyevent callback doesn't get call, nor some of the "advise" methods.

Does this work in Delphi with current versions of LO ?   64-bit ?

chris_laz

  • New member
  • *
  • Posts: 7
Re: LibreOffice events listener
« Reply #11 on: January 02, 2025, 03:07:45 pm »
Hi marcov,
Yes, that's what I found when tracing through it. Nothing crashes, but the event sink never seems to be triggered.
This code works in Delphi 2006 (32bit) with LibreOffice 7.4. I've not tried it with the most recent versions of LO.
I've not tried compiling it in Lazarus as a 32bit exe.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11986
  • FPC developer.
Re: LibreOffice events listener
« Reply #12 on: January 02, 2025, 05:02:42 pm »
I tried FPC 3.3.x with a LO 7.6 in both 32-bit and 64-bit.

I do have delphi, but only unicode versions (Seattle aka XE10)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11986
  • FPC developer.
Re: LibreOffice events listener
« Reply #13 on: January 02, 2025, 07:17:13 pm »
I tested with delphi, and after changing the units somewhat for different interfaces, it works with XE 10 32-bit with the same LO.

So the setup is not the problem, the FPC compiled binary is.

I also tried some header changes to no avail. (the 2nd type of the next parameter mostly)
« Last Edit: January 03, 2025, 03:35:16 pm by marcov »

korba812

  • Sr. Member
  • ****
  • Posts: 446
Re: LibreOffice events listener
« Reply #14 on: January 04, 2025, 04:58:47 pm »
It seems that the problem is a small bug in the TAutoObject.GetTypeInfo function - should return DispTypeInfo instead of ClassInfo.

 

TinyPortal © 2005-2018