/**************************************************/
/*                                                */
/*             Busy Bee in gtk+                   */
/*                                                */
/*         Copyright 2001 Alan Schmitt            */
/*                                                */
/* This file is distributed under the terms of    */
/* the GNU General Public License.                */
/* See http://www.gnu.org/ for detail             */
/*                                                */
/**************************************************/

#include <gtk/gtk.h>
#include <stdio.h>

#define MAX_STRING_SIZE 3000

GtkWidget *window;
GtkWidget *win_edit;
GtkWidget *win_config;

gpointer active_timeout;

gboolean dontraise = FALSE;

GDate *last_start_date;

GTimeVal last_time;
GTimeVal current_time;
GtkWidget *time_start_label;
gchar *time_start_string;
GtkWidget *time_end_label;
gchar *time_end_string;

GtkObject *delay_adjustment;

GtkWidget *project_entry;
GtkWidget *comment_entry;

GtkWidget *note_project_entry;
GtkWidget *note_comment_entry;

gint timezone = 1;

/* file containing the todo list */
gchar todofile[] = "todolist.txt";

/* box for the notes */
GtkWidget *notesbox;

/* ten timouts may be running at the same time */
int used_ids[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

struct note_struct {
  GtkWidget *notebox;
  GtkWidget *noteproject;
  GtkWidget *notecomment;
  GtkWidget *donebutton;
  GtkWidget *addbutton;
};

/* to get a free id */
gpointer new_id (void) {
  int i;
  for (i=0; i<10; i++) {
    if (used_ids[i] == 0) {
      used_ids[i] = 1;
      active_timeout = (gpointer) i;
      /*      g_print ("assigned id %d.\n", i);*/
      return (gpointer) i;
    }
  }
  /*  g_print ("no more free ids\n");*/
  return (gpointer) -1;
}

gboolean myshow( gpointer data )
{
  if (data != active_timeout) 
    {
      used_ids[(guint)data] = 0;
      /*      g_print ("freed id %d\n", (guint) data);*/
      return(FALSE);
    }
  if (dontraise == TRUE) {
    return (TRUE);
  }
  if (GTK_WIDGET_REALIZED(window)) 
    {
      /*      g_print("raising\n");*/
      gdk_window_show (window -> window);
    }
  /*  g_print("ok\n");*/
  return(TRUE);
}

gboolean create_timeout( guint delay) 
{
  gpointer id = new_id ();
  if (id == (gpointer) -1) 
    {
      return FALSE;
    }
  g_timeout_add (delay * 1000, (GSourceFunc)(myshow), id);    
  return TRUE;
}

void cancelfun( GtkWidget *widget,
		gpointer   data )
{
  dontraise = FALSE;
  
  /* not necessary all the time, but does not hurt */
  gtk_entry_set_text (GTK_ENTRY(project_entry), "");
  gtk_entry_set_text (GTK_ENTRY(comment_entry), "");  

  gtk_widget_hide(GTK_WIDGET(data));
  /*
  gtk_grab_remove(GTK_WIDGET(data));
  */
  gdk_window_show(window->window);
}

/* tell to sleep longer */
void sleep_more( GtkWidget *widget,
		 gpointer data)
{
  guint delay;

  delay = (guint) ((GTK_ADJUSTMENT(gtk_range_get_adjustment(GTK_RANGE(data)))) -> value);
  /*  g_print ("setting sleep for %d minutes.\n", delay);*/
  create_timeout(delay * 60);
  cancelfun (NULL, (gpointer)win_config);
}

void addlog( GtkWidget *widget,
	     gpointer data )
{
  FILE *file;
  
  gchar *project_string;
  gchar *comment_string;

  gchar *date_string;

  gchar *tolog;

  guint tologsize;

  glong time_used;

  date_string = g_strdup ("01/01/1970");

  project_string = gtk_editable_get_chars (GTK_EDITABLE(project_entry),0,-1);
  comment_string = gtk_editable_get_chars (GTK_EDITABLE(comment_entry),0,-1);

  time_used = (guint)(GTK_ADJUSTMENT(delay_adjustment) -> value);

  last_time.tv_sec = last_time.tv_sec + (time_used * 60);

  g_date_strftime (date_string, 14, "%d/%m/%Y", last_start_date);

  tolog = g_strdup_printf("%s\t%s\t%s\t%d\t%s\t%s\n", date_string, time_start_string, time_end_string, time_used, project_string, comment_string);

  tologsize = strlen (tolog);

  file = fopen ("test.txt","a");
  fwrite (tolog, 1, tologsize, file);
  fclose (file);

  /*  g_print ("logged: %s", tolog);*/

  g_free (project_string);
  g_free (comment_string);
  g_free (tolog);
  g_free (date_string);

  g_date_set_time (last_start_date, time(NULL));

  cancelfun( widget, data);
}

void nothing( GtkWidget *widget,
	      gpointer data )
{

  glong time_used;

  time_used = (guint)(GTK_ADJUSTMENT(delay_adjustment) -> value);
  last_time.tv_sec = last_time.tv_sec + (time_used * 60);
  cancelfun( widget, data);
}

gint delete_event( GtkWidget *widget,
                   GdkEvent  *event,
                   gpointer   data )
{
    gtk_main_quit();
    return(FALSE);
}

glong get_hours( glong seconds )
{
  return ((glong)(seconds / 3600) % 24);
}

glong get_minutes( glong seconds )
{
  return ((glong)(seconds / 60) % 60);
}

gchar *return_time_string( glong hours,
			   glong minutes )
{
  return (g_strdup_printf ("%.2d:%.2d", hours, minutes));
}

void set_time_label( GtkLabel *label,
		     gchar **string,
		     glong hours,
		     glong minutes )
{
  g_free ((gpointer)*string);
  *string = return_time_string(hours, minutes);
  gtk_label_set_text (label, *string);
}

void add_minutes( glong min )
{
  glong hours;
  glong minutes;
  glong end;
  end = last_time.tv_sec + (60 * min);
  hours = (get_hours(end) + timezone) % 24;
  minutes = get_minutes(end);
  set_time_label (GTK_LABEL(time_end_label), &time_end_string, hours, minutes);
}

void modify_end_label( GtkAdjustment *adj,
		       gpointer unused)
{
  add_minutes ((guint)(adj->value));
}

void edit( GtkWidget *widget,
	   gpointer   data )
{
  glong hours;
  glong minutes;
  glong minutes_span;
  dontraise = TRUE;
  g_get_current_time(&current_time);
  minutes_span = (current_time.tv_sec - last_time.tv_sec) / 60;
  GTK_ADJUSTMENT(delay_adjustment) -> upper = minutes_span;
  GTK_ADJUSTMENT(delay_adjustment) -> value = minutes_span;
  hours = (get_hours(last_time.tv_sec) + timezone) % 24;
  minutes = get_minutes(last_time.tv_sec);
  set_time_label (GTK_LABEL(time_start_label), &time_start_string, hours, minutes);
  add_minutes (minutes_span);
  gdk_window_hide(window->window);
  gtk_widget_show (win_edit);
}

void config( GtkWidget *widget,
	     gpointer   data )
{
  dontraise = TRUE;
  gdk_window_hide(window->window);
  gtk_widget_show (GTK_WIDGET(win_config));
}

gboolean delete_modal( GtkWidget *widget,
		  GdkEvent  *event,
		  gpointer   data )
{
  dontraise = FALSE;
  gtk_widget_hide(GTK_WIDGET(data));
  gdk_window_show(window->window);
  return(TRUE);
}

void winter_button_callback (GtkWidget *widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active) 
    {
      timezone = 1;
    } else {
      timezone = 2;
    }
}

