Is there any loading status for LoadComponentFromURL

Creating a macro - Writing a Script - Using the API

Is there any loading status for LoadComponentFromURL

Postby DS20014 » Sat Oct 04, 2014 10:47 am

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   Expand viewCollapse view
1. ... LoadComponentFromURL
2. Code for Replacing expressions


If a wait is added after the loading of the document everything works fine.

Code: Select all   Expand viewCollapse view
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
DS20014
 
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Postby B Marcelly » Sat Oct 04, 2014 3:31 pm

Hi,
Try this :
Code: Select all   Expand viewCollapse view
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
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

Postby DS20014 » Sat Oct 04, 2014 4:09 pm

B Marcelly wrote:Hi,
Try this :
Code: Select all   Expand viewCollapse view
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
DS20014
 
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Postby B Marcelly » Sun Oct 05, 2014 8:51 am

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
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

Postby RPG » Sun Oct 05, 2014 9:47 am

Hello

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

Code: Select all   Expand viewCollapse view
print oDoc.currentcontroller.pagecount


Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2165
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Is there any loading status for LoadComponentFromURL

Postby DS20014 » Sun Oct 05, 2014 1:15 pm

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
DS20014
 
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Postby RPG » Sun Oct 05, 2014 1:26 pm

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 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2165
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Is there any loading status for LoadComponentFromURL

Postby B Marcelly » Sun Oct 05, 2014 3:51 pm

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
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

Postby RPG » Mon Oct 06, 2014 12:40 am

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 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2165
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Is there any loading status for LoadComponentFromURL

Postby DS20014 » Mon Oct 06, 2014 10:02 am

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   Expand viewCollapse view
Dim oDocument
oDocument = OpenFile("/home/....../FileToProcess.odt")

Rem wait(2000)

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



Code: Select all   Expand viewCollapse view
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   Expand viewCollapse view
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
DS20014
 
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Postby RoryOF » Mon Oct 06, 2014 10:10 am

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.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29102
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Is there any loading status for LoadComponentFromURL

Postby DS20014 » Mon Oct 06, 2014 10:43 am

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
DS20014
 
Posts: 5
Joined: Sat Oct 04, 2014 10:32 am

Re: Is there any loading status for LoadComponentFromURL

Postby RoryOF » Mon Oct 06, 2014 11:58 am

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.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29102
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Is there any loading status for LoadComponentFromURL

Postby B Marcelly » Mon Oct 06, 2014 12:38 pm

A workaround as a solution :
- do not use -headless mode to start OpenOffice !
- at first line of your macro do this :
Code: Select all   Expand viewCollapse view
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
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests