#define PREFERENCES_C

#include "common/extern.h"
#include "common/asyncio.h"
#include "common/subtasksupp.h"
#include "common/Grab_mcc.h"
#include "Config.h"

#include <MUI/NListview_mcc.h>

extern LONG request_message (char *msg);
extern ULONG pool_treshSize;


/// "Macros"
__inline int
maxmin (int i, int m, int M)
{
  if (i > M)
    i = M;
  else if (i < m)
    i = m;

  return (i);
}

///

/* Preferences suppor functions */

#ifndef __DEMO__

/// "BOOL LoadPass (struct preferences *tp)"
BOOL
LoadPass (struct preferences * tp)
{
  struct AsyncFile *fi = NULL;
  BOOL valid = FALSE;
  unsigned char s1, s2, s3, *p, count;
  unsigned short check = 0, checked;
  unsigned char *username = NULL, *sernum = NULL;       // then use from prefs...


  if (gd.keyfilename)
    {
      fi = OpenAsync (gd.keyfilename, MODE_READ, 4096);
      FreeVec (gd.keyfilename);
    }

  if (!fi)
    if (!(fi = OpenAsync ("GNetGrabber.key", MODE_READ, 4096)))
      fi = OpenAsync ("s:GNetGrabber.key", MODE_READ, 4096);

  if (fi)
    {
      if (ReadAsync (fi, &s1, 1) > 0)
        {
          if (ReadAsync (fi, &s3, 1) > 0)
            {
              if (ReadAsync (fi, &s2, 1) > 0)
                {
                  s1 ^= ~0;
                  s1 -= 37;

                  s2 ^= ~0;
                  s2 -= 46;

                  if ((count = (char) s1 + s2))
                    {
                      if ((username = (STRPTR) AllocVecPooled (gd.gd_pool, (ULONG) s1 + 1)))
                        {
                          if (ReadAsync (fi, username, (ULONG) s1) >= 1)
                            {
                              username[s1] = '\0';

                              count = s2;

                              for (p = username + s1 - 1; p >= username; p--)
                                {
                                  *p -= ++count + *(p + 1);
                                  *p ^= ~0;
                                  check += *p;
                                }

                              for (; s3 != 0; s3--)
                                ReadCharAsync (fi);

                              if ((sernum = (STRPTR) AllocVecPooled (gd.gd_pool, (ULONG) s2 + 1)))
                                {
                                  if (ReadAsync (fi, sernum, (ULONG) s2) >= 1)
                                    {
                                      sernum[s2] = '\0';

                                      count = 0;

                                      for (p = sernum + s2 - 1; p >= sernum; p--)
                                        {
                                          *p -= ++count + *(p + 1);
                                          *p ^= ~0;
                                          check += *p;
                                        }

                                      if (ReadAsync (fi, &checked, sizeof (unsigned short)) > 0)
                                        {
                                          if (check == checked)
                                            valid = TRUE;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

      CloseAsync (fi);
    }

  if (!valid)
    {
      if (username)
        {
          FreeVecPooled (gd.gd_pool, username);
          username = NULL;
        }
      if (sernum)
        {
          FreeVecPooled (gd.gd_pool, sernum);
          sernum = NULL;
        }
    }
  gd.username = username;
  gd.sernum = sernum;

  return (valid);
}
///

/// "struct preferences *LoadPrefs (void)"
struct preferences *
LoadPrefs (void)
{
  struct preferences *tprefs;
  ConfigClass cfg(gd.gd_pool, "configs/GNetGrab.cfg");
  const long reqcoords[] =
  {-1, -1, -1, -1};

  if ((tprefs = (struct preferences *) AllocSPooled (gd.gd_pool, sizeof (struct preferences))))
    {
      InitSemaphore (&tprefs->sema);

      if (LoadPass (tprefs))
        {
          if (cfg.Load () != CFG_READ_FAIL)
            {
              ObtainSemaphore (&tprefs->sema);

              tprefs->closereq = TRUE;

              tprefs->httpprefs.proxy = cfg.Get ("Proxy", (char *) NULL);

              tprefs->httpprefs.proxyport = cfg.Get ("Proxy-Port", 80);
              tprefs->httpprefs.proxyport = maxmin (tprefs->httpprefs.proxyport, 0, 99999);

              tprefs->httpprefs.proxyauth = cfg.Get ("Proxy-Authorization", (char *) NULL);
              tprefs->httpprefs.recdelay = cfg.Get ("Rec-Delay", 1);
              tprefs->httpprefs.recdelay = maxmin (tprefs->httpprefs.recdelay, 0, 9999);

              tprefs->httpprefs.numretries = cfg.Get ("Rec-Retries", 5);
              tprefs->httpprefs.numretries = maxmin (tprefs->httpprefs.numretries, 0, 9999);

              tprefs->httpprefs.recbuf = cfg.Get ("Rec-Buffer", 4);
              tprefs->httpprefs.recbuf = maxmin (tprefs->httpprefs.recbuf, 4, (int) pool_treshSize / 1024);

              tprefs->email = cfg.Get ("E-mail", (char *) NULL);

              cfg.Get ("Recents", tprefs->recents, 5, NULL);
              tprefs->recents[5] = NULL;

              cfg.Get ("RequesterCoords", (long *) &tprefs->req_top, 4, (long *) reqcoords);

              tprefs->httpprefs.languages = NULL;

              tprefs->httpprefs.charsets = NULL;

              ReleaseSemaphore (&tprefs->sema);

              return (tprefs);
            }
          else
            request_message (STRING (ERR_NOMEM, "Out of Memory"));
        }
      else
        request_message (STRING (ERR_NOKEY, "Keyfile Error"));

      FreeSPooled (gd.gd_pool, tprefs, sizeof (struct preferences));
    }
  else
    request_message (STRING (ERR_NOMEM, "Out of Memory"));

  return (NULL);
}

///

/// "ULONG SavePrefs (struct preferences * tprefs)"

ULONG
SavePrefs (struct preferences * tprefs)
{
  ConfigClass cfg(gd.gd_pool, "configs/GNetGrab.cfg");

  ObtainSemaphore (&tprefs->sema);

  if (cfg.Load () != CFG_READ_FAIL)
    {
      cfg.Set ("Proxy", tprefs->httpprefs.proxy);

      cfg.Set ("Proxy-Port", tprefs->httpprefs.proxyport);

      cfg.Set ("Proxy-Authorization", tprefs->httpprefs.proxyauth);

      cfg.Set ("Rec-Buffer", tprefs->httpprefs.recbuf);

      cfg.Set ("Rec-Delay", tprefs->httpprefs.recdelay);

      cfg.Set ("Rec-Retries", tprefs->httpprefs.numretries);

      cfg.Set ("E-mail", tprefs->email);

      cfg.Set ("Languages", tprefs->httpprefs.languages);

      cfg.Set ("Char-sets", tprefs->httpprefs.charsets);

      cfg.Set ("Recents", tprefs->recents, 4);

      cfg.Set ("RequesterCoords", &tprefs->req_top, 4);

      if (!cfg.Save ())
        error_req (STRING (MSG_OK, "*_OK"), "%s", STRING (ERR_CFGSAVE, "Error while saving config file"));
    }
  else
    error_req (STRING (MSG_OK, "*_OK"), "%s", STRING (ERR_NOMEM, "Out of Memory"));

  ReleaseSemaphore (&tprefs->sema);

  return (NULL);
}

///

#else /* DEMO VERSION */

/// "struct preferences *LoadPrefs (void)"

struct preferences *
LoadPrefs (void)
{
  struct preferences *tprefs;
  ConfigClass cfg;
  const long reqcoords[] =
  {-1, -1, -1, -1};

  gd.username = "\033bDEMO VERSION";
  gd.sernum = "\033bDEMO VERSION";

  if ((tprefs = (struct preferences *) AllocSPooled (gd.gd_pool, sizeof (struct preferences))))
    {
      InitSemaphore (&tprefs->sema);

          if (cfg.Load ("configs/GNetGrab.cfg") != CFG_READ_FAIL)
            {
              ObtainSemaphore (&tprefs->sema);

              tprefs->closereq = TRUE;
              tprefs->httpprefs.proxy = NULL;
              tprefs->httpprefs.proxyport = 80;
              tprefs->httpprefs.proxyauth = NULL;
              tprefs->httpprefs.recdelay = 15;
              tprefs->httpprefs.numretries = 10;
              tprefs->httpprefs.recbuf = 4;

              tprefs->email = cfg.Get ("E-mail", (char *) NULL);

              cfg.Get ("Recents", tprefs->recents, 5, NULL);
              tprefs->recents[5] = NULL;

              cfg.Get ("RequesterCoords", (long *) &tprefs->req_top, 4, (long *) reqcoords);

              tprefs->httpprefs.languages = NULL;

              tprefs->httpprefs.charsets = NULL;

              cfg.Dispose ();

              ReleaseSemaphore (&tprefs->sema);

              return (tprefs);
            }
          else
            request_message (STRING (ERR_NOMEM, "Out of Memory"));

      FreeSPooled (gd.gd_pool, tprefs, sizeof (struct preferences));
    }
  else
    request_message (STRING (ERR_NOMEM, "Out of Memory"));

  return (NULL);
}



/*
struct preferences *
LoadPrefs (void)
{
  struct preferences *tprefs;
  ConfigClass cfg;

  if ((tprefs = (struct preferences *) AllocSPooled (gd.gd_pool, sizeof (struct preferences))))
    {
      InitSemaphore (&tprefs->sema);

      if (cfg.Load ("configs/GNetGrab.cfg") != CFG_READ_FAIL)
        {

          ObtainSemaphore (&tprefs->sema);

          tprefs->closereq = FALSE;
          tprefs->socknum = 1;
          tprefs->downdir = "PROGDIR:net/";

          tprefs->httpprefs.proxy = NULL;
          tprefs->httpprefs.proxyport = 80;
          tprefs->httpprefs.proxyauth = NULL;

          tprefs->httpprefs.recdelay = 10;
          tprefs->httpprefs.numretries = 5;

          tprefs->httpprefs.recbuf = 4;
          tprefs->httpprefs.languages = NULL;
          tprefs->httpprefs.charsets = NULL;

          tprefs->email = NULL;

          tprefs->recents[0] = cfg.Get ("Recent1", (char *) NULL);
          tprefs->recents[1] = cfg.Get ("Recent2", (char *) NULL);
          tprefs->recents[2] = cfg.Get ("Recent3", (char *) NULL);
          tprefs->recents[3] = cfg.Get ("Recent4", (char *) NULL);
          tprefs->recents[4] = cfg.Get ("Recent5", (char *) NULL);

          tprefs->req_top = cfg.Get ("RequesterTop", -1);
          tprefs->req_left = cfg.Get ("RequesterLeft", -1);
          tprefs->req_width = cfg.Get ("RequesterWidth", -1);
          tprefs->req_height = cfg.Get ("RequesterHeight", -1);

          if (tprefs->downdir)
            tprefs->downdir = newstrcpy (gd.gd_pool, tprefs->downdir);

          if (tprefs->email)
            tprefs->email = newstrcpy (gd.gd_pool, tprefs->email);

          if (tprefs->httpprefs.proxy)
            tprefs->httpprefs.proxy = newstrcpy (gd.gd_pool, tprefs->httpprefs.proxy);

          if (tprefs->httpprefs.proxyauth)
            tprefs->httpprefs.proxyauth = newstrcpy (gd.gd_pool, tprefs->httpprefs.proxyauth);

          if (tprefs->recents[0])
            tprefs->recents[0] = newstrcpy (gd.gd_pool, tprefs->recents[0]);

          if (tprefs->recents[1])
            tprefs->recents[1] = newstrcpy (gd.gd_pool, tprefs->recents[1]);

          if (tprefs->recents[2])
            tprefs->recents[2] = newstrcpy (gd.gd_pool, tprefs->recents[2]);

          if (tprefs->recents[3])
            tprefs->recents[3] = newstrcpy (gd.gd_pool, tprefs->recents[3]);

          if (tprefs->recents[4])
            tprefs->recents[4] = newstrcpy (gd.gd_pool, tprefs->recents[4]);

          tprefs->recents[5] = NULL;

          cfg.Dispose ();

          ReleaseSemaphore (&tprefs->sema);

          return (tprefs);
        }
      else
        request_message (STRING (ERR_NOMEM, "Out of Memory"));

      FreeSPooled (gd.gd_pool, tprefs, sizeof (struct preferences));
    }
  else
    request_message (STRING (ERR_NOMEM, "Out of Memory"));

  return (NULL);
} */

///

#endif /* __DEMO__ */

/* WinGrab MUI class */

/// "mPrefsWin_New costants"

char *PrefsTextArray[] =
{
  NULL,
  NULL,
  NULL
};

static const char *languages_titles[] =
{
  "English",
  "English / United States",
  "English / United Kingdom",
  "French",
  "French / France",
  "French / Canada",
  "German",
  "Japanese",
  "Chinese",
  "Chinese / Cina",
  "Chinese / Taiwan",
  "Korean",
  "Italian",
  "Spanish",
  "Spanish / Spain",
  "Portuguese",
  "Portuguese / Brazil",

  NULL
};

static const char *languages_titles2[] =
{
  "en",
  "en-US",
  "en-UK",
  "fr",
  "fr-FR",
  "fr-CA",
  "de",
  "ja",
  "zh",
  "zh-CN",
  "zh-TW",
  "ko",
  "it",
  "es",
  "es-ES",
  "pt",
  "pt-BR",

  NULL
};

static const char *charsets_titles[] =
{
  "US-ASCII",
  "ISO-8859-1",
  "ISO-8859-2",
  "ISO-8859-3",
  "ISO-8859-4",
  "ISO-8859-5",
  "ISO-8859-6",
  "ISO-8859-7",
  "ISO-8859-8",
  "ISO-8859-9",
  "ISO-2022-JP",
  "ISO-2022-JP-2",
  "ISO-2022-KR",
  "UNICODE-1-1",
  "UNICODE-1-1-UTF-7",
  "UNICODE-1-1-UTF-8",

  NULL
};

///

/// "mPrefsWin_New"

ULONG
mPrefsWin_New (struct IClass *cl, Object * obj, struct opSet *msg)
{
  APTR REG_OBJ;
  APTR BT_Use, BT_Save, BT_Reset, BT_Cancel;
  APTR STR_EMAIL, STR_PROXY, STR_PROXYAUTH, NUM_PROXYPORT;

  APTR NUM_CDELAY, NUM_RETRIES;
  APTR NUM_BUFFER, LI_PREFS, LV_PREFS;


  if (!PrefsTextArray[0])
      {
  PrefsTextArray[0] = STRING (PREFS_HTTP, " HTTP ");
  PrefsTextArray[1] = STRING (PREFS_UserData, " User Data ");
       }

// *INDENT-OFF*
  obj = (Object *) DoSuperNew(cl, obj,
                                MUIA_Window_Title, STRING (PREFS_Wintitle, "Preferences"),
                                MUIA_Window_ID, MAKE_ID('P','W','I','N'),
                                MUIA_Window_NoMenus, TRUE,
                                MUIA_HelpNode, "WIN_PREFS",
                                WindowContents, VGroup,

                                    Child, ColGroup (2),

                                        Child, LV_PREFS = ListviewObject,
                                            MUIA_Listview_Input, TRUE,
                                            MUIA_Listview_MultiSelect, MUIV_Listview_MultiSelect_None,
                                            MUIA_Listview_List, LI_PREFS = ListObject,
                                                InputListFrame,
                                                MUIA_List_AdjustWidth, TRUE,
                                                MUIA_List_AutoVisible, TRUE,
                                                MUIA_List_Active, 0,
                                                MUIA_List_SourceArray, PrefsTextArray,
                                            End,
                                        End,



                                        Child, REG_OBJ = PageGroup,
                                            MUIA_Frame, MUIV_Frame_Virtual,
                                            MUIA_Background, MUII_PageBack,


                                            /*
                                            ***
                                            *** HTTP Section
                                            ***
                                             */

                                            Child, VGroup,
                                                InnerSpacing(2,2),

                                                Child, VGroup,
                                                    MUIA_Group_Columns, 2,
                                                    MUIA_Frame, MUIV_Frame_Group,
                                                    MUIA_Background, MUII_GroupBack,
                                                    MUIA_FrameTitle, STRING (PREFS_Proxyservs, "Proxy servers"),

                                                    Child, Label2(STRING (PREFS_Proxy, "Proxy")),
                                                    Child, STR_PROXY = StrDropObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_Proxy, "\33cInsert here your\nHTTP Proxy address"),
                                                    End,

                                                    Child, Label2(STRING (PREFS_ProxyPort, "Port")),
                                                    Child, NUM_PROXYPORT = StringObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_ProxyPort, "\33cInsert here your\nHTTP Proxy port"),
                                                        MUIA_Frame, MUIV_Frame_String,
                                                        MUIA_String_Accept, "0123456789",
                                                        MUIA_String_Integer, 80,
                                                        MUIA_String_MaxLen,  5,
                                                    End,

                                                    Child, Label2(STRING (PREFS_ProxyAuth, "Authoriz.")),
                                                    Child, STR_PROXYAUTH = StrDropObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_ProxyAuth, "\33cInsert here your\nHTTP Proxy authorization\npassword"),
                                                    End,
                                                End,

                                                Child, VGroup,
                                                    MUIA_Group_Columns, 3,
                                                    MUIA_Frame, MUIV_Frame_Group,
                                                    MUIA_Background, MUII_GroupBack,
                                                    MUIA_FrameTitle, STRING (PREFS_Connection, "Connection"),

                                                    Child, Label2 (STRING (PREFS_Buffer, "Buffer")),
                                                    Child, NUM_BUFFER = SliderObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_Buffer, "\33cSelect the size\nfor each process\nof downloading buffer\n(\33iin Kilobytes\33n)"),
                                                        MUIA_Slider_Horiz, TRUE,
                                                        MUIA_Slider_Max,      pool_treshSize / 1024,
                                                        MUIA_Slider_Min,      4,
                                                        MUIA_Slider_Level,    4,
                                                    End,
                                                    Child, Label2("Kbyte"),

                                                    Child, Label2 (STRING (PREFS_Reconnect, "Reconnect")),
                                                    Child, NUM_RETRIES = SliderObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_Retries, "\33cSelect the number of\nretries to connect"),
                                                        MUIA_Slider_Horiz, TRUE,
                                                        MUIA_Slider_Max,      99,//32,
                                                        MUIA_Slider_Min,      0,
                                                        MUIA_Slider_Level,    1,
                                                    End,
                                                    Child, Label2(STRING (PREFS_Rectimes, "times")),

                                                    Child, Label2(STRING (PREFS_Delay, "Delay")),
                                                    Child, NUM_CDELAY = StringObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_Delay, "\33cSelect the number of seconds\nthat will be delayed\nbefore reconnecting"),
                                                        MUIA_Frame, MUIV_Frame_String,
                                                        MUIA_String_Accept, "0123456789",
                                                        MUIA_String_Integer, 1L,
                                                        MUIA_String_MaxLen,  4,
                                                    End,
                                                    Child, Label2(STRING (PREFS_Secdelay, "secs")),

                                                End,
                                            End,

                                            /*
                                            ***
                                            *** User Data Section!
                                            ***
                                             */


                                            Child, VGroup,
                                                InnerSpacing(2,2),

                                                Child, VGroup,
                                                    MUIA_Background, MUII_GroupBack,
                                                    MUIA_Frame, MUIV_Frame_Group,
                                                    MUIA_FrameTitle, STRING (PREFS_Email, "Your e-mail address"),
                                                    //Child, Label2("Your e-mail address "),
                                                    Child, STR_EMAIL = StrDropObject,
                                                        MUIA_ShortHelp, STRING (ShortHelp_Email, "\33cInsert here your e-mail.\n\33bWarning\33n: This information will be sended\nover the internet!"),
                                                         //MUIA_HelpNode, "STR_URL",
                                                    End,
                                                End,

                                            End,

                                      End, //End PageGroup

                                  End,

                                  Child, HGroup,
                                      Child, BT_Use    = SimpleButton(STRING (MSG_Use,    "_Use")),
                                      Child, BT_Save   = SimpleButton(STRING (MSG_Save,   "_Save")),
                                      Child, BT_Reset  = SimpleButton(STRING (MSG_Reset,  "_Reset")),
                                      Child, BT_Cancel = SimpleButton(STRING (MSG_Cancel, "_Cancel")),
                                  End,

                                End,
                                TAG_MORE, msg->ops_AttrList);

// *INDENT-ON*

  if (obj)
    {
      register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);

      //ergo, set data
      data->GR_Prefs = REG_OBJ;
      data->LV_Prefs = LI_PREFS;

      data->STR_Proxy = STR_PROXY;
      data->NUM_ProxyPort = NUM_PROXYPORT;
      data->STR_ProxyAuth = STR_PROXYAUTH;
      data->NUM_CDelay = NUM_CDELAY;
      data->NUM_Retries = NUM_RETRIES;
      data->NUM_Buffer = NUM_BUFFER;

      data->STR_Email = STR_EMAIL;

      data->BT_Use = BT_Use;
      data->BT_Save = BT_Save;
      data->BT_Reset = BT_Reset;
      data->BT_Cancel = BT_Cancel;

      DoMethod ((Object *) BT_Use, MUIM_Notify, MUIA_Pressed, FALSE,
                obj, 2, MUIM_UrlEditWin_Use, NULL);

      DoMethod ((Object *) BT_Save, MUIM_Notify, MUIA_Pressed, FALSE,
                obj, 2, MUIM_UrlEditWin_Save, NULL);

      DoMethod ((Object *) BT_Reset, MUIM_Notify, MUIA_Pressed, FALSE,
                obj, 2, MUIM_UrlEditWin_Reset, NULL);

      DoMethod ((Object *) BT_Cancel, MUIM_Notify, MUIA_Pressed, FALSE,
                obj, 3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);

      DoMethod ((Object *) LV_PREFS, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
                obj, 1, MUIM_UrlEditWin_Change);

      DoMethod ((Object *) LV_PREFS, MUIM_List_Redraw, MUIV_List_Redraw_All);

      set (obj, MUIA_Window_DefaultObject, LV_PREFS);

      DoMethod (obj,
                MUIM_Window_SetCycleChain, LV_PREFS,

                STR_PROXY,
                NUM_PROXYPORT,
                STR_PROXYAUTH,
                NUM_BUFFER,
                NUM_RETRIES,
                NUM_CDELAY,

                STR_EMAIL,

                BT_Use,
                BT_Save,
                BT_Reset,
                BT_Cancel,
                NULL);


      DoMethod ((Object *) oa->App, OM_ADDMEMBER, obj);
      DoMethod (obj, MUIM_UrlEditWin_Reset, NULL);
      set (obj, MUIA_Window_Open, TRUE);

      return ((ULONG) obj);

    }

  D (printf ("prefswin: new error\n"));
  error_req (STRING (MSG_OK, "*_OK"), "%s", STRING (ERR_NOMEM, "Out of Memory"));

  return (NULL);
}
///

/// "mPrefsWin_Dispose"
ULONG
mPrefsWin_Dispose (struct IClass * cl, Object * obj, Msg msg)
{
  //register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);

  DoMethod ((Object *) oa->App, OM_REMMEMBER, obj);

  return (DoSuperMethodA (cl, obj, msg));
}
///

/// "mPrefsWin_Set"
ULONG
mPrefsWin_Set (struct IClass * cl, Object * obj, Msg msg)
{
  register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);

  struct TagItem *tags, *tag;
  for (tags = ((struct opSet *) msg)->ops_AttrList; (tag = (struct TagItem *) NextTagItem (&tags));)
    {
      switch (tag->ti_Tag)
        {
        case MUIA_Window_CloseRequest:
          if (tag->ti_Data == TRUE)
            {
              oa->prefs_win = NULL;
              DoMethod ((Object *) oa->App, MUIM_Application_PushMethod, obj, 2, OM_DISPOSE, NULL);
              return (NULL);
            }
          break;

        }
    }

  return (DoSuperMethodA (cl, obj, msg));
}
///

#ifndef __DEMO__

/// "mPrefsWin_Use"
ULONG
mPrefsWin_Use (struct IClass * cl, Object * obj, Msg msg)
{
  register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);
  char *buffer;
  LONG num_buffer;

  ObtainSemaphore (&prefs->sema);

  get (data->STR_Proxy, MUIA_String_Contents, &buffer);
  prefs->httpprefs.proxy = newstrcpy (gd.gd_pool, buffer);

  get (data->NUM_ProxyPort, MUIA_String_Integer, &num_buffer);
  prefs->httpprefs.proxyport = num_buffer;

  get (data->STR_ProxyAuth, MUIA_String_Contents, &buffer);
  prefs->httpprefs.proxyauth = newstrcpy (gd.gd_pool, buffer);


  // Connection settings
  get (data->NUM_Buffer, MUIA_Numeric_Value, &num_buffer);
  prefs->httpprefs.recbuf = num_buffer;

  get (data->NUM_Retries, MUIA_Numeric_Value, &num_buffer);
  prefs->httpprefs.numretries = num_buffer;

  get (data->NUM_CDelay, MUIA_String_Integer, &num_buffer);
  prefs->httpprefs.recdelay = num_buffer;


  get (data->STR_Email, MUIA_String_Contents, &buffer);
  prefs->email = newstrcpy (gd.gd_pool, buffer);

  ReleaseSemaphore (&prefs->sema);

  DoMethod ((Object *) oa->App, MUIM_Application_PushMethod, obj, 3,
            MUIM_Set, MUIA_Window_CloseRequest, TRUE);

  return (NULL);
}
///

