IDE.Wizard.WizardFormFilesHelper.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. unit IDE.Wizard.WizardFormFilesHelper;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Helper to avoid duplicate code between WizardForm and FilesDesignerForm
  8. }
  9. interface
  10. uses
  11. Windows, Classes, Forms, StdCtrls,
  12. DropListBox, IDE.Wizard.WizardFileForm;
  13. type
  14. TWizardFormFilesHelper = class
  15. private
  16. FWizardFiles: TList;
  17. FForm: TForm;
  18. FNotCreateAppDirCheck: TCheckBox;
  19. FFilesListBox: TDropListBox;
  20. FEditButton: TButton;
  21. FRemoveButton: TButton;
  22. function AddWizardFile(const Source: String; const Options: TWizardFileOptions): PWizardFile;
  23. function GetWizardFilesCount: NativeInt;
  24. procedure UpdateWizardFiles;
  25. procedure UpdateWizardFilesButtons;
  26. procedure FilesListBoxClick(Sender: TObject);
  27. procedure FilesListBoxDblClick(Sender: TObject);
  28. procedure FilesListBoxDropFile(Sender: TDropListBox; const FileName: String);
  29. procedure AddButtonClick(Sender: TObject);
  30. procedure AddDirButtonClick(Sender: TObject);
  31. procedure AddDownloadButtonClick(Sender: TObject);
  32. procedure EditButtonClick(Sender: TObject);
  33. procedure RemoveButtonClick(Sender: TObject);
  34. public
  35. constructor Create(const Form: TForm;
  36. const NotCreateAppDirCheck: TCheckBox; const FilesListBox: TDropListBox;
  37. const AddButton, AddDirButton, AddDownloadButton, EditButton, RemoveButton: TButton);
  38. destructor Destroy; override;
  39. procedure AddScript(var Files: String); overload;
  40. procedure AddScript(var Files: String; out HasExtractArchive: Boolean); overload;
  41. property FilesCount: NativeInt read GetWizardFilesCount;
  42. end;
  43. implementation
  44. uses
  45. SysUtils, UITypes, Dialogs,
  46. Shared.CommonFunc.Vcl, Shared.CommonFunc, BrowseFunc, PathFunc,
  47. IDE.Messages;
  48. constructor TWizardFormFilesHelper.Create(const Form: TForm;
  49. const NotCreateAppDirCheck: TCheckBox; const FilesListBox: TDropListBox;
  50. const AddButton, AddDirButton, AddDownloadButton, EditButton, RemoveButton: TButton);
  51. begin
  52. inherited Create;
  53. FWizardFiles := TList.Create;
  54. FForm := Form;
  55. FNotCreateAppDirCheck := NotCreateAppDirCheck;
  56. FFilesListBox := FilesListBox;
  57. FEditButton := EditButton;
  58. FRemoveButton := RemoveButton;
  59. FilesListBox.OnClick := FilesListBoxClick;
  60. FilesListBox.OnDblClick := FilesListBoxDblClick;
  61. FilesListBox.OnDropFile := FilesListBoxDropFile;
  62. AddButton.OnClick := AddButtonClick;
  63. AddDirButton.OnClick := AddDirButtonClick;
  64. AddDownloadButton.OnClick := AddDownloadButtonClick;
  65. EditButton.OnClick := EditButtonClick;
  66. RemoveButton.OnClick := RemoveButtonClick;
  67. UpdateWizardFilesButtons;
  68. end;
  69. destructor TWizardFormFilesHelper.Destroy;
  70. begin
  71. for var I := 0 to FWizardFiles.Count-1 do
  72. Dispose(PWizardFile(FWizardFiles[i]));
  73. FWizardFiles.Free;
  74. end;
  75. function TWizardFormFilesHelper.AddWizardFile(const Source: String; const Options: TWizardFileOptions): PWizardFile;
  76. var
  77. WizardFile: PWizardFile;
  78. begin
  79. New(WizardFile);
  80. WizardFile.Source := Source;
  81. WizardFile.Options := Options;
  82. WizardFile.DestRootDirIsConstant := True;
  83. if not FNotCreateAppDirCheck.Checked then
  84. WizardFile.DestRootDir := '{app}'
  85. else
  86. WizardFile.DestRootDir := '{win}';
  87. WizardFile.DestSubDir := '';
  88. WizardFile.DestName := '';
  89. WizardFile.ExternalSize := 0;
  90. FWizardFiles.Add(WizardFile);
  91. Result := WizardFile;
  92. end;
  93. function TWizardFormFilesHelper.GetWizardFilesCount: NativeInt;
  94. begin
  95. Result := FWizardFiles.Count;
  96. end;
  97. procedure TWizardFormFilesHelper.UpdateWizardFiles;
  98. var
  99. WizardFile: PWizardFile;
  100. begin
  101. FFilesListBox.Items.BeginUpdate;
  102. FFilesListBox.Items.Clear;
  103. for var I := 0 to FWizardFiles.Count-1 do begin
  104. WizardFile := FWizardFiles[i];
  105. FFilesListBox.Items.Add(WizardFile.Source);
  106. end;
  107. FFilesListBox.Items.EndUpdate;
  108. UpdateHorizontalExtent(FFilesListBox);
  109. end;
  110. procedure TWizardFormFilesHelper.UpdateWizardFilesButtons;
  111. var
  112. Enabled: Boolean;
  113. begin
  114. Enabled := FFilesListBox.ItemIndex >= 0;
  115. FEditButton.Enabled := Enabled;
  116. FRemoveButton.Enabled := Enabled;
  117. end;
  118. procedure TWizardFormFilesHelper.FilesListBoxClick(Sender: TObject);
  119. begin
  120. UpdateWizardFilesButtons;
  121. end;
  122. procedure TWizardFormFilesHelper.FilesListBoxDblClick(Sender: TObject);
  123. begin
  124. if FEditButton.Enabled then
  125. FEditButton.Click;
  126. end;
  127. procedure TWizardFormFilesHelper.FilesListBoxDropFile(Sender: TDropListBox;
  128. const FileName: String);
  129. begin
  130. if DirExists(FileName) then
  131. AddWizardFile(AddBackslash(FileName) + '*', [foRecurseSubDirs, foCreateAllSubDirs])
  132. else
  133. AddWizardFile(FileName, []);
  134. UpdateWizardFiles;
  135. UpdateWizardFilesButtons;
  136. end;
  137. procedure TWizardFormFilesHelper.AddButtonClick(Sender: TObject);
  138. var
  139. FileList: TStringList;
  140. I: Integer;
  141. begin
  142. FileList := TStringList.Create;
  143. try
  144. if NewGetOpenFileNameMulti('', FileList, '', SWizardAllFilesFilter, '', FForm.Handle) then begin
  145. FileList.Sort;
  146. for I := 0 to FileList.Count-1 do
  147. AddWizardFile(FileList[I], []);
  148. UpdateWizardFiles;
  149. end;
  150. finally
  151. FileList.Free;
  152. end
  153. end;
  154. procedure TWizardFormFilesHelper.AddDirButtonClick(Sender: TObject);
  155. var
  156. Path: String;
  157. Recurse: Boolean;
  158. begin
  159. Path := '';
  160. if BrowseForFolder(SWizardAppFiles3, Path, FForm.Handle, False) then begin
  161. case MsgBox(Format(SWizardAppFilesSubDirsMessage, [Path]), '', mbConfirmation, MB_YESNOCANCEL) of
  162. IDYES: Recurse := True;
  163. IDNO: Recurse := False;
  164. else
  165. Exit;
  166. end;
  167. var Options: TWizardFileOptions;
  168. if Recurse then
  169. Options := [foRecurseSubDirs, foCreateAllSubDirs]
  170. else
  171. Options := [];
  172. AddWizardFile(AddBackslash(Path) + '*', Options);
  173. UpdateWizardFiles;
  174. end;
  175. end;
  176. procedure TWizardFormFilesHelper.AddDownloadButtonClick(Sender: TObject);
  177. const
  178. DestNamePrompts: array [Boolean] of string = (SWizardAppFilesDownloadDestNamePrompt, SWizardAppFilesDownloadArchiveDestNamePrompt);
  179. begin
  180. var Source := 'https://www.example.com/MyProg.7z';
  181. repeat
  182. if not InputQuery(FForm.Caption, SWizardAppFilesDownloadSourcePrompt, Source) then
  183. Exit;
  184. until Source <> '';
  185. const ExtractArchive = MsgBox(SWizardAppFilesDownloadExtractArchiveMessage, '', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = IDYES;
  186. var DestName := 'MyProg.7z';
  187. repeat
  188. if not InputQuery(FForm.Caption, DestNamePrompts[ExtractArchive], DestName) then
  189. Exit;
  190. until DestName <> '';
  191. var ExternalSizeAsString := '';
  192. var ExternalSize: Extended;
  193. repeat
  194. if not InputQuery(FForm.Caption, SWizardAppFilesDownloadExternalSizePrompt, ExternalSizeAsString) then
  195. Exit;
  196. until TryStrToFloat(ExternalSizeAsString, ExternalSize);
  197. var Options: TWizardFileOptions := [foDownload];
  198. if ExtractArchive then
  199. Options := Options + [foExtractArchive, foRecurseSubDirs, foCreateAllSubDirs];
  200. const WizardFile = AddWizardFile(Source, Options);
  201. WizardFile.DestName := DestName;
  202. WizardFile.ExternalSize := Round(ExternalSize*1024*1024);
  203. UpdateWizardFiles;
  204. end;
  205. procedure TWizardFormFilesHelper.EditButtonClick(Sender: TObject);
  206. var
  207. WizardFileForm: TWizardFileForm;
  208. Index: Integer;
  209. begin
  210. WizardFileForm := TWizardFileForm.Create(Application);
  211. try
  212. Index := FFilesListBox.ItemIndex;
  213. WizardFileForm.AllowAppDestRootDir := not FNotCreateAppDirCheck.Checked;
  214. WizardFileForm.WizardFile := FWizardFiles[Index];
  215. if WizardFileForm.ShowModal = mrOk then begin
  216. UpdateWizardFiles;
  217. FFilesListBox.ItemIndex := Index;
  218. FFilesListBox.TopIndex := Index;
  219. UpdateWizardFilesButtons;
  220. end;
  221. finally
  222. WizardFileForm.Free;
  223. end;
  224. end;
  225. procedure TWizardFormFilesHelper.RemoveButtonClick(Sender: TObject);
  226. var
  227. I: Integer;
  228. begin
  229. I := FFilesListBox.ItemIndex;
  230. Dispose(PWizardFile(FWizardFiles[I]));
  231. FWizardFiles.Delete(I);
  232. UpdateWizardFiles;
  233. UpdateWizardFilesButtons;
  234. end;
  235. procedure TWizardFormFilesHelper.AddScript(var Files: String; out HasExtractArchive: Boolean);
  236. var
  237. WizardFile: PWizardFile;
  238. begin
  239. var AddedVerificationNote := False;
  240. for var I := 0 to FWizardFiles.Count-1 do begin
  241. WizardFile := FWizardFiles[I];
  242. if (foDownload in WizardFile.Options) and not AddedVerificationNote then begin
  243. Files := Files + '; NOTE: Use the "issigverify" flag or the "Hash" parameter to verify downloads' + SNewLine;
  244. AddedVerificationNote := True;
  245. end;
  246. if foExtractArchive in WizardFile.Options then
  247. HasExtractArchive := True;
  248. Files := Files + 'Source: "' + WizardFile.Source + '"; DestDir: "' + RemoveBackslashUnlessRoot(AddBackslash(WizardFile.DestRootDir) + WizardFile.DestSubDir) + '"';
  249. if WizardFile.DestName <> '' then
  250. Files := Files + '; DestName: "' + WizardFile.DestName + '"';
  251. if WizardFile.ExternalSize <> 0 then
  252. Files := Files + '; ExternalSize: "' + WizardFile.ExternalSize.ToString + '"';
  253. Files := Files + '; Flags: ignoreversion';
  254. if WizardFile.Options * [foDownload, foExtractArchive] <> [] then
  255. Files := Files + ' external';
  256. if foDownload in WizardFile.Options then
  257. Files := Files + ' download';
  258. if foExtractArchive in WizardFile.Options then
  259. Files := Files + ' extractarchive';
  260. if foRecurseSubDirs in WizardFile.Options then
  261. Files := Files + ' recursesubdirs';
  262. if foCreateAllSubDirs in WizardFile.Options then
  263. Files := Files + ' createallsubdirs';
  264. Files := Files + SNewLine;
  265. end;
  266. end;
  267. procedure TWizardFormFilesHelper.AddScript(var Files: String);
  268. begin
  269. var HasExtractArchive: Boolean;
  270. AddScript(Files, HasExtractArchive);
  271. end;
  272. end.