/*  gfocustimer
 *  Copyright (C) 1999 James Cameron, <quozl@us.netrek.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <sys/time.h>
#include <gtk/gtk.h>
#include <stdlib.h>

#include "utilities.h"
#include "data.h"
#include "total.h"


/* main window */
extern struct mw mw;

/* preferences data */
extern struct pd pd;


/* update the total */
void total_update ()
{
  char *new;

  if (pd.totals) {
    new = time_to_text (mw.total.time);
    gtk_clist_set_text (mw.list, mw.total.row, 0, new);
    g_free (new);
  }

  /* detect request to warn of work limit reached */
  {
    char *value = getenv("GFOCUSTIMER_LIMIT_VALUE");
    char *action = getenv("GFOCUSTIMER_LIMIT_ACTION");
    static int done = 0;
    if (value != NULL) {
      if (mw.total.time > atoi(value)) {
	if (!done) {
	  if (action != NULL) {
	    system(action);
	  }
	  done++;
	}
      }
      if (mw.total.time <= atoi(value)) {
        done=0;  /* Reset work limit trigger */
      }
    }
  }
}


/* adjust the total */
void total_adjust (time_t amount)
{
  time_t newtotal=0;
  struct ed *ptr;
  gint row=0;
  while ((ptr=(struct ed *) gtk_clist_get_row_data (mw.list, row))) {
    newtotal+=ptr->time;
    row++;
  };
  mw.total.time = newtotal;
  total_update();

#if 0
  if (amount != 0) {
    mw.total.time += amount;
    total_update ();
  }
#endif
}


/* show the total */
void total_show ()
{
  char *columns[ENTRYLISTCOLUMNS];

  columns[0] = "";
  columns[1] = "";
  mw.total.row = gtk_clist_append (mw.list, columns);
  gtk_clist_set_selectable (mw.list, mw.total.row, FALSE);

  columns[0] = time_to_text (mw.total.time);
  columns[1] = "Total";
  mw.total.row = gtk_clist_append (mw.list, columns);
  gtk_clist_set_selectable (mw.list, mw.total.row, FALSE);

  g_free (columns[0]);
}


/* hide the total */
void total_hide ()
{
  gtk_clist_remove (mw.list, mw.total.row);
  gtk_clist_remove (mw.list, mw.total.row-1);
}