GtkWidget *create_config_window (void)
{
  GtkWidget *win_config;

  GtkWidget *box;
  
  GtkWidget *button;

  GtkWidget *title_label;

  GtkWidget *separator;

  /* for the range adjustement */
  GtkWidget *rangebox;
  GtkObject *adj;
  GtkWidget *hscale;
  GtkWidget *setbox;

  /* radio button for winter / summer */
  GtkWidget *rbutton;
  GSList *group;	

  win_config = gtk_window_new (GTK_WINDOW_DIALOG);
  gtk_window_set_policy (GTK_WINDOW(win_config), FALSE,FALSE,TRUE);
  gtk_window_set_title (GTK_WINDOW (win_config), "Config");

  gtk_signal_connect (GTK_OBJECT (win_config), "delete_event",
		      GTK_SIGNAL_FUNC (delete_modal), (gpointer)win_config);

  gtk_container_set_border_width (GTK_CONTAINER (win_config), 10);

  box = gtk_vbox_new(FALSE, 0);

  gtk_container_add (GTK_CONTAINER (win_config), box);
  
  /* the range bit */
  title_label = gtk_label_new ("Minutes between pop-ups");

  gtk_box_pack_start_defaults (GTK_BOX (box), title_label);

  gtk_widget_show (title_label);

  rangebox = gtk_hbox_new(FALSE, 5);

  adj = gtk_adjustment_new (1.0, 1.0, 120.0, 1.0, 10.0, 0.0);
  gtk_adjustment_set_value (GTK_ADJUSTMENT(adj), 5.0); 
  hscale = gtk_hscale_new (GTK_ADJUSTMENT (adj));
  gtk_scale_set_digits(GTK_SCALE(hscale),0);

  /* displaying the range */
  gtk_box_pack_start(GTK_BOX(rangebox), hscale, TRUE,TRUE,0);
  gtk_widget_show(hscale);

  setbox = gtk_vbox_new(FALSE, 0);
  
  button = gtk_button_new_with_label ("Set");
  
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      GTK_SIGNAL_FUNC (sleep_more), (gpointer) hscale);
  
  gtk_box_pack_end(GTK_BOX(setbox), button, FALSE, FALSE, 0);  

  gtk_box_pack_end(GTK_BOX(rangebox), setbox, FALSE, TRUE, 0);
  
  gtk_widget_show(button);
  gtk_widget_show(setbox);  
  
  gtk_box_pack_start(GTK_BOX(box), rangebox, TRUE, TRUE, 0);
  
  gtk_widget_show(rangebox);
    
  /* winter summer selection */

  separator = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (box), separator,TRUE,TRUE, 10);
  gtk_widget_show (separator);

  title_label = gtk_label_new ("Daylight saving setting");
  gtk_box_pack_start_defaults (GTK_BOX (box), title_label);
  gtk_widget_show (title_label);

  
  rbutton = gtk_radio_button_new_with_label (NULL, "winter time");
  gtk_box_pack_start (GTK_BOX (box), rbutton, TRUE, TRUE, 0);
  gtk_widget_show (rbutton);
  gtk_signal_connect (GTK_OBJECT (rbutton), "toggled",
		      GTK_SIGNAL_FUNC (winter_button_callback), (gpointer) 0);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rbutton), TRUE);
  group = gtk_radio_button_group (GTK_RADIO_BUTTON (rbutton));
  rbutton = gtk_radio_button_new_with_label(group, "summer time");
  gtk_box_pack_start (GTK_BOX (box), rbutton, TRUE, TRUE, 0);
  gtk_widget_show (rbutton);

  /* the OK button */
  separator = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (box), separator,TRUE,TRUE,10);
  gtk_widget_show (separator);

  button = gtk_button_new_with_label ("Close");
    
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      GTK_SIGNAL_FUNC (cancelfun), (gpointer) win_config);

  gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);

  gtk_widget_show(button);

  gtk_widget_show (box);

  return (win_config);
}


