from __future__ import annotations
from typing import TYPE_CHECKING
try:
# python 3.12+
from typing import override # noqa # type: ignore
except ImportError:
from typing_extensions import override # noqa # type: ignore
from com.sun.star.document import XStorageChangeListener
from ooodev.events.args.event_args import EventArgs
from ooodev.events.args.generic_args import GenericArgs
from ooodev.adapter.adapter_base import AdapterBase
if TYPE_CHECKING:
from com.sun.star.uno import XInterface
from com.sun.star.embed import XStorage
from com.sun.star.document import XStorageBasedDocument
[docs]class StorageChangeListener(AdapterBase, XStorageChangeListener):
"""
allows to be notified when a document is switched to a new storage.
See Also:
`API XStorageChangeListener <https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1document_1_1XStorageChangeListener.html>`_
"""
[docs] def __init__(
self, trigger_args: GenericArgs | None = None, subscriber: XStorageBasedDocument | None = None
) -> None:
"""
Constructor
Args:
trigger_args (GenericArgs, Optional): Args that are passed to events when they are triggered.
subscriber (XStorageBasedDocument, optional): An UNO object that implements the ``XStorageBasedDocument`` interface.
If passed in then this listener instance is automatically added to it.
"""
super().__init__(trigger_args=trigger_args)
if subscriber:
subscriber.addStorageChangeListener(self)
[docs] @override
def notifyStorageChange(self, xDocument: XInterface, xStorage: XStorage) -> None:
"""
Event is invoked when document switches to another storage.
When this method is triggered it raises the ``notifyStorageChange`` event.
The event data is a dictionary with the following keys:
- ``document``: The document that is being switched to another storage.
- ``storage``: The new storage that the document is being switched to.
"""
args = EventArgs(self)
args.event_data = {"document": xDocument, "storage": xStorage}
self._trigger_event("notifyStorageChange", args)