/* ====================================================================
 * 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 <stdio.h>
#include <stdlib.h>
#include "ftd_gui_main.h"
#include "ftd_gui_group_view.h"
#include "ftd_gui_center.h"
#include "ftd_gui_xslt.h"
#include "ftd_gui_parse.h"

void         ftd_gui_group_view_select(GtkWidget *, gpointer);
gint         ftd_gui_group_view_sortfunc(GtkTreeModel *, GtkTreeIter *, GtkTreeIter *, gpointer);

void
ftd_gui_group_view_new(void)
{
    FtdGuiGroupView *groupview = (FtdGuiGroupView *) g_malloc(sizeof(FtdGuiGroupView));
    GtkCellRenderer *cellRenderer;
    GtkTreeViewColumn *column;
    GtkTreeIter treeIter;
    int ret = -1;
    FtdList *list;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->sidebar);
    gui->worksheet->sidebar->group_view = groupview;

    groupview->widget = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_show (groupview->widget);
    //gtk_box_pack_start (GTK_BOX (gui->worksheet->sidebar->widget), groupview->widget, TRUE, TRUE, 5);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (groupview->widget),
                                    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (groupview->widget),
                                         GTK_SHADOW_IN);

    groupview->treestoreGroups = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
    groupview->treeGroups = gtk_tree_view_new_with_model (GTK_TREE_MODEL (groupview->treestoreGroups));
    gtk_widget_show (groupview->treeGroups);
    gtk_container_add (GTK_CONTAINER (groupview->widget), groupview->treeGroups);
    gtk_tree_selection_set_mode (gtk_tree_view_get_selection(GTK_TREE_VIEW(groupview->treeGroups)), GTK_SELECTION_SINGLE);

    cellRenderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes ("Nieuwsgroep", cellRenderer, "text", 0, 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 (groupview->treeGroups), column, -1);
    gtk_tree_view_column_set_sort_column_id(column, 0);

    gtk_tree_store_insert (groupview->treestoreGroups, &treeIter, NULL, ret);

    gtk_tree_store_set (groupview->treestoreGroups, &treeIter,
                        0, "* Alle nieuwsgroepen",
                        1, NULL,
                        -1);

    list = (FtdList *) ftd_groups_get();

    while (list) {
        FtdGroup *group = (FtdGroup *) ftd_list_value(list);
        char *groupname;
        //groupname = ftd_string_strip_newsgroup(group->groupName);
        groupname = ftd_string_value(group->groupName);

        gtk_tree_store_insert (groupview->treestoreGroups, &treeIter, NULL, ret);

        gtk_tree_store_set (groupview->treestoreGroups, &treeIter,
                            0, groupname,
                            1, group,
                            -1);

        //g_free(groupname);
        list = ftd_list_next(list);
    }

    gtk_tree_model_get_iter_first(gtk_tree_view_get_model(GTK_TREE_VIEW(groupview->treeGroups)), &treeIter);
    gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(groupview->treeGroups)), &treeIter);
    gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(groupview->treestoreGroups), 0, GTK_SORT_ASCENDING);
    gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(groupview->treestoreGroups), 0, ftd_gui_group_view_sortfunc, NULL, NULL);
    g_signal_connect (G_OBJECT (gtk_tree_view_get_selection(GTK_TREE_VIEW(groupview->treeGroups))), "changed",
                      G_CALLBACK (ftd_gui_group_view_select), NULL);
}

void
ftd_gui_group_view_free(void)
{
    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->sidebar);
    g_assert(gui->worksheet->sidebar->group_view);

    gtk_widget_destroy(gui->worksheet->sidebar->group_view->widget);
    g_free(gui->worksheet->sidebar->group_view);
    gui->worksheet->sidebar->group_view = NULL;
}

void
ftd_gui_group_view_select(GtkWidget *caller, gpointer data)
{
    GtkTreeIter iter;
    GtkTreeModel *model;

    if (gtk_tree_selection_get_selected (GTK_TREE_SELECTION(caller), &model, &iter)) {
        FtdGroup *group;
        FtdFilter *nieuwFilter;
        FTDEventlist *eventlist;
#ifndef NO_EXTERNAL_BROWSER
        FtdString *uitvoer;
#endif

        gtk_tree_model_get (model, &iter, 1, &group, -1);

        // Hier geen g_assert aangezien group NULL kan zijn (oftewel toon alle nieuwsgroepen)

        nieuwFilter = ftd_filter_new_filter();

        if (group) {
            nieuwFilter->groupFilterList = ftd_list_append(NULL, ftd_string_integer(group->groupId));
        }

        eventlist = ftd_eventlist_get();
        ftd_eventlist_apply_filter(eventlist, nieuwFilter);
        g_object_unref(eventlist);
        ftd_filter_free_filter(nieuwFilter);

#ifndef NO_EXTERNAL_BROWSER
        uitvoer = ftd_events_sort_filtered(ftd_gui_parse);

        if (uitvoer == NULL) {
            g_warning("ftd_gui_group_view_select(): Sorteren van lijst mislukt\n");
            return;
        }

        ftd_gui_xslt_process(uitvoer, XSLT_TYPE_LIST_TOTAL, HTML_LIST_FILE);

        ftd_string_free (uitvoer);
#endif

        // Kijk of we wel op het juiste tabblad actief zijn
        if (gtk_notebook_get_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget)) != 0) {
            gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), 0);
        }

        ftd_gui_center_eventlist_show (NULL);
    }
}

gint
ftd_gui_group_view_sortfunc(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data)
{
    FtdGroup *group1, *group2;
    gtk_tree_model_get (model, a, 1, &group1, -1);
    gtk_tree_model_get (model, b, 1, &group2, -1);

    if (group1 == NULL) {
        return -1;
    }

    if (group2 == NULL) {
        return 1;
    }

    return g_strcasecmp(ftd_string_value(group1->groupName), ftd_string_value(group2->groupName));
}