GtkWidget *create_editing_window( void )
{
  GtkWidget *win_edit;

  GtkWidget *box;
  GtkWidget *labelbox;
  GtkWidget *buttons;
  GtkWidget *cancelbutton;
  GtkWidget *nothingbutton;
  GtkWidget *logbutton;

  GtkWidget *startl;
  GtkWidget *endl;  

  /* for the range adjustement */
  GtkWidget *hscale;

  /* to put the textboxes */
  GtkWidget *wholetextbox;
  GtkWidget *project_label;
  GtkWidget *comment_label;
  GtkWidget *textlabels;
  GtkWidget *textbox;

  win_edit = gtk_window_new (GTK_WINDOW_DIALOG);
  gtk_window_set_policy (GTK_WINDOW(win_edit), FALSE,FALSE,TRUE);
  gtk_window_set_title (GTK_WINDOW (win_edit), "Edit");

  gtk_signal_connect (GTK_OBJECT (win_edit), "delete_event",
		      GTK_SIGNAL_FUNC (delete_modal), (gpointer)win_edit);

  gtk_container_set_border_width (GTK_CONTAINER (win_edit), 10);

  box = gtk_vbox_new(FALSE, 0);

  gtk_container_add (GTK_CONTAINER (win_edit), box);

  /* the range bit */
  delay_adjustment = gtk_adjustment_new (0.0, 0.0, 180.0, 1.0, 10.0, 0.0);
  hscale = gtk_hscale_new (GTK_ADJUSTMENT (delay_adjustment));
  gtk_scale_set_digits(GTK_SCALE(hscale),0);

  gtk_box_pack_start(GTK_BOX(box), hscale, TRUE, TRUE, 0);

  gtk_widget_show(hscale);

  gtk_signal_connect (GTK_OBJECT (delay_adjustment), "value_changed",
      GTK_SIGNAL_FUNC (modify_end_label), (gpointer)0);

  /* the time labels */
  labelbox = gtk_hbox_new(FALSE,0);
  gtk_box_pack_start(GTK_BOX(box), labelbox, TRUE, TRUE, 0);

  last_start_date = g_date_new ();
  g_date_set_time (last_start_date, time(NULL));

  g_get_current_time(&last_time);
  last_time.tv_usec = 0;

  time_start_string = return_time_string (0,0);
  time_start_label = gtk_label_new (time_start_string);

  startl = gtk_label_new ("started at: ");

  gtk_box_pack_start(GTK_BOX(labelbox), startl, TRUE, TRUE, 0);
  gtk_box_pack_start(GTK_BOX(labelbox), time_start_label, TRUE, TRUE, 0);

  gtk_widget_show(startl);
  gtk_widget_show(time_start_label);

  time_end_string = return_time_string (0,0);
  time_end_label = gtk_label_new(time_end_string);

  endl = gtk_label_new("finished at: ");

  gtk_box_pack_start(GTK_BOX(labelbox), endl, TRUE, TRUE, 0);
  gtk_box_pack_start(GTK_BOX(labelbox), time_end_label, TRUE, TRUE, 0);

  gtk_widget_show(endl);
  gtk_widget_show(time_end_label);
  
  gtk_widget_show(labelbox);

  /* the box to put the text stuff */
  wholetextbox = gtk_hbox_new(FALSE,5);

  /* the labels for the text go in this box */
  textlabels = gtk_vbox_new(FALSE,0);
  
  /* the labels */
  project_label = gtk_label_new("project name");
  comment_label = gtk_label_new("comments");

  gtk_box_pack_start(GTK_BOX(textlabels), project_label, TRUE, TRUE, 0);
  gtk_box_pack_start(GTK_BOX(textlabels), comment_label, TRUE, TRUE, 0);

  gtk_widget_show (project_label);
  gtk_widget_show (comment_label);
  
  gtk_box_pack_start(GTK_BOX(wholetextbox), textlabels, TRUE, TRUE, 0);

  gtk_widget_show (textlabels);

  /* the text entries go in this box */
  textbox = gtk_vbox_new(FALSE,0);

  /* the text entries */
  project_entry = gtk_entry_new ();
  comment_entry = gtk_entry_new ();

  /* displaying the textboxes */
  gtk_box_pack_start(GTK_BOX(textbox), project_entry, TRUE,TRUE,0);
  gtk_box_pack_start(GTK_BOX(textbox), comment_entry, TRUE,TRUE,0);

  gtk_widget_show (project_entry);
  gtk_widget_show (comment_entry);

  gtk_box_pack_start(GTK_BOX(wholetextbox), textbox, TRUE,TRUE,0);

  gtk_widget_show (textbox);

  gtk_box_pack_start(GTK_BOX(box), wholetextbox, TRUE,TRUE,0);

  gtk_widget_show (wholetextbox);

  buttons = gtk_hbox_new(FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box), buttons, TRUE, TRUE, 0);

  logbutton = gtk_button_new_with_label ("Add to log");

  gtk_box_pack_start(GTK_BOX(buttons), logbutton, TRUE, TRUE, 0);

  gtk_widget_show(logbutton);

  gtk_signal_connect (GTK_OBJECT (logbutton), "clicked",
		      GTK_SIGNAL_FUNC (addlog), (gpointer) win_edit);
  

  nothingbutton = gtk_button_new_with_label ("Nothing");
    
  gtk_signal_connect (GTK_OBJECT (nothingbutton), "clicked",
		      GTK_SIGNAL_FUNC (nothing), (gpointer) win_edit);

  gtk_box_pack_start(GTK_BOX(buttons), nothingbutton, TRUE, TRUE, 0);

  gtk_widget_show(nothingbutton);

  cancelbutton = gtk_button_new_with_label ("Cancel");
    
  gtk_signal_connect (GTK_OBJECT (cancelbutton), "clicked",
		      GTK_SIGNAL_FUNC (cancelfun), (gpointer) win_edit);

  gtk_box_pack_start(GTK_BOX(buttons), cancelbutton, TRUE, TRUE, 0);

  gtk_widget_show(cancelbutton);

  gtk_widget_show(buttons);

  gtk_widget_show(box);

  return (win_edit);
}


