/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2007 Leandro A. F. Pereira <leandro@linuxmag.com.br>
 *
 *    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, version 2.
 *
 *    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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <gtk/gtk.h>
#include <config.h>
#include <string.h>

#include <hardinfo.h>
#include <shell.h>
#include <iconcache.h>

#include <expr.h>
#include <socket.h>

enum {
    DEVICES_PROCESSORS,
    DEVICES_MEMORY,
    DEVICES_PCI,
    DEVICES_USB,
    DEVICES_PRINTERS,
    DEVICES_BATTERY,
    DEVICES_SENSORS,
    DEVICES_INPUT,
    DEVICES_STORAGE,
} Entries;

static ModuleEntry hi_entries[] = {
    {"Processor",	"processor.png"},
    {"Memory",		"memory.png"},
    {"PCI Devices",	"devices.png"},
    {"USB Devices",	"usb.png"},
    {"Printers",	"printer.png"},
    {"Battery",		"battery.png"},
    {"Sensors",		"therm.png"},
    {"Input Devices",	"keyboard.png"},
    {"Storage",		"hdd.png"},
};

static GHashTable *moreinfo = NULL;
static GSList *processors = NULL;
static gchar *printer_list = NULL;
static gchar *pci_list = "";
static gchar *input_list = NULL;
static gchar *storage_list = "";
static gchar *battery_list = NULL;

#define WALK_UNTIL(x)   while((*buf != '\0') && (*buf != x)) buf++

#define GET_STR(field_name,ptr)      					\
  if (!ptr && strstr(tmp[0], field_name)) {				\
    ptr = g_markup_escape_text(g_strstrip(tmp[1]), strlen(tmp[1]));	\
    g_strfreev(tmp);                 					\
    continue;                        					\
  }

#define get_str(field_name,ptr)               \
  if (g_str_has_prefix(tmp[0], field_name)) { \
    ptr = g_strdup(tmp[1]);                   \
    g_strfreev(tmp);                          \
    continue;                                 \
  }
#define get_int(field_name,ptr)               \
  if (g_str_has_prefix(tmp[0], field_name)) { \
    ptr = atoi(tmp[1]);                       \
    g_strfreev(tmp);                          \
    continue;                                 \
  }
#define get_float(field_name,ptr)             \
  if (g_str_has_prefix(tmp[0], field_name)) { \
    ptr = atof(tmp[1]);                       \
    g_strfreev(tmp);                          \
    continue;                                 \
  }

#include <vendor.h>

typedef struct _Processor	Processor;
struct _Processor {
    gchar *model_name;
    gchar *vendor_id;
    gchar *flags;
    gint cache_size;
    gfloat bogomips, cpu_mhz;

    gchar *has_fpu;
    gchar *bug_fdiv, *bug_hlt, *bug_f00f, *bug_coma;
    
    gint model, family, stepping;
    gchar *strmodel;
    
    gint id;
};

#include <arch/this/processor.h>

#include <arch/this/pci.h>
#include <arch/common/printers.h>
#include <arch/this/inputdevices.h>
#include <arch/this/usb.h>
#include <arch/this/storage.h>
#include <arch/this/battery.h>
#include <arch/this/sensors.h>

static void
detect_devices(void)
{
    moreinfo = g_hash_table_new(g_str_hash, g_str_equal);

    shell_status_update("Getting processor information...");
    if (!processors)
        processors = computer_get_processors();

    shell_status_update("Scanning PCI devices...");
    scan_pci();

    shell_status_update("Searching for printers...");
    scan_printers();

    shell_status_update("Scanning input devices...");
    scan_inputdevices();

    shell_status_update("Scanning USB devices...");
    scan_usb();

    shell_status_update("Scanning IDE devices...");
    scan_ide();

    shell_status_update("Scanning SCSI devices...");
    scan_scsi();

    shell_status_update("Scanning batteries...");
    scan_battery();

    shell_status_update("Reading sensors...");
    read_sensors();
}

gchar *
get_processor_name(void)
{
    if (!processors) {
        processors = computer_get_processors();
    }
    
    Processor *p = (Processor *) processors->data;
    return p->model_name;
}

ShellModuleMethod *hi_exported_methods(void)
{
    static ShellModuleMethod m[] = {
      { "getProcessorName", get_processor_name },
      { NULL }
    };
    
    return m;
}

gchar *
hi_more_info(gchar * entry)
{
    gchar *info = (gchar *) g_hash_table_lookup(moreinfo, entry);

    if (info)
	return g_strdup(info);
    return g_strdup("[Empty]");
}

void
hi_reload(gint entry)
{
    switch (entry) {
    case DEVICES_BATTERY:
        scan_battery();
        break;
    case DEVICES_INPUT:
	scan_inputdevices();
	break;
    case DEVICES_PRINTERS:
	scan_printers();
	break;
    case DEVICES_USB:
	scan_usb();
	break;
    case DEVICES_SENSORS:
	read_sensors();
	break;
    case DEVICES_STORAGE:
	if (storage_list) {
	    g_free(storage_list);
	    g_free(storage_icons);
	    storage_list = g_strdup("");
	    storage_icons = g_strdup("");
	}
	scan_ide();
	scan_scsi();
	break;
    }
}

gchar *
hi_info(gint entry)
{
    if (!moreinfo) {
	detect_devices();
    }

    switch (entry) {
    case DEVICES_PROCESSORS:
        return processor_get_info(processors);
    case DEVICES_MEMORY:
        return g_strdup("[Memory]\nNot implemented=\n");
    case DEVICES_BATTERY:
        return g_strdup_printf("%s\n"
                               "[$ShellParam$]\n"
                               "ReloadInterval=4000\n", battery_list);
    case DEVICES_PCI:
	return g_strdup_printf("[PCI Devices]\n"
			       "%s"
			       "[$ShellParam$]\n"
			       "ViewType=1\n",
			       pci_list);
    case DEVICES_SENSORS:
        return g_strdup_printf("[$ShellParam$]\n"
                               "ReloadInterval=5000\n"
                               "%s", sensors);
    case DEVICES_PRINTERS:
	return g_strdup_printf("%s\n"
			       "[$ShellParam$]\n"
			       "ReloadInterval=5000", printer_list);
    case DEVICES_STORAGE:
	return g_strdup_printf("%s\n"
			       "[$ShellParam$]\n"
			       "ReloadInterval=5000\n"
			       "ViewType=1\n%s", storage_list, storage_icons);
    case DEVICES_INPUT:
	return g_strdup_printf("[Input Devices]\n"
			       "%s"
			       "[$ShellParam$]\n"
			       "ViewType=1\n"
			       "ReloadInterval=5000\n%s", input_list, input_icons);
    case DEVICES_USB:
	return g_strdup_printf("%s"
			       "[$ShellParam$]\n"
			       "ViewType=1\n"
			       "ReloadInterval=5000\n",
			       usb_list);
    default:
	return g_strdup("[Empty]\nNo info available=");
    }
}

gint
hi_n_entries(void)
{
    return G_N_ELEMENTS(hi_entries) - 1;
}

GdkPixbuf *
hi_icon(gint entry)
{
    return icon_cache_get_pixbuf(hi_entries[entry].icon);
}

gchar *
hi_name(gint entry)
{
    return hi_entries[entry].name;
}

gchar *
hi_module_name(void)
{
    return g_strdup("Devices");
}

guchar
hi_module_weight(void)
{
    return 160;
}