/* ====================================================================
 * 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_message.h"
#include "ftd_gui_mail.h"
#include "ftd_gui_center_eventlist.h"
#include "ftd_gui_errhandler.h"

#ifdef HAVE_GTKSPELL
#include <gtkspell/gtkspell.h>
#endif


static gboolean  ftd_gui_message_callback_cmdBold(GtkWidget *, gpointer);
static gboolean  ftd_gui_message_callback_cmdItalic(GtkWidget *, gpointer);
static gboolean  ftd_gui_message_callback_cmdUnderline(GtkWidget *, gpointer);
static gboolean  ftd_gui_message_callback_cmdQuote(GtkWidget *, gpointer);
static gboolean  ftd_gui_message_callback_cmdFTD(GtkWidget *, gpointer);
static gboolean  ftd_gui_message_callback_cmdVerzend(GtkWidget *, gpointer);
static gboolean  ftd_gui_message_callback_cmdCancel(GtkWidget *, gpointer);

static gboolean  ftd_gui_message_callback_cmdSmileBiggrin(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileConfused(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileClown(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileCool(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileBuigen(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileCensored(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileBloos(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileGrijns(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTDAfgerond(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTDAangekondigd(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTDGereed(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTDSpot(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTDUploaden(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTDVerwijderd(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileFTD(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileMaf(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileOgen(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileOops(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileLollig(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileHuh(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileKnipoog(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileHeh(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileRespekt(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileKlappen(GtkWidget *caller, gpointer data);
static gboolean  ftd_gui_message_callback_cmdSmileVlag(GtkWidget *caller, gpointer data);

FtdGuiMessage*
ftd_gui_message_new (const char *geadresseerde, const char *subject, const char *contents, int ftd_postid, FtdEventForumReactie *forum_reactie)
{
    FtdGuiMessage *message = (FtdGuiMessage *) g_malloc(sizeof(FtdGuiMessage));
    FtdGuiMessageData *message_data = (FtdGuiMessageData *) g_malloc(sizeof(FtdGuiMessageData));

    GtkWidget *vbox1;
    GtkWidget *hbox2;
    GtkWidget *label2;
    GtkWidget *label3;
    GtkWidget *scrolledwindow2;
    GtkWidget *viewport1;
    GtkWidget *vpaned1;
    GtkWidget *vpaned2;
    GtkWidget *scrolledwindow5;
    GtkWidget *hbox1;
    GtkWidget *table1;
    GtkWidget *cmdBold;
    GtkWidget *cmdItalic;
    GtkWidget *cmdUnderline;
    GtkWidget *cmdQuote;
#if 0
    GtkWidget *cmdFTD;
#endif
    GtkWidget *table2;
    GtkWidget *vbox2;
//    GtkWidget *chkVoorbeeld;
    GtkWidget *cmdCancel;
    GError *err = NULL;

    message_data->message = message;

    message->postid = ftd_postid;

    message->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (message->window), "Voeg een bericht toe");
    gtk_window_set_position (GTK_WINDOW (message->window), GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_set_modal(GTK_WINDOW (message->window), TRUE);
    gtk_widget_set_size_request (message->window, 620, 440);
    if (ftd_postid == 0 && gui->mail) {
        gtk_window_set_transient_for(GTK_WINDOW(message->window), GTK_WINDOW(gui->mail->window));
    } else {
        gtk_window_set_transient_for(GTK_WINDOW(message->window), GTK_WINDOW(gui->window));
    }

    vbox1 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox1);
    gtk_container_add (GTK_CONTAINER (message->window), vbox1);

    hbox2 = gtk_hbox_new (FALSE, 0);
    if (ftd_postid == 0) {
        gtk_widget_show (hbox2);
    }
    gtk_box_pack_start (GTK_BOX (vbox1), hbox2, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (hbox2), 2);

    label2 = gtk_label_new ("Aan :");
    gtk_widget_show (label2);
    gtk_box_pack_start (GTK_BOX (hbox2), label2, FALSE, FALSE, 0);
    gtk_misc_set_padding (GTK_MISC (label2), 4, 10);

    message->txtAan = gtk_entry_new ();
    gtk_widget_show (message->txtAan);
    gtk_box_pack_start (GTK_BOX (hbox2), message->txtAan, TRUE, TRUE, 0);
    if (geadresseerde) {
        if (strlen(geadresseerde) > 0) {
            gtk_entry_set_text(GTK_ENTRY(message->txtAan), geadresseerde);
            gtk_widget_set_sensitive(message->txtAan, FALSE);
        }
    }

    label3 = gtk_label_new ("Onderwerp :");
    gtk_widget_show (label3);
    gtk_box_pack_start (GTK_BOX (hbox2), label3, FALSE, FALSE, 0);
    gtk_misc_set_padding (GTK_MISC (label3), 4, 10);

    message->txtSubject = gtk_entry_new ();
    gtk_widget_show (message->txtSubject);
    gtk_box_pack_start (GTK_BOX (hbox2), message->txtSubject, TRUE, TRUE, 0);
    if (subject) {
        if (strlen(subject) > 0) {
            gtk_entry_set_text(GTK_ENTRY(message->txtSubject), subject);
        }
    }

    scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow2);
    gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow2, TRUE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (scrolledwindow2), 2);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow2), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

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

    vpaned1 = gtk_vpaned_new ();
    gtk_widget_show (vpaned1);
    gtk_container_add (GTK_CONTAINER (viewport1), vpaned1);

    gtk_paned_set_position (GTK_PANED (vpaned1), 130);

/*
    scrolledwindow4 = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow4);
    gtk_paned_pack1 (GTK_PANED (vpaned1), scrolledwindow4, FALSE, TRUE);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow4), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

    message->txtQuote = gtk_text_view_new ();
    gtk_widget_show (message->txtQuote);
    gtk_container_add (GTK_CONTAINER (scrolledwindow4), message->txtQuote);
*/

    vpaned2 = gtk_vpaned_new ();
    gtk_widget_show (vpaned2);
    gtk_paned_pack2 (GTK_PANED (vpaned1), vpaned2, TRUE, TRUE);

    scrolledwindow5 = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_show (scrolledwindow5);
    gtk_paned_pack1 (GTK_PANED (vpaned2), scrolledwindow5, FALSE, TRUE);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow5), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);

    message->txtBericht = gtk_text_view_new ();
