#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#
# SPDX-FileCopyrightText: 2011 Eike Hein <hein@kde.org>


"""
Used to post the clipboard contents to the current tab in Konversation.

"""

import subprocess

try:
    import konversation.dbus
    import konversation.i18n
    konversation.i18n.init()
except ImportError:
    sys.exit("This script is intended to be run from within Konversation.")

try:
    clipboard = subprocess.check_output(('qdbus', 'org.kde.klipper', '/klipper', 'getClipboardContents'))
except subprocess.CalledProcessError:
    konversation.dbus.error(i18n("Unable to retrieve clipboard contents from Klipper."), exit=True)

# Fall back to info if there is no target (usually means we were called from a server tab).
dispatch = konversation.dbus.say if konversation.dbus.target else konversation.dbus.info
    
for line in clipboard.decode(encoding='utf-8', errors='replace').splitlines():
    dispatch(line)
