/*-
 * Copyright (c) 2004 Seth Kingsley.  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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgment:
 *    This product includes software developed by Seth Kingsley and
 *    contributors.
 * 4. Neither the author's name, nor any of the contributors names may be
 *    used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 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 AUTHOR 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    <sys/cdefs.h>

#include    <gtk/gtkwindow.h>
#include    <gtk/gtktextview.h>

#include    <time.h>

#include    <xmk-gtk.h>

#include    "bpm-gui.h"
#include    "support.h"

__RCSID("$Id: log.c,v 1.2 2004/08/24 02:40:08 sethk Exp $");

struct log_data	*ldatap;

struct log_data
{
    GtkWindow	    *ld_window;
    GtkTextView	    *ld_textview;
    GtkTextBuffer   *ld_textbuf;
    GtkTextTag	    *ld_header_tag;
};

struct log_data *
log_init(void)
{
    static struct log_data ld;

    ld.ld_window = GTK_WINDOW(create_log_win());
    ld.ld_textview = GTK_TEXT_VIEW(lookup_widget(GTK_WIDGET(ld.ld_window),
		"textview"));

    ld.ld_textbuf = gtk_text_view_get_buffer(ld.ld_textview);
    ld.ld_header_tag = gtk_text_buffer_create_tag(ld.ld_textbuf,
	    "header",
	    "weight", PANGO_WEIGHT_BOLD,
	    NULL);

    return (ldatap = &ld);
}

void
log_fini(struct log_data *ldp)
{
}

void
log_start(struct log_data *ldp, const char *dir, const char *action)
{
    GtkTextIter iter;
    time_t t;
    char desc[128 + 1];

    time(&t);
    gtk_text_buffer_get_end_iter(ldp->ld_textbuf, &iter);
    gtk_text_buffer_insert_with_tags(ldp->ld_textbuf,
	    &iter,
	    desc,
	    g_snprintf(desc, sizeof(desc), "[%.21s] %s in %s:\n",
		ctime(&t),
		action,
		dir),
	    ldp->ld_header_tag,
	    NULL);
    gtk_text_view_scroll_to_iter(ldp->ld_textview, &iter, FALSE, 0, 0, 0);
}

void
log_finish(struct log_data *ldp)
{
    GtkTextIter iter;

    gtk_text_buffer_get_end_iter(ldp->ld_textbuf, &iter);
    gtk_text_buffer_insert_with_tags(ldp->ld_textbuf, &iter, "\n\n", 2, NULL);
    gtk_text_view_scroll_to_iter(ldp->ld_textview, &iter, FALSE, 0, 0, 0);
}

void
log_report(struct log_data *ldp, const gchar *text)
{
    GtkTextIter iter;

    gtk_text_buffer_get_end_iter(ldp->ld_textbuf, &iter);
    gtk_text_buffer_insert_with_tags(ldp->ld_textbuf, &iter, text, -1, NULL);
    gtk_text_buffer_insert_with_tags(ldp->ld_textbuf, &iter, "\n", 1, NULL);
    gtk_text_view_scroll_to_iter(ldp->ld_textview, &iter, FALSE, 0, 0, 0);
}

void
log_show(struct log_data *ldp)
{
    gtk_window_present(ldp->ld_window);
}

void
log_clear(struct log_data *ldp)
{
    gtk_text_buffer_set_text(ldp->ld_textbuf, "", 0);
}