#ifdef HAVE_GTKSPELL
    if (!gtkspell_new_attach(GTK_TEXT_VIEW(message->txtBericht), "nl", &err)) {
        char *msg = g_strdup_printf("Initialiseren van de spellingscontrole mislukt:\n%s\nSpellingscontrole wordt uitgeschakeld", err->message);
        ftd_gui_dialog_show(NULL, msg, gui->window, FALSE);
        g_error_free(err);
        g_free(msg);
    }
#endif
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(message->txtBericht), GTK_WRAP_WORD);
    gtk_widget_show (message->txtBericht);
    gtk_container_add (GTK_CONTAINER (scrolledwindow5), message->txtBericht);

    if (contents && strlen(contents) > 0) {
        FtdString *src = ftd_string_new(contents);
        FtdString *new = ftd_string_replace(src, "<br />", "");
        GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(message->txtBericht));
        gtk_text_buffer_set_text(buf, ftd_string_value(new), -1);
        ftd_string_free(new);
        ftd_string_free(src);
    }

    if (forum_reactie) {
        GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(message->txtBericht));
        FtdString *new = ftd_string_replace(forum_reactie->body, "<br />", "\n");
        char *msg = g_strdup_printf("[quote=%s]%s[/quote]", ftd_string_value(forum_reactie->author), ftd_string_value(new));
        gtk_text_buffer_set_text(buf, msg, -1);
        ftd_string_free(new);
        g_free(msg);
    }

    if (geadresseerde && subject) {
        if (strlen(geadresseerde) > 0 && strlen(subject) > 0) {
            gtk_widget_grab_focus(message->txtBericht);
        }
    }

#if 0
#ifndef HAVE_LIBGTKHTML
    message->preview = gtk_moz_embed_new();
    //gtk_moz_embed_load_url(GTK_MOZ_EMBED(message->preview), PACKAGE_DATA_DIR "/" PACKAGE "/html/selecteer_mail.html");
    gtk_paned_pack2 (GTK_PANED (vpaned2), message->preview, TRUE, TRUE);
    gtk_widget_show (message->preview);
