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>
This commit is contained in:
RMZeroFour
2024-03-24 16:14:56 +05:30
committed by Hossein
parent cbf2513fac
commit 32eaef8ddb
11 changed files with 230 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ my_example_dirs_java = \
DevelopersGuide/GUI \
DevelopersGuide/OfficeBean \
DevelopersGuide/OfficeDev \
DevelopersGuide/OfficeDev/Clipboard \
DevelopersGuide/OfficeDev/Clipboard/java \
DevelopersGuide/OfficeDev/DesktopEnvironment \
DevelopersGuide/OfficeDev/DisableCommands \
DevelopersGuide/OfficeDev/FilterDevelopment/AsciiFilter \

View File

@@ -250,11 +250,15 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples,
DevelopersGuide/OfficeBean/OfficeIconColor32.jpg \
DevelopersGuide/OfficeBean/OfficeIconMono16.jpg \
DevelopersGuide/OfficeBean/OfficeIconMono32.jpg \
DevelopersGuide/OfficeDev/Clipboard/Clipboard.java \
DevelopersGuide/OfficeDev/Clipboard/ClipboardListener.java \
DevelopersGuide/OfficeDev/Clipboard/ClipboardOwner.java \
DevelopersGuide/OfficeDev/Clipboard/Makefile \
DevelopersGuide/OfficeDev/Clipboard/TextTransferable.java \
DevelopersGuide/OfficeDev/Clipboard/java/Clipboard.java \
DevelopersGuide/OfficeDev/Clipboard/java/ClipboardListener.java \
DevelopersGuide/OfficeDev/Clipboard/java/ClipboardOwner.java \
DevelopersGuide/OfficeDev/Clipboard/java/Makefile \
DevelopersGuide/OfficeDev/Clipboard/java/TextTransferable.java \
DevelopersGuide/OfficeDev/Clipboard/python/clipboard.py \
DevelopersGuide/OfficeDev/Clipboard/python/clipboard_listener.py \
DevelopersGuide/OfficeDev/Clipboard/python/clipboard_owner.py \
DevelopersGuide/OfficeDev/Clipboard/python/text_transferable.py \
DevelopersGuide/OfficeDev/ContextMenuInterceptor.java \
DevelopersGuide/OfficeDev/DesktopEnvironment/CustomizeView.java \
DevelopersGuide/OfficeDev/DesktopEnvironment/Desk.java \

View File

@@ -34,7 +34,7 @@
# Builds the OfficeDevClipboard example of the Developers Guide.
PRJ=../../../..
PRJ=../../../../..
SETTINGS=$(PRJ)/settings
include $(SETTINGS)/settings.mk

View File

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

View File

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

View File

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

View File

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