# Copyright (C) 2009-2010 Gloobus Developers
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

from gi.repository import Gio, GLib
import subprocess


DBUS_NAME = 'org.nemo.Preview'
DBUS_PATH = '/org/nemo/Preview'

DBUS_INTERFACE = f'''
<node>
  <interface name="{DBUS_NAME}">
    <method name="Activate"/>

    <method name="ShowFile">
      <arg name="uri" type="s" direction="in"/>
      <arg name="xid" type="i" direction="in"/>
      <arg name="closeIfAlreadyShown" type="b" direction="in"/>
    </method>

    <method name="Close"/>
  </interface>
</node>
'''


class DBusWrapper:
    def __init__(self):
        self.loop = GLib.MainLoop()

        node_info = Gio.DBusNodeInfo.new_for_xml(DBUS_INTERFACE)
        interface_info = node_info.interfaces[0]

        self.owner_id = Gio.bus_own_name(
            Gio.BusType.SESSION,
            DBUS_NAME,
            Gio.BusNameOwnerFlags.NONE,
            self.on_bus_acquired,
            self.on_name_acquired,
            self.on_name_lost,
        )

        self.interface_info = interface_info

        self.loop.run()

    def on_bus_acquired(self, connection, name):
        self.connection = connection

        self.registration_id = connection.register_object(
            DBUS_PATH,
            self.interface_info,
            self.on_method_call,
            None,
            None,
        )

        if self.registration_id == 0:
            raise RuntimeError('Failed to register D-Bus object')

    def on_name_acquired(self, connection, name):
        pass

    def on_name_lost(self, connection, name):
        self.loop.quit()

    def on_method_call(
        self,
        connection,
        sender,
        object_path,
        interface_name,
        method_name,
        parameters,
        invocation,
    ):
        if method_name == 'Activate':
            print('gloobus-sushi: activate')
            invocation.return_value(None)

        elif method_name == 'ShowFile':
            uri, xid, close_if_already_shown = parameters.unpack()

            print('gloobus-sushi:', uri)

            subprocess.Popen([
                'gloobus-preview',
                uri,
            ])

            invocation.return_value(None)

        elif method_name == 'Close':
            print('gloobus-sushi: quit')

            invocation.return_value(None)

            self.loop.quit()


if __name__ == '__main__':
    DBusWrapper()
