/*  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 <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

#include "interface.h"
#include "callbacks.h"
#include "utilities.h"
#include "data.h"
#include "entry.h"
#include "charge.h"
#include "total.h"
#include "preferences.h"
#include "shortcuts.h"


/* main window */
extern struct mw mw;

/* preferences data */
extern struct pd pd;


/* preferences window */
struct pw pw;

/* update interlock, true if we are creating preferences window */
int interlock;


/*
 * Preferences Window						********
 */


void preferences_default (struct pd *pd)
{
  /* these must match the behaviour of the created windows */

  pd->charging     = FALSE;
  pd->watching     = FALSE;

  /* display format */
  pd->headings     = HEADINGS_HIDDEN;
  pd->format       = FORMAT_HH_MM;
  pd->totals       = TOTALS_HIDDEN;

  /* focus watching */
  pd->focus_rate   = 200;
  pd->unknown_do   = UNKNOWN_DO_CURRENT;
  pd->connect_do   = CONNECT_DO_BEEP;
  pd->connect_time = 2000;

  /* automatic save */
  pd->save_do      = SAVE_DO_OFF;
  pd->save_rate    = 10;

  /* check lock */
  pd->lock_do      = LOCK_DO_NOTHING;
  pd->lock_ignore_time = 0;

  /* miscellaneous */
  pd->icon_do      = ICON_DO_ME;
  pd->project_do   = PROJECT_DO_NOTHING;
}


void preferences_save (struct pd *pd)
{
  char *prefix;
  char *suffix;
  FILE *file;

  prefix = file_prefix();
  suffix = FILESUFFIXPREFS;
  file = entry_save_open (prefix, suffix);
  if (file == NULL) {
    g_free (prefix);
    return;
  }

  /* code: simplicity high, maintainability low */
  fprintf(file, "%s=%d\n", "charging",		pd->charging);
  fprintf(file, "%s=%d\n", "watching",		pd->watching);
  fprintf(file, "%s=%d\n", "headings",		pd->headings);
  fprintf(file, "%s=%d\n", "format",		pd->format);
  fprintf(file, "%s=%d\n", "totals",		pd->totals);
  fprintf(file, "%s=%d\n", "focus_rate",	pd->focus_rate);
  fprintf(file, "%s=%d\n", "unknown_do",	pd->unknown_do);
  fprintf(file, "%s=%d\n", "connect_do",	pd->connect_do);
  fprintf(file, "%s=%d\n", "connect_time",	pd->connect_time);
  fprintf(file, "%s=%d\n", "save_do",		pd->save_do);
  fprintf(file, "%s=%d\n", "save_rate",		pd->save_rate);
  fprintf(file, "%s=%d\n", "lock_do",		pd->lock_do);
  fprintf(file, "%s=%d\n", "lock_ignore_time",	pd->lock_ignore_time);
  fprintf(file, "%s=%d\n", "icon_do",		pd->icon_do);
  fprintf(file, "%s=%d\n", "project_do",	pd->project_do);

  fclose (file);

  g_free (prefix);
}


void preferences_load (struct pd *pd)
{
  char *prefix;
  char *suffix;
  char *name;
  FILE *file;

  prefix = file_prefix();
  suffix = FILESUFFIXPREFS;
  name = g_strdup_printf ("%s/%s", prefix, suffix);
  file = fopen (name, "r");
  if (file == NULL) {

    /* set defaults for new user */
    pd->headings = 1;
    pd->format = 1;
    pd->totals = 1;
    pd->charging = 1;

    return;
  }
  g_free (name);
  g_free (prefix);

  while(1) {
    char *line, buffer[80], *token, *value;

    line = fgets (buffer, 80, file);
    if (line == NULL) break;

    if (line[0] == '#') continue;

    token = strtok (line, "=");
    if (token == NULL) continue;

    value = strtok (NULL, "\n\r");
    if (value == NULL) continue;

    /* shame, shame, cut and paste programming */
    if (!g_strcasecmp(token,"charging")) {
      pd->charging = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"watching")) {
      pd->watching = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"headings")) {
      pd->headings = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"format")) {
      pd->format = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"totals")) {
      pd->totals = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"focus_rate")) {
      pd->focus_rate = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"unknown_do")) {
      pd->unknown_do = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"connect_do")) {
      pd->connect_do = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"connect_time")) {
      pd->connect_time = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"save_do")) {
      pd->save_do = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"save_rate")) {
      pd->save_rate = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"lock_do")) {
      pd->lock_do = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"lock_ignore_time")) {
      pd->lock_ignore_time = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"icon_do")) {
      pd->icon_do = atoi(value);
      continue;
    }

    if (!g_strcasecmp(token,"project_do")) {
      pd->project_do = atoi(value);
      continue;
    }

  }

  fclose (file);
}


