home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / on-line / amster / src / prefs.c < prev    next >
C/C++ Source or Header  |  2000-01-21  |  10KB  |  360 lines

  1. /*
  2. ** Preferences
  3. */
  4.  
  5. #include "config.h"
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include "mui.h"
  12. #include <libraries/asl.h>
  13.  
  14. #include "gui.h"
  15. #include "search.h"
  16. #include "prefs.h"
  17. #include "info.h"
  18. #include "amster_Cat.h"
  19.  
  20. ULONG prf_new(struct IClass *cl, Object *obj, struct opSet *msg);
  21. void _prf_set(struct prfdata *data);
  22. void _prf_use(struct prfdata *data);
  23.  
  24. /* defaults for preferences */
  25. char *pref_mainserver="server.napster.com:8875";
  26. char *pref_server=NULL;
  27. char *pref_napvers="V2.0 BETA 3";
  28. char *pref_user="luser42";
  29. char *pref_pass="pussy";
  30. char *pref_email="anon@napster.com";
  31. char *pref_dlpath="ram:";
  32. int pref_link=3;
  33. int pref_regflag=1;
  34. int pref_clrlist=0;
  35. int pref_autocon=0;
  36. int pref_fullpath=0;
  37.  
  38.  
  39. MUIF prf_dispatch(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  40. {
  41.     switch(msg->MethodID) {
  42.         case OM_NEW: return(prf_new(cl,obj,(APTR)msg));
  43.         case PRF_UPDATE:
  44.             {
  45.             if(gui_napon) nap_updateaccount();
  46.             else MUI_Request(gui->app, gui->swin, 0L, (char *)MSG_ERROR_TITLE, (char *)MSG_OK_GAD, (char *)MSG_ERROR_UPDATEEMAIL);
  47.             return(NULL);
  48.             }
  49.         case PRF_OPEN:
  50.             {
  51.             struct prfdata *data = INST_DATA(cl,obj);
  52.             _prf_set(data);
  53.             set(obj,MUIA_Window_Open,TRUE);
  54.             return(NULL);
  55.             }
  56.         case PRF_USE:
  57.             {
  58.             struct prfdata *data = INST_DATA(cl,obj);
  59.             _prf_use(data);
  60.             set(obj,MUIA_Window_Open,FALSE);
  61.             return(NULL);
  62.             }
  63.         case PRF_SAVE:
  64.             {
  65.             struct prfdata *data = INST_DATA(cl,obj);
  66.             _prf_use(data);
  67.             set(obj,MUIA_Window_Open,FALSE);
  68.             prf_save();
  69.             return(NULL);
  70.             }
  71.         case PRF_CANCEL:
  72.             set(obj,MUIA_Window_Open,FALSE);
  73.             return(NULL);
  74.     }
  75.     return(DoSuperMethodA(cl,obj,msg));
  76. }
  77.  
  78.  
  79. ULONG prf_new(struct IClass *cl, Object *obj, struct opSet *msg)
  80. {
  81.     struct prfdata *data;
  82.     Object *idstr,*pwstr,*emailstr,*emailbut,*pathstr,*links,*clrtog,*msstr,*sstr,*vstr,*savbut,*usebut,*canbut,*autotog,*fptog;
  83.     static char *PrefsPages[] = {
  84.         (char *)_MSG_GROUP_USER,
  85.         (char *)_MSG_GROUP_OPTIONS,
  86.         (char *)_MSG_GROUP_ADVANCED,
  87.         NULL
  88.     };
  89.  
  90.     localize_array(PrefsPages);
  91.  
  92.     if (obj = (Object *)DoSuperNew(cl,obj,
  93.         MUIA_HelpNode, "config",
  94.         WindowContents, VGroup,
  95.             Child, RegisterGroup(PrefsPages),
  96.             Child, ColGroup(2),
  97.                 MUIA_HelpNode, "conf-user",
  98.                 Child, Label2(MSG_USERNAME),
  99.                 Child, idstr = StringObject,
  100.                     StringFrame,
  101.                     MUIA_String_AdvanceOnCR, TRUE,
  102.                     MUIA_CycleChain, 1,
  103.                 End,
  104.                 Child, Label2(MSG_PASSWORD),
  105.                 Child, pwstr = StringObject,
  106.                     StringFrame,
  107.                     MUIA_String_AdvanceOnCR, TRUE,
  108.                     MUIA_String_Secret, TRUE,
  109.                     MUIA_CycleChain, 1,
  110.                 End,
  111.                 Child, Label2(MSG_EMAIL),
  112.                 Child, HGroup,
  113.                     Child, emailstr = StringObject,
  114.                         StringFrame,
  115.                         MUIA_String_AdvanceOnCR, TRUE,
  116.                         MUIA_CycleChain, 1,
  117.                     End,
  118.                     Child, emailbut = SimpleButton(MSG_UPDATE_GAD),
  119.                 End,
  120.                 Child, Label2(MSG_CONNECTION),
  121.                 Child, links = CycleObject,
  122.                     MUIA_Cycle_Active, pref_link,
  123.                     MUIA_Cycle_Entries, nap_linktype,
  124.                 End,
  125.                 Child, Label2(MSG_DOWNLOADPATH),
  126.                 Child, pathstr = PopaslObject,
  127.                     MUIA_Popstring_String, StringObject,
  128.                         StringFrame,
  129.                         MUIA_String_MaxLen, 256,
  130.                         MUIA_CycleChain, 1,
  131.                     End,
  132.                     MUIA_Popstring_Button, PopButton(MUII_PopFile),
  133.                     MUIA_Popasl_Type, ASL_FileRequest,
  134.                     ASLFR_TitleText, MSG_DOWNLOADPATH_REQ,
  135.                     ASLFR_DrawersOnly, TRUE,
  136.                 End,
  137.             End,
  138.             Child, ColGroup(2),
  139.                 MUIA_HelpNode, "conf-opts",
  140.                 Child, clrtog = ImageObject,
  141.                     MUIA_HorizWeight, 10,
  142.                     ButtonFrame,
  143.                     MUIA_Background, MUII_ButtonBack,
  144.                     MUIA_InputMode, MUIV_InputMode_Toggle,
  145.                     MUIA_ShowSelState, FALSE,
  146.                     MUIA_CycleChain, 1,
  147.                     MUIA_Image_Spec, "6:15",
  148.                 End,
  149.                 Child, TextObject,
  150.                     MUIA_HorizWeight, 90,
  151.                     MUIA_Text_Contents, MSG_CLEARLIST,
  152.                 End,
  153.                 Child, autotog = ImageObject,
  154.                     MUIA_HorizWeight, 10,
  155.                     ButtonFrame,
  156.                     MUIA_Background, MUII_ButtonBack,
  157.                     MUIA_InputMode, MUIV_InputMode_Toggle,
  158.                     MUIA_ShowSelState, FALSE,
  159.                     MUIA_CycleChain, 1,
  160.                     MUIA_Image_Spec, "6:15",
  161.                 End,
  162.                 Child, TextObject,
  163.                     MUIA_HorizWeight, 90,
  164.                     MUIA_Text_Contents, MSG_AUTOCONNECT,
  165.                 End,
  166.                 Child, fptog = ImageObject,
  167.                     MUIA_HorizWeight, 10,
  168.                     ButtonFrame,
  169.                     MUIA_Background, MUII_ButtonBack,
  170.                     MUIA_InputMode, MUIV_InputMode_Toggle,
  171.                     MUIA_ShowSelState, FALSE,
  172.                     MUIA_CycleChain, 1,
  173.                     MUIA_Image_Spec, "6:15",
  174.                 End,
  175.                 Child, TextObject,
  176.                     MUIA_HorizWeight, 90,
  177.                     MUIA_Text_Contents, MSG_FULLPATHS,
  178.                 End,
  179.             End,
  180.             Child, ColGroup(2),
  181.                 MUIA_HelpNode, "conf-adv",
  182.                 Child, Label2(MSG_MAINSERVER),
  183.                 Child, msstr = StringObject,
  184.                     StringFrame,
  185.                     MUIA_String_AdvanceOnCR, TRUE,
  186.                     MUIA_CycleChain, 1,
  187.                     MUIA_ShortHelp, MSG_MAINSERVER_HELP,
  188.                 End,
  189.                 Child, Label2(MSG_MP3SERVER),
  190.                 Child, sstr = StringObject,
  191.                     StringFrame,
  192.                     MUIA_String_AdvanceOnCR, TRUE,
  193.                     MUIA_CycleChain, 1,
  194.                     MUIA_ShortHelp, MSG_MP3SERVER_HELP,
  195.                 End,
  196.                 Child, Label2(MSG_VERSION),
  197.                 Child, vstr = StringObject,
  198.                     StringFrame,
  199.                     MUIA_CycleChain, 1,
  200.                 End,
  201.             End,
  202.             End,
  203.             Child, HGroup,
  204.                 Child, savbut = SimpleButton(MSG_SAVE_GAD),
  205.                 Child, usebut = SimpleButton(MSG_USE_GAD),
  206.                 Child, canbut = SimpleButton(MSG_CANCEL_GAD),
  207.             End,
  208.  
  209.         End,
  210.         TAG_MORE, msg->ops_AttrList))
  211.     {
  212.         data = INST_DATA(cl,obj);
  213.         data->idstr = idstr;
  214.         data->pwstr = pwstr;
  215.         data->emailstr = emailstr;
  216.         data->pathstr = pathstr;
  217.         data->links = links;
  218.         data->clrtog = clrtog;
  219.         data->autotog = autotog;
  220.         data->fptog = fptog;
  221.         data->msstr = msstr;
  222.         data->sstr = sstr;
  223.         data->vstr = vstr;
  224.  
  225.         set(obj,MUIA_Window_ID,MAKE_ID('S','E','T','T'));
  226.         set(obj,MUIA_Window_Title,MSG_SETTINGS_TITLE),
  227.  
  228.         DoMethod(emailbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,PRF_UPDATE);
  229.         DoMethod(canbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,PRF_CANCEL);
  230.         DoMethod(obj,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,obj,1,PRF_CANCEL);
  231.         DoMethod(usebut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,PRF_USE);
  232.         DoMethod(savbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,PRF_SAVE);
  233.  
  234.         return((ULONG)obj);
  235.     }
  236.     return(0);
  237. }
  238.  
  239.  
  240. void _prf_set(struct prfdata *data)
  241. {
  242.     set(data->msstr,MUIA_String_Contents,pref_mainserver);
  243.     set(data->sstr,MUIA_String_Contents,pref_server);
  244.     set(data->vstr,MUIA_String_Contents,pref_napvers);
  245.     set(data->idstr,MUIA_String_Contents,pref_user);
  246.     set(data->pwstr,MUIA_String_Contents,pref_pass);
  247.     set(data->emailstr,MUIA_String_Contents,pref_email);
  248.     set(data->pathstr,MUIA_String_Contents,pref_dlpath);
  249.     set(data->links,MUIA_Cycle_Active,pref_link);
  250.     set(data->clrtog,MUIA_Selected,pref_clrlist);
  251.     set(data->autotog,MUIA_Selected,pref_autocon);
  252.     set(data->fptog,MUIA_Selected,pref_fullpath);
  253. }
  254.  
  255.  
  256. void _prf_use(struct prfdata *data)
  257. {
  258.     u_long tmp;
  259.     int oldfp;
  260.  
  261.     GetAttr(MUIA_String_Acknowledge,data->msstr,&tmp);
  262.     if (((char*)tmp)[0]==0) pref_mainserver=NULL; else pref_mainserver=strdup((char*)tmp);
  263.  
  264.     GetAttr(MUIA_String_Acknowledge,data->sstr,&tmp);
  265.     if (((char*)tmp)[0]==0) pref_server=NULL; else pref_server=strdup((char*)tmp);
  266.  
  267.     GetAttr(MUIA_String_Acknowledge,data->vstr,&tmp);
  268.     if (((char*)tmp)[0]==0) pref_napvers=NULL; else pref_napvers=strdup((char*)tmp);
  269.  
  270.     GetAttr(MUIA_String_Acknowledge,data->idstr,&tmp);
  271.     if (((char*)tmp)[0]==0) pref_user=NULL; else pref_user=strdup((char*)tmp);
  272.  
  273.     GetAttr(MUIA_String_Acknowledge,data->pwstr,&tmp);
  274.     if (((char*)tmp)[0]==0) pref_pass=NULL; else pref_pass=strdup((char*)tmp);
  275.  
  276.     GetAttr(MUIA_String_Acknowledge,data->emailstr,&tmp);
  277.     if (((char*)tmp)[0]==0) pref_email=NULL; else pref_email=strdup((char*)tmp);
  278.  
  279.     GetAttr(MUIA_String_Acknowledge,data->pathstr,&tmp);
  280.     if (((char*)tmp)[0]==0) pref_dlpath=NULL; else pref_dlpath=strdup((char*)tmp);
  281.  
  282.     GetAttr(MUIA_Cycle_Active,data->links,&tmp);
  283.     pref_link = tmp;
  284.  
  285.     GetAttr(MUIA_Selected,data->clrtog,&tmp);
  286.     pref_clrlist = tmp;
  287.  
  288.     GetAttr(MUIA_Selected,data->autotog,&tmp);
  289.     pref_autocon = tmp;
  290.  
  291.     oldfp = pref_fullpath;
  292.     GetAttr(MUIA_Selected,data->fptog,&tmp);
  293.     pref_fullpath = tmp;
  294.     if(pref_fullpath!=oldfp) DoMethod(gui->searchpanel,SEARCH_REFRESH);
  295. }
  296.  
  297.  
  298. void prf_load(void)
  299. {
  300.     FILE *f;
  301.     char tmp[100],*s;
  302.  
  303.     f=fopen(AMSTER_PREFS,"r");
  304.     if(f==0) return;
  305.  
  306.     while(fgets(tmp,sizeof(tmp),f)) {
  307.         s = strtok(tmp," ");
  308.         if(!s) {
  309.             continue;
  310.         } else if(strcmp(s,"mainserver")==0) {
  311.             if(s=strtok(NULL,"\n")) pref_mainserver=strdup(s);
  312.         } else if(strcmp(s,"server")==0) {
  313.             if(s=strtok(NULL,"\n")) pref_server=strdup(s);
  314.         } else if(strcmp(s,"napvers")==0) {
  315.             if(s=strtok(NULL,"\n")) pref_napvers=strdup(s);
  316.         } else if(strcmp(s,"user")==0) {
  317.             if(s=strtok(NULL,"\n")) pref_user=strdup(s);
  318.         } else if(strcmp(s,"pass")==0) {
  319.             if(s=strtok(NULL,"\n")) pref_pass=strdup(s);
  320.         } else if(strcmp(s,"email")==0) {
  321.             if(s=strtok(NULL,"\n")) pref_email=strdup(s);
  322.         } else if(strcmp(s,"dlpath")==0) {
  323.             if(s=strtok(NULL,"\n")) pref_dlpath=strdup(s);
  324.         } else if(strcmp(s,"linktype")==0) {
  325.             if(s=strtok(NULL,"\n")) pref_link=atoi(s);
  326.         } else if(strcmp(s,"optclrlist")==0) {
  327.             pref_clrlist = 1;
  328.         } else if(strcmp(s,"autoconnect")==0) {
  329.             pref_autocon = 1;
  330.         } else if(strcmp(s,"fullpath")==0) {
  331.             pref_fullpath = 1;
  332.         }
  333.     }
  334.     fclose(f);
  335. }
  336.  
  337.  
  338. void prf_save(void)
  339. {
  340.     FILE *f;
  341.  
  342.     f=fopen(AMSTER_PREFS,"w");
  343.     if(f==0) {
  344.         gui_debug((char *)MSG_ERROR_SAVE);
  345.         return;
  346.     }
  347.     if(pref_mainserver) fprintf(f,"mainserver %s\n",pref_mainserver);
  348.     if(pref_server) fprintf(f,"server %s\n",pref_server);
  349.     if(pref_napvers) fprintf(f,"napvers %s\n",pref_napvers);
  350.     if(pref_user) fprintf(f,"user %s\n",pref_user);
  351.     if(pref_pass) fprintf(f,"pass %s\n",pref_pass);
  352.     if(pref_email) fprintf(f,"email %s\n",pref_email);
  353.     if(pref_dlpath) fprintf(f,"dlpath %s\n",pref_dlpath);
  354.     fprintf(f,"linktype %d\n",pref_link);
  355.     if(pref_clrlist) fprintf(f,"optclrlist \n");
  356.     if(pref_autocon) fprintf(f,"autoconnect \n");
  357.     if(pref_fullpath) fprintf(f,"fullpath \n");
  358.     fclose(f);
  359. }
  360.