odk/CustomTarget_build-examples_java.mk                                       
|    2 
 odk/Package_examples.mk                                                       
|   14 -
 odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/Makefile                
|    2 
 odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard.py          
|  128 ++++++++++
 odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_listener.py 
|   24 +
 odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/clipboard_owner.py    
|   27 ++
 odk/examples/DevelopersGuide/OfficeDev/Clipboard/python/text_transferable.py  
|   40 +++
 7 files changed, 230 insertions(+), 7 deletions(-)

New commits:
commit 32eaef8ddb7acbd8ab40c69acd9b8ca06a26b015
Author:     RMZeroFour <ritobrot...@gmail.com>
AuthorDate: Sun Mar 24 16:14:56 2024 +0530
Commit:     Hossein <hoss...@libreoffice.org>
CommitDate: Wed Jun 5 01:45:19 2024 +0200

    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 <hoss...@libreoffice.org>

diff --git a/odk/CustomTarget_build-examples_java.mk 
b/odk/CustomTarget_build-examples_java.mk
index 45fe7db2a0d4..cec90d4066ad 100644
--- a/odk/CustomTarget_build-examples_java.mk
+++ b/odk/CustomTarget_build-examples_java.mk
@@ -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 \
diff --git a/odk/Package_examples.mk b/odk/Package_examples.mk
index bb4aff66ec25..5588b74d7ea0 100644
--- a/odk/Package_examples.mk
+++ b/odk/Package_examples.mk
@@ -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 \
diff --git a/odk/examples/DevelopersGuide/OfficeDev/Clipboard/Clipboard.java 
b/odk/examples/DevelopersGuide/OfficeDev/Clipboard/java/Clipboard.java
similarity index 100%
rename from odk/examples/DevelopersGuide/OfficeDev/Clipboard/Clipboard.java
rename to 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
similarity index 100%
rename from 
odk/examples/DevelopersGuide/OfficeDev/Clipboard/ClipboardListener.java
rename to 
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
similarity index 100%
rename from odk/examples/DevelopersGuide/OfficeDev/Clipboard/ClipboardOwner.java
rename to 
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
similarity index 99%
rename from odk/examples/DevelopersGuide/OfficeDev/Clipboard/Makefile
rename to 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
similarity index 100%
rename from 
odk/examples/DevelopersGuide/OfficeDev/Clipboard/TextTransferable.java
rename to 
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!
The text \"Hello world!\" shall be insert at the current cursor position below.

In the second step, please select some words and put it into the clipboard! ...

Current 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

Reply via email to