home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / discs / a / backup / !backup / h / wimpy < prev   
Encoding:
Text File  |  1994-02-26  |  13.7 KB  |  539 lines

  1. #include "wimp.h"
  2. #include "wimpt.h"
  3. #include "win.h"
  4. #include "event.h"
  5. #include "baricon.h"
  6. #include "res.h"
  7. #include "resspr.h"
  8. #include "menu.h"
  9. #include "template.h"
  10. #include "dbox.h"
  11. #include "werr.h"
  12. #include "xferrecv.h"
  13. #include "kernel.h"
  14. #include "alarm.h"
  15. #include "interface.h"
  16. #define menu_info     1
  17. #define menu_status   2
  18. #define menu_over     3
  19. #define menu_auto     4
  20. #define menu_time     5
  21. #define menu_start_at 6  
  22. #define menu_quit     7
  23.  
  24. #define info_field    4
  25. #define author_field 3
  26. #define file_field    4
  27. #define backup_number_field 3
  28. #define overwrite_field 1
  29. #define time_field 2
  30. /**********wimp************/
  31. static BOOL initialise(void);
  32. static BOOL window_open = FALSE;
  33. static BOOL status_window = FALSE;
  34. static BOOL overwrite = FALSE;
  35. static BOOL autobackup = FALSE;
  36. static dbox info_dialogue;
  37. static dbox time_dialogue;
  38. static dbox main_dialogue;
  39. static dbox start_dialogue;
  40. static char *Version_String = "1.01 31th Oct '91";
  41. static char *Author = "Briag";
  42. char Time[] = "Off";
  43. char Overwrite[] = "Off";
  44. char backup_number[4];
  45. int timer = 5;
  46. static menu top_menu;
  47. static void iconclick(wimp_i icon);
  48. static void program_info(void);
  49. static void program_status(void);
  50. static void program_time(void);
  51. static void program_start_at(void);
  52. static void menuproc(void *handle, char *hit);
  53. static void event_handler(wimp_eventstr *e, void *handle);
  54. static void event_handler2(wimp_eventstr *e, void *handle);
  55. static void redraw_window(wimp_w handle);
  56. static void alarm_proc(int at,void *handle);
  57. static void status_handler(wimp_mousestr);
  58. void workarea_button (wimp_mousestr);
  59. /****************************/
  60.  
  61. BOOL dialogue_raw_event (dialogue_box, action, handle)
  62.      
  63.      dbox dialogue_box;
  64.      wimp_eventstr *action;
  65.      void *handle;
  66.  
  67.      {
  68.  
  69.           switch (action->e)
  70.           {
  71.                case wimp_EREDRAW:
  72.                     redraw_window (action->data.o.w);
  73.                     break;
  74.                     
  75.                case wimp_EOPEN:
  76.                     wimpt_noerr (wimp_open_wind (&action->data.o));
  77.                     break;
  78.                     
  79.                case wimp_ECLOSE:
  80.                     dbox_dispose (&dialogue_box);
  81.                     break;
  82.  
  83.                case wimp_EBUT:
  84.                   if (action->data.but.m.bbits != 2){
  85.                      workarea_button (action->data.but.m);
  86.                      }
  87.                   if(status_window){
  88.                      status_handler(action->data.but.m);
  89.                      }
  90.                   break;
  91.  
  92.          }
  93.      return TRUE;
  94.      }
  95.  
  96.  
  97. static BOOL initialise(void)
  98. {
  99. FILE *fp;
  100. int result,i;              
  101. char file[62],*ptr = file_name;
  102.  
  103. _kernel_osfile_block inouts,*inout;
  104. inout = &inouts;
  105. wimpt_init("Progy Backeruperer");     /* Main Wimp initialisation */
  106. res_init("Backup");                 /* Resources */
  107. resspr_init();                      /* Application sprites */
  108. template_init();                    /* Templates */
  109. dbox_init();                        /* Dialogue boxes */
  110. alarm_init();
  111.  
  112. if (top_menu = menu_new("backup", ">Info,Status,Overwrite,Auto backup,>Every,>Start at,Quit"),top_menu ==   NULL)
  113.    return FALSE;
  114.  
  115. baricon("!backup", (int)resspr_area(), iconclick);    
  116.  
  117. win_register_event_handler(win_ICONBAR,event_handler2,0);
  118. win_register_event_handler(win_ICONBARLOAD,event_handler,0);
  119. if(!event_attachmenu(win_ICONBAR, top_menu, menuproc, 0)){
  120.    return FALSE; 
  121.    }  
  122.                
  123.  
  124. /***backup init***/
  125. if((fp = fopen(current,"r")) == NULL){
  126.    strcpy(backup_number,"0");
  127.    fclose(fp);
  128.    }
  129. else{
  130.    fscanf(fp,"%s",backup_number);
  131.    fclose(fp);
  132.    }
  133.  
  134. /*** read in full path name***/
  135. if((fp = fopen(file_path,"r")) == NULL){
  136.    fclose(fp);
  137.    strcpy(file_name,"None as yet");
  138.    }
  139. else{
  140.    fscanf(fp,"%s",file_name);
  141.    fclose(fp);
  142.    if((i = strlen(file_name)) > 59){
  143.       i -= 59;
  144.       for(;i > 0;i--,ptr++)
  145.          ;
  146.       strcpy(file,ptr);
  147.       strcpy(short_file_name,file);
  148.       }
  149.    else{
  150.       strcpy(short_file_name,file_name);
  151.       }
  152.    }
  153. inout->load=0xffd,inout->exec=0,inout->start=0,inout->end=0;
  154. result = _kernel_osfile(18,file_path,inout);
  155. inout->load=0xffd,inout->exec=0,inout->start=0,inout->end=0;
  156. result = _kernel_osfile(18,current,inout);
  157.  
  158. return TRUE;
  159. }
  160.  
  161. static void event_handler(wimp_eventstr *e, void *handle)
  162. {
  163. int filetype,result;
  164. char *filename;
  165. _kernel_osfile_block *inout,inouts;
  166. handle = handle;
  167. inout = &inouts;
  168.   switch (e->e)
  169.   {
  170.     case wimp_EREDRAW:
  171.        redraw_window(e->data.o.w);
  172.        break;
  173.  
  174.     case wimp_ESEND:
  175.    case wimp_ESENDWANTACK:
  176.     {
  177.       switch (e->data.msg.hdr.action)
  178.       {
  179.         case wimp_MDATALOAD:   /* insert data */
  180.         case wimp_MDATAOPEN:
  181.         filetype = xferrecv_checkinsert(&filename);
  182.         if (strlen(filename) > LINESIZE)
  183.            {
  184.            werr(0, "File path name is too long");
  185.            }
  186.         else{
  187.             inout->load=0,inout->exec=0,inout->start=0,inout->end=0;
  188.             result = _kernel_osfile(13,filename,inout);
  189.             if(result == 0){
  190.                werr(0,"file not found");
  191.                }
  192.             else if(result == 1){
  193.                strcpy(file_name, filename);
  194.                filelength = inout->start;
  195.                file_attrib = inout->end;
  196.                changepath();
  197.                }
  198.             else if(result == 2){
  199.                werr(0,"sorry can't backup directories");
  200.                }
  201.             else{
  202.                werr(0,"errr, u shouldn't get this msg");
  203.                }
  204.            }
  205.           break;
  206.           default: break;        
  207.        }
  208.      }
  209.    xferrecv_insertfileok();
  210.    break;
  211.  
  212.    case wimp_ECLOSE:  
  213.       wimpt_noerr(wimp_close_wind(e->data.o.w));
  214.       window_open = FALSE;
  215.       break;
  216.  
  217.    default:      
  218.    break;
  219.   }
  220. }
  221.  
  222.  
  223. static void event_handler2(wimp_eventstr *e, void *handle)
  224. {
  225. wimp_i iconbar;
  226. handle = handle;
  227.  
  228.  
  229.   switch (e->e)
  230.   {
  231.     case wimp_EREDRAW:
  232.        redraw_window(e->data.o.w);
  233.        break;
  234.  
  235.    case wimp_EBUT:
  236.       if(e->data.but.m.bbits != 4 && e->data.but.m.bbits != 2){
  237.          program_status();
  238.          }
  239.       else if(e->data.but.m.bbits == 4)
  240.          iconclick(iconbar);
  241.       break;
  242.  
  243.    case wimp_ECLOSE:  
  244.       wimpt_noerr(wimp_close_wind(e->data.o.w));
  245.       window_open = FALSE;
  246.       break;
  247.  
  248.    default:      
  249.    break;
  250.   }
  251. }
  252.  
  253. static void menuproc(void *handle, char *hit)
  254. {
  255. int time_now,alarm_at;
  256. handle = handle; 
  257.  
  258. switch (hit[0]){
  259.  
  260.    case menu_info:
  261.       program_info();
  262.       break;
  263.  
  264.    case menu_status:
  265.       program_status();
  266.    break;
  267.  
  268.    case menu_auto:
  269.       if(autobackup){
  270.          autobackup = FALSE;
  271.          alarm_init();
  272.          strcpy(Time,"Off");
  273.          menu_setflags(top_menu,menu_auto,0,0);
  274.          }
  275.       else{
  276.          autobackup = TRUE;
  277.          time_now = alarm_timenow();
  278.          alarm_at = ((timer*100*60)+time_now);
  279.          alarm_set(alarm_at,alarm_proc,0);
  280.          strcpy(Time,"On");
  281.          menu_setflags(top_menu,menu_auto,1,0);
  282.          }
  283.       break;
  284.  
  285.    case menu_time:
  286.       program_time();
  287.       break;
  288.  
  289.    case menu_start_at:
  290.       program_start_at();
  291.       break;
  292.  
  293.    case menu_over:
  294.       if(overwrite){
  295.          strcpy(Overwrite,"Off");
  296.          overwrite = FALSE;
  297.          menu_setflags(top_menu,menu_over,0,0);
  298.          }
  299.       else{
  300.          strcpy(Overwrite,"On");
  301.          overwrite = TRUE;
  302.          menu_setflags(top_menu,menu_over,1,0);
  303.          }
  304.       break;
  305.  
  306.    case menu_quit:
  307.       exit(0);
  308.  
  309.    default:
  310.       break;
  311.    }
  312. }
  313.  
  314. static void iconclick(wimp_i icon)
  315. {
  316. icon = icon;
  317. backup();
  318. return;
  319. }
  320.  
  321. static void program_info(void)
  322. {
  323. if (info_dialogue = dbox_new("ProgInfo"),info_dialogue != NULL){
  324.    dbox_raw_eventhandler (info_dialogue, (dbox_raw_handler_proc)                
  325.    dialogue_raw_event, 0);
  326.  
  327.    dbox_setfield(info_dialogue, info_field, Version_String);
  328.    dbox_setfield(info_dialogue, author_field,Author);
  329.    dbox_show(info_dialogue);
  330.    dbox_fillin(info_dialogue);
  331.    dbox_dispose(&info_dialogue);
  332.    }
  333. }
  334.  
  335. static void program_status(void)
  336. {
  337. int i;
  338.  
  339. if (main_dialogue = dbox_new("MainWindow"), main_dialogue != NULL){
  340.    status_window = TRUE;
  341.     /*wimp_set_icon_state(main_dialogue,file_field,0,0);*/
  342.    dbox_raw_eventhandler (main_dialogue, (dbox_raw_handler_proc)                   dialogue_raw_event, 0);
  343.    dbox_setfield(main_dialogue, file_field, short_file_name);
  344.    i = atoi(backup_number),i++;
  345.    dbox_setnumeric(main_dialogue, backup_number_field, i);
  346.    if(strcmp("On",Overwrite) == 0){
  347.       dbox_setnumeric(main_dialogue, overwrite_field, 1);
  348.       }
  349.    if(strcmp("On",Time) == 0){
  350.       dbox_setnumeric(main_dialogue,time_field, 1);
  351.       }
  352.    dbox_setfield(main_dialogue, overwrite_field, Overwrite);
  353.    dbox_setfield(main_dialogue, time_field, Time);
  354.    dbox_show(main_dialogue);
  355.    dbox_fillin(main_dialogue);
  356.    dbox_dispose(&main_dialogue);
  357.    status_window = FALSE;   
  358.    }
  359. }
  360.  
  361. static void program_time(void)
  362. {
  363.  
  364. if (time_dialogue = dbox_new("Time"), time_dialogue != NULL){
  365.   
  366.    dbox_setnumeric(time_dialogue, 0, timer);
  367.    dbox_show(time_dialogue);
  368.    dbox_fillin(time_dialogue);
  369.    timer = dbox_getnumeric(time_dialogue,0);
  370.    dbox_dispose(&time_dialogue);
  371.    }
  372. }
  373.  
  374. static void program_start_at(void)
  375. {
  376. int i,b;
  377. FILE *fp;
  378.  
  379. if (start_dialogue = dbox_new("Every"), start_dialogue != NULL){
  380.    
  381.    b = atoi(backup_number);
  382.    dbox_setnumeric(start_dialogue, 0,b+1);
  383.    dbox_show(start_dialogue);
  384.    dbox_fillin(start_dialogue);
  385.    i = dbox_getnumeric(start_dialogue,0);
  386.    dbox_dispose(&start_dialogue);
  387.    if((i-1) != b){
  388.       i--;
  389.       strcpy(backup_number,itoa(i));
  390.  
  391.       if((fp = fopen(current,"w")) != NULL){
  392.          fprintf(fp,"%s",backup_number);
  393.          fclose(fp);
  394.          }
  395.       else{
  396.          werr(0,"couldn't open data file \"%s\"",current);
  397.          }
  398.       }
  399.    }
  400. return;
  401. }
  402.  
  403.  
  404. void redraw_window (handle)
  405.      
  406.      wimp_w handle;
  407.      
  408.      {
  409.           BOOL redraw;
  410.           wimp_redrawstr redraw_box;
  411.           
  412.           redraw_box.w = handle;
  413.           wimpt_noerr (wimp_redraw_wind (&redraw_box, &redraw));
  414.           
  415.           while (redraw)
  416.           {
  417.                wimpt_complain (wimp_borderwindow (redraw_box));
  418.                wimp_get_rectangle (&redraw_box, &redraw);
  419.           }
  420.      }
  421.  
  422.  
  423.  
  424. void alarm_proc(int called_at,void *handler){
  425. int time_now,alarm_at,i;
  426. called_at=called_at;
  427. if(autobackup){
  428.    time_now = alarm_timenow();
  429.    alarm_at = ((timer*100*60)+time_now);
  430.    alarm_set(alarm_at,alarm_proc,(void *)0);
  431.    }
  432. backup();
  433. if(status_window){
  434.    i = atoi(backup_number),i++;
  435.    dbox_setnumeric(main_dialogue, backup_number_field, i);
  436.    }
  437. return;
  438. }
  439.  
  440. void status_handler (wimp_mousestr mouse){
  441. int time_now;
  442. int alarm_at,i;
  443. FILE *fp;
  444.  
  445.           switch (mouse.i)
  446.           {
  447.              case 1:
  448.                 if(mouse.bbits == 4 || mouse.bbits == 1){
  449.                    if(overwrite){
  450.                       strcpy(Overwrite,"Off");
  451.                       overwrite = FALSE;
  452.                       menu_setflags(top_menu,menu_over,0,0);
  453.                       }
  454.                    else{
  455.                       strcpy(Overwrite,"On");
  456.                       overwrite = TRUE;
  457.                       menu_setflags(top_menu,menu_over,1,0);
  458.                       dbox_setfield(main_dialogue, overwrite_field, "    ");
  459.                       dbox_setfield(main_dialogue, overwrite_field, Overwrite);
  460.                       }
  461.                    dbox_setfield(main_dialogue, overwrite_field, Overwrite);
  462.                    mouse.bbits = 0;
  463.                    workarea_button (mouse);
  464.                    }
  465.                 break;
  466.  
  467.              case 2:
  468.                 if(mouse.bbits == 4 || mouse.bbits == 1){
  469.                    if(autobackup){
  470.                       autobackup = FALSE;
  471.                       alarm_init();
  472.                       strcpy(Time,"Off");
  473.                       menu_setflags(top_menu,menu_auto,0,0);
  474.                       }
  475.                    else{
  476.                       autobackup = TRUE;
  477.                       time_now = alarm_timenow();
  478.                       alarm_at = ((timer*100*60)+time_now);
  479.                       alarm_set(alarm_at,alarm_proc,0);
  480.                       strcpy(Time,"On");
  481.                       menu_setflags(top_menu,menu_auto,1,0);
  482.                       }
  483.                    dbox_setfield(main_dialogue, time_field, "    ");
  484.                    dbox_setfield(main_dialogue, time_field, Time);
  485.                    mouse.bbits = 0;
  486.                    workarea_button (mouse);
  487.                    }
  488.                 break;
  489.              
  490.              case 3:
  491.                 if(mouse.bbits == 4){
  492.                    i = atoi(backup_number);    
  493.                    i++;
  494.                    strcpy(backup_number,itoa(i));
  495.                    dbox_setnumeric(main_dialogue,3,i+1);
  496.                    mouse.bbits = 0;
  497.                    workarea_button (mouse);
  498.                    }
  499.                 else if(mouse.bbits == 1){
  500.                    i = atoi(backup_number);    
  501.                    i--;
  502.                    strcpy(backup_number,itoa(i));
  503.                    dbox_setnumeric(main_dialogue,3,i+1);
  504.                    mouse.bbits = 0;
  505.                    workarea_button (mouse);
  506.                    }
  507.                 else if(mouse.bbits == 2){
  508.                    break;
  509.                    }
  510.                 if((fp = fopen(current,"w")) != NULL){
  511.                    fprintf(fp,"%s",backup_number);
  512.                    fclose(fp);
  513.                    }
  514.                 else{
  515.                    werr(0,"couldn't open data file");
  516.                    }
  517.                 break;
  518.  
  519.              case 4:
  520.                 if(mouse.bbits == 4 || mouse.bbits == 1){
  521.                    backup();
  522.                    i = atoi(backup_number);
  523.                    dbox_setnumeric(main_dialogue,3,i+1);
  524.                    mouse.bbits = 0;
  525.                    workarea_button (mouse);
  526.                    }
  527.                 break;
  528.  
  529.              default:
  530.                 break;
  531.           }
  532.  
  533. }
  534.  
  535. void workarea_button (wimp_mousestr mouse){
  536. if (mouse.bbits < 5)
  537.    wimpt_complain (wimp_bordericon (mouse));
  538. }
  539.