#!/usr/bin/env python3
#
# Copyright (c) 2024 Stormux
# Copyright (c) 2010-2012 The Orca Team
# Copyright (c) 2012 Igalia, S.L.
# Copyright (c) 2005-2010 Sun Microsystems Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.
#
# Fork of Orca Screen Reader (GNOME)
# Original source: https://gitlab.gnome.org/GNOME/orca

import gi
from gi.repository import GObject

from cthulhu import resource_manager

class SignalManager():
    def __init__(self, app):
        self.app = app
        self.resourceManager = self.app.getResourceManager()

    def registerSignal(self, signalName, signalFlag = GObject.SignalFlags.RUN_LAST, closure = GObject.TYPE_NONE, accumulator=(), contextName = None):
        # register signal
        ok = False
        if not self.signalExist(signalName):
            GObject.signal_new(signalName, self.app, signalFlag, closure,accumulator)
            ok = True
        resourceContext = self.resourceManager.getResourceContext(contextName)
        if resourceContext:
            resourceEntry = resource_manager.ResourceEntry('signal', signalName, signalName, signalName, signalName)
            resourceContext.addSignal(signalName, resourceEntry)
        return ok

    def signalExist(self, signalName):
        return GObject.signal_lookup(signalName, self.app) != 0
    def connectSignal(self, signalName, function, profile, param = None, contextName = None):
        signalID = None
        try:
            if self.signalExist(signalName):
                tryFunction = resource_manager.TryFunction(function)
                signalID = self.app.connect(signalName, tryFunction.runSignal)
                resourceContext = self.resourceManager.getResourceContext(contextName)
                if resourceContext:
                    resourceEntry = resource_manager.ResourceEntry('subscription', signalID, function, tryFunction, signalName)
                    resourceContext.addSubscription(signalName, function, resourceEntry)
            else:
                print('signal {} does not exist'.format(signalName))
        except Exception as e:
            print(e)

        return signalID
    def disconnectSignalByFunction(self, function, contextName = None):
        ok = False
        try:
            self.app.disconnect_by_func(function)
            ok = True
        except:
            pass
        resourceContext = self.resourceManager.getResourceContext(contextName)
        if resourceContext:
            resourceContext.removeSubscriptionByFunction(function)
        return ok
    def emitSignal(self, signalName):
        # emit an signal
        try:
            self.app.emit(signalName)
            print('after Emit Signal: {}'.format(signalName))
        except:
            print('Signal "{}" does not exist.'.format(signalName))