void list_one_child ( gpointer data,
		      gpointer user_data )
{
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *projectlabel;
  GtkWidget *commentlabel;
  gchar *project_string;
  gchar *comment_string;

  gchar *tofile;
  guint tofilesize;

  GList *vbox_children;

  FILE *file = (FILE *)user_data;

  vbox_children = GTK_BOX(data) -> children;

  hbox = ((GtkBoxChild *)(g_list_nth_data (vbox_children, 1)))->widget;

  projectlabel = ((GtkBoxChild *)g_list_nth_data (GTK_BOX(hbox) -> children, 0))->widget;
  commentlabel = ((GtkBoxChild *)g_list_nth_data (GTK_BOX(hbox) -> children, 1))->widget;
  gtk_label_get (GTK_LABEL(projectlabel), &project_string);
  gtk_label_get (GTK_LABEL(commentlabel), &comment_string);

  tofile = g_strdup_printf("%s\n%s\n", project_string, comment_string);
  /*g_print (tofile);*/
  tofilesize = strlen (tofile);

  fwrite (tofile, 1, tofilesize, file);

  g_free (tofile);
}

void list_children ( void )
{
  GList *children;
  FILE *file;

  file = fopen (todofile, "w");
  children = gtk_container_children (GTK_CONTAINER(notesbox));
  g_list_foreach (children, (GFunc)list_one_child, (gpointer)file);
  fclose (file);
}