void preferences_apply (struct pd *was, struct pd *is)
{
  /* was the time format changed? */
  if (is->format   != was->format) {
    charge_reschedule();

    /* refresh the current entries and the total */
    entry_refresh ();
    total_update ();
  }

  /* was the headings setting changed? */
  if (is->headings && ! was->headings)
    gtk_clist_column_titles_show (mw.list);

  if (! is->headings && was->headings)
    gtk_clist_column_titles_hide (mw.list);

  /* was the totals setting changed? */
  if (is->totals && ! was->totals)
    total_show ();

  if (! is->totals && was->totals)
    total_hide ();

  /* was the focus rate changed? */
  if (is->focus_rate != was->focus_rate) {
    focus_reschedule();
  }

}


void preferences_create()
{
  if (pw.widget != NULL) return;
  gtk_widget_set_sensitive (GTK_WIDGET(mw.preferences), 0);
  interlock = TRUE;
  pw.widget = create_preferences ();
  pw.shortcuts = GTK_CLIST(lookup (pw.widget, "shortcut_list"));
  shortcut_show (GTK_WIDGET(pw.shortcuts));

  /* set the preferences settings from memory */

  /* display format */
  widget_set_int (pw.widget, "headings", pd.headings);
  widget_set_int (pw.widget, "format", pd.format);
  widget_set_int (pw.widget, "totals", pd.totals);

  /* focus watching */
  widget_set_int (pw.widget, "focus_rate", pd.focus_rate);
  widget_set_int (pw.widget, "unknown_do", pd.unknown_do);
  widget_set_int (pw.widget, "connect_do", pd.connect_do);
  widget_set_int (pw.widget, "connect_time", pd.connect_time);

  /* automatic save */
  widget_set_int (pw.widget, "save_do", pd.save_do);
  widget_set_int (pw.widget, "save_rate", pd.save_rate*1000);

  /* check lock */
  widget_set_int (pw.widget, "lock_do", pd.lock_do);
  widget_set_int (pw.widget, "lock_ignore_time", pd.lock_ignore_time*1000);

  /* miscellaneous */
  widget_set_int (pw.widget, "icon_do", pd.icon_do);
  widget_set_int (pw.widget, "project_do", pd.project_do);

  gtk_widget_show (pw.widget);
  interlock = FALSE;
}


void
on_preferences_apply_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
  struct pd was;

  if (interlock) return;

  /* save a copy of the old preferences */
  was = pd;

  /* read them from the widgets */
  /* display format */
  pd.headings = widget_get_int (pw.widget, "headings");
  pd.format = widget_get_int (pw.widget, "format");
  pd.totals = widget_get_int (pw.widget, "totals");

  /* focus watching */
  pd.focus_rate = widget_get_int (pw.widget, "focus_rate");
  pd.unknown_do = widget_get_int (pw.widget, "unknown_do");
  pd.connect_do = widget_get_int (pw.widget, "connect_do");
  pd.connect_time = widget_get_int (pw.widget, "connect_time");

  /* automatic save */
  pd.save_do = widget_get_int (pw.widget, "save_do");
  pd.save_rate = widget_get_int (pw.widget, "save_rate") / 1000;

  /* check lock */
  pd.lock_do = widget_get_int (pw.widget, "lock_do");
  pd.lock_ignore_time = widget_get_int (pw.widget, "lock_ignore_time") / 1000;

  /* miscellaneous */
  pd.icon_do = widget_get_int (pw.widget, "icon_do");
  pd.project_do = widget_get_int (pw.widget, "project_do");

  /* now apply them to reality, if changed */
  if (memcmp(&was, &pd, sizeof (struct pd))) {
    preferences_apply (&was, &pd);
  }
}


/* obsolete */
void
on_preferences_cancel_clicked          (GtkButton       *button,
                                        gpointer         user_data)
{
  gtk_widget_destroy (pw.widget);
  pw.widget = NULL;
  gtk_widget_set_sensitive (GTK_WIDGET(mw.preferences), 1);
}


/* obsolete */
void
on_preferences_ok_clicked              (GtkButton       *button,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (button, user_data);
  on_preferences_cancel_clicked (button, user_data);
}


gboolean
on_preferences_delete_event            (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
  gtk_widget_set_sensitive (GTK_WIDGET(mw.preferences), 1);
  pw.widget = NULL;
  return FALSE;
}


void
on_headings_0_toggled                  (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_headings_1_toggled                  (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_totals_0_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_totals_1_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_format_0_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_format_1_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_format_2_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_format_3_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


gboolean
on_focus_rate_leave_notify_event       (GtkWidget       *widget,
                                        GdkEventCrossing *event,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
  return FALSE;
}


gboolean
on_connect_time_leave_notify_event     (GtkWidget       *widget,
                                        GdkEventCrossing *event,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
  return FALSE;
}


void
on_unknown_do_1_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_unknown_do_0_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_connect_do_0_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}


void
on_connect_do_1_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  on_preferences_apply_clicked (NULL, NULL);
}

