/****** WatcherPrefs *********************************************************
*
*   NAME
*       WatcherPrefs -- Preferences program for Watcher (V37)
*
*   PURPOSE
*       To set the preferences of Watcher. Watcher will automatically be
*       notified when the preferences change.
*
*   NOTES
*
*   AUTHOR
*       Franz Hemmer
*
*   SEE ALSO
*
******************************************************************************
*     __
*  __///
*  \XX/ й1993 by Hemsoft Developments
*
*----------------------------------------------------------------------------
*
* Author:		Franz Hemmer
* Written:		07.04.93
* System Req.:	KS 2.04 or higher.
* Notes:
*
*/

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                            I N C L U D E S									*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
#include <exec/types.h>								// For UBYTE etc.
#include <exec/memory.h>							// Proto for MEMF_ANY etc.
#include <clib/asl_protos.h>						// Protos for AllocAslRequest() etc.
#include <clib/dos_protos.h>						// Protos for Lock() etc.
#include <clib/exec_protos.h>						// Protos for FindTask() etc.
#include <clib/gadtools_protos.h>				// Protos for LayoutMenus() etc.
#include <clib/graphics_protos.h>				// Protos for SetAPen() etc.
#include <clib/intuition_protos.h>				// Protos for CloseScreen() etc.
#include <dos/dos.h>									// For RETURN_FAIL etc.
#include <dos/dosextens.h>							// For struct Process.
#include <graphics/gfxmacros.h>					// For SetAfPt() macro.
#include <intuition/intuition.h>					// For struct EasyStruct
#include <intuition/gadgetclass.h>				// For GA_Disabled.
#include <libraries/gadtools.h>					// For GTST_String
#include <pragmas/asl_pragmas.h>					// Pragmas for asl.library.
#include <pragmas/dos_pragmas.h>					// Pragmas for dos.library.
#include <pragmas/exec_pragmas.h>				// Pragmas for exec.library.
#include <pragmas/gadtools_pragmas.h>			// Pragmas for gadtools.library.
#include <pragmas/graphics_pragmas.h>			// Pragmas for graphics.library.
#include <pragmas/intuition_pragmas.h>			// Pragmas for intuition.library.
#include <stdlib.h>									// Proto for exit().
#include <string.h>									// Proto for strcpy ().

#include "WatcherPrefs.h"							// GadToolsBox generated.
#include "WatcherPrefs_.h"							// Revision header.
#include "/Include/Prefs.h"						// For struct WPrefs.

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                             D E F I N E S										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
#define VBYTE			0								// Defines for VolDisplayType.
#define VKILOBYTE		1								// do
#define VMEGABYTE		2								// do
#define VAUTO			3								// do

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                             G L O B A L S										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
UBYTE					VersTag []	= VERSTAG;		// Version string.
ULONG					What;

/*------------*/
/* Libraries: */
/*------------*/
struct DosLibrary	*DOSBase;						// dos.library.
struct Library		*AslBase	= NULL;				// asl.library.

/*-----------------*/
/* For CLI window: */
/*-----------------*/
struct Process		*p				= NULL;			// Our process.
struct Window		*WindowPtr	= NULL;			// CLI window.

/*--------------*/
/* Preferences: */
/*--------------*/
struct WPrefs		*WPrefs		= NULL;			// Preference structure.
struct WPrefs		*WPrefsBack	= NULL;			// Backup, so that we can restore values.
BPTR					PrefFile		= NULL;			// Preference file.

/*-------*/
/* Misc: */
/*-------*/
UWORD chip					WaitPointer [] =		// Designed by C=
{
	0x0000, 0x0000, 0x0400, 0x07C0, 0x0000, 0x07C0, 0x0100, 0x0380, 0x0000, 0x07E0,
	0x07C0, 0x1FF8, 0x1FF0, 0x3FEC, 0x3FF8, 0x7FDE, 0x3FF8, 0x7FBE, 0x7FFC, 0xFF7F,
	0x7EFC, 0xFFFF, 0x7FFC, 0xFFFF, 0x3FF8, 0x7FFE, 0x3FF8, 0x7FFE, 0x1FF0, 0x3FFC,
	0x07C0, 0x1FF8, 0x0000, 0x07E0, 0x0000, 0x0000
};
struct Requester			SleepReq;				// For SleepWindow() etc.

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                              P R O T O S										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void	OpenLibs				(void);
void	CloseLibs			(void);
void	AllocPrefs			(void);
BOOL	ReadPrefs			(UBYTE *fname);
BOOL	WritePrefs			(UBYTE *fname);
void	ParsePrefs			(void);
void	CleanExit			(UWORD ExitCode);

