Is there any loading status for LoadComponentFromURL

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
DS20014
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Is there any loading status for LoadComponentFromURL

Post by DS20014 »

Hello,

I've got a problem with <LoadComponentFromURL>.
I've got a dummy odt-document which includes only the macro code. This code opens another document with LoadComponentFromUrl, then changes several things (e.g. with ReplaceDescriptor - ReplaceAll) and then closes then document again. This is repeated with several documents.

So the issues is : In this scenario the code after LoadComponentFromURL does not work properly.

Code: Select all

 1. ... LoadComponentFromURL
2. Code for Replacing expressions
If a wait is added after the loading of the document everything works fine.

Code: Select all

 1. ... LoadComponentFromURL
2.  wait(2000)
3. Code for Replacing expressions
So I guess there is a problem in Basic. Probably the code is continuing after LoadComponentFromURL although the document is not loaded completly. The work around for this would be to add the wait(2000) statement, which is quick and dirty but not a proper way to handle this issue.

So my question is:

Is there any possiblity in Basic to get a status of the loading of a document ? Like a readystate of the document itself ?

Thx
Daniel.
openOffice 4.1.1 on OpenSUSE 12.1
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Is there any loading status for LoadComponentFromURL

Post by B Marcelly »

Hi,
Try this :

Code: Select all

myDoc = StarDesktop.loadComponentFromURL(.....)
Do Until myDoc.IsLoaded
  wait 200
Loop 
By the way, this is not a problem with Basic, but with the API.
OpenOffice distinguishes the programming language and the Application Programming Interface.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
DS20014
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Post by DS20014 »

B Marcelly wrote:Hi,
Try this :

Code: Select all

myDoc = StarDesktop.loadComponentFromURL(.....)
Do Until myDoc.IsLoaded
  wait 200
Loop 
By the way, this is not a problem with Basic, but with the API.
OpenOffice distinguishes the programming language and the Application Programming Interface.
Sorry, but afaik there is no method like "isLoaded" for this object.
I tried it, but it does not work.
openOffice 4.1.1 on OpenSUSE 12.1
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Is there any loading status for LoadComponentFromURL

Post by B Marcelly »

DS20014 wrote:Sorry, but afaik there is no method like "isLoaded" for this object.
:oops: this property is available in Calc but not in Writer.

I did some tests with Writer documents.
Method .loadComponentFromURL returns after some time, e.g 600ms for a 170 pages document, and up to 20 seconds for the huge AndrewMacro document (445 pages).
I did a replaceAll (replace the term OpenOffice by a long sentence) in both documents, it works correctly.
Stats for AndrewMacro document: 19 sec for loading (Hidden mode), 186 replacements in 78ms, 40 sec to store and close.
DS20014 wrote:the code after LoadComponentFromURL does not work properly.
What do you mean ?
Perhaps your code is creating the problem.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Is there any loading status for LoadComponentFromURL

Post by RPG »

Hello

I think there is no real solution but maybe the pagecount can help.

Code: Select all

print oDoc.currentcontroller.pagecount
Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
DS20014
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Post by DS20014 »

B Marcelly wrote:
DS20014 wrote:Sorry, but afaik there is no method like "isLoaded" for this object.
:oops: this property is available in Calc but not in Writer.

I did some tests with Writer documents.
Method .loadComponentFromURL returns after some time, e.g 600ms for a 170 pages document, and up to 20 seconds for the huge AndrewMacro document (445 pages).
I did a replaceAll (replace the term OpenOffice by a long sentence) in both documents, it works correctly.
Stats for AndrewMacro document: 19 sec for loading (Hidden mode), 186 replacements in 78ms, 40 sec to store and close.
DS20014 wrote:the code after LoadComponentFromURL does not work properly.
What do you mean ?
Perhaps your code is creating the problem.
To complete my description : The dummy document with the macro code is started with a shell command in hidden mode. So even OO itselft is running in hidden mode ( so no OO is visible) . Then the code in the dummy document is executed. It opens a document (LoadComponentFromURL) with only 1(!!) page and also in hidden mode .
The opened document contains several frames with images and text.
Now the code sets an ReplaceDescriptor and runs ReplaceAll.
The target word is 4 times in the document. Only the first 2 ones are replaced.

BUT, if everything is done in visible mode (OO running visible and documents opened visible) everything works fine, all 4 target words are replaced. Therefore I'm quite sure the code is correct.

So I guess the timing in hidden mode is different and the issue lays in LoadComponentFromURL which is finish in the code, before the document is loaded completly which causes the sideeffects on the following code.
openOffice 4.1.1 on OpenSUSE 12.1
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Is there any loading status for LoadComponentFromURL

