Prevent closing with InstanceLocker

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Prevent closing with InstanceLocker

Bernard Marcelly
Hello,

With OpenOffice Basic, I want to prevent a document from closing.
I tried this simple code, stored in a Writer document:

Option Explicit

Global dontClose As Object

Sub denyClose
if IsNull(dontClose)  then
   dontClose =
com.sun.star.embed.InstanceLocker.InstanceLockerCtor1(ThisComponent,
com.sun.star.embed.Actions.PREVENT_CLOSE)
end if
End Sub

Sub allowClose
if not IsNull(dontClose)  then
   dontClose.dispose
   dontClose = Nothing
end if
End Sub


The code was tested on AOO 3.4.1 with Windows XP Home.
1 - I run macro denyClose. I still can close the document.
2 - I load the document again, I run macro denyClose. Then I run macro
allowClose : the document closes.

I changed the argument ThisComponent to : StarDesktop.CurrentFrame

I run macro denyClose. I can't close the document. But OpenOffice freezes and I
have to kill it.

Does anybody have a working code in Basic ?

Regards
   Bernard
Reply | Threaded
Open this post in threaded view
|

Re: Prevent closing with InstanceLocker

Ariel Constenla-Haile-2
Hi Marcel,

On Thu, Dec 27, 2012 at 08:55:59AM +0100, Bernard Marcelly wrote:

> Hello,
>
> With OpenOffice Basic, I want to prevent a document from closing.
> I tried this simple code, stored in a Writer document:
>
> Option Explicit
>
> Global dontClose As Object
>
> Sub denyClose
> if IsNull(dontClose)  then
>   dontClose =
> com.sun.star.embed.InstanceLocker.InstanceLockerCtor1(ThisComponent,
> com.sun.star.embed.Actions.PREVENT_CLOSE)
> end if
> End Sub
>
> Sub allowClose
> if not IsNull(dontClose)  then
>   dontClose.dispose
>   dontClose = Nothing
> end if
> End Sub
>
>
> The code was tested on AOO 3.4.1 with Windows XP Home.
> 1 - I run macro denyClose. I still can close the document.
There is a bug in the implementation: http://s.apache.org/FC9 circa line
353 a CloseVetoException is only thrown *only* when there is an
XActionsApproval instance and it approves preventing close.

This is obviously wrong, as it makes useless the first constructor (the
one without an XActionsApproval instance). Also, the second constructor
is said to admit an empty reference for the XActionsApproval, in which
case it should behave like the first constructor.

Does the InstanceLocker work if you provide an XActionsApproval (in
Basic, via a UNO listener)? In a dummy test, AOO crashed.

> 2 - I load the document again, I run macro denyClose. Then I run
> macro allowClose : the document closes.
>
> I changed the argument ThisComponent to : StarDesktop.CurrentFrame
>
> I run macro denyClose. I can't close the document. But OpenOffice
> freezes and I have to kill it.

I can reproduce it when there is only one frame in the desktop, the one
with the document that is running the macro. There seems to be a dead
lock, according to the backtrace.

Another thing that does not work is that disposing the InstanceLocker
that is vetoing the closing of the frame, the frame gets closed even if
I don't close it manually. I understood the specification as saying that
disposing the InstanceLocker will remove the close listener, not close
the instance. How do you interpret this?


Regards
--
Ariel Constenla-Haile
La Plata, Argentina

attachment0 (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Prevent closing with InstanceLocker

Bernard Marcelly
> Does the InstanceLocker work if you provide an XActionsApproval (in
> Basic, via a UNO listener)? In a dummy test, AOO crashed.

Indeed, it works for me if there is another OpenOffice window in the background.
But running my routine allowClose still closes the window.

If the document is the unique window, the closing attempt still freezes OpenOffice.
Here is my new code.

Option Explicit

Global dontClose As Object, closeDenier As Object

Sub denyClose
if IsNull(dontClose)  then
   closeDenier = CreateUnoListener("nono_", "com.sun.star.embed.XActionsApproval")
   dontClose =
com.sun.star.embed.InstanceLocker.InstanceLockerCtor2(StarDesktop.CurrentFrame,
com.sun.star.embed.Actions.PREVENT_CLOSE, closeDenier)
end if
End Sub


Sub allowClose
if not IsNull(dontClose)  then
   dontClose.dispose
   closeDenier = Nothing
   dontClose = Nothing
end if
End Sub


function nono_approveAction(nAction As Long) As Boolean
nono_approveAction = True
end function


> Another thing that does not work is that disposing the InstanceLocker
> that is vetoing the closing of the frame, the frame gets closed even if
> I don't close it manually. I understood the specification as saying that
> disposing the InstanceLocker will remove the close listener, not close
> the instance. How do you interpret this?
>

"After creation the service adds a listener of requested type ( close, terminate
and/or etc. ) to the controlled object and let the listener throw related veto
exception until the service is disposed."

I have the same interpretation. After disposal of the service the programmer
should be free to let the window (or the document) open or close it.

Useful references from old mailing lists:
   Annoucement of service InstanceLocker (May 2006)
<http://www.mail-archive.com/allfeatures@.../msg00282.html>
   Problem with InstanceLocker in Basic (Oct 2007)
<http://www.mail-archive.com/dev@.../msg06210.html>

Regards
   Bernard


Reply | Threaded
Open this post in threaded view
|

Re: Prevent closing with InstanceLocker

Ariel Constenla-Haile-2
On Thu, Dec 27, 2012 at 04:50:13PM +0100, Bernard Marcelly wrote:
> Useful references from old mailing lists:
>   Annoucement of service InstanceLocker (May 2006)
> <http://www.mail-archive.com/allfeatures@.../msg00282.html>
>   Problem with InstanceLocker in Basic (Oct 2007)
> <http://www.mail-archive.com/dev@.../msg06210.html>

Useful too a quick search in bugzilla shows:
https://issues.apache.org/ooo/buglist.cgi?quicksearch=InstanceLocker

https://issues.apache.org/ooo/show_bug.cgi?id=118965
https://issues.apache.org/ooo/show_bug.cgi?id=118966
https://issues.apache.org/ooo/show_bug.cgi?id=118896

Looks like this service didn't get enough qa and it's really broken.


Regard
--
Ariel Constenla-Haile
La Plata, Argentina

attachment0 (853 bytes) Download Attachment