#endif
#endif

    hbox1 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox1);
    gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (hbox1), 3);

    table1 = gtk_table_new (2, 2, FALSE);
    gtk_widget_show (table1);
    gtk_box_pack_start (GTK_BOX (hbox1), table1, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (table1), 2);

    cmdBold = gtk_button_new_with_mnemonic ("B");
    gtk_widget_show (cmdBold);
    gtk_table_attach (GTK_TABLE (table1), cmdBold, 0, 1, 0, 1,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);
    gtk_widget_set_size_request (cmdBold, 45, 28);

    cmdItalic = gtk_button_new_with_mnemonic ("i");
    gtk_widget_show (cmdItalic);
    gtk_table_attach (GTK_TABLE (table1), cmdItalic, 1, 2, 0, 1,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);
    gtk_widget_set_size_request (cmdItalic, 45, 28);

    cmdUnderline = gtk_button_new_with_mnemonic ("U");
    gtk_widget_show (cmdUnderline);
    gtk_table_attach (GTK_TABLE (table1), cmdUnderline, 0, 1, 1, 2,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);
    gtk_widget_set_size_request (cmdUnderline, 45, 28);

    cmdQuote = gtk_button_new_with_mnemonic ("[..]");
    gtk_widget_show (cmdQuote);
    gtk_table_attach (GTK_TABLE (table1), cmdQuote, 1, 2, 1, 2,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);
    gtk_widget_set_size_request (cmdQuote, 45, 28);

#if 0
    cmdFTD = gtk_button_new_with_mnemonic ("[ftd]...[/ftd]");
    gtk_widget_show (cmdFTD);
    gtk_box_pack_start (GTK_BOX (hbox1), cmdFTD, FALSE, FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (cmdFTD), 2);
#endif

    table2 = gtk_table_new (2, 7, FALSE);
    gtk_widget_show (table2);
    gtk_box_pack_start (GTK_BOX (hbox1), table2, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (table2), 2);

    ftd_gui_message_verwerk_smilies (table2, message);

    vbox2 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox2);
    gtk_box_pack_start (GTK_BOX (hbox1), vbox2, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (vbox2), 2);

    //chkVoorbeeld = gtk_check_button_new_with_mnemonic ("Voorbeeld");
    //gtk_widget_show (chkVoorbeeld);
    //gtk_box_pack_start (GTK_BOX (vbox2), chkVoorbeeld, FALSE, FALSE, 0);

    message->btnVerzend = gtk_button_new_with_mnemonic ("OK");
    gtk_widget_show (message->btnVerzend);
    gtk_box_pack_start (GTK_BOX (vbox2), message->btnVerzend, FALSE, FALSE, 0);
    gtk_widget_set_size_request (message->btnVerzend, -1, 28);

    cmdCancel = gtk_button_new_with_mnemonic("Annuleren");
    gtk_widget_show(cmdCancel);
    gtk_box_pack_start (GTK_BOX (vbox2), cmdCancel, FALSE, FALSE, 0);
    gtk_widget_set_size_request (cmdCancel, -1, 28);

    message->statusbar = gtk_statusbar_new ();
    gtk_widget_show (message->statusbar);
    gtk_box_pack_start (GTK_BOX (vbox1), message->statusbar, FALSE, FALSE, 0);

    /* Signal handlers */
    g_signal_connect (G_OBJECT (cmdBold), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdBold), message);
    g_signal_connect (G_OBJECT (cmdItalic), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdItalic), message);
    g_signal_connect (G_OBJECT (cmdUnderline), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdUnderline), message);
    g_signal_connect (G_OBJECT (cmdQuote), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdQuote), message);
#if 0
    g_signal_connect (G_OBJECT (cmdFTD), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdFTD), message);
#endif

    //g_signal_connect (G_OBJECT (chkVoorbeeld), "toggled",
    //                  G_CALLBACK (ftd_gui_message_callback_chkVoorbeeld), message);

    g_signal_connect (G_OBJECT (message->btnVerzend), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdVerzend), message_data);
    g_signal_connect (G_OBJECT (cmdCancel), "clicked",
                      G_CALLBACK (ftd_gui_message_callback_cmdCancel), message->window);

    /* Voor het mozilla componentje hebben we een speciale behandeling
     * deze moet namelijk eerst getoond worden met gtk_widget_show omdat
     * deze anders het programma laat crashen als dit venster gesloten wordt
     * Omdat we standaard het mozilla componentje niet hoeven te tonen (alleen
     * als de optie 'Voorbeeld' is aangevinkt) laten we na het realize
     * signaal dit componentje onzichtbaar maken */
#ifndef HAVE_LIBGTKHTML
#if 0
    g_signal_connect_after (GTK_WIDGET (message->preview), "realize", G_CALLBACK(ftd_gui_message_preview_realized), NULL);
#endif
#endif

    gtk_widget_show (message->window);

    return message;
}