void remove_note_callback (GtkWidget *widget, gpointer data)
{
  struct note_struct *note = (struct note_struct *)data;

  gtk_container_remove (GTK_CONTAINER(notesbox), note->notebox);

  /* if someone knows why I cannot free this stuff, I'd like
     to know. I'm note sure I understand this well. */
  /*
  g_free (note->notelabel);
  g_free (note->notebox);
  g_free (note->notebutton);
  */

  g_free (note);

  list_children ();
}

void add_event_note_callback (GtkWidget *widget, gpointer data)
{
  gchar *project_string;
  gchar *comment_string;
  
  struct note_struct *note = (struct note_struct *)data;

  gtk_label_get (GTK_LABEL(note->noteproject), &project_string);
  gtk_label_get (GTK_LABEL(note->notecomment), &comment_string);

  gtk_entry_set_text (GTK_ENTRY(project_entry), project_string);
  gtk_entry_set_text (GTK_ENTRY(comment_entry), comment_string);  

  /*
  gtk_container_remove (GTK_CONTAINER(notesbox), note->notebox);
  gfree (note);
  */
  edit( NULL, NULL);
}

void add_note ( gchar *project_string,
		gchar *comment_string )
{
  
  struct note_struct *note;

  GtkWidget *hbox;
  GtkWidget *separator;

  note = (struct note_struct *)g_malloc(sizeof(struct note_struct));

  note->notebox = gtk_vbox_new (FALSE,0);

  separator = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (note->notebox), separator,TRUE,TRUE, 10);
  gtk_widget_show (separator);
  
  hbox = gtk_hbox_new (FALSE,10);

  note->noteproject = gtk_label_new (project_string);
  note->notecomment = gtk_label_new (comment_string);  
  gtk_misc_set_alignment (GTK_MISC(note->noteproject), 0.0f, 0.5f);
  gtk_misc_set_alignment (GTK_MISC(note->notecomment), 1.0f, 0.5f);
  gtk_box_pack_start (GTK_BOX (hbox), note->noteproject,TRUE,TRUE,0);
  gtk_box_pack_start (GTK_BOX (hbox), note->notecomment,TRUE,TRUE,5);

  gtk_widget_show (note->noteproject);
  gtk_widget_show (note->notecomment);
  
  note->donebutton = gtk_button_new_with_label ("Done");
  gtk_box_pack_start (GTK_BOX (hbox), note->donebutton,FALSE,FALSE,0);  
  gtk_widget_show (note->donebutton);
  gtk_signal_connect (GTK_OBJECT (note->donebutton), "clicked",
		      GTK_SIGNAL_FUNC (remove_note_callback), (gpointer) note );
  
  note->addbutton = gtk_button_new_with_label ("Log Time");
  gtk_box_pack_start (GTK_BOX (hbox), note->addbutton,FALSE,FALSE,0);  
  gtk_widget_show (note->addbutton);
  gtk_signal_connect (GTK_OBJECT (note->addbutton), "clicked",
		      GTK_SIGNAL_FUNC (add_event_note_callback), (gpointer) note );

  gtk_box_pack_end_defaults (GTK_BOX(note->notebox), hbox);
  gtk_box_pack_start_defaults (GTK_BOX(notesbox), note->notebox);
  gtk_widget_show (hbox);
  gtk_widget_show (note->notebox);
}

