home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 November / PCWorld_2004-11_cd.bin / software / temacd / poweroff / pwroff30.exe / src / settings.c < prev    next >
C/C++ Source or Header  |  2003-07-28  |  14KB  |  324 lines

  1. #include "poweroff.h"
  2.  
  3. #define CHKREGISTRY(f) if (!(f)) { return 0;}
  4.  
  5. void InitializeSettings(PowerSettings *ps)
  6. {
  7.   memset(ps,0,sizeof(PowerSettings));
  8.   ps->action=POWEROFF;
  9.   ps->schedule.schedule=FIXED_DAY;
  10.   GetLocalTime(&ps->schedule.date);
  11.   ps->when=IMMEDIATE;
  12.   ps->who=LOCAL_COMPUTER;
  13.   ps->schedule.time.wHour=25;
  14.   ps->schedule.date.wYear=0;
  15.   ps->interactive=1;
  16.   ps->serversocket=INVALID_SOCKET;
  17.   ps->schedule.day=1;
  18.   ps->options.allow_cancel=1;
  19.   ps->options.in_tray=1;
  20.   strcpy(ps->remote.mac_address,"000000000000");
  21.   ps->remote.current_credentials=1;
  22.   ps->remote.port=LISTEN_PORT;
  23.   ps->schedule.week1=1;
  24.   ps->schedule.week2=1;
  25.   ps->schedule.week3=1;
  26.   ps->schedule.week4=1;
  27.   ps->schedule.week5=1;
  28.   ps->windowsversion.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  29.   if (!GetVersionEx(&ps->windowsversion))
  30.   {
  31.     DisplayLastError(ps,NULL);
  32.   }
  33. }
  34.  
  35. void ReadDefaultSettings(PowerSettings *ps)
  36. {
  37.   if (!ps->run_as_service)
  38.   {
  39.     if (!ReadSettings(HKEY_CURRENT_USER,"SOFTWARE\\JoBo\\Poweroff",ps))
  40.       if (!ReadSettings(HKEY_LOCAL_MACHINE,"SOFTWARE\\JoBo\\Poweroff",ps))
  41.         if (!ReadSettings(HKEY_CURRENT_USER,"SOFTWARE\\Poweroff",ps))
  42.           ReadSettings(HKEY_LOCAL_MACHINE,"SOFTWARE\\Poweroff",ps);
  43.   }
  44.   else
  45.   {
  46.     ReadSettings(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\Poweroff\\Parameters",ps);
  47.   }
  48.   if (ps->remote.port<=0)
  49.     ps->remote.port=LISTEN_PORT;
  50.   if (ps->remote_control.port<=0)
  51.     ps->remote_control.port=LISTEN_PORT;
  52. }
  53.  
  54. int SaveSettings(HKEY hive,char *base,PowerSettings *ps)
  55. {
  56.   char str[20];
  57.  
  58.   Log("SaveSettings start, base=%s",base);
  59.   CHKREGISTRY(WriteRegistryInteger(hive,base,"action",ps->action));
  60.   CHKREGISTRY(WriteRegistryInteger(hive,base,"who",ps->who));
  61.   CHKREGISTRY(WriteRegistryInteger(hive,base,"when",ps->when));
  62.   CHKREGISTRY(WriteRegistryString(hive,base,"computer_name",ps->remote.computer_name));
  63.   CHKREGISTRY(WriteRegistryString(hive,base,"mac_address",ps->remote.mac_address));
  64.   CHKREGISTRY(WriteRegistryString(hive,base,"ip_address",ps->remote.ip_address));
  65.   CHKREGISTRY(WriteRegistryInteger(hive,base,"remote_use_nt",ps->remote.use_nt));
  66.   CHKREGISTRY(WriteRegistryInteger(hive,base,"remote_schedule",ps->remote.schedule_remote));
  67.   CHKREGISTRY(WriteRegistryInteger(hive,base,"remote_port",ps->remote.port));
  68.   if (ps->remote.save_password)
  69.   {
  70.     CHKREGISTRY(WriteRegistryString(hive,base,"remote_password",ps->remote.password));
  71.   }
  72.   else
  73.   {
  74.     CHKREGISTRY(WriteRegistryString(hive,base,"remote_password",""));
  75.   }
  76.   CHKREGISTRY(WriteRegistryInteger(hive,base,"save_password",ps->remote.save_password));
  77.   CHKREGISTRY(WriteRegistryInteger(hive,base,"current_credentials",ps->remote.current_credentials));
  78.   CHKREGISTRY(WriteRegistryString(hive,base,"remote_username",ps->remote.username));
  79.   CHKREGISTRY(WriteRegistryInteger(hive,base,"remote_control_port",ps->remote_control.port));
  80.   CHKREGISTRY(WriteRegistryString(hive,base,"remote_control_password",ps->remote_control.password));
  81.   CHKREGISTRY(WriteRegistryString(hive,base,"subnet_mask",ps->remote.subnet_mask));
  82.   CHKREGISTRY(WriteRegistryInteger(hive,base,"seconds",ps->warning.seconds));
  83.   CHKREGISTRY(WriteRegistryString(hive,base,"message",ps->warning.message));
  84.   CHKREGISTRY(WriteRegistryInteger(hive,base,"play_sound",ps->warning.play_sound));
  85.   CHKREGISTRY(WriteRegistryString(hive,base,"sound_file",ps->warning.sound_file));
  86.   CHKREGISTRY(WriteRegistryString(hive,base,"program",ps->program.program));
  87.   CHKREGISTRY(WriteRegistryString(hive,base,"directory",ps->program.directory));
  88.   CHKREGISTRY(WriteRegistryInteger(hive,base,"schedule",ps->schedule.schedule));
  89.   sprintf(str,"%02d:%02d",ps->schedule.time.wHour,ps->schedule.time.wMinute);
  90.   CHKREGISTRY(WriteRegistryString(hive,base,"time",str));
  91.   sprintf(str,"%d/%d/%d",ps->schedule.date.wDay,ps->schedule.date.wMonth,ps->schedule.date.wYear);
  92.   CHKREGISTRY(WriteRegistryString(hive,base,"date",str));
  93.   CHKREGISTRY(WriteRegistryInteger(hive,base,"monday",ps->schedule.monday));
  94.   CHKREGISTRY(WriteRegistryInteger(hive,base,"tuesday",ps->schedule.tuesday));
  95.   CHKREGISTRY(WriteRegistryInteger(hive,base,"wednesday",ps->schedule.wednesday));
  96.   CHKREGISTRY(WriteRegistryInteger(hive,base,"thursday",ps->schedule.thursday));
  97.   CHKREGISTRY(WriteRegistryInteger(hive,base,"friday",ps->schedule.friday));
  98.   CHKREGISTRY(WriteRegistryInteger(hive,base,"saturday",ps->schedule.saturday));
  99.   CHKREGISTRY(WriteRegistryInteger(hive,base,"sunday",ps->schedule.sunday));
  100.   CHKREGISTRY(WriteRegistryInteger(hive,base,"week1",ps->schedule.week1));
  101.   CHKREGISTRY(WriteRegistryInteger(hive,base,"week2",ps->schedule.week2));
  102.   CHKREGISTRY(WriteRegistryInteger(hive,base,"week3",ps->schedule.week3));
  103.   CHKREGISTRY(WriteRegistryInteger(hive,base,"week4",ps->schedule.week4));
  104.   CHKREGISTRY(WriteRegistryInteger(hive,base,"week5",ps->schedule.week5));
  105.   CHKREGISTRY(WriteRegistryInteger(hive,base,"day",ps->schedule.day));
  106.   CHKREGISTRY(WriteRegistryInteger(hive,base,"wait",ps->schedule.wait));
  107.   CHKREGISTRY(WriteRegistryInteger(hive,base,"after_seconds",ps->schedule.seconds));
  108.   CHKREGISTRY(WriteRegistryString(hive,base,"process",ps->process.process));
  109.   CHKREGISTRY(WriteRegistryInteger(hive,base,"warning",ps->options.warning));
  110.   CHKREGISTRY(WriteRegistryInteger(hive,base,"run_program",ps->options.run_program));
  111.   CHKREGISTRY(WriteRegistryInteger(hive,base,"force",ps->options.force));
  112.   CHKREGISTRY(WriteRegistryInteger(hive,base,"in_tray",ps->options.in_tray));
  113.   CHKREGISTRY(WriteRegistryInteger(hive,base,"allow_cancel",ps->options.allow_cancel));
  114.   CHKREGISTRY(WriteRegistryInteger(hive,base,"allow_remote_control",ps->options.allow_remote_control));
  115.   Log("SaveSettings end");
  116.   return 1;
  117. }
  118.  
  119. int ReadSettings(HKEY hive,char *base,PowerSettings *ps)
  120. {
  121.   char str[20];
  122.   int hour,minute,day,month,year;
  123.  
  124.   Log("ReadSettings start, base=%s",base);
  125.   CHKREGISTRY(ReadRegistryInteger(hive,base,"action",&ps->action));
  126.   ReadRegistryInteger(hive,base,"who",&ps->who);
  127.   ReadRegistryShort(hive,base,"debug",&debug);
  128.   ReadRegistryInteger(hive,base,"when",&ps->when);
  129.   ReadRegistryString(hive,base,"computer_name",ps->remote.computer_name,100);
  130.   ReadRegistryString(hive,base,"mac_address",ps->remote.mac_address,14);
  131.   ReadRegistryString(hive,base,"ip_address",ps->remote.ip_address,20);
  132.   ReadRegistryString(hive,base,"subnet_mask",ps->remote.subnet_mask,20);
  133.   ReadRegistryShort(hive,base,"remote_use_nt",&ps->remote.use_nt);
  134.   ReadRegistryShort(hive,base,"remote_schedule",&ps->remote.schedule_remote);
  135.   ReadRegistryInteger(hive,base,"remote_port",&ps->remote.port);
  136.   ReadRegistryString(hive,base,"remote_password",ps->remote.password,40);
  137.   ReadRegistryShort(hive,base,"current_credentials",&ps->remote.current_credentials);
  138.   ReadRegistryString(hive,base,"remote_username",ps->remote.username,40);
  139.   ReadRegistryInteger(hive,base,"remote_control_port",&ps->remote_control.port);
  140.   ReadRegistryString(hive,base,"remote_control_password",ps->remote_control.password,40);
  141.   ReadRegistryShort(hive,base,"save_password",&ps->remote.save_password);
  142.   ReadRegistryInteger(hive,base,"seconds",&ps->warning.seconds);
  143.   ReadRegistryString(hive,base,"message",ps->warning.message,200);
  144.   ReadRegistryShort(hive,base,"play_sound",&ps->warning.play_sound);
  145.   ReadRegistryString(hive,base,"sound_file",ps->warning.sound_file,MAX_PATH);
  146.   ReadRegistryString(hive,base,"program",ps->program.program,MAX_PATH);
  147.   ReadRegistryString(hive,base,"directory",ps->program.directory,MAX_PATH);
  148.   ReadRegistryInteger(hive,base,"schedule",&ps->schedule.schedule);
  149.   ReadRegistryString(hive,base,"time",str,20);
  150.   GetLocalTime(&ps->schedule.time);
  151.   sscanf(str,"%d:%d",&hour,&minute);
  152.   ps->schedule.time.wHour=hour;
  153.   ps->schedule.time.wMinute=minute;
  154.   ReadRegistryString(hive,base,"date",str,20);
  155.   GetLocalTime(&ps->schedule.date);
  156.   sscanf(str,"%d/%d/%d",&day,&month,&year);
  157.   ps->schedule.date.wDay=day;
  158.   ps->schedule.date.wMonth=month;
  159.   ps->schedule.date.wYear=year;
  160.   ReadRegistryShort(hive,base,"monday",&ps->schedule.monday);
  161.   ReadRegistryShort(hive,base,"tuesday",&ps->schedule.tuesday);
  162.   ReadRegistryShort(hive,base,"wednesday",&ps->schedule.wednesday);
  163.   ReadRegistryShort(hive,base,"thursday",&ps->schedule.thursday);
  164.   ReadRegistryShort(hive,base,"friday",&ps->schedule.friday);
  165.   ReadRegistryShort(hive,base,"saturday",&ps->schedule.saturday);
  166.   ReadRegistryShort(hive,base,"sunday",&ps->schedule.sunday);
  167.   ReadRegistryShort(hive,base,"week1",&ps->schedule.week1);
  168.   ReadRegistryShort(hive,base,"week2",&ps->schedule.week2);
  169.   ReadRegistryShort(hive,base,"week3",&ps->schedule.week3);
  170.   ReadRegistryShort(hive,base,"week4",&ps->schedule.week4);
  171.   ReadRegistryShort(hive,base,"week5",&ps->schedule.week5);
  172.   ReadRegistryInteger(hive,base,"day",&ps->schedule.day);
  173.   ReadRegistryInteger(hive,base,"wait",&ps->schedule.wait);
  174.   ReadRegistryInteger(hive,base,"after_seconds",&ps->schedule.seconds);
  175.   ReadRegistryString(hive,base,"process",ps->process.process,200);
  176.   ReadRegistryShort(hive,base,"warning",&ps->options.warning);
  177.   ReadRegistryShort(hive,base,"run_program",&ps->options.run_program);
  178.   ReadRegistryShort(hive,base,"force",&ps->options.force);
  179.   ReadRegistryShort(hive,base,"in_tray",&ps->options.in_tray);
  180.   ReadRegistryShort(hive,base,"allow_cancel",&ps->options.allow_cancel);
  181.   ReadRegistryShort(hive,base,"allow_remote_control",&ps->options.allow_remote_control);
  182.   Log("ReadSettings end");
  183.   return 1;
  184. }
  185.  
  186. void DebugSettings(PowerSettings *ps)
  187. {
  188.   Log("SebugSettings start");
  189.   switch (ps->action)
  190.   {
  191.     case LOGOFF:
  192.       Log("action=LOGOFF");
  193.       break;
  194.     case REBOOT:
  195.       Log("action=REBOOT");
  196.       break;
  197.     case SHUTDOWN:
  198.       Log("action=SHUTDOWN");
  199.       break;
  200.     case POWEROFF:
  201.       Log("action=POWEROFF");
  202.       break;
  203.     case STANDBY:
  204.       Log("action=STANDBY");
  205.       break;
  206.     case HIBERNATE:
  207.       Log("action=HIBERNATE");
  208.       break;
  209.     case LOCK:
  210.       Log("action=LOCK");
  211.       break;
  212.     case WAKE_ON_LAN:
  213.       Log("action=WAKE_ON_LAN");
  214.       break;
  215.     case MONITOR_OFF:
  216.       Log("action=MONITOR_OFF");
  217.       break;
  218.     case MONITOR_ON:
  219.       Log("action=MONITOR_ON");
  220.       break;
  221.     case NO_ACTION:
  222.       Log("action=NO_ACTION");
  223.       break;
  224.     default:
  225.       Log("action=UNKNOWN");
  226.       break;
  227.   }
  228.   switch (ps->who)
  229.   {
  230.     case LOCAL_COMPUTER:
  231.       Log("who=LOCAL_COMPUTER");
  232.       break;
  233.     case REMOTE_COMPUTER:
  234.       Log("who=REMOTE_COMPUTER");
  235.       break;
  236.     default:
  237.       Log("who=UNKNOWN");
  238.       break;
  239.   }
  240.   switch (ps->when)
  241.   {
  242.     case IMMEDIATE:
  243.       Log("when=IMMEDIATE");
  244.       break;
  245.     case SCHEDULED:
  246.       Log("when=SCHEDULED");
  247.       break;
  248.     case PROCESS:
  249.       Log("when=PROCESS");
  250.       break;
  251.     default:
  252.       Log("when=UNKNOWN");
  253.       break;
  254.   }
  255.   Log("interactive=%d",ps->interactive);
  256.   Log("active_timer=%u",ps->active_timer);
  257.   Log("simulate=%d",ps->simulate);
  258.   Log("start_minimized=%d",ps->start_minimized);
  259.   Log("run_as_service=%d",ps->run_as_service);
  260.   Log("executable=%s",ps->executable);
  261.   Log("authenticated=%d",ps->authenticated);
  262.   Log("quiet=%d",ps->quiet);
  263.   Log("remaining_seconds=%d",ps->remaining_seconds);
  264.   Log("remote.computer_name=%s",ps->remote.computer_name);
  265.   Log("remote.mac_address=%s",ps->remote.mac_address);
  266.   Log("remote.ip_address=%s",ps->remote.ip_address);
  267.   Log("remote.subnet_mask=%s",ps->remote.subnet_mask);
  268.   Log("remote.use_nt=%d",ps->remote.use_nt);
  269.   Log("remote.port=%d",ps->remote.port);
  270.   Log("remote.save_password=%d",ps->remote.save_password);
  271.   Log("remote.password=%s",ps->remote.password);
  272.   Log("remote.schedule_remote=%d",ps->remote.schedule_remote);
  273.   Log("remote.current_credentials=%d",ps->remote.current_credentials);
  274.   Log("remote.username=%s",ps->remote.username);
  275.   Log("remote_control.port=%d",ps->remote_control.port);
  276.   Log("remote_control.password=%s",ps->remote_control.password);
  277.   Log("warning.seconds=%d",ps->warning.seconds);
  278.   Log("warning.message=%s",ps->warning.message);
  279.   Log("warning.play_sound=%d",ps->warning.play_sound);
  280.   Log("warning.sound_file=%s",ps->warning.sound_file);
  281.   Log("program.program=%s",ps->program.program);
  282.   Log("program.directory=%s",ps->program.directory);
  283.   switch (ps->schedule.schedule)
  284.   {
  285.     case FIXED_DAY:
  286.       Log("schedule.schedule=FIXED_DAY");
  287.       break;
  288.     case DAILY:
  289.       Log("schedule.schedule=DAILY");
  290.       break;
  291.     case DAY_OF_MONTH:
  292.       Log("schedule.schedule=DAY_OF_MONTH");
  293.       break;
  294.     case AFTER_X_SECONDS:
  295.       Log("schedule.schedule=AFTER_X_SECONDS");
  296.       break;
  297.     default:
  298.       Log("schedule.schedule=UNKNOWN");
  299.       break;
  300.   }
  301.   Log("schedule.monday=%d",ps->schedule.monday);
  302.   Log("schedule.tuesday=%d",ps->schedule.tuesday);
  303.   Log("schedule.wednesday=%d",ps->schedule.wednesday);
  304.   Log("schedule.thursday=%d",ps->schedule.thursday);
  305.   Log("schedule.friday=%d",ps->schedule.friday);
  306.   Log("schedule.saturday=%d",ps->schedule.saturday);
  307.   Log("schedule.sunday=%d",ps->schedule.sunday);
  308.   Log("schedule.week1=%d",ps->schedule.week1);
  309.   Log("schedule.week2=%d",ps->schedule.week2);
  310.   Log("schedule.week3=%d",ps->schedule.week3);
  311.   Log("schedule.week4=%d",ps->schedule.week4);
  312.   Log("schedule.week5=%d",ps->schedule.week5);
  313.   Log("schedule.day=%u",ps->schedule.day);
  314.   Log("schedule.seconds=%u",ps->schedule.seconds);
  315.   Log("schedule.wait=%d",ps->schedule.wait);
  316.   Log("process.process=%s",ps->process.process);
  317.   Log("options.warning=%d",ps->options.warning);
  318.   Log("options.run_program=%d",ps->options.run_program);
  319.   Log("options.force=%d",ps->options.force);
  320.   Log("options.in_tray=%d",ps->options.in_tray);
  321.   Log("options.allow_cancel=%d",ps->options.allow_cancel);
  322.   Log("options.allow_remote_control=%d",ps->options.allow_remote_control);
  323.   Log("SebugSettings end");
  324. }