/// "mPrefsWin_Save"
ULONG
mPrefsWin_Save (struct IClass * cl, Object * obj, Msg msg)
{
  register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);
  char *buffer;
  LONG num_buffer;

  ObtainSemaphore (&prefs->sema);

  get (data->STR_Proxy, MUIA_String_Contents, &buffer);
  prefs->httpprefs.proxy = newstrcpy (gd.gd_pool, buffer);

  get (data->NUM_ProxyPort, MUIA_String_Integer, &num_buffer);
  prefs->httpprefs.proxyport = num_buffer;

  get (data->STR_ProxyAuth, MUIA_String_Contents, &buffer);
  prefs->httpprefs.proxyauth = newstrcpy (gd.gd_pool, buffer);


  // Connection settings
  get (data->NUM_Buffer, MUIA_Numeric_Value, &num_buffer);
  prefs->httpprefs.recbuf = num_buffer;

  get (data->NUM_Retries, MUIA_Numeric_Value, &num_buffer);
  prefs->httpprefs.numretries = num_buffer;

  get (data->NUM_CDelay, MUIA_String_Integer, &num_buffer);
  prefs->httpprefs.recdelay = num_buffer;


  get (data->STR_Email, MUIA_String_Contents, &buffer);
  prefs->email = newstrcpy (gd.gd_pool, buffer);

  ReleaseSemaphore (&prefs->sema);

  SavePrefs (prefs);

  return ((ULONG) DoMethod ((Object *) oa->App, MUIM_Application_PushMethod, obj, 3, MUIM_Set, MUIA_Window_CloseRequest, TRUE));

  return (NULL);
}
///

