#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# clamtk, copyright (C) 2004-2024 Dave M, Tord Dellsén
#
# This file is part of clamtk
# Dave M <dave.nerd@gmail.com>
# https://github.com/dave-theunsub/clamtk
# https://github.com/dave-theunsub/clamtk-gnome
#
# clamtk is free software; you can redistribute it and/or modify it
# under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
# Foundation; either version 1, or (at your option) any later version, or
#
# b) the "Artistic License".

import locale
import subprocess
import gettext
import gi

gi.require_version("Nautilus", "4.0")

from gi.repository import Nautilus
from gi.repository import GObject

locale.setlocale(locale.LC_ALL, "")

gettext.bindtextdomain("clamtk", "/usr/share/locale")
gettext.textdomain("clamtk")
_ = gettext.gettext

class OpenTerminalExtension(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        print("Initializing clamtk-gnome")

    def _open_scanner(self, file):
        filename = file.get_location().get_path()
        # - file is of type nautiuls-vsf-file
        # https://github.com/GNOME/nautilus/blob/master/src/nautilus-file.h
        # which inherits from nautilus-file
        # https://github.com/GNOME/nautilus/blob/master/src/nautilus-vfs-file.h
        # - get_location returns a GFile
        # https://developer.gnome.org/gio/stable/GFile.html
        # which has the get_path function which returns the absolute path as a string

        cmd = ["clamtk", filename]
        subprocess.Popen(cmd)

    def menu_activate_cb(self, menu, file):
        self._open_scanner(file)

    def menu_background_activate_cb(self, menu, file):
        self._open_scanner(file)

    def get_file_items(self, *args):
        files = args[-1]
        if len(files) != 1:
            return
        file = files[0]

        item = Nautilus.MenuItem(
            name="NautilusPython::openscanner",
            label=_("Scan for threats..."),
            tip=_("Scan for threats..."),
            icon="clamtk",
        )
        # - the tooltips are not shown any longer in Nautilus
        # (the code is kept here in case this changes again for Nautilus)
        item.connect("activate", self.menu_activate_cb, file)

        return [item]

    def get_background_items(self, *args):
        file = args[-1]
        item = Nautilus.MenuItem(
            name="NautilusPython::openscanner_directory",
            label=_("Scan directory for threats..."),
            tip=_("Scan this directory for threats..."),
            icon="clamtk",
        )
        # - the tooltips are not shown any longer in Nautilus
        # (the code is kept here in case this changes again for Nautilus
        item.connect("activate", self.menu_background_activate_cb, file)

        return [item]