void	UsePrefsDefaults	(void);
void	CorrectGadgets		(struct WPrefs *wprefs);
BOOL	Inform				(struct Window *w, STRPTR Title, STRPTR Text, APTR args);
BOOL	RequestPrefFile	(UBYTE *startdir, UBYTE *startfile, UBYTE *resultpath, UBYTE *title);
void	SplitPath			(STRPTR FullPath, STRPTR PathPart, STRPTR NamePart);
BOOL	SleepWindow			(struct Window *w, struct Requester *r);
void	WakeUpWindow		(struct Window *w, struct Requester *r);

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                                M A I N											*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void main (void)
{
	/*-------------*/
	/* L O C A L S */
	/*-------------*/
	BOOL GoOn = TRUE;

	/*---------*/
	/* C O D E */
	/*---------*/
	/*-----------------------------------------------------------*/
	/* Finding a pointer to our task's window for EasyRequest(). */
	/*-----------------------------------------------------------*/
	p = (struct Process *) FindTask (NULL);
	WindowPtr = (struct Window *) p->pr_WindowPtr;

	/*-----------------*/
	/* Open libraries. */
	/*-----------------*/
	OpenLibs ();

	/*--------------------------------------------*/
	/* Allocate memory for preferences structure. */
	/*--------------------------------------------*/
	AllocPrefs ();

	/*-------------------------------------------------*/
	/* Read the preferences before opening the window. */
	/*-------------------------------------------------*/
	if (!ReadPrefs ("ENV:Watcher.Prefs"))
	{
		if (!ReadPrefs ("ENVARC:Watcher.Prefs"))
		{
			if (WindowPtr)
			{
				Inform (WindowPtr, "Warning", "!WARNING!\n"
														"Can't find Watcher preferences in the ENV:\n"
														"and ENVARC: directory. I'm using internal\n"
														"defaults.", NULL);
			}
			UsePrefsDefaults ();
		}
	}

	/*-------------------------------------------------*/
	/* Copy the contents for a later possible Restore. */
	/*-------------------------------------------------*/
	CopyMem (WPrefs, WPrefsBack, sizeof (struct WPrefs));

	/*---------------------------------------------*/
	/* Find public screen and allocate VisualInfo. */
	/*---------------------------------------------*/
	SetupScreen ();

	/*------------------*/
	/* Open our window. */
	/*------------------*/
	OpenWatcherPrefsWindow ();

	/*-------------------------------------*/
	/* Correct gadgets according to prefs. */
	/*-------------------------------------*/
	CorrectGadgets (WPrefs);

	/*-------------*/
	/* IDCMP loop. */
	/*-------------*/
	while (GoOn)
	{
		Wait (1L << WatcherPrefsWnd->UserPort->mp_SigBit);
		GoOn = HandleWatcherPrefsIDCMP ();
	}

	/*----------------*/
	/* Graceful exit. */
	/*----------------*/
	CleanExit (0);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                              OpenLibs()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void OpenLibs (void)
{
	/*-----------------------*/
	/* Open asl.library V37. */
	/*-----------------------*/
	if (!(AslBase = OpenLibrary ("asl.library", 37L)))
	{
		CleanExit (RETURN_FAIL);
	}

	/*----------------------------*/
	/* Open graphics.library V37. */
	/*----------------------------*/
	if (!(GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 37L)))
	{
		CleanExit (RETURN_FAIL);
	}
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                              CloseLibs()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void CloseLibs (void)
{
	if (AslBase)
	{
		CloseLibrary (AslBase);
	}
	if (GfxBase)
	{
		CloseLibrary ((struct Library *) GfxBase);
	}
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                             AllocPrefs()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void AllocPrefs (void)
{
	/*---------------------------------*/
	/* Allocate memory for main prefs. */
	/*---------------------------------*/
	if (!(WPrefs = (struct WPrefs *) AllocVec (sizeof (struct WPrefs), MEMF_ANY | MEMF_CLEAR)))
	{
		exit (RETURN_FAIL);
	}

	/*-------------------------------------------*/
	/* Allocate memory for backup/restore prefs. */
	/*-------------------------------------------*/
	if (!(WPrefsBack = (struct WPrefs *) AllocVec (sizeof (struct WPrefs), MEMF_ANY | MEMF_CLEAR)))
	{
		exit (RETURN_FAIL);
	}
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                              ReadPrefs()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
BOOL ReadPrefs (UBYTE *fname)
{
	/*----------------------------*/
	/* Open the preferences file. */
	/*----------------------------*/
	if (!(PrefFile = Open (fname, MODE_OLDFILE)))
	{
		return (FALSE);
	}

	/*---------------------------*/
	/* Read the preference file. */
	/*---------------------------*/
	if (Read (PrefFile, WPrefs, sizeof (struct WPrefs)) != sizeof (struct WPrefs))
	{
		Close (PrefFile);
		return (FALSE);
	}

	/*----------------------------*/
	/* Close the preference file. */
	/*----------------------------*/
	Close (PrefFile);

	return (TRUE);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                             WritePrefs()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
BOOL WritePrefs (UBYTE *fname)
{
	/*-------------------------------------------------------------------*/
	/* Before we write the preferences we must parse the string gadgets. */
	/* This is necessary, because, then, Watcher doesn't need to figure  */
	/* out how to extract the volume array from the string gadget.			*/
	/*-------------------------------------------------------------------*/
	ParsePrefs ();

	/*----------------------------*/
	/* Open the preferences file. */
	/*----------------------------*/
	if (!(PrefFile = Open (fname, MODE_READWRITE)))
	{
		return (FALSE);
	}

	/*---------------------------*/
	/* Read the preference file. */
	/*---------------------------*/
	if (Write (PrefFile, WPrefs, sizeof (struct WPrefs)) != sizeof (struct WPrefs))
	{
		Close (PrefFile);
		Inform (WindowPtr, "Warning", "!WARNING!\n"
												"Error writing preferences.\n", NULL);
		return (FALSE);
	}

	/*----------------------------*/
	/* Close the preference file. */
	/*----------------------------*/
	Close (PrefFile);

	return (TRUE);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                             ParsePrefs()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void ParsePrefs (void)
{
	/*-------------*/
	/* L O C A L S */
	/*-------------*/
	UBYTE *s, scopy [20];
	ULONG	SCount = 0, VCount = 0;

	/*---------*/
	/* C O D E */
	/*---------*/
	/*-------------------------------------------------------------*/
	/* First we parse wp_VolNameStrGad, and put the results in the */
	/* wp_VolName [][] array.													*/
	/*-------------------------------------------------------------*/
	s = WPrefs->wp_VolNameStrGad;

	while (*s)
	{
		switch (*s)
		{
			case ':' :
			{
				scopy [SCount++] = ':';								// Copy also ':' into scopy.
				scopy [SCount] = '\0';								// NULL terminate scopy.
				strcpy (WPrefs->wp_VolName [VCount], scopy);	// Copy volume name.
				scopy [SCount = 0] = '\0';							// NULL scopy.
				*s++;														// Advance one character.
				if (*s == '-')											// Check for indicator.
				{
					*s++;
					if ((*s == 'b') || (*s == 'B'))
					{
						WPrefs->wp_VolNameType [VCount] = VBYTE;
					}
					if ((*s == 'k') || (*s == 'K'))
					{
						WPrefs->wp_VolNameType [VCount] = VKILOBYTE;
					}
					if ((*s == 'm') || (*s == 'M'))
					{
						WPrefs->wp_VolNameType [VCount] = VMEGABYTE;
					}
					if ((*s == 'a') || (*s == 'A'))
					{
						WPrefs->wp_VolNameType [VCount] = VAUTO;
					}
				}
				VCount++;
				*s++;
				break;
			}
			case ',' :
			{
				*s++;
				scopy [SCount = 0] = '\0';
				break;
			}
			case '-' :
			{
				break;
			}
			default :
			{
				scopy [SCount++] = *s++;
				break;
			}
		}
	}
	WPrefs->wp_VolCount = VCount;

	s = WPrefs->wp_VolAlertStrGad;

	VCount = 0;
	while (*s)
	{
		LONG num;

		switch (*s)
		{
			case ':' :
			{
				scopy [SCount++] = ':';									// Copy also ':' into scopy.
				scopy [SCount] = '\0';									// NULL terminate scopy.
				strcpy (WPrefs->wp_VolAlert [VCount], scopy);	// Copy volume name.
				scopy [SCount = 0] = '\0';								// NULL scopy.
				*s++;															// Advance one character.
				if (*s == '-')												// Check for indicator.
				{
					*s++;
					if ((*s == 'b') || (*s == 'B'))
					{
						*s++;
						num = atoi (s);
						WPrefs->wp_VolAlertLimit [VCount] = num;
						while (*s && *s != ',')
						{
							*s++;
						}
					}
					if ((*s == 'k') || (*s == 'K'))
					{
						*s++;
						num = atoi (s);
						WPrefs->wp_VolAlertLimit [VCount] = num * 1024;
						while (*s && *s != ',')
						{
							*s++;
						}
					}
					if ((*s == 'm') || (*s == 'M'))
					{
						*s++;
						num = atoi (s);
						WPrefs->wp_VolAlertLimit [VCount] = num * 1048576;
						while (*s && *s != ',')
						{
							*s++;
						}
					}
				}
				else
				{
					*s++;
				}
				VCount++;
				break;
			}
			case ',' :
			{
				*s++;
				scopy [SCount = 0] = '\0';
				break;
			}
			case '-' :
			{
				break;
			}
			default :
			{
				scopy [SCount++] = *s++;
				break;
			}
		}
	}
	WPrefs->wp_VolAlertCount = VCount;
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                              CleanExit()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void CleanExit (UWORD ExitCode)
{
	/*-----------------------------*/
	/* User is done, we shut down. */
	/*-----------------------------*/
	CloseWatcherPrefsWindow ();
	CloseDownScreen ();

	/*--------------------*/
	/* Deallocate memory. */
	/*--------------------*/
	if (WPrefsBack)
	{
		FreeVec (WPrefsBack);
	}
	if (WPrefs)
	{
		FreeVec (WPrefs);
	}

	/*------------------*/
	/* Close libraries. */
	/*------------------*/
	CloseLibs ();
	exit (ExitCode);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                          UsePrefsDefaults()									*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void UsePrefsDefaults (void)
{
	WPrefs->wp_PrefsIdentifier			= 87808270;
	strcpy (WPrefs->wp_VolNameStrGad, "SYS:-a");
	WPrefs->wp_Total						= TRUE;
	WPrefs->wp_Fast						= FALSE;
	WPrefs->wp_Chip						= FALSE;
	WPrefs->wp_Largest					= FALSE;
	WPrefs->wp_LargestFast				= FALSE;
	WPrefs->wp_LargestChip				= FALSE;
	WPrefs->wp_DayOfWeek					= TRUE;
	WPrefs->wp_Date						= TRUE;
	WPrefs->wp_Time						= TRUE;
	WPrefs->wp_WinLeftEdge				= 0;
	WPrefs->wp_WinTopEdge				= 12;
	WPrefs->wp_WinWidth					= 640;
	WPrefs->wp_UpdateSec					= 1;
	WPrefs->wp_UpdateMic					= 0;
	WPrefs->wp_NoJumpGadget				= FALSE;
	WPrefs->wp_Cpu							= FALSE;
	strcpy (WPrefs->wp_PubScreen, "Workbench");
	strcpy (WPrefs->wp_GuideFile, "Watcher.guide");
	WPrefs->wp_VolumeFreeDirection	= FALSE;
	strcpy ((UBYTE *) WPrefs->wp_VolumeFreeDirectionInd, " ^v");
	strcpy ((UBYTE *) WPrefs->wp_VolAlertStrGad, "SYS:-k500");
	WPrefs->wp_AppInfoAutoStart		= TRUE;
	WPrefs->wp_AppInfoLeftEdge			= 0;
	WPrefs->wp_AppInfoTopEdge			= 0;
	strcpy (WPrefs->wp_Language, "english");
	WPrefs->wp_LanguageNum				= 1;
	WPrefs->wp_TimeFormat24				= TRUE;
	WPrefs->wp_DateFormat				= 0;
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                           CorrectGadgets()									*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void CorrectGadgets (struct WPrefs *wprefs)
{
	/*---------------------*/
	/* Correct GD_VOLUMES: */
	/*---------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_VOLUMES],
							 WatcherPrefsWnd, NULL,
							 GTST_String,		wprefs->wp_VolNameStrGad,
							 TAG_END);

	/*-------------------*/
	/* Correct GD_TOTAL: */
	/*-------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_TOTAL],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Total,
							 TAG_END);

	/*------------------*/
	/* Correct GD_FAST: */
	/*------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_FAST],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Fast,
							 TAG_END);

	/*------------------*/
	/* Correct GD_CHIP: */
	/*------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_CHIP],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Chip,
							 TAG_END);

	/*---------------------*/
	/* Correct GD_LARGEST: */
	/*---------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_LARGEST],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Largest,
							 TAG_END);

	/*-------------------------*/
	/* Correct GD_LARGESTFAST: */
	/*-------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_LARGESTFAST],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_LargestFast,
							 TAG_END);

	/*-------------------------*/
	/* Correct GD_LARGESTCHIP: */
	/*-------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_LARGESTCHIP],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_LargestChip,
							 TAG_END);

	/*-----------------------*/
	/* Correct GD_DAYOFWEEK: */
	/*-----------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_DAYOFWEEK],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_DayOfWeek,
							 TAG_END);

	/*------------------*/
	/* Correct GD_DATE: */
	/*------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_DATE],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Date,
							 TAG_END);

	/*------------------*/
	/* Correct GD_TIME: */
	/*------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_TIME],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Time,
							 TAG_END);

	/*-------------------------*/
	/* Correct GD_WINLEFTEDGE: */
	/*-------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_WINLEFTEDGE],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_WinLeftEdge,
							 TAG_END);

	/*------------------------*/
	/* Correct GD_WINTOPEDGE: */
	/*------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_WINTOPEDGE],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_WinTopEdge,
							 TAG_END);

	/*----------------------*/
	/* Correct GD_WINWIDTH: */
	/*----------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_WINWIDTH],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_WinWidth,
							 TAG_END);

	/*-----------------------*/
	/* Correct GD_UPDATESEC: */
	/*-----------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_UPDATESEC],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_UpdateSec,
							 TAG_END);

	/*-----------------------*/
	/* Correct GD_UPDATEMIC: */
	/*-----------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_UPDATEMIC],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_UpdateMic,
							 TAG_END);

	/*--------------------------*/
	/* Correct GD_NOJUMPGADGET: */
	/*--------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_NOJUMPGADGET],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_NoJumpGadget,
							 TAG_END);

	/*-----------------*/
	/* Correct GD_CPU: */
	/*-----------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_CPU],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_Cpu,
							 TAG_END);

	/*--------------------------*/
	/* Correct GD_PUBLICSCREEN: */
	/*--------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_PUBLICSCREEN],
							 WatcherPrefsWnd, NULL,
							 GTST_String,		wprefs->wp_PubScreen,
							 TAG_END);

	/*-----------------------*/
	/* Correct GD_GUIDEFILE: */
	/*-----------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_GUIDEFILE],
							 WatcherPrefsWnd, NULL,
							 GTST_String,		wprefs->wp_GuideFile,
							 TAG_END);

	/*---------------------------------*/
	/* Correct GD_VOLUMEFREEDIRECTION: */
	/*---------------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_VOLUMEFREEDIRECTION],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_VolumeFreeDirection,
							 TAG_END);

	/*-------------------------------------------*/
	/* Correct GD_VOLUMEFREEDIRECTIONINDICATORS: */
	/*-------------------------------------------*/
	if (WatcherPrefsGadgets [GD_VOLUMEFREEDIRECTION]->Flags & SELECTED)
	{
		WPrefs->wp_VolumeFreeDirection = TRUE;
		GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_VOLUMEFREEDIRECTIONINDICATORS],
								 WatcherPrefsWnd, NULL,
								 GA_Disabled, FALSE,
								 GTST_String, wprefs->wp_VolumeFreeDirectionInd,
								 TAG_END);
	}
	else
	{
		WPrefs->wp_VolumeFreeDirection = FALSE;
		GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_VOLUMEFREEDIRECTIONINDICATORS],
								 WatcherPrefsWnd, NULL,
								 GA_Disabled, TRUE,
								 GTST_String, wprefs->wp_VolumeFreeDirectionInd,
								 TAG_END);
	}

	/*------------------------*/
	/* Correct GD_VOLUMALERT: */
	/*------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_VOLUMALERT],
							 WatcherPrefsWnd, NULL,
							 GTST_String,		wprefs->wp_VolAlertStrGad,
							 TAG_END);

	/*------------------------------*/
	/* Correct GD_APPINFOAUTOSTART: */
	/*------------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_APPINFOAUTOSTART],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_AppInfoAutoStart,
							 TAG_END);

	/*-----------------------------*/
	/* Correct GD_APPINFOLEFTEDGE: */							 
	/*-----------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_APPINFOLEFTEDGE],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_AppInfoLeftEdge,
							 TAG_END);

	/*----------------------------*/
	/* Correct GD_APPINFOTOPEDGE: */
	/*----------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_APPINFOTOPEDGE],
							 WatcherPrefsWnd, NULL,
							 GTIN_Number,		wprefs->wp_AppInfoTopEdge,
							 TAG_END);

	/*----------------------*/
	/* Correct GD_LANGAUGE: */
	/*----------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_LANGUAGE],
							 WatcherPrefsWnd, NULL,
							 GTLV_Selected,	wprefs->wp_LanguageNum,
							 TAG_END);

	/*-------------------------*/
	/* Correct GD_TIME_FORMAT: */
	/*-------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_TIME_FORMAT],
							 WatcherPrefsWnd, NULL,
							 GTCB_Checked,		wprefs->wp_TimeFormat24,
							 TAG_END);

	/*-------------------------*/
	/* Correct GD_DATE_FORMAT: */
	/*-------------------------*/
	GT_SetGadgetAttrs (WatcherPrefsGadgets [GD_DATE_FORMAT],
							 WatcherPrefsWnd, NULL,
							 GTCY_Active,		wprefs->wp_DateFormat,
							 TAG_END);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                               Inform()											*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
BOOL Inform (struct Window *w, STRPTR Title, STRPTR Text, APTR args)
{
	/*-------------*/
	/* L O C A L S */
	/*-------------*/
	struct EasyStruct es;
	BOOL					rc;

	/*---------*/
	/* C O D E */
	/*---------*/
	SleepWindow (WatcherPrefsWnd, &SleepReq);
	if (IntuitionBase)
	{
		/*-------------------*/
		/* Build EasyStruct. */
		/*-------------------*/
		es.es_StructSize		= sizeof (struct EasyStruct);
		es.es_Flags				= 0;
		es.es_Title				= (Title) ? Title : (STRPTR) "Information";
		es.es_TextFormat		= Text;
		es.es_GadgetFormat	= "Okay";

		/*------------------------------------------------------*/
		/* Call EasyRequestArgs() and return it's return value. */
		/*------------------------------------------------------*/
		rc = EasyRequestArgs (w, &es, NULL, args);
		WakeUpWindow (WatcherPrefsWnd, &SleepReq);
		return (rc);
	}
	WakeUpWindow (WatcherPrefsWnd, &SleepReq);
	return (FALSE);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                           RequestPrefFile()									*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
BOOL RequestPrefFile (UBYTE *startdir, UBYTE *startfile, UBYTE *resultpath, UBYTE *title)
{
	/*-------------*/
	/* L O C A L S */
	/*-------------*/
	struct FileRequester	*freq	= NULL;
	BOOL						rc		= TRUE;

	/*---------*/
	/* C O D E */
	/*---------*/
	/*--------------------------------*/
	/* Make sure resultpath is empty. */
	/*--------------------------------*/
	resultpath [0] = '\0';

	/*-------------------------------------------*/
	/* Allocating file requester for asl.library */
	/*-------------------------------------------*/
	if (!(freq = AllocAslRequest (ASL_FileRequest, NULL)))
	{
		return (FALSE);
	}

	/*------------------*/
	/* Requesting file. */
	/*------------------*/
	if (AslRequestTags (freq,
							  ASLFR_Window,			(ULONG) WatcherPrefsWnd,
							  ASLFR_TitleText,		title,
							  ASLFR_InitialFile,		startfile,
							  ASLFR_InitialDrawer,	startdir))
	{
		// Building TGI->gi_HighFile...
		AddPart (resultpath, freq->rf_Dir, 256);
		AddPart (resultpath, freq->rf_File, 256);
	}
	else
	{
		rc = FALSE;
	}

	/*--------------------*/
	/* Deallocate memory. */
	/*--------------------*/
	if (freq)
	{
		FreeAslRequest (freq);
	}

	return (rc);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                              SplitPath()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void SplitPath (STRPTR FullPath, STRPTR PathPart, STRPTR NamePart)
{
	/*-------------*/
	/* L O C A L S */
	/*-------------*/
	UBYTE TempBuffer [256];
	UWORD	pos = 0;

	/*---------*/
	/* C O D E */
	/*---------*/
	/*------------------------*/
	/* First we split on ':'. */
	/*------------------------*/
	pos = SplitName (FullPath, ':', (UBYTE *) &TempBuffer [0], pos, 256);

	/*---------------------------------------------------------*/
	/* Checking for ':' separator, and append it if necessary. */
	/*---------------------------------------------------------*/
	if (pos == -1)
	{
		// No separator found.
		pos = 0;
	}
	else
	{
		// Separator found.
		strcat (PathPart, TempBuffer);
		strcat (PathPart, ":");
	}

	/*-----------------------------------------------*/
	/* Repeat SplitName until no more '/' are found. */
	/*-----------------------------------------------*/
	do
	{
		pos = SplitName (FullPath, '/', TempBuffer, pos, 256);
		if (pos != -1)
		{
			strcat (PathPart, TempBuffer);
			strcat (PathPart, "/");
		}
	} while (pos != -1);

	/*---------------------------------------------*/
	/* TempBuffer now contains the file name part. */
	/*---------------------------------------------*/
	strcpy (NamePart, TempBuffer);
	return;
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                             SleepWindow()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
BOOL SleepWindow (struct Window *w, struct Requester *r)
{
	/*------------------------------------------------*/
	/* Initializing user supplied requster structure. */
	/*------------------------------------------------*/
	InitRequester (r);

	/*---------------------------------*/
	/* Putting up the empty requester. */
	/*---------------------------------*/
	if (Request (r, w))
	{
		/*--------------------------------------------*/
		/* Success! We also implement a busy pointer! */
		/*--------------------------------------------*/
		SetPointer (w, WaitPointer, 16, 16, -6, 0);
		return (TRUE);
	}

	/*-------------------------*/
	/* Error, we return FALSE. */
	/*-------------------------*/
	return (FALSE);
}

/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
/*                            WakeUpWindow()										*/
/*лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл*/
void WakeUpWindow (struct Window *w, struct Requester *r)
{
	/*----------------------------*/
	/* Clearing the busy pointer. */
	/*----------------------------*/
	ClearPointer (w);

	/*-------------------------------------------------------------------*/
	/* Removing the empty requester. This reenables input in the window. */
	/*-------------------------------------------------------------------*/
	EndRequest (r, w);
}