void
ftd_gui_message_preview_realized (GtkWidget *caller, gpointer data)
{
    gtk_widget_hide(caller);
}

static GtkWidget *
create_smiley_button(const char *smiley_filename, gboolean small_image, gboolean (*callback) (GtkWidget *, gpointer), gpointer data)
{
    GtkWidget *image;
    GtkWidget *button;
    char *volledigeFilename;

    button = gtk_button_new();
    gtk_widget_show (button);

    volledigeFilename = ftd_file_parse_data_dir(smiley_filename);
    image = gtk_image_new_from_file (volledigeFilename);
    g_free(volledigeFilename);

    gtk_widget_show (image);
    gtk_container_add (GTK_CONTAINER (button), image);
    if (small_image) {
        gtk_widget_set_size_request (button, 60, 30);
    } else {
        gtk_widget_set_size_request (button, 60, 45);
    }

    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback), data);

    return button;
}

void
ftd_gui_message_verwerk_smilies(GtkWidget *table2, FtdGuiMessage *message)
{
    GtkWidget *button;
    GtkWidget *buttonbox;

    button = create_smiley_button("/emoticons/biggrin.gif", TRUE, ftd_gui_message_callback_cmdSmileBiggrin, message);
    gtk_table_attach (GTK_TABLE (table2), button, 0, 1, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/bloos.gif", TRUE, ftd_gui_message_callback_cmdSmileBloos, message);
    gtk_table_attach (GTK_TABLE (table2), button, 1, 2, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/clown.gif", TRUE, ftd_gui_message_callback_cmdSmileClown, message);
    gtk_table_attach (GTK_TABLE (table2), button, 2, 3, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/confused.gif", TRUE, ftd_gui_message_callback_cmdSmileConfused, message);
    gtk_table_attach (GTK_TABLE (table2), button, 3, 4, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/cool.gif", TRUE, ftd_gui_message_callback_cmdSmileCool, message);
    gtk_table_attach (GTK_TABLE (table2), button, 4, 5, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/grijns.gif", TRUE, ftd_gui_message_callback_cmdSmileGrijns, message);
    gtk_table_attach (GTK_TABLE (table2), button, 5, 6, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd_aangekondigd.gif", TRUE,  ftd_gui_message_callback_cmdSmileFTDAangekondigd, message);
    gtk_table_attach (GTK_TABLE (table2), button, 0, 1, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd_gereed.gif", TRUE, ftd_gui_message_callback_cmdSmileFTDGereed, message);
    gtk_table_attach (GTK_TABLE (table2), button, 1, 2, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd_spot.gif", TRUE, ftd_gui_message_callback_cmdSmileFTDSpot, message);
    gtk_table_attach (GTK_TABLE (table2), button, 2, 3, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd_uploaden.gif", TRUE, ftd_gui_message_callback_cmdSmileFTDUploaden, message);
    gtk_table_attach (GTK_TABLE (table2), button, 3, 4, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd_verwijderd.gif", TRUE, ftd_gui_message_callback_cmdSmileFTDVerwijderd, message);
    gtk_table_attach (GTK_TABLE (table2), button, 4, 5, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd_afgerond.gif", TRUE,  ftd_gui_message_callback_cmdSmileFTDAfgerond, message);
    gtk_table_attach (GTK_TABLE (table2), button, 5, 6, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/maf.gif", TRUE, ftd_gui_message_callback_cmdSmileMaf, message);
    gtk_table_attach (GTK_TABLE (table2), button, 0, 1, 2, 3,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ogen.gif", TRUE, ftd_gui_message_callback_cmdSmileOgen, message);
    gtk_table_attach (GTK_TABLE (table2), button, 1, 2, 2, 3,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/lollig.gif", TRUE, ftd_gui_message_callback_cmdSmileLollig, message);
    gtk_table_attach (GTK_TABLE (table2), button, 2, 3, 2, 3,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/huh.gif", TRUE, ftd_gui_message_callback_cmdSmileHuh, message);
    gtk_table_attach (GTK_TABLE (table2), button, 3, 4, 2, 3,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/knipoog.gif", TRUE, ftd_gui_message_callback_cmdSmileKnipoog, message);
    gtk_table_attach (GTK_TABLE (table2), button, 4, 5, 2, 3,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/heh.gif", TRUE, ftd_gui_message_callback_cmdSmileHeh, message);
    gtk_table_attach (GTK_TABLE (table2), button, 5, 6, 2, 3,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/respekt.gif", FALSE, ftd_gui_message_callback_cmdSmileRespekt, message);
    gtk_table_attach (GTK_TABLE (table2), button, 0, 1, 3, 4,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/klappen.gif", FALSE, ftd_gui_message_callback_cmdSmileKlappen, message);
    gtk_table_attach (GTK_TABLE (table2), button, 1, 2, 3, 4,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/vlag.gif", FALSE, ftd_gui_message_callback_cmdSmileVlag, message);
    gtk_table_attach (GTK_TABLE (table2), button, 2, 3, 3, 4,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/buigen.gif", FALSE, ftd_gui_message_callback_cmdSmileBuigen, message);
    gtk_table_attach (GTK_TABLE (table2), button, 3, 4, 3, 4,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/censored.gif", FALSE, ftd_gui_message_callback_cmdSmileCensored, message);
    gtk_table_attach (GTK_TABLE (table2), button, 4, 5, 3, 4,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/ftd.gif", TRUE, ftd_gui_message_callback_cmdSmileFTD, message);
    gtk_table_attach (GTK_TABLE (table2), button, 6, 7, 1, 2,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);

    button = create_smiley_button("/emoticons/oops.gif", TRUE, ftd_gui_message_callback_cmdSmileOops, message);
    gtk_table_attach (GTK_TABLE (table2), button, 6, 7, 0, 1,
                      (GtkAttachOptions) (0),
                      (GtkAttachOptions) (0), 0, 0);
}

void
ftd_gui_message_callback_chkVoorbeeld(GtkWidget *caller, gpointer data)
{

}

void
ftd_gui_message_insert_text (FtdGuiMessage *message, char *beginTag, char *endTag)
{
    GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (message->txtBericht));

    if (endTag == NULL) {
        gtk_text_buffer_insert_at_cursor (buffer, beginTag, (gint) strlen(beginTag));
    } else {
//        GtkTextIter *beginPos, *eindPos;
        gboolean retval = TRUE; // = gtk_text_buffer_get_selection_bounds (buffer, beginPos, eindPos);

        if (retval != TRUE) { /* Hebben we wel een selectie ? */
            /* Geen selectie, return */
            return;
        }

/* FIXME: Iemand een idee hoe we hier tekst kunnen toevoegen aan het begin en einde van een selectie ?
        gtk_text_buffer_insert_text (buffer, beginTag, strlen(beginTag), beginPos);
        eindPos += strlen(beginTag);
        gtk_text_buffer_insert_text (buffer, endTag, strlen(endTag), eindPos);
*/
    }
}

gboolean
ftd_gui_message_callback_cmdBold(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, "[b]", "[/b]");

    return FALSE;
}

gboolean
ftd_gui_message_callback_cmdItalic(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, "[i]", "[/i]");

    return FALSE;
}

gboolean
ftd_gui_message_callback_cmdUnderline(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, "[u]", "[/u]");

    return FALSE;
}

gboolean
ftd_gui_message_callback_cmdQuote(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, "[quote]", "[/quote]");

    return FALSE;
}

gboolean
ftd_gui_message_callback_cmdFTD(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, "[ftd]", "[/ftd]");

    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileBiggrin(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=biggrin] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileConfused(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=confused] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileClown(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=clown] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileCool(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=cool] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileBuigen(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=buigen] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileCensored(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=censored] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileBloos(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=bloos] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileGrijns(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=grijns] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTDAfgerond(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd_afgerond] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTDAangekondigd(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd_aangekondigd] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTDGereed(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd_gereed] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTDSpot(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd_spot] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTDUploaden(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd_uploaden] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTDVerwijderd(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd_verwijderd] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileFTD(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ftd] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileMaf(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=maf] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileOgen(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=ogen] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileOops(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=oops] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileLollig(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=lollig] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileHuh(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=huh] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileKnipoog(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=knipoog] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileHeh(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=heh] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileRespekt(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=respekt] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileKlappen(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=klappen] ", NULL);
    return FALSE;
}

static gboolean
ftd_gui_message_callback_cmdSmileVlag(GtkWidget *caller, gpointer data)
{
    FtdGuiMessage *message = (FtdGuiMessage *)data;
    g_assert(message);

    ftd_gui_message_insert_text (message, " [img=vlag] ", NULL);
    return FALSE;
}

gboolean
ftd_gui_message_callback_cmdVerzend(GtkWidget *caller, gpointer data)
{
    FtdGuiMessageData *message_data = (FtdGuiMessageData *) data;
    GtkTextIter start, end;
    GtkTextBuffer *buffer;
    gchar *tekst;
    FtdErrorCode retval;

    g_assert(message_data);

    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(message_data->message->txtBericht));
    gtk_text_buffer_get_start_iter(buffer, &start);
    gtk_text_buffer_get_end_iter(buffer, &end);
    tekst = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);

    if (message_data->message->postid == 0) {
        if (!strcmp(gtk_entry_get_text(GTK_ENTRY(message_data->message->txtAan)), "")) {
            ftd_gui_dialog_show(NULL, "Er is geen geadresseerde ingevuld.", message_data->message->window, FALSE);
            return FALSE;
        }

        if (!strcmp(gtk_entry_get_text(GTK_ENTRY(message_data->message->txtSubject)), "")) {
            ftd_gui_dialog_show(NULL, "Er is geen onderwerp ingevuld.", message_data->message->window, FALSE);
            return FALSE;
        }
    }

    if (!strcmp(tekst, "")) {
        ftd_gui_dialog_show(NULL, "Er is geen bericht opgegeven dat verzonden moet worden", message_data->message->window, FALSE);
        return FALSE;
    }

    gtk_window_set_modal(GTK_WINDOW(message_data->message->window), FALSE);
    gtk_widget_set_sensitive(message_data->message->window, FALSE);

    if (message_data->message->postid == 0) {
        char *date;
        time_t stamp;
        char tmp[1024];
        char *errmsg;

        // We hebben een FTD Mail bericht
        message_data->bericht = (FtdMailBericht *) g_malloc(sizeof(FtdMailBericht));
        // FIXME: tekst kan UTF8 code bevatten
        message_data->bericht->from = ftd_string_new(gtk_entry_get_text(GTK_ENTRY(message_data->message->txtAan)));
        message_data->bericht->subject = ftd_string_new(gtk_entry_get_text(GTK_ENTRY(message_data->message->txtSubject)));
        message_data->bericht->body = ftd_string_new(tekst);

        retval = ftd_mail_post(message_data->bericht, &errmsg);

        stamp = time(NULL);
        date = ftd_string_date(stamp);

        if (retval > 0 || retval == -1) {
            if (retval > 0) {
                ftd_gui_errhandler(gui->mail->window, FTD_ONDERDEEL_POSTMAIL, retval, errmsg);
            } else {
                ftd_gui_dialog_show(NULL, "Versturen van FTD Mail bericht mislukt", message_data->message->window, TRUE);
            }
            snprintf(tmp, sizeof(tmp), "%s: Versturen van FTD Mail bericht mislukt!", date);
            g_free(date);
            if (gui->mail) {
                gtk_statusbar_push(GTK_STATUSBAR(gui->mail->statusbar), 0, tmp);
            }
            gtk_widget_set_sensitive(message_data->message->window, TRUE);
            gtk_window_set_modal(GTK_WINDOW(message_data->message->window), TRUE);
            return FALSE;
        }

        gtk_widget_destroy(message_data->message->window);
        g_free(message_data);
        snprintf(tmp, sizeof(tmp), "%s: Versturen van FTD Mail bericht geslaagd - %i nieuw(e) bericht(en); %i berichten totaal", date, -retval, ftd_mail_get_num_messages());
        g_free(date);
        if (gui->mail) {
            gtk_statusbar_push(GTK_STATUSBAR(gui->mail->statusbar), 0, tmp);
            ftd_gui_mail_treeVerzonden_update(NULL);
        }
    } else {
        char tmp[64];
        char *errmsg;

        // We hebben een FTD post reactie
        message_data->bericht = NULL;

        retval = ftd_events_forum_post(tekst, message_data->message->postid, message_data, &errmsg);

        if (retval != ERROR_NONE) {
            ftd_gui_errhandler(gui->window, FTD_ONDERDEEL_EVENTINFO_FORUM, retval, errmsg);
            gtk_widget_set_sensitive(message_data->message->window, TRUE);
            return FALSE;
        }

        /* De event info moet nu opnieuw opgehaald en getoont worden */
        snprintf(tmp, sizeof(tmp), "iets.html#post%i", message_data->message->postid);
        ftd_gui_center_eventlist_open_uri(NULL, tmp, NULL);

        gtk_widget_destroy(message_data->message->window);
        g_free(message_data);
    }

    g_free(tekst);

    return FALSE;
}

gboolean
ftd_gui_message_callback_cmdCancel(GtkWidget *caller, gpointer data)
{
    gtk_widget_destroy((GtkWidget *) data);
    return FALSE;
}
