Updates to Client for compatibility with the next pithos+ server
[pithos-ms-client] / trunk / Pithos.Installer / it_download.iss
1 [Files]
2 Source: {#emit ReadReg(HKEY_LOCAL_MACHINE,'Software\Sherlock Software\InnoTools\Downloader','InstallPath','')}\itdownload.dll; Flags: dontcopy; DestDir: {tmp}
3
4 [Code]
5 (*
6  Inno Tools Downloader DLL
7  Copyright (C) Sherlock Software 2008
8  Version 0.3.5
9
10  Contact:
11   The author, Nicholas Sherlock, at nick@sherlocksoftware.org.
12   Comments, questions and suggestions welcome.
13
14  Website:
15   http://www.sherlocksoftware.org/
16
17  History:
18   0.3.5 - Moved strings used in the updater example to the language file, so that they
19           may be more easily translated.
20           Added event functions to support the example of integration with InnoTools tray.
21           Fixes bugs that could cause the download to hang until
22           some mouse movements were received.
23           Allows "detailed mode" to be turned on with ITD_SetOption
24           Allows the HTTP agent to be set with ITD_SetOption
25   0.3.4 - Added Brazilian Portuguese translation by Ronaldo Souza
26   0.3.3 - The "Hilbrand Edskes" release :), lots of suggestions and corrections from him, thank you!
27           The "Retry" button is now translated.
28           The "Current file" progress bar is hidden if only one file is being downloaded.
29           The page's title and description are updated in the case of failed downloads.
30           Several updates to the translations.
31           Several small GUI fixes.
32   0.3.2 - Minor tweaks to the English language file and the translation example script (example4.iss)
33           Added Dutch translation by Hilbrand Edskes
34           Added French translation by Néo
35   0.3.1 - Added language file examples, fixed several missing language strings
36           Preliminary support for proxy server autodetection
37           Allows the size of a file to be queried with ITD_GetFileSize
38           Several small bugfixes
39   0.3.0 - Properly supports timeouts.
40           Fixes bug with time remaining.
41           DLL is required again.
42           Supports localization through ITD_LoadStrings, ITD_SetString
43           Add mirrors for files
44           Post HTTP documents
45           Quick view and detailed view
46   0.2.2 - Fixed empty strings '' in calls and added timeouts
47   0.2.1 - Renamed identifiers to avoid name clashes
48   0.2.0 - Converted from DLL to pure native code
49 *)
50
51 procedure ITD_Cancel;
52   external 'itd_cancel@files:itdownload.dll stdcall';
53
54 procedure ITD_ClearFiles;
55   external 'itd_clearfiles@files:itdownload.dll stdcall';
56
57 function ITD_DownloadFile(url: PChar; destfilename: PChar): integer;
58   external 'itd_downloadfile@files:itdownload.dll stdcall';
59
60 function ITD_GetResultLen: integer;
61   external 'itd_getresultlen@files:itdownload.dll stdcall';
62
63 procedure ITD_GetResultString(buffer: pchar; maxlen: integer);
64   external 'itd_getresultstring@files:itdownload.dll stdcall';
65
66 procedure ITD_Internal_InitUI(HostHwnd: dword);
67   external 'itd_initui@files:itdownload.dll stdcall';
68
69 function ITD_Internal_LoadStrings(filename: PChar): boolean;
70   external 'itd_loadstrings@files:itdownload.dll stdcall';
71
72 procedure ITD_Internal_SetOption(option, value: PChar);
73   external 'itd_setoption@files:itdownload.dll stdcall';
74
75 function ITD_Internal_GetFileSize(url: pchar; var size: Cardinal): boolean;
76   external 'itd_getfilesize@files:itdownload.dll stdcall';
77
78 function ITD_Internal_GetString(index: integer): boolean;
79   external 'itd_getstring@files:itdownload.dll stdcall';
80
81 function ITD_Internal_GetOption(option: PChar; buffer: PChar; length: integer): integer;
82   external 'itd_getoption@files:itdownload.dll stdcall';
83
84 procedure ITD_Internal_SetString(index: integer; value: PChar);
85   external 'itd_setstring@files:itdownload.dll stdcall';
86
87 procedure ITD_Internal_AddFile(url: PChar; destfilename: PChar);
88   external 'itd_addfile@files:itdownload.dll stdcall';
89
90 procedure ITD_Internal_AddMirror(url: PChar; destfilename: PChar);
91   external 'itd_addmirror@files:itdownload.dll stdcall';
92
93 procedure ITD_Internal_AddFileSize(url: PChar; destfilename: PChar; size: integer);
94   external 'itd_addfilesize@files:itdownload.dll stdcall';
95
96 function ITD_Internal_DownloadFiles(surface: hwnd): integer;
97   external 'itd_downloadfiles@files:itdownload.dll stdcall';
98
99 function ITD_FileCount: integer;
100   external 'itd_filecount@files:itdownload.dll stdcall';
101
102 function ITD_Internal_PostPage(url, buffer: PChar; length: integer): boolean;
103   external 'itd_postpage@files:itdownload.dll stdcall';
104
105
106 const
107   ITDERR_SUCCESS = 0;
108   ITDERR_USERCANCEL = 1;
109   ITDERR_ERROR = 3;
110
111   {Constants for Language String indexes:}
112   ITDS_DownloadFailed = 104;
113
114   ITDS_TitleCaption = 200;
115   ITDS_TitleDescription = 201;
116
117   ITDS_MessageFailRetryContinue = 250;
118   ITDS_MessageFailRetry = 251;
119
120   ITDS_Retry = 502;
121
122   {Constants for language strings of updater example}
123   ITDS_Update_Caption = 600;
124   ITDS_Update_Description = 601;
125   ITDS_Update_Checking = 602;
126   ITDS_Update_NewAvailable = 603;
127   ITDS_Update_NoNewAvailable = 604;
128   ITDS_Update_WantToCheck = 605;
129   ITDS_Update_Failed = 606;
130   ITDS_Update_WillLaunch = 607;
131   ITDS_Update_WillLaunchWithPath = 608;
132
133   ITD_Event_DownloadPageEntered = 1;
134   ITD_Event_DownloadPageLeft = 2;
135   ITD_Event_DownloadFailed = 3;
136
137 var
138   itd_allowcontinue: boolean;
139   itd_retryonback: boolean;
140
141   ITD_AfterSuccess: procedure(downloadPage: TWizardPage);
142   ITD_EventHandler: procedure(event: integer);
143
144 procedure ITD_DownloadFiles();
145 begin
146   ITD_Internal_DownloadFiles(0);
147 end;
148
149 procedure ITD_AddFile(const URL, filename: string);
150 begin
151   ITD_Internal_AddFile(URL, filename);
152 end;
153
154 procedure ITD_AddMirror(const URL, filename: string);
155 begin
156   ITD_Internal_AddMirror(URL, filename);
157 end;
158
159 procedure ITD_AddFileSize(const URL, filename: string; size: integer);
160 begin
161   ITD_Internal_AddFileSize(URL, filename, size);
162 end;
163
164 function ITD_HandleSkipPage(sender: TWizardPage): boolean;
165 begin
166   result := (itd_filecount = 0);
167 end;
168
169 procedure ITD_SetString(index: integer; value: string);
170 begin
171   itd_internal_setstring(index, value);
172 end;
173
174 function ITD_GetFileSize(const url: string; var size: cardinal): boolean;
175 begin
176   result := itd_internal_getfilesize(PChar(url), size);
177 end;
178
179 function ITD_LoadStrings(const filename: string): boolean;
180 begin
181   result := itd_internal_loadstrings(filename);
182 end;
183
184 function ITD_GetString(index: integer): string;
185 begin
186   itd_internal_getstring(index);
187   setlength(result, ITD_GetResultLen);
188   ITD_GetResultString(PChar(result), length(result));
189 end;
190
191 procedure ITD_NowDoDownload(sender: TWizardPage);
192 var err: integer;
193 begin
194   wizardform.backbutton.enabled := false;
195   wizardform.nextbutton.enabled := false;
196
197   sender.caption := ITD_GetString(ITDS_TitleCaption);
198   sender.description := ITD_GetString(ITDS_TitleDescription);
199
200   err := ITD_Internal_DownloadFiles(sender.surface.handle);
201
202   case err of
203     ITDERR_SUCCESS: begin
204         wizardform.nextbutton.enabled := true;
205         wizardform.nextbutton.onclick(nil);
206
207         if itd_aftersuccess <> nil then
208           itd_aftersuccess(sender);
209       end;
210     ITDERR_USERCANCEL: ; //Don't show a message, this happens on setup close and cancel click
211   else begin
212     //Some unexpected error, like connection interrupted
213       wizardform.backbutton.caption := ITD_GetString(ITDS_Retry);
214       wizardform.backbutton.enabled := true;
215       wizardform.backbutton.show();
216       itd_retryonback := true;
217
218       wizardform.nextbutton.enabled := itd_allowcontinue;
219
220       if ITD_EventHandler <> nil then
221         ITD_EventHandler(ITD_Event_DownloadFailed);
222
223       if itd_allowcontinue then begin //Download failed, we can retry, continue, or exit
224         sender.caption := ITD_GetString(ITDS_DownloadFailed);
225         sender.description := ITD_GetString(ITDS_MessageFailRetryContinue);
226
227         MsgBox(ITD_GetString(ITDS_MessageFailRetryContinue), mbError, MB_OK)
228       end else begin //Download failed, we must retry or exit setup
229         sender.caption := ITD_GetString(ITDS_DownloadFailed);
230         sender.description := ITD_GetString(ITDS_MessageFailRetry);
231
232         MsgBox(ITD_GetString(ITDS_MessageFailRetry), mbError, MB_OK)
233       end;
234     end;
235   end;
236 end;
237
238 procedure ITD_HandleShowPage(sender: TWizardPage);
239 begin
240   wizardform.nextbutton.enabled := false;
241   wizardform.backbutton.hide();
242
243   if ITD_EventHandler <> nil then
244     ITD_EventHandler(ITD_Event_DownloadPageEntered);
245
246   itd_nowdodownload(sender);
247 end;
248
249 function ITD_HandleBackClick(sender: TWizardpage): boolean;
250 begin
251   result := false;
252   if itd_retryonback then begin
253     itd_retryonback := false;
254     wizardform.backbutton.hide();
255     itd_nowdodownload(sender);
256   end;
257 end;
258
259 function ITD_HandleNextClick(sender: TWizardpage): boolean;
260 begin
261   if ITD_EventHandler <> nil then
262     ITD_EventHandler(ITD_Event_DownloadPageLeft);
263
264   result := true;
265 end;
266
267 procedure ITD_Init;
268 begin
269  //Currently a NOP. Don't count on it in future.
270 end;
271
272 function ITD_PostPage(const url, data: string; out response: string): boolean;
273 begin
274   result := ITD_Internal_PostPage(PChar(url), PChar(data), length(data));
275
276   if result then begin
277     setlength(response, ITD_GetResultLen);
278     ITD_GetResultString(PChar(response), length(response));
279   end;
280 end;
281
282 function ITD_DownloadAfter(afterID: integer): TWizardPage;
283 var itd_downloadPage: TWizardPage;
284 begin
285   itd_downloadpage := CreateCustomPage(afterID, ITD_GetString(ITDS_TitleCaption), ITD_GetString(ITDS_TitleDescription));
286
287   itd_downloadpage.onactivate := @itd_handleshowpage;
288   itd_downloadpage.onshouldskippage := @itd_handleskippage;
289   itd_downloadpage.onbackbuttonclick := @itd_handlebackclick;
290   itd_downloadpage.onnextbuttonclick := @itd_handlenextclick;
291
292   itd_internal_initui(itd_downloadpage.surface.handle);
293
294   result := itd_downloadpage;
295 end;
296
297 procedure ITD_SetOption(const option, value: string);
298 begin
299   //The options which call ITD_SetString are depreciated, use ITD_SetString directly
300   if comparetext(option, 'UI_Caption') = 0 then
301     ITD_SetString(ITDS_TitleCaption, value)
302   else if comparetext(option, 'UI_Description') = 0 then
303     ITD_SetString(ITDS_TitleDescription, value)
304   else if comparetext(option, 'UI_FailMessage') = 0 then
305     ITD_SetString(ITDS_MessageFailRetry, value)
306   else if comparetext(option, 'UI_FailOrContinueMessage') = 0 then
307     ITD_SetString(ITDS_MessageFailRetryContinue, value)
308   else if comparetext(option, 'UI_AllowContinue') = 0 then
309     ITD_AllowContinue := (value = '1')
310   else
311     ITD_Internal_SetOption(option, value);
312 end;
313
314 function ITD_GetOption(const option: string): string;
315 begin
316   setlength(result, 500);
317   setlength(result, itd_internal_getoption(pchar(option), pchar(result), length(result)));
318 end;