/* Themes.c- Theme stuff
 * 
 *  WPrefs - Window Maker Preferences Program
 * 
 *  Copyright (c) 1998 Alfredo K. Kojima
 * 
 *  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.
 */


#include "WPrefs.h"

#include <unistd.h>

typedef struct _Panel {
    WMFrame *frame;

    char *sectionName;

    CallbackRec callbacks;
    
    WMWindow *win;

    WMButton *saveB;
    WMList *list;
    WMButton *loadB;
    WMButton *instB;
    
    WMFrame *totF;
    WMButton *totB;
    WMLabel *totL;
    
    WMFrame *botF;
    WMButton *botB;
    WMLabel *botL;

    pid_t tilePID;
    pid_t barPID;
} _Panel;



#define ICON_FILE	"theme"


static void
showData(_Panel *panel)
{

}


static void
finishedTileDownload(void *data)
{
    _Panel *panel = (_Panel*)data;

    WMSetButtonText(panel->totB, _("Set"));
    panel->tilePID = 0;
}



static void
finishedBarDownload(void *data)
{
    _Panel *panel = (_Panel*)data;

    WMSetButtonText(panel->botB, _("Set"));
    panel->barPID = 0;
}


static pid_t
downloadFile(WMScreen *scr, _Panel *panel, char *file)
{
    pid_t pid;
    
    pid = fork();
    if (pid < 0) {
	wsyserror("could not fork() process");

	WMRunAlertPanel(scr, panel->win, _("Error"), 
			"Could not start download. fork() failed",
			_("OK"), NULL, NULL);
	return -1;
    }
    if (pid != 0) {
	return pid;
    }

    close(ConnectionNumber(WMScreenDisplay(scr)));

    

    exit(1);
}


static void
downloadCallback(WMWidget *w, void *data)
{
    _Panel *panel = (_Panel*)data;
    pid_t newPid;
    WMButton *button = (WMButton*)w;
    pid_t *pid;

    if (button == panel->totB) {
	pid = &panel->tilePID;
    } else {
	pid = &panel->barPID;
    }
    
    if (*pid == 0) {
	newPid = downloadFile(WMWidgetScreen(w), panel, NULL);
	if (newPid < 0) {
	    return;
	}
	WMSetButtonText(button, _("Stop"));

	if (button == panel->totB) {
	    AddDeadChildHandler(newPid, finishedTileDownload, data);
	} else {
	    AddDeadChildHandler(newPid, finishedBarDownload, data);
	}
	*pid = newPid;
    } else {
	*pid = 0;

	WMSetButtonText(button, _("Download"));
    }
}



static void
updateThemeList(_Panel *panel)
{
    WMClearList(panel->list);

    
    
}



static void
createPanel(Panel *p)
{
    _Panel *panel = (_Panel*)p;

    panel->frame = WMCreateFrame(panel->win);
    WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
    WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);

    panel->saveB = WMCreateCommandButton(panel->frame);
    WMResizeWidget(panel->saveB, 154, 24);
    WMMoveWidget(panel->saveB, 15, 10);
    WMSetButtonText(panel->saveB, _("Save Current Theme"));

    panel->list = WMCreateList(panel->frame);
    WMResizeWidget(panel->list, 154, 150);
    WMMoveWidget(panel->list, 15, 40);

    panel->loadB = WMCreateCommandButton(panel->frame);
    WMResizeWidget(panel->loadB, 74, 24);
    WMMoveWidget(panel->loadB, 15, 200);
    WMSetButtonText(panel->loadB, _("Load"));

    panel->instB = WMCreateCommandButton(panel->frame);
    WMResizeWidget(panel->instB, 74, 24);
    WMMoveWidget(panel->instB, 95, 200);
    WMSetButtonText(panel->instB, _("Install"));

    
    /**************** Tile of the day ****************/
    
    panel->totF = WMCreateFrame(panel->frame);
    WMResizeWidget(panel->totF, 210, 105);
    WMMoveWidget(panel->totF, 240, 10);
    WMSetFrameTitle(panel->totF, _("Tile of The Day"));

    panel->totL = WMCreateLabel(panel->totF);
    WMResizeWidget(panel->totL, 67, 67);
    WMMoveWidget(panel->totL, 25, 25);
    WMSetLabelRelief(panel->totL, WRSunken);

    panel->totB = WMCreateCommandButton(panel->totF);
    WMResizeWidget(panel->totB, 86, 24);
    WMMoveWidget(panel->totB, 105, 45);
    WMSetButtonText(panel->totB, _("Download"));
    WMSetButtonAction(panel->totB, downloadCallback, panel);

    WMMapSubwidgets(panel->totF);
    
    /**************** Bar of the day ****************/

    panel->botF = WMCreateFrame(panel->frame);
    WMResizeWidget(panel->botF, 315, 95);
    WMMoveWidget(panel->botF, 190, 125);
    WMSetFrameTitle(panel->botF, _("Bar of The Day"));

    panel->botL = WMCreateLabel(panel->botF);
    WMResizeWidget(panel->botL, 285, 32);
    WMMoveWidget(panel->botL, 15, 20);
    WMSetLabelRelief(panel->botL, WRSunken);

    panel->botB = WMCreateCommandButton(panel->botF);
    WMResizeWidget(panel->botB, 86, 24);
    WMMoveWidget(panel->botB, 110, 60);
    WMSetButtonText(panel->botB, _("Download"));
    WMSetButtonAction(panel->botB, downloadCallback, panel);

    WMMapSubwidgets(panel->botF);

    WMRealizeWidget(panel->frame);
    WMMapSubwidgets(panel->frame);
    
    showData(panel);
}


static void
storeData(_Panel *panel)
{
}



Panel*
InitThemes(WMScreen *scr, WMWindow *win)
{
    _Panel *panel;

    panel = wmalloc(sizeof(_Panel));
    memset(panel, 0, sizeof(_Panel));

    panel->sectionName = _("Themes");
    
    panel->win = win;
    
    panel->callbacks.createWidgets = createPanel;
    panel->callbacks.updateDomain = storeData;

    AddSection(panel, ICON_FILE);

    return panel;
}
