/*
 * Copyright (C) 2024 Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the AUTHORS file distributed
 * with this source distribution
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include "birbfilter.h"

enum {
    SIG_CHANGED,
    N_SIGNALS,
};
static guint signals[N_SIGNALS] = {0, };

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_TYPE(BirbFilter, birb_filter, G_TYPE_OBJECT)

static void
birb_filter_init(G_GNUC_UNUSED BirbFilter *filter) {
}

static void
birb_filter_class_init(BirbFilterClass *klass) {
    /**
     * BirbFilter::changed:
     *
     * Emitted when the filter changes in a way that filter should be performed
     * again.
     *
     * Sub classes should call [method@Filter.changed] to emit this signal.
     *
     * Since: 0.1
     */
    signals[SIG_CHANGED] = g_signal_new_class_handler(
        "changed",
        G_OBJECT_CLASS_TYPE(klass),
        G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
        NULL,
        NULL,
        NULL,
        NULL,
        G_TYPE_NONE,
        0);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
birb_filter_changed(BirbFilter *filter) {
    g_return_if_fail(BIRB_IS_FILTER(filter));

    g_signal_emit(filter, signals[SIG_CHANGED], 0);
}

gboolean
birb_filter_matches(BirbFilter *filter, GObject *item) {
    BirbFilterClass *klass = NULL;

    g_return_val_if_fail(BIRB_IS_FILTER(filter), FALSE);

    klass = BIRB_FILTER_GET_CLASS(filter);
    if(klass != NULL && klass->matches != NULL) {
        return klass->matches(filter, item);
    }

    return FALSE;
}