Post by RPG »

Hello
DS20014 wrote:Therefore I'm quite sure the code is correct.
I 'm not sure about this when your document is hidden.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Is there any loading status for LoadComponentFromURL

Post by B Marcelly »

I did my test from the Basic IDE window, but the documents were loaded in Hidden mode.

Running from shell OpenOffice headless is different. But I don't understand why the result would be different. And a delay of 2 seconds to avoid the problem is very strange for a one-page document.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Is there any loading status for LoadComponentFromURL

Post by RPG »

Hello

I did have test it now. When I work with thiscomponent in combination with currentcontroller when the document is hidden then I get an error. When I use the object exported from loadcomponentfromurl instead of thiscomponent then it can work. I have not use a shell command.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
DS20014
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Post by DS20014 »

RPG wrote:Hello
DS20014 wrote:Therefore I'm quite sure the code is correct.
I 'm not sure about this when your document is hidden.

Romke
So here is the code:

Code: Select all

Dim oDocument
oDocument = OpenFile("/home/....../FileToProcess.odt")

Rem wait(2000)

ReplacePlaceHolder(oDocument, "<#Placeholder#>","new Text")

Code: Select all

Function OpenFile (sPath as String) as variant

	Dim sURL as String
	sURL = ConvertToURL(sPath)


	Dim args(0) as new com.sun.star.beans.PropertyValue
	args(0).name = "Hidden"
	args(0).Value = true

	Dim oDocument as variant
	If FileExists(sURL) Then
	    oDocument = StarDesktop.loadComponentFromURL(sURL, "_blank", 0, args())
	
	    OpenFile = oDocument

end function

Code: Select all

sub ReplacePlaceholder(oDocument As variant, placeholder As String, value As String)
     
      Dim oReplace
   
      oReplace = oDocument.createReplaceDescriptor()

      oReplace.SearchCaseSensitive = True
      oReplace.SearchString = placeholder
      oReplace.ReplaceString = value

      Dim xReplaced
      xReplaced = oDocument.ReplaceAll(oReplace)

 exit sub
And as I said before, they issue only occurs, if OO is started in headless mode and the documents are opened in hidden mode.
openOffice 4.1.1 on OpenSUSE 12.1
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Is there any loading status for LoadComponentFromURL

Post by RoryOF »

Can you put in a condition to the effect that if the document is in hidden mode (etc) a time delay is inserted?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
DS20014
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Post by DS20014 »

RoryOF wrote:Can you put in a condition to the effect that if the document is in hidden mode (etc) a time delay is inserted?
Yes I could, but that's exactly what I'd like to avoid. After development is finished the code will always run in headless/hidden mode.
Add a time delay is just a work around for the problem but not a proper solution.

If LoadComponentFromURL would work properly, the code should not continue until the document has been loaded completed, but obviously this is not the case !!!

Therefore I was asking for a posibility to manually request the status of the document, so I could add a loop until the document is really ready.

Any ideas ?
openOffice 4.1.1 on OpenSUSE 12.1
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Is there any loading status for LoadComponentFromURL

Post by RoryOF »

You have observed the note on the Return value for this component?
Returns
a ::com::sun::star::lang::XComponent for successfully loaded documents or
NULL if it failed

This interface is a generic one and can be used to start further requests on loaded document or control the lifetime of it (means dispose() it after using). The real document service behind this interface can be one of follow three ones:

::com::sun::star::awt::XWindow for simple components
Should be used for viewable components only. It is not allowed to dispose it after use directly, because the frame containing the component is its owner. Because the frame object is not accessible through the interface too, only an interacting user can do this by closing the frames' window.
XController for richer components
Should be used for real editable components which doesn't need a model. It is not allowed to dispose it after use directly, because the frame containing the component is its owner. Here the object can be disposed by disposing the frame, that the XController::getFrame() method of the controller returns. But for visible components the controller should be asked for permission by calling XController::suspend() before.
XModel for full featured components
A model that in general can be shared between several view/controller pairs, does not have an explicit owner. Every view and also the component that loaded the document may consider itself as an owner. Simply calling XComponent::dispose on this model may cause problems, if some members of the "owner community" are currently busy working on the model. These problems are handled by explicit closing negotiations through the interface ::com::sun::star::util::XCloseable. Direct dispose of the model is allowed only, if this special interface doesn't exist.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Is there any loading status for LoadComponentFromURL

Post by B Marcelly »

A workaround as a solution :
- do not use -headless mode to start OpenOffice !
- at first line of your macro do this :

Code: Select all

ThisComponent.CurrentController.Frame.ContainerWindow.Visible = False
You will only see the window in a flash, because it is immediately hidden.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
Post Reply