/* ====================================================================
 * 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_center.h"
#include "ftd_gui_worksheet.h"
#include "ftd_gui_errhandler.h"
#include "ftd_gui_pixbuf.h"

static void      watcher_add_gui_func(const FtdEvent *event, const char *zoekterm, gpointer data);
static void      zoekterm_selection_changed(GtkTreeSelection *caller, gpointer data);
static void      zoekterm_toevoegen_clicked(GtkButton *caller, gpointer data);
static void      verwijder_zoekterm_clicked(GtkButton *caller, gpointer data);
static void      verwijder_alles_clicked(GtkButton *caller, gpointer data);
static gboolean  double_click_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data);

void
ftd_gui_center_watcher_new(void)
{
    GtkWidget *label;
    GtkWidget *tabby;
    FtdGuiCenterWatcher *watcher;
    int teller;
    GtkWidget *frame1;
    GtkWidget *hbox1;
    GtkWidget *scrolledwindow2;
    GtkWidget *vbuttonbox1;
    GtkWidget *button1;
    GtkWidget *label1;
    GtkWidget *hseparator1;
    GtkWidget *scrolledwindow1;
    GtkWidget *viewport1;
    GtkWidget *vbox2;
    FtdList *list;
    GtkTreeIter iter;
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    GtkTreeSelection *select;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);

    // Hebben we reeds een watcher open staan ?
    if (gui->worksheet->center->watcher) {
        // Zoek de tab waar de watchlist op vermeld staat en activeer deze
        for (teller = 0; teller < gtk_notebook_get_n_pages(GTK_NOTEBOOK (gui->worksheet->center->widget)); teller++) {
            if (gtk_notebook_get_nth_page(GTK_NOTEBOOK (gui->worksheet->center->widget), teller) == gui->worksheet->center->watcher->widget) {
                // Activeer de tab. De signal handler van de notebook zorgt ervoor
                // dat de gegevens opnieuw aangemaakt en getoond worden.
                gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), teller);

                return;
            }
        }
    }

    watcher = (FtdGuiCenterWatcher *) g_malloc0(sizeof(FtdGuiCenterWatcher));
    gui->worksheet->center->watcher = watcher;

    watcher->widget = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (watcher->widget);
    gtk_container_set_border_width (GTK_CONTAINER (watcher->widget), 4);

    frame1 = gtk_frame_new (NULL);
    gtk_widget_show (frame1);
    gtk_box_pack_start (GTK_BOX (watcher->widget), frame1, FALSE, TRUE, 4);
    gtk_container_set_border_width (GTK_CONTAINER (frame1), 4);

    hbox1 = gtk_hbox_new (FALSE, 4);
    gtk_widget_show (hbox1);
    gtk_container_add (GTK_CONTAINER (frame1), hbox1);
    gtk_container_set_border_width (GTK_CONTAINER (hbox1), 4);

    scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow2);
    gtk_box_pack_start (GTK_BOX (hbox1), scrolledwindow2, TRUE, TRUE, 0);
    GTK_WIDGET_UNSET_FLAGS (scrolledwindow2, GTK_CAN_FOCUS);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow2), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    vbuttonbox1 = gtk_vbutton_box_new ();
    gtk_widget_show (vbuttonbox1);
    gtk_box_pack_start (GTK_BOX (hbox1), vbuttonbox1, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (vbuttonbox1), 4);
    gtk_box_set_spacing (GTK_BOX (vbuttonbox1), 4);

    button1 = gtk_button_new_with_mnemonic ("Voeg nieuwe zoekterm toe");
    gtk_widget_show (button1);
    gtk_container_add (GTK_CONTAINER (vbuttonbox1), button1);
    GTK_WIDGET_UNSET_FLAGS (button1, GTK_CAN_FOCUS);
    g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK(zoekterm_toevoegen_clicked), NULL);

    watcher->btnVerwijderZoekterm = gtk_button_new_with_mnemonic ("Verwijder geselecteerde zoekterm");
    gtk_widget_show (watcher->btnVerwijderZoekterm);
    gtk_container_add (GTK_CONTAINER (vbuttonbox1), watcher->btnVerwijderZoekterm);
    GTK_WIDGET_UNSET_FLAGS (watcher->btnVerwijderZoekterm, GTK_CAN_FOCUS);
    gtk_widget_set_sensitive(watcher->btnVerwijderZoekterm, FALSE);
    g_signal_connect(G_OBJECT(watcher->btnVerwijderZoekterm), "clicked", G_CALLBACK(verwijder_zoekterm_clicked), NULL);

    watcher->btnVerwijderAlles = gtk_button_new_with_mnemonic ("Verwijder alle zoektermen");
    gtk_widget_show (watcher->btnVerwijderAlles);
    gtk_container_add (GTK_CONTAINER (vbuttonbox1), watcher->btnVerwijderAlles);
    GTK_WIDGET_UNSET_FLAGS (watcher->btnVerwijderAlles, GTK_CAN_FOCUS);
    if (ftd_config_get()->watcher == NULL) {
        gtk_widget_set_sensitive(watcher->btnVerwijderAlles, FALSE);
    }
    g_signal_connect(G_OBJECT(watcher->btnVerwijderAlles), "clicked", G_CALLBACK(verwijder_alles_clicked), NULL);

    watcher->storeZoektermen = gtk_list_store_new(1, G_TYPE_STRING);

    list = ftd_config_get()->watcher;
    while (list) {
        FtdString *zoekterm = ftd_list_value(list);

        gtk_list_store_append(watcher->storeZoektermen, &iter);
        gtk_list_store_set(watcher->storeZoektermen, &iter, 0, ftd_string_value(zoekterm), -1);

        list = ftd_list_next(list);
    }

    watcher->treeviewZoektermen = gtk_tree_view_new_with_model(GTK_TREE_MODEL(watcher->storeZoektermen));
    gtk_widget_show (watcher->treeviewZoektermen);
    gtk_container_add (GTK_CONTAINER (scrolledwindow2), watcher->treeviewZoektermen);
    GTK_WIDGET_UNSET_FLAGS (watcher->treeviewZoektermen, GTK_CAN_FOCUS);

    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Zoekterm", renderer, "text", 0, NULL);
    gtk_tree_view_column_set_min_width (column, 100);
    gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
    gtk_tree_view_column_set_resizable (column, FALSE);
    gtk_tree_view_append_column (GTK_TREE_VIEW (watcher->treeviewZoektermen), column);

    select = gtk_tree_view_get_selection(GTK_TREE_VIEW(watcher->treeviewZoektermen));
    g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(zoekterm_selection_changed), NULL);

    label1 = gtk_label_new ("Beheer OpenFTD Watcher");
    gtk_widget_show (label1);
    gtk_frame_set_label_widget (GTK_FRAME (frame1), label1);

    hseparator1 = gtk_hseparator_new ();
    gtk_widget_show (hseparator1);
    gtk_box_pack_start (GTK_BOX (watcher->widget), hseparator1, FALSE, TRUE, 4);

    scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow1);
    gtk_box_pack_start (GTK_BOX (watcher->widget), scrolledwindow1, TRUE, TRUE, 4);
    GTK_WIDGET_UNSET_FLAGS (scrolledwindow1, GTK_CAN_FOCUS);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    viewport1 = gtk_viewport_new (NULL, NULL);
    gtk_widget_show (viewport1);
    gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);

    vbox2 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox2);
    gtk_container_add (GTK_CONTAINER (viewport1), vbox2);

    list = ftd_config_get()->watcher;
    while (list) {
        FtdString *zoekterm = ftd_list_value(list);
        GtkWidget *treeview;
        GtkWidget *label2;
        GtkListStore *store;
        char *title;
        FtdGuiCenterWatcherZoekresultaat *zoekresultaat = g_malloc0(sizeof(FtdGuiCenterWatcherZoekresultaat));

        //                              postId      status           stamp          title
        store = gtk_list_store_new(8,   G_TYPE_INT, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING,
        //                              poster         nieuwsgroepen  sCat             watcherState
                                        G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF, GDK_TYPE_PIXBUF);

        watcher->zoekresultaten = ftd_list_append(watcher->zoekresultaten, zoekresultaat);

        zoekresultaat->zoekterm = zoekterm;
        zoekresultaat->widget = gtk_frame_new (NULL);
        gtk_widget_show (zoekresultaat->widget);
        gtk_box_pack_start (GTK_BOX (vbox2), zoekresultaat->widget, TRUE, TRUE, 4);
        gtk_container_set_border_width (GTK_CONTAINER (zoekresultaat->widget), 4);

        treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
        gtk_widget_show (treeview);
        gtk_container_add (GTK_CONTAINER (zoekresultaat->widget), treeview);
        gtk_container_set_border_width (GTK_CONTAINER (treeview), 4);
        GTK_WIDGET_UNSET_FLAGS (treeview, GTK_CAN_FOCUS);

        renderer = gtk_cell_renderer_pixbuf_new ();
        column = gtk_tree_view_column_new_with_attributes ("", renderer, "pixbuf", 7, NULL);
        gtk_tree_view_column_set_min_width (column, 25);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
        gtk_tree_view_column_set_resizable (column, FALSE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        renderer = gtk_cell_renderer_pixbuf_new ();
        column = gtk_tree_view_column_new_with_attributes ("S", renderer, "pixbuf", 1, NULL);
        gtk_tree_view_column_set_min_width (column, 25);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
        gtk_tree_view_column_set_resizable (column, FALSE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        renderer = gtk_cell_renderer_text_new ();
        column = gtk_tree_view_column_new_with_attributes ("Datum", renderer, "text", 2, NULL);
        gtk_tree_view_column_set_min_width (column, 100);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
        gtk_tree_view_column_set_resizable (column, FALSE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        renderer = gtk_cell_renderer_pixbuf_new ();
        column = gtk_tree_view_column_new_with_attributes ("Cat", renderer, "pixbuf", 6, NULL);
        gtk_tree_view_column_set_min_width (column, 40);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
        gtk_tree_view_column_set_resizable (column, FALSE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        renderer = gtk_cell_renderer_text_new ();
        column = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", 3, NULL);
        gtk_tree_view_column_set_min_width (column, 100);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_expand(column, TRUE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        renderer = gtk_cell_renderer_text_new ();
        column = gtk_tree_view_column_new_with_attributes ("Poster", renderer, "text", 4, NULL);
        gtk_tree_view_column_set_min_width (column, 100);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_expand(column, TRUE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        renderer = gtk_cell_renderer_text_new ();
        column = gtk_tree_view_column_new_with_attributes ("Nieuwsgroepen", renderer, "text", 5, NULL);
        gtk_tree_view_column_set_min_width (column, 100);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_expand(column, TRUE);
        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        title = g_strdup_printf("Zoekresultaten voor '%s'", ftd_string_value(zoekterm));
        label2 = gtk_label_new (title);
        g_free(title);
        gtk_widget_show (label2);
        gtk_frame_set_label_widget (GTK_FRAME (zoekresultaat->widget), label2);

        ftd_watcher_foreach(ftd_string_value(zoekterm), watcher_add_gui_func, store);

        g_signal_connect(G_OBJECT(treeview), "button-press-event", G_CALLBACK(double_click_event), NULL);

        list = ftd_list_next(list);
    }

    /* Voeg een pagina toe aan de notebook, met bijbehorend sluit-knopje */
    label = gtk_label_new ("Watcher");
    gtk_widget_show (label);

    tabby = gtk_hbox_new(FALSE, 2);
    watcher->cmdclose = gtk_button_new();
    gtk_widget_set_size_request(GTK_WIDGET(watcher->cmdclose), 16, 16);
    gtk_container_add(GTK_CONTAINER(watcher->cmdclose), gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
    gtk_widget_show_all (watcher->cmdclose);
    gtk_button_set_relief(GTK_BUTTON(watcher->cmdclose), GTK_RELIEF_NONE);

    gtk_box_pack_start(GTK_BOX(tabby), label, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(tabby), watcher->cmdclose, FALSE, FALSE, 0);

    gtk_notebook_append_page (GTK_NOTEBOOK (gui->worksheet->center->widget), watcher->widget, tabby);

    /* Signal handler voor het afsluiten knopje */
    g_signal_connect (GTK_WIDGET (watcher->cmdclose), "clicked", G_CALLBACK(ftd_gui_center_watcher_sluiten), NULL);

    /* Trek gelijk de aandacht */
    //gtk_widget_grab_focus (GTK_WIDGET (watchlist->widget));
    gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), -1);
}

