home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 November / PCWorld_2004-11_cd.bin / software / temacd / poweroff / pwroff30.exe / src / schedule.c < prev    next >
C/C++ Source or Header  |  2002-02-16  |  15KB  |  336 lines

  1. #include "poweroff.h"
  2.  
  3. void CheckScheduleTimer(HWND hWnd,PowerSettings *ps)
  4. {
  5.   SYSTEMTIME systime;
  6.  
  7.   Log("CheckScheduleTimer start, wait=%d",ps->schedule.wait);
  8.   if (ps->schedule.wait)
  9.     ps->schedule.wait--;
  10.   else
  11.   {
  12.     GetLocalTime(&systime);
  13.     switch (ps->schedule.schedule)
  14.     {
  15.       case FIXED_DAY:
  16.         Log("FIXED DAY, hour=%d, minute=%d, day=%d, month=%d, year=%d",ps->schedule.time.wHour,ps->schedule.time.wMinute,ps->schedule.date.wDay,ps->schedule.date.wMonth,ps->schedule.date.wYear);
  17.         Log("systime, hour=%d, minute=%d, day=%d, month=%d, year=%d",systime.wHour,systime.wMinute,systime.wDay,systime.wMonth,systime.wYear);
  18.         if (ps->schedule.time.wHour==systime.wHour && 
  19.               ps->schedule.time.wMinute==systime.wMinute &&
  20.               ps->schedule.date.wDay==systime.wDay &&
  21.               ps->schedule.date.wMonth==systime.wMonth &&
  22.               ps->schedule.date.wYear==systime.wYear)
  23.         {
  24.           PowerOff(hWnd,ps);
  25.         }
  26.         break;
  27.       case DAILY:
  28.         Log("DAILY, hour=%d, minute=%d, day=%d, month=%d, year=%d",ps->schedule.time.wHour,ps->schedule.time.wMinute,ps->schedule.date.wDay,ps->schedule.date.wMonth,ps->schedule.date.wYear);
  29.         Log("systime, hour=%d, minute=%d, day=%d, month=%d, year=%d",systime.wHour,systime.wMinute,systime.wDay,systime.wMonth,systime.wYear);
  30.         Log("monday=%d, tuesday=%d, wednesday=%d, thursday=%d, friday=%d, saturday=%d, sunday=%d",ps->schedule.monday,ps->schedule.tuesday,ps->schedule.wednesday,ps->schedule.thursday,ps->schedule.friday,ps->schedule.saturday,ps->schedule.sunday);
  31.         Log("DayOfWeek=%d, Day=%d",systime.wDayOfWeek,(systime.wDay-1)/7+1);
  32.         Log("week1=%d, week2=%d, week3=%d, week4=%d, week5=%d",ps->schedule.week1,ps->schedule.week2,ps->schedule.week3,ps->schedule.week4,ps->schedule.week5);
  33.         if (ps->schedule.time.wHour==systime.wHour && 
  34.               ps->schedule.time.wMinute==systime.wMinute &&
  35.             ((ps->schedule.monday && systime.wDayOfWeek==1) ||
  36.              (ps->schedule.tuesday && systime.wDayOfWeek==2) ||
  37.              (ps->schedule.wednesday && systime.wDayOfWeek==3) ||
  38.              (ps->schedule.thursday && systime.wDayOfWeek==4) ||
  39.              (ps->schedule.friday && systime.wDayOfWeek==5) ||
  40.              (ps->schedule.saturday && systime.wDayOfWeek==6) ||
  41.              (ps->schedule.sunday && systime.wDayOfWeek==0)) &&
  42.             (((systime.wDay-1)/7+1==1 && ps->schedule.week1) ||
  43.              ((systime.wDay-1)/7+1==2 && ps->schedule.week2) ||
  44.              ((systime.wDay-1)/7+1==3 && ps->schedule.week3) ||
  45.              ((systime.wDay-1)/7+1==4 && ps->schedule.week4) ||
  46.              ((systime.wDay-1)/7+1==5 && ps->schedule.week5)))
  47.         {
  48.           PowerOff(hWnd,ps);
  49.         }
  50.         break;
  51.       case DAY_OF_MONTH:
  52.         Log("DAY_OF_MONTH: day=%d, hour=%d, minute=%d",ps->schedule.day,ps->schedule.time.wHour,ps->schedule.time.wMinute);
  53.         Log("systime: day=%d, hour=%d, minute=%d",systime.wDay,systime.wHour,systime.wMinute);
  54.         if (ps->schedule.time.wHour==systime.wHour && 
  55.               ps->schedule.time.wMinute==systime.wMinute &&
  56.               ps->schedule.day==systime.wDay)
  57.         {
  58.           PowerOff(hWnd,ps);
  59.         }
  60.         break;
  61.       case AFTER_X_SECONDS:
  62.         Log("AFTER_X_SECONDS: remaining=%d",ps->remaining_seconds);
  63.         ps->remaining_seconds--;
  64.         if (ps->remaining_seconds<=0)
  65.           PowerOff(hWnd,ps);
  66.     }           
  67.   }
  68.   Log("CheckScheduleTimer end");
  69. }
  70.  
  71. void RoundToHour(SYSTEMTIME *st)
  72. {
  73.   FILETIME ft;
  74.   LARGE_INTEGER li;
  75.  
  76.   Log("RoundToHour start");
  77.   st->wMilliseconds=0;
  78.   st->wSecond=0;
  79.   st->wMinute=0;
  80.   SystemTimeToFileTime(st,&ft);
  81.   li.LowPart=ft.dwLowDateTime;
  82.   li.HighPart=ft.dwHighDateTime;
  83.   li.QuadPart+=(LONGLONG)10000000*(LONGLONG)60*(LONGLONG)60;
  84.   ft.dwLowDateTime=li.LowPart;
  85.   ft.dwHighDateTime=li.HighPart;
  86.   FileTimeToSystemTime(&ft,st);
  87.   Log("RoundToHour end");
  88. }
  89.  
  90. void EnableScheduleFields(HWND hWnd)
  91. {
  92.   Log("EnableScheduleFields start");
  93.   if (IsDlgButtonChecked(hWnd,IDC_FIXED_DAY))
  94.   {
  95.     EnableWindow(GetDlgItem(hWnd,IDC_TIME),TRUE);
  96.     EnableWindow(GetDlgItem(hWnd,IDC_DATE),TRUE);
  97.     EnableWindow(GetDlgItem(hWnd,IDC_MONDAY),FALSE);
  98.     EnableWindow(GetDlgItem(hWnd,IDC_TUESDAY),FALSE);
  99.     EnableWindow(GetDlgItem(hWnd,IDC_WEDNESDAY),FALSE);
  100.     EnableWindow(GetDlgItem(hWnd,IDC_THURSDAY),FALSE);
  101.     EnableWindow(GetDlgItem(hWnd,IDC_FRIDAY),FALSE);
  102.     EnableWindow(GetDlgItem(hWnd,IDC_SATURDAY),FALSE);
  103.     EnableWindow(GetDlgItem(hWnd,IDC_SUNDAY),FALSE);
  104.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK1),FALSE);
  105.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK2),FALSE);
  106.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK3),FALSE);
  107.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK4),FALSE);
  108.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK5),FALSE);
  109.     EnableWindow(GetDlgItem(hWnd,IDC_MONTH_DAY),FALSE);
  110.     EnableWindow(GetDlgItem(hWnd,IDC_AFTER_SECONDS),FALSE);
  111.   }
  112.   else if (IsDlgButtonChecked(hWnd,IDC_FIXED_DAYS))
  113.   {
  114.     EnableWindow(GetDlgItem(hWnd,IDC_TIME),TRUE);
  115.     EnableWindow(GetDlgItem(hWnd,IDC_DATE),FALSE);
  116.     EnableWindow(GetDlgItem(hWnd,IDC_MONDAY),TRUE);
  117.     EnableWindow(GetDlgItem(hWnd,IDC_TUESDAY),TRUE);
  118.     EnableWindow(GetDlgItem(hWnd,IDC_WEDNESDAY),TRUE);
  119.     EnableWindow(GetDlgItem(hWnd,IDC_THURSDAY),TRUE);
  120.     EnableWindow(GetDlgItem(hWnd,IDC_FRIDAY),TRUE);
  121.     EnableWindow(GetDlgItem(hWnd,IDC_SATURDAY),TRUE);
  122.     EnableWindow(GetDlgItem(hWnd,IDC_SUNDAY),TRUE);
  123.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK1),TRUE);
  124.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK2),TRUE);
  125.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK3),TRUE);
  126.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK4),TRUE);
  127.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK5),TRUE);
  128.     EnableWindow(GetDlgItem(hWnd,IDC_MONTH_DAY),FALSE);
  129.     EnableWindow(GetDlgItem(hWnd,IDC_AFTER_SECONDS),FALSE);
  130.   }
  131.   else if (IsDlgButtonChecked(hWnd,IDC_FIXED_MONTH_DAY))
  132.   {
  133.     EnableWindow(GetDlgItem(hWnd,IDC_TIME),TRUE);
  134.     EnableWindow(GetDlgItem(hWnd,IDC_DATE),FALSE);
  135.     EnableWindow(GetDlgItem(hWnd,IDC_MONDAY),FALSE);
  136.     EnableWindow(GetDlgItem(hWnd,IDC_TUESDAY),FALSE);
  137.     EnableWindow(GetDlgItem(hWnd,IDC_WEDNESDAY),FALSE);
  138.     EnableWindow(GetDlgItem(hWnd,IDC_THURSDAY),FALSE);
  139.     EnableWindow(GetDlgItem(hWnd,IDC_FRIDAY),FALSE);
  140.     EnableWindow(GetDlgItem(hWnd,IDC_SATURDAY),FALSE);
  141.     EnableWindow(GetDlgItem(hWnd,IDC_SUNDAY),FALSE);
  142.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK1),FALSE);
  143.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK2),FALSE);
  144.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK3),FALSE);
  145.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK4),FALSE);
  146.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK5),FALSE);
  147.     EnableWindow(GetDlgItem(hWnd,IDC_MONTH_DAY),TRUE);
  148.     EnableWindow(GetDlgItem(hWnd,IDC_AFTER_SECONDS),FALSE);
  149.   }
  150.   else if (IsDlgButtonChecked(hWnd,IDC_AFTER_X_SECONDS))
  151.   {
  152.     EnableWindow(GetDlgItem(hWnd,IDC_TIME),FALSE);
  153.     EnableWindow(GetDlgItem(hWnd,IDC_DATE),FALSE);
  154.     EnableWindow(GetDlgItem(hWnd,IDC_MONDAY),FALSE);
  155.     EnableWindow(GetDlgItem(hWnd,IDC_TUESDAY),FALSE);
  156.     EnableWindow(GetDlgItem(hWnd,IDC_WEDNESDAY),FALSE);
  157.     EnableWindow(GetDlgItem(hWnd,IDC_THURSDAY),FALSE);
  158.     EnableWindow(GetDlgItem(hWnd,IDC_FRIDAY),FALSE);
  159.     EnableWindow(GetDlgItem(hWnd,IDC_SATURDAY),FALSE);
  160.     EnableWindow(GetDlgItem(hWnd,IDC_SUNDAY),FALSE);
  161.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK1),FALSE);
  162.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK2),FALSE);
  163.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK3),FALSE);
  164.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK4),FALSE);
  165.     EnableWindow(GetDlgItem(hWnd,IDC_WEEK5),FALSE);
  166.     EnableWindow(GetDlgItem(hWnd,IDC_MONTH_DAY),FALSE);
  167.     EnableWindow(GetDlgItem(hWnd,IDC_AFTER_SECONDS),TRUE);
  168.   }
  169.   Log("EnableScheduleFields end");
  170. }
  171.  
  172. void ShowCurrentTime(HWND hWnd)
  173. {
  174.   SYSTEMTIME systime;
  175.   char str[20];
  176.  
  177.   Log("ShowCurrentTime start");
  178.   GetLocalTime(&systime);
  179.   sprintf(str,"%02d:%02d:%02d",systime.wHour,systime.wMinute,systime.wSecond);
  180.   SetDlgItemText(hWnd,IDC_CURRENT_TIME,str);
  181.   Log("ShowCurrentTime end, time=%s",str);
  182. }
  183.  
  184. BOOL FAR PASCAL ScheduleProc(HWND hWnd, unsigned message,DWORD wParam, LONG lParam)
  185. {
  186.   static PowerSettings *ps;
  187.  
  188.   switch (message) 
  189.   {
  190.     case WM_INITDIALOG:
  191.       {
  192.         SYSTEMTIME systime;
  193.         char str[20];
  194.  
  195.         ps=(PowerSettings*)lParam;
  196.         DateTime_SetFormat(GetDlgItem(hWnd,IDC_TIME),"HH:mm");
  197.         DateTime_SetFormat(GetDlgItem(hWnd,IDC_DATE),"dd/MM/yyy");
  198.         GetLocalTime(&systime);
  199.         RoundToHour(&systime);
  200.         if (ps->schedule.time.wHour==25)
  201.           DateTime_SetSystemtime(GetDlgItem(hWnd,IDC_TIME),GDT_VALID ,&systime);
  202.         else
  203.           DateTime_SetSystemtime(GetDlgItem(hWnd,IDC_TIME),GDT_VALID ,&ps->schedule.time);
  204.         if (ps->schedule.date.wYear==0)
  205.           DateTime_SetSystemtime(GetDlgItem(hWnd,IDC_DATE),GDT_VALID ,&systime);
  206.         else
  207.           DateTime_SetSystemtime(GetDlgItem(hWnd,IDC_DATE),GDT_VALID ,&ps->schedule.date);
  208.         CheckDlgButton(hWnd,IDC_MONDAY,ps->schedule.monday?BST_CHECKED:BST_UNCHECKED);
  209.         CheckDlgButton(hWnd,IDC_TUESDAY,ps->schedule.tuesday?BST_CHECKED:BST_UNCHECKED);
  210.         CheckDlgButton(hWnd,IDC_WEDNESDAY,ps->schedule.wednesday?BST_CHECKED:BST_UNCHECKED);
  211.         CheckDlgButton(hWnd,IDC_THURSDAY,ps->schedule.thursday?BST_CHECKED:BST_UNCHECKED);
  212.         CheckDlgButton(hWnd,IDC_FRIDAY,ps->schedule.friday?BST_CHECKED:BST_UNCHECKED);
  213.         CheckDlgButton(hWnd,IDC_SATURDAY,ps->schedule.saturday?BST_CHECKED:BST_UNCHECKED);
  214.         CheckDlgButton(hWnd,IDC_SUNDAY,ps->schedule.sunday?BST_CHECKED:BST_UNCHECKED);
  215.         CheckDlgButton(hWnd,IDC_WEEK1,ps->schedule.week1?BST_CHECKED:BST_UNCHECKED);
  216.         CheckDlgButton(hWnd,IDC_WEEK2,ps->schedule.week2?BST_CHECKED:BST_UNCHECKED);
  217.         CheckDlgButton(hWnd,IDC_WEEK3,ps->schedule.week3?BST_CHECKED:BST_UNCHECKED);
  218.         CheckDlgButton(hWnd,IDC_WEEK4,ps->schedule.week4?BST_CHECKED:BST_UNCHECKED);
  219.         CheckDlgButton(hWnd,IDC_WEEK5,ps->schedule.week5?BST_CHECKED:BST_UNCHECKED);
  220.         sprintf(str,"%d",ps->schedule.day);
  221.         SetDlgItemText(hWnd,IDC_MONTH_DAY,str);
  222.         sprintf(str,"%d",ps->schedule.seconds);
  223.         SetDlgItemText(hWnd,IDC_AFTER_SECONDS,str);
  224.         switch (ps->schedule.schedule)
  225.         {
  226.           case FIXED_DAY:
  227.             CheckRadioButton(hWnd,IDC_FIXED_DAY,IDC_AFTER_X_SECONDS,IDC_FIXED_DAY);
  228.             break;
  229.           case DAILY:
  230.             CheckRadioButton(hWnd,IDC_FIXED_DAY,IDC_AFTER_X_SECONDS,IDC_FIXED_DAYS);
  231.             break;
  232.           case DAY_OF_MONTH:
  233.             CheckRadioButton(hWnd,IDC_FIXED_DAY,IDC_AFTER_X_SECONDS,IDC_FIXED_MONTH_DAY);
  234.             break;
  235.           case AFTER_X_SECONDS:
  236.             CheckRadioButton(hWnd,IDC_FIXED_DAY,IDC_AFTER_X_SECONDS,IDC_AFTER_X_SECONDS);
  237.             break;
  238.         }
  239.         EnableScheduleFields(hWnd);
  240.         sprintf(str,"%d",ps->schedule.wait);
  241.         SetDlgItemText(hWnd,IDC_WAITTIME,str);
  242.         ShowCurrentTime(hWnd);
  243.         SetTimer(hWnd,ID_TIME_TIMER,1000,NULL);
  244.       }   
  245.       return TRUE;
  246.     case WM_COMMAND:
  247.       switch (LOWORD(wParam))
  248.       {
  249.         case IDCANCEL:
  250.           EndDialog(hWnd,TRUE);
  251.           break;
  252.         case IDOK:
  253.           {
  254.             char str[20];
  255.  
  256.             DateTime_GetSystemtime(GetDlgItem(hWnd,IDC_TIME),&ps->schedule.time);
  257.             if (IsDlgButtonChecked(hWnd,IDC_FIXED_DAY))
  258.             {
  259.               DateTime_GetSystemtime(GetDlgItem(hWnd,IDC_DATE),&ps->schedule.date);
  260.               ps->schedule.schedule=FIXED_DAY;
  261.             }
  262.             else if (IsDlgButtonChecked(hWnd,IDC_FIXED_DAYS))
  263.             {
  264.               ps->schedule.monday=IsDlgButtonChecked(hWnd,IDC_MONDAY);
  265.               ps->schedule.tuesday=IsDlgButtonChecked(hWnd,IDC_TUESDAY);
  266.               ps->schedule.wednesday=IsDlgButtonChecked(hWnd,IDC_WEDNESDAY);
  267.               ps->schedule.thursday=IsDlgButtonChecked(hWnd,IDC_THURSDAY);
  268.               ps->schedule.friday=IsDlgButtonChecked(hWnd,IDC_FRIDAY);
  269.               ps->schedule.saturday=IsDlgButtonChecked(hWnd,IDC_SATURDAY);
  270.               ps->schedule.sunday=IsDlgButtonChecked(hWnd,IDC_SUNDAY);
  271.               ps->schedule.week1=IsDlgButtonChecked(hWnd,IDC_WEEK1);
  272.               ps->schedule.week2=IsDlgButtonChecked(hWnd,IDC_WEEK2);
  273.               ps->schedule.week3=IsDlgButtonChecked(hWnd,IDC_WEEK3);
  274.               ps->schedule.week4=IsDlgButtonChecked(hWnd,IDC_WEEK4);
  275.               ps->schedule.week5=IsDlgButtonChecked(hWnd,IDC_WEEK5);
  276.                     if (!ps->schedule.week1 && !ps->schedule.week2 && !ps->schedule.week3 && !ps->schedule.week4 && !ps->schedule.week5)
  277.               {
  278.                 Error(ps,hWnd,"You must select at least one week");
  279.                 return 0;
  280.               }
  281.               if (ps->schedule.monday==0 && ps->schedule.tuesday==0 && ps->schedule.wednesday==0 && ps->schedule.thursday==0 && ps->schedule.friday==0 && ps->schedule.saturday==0 && ps->schedule.sunday==0)
  282.               {
  283.                 Error(ps,hWnd,"You must select at least one day of the week");
  284.                 return 0;
  285.               }
  286.               ps->schedule.date.wYear=0;
  287.               ps->schedule.schedule=DAILY;
  288.               DisplayInformation(ps,hWnd,"Please make sure that you create or update the poweroff service, so that the action can be performed according to your schedule");
  289.             }
  290.             else if (IsDlgButtonChecked(hWnd,IDC_FIXED_MONTH_DAY))
  291.             {
  292.               GetDlgItemText(hWnd,IDC_MONTH_DAY,str,20);
  293.               ps->schedule.day=atoi(str);
  294.               ps->schedule.schedule=DAY_OF_MONTH;
  295.               ps->schedule.date.wYear=0;
  296.               DisplayInformation(ps,hWnd,"Please make sure that you create or update the poweroff service, so that the action can be performed according to your schedule");
  297.             }
  298.             else if (IsDlgButtonChecked(hWnd,IDC_AFTER_X_SECONDS))
  299.             {
  300.               GetDlgItemText(hWnd,IDC_AFTER_SECONDS,str,20);
  301.               ps->schedule.seconds=atoi(str);
  302.               ps->schedule.schedule=AFTER_X_SECONDS;
  303.             }
  304.             GetDlgItemText(hWnd,IDC_WAITTIME,str,20);
  305.             ps->schedule.wait=atoi(str);
  306.             EndDialog(hWnd,TRUE);
  307.           }
  308.           break;
  309.         case IDC_FIXED_DAY:
  310.         case IDC_FIXED_DAYS:
  311.         case IDC_FIXED_MONTH_DAY:
  312.         case IDC_AFTER_X_SECONDS:
  313.           EnableScheduleFields(hWnd);
  314.           break;
  315.       }
  316.       break;
  317.     case WM_TIMER:
  318.       ShowCurrentTime(hWnd);
  319.       break;
  320.       default:
  321.          break;
  322.   }
  323.   return FALSE;
  324. }
  325.  
  326. void ShowScheduleDialog(HWND hWnd,PowerSettings *ps)
  327. {
  328.   FARPROC dlgproc;
  329.  
  330.   Log("ShowScheduleDialog start");
  331.   dlgproc=MakeProcInstance((FARPROC)ScheduleProc,hInst);
  332.     if (DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_SCHEDULE),hWnd,(DLGPROC)dlgproc,(LPARAM)ps)==-1)
  333.     DisplayLastError(ps,hWnd);
  334.     FreeProcInstance(dlgproc);
  335.   Log("ShowScheduleDialog end");
  336. }