#else

/// "mPrefsWin_Use"
ULONG
mPrefsWin_Use (struct IClass * cl, Object * obj, Msg msg)
{
  //register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);
  error_req (STRING (MSG_OK, "*_OK"), "\033c%s", STRING (DEMO_NOPREFS, "In this DEMO version\nyou can't change prefs!"));
  return (NULL);
}
///

#endif

/// "mPrefsWin_Reset"
ULONG
mPrefsWin_Reset (struct IClass * cl, Object * obj, Msg msg)
{
  register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);

  char *null_str = "";

  ObtainSemaphore (&prefs->sema);

  if (data->STR_Proxy)
    set (data->STR_Proxy, MUIA_String_Contents, prefs->httpprefs.proxy);
  else
    set (data->STR_Proxy, MUIA_String_Contents, null_str);

  set (data->NUM_ProxyPort, MUIA_String_Integer, prefs->httpprefs.proxyport);

  if (data->STR_ProxyAuth)
    set (data->STR_ProxyAuth, MUIA_String_Contents, prefs->httpprefs.proxyauth);
  else
    set (data->STR_ProxyAuth, MUIA_String_Contents, null_str);

  set (data->NUM_Buffer, MUIA_Numeric_Value, prefs->httpprefs.recbuf);
  set (data->NUM_Retries, MUIA_Numeric_Value, prefs->httpprefs.numretries);
  set (data->NUM_CDelay, MUIA_String_Integer, prefs->httpprefs.recdelay);

  if (data->STR_Email)
    set (data->STR_Email, MUIA_String_Contents, prefs->email);
  else
    set (data->STR_Email, MUIA_String_Contents, null_str);

  ReleaseSemaphore (&prefs->sema);

  return (NULL);
}
///

