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

static void
ftd_gui_nzb_activated(GtkWidget *caller, gpointer data)
{
    gchar **values = g_strsplit((const char *) data, "\n", 0);
    FtdString *url = ftd_string_new(values[0]);
    FtdString *parsed_url;
    FtdString *parsed_url2;
    FtdString *parsed_url3;
    FtdEvent *event;
    FtdGroup *group;
    FTDEventlist *eventlist;

    eventlist = ftd_eventlist_get();
    event = ftd_eventlist_lookup_id(eventlist, atoi(values[1]));
    g_object_unref(eventlist);

    g_assert(event);
    g_assert(event->fileName);

    parsed_url = ftd_string_replace(url, "$filename$", ftd_string_value(event->fileName));

    group = ftd_group_by_id(atoi(ftd_string_value((FtdString *) event->groups->data)));

    if (!group) {
        g_warning("Nieuwsgroep id %s behorende bij FTD ID %i onbekend!\n", ftd_string_value((FtdString *) event->groups->data), event->postId);
        parsed_url2 = ftd_string_replace(parsed_url, "$group$", "");
    } else {
        parsed_url2 = ftd_string_replace(parsed_url, "$group$", ftd_string_value(group->groupName));
    }

    parsed_url3 = ftd_string_replace(parsed_url2, " ", "%20");

    ftd_gui_center_launch_browser(ftd_string_value(parsed_url3));

    ftd_string_free(parsed_url3);
    ftd_string_free(parsed_url2);
    ftd_string_free(parsed_url);
    ftd_string_free(url);
    g_strfreev(values);

    ftd_event_free(event);
}

static void
ftd_gui_nzb_destroy(GtkWidget *caller, gpointer data)
{
    g_free(data);
}

void
ftd_gui_nzb_popup(int postId)
{
    GtkWidget *menu = gtk_menu_new();
    GtkWidget *item;
    GIOChannel *channel;
    char *filename;
    char *line = NULL;
    FtdEvent *event;
    FTDEventlist *eventlist;

    eventlist = ftd_eventlist_get();
    event = ftd_eventlist_lookup_id(eventlist, postId);
    g_object_unref(eventlist);

    g_assert(event);

    if (event->fileName == NULL || ftd_string_length(event->fileName) == 0) {
        ftd_gui_dialog_show(NULL, "Opzoeken van NZB's is niet mogelijk bij M3U playlists", NULL, FALSE);
        ftd_event_free(event);
        return;
    }

    ftd_event_free(event);
    event = NULL;

    filename = ftd_file_parse_data_filename("nzb.conf");
    channel = g_io_channel_new_file(filename, "r", NULL);
    g_free(filename);
    if (!channel) {
        ftd_gui_dialog_show(NULL, "Geen NZB diensten bekend", NULL, TRUE);
        return;
    }

    while (g_io_channel_read_line(channel, &line, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {    // Let op: line bevat een afsluitende \n!
        gchar **values = g_strsplit(line, "\t", 0);
        if (values) {
            if (values[0]) {
                if (values[1]) {
                    char *data = g_strdup_printf("%s%i", values[1], postId);
                    item = gtk_menu_item_new_with_label(values[0]);
                    g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(ftd_gui_nzb_activated), data);
                    g_signal_connect(GTK_OBJECT(item), "destroy", G_CALLBACK(ftd_gui_nzb_destroy), data);
                    gtk_widget_show(item);
                    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
                }
            }
        }

        g_strfreev(values);
        g_free(line);
        line = NULL;
    }

    gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, GINT_TO_POINTER(postId), 0, gtk_get_current_event_time());

    g_io_channel_unref(channel);
}