void
ftd_gui_center_watcher_sluiten (GtkWidget *caller, gpointer data)
{
    FtdGuiCenterWatcher *watcher;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->watcher);
    watcher = gui->worksheet->center->watcher;

    gtk_widget_destroy(watcher->widget);

    /* Verwijder deze postinfo uit de gui */
    g_free(watcher);
    gui->worksheet->center->watcher = NULL;
}

static void
watcher_add_gui_func(const FtdEvent *event, const char *zoekterm, gpointer data)
{
    GtkListStore *store = GTK_LIST_STORE(data);
    GtkTreeIter iter;
    char *stamp;
    char *nieuwsgroepen = NULL;
    FtdList *list;
    GdkPixbuf *status_image;
    GdkPixbuf *scat_image;
    char *tmp;
    GdkPixbuf *nieuw_image = NULL;
    FtdString *title;

    stamp = ftd_string_date(event->stamp);

    list = event->groups;
    while (list) {
        FtdString *nieuwsgroep_id = ftd_list_value(list);
        FtdGroup *group = ftd_group_by_id(atoi(ftd_string_value(nieuwsgroep_id)));

        if (!group) {
            g_warning("Nieuwsgroep id %s behorende bij FTD ID %i onbekend!\n", ftd_string_value(nieuwsgroep_id), event->postId);
            list = ftd_list_next(list);
            continue;
        }

        if (nieuwsgroepen) {
            char *tmp;
            tmp = g_strdup_printf("%s, %s", nieuwsgroepen, ftd_string_value(group->groupName));
            g_free(nieuwsgroepen);
            nieuwsgroepen = tmp;
        } else {
            nieuwsgroepen = g_strdup_printf("%s", ftd_string_value(group->groupName));
        }

        list = ftd_list_next(list);
    }

    switch(event->status) {
        case FTDSTATUS_DELETED2:
        case FTDSTATUS_DELETED:
            status_image = ftd_gui_main_get_pixbuf("deleted");
            break;
        case FTDSTATUS_SPOT:
            status_image = ftd_gui_main_get_pixbuf("spot");
            break;
        case FTDSTATUS_READY:
            status_image = ftd_gui_main_get_pixbuf("ready");
            break;
        case FTDSTATUS_ANNOUNCE:
            status_image = ftd_gui_main_get_pixbuf("announce");
            break;
        case FTDSTATUS_UPLOADING:
            status_image = ftd_gui_main_get_pixbuf("uploading");
            break;
        case FTDSTATUS_UNSUPPORTED:
            status_image = ftd_gui_main_get_pixbuf("unsupported");
            break;
        default:
            break;
    }

    tmp = g_strdup_printf("ct%i%i", event->cat, event->sCat);
    scat_image = ftd_gui_main_get_pixbuf(tmp);
    g_free(tmp);

    if (event->gevonden_voor_watcher) {
        nieuw_image = ftd_gui_main_get_pixbuf("new");
    }

    title = ftd_string_replace(event->title, "&quot;", "\"");

    gtk_list_store_append(store, &iter);
    gtk_list_store_set(store, &iter, 0, event->postId, 1, status_image, 2, stamp, 3, ftd_string_value(title), 4, ftd_string_value(event->poster), 5, nieuwsgroepen, 6, scat_image, 7, nieuw_image, -1);

    g_free(stamp);
    g_free(nieuwsgroepen);
    ftd_string_free(title);
}

