-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
303 lines (280 loc) · 8.37 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// ============================================================================
//
// This file contains the actions, conditions and expressions your object uses
//
// ============================================================================
#include "common.h"
string EventLastError;
void ChangeLastError(string NewError,LPRDATA rdPtr)
{
if (EventLastError.length()>0)
EventLastError.append(" and "); //Join error strings
else
rdPtr->rRd->PushEvent(4); //Push event if not already called
EventLastError.append(NewError);
}
// ============================================================================
//
// CONDITIONS
//
// ============================================================================
CONDITION(
/* ID */ 0,
/* Name */ "%o: Download in progress in slot %0",
/* Flags */ EVFLAGS_ALWAYS+EVFLAGS_NOTABLE,
/* Params */ (1,PARAM_NUMBER,"Enter the slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS) {
return rdPtr->slots[n].bPause|| //If Paused, No
rdPtr->slots[n].bCompleted; //If Completed, No
//If paused or completed, download is not in progress.
}
else
return 0;
}
CONDITION(
/* ID */ 1,
/* Name */ "%o: Download successfully completed in slot %0",
/* Flags */ EVFLAGS_ALWAYS+EVFLAGS_NOTABLE,
/* Params */ (1,PARAM_NUMBER,"Enter the slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS)
return rdPtr->slots[n].bCompleted;
else
{
ChangeLastError("Invalid port number for \"Download Complete\" condition",rdPtr);
return 0;
}
}
CONDITION(
/* ID */ 2,
/* Name */ "%o: Connection error in slot %0",
/* Flags */ EVFLAGS_ALWAYS+EVFLAGS_NOTABLE,
/* Params */ (1,PARAM_NUMBER,"Enter the slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS)
return rdPtr->slots[n].bError; //If error
else
return 0;
}
CONDITION(
/* ID */ 3,
/* Name */ "%o: Download aborted in slot %0",
/* Flags */ EVFLAGS_ALWAYS+EVFLAGS_NOTABLE,
/* Params */ (1,PARAM_NUMBER,"Enter the slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS)
return rdPtr->slots[n].bAborted; //If aborted...
else
return 0;
}
CONDITION(
/* ID */ 4,
/* Name */ "%o: On error",
/* Flags */ 0,
/* Params */ (0)
) {
return true;
}
// ============================================================================
//
// ACTIONS
//
// ============================================================================
#include <MMSystem.h>
ACTION(
/* ID */ 0,
/* Name */ "Download file, URL=%0, destination=%1, slot %2",
/* Flags */ 0,
/* Params */ (4,PARAM_STRING, "Enter the URL of the file. It must begin with HTTP:// or HTTPS://",
PARAM_FILENAME, "Destination filename:",
PARAM_NUMBER, "Slot number (1-32):",
PARAM_NUMBER, "Download initial position (0 for new download, -1 to automatically find out, else number of bytes to start from)")
) {
string URL;
URL=(char *)Param(PARAM_STRING);
HANDLE DownloadTo=fopen((char *)Param(PARAM_STRING),"ab"); //a=append, b=binary
int n=Param(PARAM_NUMBER)-1;
if (n>=0&&n<MAX_SLOTS)
{
char Buffer[64];
DWORD dwTaille=64;
DWORD dwHeader=0;
if (rdPtr->lpHttpQueryInfo(rdPtr->slots[n].hOpenedURL, HTTP_QUERY_CONTENT_LENGTH, Buffer, &dwTaille, &dwHeader)>0) //Check size of file
{
rdPtr->slots[n].FileSize=atoi(Buffer);
rdPtr->slots[n].InitialTime=timeGetTime(); //timeGetSystemTime()?
}
else
ChangeLastError("Bad URL, size <1",rdPtr);
}
else
ChangeLastError("Bad slot!",rdPtr);
}
ACTION(
/* ID */ 1,
/* Name */ "Abort download slot %0",
/* Flags */ 0,
/* Params */ (1,PARAM_NUMBER, "Slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1;
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].bAborted==true)
ChangeLastError("Already aborted.",rdPtr);
else {
if (rdPtr->slots[n].pURL.length()>0)
rdPtr->slots[n].bAborted=true;} }
else
ChangeLastError("Bad slot",rdPtr);
}
ACTION(
/* ID */ 2,
/* Name */ "Pause download slot %0",
/* Flags */ 0,
/* Params */ (1,PARAM_NUMBER, "Slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1;
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].bPause==true)
ChangeLastError("Already paused.",rdPtr);
else {
if (rdPtr->slots[n].bAborted==true)
ChangeLastError("Download cannot be paused, is currently aborted.",rdPtr);
else
rdPtr->slots[n].bPause=true;} }
else
ChangeLastError("Bad slot",rdPtr);
}
ACTION(
/* ID */ 3,
/* Name */ "Resume download slot %0",
/* Flags */ 0,
/* Params */ (1,PARAM_NUMBER, "Slot number (1-32):")
) {
int n=Param(PARAM_NUMBER)-1;
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].bPause==false)
ChangeLastError("Not paused.",rdPtr);
else {
if (rdPtr->slots[n].bAborted==true)
ChangeLastError("Download cannot be resumed, is currently aborted.",rdPtr);
else
rdPtr->slots[n].bPause=false;} }
else
ChangeLastError("Bad slot",rdPtr);
}
// ============================================================================
//
// EXPRESSIONS
//
// ============================================================================
EXPRESSION(
/* ID */ 0,
/* Name */ "InstantFileSize(",
/* Flags */ 0,
/* Params */ (1,EXPPARAM_STRING,"Enter file URL")
) {
return 0;
}
EXPRESSION(
/* ID */ 1,
/* Name */ "CurrentURL$(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (1,EXPPARAM_LONG,"Enter the slot number (1-32)")
) {
int n=Param(EXPPARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].pURL.length()>0) {
char temp[255];
sprintf_s(temp,rdPtr->slots[n].pURL.length(),"%s",rdPtr->slots[n].pURL);
ReturnString(*temp); }//If in range and there is a URL
else {
ChangeLastError("Unknown URL",rdPtr); //If in range but no URL
ReturnString(""); } }
else {
ChangeLastError("Invalid slot",rdPtr); //If out of range
ReturnString(""); }
}
EXPRESSION(
/* ID */ 3,
/* Name */ "CurrentPos(",
/* Flags */ 0,
/* Params */ (1,EXPPARAM_NUMBER,"Enter the slot number (1-32)")
) {
int n=Param(EXPPARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].TotalRead>0)
return rdPtr->slots[n].TotalRead; //If in range and size saved
else {
ChangeLastError("Download failed or not initiated", rdPtr); //If in range but no size
ReturnString(""); } }
else {
ChangeLastError("Invalid slot",rdPtr); //If out of range
ReturnString(""); }
}
EXPRESSION(
/* ID */ 4,
/* Name */ "CurrentPercent(",
/* Flags */ 0,
/* Params */ (1,EXPPARAM_NUMBER,"Enter the slot number (1-32)")
) {
int n=Param(EXPPARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].TotalRead>0)
{ReturnFloat(((rdPtr->slots[n].FileSize*1.0)/(rdPtr->slots[n].TotalRead*1.0))*100.0);}//If in range and size saved
else {
ChangeLastError("Download failed or not initiated",rdPtr); //If in range but no size
ReturnString(""); } }
else {
ChangeLastError("Invalid slot",rdPtr); //If out of range
ReturnString(""); }
}
EXPRESSION(
/* ID */ 5,
/* Name */ "CurrentSpeed(",
/* Flags */ 0,
/* Params */ (1,EXPPARAM_NUMBER,"Enter the slot number (1-32)")
) {
int n=Param(EXPPARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].TotalRead>0)
return rdPtr->slots[n].Speed; //If in range and size saved
else {
ChangeLastError("Download failed, not initiated, or paused", rdPtr); //If in range but no size
ReturnString(""); } }
else {
ChangeLastError("Invalid slot",rdPtr); //If out of range
ReturnString(""); }
}
EXPRESSION(
/* ID */ 6,
/* Name */ "TotalTime(",
/* Flags */ 0,
/* Params */ (1,EXPPARAM_NUMBER,"Enter the slot number (1-32)")
) {
int n=Param(EXPPARAM_NUMBER)-1; //n-1 because 0-based.
if (n>=0&&n<MAX_SLOTS) {
if (rdPtr->slots[n].TotalRead>0)
{ReturnFloat(((rdPtr->slots[n].TotalRead-rdPtr->slots[n].FileSize)*1.0)/(rdPtr->slots[n].Speed*1.0));} //If in range and size saved
else {
ChangeLastError("Download failed, not initiated, or paused", rdPtr); //If in range but no size
ReturnString(""); } }
else {
ChangeLastError("Invalid slot",rdPtr); //If out of range
ReturnString(""); }
}
EXPRESSION(
/* ID */ 7,
/* Name */ "LastError$(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (0)
) {
char temp[255];
sprintf_s(temp,EventLastError.length(),"%s.",EventLastError);
ReturnString(*temp);
EventLastError=""; //Resets error string
}