void add_note_callback (GtkWidget *widget, gpointer data)
{
  gchar *notestringproject;
  gchar *notestringcomment;

  notestringproject = gtk_editable_get_chars (GTK_EDITABLE(note_project_entry),0,-1);
  notestringcomment = gtk_editable_get_chars (GTK_EDITABLE(note_comment_entry),0,-1);
  gtk_entry_set_text (GTK_ENTRY(note_project_entry), "");
  gtk_entry_set_text (GTK_ENTRY(note_comment_entry), "");

  add_note (notestringproject, notestringcomment);

  g_free (notestringproject);
  g_free (notestringcomment);

  list_children ();
}

void read_todofile ( void )
{
  FILE *file;
  gchar *project_string;
  gchar *comment_string;
  gchar *tmp_string;
  int retval;
  int stop = 1;
  int addnote;
  int free_project;
  int free_comment;

  tmp_string = (gchar *)g_malloc(MAX_STRING_SIZE * (sizeof(gchar)));
  
  file = fopen (todofile, "r");
  if (file == NULL) return;

  while (stop != 0) {
    addnote = 0;
    free_project = 0;
    free_comment = 0;
    retval = fscanf (file,"%[^\n]%*c", tmp_string);
    g_print ("retval: %d\n",retval);
    if (retval == 1) {
      /*g_print ("read: %s\n", tmp_string);*/
      project_string = g_strdup (tmp_string);
      free_project = 1;
      addnote = 1;
    }
    else {
      project_string = "";
      if (retval == 0) {
	fscanf (file,"%*c");
      } else {
	stop = 0;
      }
    }

    retval = fscanf (file,"%[^\n]%*c", tmp_string);
    g_print ("retval: %d\n",retval);
    if (retval == 1) {
      /*g_print ("read: %s\n", tmp_string);*/
      comment_string = g_strdup (tmp_string);
      free_comment = 1;
      addnote = 1;
    }
    else {
      comment_string = "";
      if (retval == 0) {
	fscanf (file,"%*c");
      } else {
	stop = 0;
      }
    }
    
    if (addnote == 1) {
      add_note (project_string, comment_string);
      if (free_project == 1) {
	g_free (project_string);
      }
      if (free_comment == 1) {
	g_free (comment_string);
      }
    }
  }
  
  fclose(file);
  
  g_free (tmp_string);
}