static void
zoekterm_selection_changed(GtkTreeSelection *caller, gpointer data)
{
    if (gtk_tree_selection_count_selected_rows(caller) == 1) {
        gtk_widget_set_sensitive(gui->worksheet->center->watcher->btnVerwijderZoekterm, TRUE);
    } else {
        gtk_widget_set_sensitive(gui->worksheet->center->watcher->btnVerwijderZoekterm, FALSE);
    }

    if (ftd_config_get()->watcher != NULL) {
        gtk_widget_set_sensitive(gui->worksheet->center->watcher->btnVerwijderAlles, TRUE);
    } else {
        gtk_widget_set_sensitive(gui->worksheet->center->watcher->btnVerwijderAlles, FALSE);
    }
}

static void
zoekterm_toevoegen_clicked(GtkButton *caller, gpointer data)
{
    GtkWidget *entry;
    GtkWidget *label;
    GtkWidget *dialog;
    FtdString *zoekterm;

    dialog = gtk_dialog_new_with_buttons ("OpenFTD",
                                          GTK_WINDOW(gui->window),
                                          GTK_DIALOG_DESTROY_WITH_PARENT,
                                          GTK_STOCK_OK,
                                          GTK_RESPONSE_OK,
                                          NULL);

    label = gtk_label_new("Voer een nieuwe zoekterm in");
    gtk_widget_show(label);
    gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);

    entry = gtk_entry_new ();
    gtk_widget_show(entry);
    gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), entry);

    gint result = gtk_dialog_run (GTK_DIALOG (dialog));
    switch (result) {
        case GTK_RESPONSE_OK:
            zoekterm = ftd_string_new(gtk_entry_get_text(GTK_ENTRY(entry)));

            // Hebben we wel een nuttige zoekterm?
            if (ftd_string_length(zoekterm) == 0) {
                gtk_widget_destroy (dialog);
                return;
            }

            ftd_config_get()->watcher = ftd_list_append(ftd_config_get()->watcher, zoekterm);

            ftd_config_save();

            ftd_gui_center_watcher_sluiten(NULL, NULL);
            ftd_gui_center_watcher_new();

            break;
        default:
            g_print("response anders\n");
            break;
    }

    gtk_widget_destroy (dialog);
}

