diff src/main/kotlin/name/blackcap/passman/Clipboard.kt @ 0:a6cfdffcaa94

Initial commit, incomplete but it runs sorta.
author David Barts <n5jrn@me.com>
date Sun, 11 Sep 2022 16:11:37 -0700
parents
children 3c792ad36b3d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/kotlin/name/blackcap/passman/Clipboard.kt	Sun Sep 11 16:11:37 2022 -0700
@@ -0,0 +1,41 @@
+package name.blackcap.passman
+
+import java.awt.Toolkit
+import java.awt.datatransfer.Clipboard
+import java.awt.datatransfer.DataFlavor
+import java.awt.datatransfer.Transferable
+import java.awt.datatransfer.UnsupportedFlavorException
+
+private val CLIPBOARD = Toolkit.getDefaultToolkit().systemClipboard
+
+private class ClipboardData(val item: String): Transferable {
+    private val FLAVORS = arrayOf<DataFlavor>(DataFlavor.stringFlavor)
+
+    override fun getTransferDataFlavors(): Array<DataFlavor> = FLAVORS
+
+    override fun isDataFlavorSupported(flavor: DataFlavor?): Boolean =
+        FLAVORS.contains(flavor)
+
+    override fun getTransferData(flavor: DataFlavor?): Any {
+        if (!isDataFlavorSupported(flavor)) {
+            throw UnsupportedFlavorException(flavor)
+        }
+        return item
+    }
+
+}
+
+private class ClipboardOwner(): java.awt.datatransfer.ClipboardOwner {
+    override fun lostOwnership(clipboard: Clipboard?, contents: Transferable?) {
+        /* we don't care */
+    }
+}
+
+/* xxx: this often makes a string out of a password */
+fun writeToClipboard(charArray: CharArray) {
+    CLIPBOARD.setContents(ClipboardData(String(charArray)), ClipboardOwner())
+}
+
+fun writeToClipboard(string: String) {
+    CLIPBOARD.setContents(ClipboardData(string), ClipboardOwner())
+}