I don’t know if this example can help you, because I think it will be not so trusty and maybe some ugly problem will rise up.
For testing you need to create the Module TimeStamp in your Basic Standard library and put there the macro code.
Or you can create any module, but then change the global constant gsMacro. Next global constants are for the Backup Time, and for the string added to the name of the backup file (for example your document: mySmallDoc.odt → and the backup will be: mySmallDoc.odt.backup in the same directory).
Open your document and run the macro StartBackup and this macro sets the Listeners for the keyboard and for the mouse, and also sets the Document Event for the macro EndBackup. Every time you will press the key or click the mouse, the Listeners will call the procedure CreateCopy. And in this procedure there is condition for the time comparison - and saving the document and copy saved file as backup to the same location.
If you will close the document, the macro will remove the Listeners and the Document Event automatically. And once more the macro will save the document (because removing the Document Event did the change in the document). So if you are accustomed to the final alert “Save changes to document “blablublo” before closing?”, then don’t be surprised this alert will not show.
Maybe the better way exists with some other listener etc., but I got this gimmick.
option explicit
global const gsTime="00:10:00" 'default time for backup HH:MM:SS (it is for the function TimeValue)
global const gsMacro="Standard.TimeStamp.EndBackup" 'path to the end macro
global const gsExt=".backup" 'string added to the backup file
global goKeyHandler as object, goDocController as object, goMouseHandler as object, gTime as date, gBackupTime as date
Sub StartBackup
dim oDoc as object, s$
s=Inputbox("HH:MM:SS - the value isn't controlled in the macro!!!", "Backup Time", gsTime)
if s="" then exit sub
gTime=Now
gBackupTime=TimeValue(s) 'set your time for the backup HH:MM:SS
' gBackupTime=TimeValue(gsTime) 'default backup time
rem set the Event to the document
dim mEventProps(1) as new com.sun.star.beans.PropertyValue
mEventProps(0).Name="EventType"
mEventProps(0).Value="StarBasic"
mEventProps(1).Name="Script"
mEventProps(1).Value="macro:///" & gsMacro
ThisComponent.Events.ReplaceByName("OnPrepareViewClosing", mEventProps())
rem add the keyboard and mouse handlers
RegisterHandlers
End Sub
Sub EndBackup 'end macro
UnregisterHandlers
dim mEventProps(0) as new com.sun.star.beans.PropertyValue
mEventProps(0).Name=""
mEventProps(0).Value=""
ThisComponent.Events.ReplaceByName("OnPrepareViewClosing", mEventProps()) 'set empty document Event
SaveFile 'save the file because the document Event is changed
End Sub
Sub CreateCopy
dim s$
if Now()>gTime+gBackupTime then
SaveFile
s=ThisComponent.Location
FileCopy(s, s & gsExt) 'copy document with the extension .backup to the same location
gTime=Now
end if
End Sub
Sub SaveFile
createUnoService("com.sun.star.frame.DispatchHelper").executeDispatch(ThisComponent.CurrentController.Frame, ".uno:Save", "", 0, array()) 'save the file
End Sub
rem handlers
Sub RegisterHandlers
goDocController=ThisComponent.CurrentController
goKeyHandler=createUnoListener("Key_", "com.sun.star.awt.XKeyHandler")
goDocController.addKeyHandler(goKeyHandler)
goMouseHandler=CreateUnoListener("Mouse_","com.sun.star.awt.XMouseClickHandler")
goDocController.addMouseClickHandler(goMouseHandler)
End Sub
Sub UnregisterHandlers
if NOT IsNull(goDocController) AND NOT IsEmpty(goDocController) then
goDocController.removeKeyHandler(goKeyHandler)
goDocController.removeMouseClickHandler(goMouseHandler)
end if
End Sub
rem listeners
Function Key_keyPressed(optional oEvt) as boolean
Key_keyPressed=false
End Function
Function Key_keyReleased(optional oEvt) as boolean
CreateCopy
Key_keyReleased=false
End Function
Function Key_disposing(optional oEvt)
End Function
Function Mouse_mousePressed( oEvent as com.sun.star.awt.MouseEvent ) as boolean
Mouse_mousePressed=false
End Function
Function Mouse_mouseReleased( oEvent as com.sun.star.awt.MouseEvent ) as boolean
CreateCopy
Mouse_mouseReleased=False
End Function