static void
verwijder_zoekterm_clicked(GtkButton *caller, gpointer data)
{
    GtkTreeIter iter;
    GtkTreeSelection *select = gtk_tree_view_get_selection(GTK_TREE_VIEW(gui->worksheet->center->watcher->treeviewZoektermen));

    if (gtk_tree_selection_get_selected(select, NULL, &iter)) {
        char *zoekterm = NULL;
        FtdList *list = ftd_config_get()->watcher;

        gtk_tree_model_get(GTK_TREE_MODEL(gui->worksheet->center->watcher->storeZoektermen), &iter, 0, &zoekterm, -1);

        while (list) {
            int nummer;
            FtdString *zoekterm_elem = ftd_list_value(list);

            if (!strcmp(ftd_string_value(zoekterm_elem), zoekterm)) {
                // bingo!

                // Vernietig eerst het GUI gedeelte
                list = gui->worksheet->center->watcher->zoekresultaten;
                while (list) {
                    FtdGuiCenterWatcherZoekresultaat *zoekresultaat = ftd_list_value(list);

                    if (zoekterm_elem == zoekresultaat->zoekterm) {
                        gui->worksheet->center->watcher->zoekresultaten = ftd_list_remove(gui->worksheet->center->watcher->zoekresultaten, zoekresultaat);

                        gtk_widget_destroy(zoekresultaat->widget);
                        g_free(zoekresultaat);

                        break;
                    }

                    list = ftd_list_next(list);
                }

                // En tenslotte de verwijzing uit de config
                ftd_config_get()->watcher = ftd_list_remove(ftd_config_get()->watcher, zoekterm_elem);
                ftd_string_free(zoekterm_elem);

                ftd_config_save();

                gtk_list_store_remove(gui->worksheet->center->watcher->storeZoektermen, &iter);

                return;
            }

            list = ftd_list_next(list);
        }
    }
}

