#!/bin/ev python3
#
# remote gedit document emergency recovery utility
#
# Rescue an unsaved gedit document from a remote system over SSH or a
# unresponsive X11/Wayland window server session on the local system. Uses dbus
# to instruct gedit to select all text in all active windows and copy it to
# the clipboard. The clipboard is then dumped to standard output for each
# document. If your documents are in tabs rather than windows and you are
# desperate enough, uncomment tab code to blindly switch tabs.
#
# Copyright 2015, Daniel Aleksandersen
# Copyright 2015, Daniel Nebdal
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#  notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
#  notice, this list of conditions and the following disclaimer in the
#  documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import dbus
import sys
from gi.repository import Gdk, Gtk
from lxml import etree

sbus = dbus.SessionBus()

if not sbus.name_has_owner("org.gnome.gedit"):
    sys.exit("gedit is either not running or otherwise unavailable to dbus")

gedit = sbus.get_object("org.gnome.gedit", "/org/gnome/gedit/window")
gedit_iface = dbus.Interface(gedit, "org.freedesktop.DBus.Introspectable")

window_desc = etree.fromstring(gedit_iface.Introspect())
windows = [window_name.get("name") for window_name in window_desc]

if not windows:
    sys.exit("gedit has no open windows")

for window_name in sorted(windows):
    window_path = "/org/gnome/gedit/window/%s" % window_name
    gedit_window = sbus.get_object("org.gnome.gedit", window_path)
    window_iface = dbus.Interface(gedit_window, "org.gtk.Actions")
    # window_iface.Activate("next-document", "", "") # tab right
    # window_iface.Activate("previous-document", "", "") # tab left
    window_iface.Activate("select-all", "", "")
    window_iface.Activate("copy", "", "")
    clipboard = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
    document_text = clipboard.wait_for_text()
    if (document_text != None):
        print("\n\nPrinting document from window %s:\n\n" % window_name)
        print(document_text)
    else:
        print("\n\nCould not get document from %s.\n\n" % window_name)