/// "mPrefsWin_Change"
ULONG
mPrefsWin_Change (struct IClass * cl, Object * obj, Msg msg)
{
  register struct PrefsWinData *data = (struct PrefsWinData *) INST_DATA (cl, obj);

  LONG active;

  get ((Object *) data->LV_Prefs, MUIA_List_Active, &active);

  set ((Object *) data->GR_Prefs, MUIA_Group_ActivePage, active);

  return (0);
}

///

/// "PrefsWin_Dispatcher"
SAVEDS (ULONG)
PrefsWin_Dispatcher (REG (a0, struct IClass * cl), REG (a2, Object * obj), REG (a1, Msg msg))
{
  switch (msg->MethodID)
    {
    case OM_NEW:
      return ((ULONG) mPrefsWin_New (cl, obj, (struct opSet *) msg));

    case OM_DISPOSE:
      return ((ULONG) mPrefsWin_Dispose (cl, obj, (Msg) msg));

    case OM_SET:
      return ((ULONG) mPrefsWin_Set (cl, obj, (Msg) msg));

    case MUIM_UrlEditWin_Reset:
      return ((ULONG) mPrefsWin_Reset (cl, obj, (Msg) msg));

#ifndef __DEMO__

    case MUIM_UrlEditWin_Use:
      return ((ULONG) mPrefsWin_Use (cl, obj, (Msg) msg));

    case MUIM_UrlEditWin_Save:
      return ((ULONG) mPrefsWin_Save (cl, obj, (Msg) msg));

#else

    case MUIM_UrlEditWin_Use:
    case MUIM_UrlEditWin_Save:
      return ((ULONG) mPrefsWin_Use (cl, obj, (Msg) msg));

#endif


    case MUIM_UrlEditWin_Change:
      return ((ULONG) mPrefsWin_Change (cl, obj, (Msg) msg));
    }

  return (DoSuperMethodA (cl, obj, msg));
}
///
