/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "ftd4linux.h"
#include "ftd_app_core.h"
#include "ftd_config.h"
#include "ftd_events.h"

struct _foreach_func_data
{
    void (*cb) (const FtdEvent *, const char *, gpointer);
    gpointer data;
    const char *zoekterm;
};

static gboolean
foreach_func(gpointer key, gpointer value, gpointer data)
{
    struct _foreach_func_data *func_data = (struct _foreach_func_data *) data;

    func_data->cb(value, func_data->zoekterm, func_data->data);

    return FALSE;
}

void
ftd_watcher_foreach(const char *zoekterm, void (*cb) (const FtdEvent *, const char *, gpointer), gpointer data)
{
    FtdFilter *filter = ftd_filter_new_filter();
    struct _foreach_func_data func_data;

    func_data.cb = cb;
    func_data.data = data;
    func_data.zoekterm = zoekterm;

    filter->title = ftd_string_new(zoekterm);

    ftd_eventlist_apply_filter(core->eventlist, filter);
    ftd_eventlist_foreach(core->eventlist, TRUE, TRUE, foreach_func, &func_data);

    ftd_filter_free_filter(filter);
}

gboolean
ftd_watcher_test_event(const FtdEvent *event)
{
    FtdList *list = core->config->watcher;

    while (list) {
        FtdString *zoekterm = ftd_list_value(list);
        FtdFilter *filter = ftd_filter_new_filter();

        filter->title = ftd_string_new(ftd_string_value(zoekterm));

        if (ftd_filter_accepts(filter, event, FALSE)) {
            ftd_filter_free_filter(filter);

            return TRUE;
        }

        ftd_filter_free_filter(filter);

        list = ftd_list_next(list);
    }

    return FALSE;
}

int
ftd_watcher_get_state(void)
{
    g_assert(core);

    return core->watcher_state;
}

void
ftd_watcher_set_state(int value)
{
    g_assert(core);
    core->watcher_state = value;
}
