diff options
author | RMZeroFour <ritobroto04@gmail.com> | 2024-03-24 16:14:56 +0530 |
---|---|---|
committer | Hossein <hossein@libreoffice.org> | 2024-06-05 01:45:19 +0200 |
commit | 32eaef8ddb7acbd8ab40c69acd9b8ca06a26b015 (patch) | |
tree | 8c611a2bece3aeb32781f49eabc297cdb05cab52 /odk/examples/DevelopersGuide/OfficeDev/Clipboard | |
parent | cbf2513facdf75b76cc01aafbffebb0e1c4c4c44 (diff) |
tdf#143123 Port Java clipboard examples to Python
This commit ports the Clipboard example from
odk/examples/DevelopersGuide/OfficeDev/ to Python.
The original Java example code was moved to a java/ subdirectory, and
the new Python code was added to the python/ subdirectory. Necessary
changes were made in the corresponding Makefiles.
Along with the sample code in Clipboard.java, the ClipboardListener,
ClipboardOwner and TextTransferable classes were ported.
Change-Id: Ic3a48023cfc78b770ae64adb2aee2b4d0bcefaff
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165225
Tested-by: Jenkins
Reviewed-by: Hossein <hossein@libreoffice.org>
Diffstat (limited to 'odk/examples/DevelopersGuide/OfficeDev/Clipboard')
9 files changed, 220 insertions, 1 deletions
diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/Clipboard.java b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/Clipboard.java index 742fed8ed4ba..742fed8ed4ba 100644 --- a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/Clipboard.java +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/Clipboard.java diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/ClipboardListener.java b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/ClipboardListener.java index b6af516023f9..b6af516023f9 100644 --- a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/ClipboardListener.java +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/ClipboardListener.java diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/ClipboardOwner.java b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/ClipboardOwner.java index f3b3c8087cab..f3b3c8087cab 100644 --- a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/ClipboardOwner.java +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/ClipboardOwner.java diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/Makefile b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/Makefile index 90884f2f4693..79de4b77df61 100644 --- a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/Makefile +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/Makefile @@ -34,7 +34,7 @@ # Builds the OfficeDevClipboard example of the Developers Guide. -PRJ=../../../.. +PRJ=../../../../.. SETTINGS=$(PRJ)/settings include $(SETTINGS)/settings.mk diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/TextTransferable.java b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/TextTransferable.java index 82a2b316ad84..82a2b316ad84 100644 --- a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/TextTransferable.java +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/TextTransferable.java diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard.py b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard.py new file mode 100644 index 000000000000..e506dfdacad4 --- /dev/null +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard.py @@ -0,0 +1,128 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +import time +import traceback + +import officehelper + +from clipboard_owner import ClipboardOwner +from clipboard_listener import ClipboardListener +from text_transferable import TextTransferable + +from com.sun.star.datatransfer import UnsupportedFlavorException + +# This example demonstrates the usage of the clipboard service + +def demonstrate_clipboard(): + try: + # Create a new office context (if needed), and get a reference to it + context = officehelper.bootstrap() + print("Connected to a running office ...") + + # Get a reference to the multi component factory/service manager + srv_mgr = context.getServiceManager() + + # Create a new blank document, and write user instructions to it + desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", context) + doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple()) + doc.getText().setString("In the first step, paste the current content of the clipboard in the document!\nThe text \"Hello world!\" shall be insert at the current cursor position below.\n\nIn the second step, please select some words and put it into the clipboard! ...\n\nCurrent clipboard content = ") + + # Get the current controller, and ensure that the document + # content is visible to the user by zooming in + view_settings_supplier = doc.getCurrentController() + view_settings_supplier.getViewSettings() \ + .setPropertyValue("ZoomType", 0) + + # Create an instance of systemclipboard that abstracts the + # low level system-specific clipboard + clipboard = srv_mgr.createInstance("com.sun.star.datatransfer.clipboard.SystemClipboard") + + # Create a listener for the clipboard + print("Registering a clipboard listener...") + clip_listener = ClipboardListener() + clipboard.addClipboardListener(clip_listener) + + read_clipboard(clipboard) + + # Create an owner for the clipboard, and "copy" something to it + print("Becoming a clipboard owner...") + clip_owner = ClipboardOwner() + clipboard.setContents(TextTransferable("Hello World!"), clip_owner) + + # Show a hint to the user running the example for what to do + # as an ellipses style indicator + i = 0 + while clip_owner.isClipboardOwner(): + if i != 2: + if i == 1: + print("Change clipboard ownership by putting something into the clipboard!") + print("Still clipboard owner...", end='') + else: + print("Still clipboard owner...", end='') + i += 1 + else: + print(".", end='') + time.sleep(1) + print() + + read_clipboard(clipboard) + + # End the clipboard listener + print("Unregistering a clipboard listener...") + clipboard.removeClipboardListener(clip_listener) + + # Close the temporary test document + doc.close(False) + + except Exception as e: + print(str(e)) + traceback.print_exc() + + +def read_clipboard(clipboard): + # get a list of formats currently on the clipboard + transferable = clipboard.getContents() + data_flavors_list = transferable.getTransferDataFlavors() + + # print all available formats + + print("Reading the clipboard...") + print("Available clipboard formats:", data_flavors_list) + + unicode_flavor = None + + for flavor in data_flavors_list: + print("MimeType:", flavor.MimeType, + "HumanPresentableName:", flavor.HumanPresentableName) + + # Select the flavor that supports utf-16 + # str.casefold() allows reliable case-insensitive comparision + if flavor.MimeType.casefold() == TextTransferable.UNICODE_CONTENT_TYPE.casefold(): + unicode_flavor = flavor + print() + + try: + if unicode_flavor is not None: + # Get the clipboard data as unicode + data = transferable.getTransferData(unicode_flavor) + + print("Unicode text on the clipboard ...") + print(f"Your selected text \"{data}\" is now in the clipboard.") + + except UnsupportedFlavorException as ex: + print("Requested format is not available on the clipboard!") + print(str(ex)) + traceback.print_exc() + + +if __name__ == "__main__": + demonstrate_clipboard() + +# vim: set shiftwidth=4 softtabstop=4 expandtab: diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_listener.py b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_listener.py new file mode 100644 index 000000000000..c16be51d860c --- /dev/null +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_listener.py @@ -0,0 +1,24 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import unohelper + +from com.sun.star.datatransfer.clipboard import XClipboardListener + +# A simple clipboard listener for the clipboard.py example + +class ClipboardListener(unohelper.Base, XClipboardListener): + + ''' XClipboardListener methods ''' + def disposing(self, event): + pass + + def changedContents(self, event): + print("Clipboard content has changed!") + +# vim: set shiftwidth=4 softtabstop=4 expandtab:
\ No newline at end of file diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_owner.py b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_owner.py new file mode 100644 index 000000000000..e09e28a8e9ce --- /dev/null +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_owner.py @@ -0,0 +1,27 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import unohelper + +from com.sun.star.datatransfer.clipboard import XClipboardOwner + +# A simple clipboard owner for the clipboard.py example + +class ClipboardOwner(unohelper.Base, XClipboardOwner): + def __init__(self): + self.is_owner = True + + def isClipboardOwner(self): + return self.is_owner + + ''' XClipboardOwner methods ''' + def lostOwnership(self, x_clipboard, x_transferable): + print("Lost clipboard ownership...") + self.is_owner = False + +# vim: set shiftwidth=4 softtabstop=4 expandtab:
\ No newline at end of file diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/text_transferable.py b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/text_transferable.py new file mode 100644 index 000000000000..71f7e3b428e4 --- /dev/null +++ b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/text_transferable.py @@ -0,0 +1,40 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import uno +import unohelper + +from com.sun.star.datatransfer import DataFlavor +from com.sun.star.datatransfer import UnsupportedFlavorException +from com.sun.star.datatransfer import XTransferable + +# A simple transferable for the clipboard.py example +# contains only one format, that is, unicode text + +class TextTransferable(unohelper.Base, XTransferable): + UNICODE_CONTENT_TYPE = "text/plain;charset=utf-16" + + def __init__(self, text): + self.text = text + + ''' XTransferable methods ''' + def getTransferData(self, flavor): + # str.casefold() allows reliable case-insensitive comparision + if flavor.MimeType.casefold() != __class__.UNICODE_CONTENT_TYPE.casefold(): + raise UnsupportedFlavorException() + return self.text + + def getTransferDataFlavors(self): + unicode_flavor = DataFlavor(__class__.UNICODE_CONTENT_TYPE, "Unicode Text", uno.getTypeByName("string")) + return [unicode_flavor] + + def isDataFlavorSupported(self, flavor): + # str.casefold() allows reliable case-insensitive comparision + return flavor.MimeType.casefold() == __class__.UNICODE_CONTENT_TYPE.casefold() + +# vim: set shiftwidth=4 softtabstop=4 expandtab:
\ No newline at end of file |