#!/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.
#
# Forked from Orca screen reader.
# Cthulhu project: https://git.stormux.org/storm/cthulhu

"""Custom script utilities for Mumble."""

__id__        = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2024 Stormux."
__license__   = "LGPL"

import cthulhu.scripts.toolkits.Qt.script_utilities as Qt
from cthulhu.ax_object import AXObject
from cthulhu.ax_utilities import AXUtilities


class Utilities(Qt.Utilities):
    """Mumble-specific script utilities."""

    def shouldReadFullRow(self, obj):
        if self._is_connect_server_list_cell(obj):
            return False

        return super().shouldReadFullRow(obj)

    def _is_connect_server_list_cell(self, obj):
        if not AXUtilities.is_table_cell_or_header(obj):
            return False

        dialog = AXObject.find_ancestor(obj, AXUtilities.is_dialog)
        if not dialog or AXObject.get_name(dialog) != "Mumble Server Connect":
            return False

        tree = AXObject.find_ancestor(
            obj,
            lambda x: AXUtilities.is_tree(x) and AXObject.get_name(x) == "Server list"
        )
        return tree is not None
