/* ====================================================================
 * 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_gui_config.h"

void     ftd_gui_config_groupfilter_toggled(GtkWidget *, gchar *, gpointer);
void     ftd_gui_config_groupfilter_filter_alles(GtkWidget *, gpointer);
void     ftd_gui_config_groupfilter_toon_alles(GtkWidget *, gpointer);

void
ftd_gui_config_groupfilter_new(FtdGuiConfig *config)
{
    FtdGuiConfigGroupfilter *groupfilter;
    GtkWidget *vbox1;
    GtkWidget *scrolledwindow;
    GtkCellRenderer *cellRenderer;
    GtkTreeViewColumn *column;
    GtkTreeIter treeIter;
    GtkWidget *btnFilterAlles;
    GtkWidget *btnToonAlles;
    GtkWidget *hbox;
    int ret = -1;
    FtdList *list;

    g_assert(config);

    groupfilter = g_malloc(sizeof(FtdGuiConfigGroupfilter));
    config->groupfilter = groupfilter;

    groupfilter->widget = gtk_frame_new("Aangevinkte nieuwsgroepen zijn zichtbaar");
    gtk_widget_show (groupfilter->widget);

    vbox1 = gtk_vbox_new (FALSE, 0);
    gtk_container_add(GTK_CONTAINER (groupfilter->widget), vbox1);
    gtk_widget_show (vbox1);

    scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow);
    gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow, TRUE, TRUE, 5);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
                                    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow),
                                         GTK_SHADOW_IN);

    groupfilter->treestoreGroups = gtk_tree_store_new (3, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_POINTER);
    groupfilter->treeGroups = gtk_tree_view_new_with_model (GTK_TREE_MODEL (groupfilter->treestoreGroups));
    gtk_widget_show (groupfilter->treeGroups);
    gtk_container_add (GTK_CONTAINER (scrolledwindow), groupfilter->treeGroups);
    gtk_tree_selection_set_mode (gtk_tree_view_get_selection(GTK_TREE_VIEW(groupfilter->treeGroups)), GTK_SELECTION_SINGLE);

    cellRenderer = gtk_cell_renderer_toggle_new();
    column = gtk_tree_view_column_new_with_attributes ("", cellRenderer, "active", 0, NULL);
    gtk_tree_view_column_set_min_width (column, 20);
    gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
    gtk_tree_view_insert_column (GTK_TREE_VIEW (groupfilter->treeGroups), column, -1);

    g_signal_connect((gpointer) cellRenderer, "toggled",
                    G_CALLBACK(ftd_gui_config_groupfilter_toggled), config);

    cellRenderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes ("Nieuwsgroep", cellRenderer, "text", 1, NULL);
    gtk_tree_view_column_set_min_width (column, 100);
    gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
    gtk_tree_view_insert_column (GTK_TREE_VIEW (groupfilter->treeGroups), column, -1);
    gtk_tree_view_column_set_sort_column_id(column, 1);

    if (ftd_config_get()->groupFilterList) {
        ftd_group_set_group_id_list(ftd_config_get()->groupFilterList);
    }

    list = (FtdList *) ftd_groups_get();

    while (list) {
        FtdGroup *group = (FtdGroup *) ftd_list_value(list);
        gboolean value;
        gtk_tree_store_insert (groupfilter->treestoreGroups, &treeIter, NULL, ret);

        if (!ftd_config_get()->groupFilterList) {
            value = TRUE;
        } else {
            value = ftd_group_id_is_in_list(group->groupId);
        }

        gtk_tree_store_set (groupfilter->treestoreGroups, &treeIter,
                            0, value,
                            1, ftd_string_value(group->groupName),
                            2, group,
                            -1);

        list = ftd_list_next(list);
    }

    if (ftd_config_get()->groupFilterList) {
        ftd_group_unset_group_id_list();
    }

    gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(groupfilter->treestoreGroups), 1, GTK_SORT_ASCENDING);

    gtk_container_set_border_width (GTK_CONTAINER (groupfilter->widget), 5);
    gtk_container_set_border_width (GTK_CONTAINER (vbox1), 5);

    hbox = gtk_hbox_new(TRUE, 0);
    gtk_box_pack_end(GTK_BOX(vbox1), hbox, FALSE, FALSE, 5);
    gtk_widget_show(hbox);

    btnFilterAlles = gtk_button_new_with_label("Filter Alles");
    gtk_widget_show(btnFilterAlles);
    gtk_box_pack_start(GTK_BOX(hbox), btnFilterAlles, FALSE, TRUE, 5);

    g_signal_connect((gpointer)btnFilterAlles, "clicked",
                     G_CALLBACK(ftd_gui_config_groupfilter_filter_alles), config);

    btnToonAlles = gtk_button_new_with_label("Toon Alles");
    gtk_widget_show(btnToonAlles);
    gtk_box_pack_end(GTK_BOX(hbox), btnToonAlles, FALSE, TRUE, 5);

    g_signal_connect((gpointer)btnToonAlles, "clicked",
                     G_CALLBACK(ftd_gui_config_groupfilter_toon_alles), config);
}

void
ftd_gui_config_groupfilter_free(FtdGuiConfig *config)
{
    g_assert(config);

    gtk_widget_destroy(config->groupfilter->widget);
    g_free(config->groupfilter);
    config->groupfilter = NULL;
}

void
ftd_gui_config_groupfilter_toggled(GtkWidget *caller, gchar *path, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    gboolean value;
    guint column_number = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(caller), "column_number"));
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(config->groupfilter->treeGroups));

    if (gtk_tree_model_get_iter_from_string(model, &iter, path)) {
        gtk_tree_model_get(model, &iter, column_number, &value, -1);
        if (value) {
            value = FALSE;
        } else {
            value = TRUE;
        }
        gtk_tree_store_set(GTK_TREE_STORE(model), &iter, column_number, value, -1);
    }
}

void
ftd_gui_config_groupfilter_filter_alles(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(config->groupfilter->treeGroups));

    gtk_tree_model_get_iter_first(model, &iter);
    gtk_tree_store_set(GTK_TREE_STORE(model), &iter, 0, FALSE, -1);
    while (gtk_tree_model_iter_next(model, &iter)) {
        gtk_tree_store_set(GTK_TREE_STORE(model), &iter, 0, FALSE, -1);
    }
}

void
ftd_gui_config_groupfilter_toon_alles(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(config->groupfilter->treeGroups));

    gtk_tree_model_get_iter_first(model, &iter);
    gtk_tree_store_set(GTK_TREE_STORE(model), &iter, 0, TRUE, -1);
    while (gtk_tree_model_iter_next(model, &iter)) {
        gtk_tree_store_set(GTK_TREE_STORE(model), &iter, 0, TRUE, -1);
    }
}

FtdList *
ftd_gui_config_groupfilter_get_list(FtdGuiConfig *config)
{
    FtdList *list = NULL;
    FtdGroup *group;
    gboolean value;
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(config->groupfilter->treeGroups));

    gtk_tree_model_get_iter_first(model, &iter);
    gtk_tree_model_get(model, &iter, 0, &value, 2, &group, -1);
    if (value) {
        list = ftd_list_append(list, ftd_string_integer(group->groupId));
    }

    while (gtk_tree_model_iter_next(model, &iter)) {
        gtk_tree_model_get(model, &iter, 0, &value, 2, &group, -1);
        if (value) {
            list = ftd_list_append(list, ftd_string_integer(group->groupId));
        }
    }

    return list;
}
