home *** CD-ROM | disk | FTP | other *** search
- unit WebModule;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, HTTPApp;
-
- Const
- ciStartOfWeek = 1; { The leftmost day name in the HTML calendar: Sunday = 0, Monday = 1 }
- ciFirstYear = 1990;
- ciLastYear = 2020;
- strPipeName = '\\.\pipe\weblook_tdm_version_1';
-
- type
- TCalendarGrid = Array[0..6,1..6] of String;
-
- TWeblookWM = class(TWebModule)
- procedure WeblookWMDefaultActionAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- procedure WeblookWMCalendarAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- procedure WeblookWMContactsAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- procedure WeblookWMTasksAction(Sender: TObject; Request: TWebRequest;
- Response: TWebResponse; var Handled: Boolean);
- procedure WeblookWMNotesAction(Sender: TObject; Request: TWebRequest;
- Response: TWebResponse; var Handled: Boolean);
- procedure WeblookWMInboxAction(Sender: TObject; Request: TWebRequest;
- Response: TWebResponse; var Handled: Boolean);
- procedure WeblookWMTodayAction(Sender: TObject; Request: TWebRequest;
- Response: TWebResponse; var Handled: Boolean);
- private
- { Private declarations }
- iYear,iMonth : Integer;
- cgCalendarGrid : TCalendarGrid;
- hPipe : THandle;
- Procedure CreateNamedPipe;
- Function ReadPipeRequestResponse(strRequest : String) : String;
- public
- { Public declarations }
- end;
-
- var
- WeblookWM: TWeblookWM;
-
- implementation
-
- uses HTMLTemplates;
-
- {$R *.DFM}
-
- Procedure ShowDebugMsg(Msg : String);
- Begin
- { TIP: use this function for displaying a brief message on the screen! }
- MessageBox(0,PChar('Thread #'+IntToStr(GetCurrentThreadID)+' at '+DateTimeToStr(Now)+':'#13+Msg),
- PChar(ParamStr(0)),mb_Service_Notification+mb_TopMost+mb_OK);
- End;
-
- procedure TWeblookWM.WeblookWMDefaultActionAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- begin
- Response.Content := '<HTML>Invalid DLL call.</HTML>';
- end;
-
- Procedure TWeblookWM.CreateNamedPipe;
- Var iTries : Integer;
- Begin
- iTries := 0;
- While (iTries < 3) do Begin
- Inc(iTries);
- hPipe := CreateFile(strPipeName,Generic_Read Or Generic_Write,0,nil,Open_Existing,0,0);
- If (hPipe <> Invalid_Handle_Value) Then Exit; { succesfully opened the pipe }
- If (GetLastError = Error_Pipe_Busy) Then Begin
- { the pipe is busy, so wait 5 seconds for it become available }
- WaitNamedPipe(strPipeName,5*1000);
- End
- Else Raise Exception.Create('Cannot open pipe: '+SysErrorMessage(GetLastError)); { other error occurred }
- End;
- Raise Exception.Create('Timeout reached while opening pipe');
- End;
-
- Function TWeblookWM.ReadPipeRequestResponse(strRequest : String) : String;
- Var
- iBW,iBR : Cardinal; { bytes written, bytes read }
- cResponse : Array[0..1023] of Char; { 1kB }
-
- Begin
- CreateNamedPipe; { create the pipe for reading }
- Result := '';
- If (Not WriteFile(hPipe,Pointer(strRequest)^,Length(strRequest),iBW,nil)) Then { write the request }
- Raise Exception.Create('Cannot write to pipe: '+SysErrorMessage(GetLastError));
- If ReadFile(hPipe,cResponse,SizeOf(cResponse)-1,iBR,nil) Then Begin { read the response }
- cResponse[iBR] := #0; { properly terminate the string }
- Result := String(cResponse);
- End
- Else Raise Exception.Create('Cannot read from pipe: '+SysErrorMessage(GetLastError));
- CloseHandle(hPipe);
- End;
-
- procedure TWeblookWM.WeblookWMCalendarAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- Var
- iCol,iRow,iDay : Integer;
- strHTML,strRows : String;
- wY,wM,wD : Word;
-
- Function GetMonthLinkHTML(iMonth,iYear,iDelta : Integer) : String;
- Begin
- iMonth := iMonth+iDelta;
- If (iMonth < 1) Then Begin
- iMonth := 12;
- Dec(iYear);
- End
- Else If (iMonth > 12) Then Begin
- iMonth := 1;
- Inc(iYear);
- End;
- { is the year in range? }
- If (iYear >= ciFirstYear) And (iYear <= ciLastYear) Then
- Result := '<A HREF="/scripts/weblook.dll/calendar?year='+IntToStr(iYear)+'&month='+IntToStr(iMonth)+'">'
- Else Result := '';
- If (iDelta < 0) Then Result := Result+'Previous'
- Else If (iDelta > 0) Then Result := Result+'Next'
- Else Result := Result+'Current';
- If (iYear >= ciFirstYear) And (iYear <= ciLastYear) Then Result := Result+'</A>';
- End;
-
- begin
- { check to see which month we should display }
- DecodeDate(Date,wY,wM,wD);
- Try
- iYear := StrToInt(Request.QueryFields.Values['year']);
- If (iYear < ciFirstYear) Or (iYear > ciLastYear) Then iYear := wY;
- iMonth := StrToInt(Request.QueryFields.Values['month']);
- If (iMonth < 1) Or (iMonth > 12) Then iMonth := wM;
- Except
- iYear := wY; { current month }
- iMonth := wM;
- End;
- { clear the calendar grid by setting a non-blocking space for HTML table cells to display }
- For iCol := 0 to 6 do Begin
- For iRow := 1 to 6 do cgCalendarGrid[iCol,iRow] := ' ';
- End;
- { then fill in the day numbers in the calendar grid }
- iCol := DayOfWeek(EncodeDate(iYear,iMonth,1))-1;
- iCol := (iCol-ciStartOfWeek) mod 7;
- If (iCol < 0) Then iCol := 7+iCol;
- iRow := 1;
- For iDay := 1 to MonthDays[IsLeapYear(iYear),iMonth] do Begin
- cgCalendarGrid[iCol,iRow] := IntToStr(iDay)+'<BR>'+
- ReadPipeRequestResponse('CALENDAR '+IntToStr(Trunc(EncodeDate(iYear,iMonth,iDay))));
- Inc(iCol);
- If (iCol > 6) Then Begin
- iCol := 0;
- Inc(iRow);
- End;
- End;
- { finally generate the HTML code for the calendar }
- strHTML := StringReplace(cstrCalendarHTML,'%month%',LongMonthNames[iMonth]+' '+IntToStr(iYear),[]);
- { create the previous and next links }
- strHTML := StringReplace(strHTML,'%prev%',GetMonthLinkHTML(iMonth,iYear,-1),[]);
- strHTML := StringReplace(strHTML,'%next%',GetMonthLinkHTML(iMonth,iYear,+1),[]);
- strHTML := StringReplace(strHTML,'%current%',GetMonthLinkHTML(wM,wY,0),[]);
- { create the table content }
- For iRow := 1 to 6 do Begin
- strRows := strRows+' <TR>'+CRLF;
- For iCol := 0 to 6 do Begin
- strRows := strRows+' <TD WIDTH="102"><FONT SIZE="2" FACE="Arial, Helvetica, sans-serif">'+
- cgCalendarGrid[iCol,iRow]+'</FONT></TD>'+CRLF;
- End;
- strRows := strRows+' </TR>'+CRLF;
- End;
- strHTML := StringReplace(strHTML,'%rows%',strRows,[]);
- Response.Content := strHTML;
- end;
-
- procedure TWeblookWM.WeblookWMContactsAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- Var
- iIndex,iCount : Integer;
- strRows : String;
-
- begin
- iCount := StrToInt(ReadPipeRequestResponse('CONTACT COUNT'));
- For iIndex := 0 to iCount-1 do Begin
- If ((iIndex mod 3) = 0) Then Begin
- If (iIndex > 0) Then strRows := strRows+' </TR>'+CRLF;
- strRows := strRows+' <TR VALIGN="TOP">'+CRLF;
- End;
- strRows := strRows+' <TD WIDTH="33%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">'+CRLF+
- ReadPipeRequestResponse('CONTACT '+IntToStr(iIndex))+CRLF+'</FONT></TD>'+CRLF;
- End;
- Response.Content := StringReplace(cstrContactsHTML,'%rows%',strRows,[]);
- end;
-
- procedure TWeblookWM.WeblookWMTasksAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- Var
- iIndex,iCount : Integer;
- strRows : String;
-
- begin
- iCount := StrToInt(ReadPipeRequestResponse('TASK COUNT'));
- For iIndex := 0 to iCount-1 do Begin
- If ((iIndex mod 3) = 0) Then Begin
- If (iIndex > 0) Then strRows := strRows+' </TR>'+CRLF;
- strRows := strRows+' <TR VALIGN="TOP">'+CRLF;
- End;
- strRows := strRows+' <TD WIDTH="33%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">'+CRLF+
- ReadPipeRequestResponse('TASK '+IntToStr(iIndex))+CRLF+'</FONT></TD>'+CRLF;
- End;
- Response.Content := StringReplace(cstrTasksHTML,'%rows%',strRows,[]);
- end;
-
- procedure TWeblookWM.WeblookWMNotesAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- Var
- iIndex,iCount : Integer;
- strRows : String;
-
- begin
- iCount := StrToInt(ReadPipeRequestResponse('NOTE COUNT'));
- For iIndex := 0 to iCount-1 do Begin
- strRows := strRows+' <TR VALIGN="TOP">'+CRLF+
- ReadPipeRequestResponse('NOTE '+IntToStr(iIndex))+
- ' </TR>'+CRLF;
- End;
- Response.Content := StringReplace(cstrNotesHTML,'%rows%',strRows,[]);
- end;
-
- procedure TWeblookWM.WeblookWMInboxAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- Var
- iIndex,iCount : Integer;
- strRows : String;
-
- begin
- iCount := StrToInt(ReadPipeRequestResponse('INBOX COUNT'));
- For iIndex := 0 to iCount-1 do
- strRows := strRows+ReadPipeRequestResponse('INBOX '+IntToStr(iIndex));
- Response.Content := StringReplace(cstrInboxHTML,'%rows%',strRows,[]);
- end;
-
- procedure TWeblookWM.WeblookWMTodayAction(Sender: TObject;
- Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
- Var strHTML : String;
-
- Function CalcAppointmentCount(dtDate : TDateTime) : String;
- Var
- strTemp : String;
- iCount,iPos : Integer;
-
- Begin
- iCount := -1;
- strTemp := ReadPipeRequestResponse('CALENDAR '+IntToStr(Trunc(dtDate)));
- { calculate how many <BR>'s there are on the string }
- iPos := Pos('<BR>',strTemp);
- While (iPos > 0) do Begin
- Inc(iCount);
- Delete(strTemp,1,iPos+3);
- iPos := Pos('<BR>',strTemp);
- End;
- Result := IntToStr(iCount);
- End;
-
- begin
- strHTML := StringReplace(cstrTodayHTML,'%date%',DateTimeToStr(Now),[]);
- strHTML := StringReplace(strHTML,'%inbox%',ReadPipeRequestResponse('INBOX COUNT'),[]);
- strHTML := StringReplace(strHTML,'%caltoday%',CalcAppointmentCount(Date),[]);
- strHTML := StringReplace(strHTML,'%caltomorrow%',CalcAppointmentCount(Date+1),[]);
- Response.Content := strHTML;
- end;
-
- end.
-