/
WebCDCovers.cpp
executable file
·528 lines (424 loc) · 14.4 KB
/
WebCDCovers.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// WebCDCovers.cpp: implementation of the CWebCDCovers class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CdCoverCreator2.h"
#include "WebCDCovers.h"
#include "HttpReqSocket.h"
#include "HttpRequest.h"
#include "wcc_data.h"
#include "..\3rdParty\ZipArchive\ZipArchive.h"
#include <list>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern DWORD gl_dwNextDownloadId;
extern std::list<DWORD> gl_stkDownloads;
// thread functions
UINT wccGetList(LPVOID pvParams)
{
WCC_GETLISTPARAMS* pParams = (WCC_GETLISTPARAMS*) pvParams;
CWebCDCovers wcc;
wcc.SetDownloadCancelFlag (pParams->pCancelDownload);
wcc.m_dwDownloadId = ++gl_dwNextDownloadId;
bool bResult = wcc.GetList (pParams->strValue,
pParams->hwndList, pParams->htiParent,
pParams->hwndProgress, pParams->hwndStatus);
if (pParams->hwndStatus != NULL)
for (int i = 1; i <= 4; i++)
::PostMessage (pParams->hwndStatus, SB_SETTEXT, i, (LPARAM) "");
::SendMessage (pParams->hwndNotify, WCCM_LISTCOVERS, 0, 0);
delete pParams;
return bResult ? 0 : 1;
}
UINT wccSearchCovers(LPVOID pvParams)
{
WCC_GETLISTPARAMS* pParams = (WCC_GETLISTPARAMS*) pvParams;
CWebCDCovers wcc;
wcc.SetDownloadCancelFlag (pParams->pCancelDownload);
wcc.m_dwDownloadId = ++gl_dwNextDownloadId;
bool bResult = wcc.SearchCovers (pParams->strValue,
pParams->hwndList, pParams->hwndProgress, pParams->hwndStatus);
if (pParams->hwndStatus != NULL)
{
for (int i = 1; i <= 4; i++)
::PostMessage (pParams->hwndStatus, SB_SETTEXT, i, (LPARAM) "");
if (!bResult)
{
CString str;
str.LoadString (IDS_QUERYEMPTY);
::PostMessage (pParams->hwndStatus, SB_SETTEXT, 1, (LPARAM) str.GetBuffer (0));
::MessageBeep (0);
}
}
::SendMessage (pParams->hwndNotify, WCCM_SEARCHCOVERS, 0, 0);
delete pParams;
return bResult ? 0 : 1;
}
UINT wccGetImages(LPVOID pvParams)
{
WCC_GETIMAGESPARAMS* pParams = (WCC_GETIMAGESPARAMS*) pvParams;
CWebCDCovers wcc;
wcc.SetDownloadCancelFlag (pParams->pCancelDownload);
wcc.m_dwDownloadId = ++gl_dwNextDownloadId;
wcc.DownloadImage (pParams->strLetter, pParams->strName,
pParams->pCoverImages->strFile[pParams->rt], pParams->pCoverImages->pHost,
pParams->hwndProgress, pParams->hwndStatus,
pParams->pCoverImages->strLocalFile[pParams->rt]);
::SendMessage (pParams->hwndNotify, WCCM_GETIMAGES, 0, 0);
delete pParams;
return 0;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWebCDCovers::CWebCDCovers()
{
HOSTMAP* pMap = &((CCdCoverCreator2App*) AfxGetApp ())->m_mapCoverHosts;
RESOURCEHOST* pHost;
for (HOSTMAP::iterator it = pMap->begin (); it != pMap->end (); it++)
{
pHost = (*it).second;
pHost->hInst = LoadLibrary (pHost->strLibFilename);
}
m_nError = 0;
//if (!CHttpReqSocket::IsInternetConnected ())
//{
// AfxMessageBox (IDS_INTERNETERROR_IMAGES, MB_ICONEXCLAMATION);
// m_nError = -1;
// return;
//}
// do the authorization
}
CWebCDCovers::~CWebCDCovers()
{
HOSTMAP* pMap = &((CCdCoverCreator2App*) AfxGetApp ())->m_mapCoverHosts;
for (HOSTMAP::iterator it = pMap->begin (); it != pMap->end (); it++)
FreeLibrary ((*it).second->hInst);
}
void CWebCDCovers::SetDownloadCancelFlag(bool *pbCancelDownload)
{
m_pbCancelDownload = pbCancelDownload;
}
bool CWebCDCovers::GetList(CString strLetter, HWND hwndList, HTREEITEM htiParent, HWND hwndProgress, HWND hwndStatus)
{
bool bSuccess = false;
//if (!CHttpReqSocket::IsInternetConnected ())
//{
// AfxMessageBox (IDS_INTERNETERROR_IMAGES, MB_ICONEXCLAMATION);
// m_nError = -1;
// return false;
//}
CString strMsg;
strMsg.LoadString (IDS_DOWNLOADING_LIST);
if (m_dwDownloadId == gl_stkDownloads.back ())
{
::SendMessage (hwndProgress, PBM_SETPOS, 0, 0);
::ShowWindow (hwndProgress, SW_SHOW);
}
/*
for (int i = 0; i < pList->GetItemCount (); i++)
delete (COVERIMAGES*) pList->GetItemData (i);
pList->DeleteAllItems ();
*/
// do the downloads
bSuccess = RetrieveList (WCC_LIST, strLetter, hwndList, htiParent, hwndProgress, hwndStatus, strMsg);
if (m_dwDownloadId == gl_stkDownloads.back ())
{
for (int i = 1; i <= 4; i++)
::SendMessage (hwndStatus, SB_SETTEXT, i, (LPARAM) "");
::ShowWindow (hwndProgress, SW_HIDE);
}
return bSuccess;
}
bool CWebCDCovers::SearchCovers(CString strKeywords, HWND hwndList, HWND hwndProgress, HWND hwndStatus)
{
bool bSuccess = false;
//if (!CHttpReqSocket::IsInternetConnected ())
//{
// AfxMessageBox (IDS_INTERNETERROR_IMAGES, MB_ICONEXCLAMATION);
// m_nError = -1;
// return false;
//}
CString strMsg;
strMsg.Format (IDS_SEARCHING, (LPSTR) (LPCTSTR) strKeywords);
if (m_dwDownloadId == gl_stkDownloads.back ())
{
::SendMessage (hwndProgress, PBM_SETPOS, 0, 0);
::ShowWindow (hwndProgress, SW_SHOW);
}
LVITEM lvItem;
for (int i = 0; i < ListView_GetItemCount (hwndList); i++)
{
ListView_GetItem (hwndList, &lvItem);
//delete (COVERIMAGES*) lvItem.lParam;
}
ListView_DeleteAllItems (hwndList);
// do the downloads
bSuccess = RetrieveList (WCC_SEARCH, CHttpReqSocket::URLEncode (strKeywords),
hwndList, NULL, hwndProgress, hwndStatus, strMsg);
if (m_dwDownloadId == gl_stkDownloads.back ())
{
for (int i = 1; i <= 4; i++)
::SendMessage (hwndStatus, SB_SETTEXT, i, (LPARAM) "");
::ShowWindow (hwndProgress, SW_HIDE);
}
return bSuccess;
}
///////////////////////////////////////////////////////////////////
// internal helpers
CString CWebCDCovers::GetCookies(CString strResponse)
{
CString strCookie;
strcpy (strCookie.GetBuffer (strResponse.GetLength () + 1), strResponse.GetBuffer (0));
strCookie.ReleaseBuffer ();
strCookie.MakeLower ();
int nPos = strCookie.Find ("set-cookie: ");
if (nPos >= 0)
{
int nPos2 = strCookie.Find (';', nPos);
strCookie = strCookie.Mid (nPos + 12, nPos2 - nPos - 12);
}
return strCookie;
}
bool CWebCDCovers::DownloadImage(CString strLetter, CString strName, CString strFilename, RESOURCEHOST* pHost, HWND hwndProgress, HWND hwndStatus, CString& strLocalFilename)
{
// get the html page for the image source
*m_pbCancelDownload = false;
WCC_GETIMAGEREQUEST* pfn_wccRequest = (WCC_GETIMAGEREQUEST*) GetProcAddress (pHost->hInst, "GetImageRequest");
WCC_GETIMAGEURL* pfn_wccImgURL = (WCC_GETIMAGEURL*) GetProcAddress (pHost->hInst, "GetImageURL");
char szUrl[300], szHeaders[300], szData[300];
int nMethod;
pfn_wccRequest (strFilename.GetBuffer (0), strLetter.GetBuffer (0), "", szUrl, &nMethod, szHeaders, szData);
CString strHTML = (*szUrl == 0) ? strFilename.GetBuffer (0) :
CHttpRequest::DownloadBuffer (szUrl, nMethod, szHeaders, szData,
m_pbCancelDownload, m_dwDownloadId, m_nError);
// MCH 29/08/04
// Send version info to the DLL
strcpy (szData, ((CCdCoverCreator2App*) AfxGetApp ())->GetVersion ().GetBuffer (-1));
// get the URL of the image
CString strURL;
if (!pfn_wccImgURL (strHTML.GetBuffer (0), strURL.GetBuffer (500), &nMethod, szHeaders, szData))
return false;
strURL.ReleaseBuffer ();
// generate a local temporary file name
strLocalFilename = ((CCdCoverCreator2App*) AfxGetApp ())->GetImageCacheDir () + strURL.Mid (strURL.ReverseFind ('/') + 1);
if (strLocalFilename.Find (".php?") >= 0)
{
// MCH 15/09/04
// www.darktown.to sends again JPGs, but via a PHP script. Filename provided as HTTP header which is not available here
strLocalFilename = ((CCdCoverCreator2App*) AfxGetApp ())->GetImageCacheDir ();
strLocalFilename.AppendFormat ("dld%d.jpg", GetTickCount ());
}
// get the jpeg image
*m_pbCancelDownload = false;
CHttpRequest::DownloadFile (strURL, nMethod, szHeaders, szData, strLocalFilename,
m_pbCancelDownload, m_dwDownloadId, m_nError, 0, hwndProgress, hwndStatus);
if (m_nError)
return false;
// MCH 29/08/04
// if the download is a zip file, extract the image
if (strLocalFilename.Right (4).MakeLower () == ".zip")
{
CString strPath = strLocalFilename.Left (strLocalFilename.ReverseFind ('\\'));
CString strZipFile = strLocalFilename;
// set up and open a zip archive, get the image files inside the zip file
CZipArchive zip;
zip.Open (strZipFile);
CZipWordArray ar;
zip.FindMatches ("*.jp*g", ar);
strLocalFilename.Empty ();
// extract the image files
for (int i = 0; i < ar.GetSize (); i++)
{
// set the local file name
CZipFileHeader info;
zip.GetFileInfo (info, i);
if (!strLocalFilename.IsEmpty ())
strLocalFilename += ";";
strLocalFilename += strPath + "\\" + info.GetFileName ();
// extract the file
zip.ExtractFile (ar.GetAt (i), strPath, false);
}
zip.Close ();
// delete the zip file
::DeleteFile (strZipFile);
}
return true;
}
bool CWebCDCovers::RetrieveList(int nIndex, CString strCriteria, HWND hwndList, HTREEITEM htiParent, HWND hwndProgress, HWND hwndStatus, CString strMsg)
{
bool bSuccess = false;
COVERIMAGES* pCoverImages;
HOSTMAP* pMap = &((CCdCoverCreator2App*) AfxGetApp ())->m_mapCoverHosts;
RESOURCEHOST* pHost;
char* lpszData = NULL;
CString strHTML, strName;
char szUrl[300], szHeaders[300], szData[300];
int nMethod, nPages;
bool bPagesSet, bExpanded = false;
::PostMessage (hwndProgress, PBM_SETPOS, 0, 0);
::ShowWindow (hwndProgress, SW_SHOW);
for (HOSTMAP::iterator it = pMap->begin (); it != pMap->end (); it++)
{
pHost = (*it).second;
if (!pHost->bEnabled)
continue;
WCC_GETREQUEST* pfn_wccRequest = (WCC_GETREQUEST*) GetProcAddress (pHost->hInst, nIndex == WCC_LIST ? "GetListCoversRequest" : "GetSearchCoversRequest");
WCC_GETLIST* pfn_wccList = (WCC_GETLIST*) GetProcAddress (pHost->hInst, nIndex == WCC_LIST ? "ListCovers" : "SearchCovers");
bSuccess = true;
bPagesSet = false;
pfn_wccRequest (strCriteria.GetBuffer (0), "", szUrl, &nMethod, szHeaders, szData);
while (*szUrl)
{
// Send the request
AfxGetApp ()->BeginWaitCursor ();
strHTML = CHttpRequest::DownloadBuffer (szUrl, nMethod, szHeaders, szData,
m_pbCancelDownload, m_dwDownloadId, m_nError, 0, NULL, hwndStatus, strMsg);
AfxGetApp ()->EndWaitCursor ();
lpszData = new char[strHTML.GetLength () + 1];
// Parse the list
if (!pfn_wccList (strHTML.GetBuffer (0), strCriteria.GetBuffer (0), lpszData, szUrl, &nMethod, szHeaders, szData, &nPages))
{
delete lpszData;
lpszData = NULL;
continue;
}
if (nPages >= 1)
{
// has multiple pages
if (!bPagesSet)
{
::SendMessage (hwndProgress, PBM_SETRANGE32, 1, nPages);
::SendMessage (hwndProgress, PBM_SETSTEP, 1, 0);
::SendMessage (hwndProgress, PBM_SETPOS, 0, 0);
::ShowWindow (hwndProgress, SW_SHOW);
bPagesSet = true;
}
else
::SendMessage (hwndProgress, PBM_STEPIT, 0, 0);
}
else
{
// only one page...
::ShowWindow (hwndProgress, SW_HIDE);
}
// fill the List
for (int nPos = 0; ; )
{
pCoverImages = new COVERIMAGES;
pCoverImages->pHost = pHost;
if (!GetData (lpszData, nPos, strName, pCoverImages))
{
delete pCoverImages;
break;
}
if (htiParent == NULL)
{
// this is a list control
LVITEM item;
item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
item.iItem = ListView_GetItemCount (hwndList);
item.iSubItem = 0;
item.pszText = (LPSTR) (LPCTSTR) strName;
item.iImage = 1;
item.lParam = (DWORD) pCoverImages;
ListView_InsertItem (hwndList, &item);
}
else
{
bool bChanged = true;
// look for item already in the tree
TVITEM item;
char szText[500];
for (HTREEITEM hItem = TreeView_GetChild (hwndList, htiParent); hItem != NULL; hItem = TreeView_GetNextSibling (hwndList, hItem))
{
item.mask = TVIF_TEXT | TVIF_PARAM;
item.hItem = hItem;
item.pszText = szText;
item.cchTextMax = 500;
TreeView_GetItem (hwndList, &item);
// item has been found
if (strName == szText)
{
if (!CoverImagesAreEqual (pCoverImages, (COVERIMAGES*) item.lParam))
{
// remove old item and replace it by new one if it has changed
delete (COVERIMAGES*) item.lParam;
TreeView_DeleteItem (hwndList, hItem);
}
else
bChanged = false;
break;
}
}
// insert the item if it has changed (or is new)
if (bChanged)
{
TVINSERTSTRUCT insert;
insert.hParent = htiParent;
insert.hInsertAfter = TVI_SORT;
insert.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
insert.item.pszText = (LPSTR) (LPCTSTR) strName;
insert.item.iImage = 1;
insert.item.iSelectedImage = 1;
insert.item.lParam = (DWORD) pCoverImages;
TreeView_InsertItem (hwndList, &insert);
}
}
}
// expand the branch
if (!bExpanded)
{
TreeView_Expand (hwndList, htiParent, TVE_EXPAND);
bExpanded = true;
}
// clean up
if (lpszData != NULL)
{
delete lpszData;
lpszData = NULL;
}
}
}
::ShowWindow (hwndProgress, SW_HIDE);
return bSuccess;
}
bool CWebCDCovers::GetData(LPSTR lpszData, int& nPos, CString &strName, COVERIMAGES* pCoverImages)
{
if (lpszData[nPos] == 0)
return false;
CopyData (lpszData, nPos, strName);
CopyData (lpszData, nPos, pCoverImages->strFile[Cover]);
CopyData (lpszData, nPos, pCoverImages->strFile[Back]);
CopyData (lpszData, nPos, pCoverImages->strFile[Inlay]);
CopyData (lpszData, nPos, pCoverImages->strFile[Label]);
return true;
}
void CWebCDCovers::CopyData(LPSTR lpszData, int &nPos, CString &strRet)
{
int nOldPos = nPos;
for ( ; lpszData[nPos] != 1; nPos++)
;
int n = nPos - nOldPos;
LPSTR lpszBuf = strRet.GetBuffer (n + 1);
strncpy (lpszBuf, lpszData + nOldPos, n);
lpszBuf[n] = 0;
strRet.ReleaseBuffer ();
strRet.Replace (" ", " ");
strRet.Replace ("&", "&");
strRet.Replace ("<", "<");
strRet.Replace (">", ">");
nPos++;
}
bool CWebCDCovers::CoverImagesAreEqual(COVERIMAGES *pCi1, COVERIMAGES *pCi2)
{
if (pCi1->pHost != pCi2->pHost)
return false;
for (int i = 0; i < 4; i++)
if (pCi1->strFile[i] != pCi2->strFile[i])
return false;
return true;
}