//
// $Id: mavbiff.c,v 1.4 2001/12/19 12:29:45 mavetju Exp $
//

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include "mavraise.h"

#define STRSIZE	200

struct mailbox {
    char *	label;
    char *	mbox;
    char *	xterm;
    GtkButton *	button;
    time_t	lastchanged;

    struct mailbox *next;
};

struct mailbox *mailboxes=NULL;

//
// Check if the modified time of the file has changed.
// If so, change the button.
//
gint update(void) {
    struct stat sb;
    struct mailbox *mailbox=mailboxes;

    while (mailbox!=NULL) {
	if (stat(mailbox->mbox,&sb)==0) {
	    if (mailbox->lastchanged < sb.st_mtime) {
		mailbox->lastchanged=sb.st_mtime;
		gtk_button_set_relief(mailbox->button,GTK_RELIEF_NORMAL);
		gtk_widget_set_state(GTK_WIDGET(mailbox->button),GTK_STATE_ACTIVE);
	    }
	}
	mailbox=mailbox->next;
    }

    return 1;
}

//
// If a button is clicked, bring the right window to the front.
//
void on_button_clicked(GtkButton *button,gpointer user_data) {
    struct mailbox *mailbox;

    mailbox=mailboxes;
    while (mailbox->button!=button)
	mailbox=mailbox->next;

    mavraise(mailbox->xterm);
    gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NONE);
}

void quit(void) {
    gtk_exit(0);
}

void readmailbox(FILE *fin,char *mbox,char *label,char *xterm) {
    char line[STRSIZE];

    xterm[0]=0;
    mbox[0]=0;
    label[0]=0;

    if (fgets(label,STRSIZE,fin)==NULL) return;
    if (fgets(mbox,STRSIZE,fin)==NULL) return;
    if (fgets(xterm,STRSIZE,fin)==NULL) return;
    fgets(line,STRSIZE,fin);

    label[strlen(label)-1]=0;
    mbox[strlen(mbox)-1]=0;
    xterm[strlen(xterm)-1]=0;
}

GtkWidget *init_window(void) {
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *button;
    FILE *fin;
    char *filename;
    struct mailbox *mailbox;
    char label[STRSIZE];
    char mbox[STRSIZE];
    char xterm[STRSIZE];

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_object_set_data(GTK_OBJECT(window),"window",window);
    gtk_window_set_title(GTK_WINDOW(window),"MavBiff");

    vbox = gtk_vbox_new(FALSE,0);
    gtk_widget_ref(vbox);
    gtk_object_set_data_full(GTK_OBJECT(window),"vbox",vbox,
		    (GtkDestroyNotify)gtk_widget_unref);
    gtk_widget_show(vbox);
    gtk_container_add(GTK_CONTAINER(window),vbox);

    asprintf(&filename,"%s/.mavbiffrc",getenv("HOME"));
    if ((fin=fopen(filename,"r"))==NULL) {
	perror("fopen");
	exit(0);
    }
    free(filename);

    while (!feof(fin)) {
	readmailbox(fin,mbox,label,xterm);

	if (mbox[0]==0) break;

	button=gtk_button_new_with_label(label);
	gtk_widget_ref(button);
	gtk_object_set_data_full(GTK_OBJECT(window),"button",button,
			(GtkDestroyNotify)gtk_widget_unref);
	gtk_widget_show(button);
	gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,1);
	gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NORMAL);

	gtk_signal_connect(GTK_OBJECT(button),"clicked",
			    GTK_SIGNAL_FUNC(on_button_clicked),
			    NULL);

	if (mailboxes==NULL) {
	    mailboxes=(struct mailbox *)calloc(1,sizeof(struct mailbox));
	    mailboxes->next=NULL;
	    mailbox=mailboxes;
	} else {
	    mailbox=(struct mailbox *)calloc(1,sizeof(struct mailbox));
	    mailbox->next=mailboxes;
	    mailboxes=mailbox;
	}

	mailbox->xterm=strdup(xterm);
	mailbox->mbox=strdup(mbox);
	mailbox->label=strdup(label);
	mailbox->button=(GtkButton *)button;
	mailbox->xterm=strdup(xterm);
	mailbox->lastchanged=0l;
    }

    gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(quit),NULL);
    gtk_widget_show(window);

    return window;
}

int main(int argc,char **argv) {
    init_X();
    gtk_init(&argc,&argv);
    init_window();
    gtk_timeout_add(1000,(GtkFunction)update,NULL);
    gtk_main();
    close_X();
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1