int main( int   argc,
          char *argv[] )
{
    GtkWidget *button;
    GtkWidget *box0;
    GtkWidget *box1;
    
    /* notes */
    GtkWidget *wholetextbox;
    GtkWidget *project_label;
    GtkWidget *comment_label;
    GtkWidget *textlabels;
    GtkWidget *textbox;

    gtk_init (&argc, &argv);

    win_edit = create_editing_window ();
    win_config = create_config_window ();

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_policy (GTK_WINDOW(window),FALSE,FALSE,TRUE);

    gtk_window_set_title (GTK_WINDOW (window), "Busy Bee");

    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
			GTK_SIGNAL_FUNC (delete_event), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    /* The big box that contains everything */
    box0 = gtk_vbox_new(FALSE, 0);

    /* The box with the buttons */
    box1 = gtk_hbox_new(FALSE, 0);

    /* Put the box into the main window. */
    gtk_container_add (GTK_CONTAINER (window), box0);

    /* to show the edit window */
    button = gtk_button_new_with_label ("New event");

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

    gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);

    gtk_widget_show(button);

    /* to show the config window */
    button = gtk_button_new_with_label ("Config");

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

    gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);

    gtk_widget_show(button);

    gtk_box_pack_start(GTK_BOX(box0), box1, TRUE, TRUE, 0);

    gtk_widget_show(box1);

    /* the box to put the text stuff */
    wholetextbox = gtk_hbox_new(FALSE,5);
    
    /* the labels for the text go in this box */
    textlabels = gtk_vbox_new(FALSE,0);
    
    /* the labels */
    project_label = gtk_label_new("project name");
    comment_label = gtk_label_new("comments");
    
    gtk_box_pack_start(GTK_BOX(textlabels), project_label, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(textlabels), comment_label, TRUE, TRUE, 0);
    
    gtk_widget_show (project_label);
    gtk_widget_show (comment_label);
    
    gtk_box_pack_start(GTK_BOX(wholetextbox), textlabels, TRUE, TRUE, 0);
    
    gtk_widget_show (textlabels);
    
    /* the text entries go in this box */
    textbox = gtk_vbox_new(FALSE,0);
    
    /* the text entries */
    note_project_entry = gtk_entry_new ();
    note_comment_entry = gtk_entry_new ();
    
    /* displaying the textboxes */
    gtk_box_pack_start(GTK_BOX(textbox), note_project_entry, TRUE,TRUE,0);
    gtk_box_pack_start(GTK_BOX(textbox), note_comment_entry, TRUE,TRUE,0);
    
    gtk_widget_show (note_project_entry);
    gtk_widget_show (note_comment_entry);
    
    gtk_box_pack_start(GTK_BOX(wholetextbox), textbox, TRUE,TRUE,0);
    
    gtk_widget_show (textbox);

    button = gtk_button_new_with_label ("To do");

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

    gtk_box_pack_start(GTK_BOX(wholetextbox), button, FALSE, FALSE, 0);

    gtk_widget_show(button);
    
    gtk_box_pack_start(GTK_BOX(box0), wholetextbox, TRUE,TRUE,5);

    gtk_widget_show (wholetextbox);
    
    notesbox = gtk_vbox_new(FALSE, 0);

    gtk_box_pack_start (GTK_BOX (box0), notesbox, TRUE, TRUE, 0);        
    
    gtk_widget_show (notesbox);

    gtk_widget_show (box0);

    if (create_timeout (600) == FALSE) return 1;

    read_todofile ();

    gtk_widget_show (window);

    gtk_main ();

    return(0);
}