static void
verwijder_alles_clicked(GtkButton *caller, gpointer data)
{
    GtkTreeIter iter;
    FtdList *list;

    list = gui->worksheet->center->watcher->zoekresultaten;
    while (list) {
        FtdGuiCenterWatcherZoekresultaat *zoekresultaat = ftd_list_value(list);

        gtk_widget_destroy(zoekresultaat->widget);
        g_free(zoekresultaat);

        list = ftd_list_next(list);
    }

    ftd_list_free(gui->worksheet->center->watcher->zoekresultaten);
    gui->worksheet->center->watcher->zoekresultaten = NULL;

    list = ftd_config_get()->watcher;
    while (list) {
        FtdString *zoekterm_elem = ftd_list_value(list);

        ftd_string_free(zoekterm_elem);

        list = ftd_list_next(list);
    }

    ftd_list_free(ftd_config_get()->watcher);
    ftd_config_get()->watcher = NULL;

    gtk_list_store_clear(gui->worksheet->center->watcher->storeZoektermen);
}

static gboolean
double_click_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
    if (event->type == GDK_2BUTTON_PRESS) {
        GtkTreeIter iter;
        GtkTreeSelection *select = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));

        if (gtk_tree_selection_get_selected(select, NULL, &iter)) {
            int postId;
            char *tmp;

            gtk_tree_model_get(GTK_TREE_MODEL(gtk_tree_view_get_model(GTK_TREE_VIEW(widget))), &iter, 0, &postId, -1);

            tmp = g_strdup_printf("iets.html#post%i", postId);
            ftd_gui_center_eventlist_open_uri(NULL, tmp, NULL);
            g_free(tmp);
        }

        return TRUE;
    }

    return FALSE;
}

