EncoderPlayground.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 23936: EncoderPlayground.pas
  11. {
  12. { Rev 1.1 04/10/2003 15:22:50 CCostelloe
  13. { Emails generated now have the same date
  14. }
  15. {
  16. { Rev 1.0 26/09/2003 00:04:08 CCostelloe
  17. { Initial
  18. }
  19. unit EncoderPlayground;
  20. interface
  21. {$I IdCompilerDefines.inc}
  22. uses
  23. EncoderBox,
  24. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  25. Dialogs, ComCtrls, Menus, ActnList, ImgList, ToolWin, ExtCtrls, StdCtrls,
  26. BXBubble,
  27. IdMessage, Spin;
  28. type
  29. TformEncoderPlayground = class(TForm)
  30. lboxMessages: TListBox;
  31. Splitter1: TSplitter;
  32. alstMain: TActionList;
  33. MainMenu1: TMainMenu;
  34. PopupMenu1: TPopupMenu;
  35. ToolBar1: TToolBar;
  36. ImageList1: TImageList;
  37. actnFile_Exit: TAction;
  38. actnTest_Test: TAction;
  39. File1: TMenuItem;
  40. actnFileTest1: TMenuItem;
  41. Exit1: TMenuItem;
  42. ToolButton1: TToolButton;
  43. Panel2: TPanel;
  44. Panel1: TPanel;
  45. Label1: TLabel;
  46. lablFilename: TLabel;
  47. actnTest_Emit: TAction;
  48. Exit2: TMenuItem;
  49. Emit1: TMenuItem;
  50. Emit2: TMenuItem;
  51. actnTest_Verify: TAction;
  52. estandVerify1: TMenuItem;
  53. estandVerify2: TMenuItem;
  54. eset1: TMenuItem;
  55. Label4: TLabel;
  56. lablErrors: TLabel;
  57. N1: TMenuItem;
  58. actnTest_VerifyAll: TAction;
  59. VerifyAll1: TMenuItem;
  60. bublEncoderPlayground: TBXBubble;
  61. Label5: TLabel;
  62. pctlMessage: TPageControl;
  63. TabSheet1: TTabSheet;
  64. memoRaw: TMemo;
  65. Panel3: TPanel;
  66. Label2: TLabel;
  67. Memo1: TMemo;
  68. Label3: TLabel;
  69. ComboBox1: TComboBox;
  70. Label6: TLabel;
  71. Label7: TLabel;
  72. ComboBox2: TComboBox;
  73. Label8: TLabel;
  74. Label9: TLabel;
  75. ListBox1: TListBox;
  76. Label10: TLabel;
  77. Edit1: TEdit;
  78. Button2: TButton;
  79. Button3: TButton;
  80. RadioGroup1: TRadioGroup;
  81. OpenDialog1: TOpenDialog;
  82. Label11: TLabel;
  83. ComboBox3: TComboBox;
  84. Button4: TButton;
  85. Label12: TLabel;
  86. ComboBox4: TComboBox;
  87. Bevel1: TBevel;
  88. Bevel2: TBevel;
  89. Button5: TButton;
  90. Button1: TButton;
  91. TabSheet2: TTabSheet;
  92. memoCorrect: TMemo;
  93. Label13: TLabel;
  94. Edit2: TEdit;
  95. Bevel3: TBevel;
  96. Button6: TButton;
  97. Button7: TButton;
  98. Button8: TButton;
  99. Label14: TLabel;
  100. ComboBox5: TComboBox;
  101. SpinEdit1: TSpinEdit;
  102. Label15: TLabel;
  103. Label16: TLabel;
  104. Edit3: TEdit;
  105. procedure actnFile_ExitExecute(Sender: TObject);
  106. procedure actnTest_TestExecute(Sender: TObject);
  107. procedure alstMainUpdate(Action: TBasicAction; var Handled: Boolean);
  108. procedure lboxMessagesDblClick(Sender: TObject);
  109. procedure FormCreate(Sender: TObject);
  110. procedure actnTest_VerifyAllExecute(Sender: TObject);
  111. procedure bublEncoderPlaygroundPlayground(Sender: TBXBubble);
  112. procedure Button4Click(Sender: TObject);
  113. procedure Edit1Change(Sender: TObject);
  114. procedure Button2Click(Sender: TObject);
  115. procedure ListBox1Click(Sender: TObject);
  116. procedure Button3Click(Sender: TObject);
  117. procedure Button1Click(Sender: TObject);
  118. procedure Button5Click(Sender: TObject);
  119. procedure Button6Click(Sender: TObject);
  120. procedure Button7Click(Sender: TObject);
  121. procedure Button8Click(Sender: TObject);
  122. private
  123. protected
  124. FDataPath: string;
  125. FEncoderBox: TEncoderBox;
  126. public
  127. TheMsg: TIdMessage;
  128. procedure ResetFieldsToDefaults;
  129. procedure SetupEmail;
  130. end;
  131. var
  132. formEncoderPlayground: TformEncoderPlayground;
  133. implementation
  134. {$R *.dfm}
  135. uses
  136. IdGlobal, IdText, IdAttachmentFile,
  137. IdCoreGlobal, EmailSender;
  138. const
  139. EncoderBody = 'This is the text for the sample body.' + EOL + 'This is a deliberately long line, and the reason it is a long line is that it should test whether the encoder breaks and reassembles it properly since it is longer than any line length I can think of.' + EOL;
  140. procedure TformEncoderPlayground.actnFile_ExitExecute(Sender: TObject);
  141. begin
  142. Close;
  143. end;
  144. procedure TformEncoderPlayground.actnTest_TestExecute(Sender: TObject);
  145. var
  146. LFilename: string;
  147. begin
  148. LFilename := '';
  149. Screen.Cursor := crHourglass; try
  150. try
  151. if Assigned(FEncoderBox) then begin
  152. FreeAndNil(FEncoderBox);
  153. end;
  154. lablErrors.Caption := '';
  155. LFilename := Copy(lboxMessages.Items[lboxMessages.ItemIndex], 3, MaxInt);
  156. FEncoderBox := TEncoderBox.Create(Self);
  157. with FEncoderBox do begin
  158. TestMessage(FDataPath + LFilename, Sender = actnTest_Verify, Sender = actnTest_Emit);
  159. lablFilename.Caption := LFilename;
  160. lablErrors.Caption := '<None>';
  161. //Load generated message into raw message...
  162. GeneratedStream.Seek(0, soFromBeginning);
  163. memoRaw.Lines.LoadFromStream(GeneratedStream);
  164. //Load what the correct result is into memoCorrect...
  165. memoCorrect.Lines.LoadFromFile(TestMessageName);
  166. end;
  167. lboxMessages.Items[lboxMessages.ItemIndex] := '+'
  168. + Copy(lboxMessages.Items[lboxMessages.ItemIndex], 2, MaxInt);
  169. except
  170. on E: Exception do begin
  171. lablFilename.Caption := LFilename;
  172. lablErrors.Caption := E.Message;
  173. lboxMessages.Items[lboxMessages.ItemIndex] := '-'
  174. + Copy(lboxMessages.Items[lboxMessages.ItemIndex], 2, MaxInt);
  175. memoCorrect.Clear;
  176. if FEncoderBox.TestMessageName <> '' then memoCorrect.Lines.LoadFromFile(FEncoderBox.TestMessageName);
  177. memoRaw.Clear;
  178. if Assigned(FEncoderBox.GeneratedStream) then memoRaw.Lines.LoadFromStream(FEncoderBox.GeneratedStream);
  179. end;
  180. end;
  181. finally Screen.Cursor := crDefault; end;
  182. end;
  183. procedure TformEncoderPlayground.alstMainUpdate(Action: TBasicAction; var Handled: Boolean);
  184. begin
  185. actnTest_Test.Enabled := lboxMessages.ItemIndex > -1;
  186. Handled := True;
  187. end;
  188. procedure TformEncoderPlayground.lboxMessagesDblClick(Sender: TObject);
  189. begin
  190. // Here instead of linked at design because of .Enabled
  191. actnTest_Verify.Execute;
  192. end;
  193. procedure TformEncoderPlayground.FormCreate(Sender: TObject);
  194. var
  195. i: integer;
  196. LRec: TSearchRec;
  197. begin
  198. pctlMessage.ActivePage := TabSheet1;
  199. {CC: Don't append \ if already in AppDataDir...}
  200. FDataPath := bublEncoderPlayground.AppDataDir;
  201. if FDataPath[Length(FDataPath)] <> '\' then FDataPath := FDataPath + '\';
  202. FDataPath := FDataPath + 'Encoder\';
  203. //Find and display all the test messages...
  204. i := FindFirst(FDataPath + '*.ini', faAnyFile, LRec);
  205. try
  206. while i = 0 do begin
  207. lboxMessages.Items.Add(' ' + LRec.Name);
  208. i := FindNext(LRec);
  209. end;
  210. finally
  211. FindClose(LRec);
  212. end;
  213. //Set up the comboboxes with the options in TIdMessage...
  214. OpenDialog1.InitialDir := 'C:\';
  215. ComboBox1.Items.Add('Default');
  216. ComboBox1.Items.Add('base64');
  217. ComboBox1.Items.Add('quoted-printable');
  218. ComboBox2.Items.Add('Default');
  219. ComboBox2.Items.Add('True');
  220. ComboBox2.Items.Add('False');
  221. ComboBox3.Items.Add('Default');
  222. ComboBox3.Items.Add('7bit');
  223. ComboBox3.Items.Add('base64');
  224. ComboBox3.Items.Add('quoted-printable');
  225. ComboBox4.Items.Add('Default');
  226. ComboBox4.Items.Add('meMIME');
  227. ComboBox4.Items.Add('meUU');
  228. ComboBox4.Items.Add('meXX');
  229. ComboBox5.Items.Add('Default');
  230. ComboBox5.Items.Add('text/plain');
  231. ComboBox5.Items.Add('text/html');
  232. ComboBox5.Items.Add('multipart/alternative');
  233. ComboBox5.Items.Add('multipart/mixed');
  234. ResetFieldsToDefaults;
  235. end;
  236. procedure TformEncoderPlayground.actnTest_VerifyAllExecute(Sender: TObject);
  237. var
  238. i: Integer;
  239. begin
  240. for i := 0 to lboxMessages.Items.Count - 1 do begin
  241. lboxMessages.ItemIndex := i;
  242. actnTest_Verify.Execute;
  243. end;
  244. end;
  245. procedure TformEncoderPlayground.bublEncoderPlaygroundPlayground(Sender: TBXBubble);
  246. begin
  247. ShowModal;
  248. end;
  249. procedure TformEncoderPlayground.Button4Click(Sender: TObject);
  250. var
  251. sTemp: string;
  252. begin
  253. if RadioGroup1.ItemIndex = 0 then begin
  254. sTemp := 'TIdAttachment,';
  255. end else begin
  256. sTemp := 'TIdText,';
  257. end;
  258. sTemp := sTemp+ComboBox3.Items[ComboBox3.ItemIndex]+','+Edit1.Text;
  259. sTemp := sTemp+IntToStr(SpinEdit1.Value); //ParentPart
  260. sTemp := sTemp+ComboBox5.Items[ComboBox5.ItemIndex]; //ContentType
  261. ListBox1.Items.Add(sTemp);
  262. end;
  263. procedure TformEncoderPlayground.Edit1Change(Sender: TObject);
  264. begin
  265. if Edit1.Text = '' then begin
  266. Button4.Enabled := False;
  267. end else begin
  268. Button4.Enabled := True;
  269. end;
  270. end;
  271. procedure TformEncoderPlayground.Button2Click(Sender: TObject);
  272. begin
  273. if OpenDialog1.Execute = True then Edit1.Text := OpenDialog1.FileName;
  274. end;
  275. procedure TformEncoderPlayground.ListBox1Click(Sender: TObject);
  276. begin
  277. if ListBox1.ItemIndex = -1 then begin
  278. Button3.Enabled := False;
  279. end else begin
  280. Button3.Enabled := True;
  281. end;
  282. end;
  283. procedure TformEncoderPlayground.Button3Click(Sender: TObject);
  284. begin
  285. ListBox1.Items.Delete(ListBox1.ItemIndex);
  286. Button3.Enabled := False;
  287. end;
  288. procedure TformEncoderPlayground.Button1Click(Sender: TObject);
  289. var
  290. TempStream: TMemoryStream;
  291. begin
  292. memoRaw.Clear;
  293. memoCorrect.Clear;
  294. SetupEmail;
  295. //Finally save it to a stream...
  296. TempStream := TMemoryStream.Create;
  297. TheMsg.SaveToStream(TempStream);
  298. TempStream.Seek(0, soFromBeginning);
  299. memoRaw.Lines.LoadFromStream(TempStream);
  300. Button5.Enabled := True;
  301. end;
  302. procedure TformEncoderPlayground.SetupEmail;
  303. var
  304. i: integer;
  305. sTemp, sType, sEncoding, sFile, sContentType: string;
  306. nPos, nParentPart: integer;
  307. TheTextPart: TIdText;
  308. {$IFDEF INDY100}
  309. TheAttachment: TIdAttachmentFile;
  310. {$ELSE}
  311. TheAttachment: TIdAttachment;
  312. {$ENDIF}
  313. begin
  314. //Make the message from the control values...
  315. if Assigned(TheMsg) then FreeAndNil(TheMsg);
  316. TheMsg := TIdMessage.Create(nil);
  317. //Make sure the date will always be the same, else get different
  318. //outputs for the Date header...
  319. TheMsg.UseNowForDate := False;
  320. TheMsg.Date := EncodeDate(2011, 11, 11);
  321. if Memo1.Text <> '' then TheMsg.Body.Text := Memo1.Text;
  322. if ComboBox1.Items[ComboBox1.ItemIndex] <> 'Default' then TheMsg.ContentTransferEncoding := ComboBox1.Items[ComboBox1.ItemIndex];
  323. if ComboBox2.Items[ComboBox2.ItemIndex] = 'True' then begin
  324. TheMsg.ConvertPreamble := True;
  325. end else if ComboBox2.Items[ComboBox2.ItemIndex] = 'False' then begin
  326. TheMsg.ConvertPreamble := False;
  327. end;
  328. for i := 0 to ListBox1.Items.Count-1 do begin
  329. sTemp := ListBox1.Items.Strings[i];
  330. nPos := Pos(',', sTemp);
  331. sType := Copy(sTemp, 1, nPos-1);
  332. sTemp := Copy(sTemp, nPos+1, MAXINT);
  333. nPos := Pos(',', sTemp);
  334. sEncoding := Copy(sTemp, 1, nPos-1);
  335. sTemp := Copy(sTemp, nPos+1, MAXINT);
  336. sContentType := '';
  337. nParentPart := -999;
  338. nPos := Pos(',', sTemp);
  339. if nPos > 0 then begin //ParentPart+ContentType are optional
  340. sFile := Copy(sTemp, 1, nPos-1);
  341. sTemp := Copy(sTemp, nPos+1, MAXINT);
  342. nPos := Pos(',', sTemp);
  343. sContentType := Copy(sTemp, nPos+1, MAXINT);
  344. sTemp := Copy(sTemp, 1, nPos-1);
  345. nParentPart := StrToInt(sTemp);
  346. end else begin
  347. sFile := sTemp;
  348. end;
  349. if sType = 'TIdText' then begin
  350. TheTextPart := TIdText.Create(TheMsg.MessageParts);
  351. TheTextPart.Body.LoadFromFile(sFile);
  352. if sEncoding <> 'Default' then TheTextPart.ContentTransfer := sEncoding;
  353. if ((sContentType <> '') and (sContentType <> 'Default')) then TheTextPart.ContentType := sContentType;
  354. {$IFDEF INDY100}
  355. if nParentPart <> -999 then TheTextPart.ParentPart := nParentPart;
  356. {$ENDIF}
  357. end else begin
  358. {$IFDEF INDY100}
  359. TheAttachment := TIdAttachmentFile.Create(TheMsg.MessageParts, sFile);
  360. {$ELSE}
  361. TheAttachment := TIdAttachment.Create(TheMsg.MessageParts, sFile);
  362. {$ENDIF}
  363. if sEncoding <> 'Default' then TheAttachment.ContentTransfer := sEncoding;
  364. if ((sContentType <> '') and (sContentType <> 'Default')) then TheAttachment.ContentType := sContentType;
  365. {$IFDEF INDY100}
  366. if nParentPart <> -999 then TheAttachment.ParentPart := nParentPart;
  367. {$ENDIF}
  368. end;
  369. end;
  370. if TheMsg.Encoding <> meDefault then ShowMessage('Warning: Message encoding was not initially meDefault???');
  371. if ComboBox4.Items[ComboBox4.ItemIndex] = 'meMIME' then begin
  372. TheMsg.Encoding := meMIME;
  373. end else if ComboBox4.Items[ComboBox4.ItemIndex] = 'meUU' then begin
  374. TheMsg.Encoding := meUU;
  375. end else if ComboBox4.Items[ComboBox4.ItemIndex] = 'meXX' then begin
  376. TheMsg.Encoding := meXX;
  377. end;
  378. if Edit3.Text <> '' then begin
  379. TheMsg.ContentType := Edit3.Text;
  380. end;
  381. end;
  382. procedure TformEncoderPlayground.Button5Click(Sender: TObject);
  383. var
  384. ExtractPath: string;
  385. TestName: string;
  386. TestIni: TStringList;
  387. i: Integer;
  388. AttachmentName, PortedAttachmentName: string;
  389. nPos: Integer;
  390. sContentType, sType, sEncoding, sParentPath, sTemp: string;
  391. begin
  392. if MessageDlg('Warning: Dont add tests in this manner unless you are sure they are valid tests. Add this test?',
  393. mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
  394. //Make sure we don't have a test of this name already...
  395. TestName := Edit2.Text;
  396. if TestName = '' then begin
  397. ShowMessage('You must enter a test name in the edit box provided');
  398. Exit;
  399. end;
  400. if Pos('.', TestName) > 0 then begin
  401. ShowMessage('Test name may not include a period');
  402. Exit;
  403. end;
  404. if FileExists(FDataPath+TestName+'.ini') then begin
  405. ShowMessage('This test name exists already, try another.');
  406. Exit;
  407. end;
  408. //Create the test directory...
  409. ExtractPath := FDataPath + ChangeFileExt(TestName, '') + '\';
  410. ForceDirectories(ExtractPath);
  411. //Copy the generated message to it as a .msg...
  412. memoRaw.Lines.SaveToFile(ExtractPath+TestName+'.msg');
  413. //Write out the INI...
  414. TestIni := TStringList.Create;
  415. if Memo1.Text <> '' then begin
  416. for i := 0 to Memo1.Lines.Count-1 do begin
  417. TestIni.Add('Body'+IntToStr(i)+'='+Memo1.Lines[i]);
  418. end;
  419. end;
  420. if ComboBox1.Items[ComboBox1.ItemIndex] <> 'Default' then TestIni.Add('ContentTransferEncoding='+ComboBox1.Items[ComboBox1.ItemIndex]);
  421. if ComboBox2.Items[ComboBox2.ItemIndex] <> 'Default' then TestIni.Add('ConvertPreamble='+ComboBox2.Items[ComboBox2.ItemIndex]);
  422. if ComboBox4.Items[ComboBox4.ItemIndex] <> 'Default' then TestIni.Add('Encoding='+ComboBox4.Items[ComboBox4.ItemIndex]);
  423. if Edit3.Text <> '' then TestIni.Add('ContentType='+Edit3.Text);
  424. //Copy any attachments into test dir, note the same attachment may be in more than one part...
  425. for i := 0 to ListBox1.Items.Count-1 do begin
  426. AttachmentName := ListBox1.Items.Strings[i];
  427. nPos := Pos(',', AttachmentName);
  428. sType := Copy(AttachmentName, 1, nPos-1);
  429. AttachmentName := Copy(AttachmentName, nPos+1, MAXINT);
  430. nPos := Pos(',', AttachmentName);
  431. sEncoding := Copy(AttachmentName, 1, nPos-1);
  432. AttachmentName := Copy(AttachmentName, nPos+1, MAXINT);
  433. nPos := Pos(',', AttachmentName);
  434. AttachmentName := Copy(AttachmentName, 1, nPos-1);
  435. sTemp := Copy(AttachmentName, nPos+1, MAXINT);
  436. nPos := Pos(',', sTemp);
  437. sContentType := Copy(sTemp, nPos+1, MAXINT);
  438. sParentPath := Copy(sTemp, 1, nPos-1);
  439. PortedAttachmentName := ExtractPath+ExtractFileName(AttachmentName);
  440. CopyFile(PAnsiChar(AttachmentName), PAnsiChar(PortedAttachmentName), False);
  441. //Update our INI with the ported path...
  442. TestIni.Add('Part'+IntToStr(i)+'='+sType+','+sEncoding+','+PortedAttachmentName+','+sParentPath+','+sContentType);
  443. end;
  444. TestIni.SaveToFile(FDataPath+TestName+'.ini');
  445. ShowMessage('Test message '+TestName+' successfully set up, you may need to restart to see it listed.');
  446. end;
  447. end;
  448. procedure TformEncoderPlayground.Button6Click(Sender: TObject);
  449. begin
  450. Memo1.Text := EncoderBody;
  451. end;
  452. procedure TformEncoderPlayground.ResetFieldsToDefaults;
  453. begin
  454. Memo1.Text := '';
  455. Edit1.Text := '';
  456. Edit2.Text := '';
  457. Edit3.Text := '';
  458. ComboBox1.ItemIndex := 0;
  459. ComboBox2.ItemIndex := 0;
  460. ComboBox3.ItemIndex := 0;
  461. ComboBox4.ItemIndex := 0;
  462. ComboBox5.ItemIndex := 0;
  463. Button3.Enabled := False;
  464. Button4.Enabled := False;
  465. Button5.Enabled := False;
  466. ListBox1.Items.Clear;
  467. end;
  468. procedure TformEncoderPlayground.Button7Click(Sender: TObject);
  469. begin
  470. ResetFieldsToDefaults;
  471. end;
  472. procedure TformEncoderPlayground.Button8Click(Sender: TObject);
  473. begin
  474. //This sends an email so you can see if that client can decode it...
  475. SendEmail.ShowModal;
  476. end;
  477. end.