CDDB.Cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. #include "main.h"
  2. #include "api__in_cdda.h"
  3. #include "cddbinterface.h"
  4. #include "cddb.h"
  5. #include "../nu/AutoWide.h"
  6. #include "../nu/AutoChar.h"
  7. #include "../winamp/wa_ipc.h"
  8. #include "../nu/ns_wc.h"
  9. #include ".\cddbevnt.h"
  10. #include ".\cddbui.h"
  11. #include ".\grabwnd.h"
  12. #include <api/application/api_application.h>
  13. #include <atlbase.h>
  14. #include "../nde/ndestring.h"
  15. #include "../Winamp/buildtype.h"
  16. #include <commctrl.h>
  17. #include <strsafe.h>
  18. // {C0A565DC-0CFE-405a-A27C-468B0C8A3A5C}
  19. static const GUID internetConfigGroupGUID =
  20. { 0xc0a565dc, 0xcfe, 0x405a, { 0xa2, 0x7c, 0x46, 0x8b, 0xc, 0x8a, 0x3a, 0x5c } };
  21. TRACKINFO::~TRACKINFO()
  22. {
  23. Reset();
  24. }
  25. void TRACKINFO::Reset()
  26. {
  27. ndestring_release(artist);
  28. artist=0;
  29. ndestring_release(title);
  30. title=0;
  31. ndestring_release(tagID);
  32. tagID=0;
  33. ndestring_release(composer);
  34. composer=0;
  35. ndestring_release(conductor);
  36. conductor=0;
  37. ndestring_release(extData);
  38. extData=0;
  39. ndestring_release(remixing);
  40. remixing=0;
  41. ndestring_release(isrc);
  42. isrc=0;
  43. }
  44. TRACKINFO::TRACKINFO(const TRACKINFO &copy)
  45. {
  46. operator =(copy);
  47. }
  48. TRACKINFO &TRACKINFO::operator =(const TRACKINFO &copy)
  49. {
  50. ndestring_retain(artist=copy.artist);
  51. ndestring_retain(title=copy.title);
  52. ndestring_retain(tagID=copy.tagID);
  53. ndestring_retain(composer=copy.composer);
  54. ndestring_retain(conductor=copy.conductor);
  55. ndestring_retain(extData=copy.extData);
  56. ndestring_retain(remixing=copy.remixing);
  57. ndestring_retain(isrc=copy.isrc);
  58. return *this;
  59. }
  60. void DINFO::Reset()
  61. {
  62. ndestring_release(title);
  63. title=0;
  64. ndestring_release(artist);
  65. artist=0;
  66. ndestring_release(tuid);
  67. tuid=0;
  68. ndestring_release(year);
  69. year=0;
  70. ndestring_release(genre);
  71. genre=0;
  72. ndestring_release(label);
  73. label=0;
  74. ndestring_release(notes);
  75. notes=0;
  76. ndestring_release(conductor);
  77. conductor=0;
  78. ndestring_release(composer);
  79. composer=0;
  80. ndestring_release(remixing);
  81. remixing=0;
  82. discnum=0;
  83. numdiscs=0;
  84. }
  85. DINFO::~DINFO()
  86. {
  87. Reset();
  88. }
  89. DINFO::DINFO(const DINFO &copy)
  90. {
  91. operator =(copy);
  92. }
  93. DINFO &DINFO::operator =(const DINFO &copy)
  94. {
  95. ndestring_retain(title=copy.title);
  96. ndestring_retain(artist=copy.artist);
  97. ndestring_retain(tuid=copy.tuid);
  98. ndestring_retain(year=copy.year);
  99. ndestring_retain(genre=copy.genre);
  100. ndestring_retain(label=copy.label);
  101. ndestring_retain(notes=copy.notes);
  102. compilation=copy.compilation;
  103. discnum=copy.discnum;
  104. numdiscs=copy.numdiscs;
  105. ntracks=copy.ntracks;
  106. ndestring_retain(conductor=copy.conductor);
  107. ndestring_retain(composer=copy.composer);
  108. ndestring_retain(remixing=copy.remixing);
  109. for (int i=0;i<sizeof(tracks)/sizeof(*tracks);i++)
  110. tracks[i]=copy.tracks[i];
  111. CDDBID=copy.CDDBID;
  112. memcpy(pnFrames, copy.pnFrames, sizeof(pnFrames));
  113. nDiscLength=copy.nDiscLength;
  114. populated=copy.populated;
  115. return *this;
  116. }
  117. #define TM_INVOKERESULTS (WM_APP + 222)
  118. #define TM_UNINITTHREAD (WM_APP + 2)
  119. #define CENTER_PARENT (-32000)
  120. #define CENTER_LEFT 0x0001
  121. #define CENTER_TOP 0x0002
  122. #define WASABI_WND_CLASSW L"BaseWindow_RootWnd"
  123. #define INVARIANT_LCID MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
  124. typedef struct _MEDIALOOKUP
  125. {
  126. HRESULT result;
  127. HWND hwndInfo;
  128. CDDB_CB callback;
  129. ULONG_PTR user;
  130. UINT flags;
  131. BSTR bstrTOC;
  132. DWORD threadId;
  133. } MEDIALOOKUP;
  134. typedef struct _THREADDATA
  135. {
  136. LONG ref;
  137. HHOOK hhook;
  138. BOOL uninitCom;
  139. } THREADDATA;
  140. CRITICAL_SECTION lockThread;
  141. typedef struct _INVOKEDATA
  142. {
  143. MEDIALOOKUP lookup;
  144. ICddbDisc *pDisc;
  145. HANDLE evntDone;
  146. DWORD *pdwAutoCloseDelay;
  147. } INVOKEDATA;
  148. static DWORD tlsSlot = TLS_OUT_OF_INDEXES;
  149. #ifndef IGNORE_API_GRACENOTE
  150. ICDDBControl *pCDDBControl=0;
  151. static CDBBEventManager eventMngr;
  152. #endif
  153. static MEDIALOOKUP g_lookup = {0, 0, };
  154. static HANDLE evntBusy = NULL;
  155. static LONG evntBusyRef = 0;
  156. static HWND hwndProgressListener = NULL;
  157. static ICddbDisc *pSubmitDisc = NULL;
  158. static POINT g_lastcddbpos = { CENTER_PARENT, CENTER_PARENT}; // center
  159. static HRESULT SetupCDDB(BOOL resetError);
  160. static void CALLBACK Cddb_OnCommandCompleted(LONG lCommandCode, HRESULT hCommandResult, VARIANT *pCommandData, UINT_PTR user);
  161. static void CALLBACK Cddb_OnCommandProgress(LONG lCommandCode, LONG lProgressCode, LONG lBytesDone, LONG lBytesTotal, UINT_PTR user);
  162. #ifndef IGNORE_API_GRACENOTE
  163. static void Cddb_OnMediaLookupCompleted(HRESULT hr, CDDBMatchCode matchCode, MEDIALOOKUP *pLookup);
  164. static void Cddb_OnGetFullDiscInfoCompleted(HRESULT hr, ICddbDisc *pDisc, MEDIALOOKUP *pLookup);
  165. static void Cddb_OnSubmitDiscCompleted(HRESULT result, MEDIALOOKUP *pLookup);
  166. static HRESULT Cddb_DoFuzzyMatchDlg(HWND hwndCaller, UINT flags, ICddbDiscs *pDiscs, LONG *plVal);
  167. static HRESULT Cddb_DoSubmitNewDlg(HWND hwndCaller, LPCWSTR pszTOC, UINT flags, ICddbDisc **ppDisc);
  168. #define CLEARLOOKUP() SecureZeroMemory(&g_lookup, sizeof(MEDIALOOKUP))
  169. #define IS_BUSY(__timeout) (evntBusy && (WAIT_OBJECT_0 == WaitForSingleObject(evntBusy, (__timeout))))
  170. #define SET_BUSY(__enable) ((__enable) ? (SetEvent(evntBusy)) : ResetEvent(evntBusy))
  171. void ShutDownCDDB()
  172. {
  173. if (pCDDBControl)
  174. {
  175. eventMngr.Unadvise(pCDDBControl);
  176. pCDDBControl->Release();
  177. pCDDBControl=0;
  178. }
  179. }
  180. #endif
  181. static HWND GetAdaptedParent(HWND hwndParent)
  182. {
  183. if (!hwndParent || !IsWindow(hwndParent)) hwndParent = line.hMainWindow;
  184. if (!hwndParent || !IsWindow(hwndParent)) hwndParent = GetDesktopWindow();
  185. if (hwndParent == line.hMainWindow)
  186. {
  187. hwndParent = (HWND)SendMessageW(line.hMainWindow, WM_WA_IPC, 0, IPC_GETDIALOGBOXPARENT);
  188. if (!IsWindow(hwndParent)) hwndParent = line.hMainWindow;
  189. }
  190. return hwndParent;
  191. }
  192. static BOOL CalculatePopUpPos(RECT *prcParent, RECT *prcPopUp, UINT flags)
  193. {
  194. LONG x, y;
  195. MONITORINFO mi;
  196. if (!prcPopUp) return FALSE;
  197. if (prcParent)
  198. {
  199. x = prcParent->left;
  200. y = prcParent->top;
  201. OffsetRect(prcPopUp, -prcPopUp->left, -prcPopUp->top);
  202. OffsetRect(prcParent, -prcParent->left, -prcParent->top);
  203. if (CENTER_LEFT & flags) x += (prcParent->right - prcPopUp->right)/2;
  204. if (CENTER_TOP & flags) y += (prcParent->bottom - prcPopUp->bottom )/2;
  205. SetRect(prcPopUp, x, y, x + prcPopUp->right, y + prcPopUp->bottom);
  206. }
  207. mi.cbSize = sizeof(MONITORINFO);
  208. HMONITOR hMonitor = MonitorFromRect(prcPopUp, MONITOR_DEFAULTTONULL);
  209. if (!hMonitor)
  210. {
  211. OffsetRect(prcPopUp, -prcPopUp->left, -prcPopUp->top);
  212. hMonitor = MonitorFromRect(prcPopUp, MONITOR_DEFAULTTONEAREST);
  213. if(GetMonitorInfo(hMonitor, &mi))
  214. {
  215. x = mi.rcWork.left + ((mi.rcWork.right - mi.rcWork.left) - prcPopUp->right)/2;
  216. if(x < mi.rcWork.left) x = mi.rcWork.left;
  217. y = mi.rcWork.top + ((mi.rcWork.bottom - mi.rcWork.top) - prcPopUp->bottom)/2;
  218. if(y < mi.rcWork.top) y = mi.rcWork.top;
  219. SetRect(prcPopUp, x, y, x + prcPopUp->right, y + prcPopUp->bottom);
  220. }
  221. }
  222. else
  223. {
  224. if(GetMonitorInfo(hMonitor, &mi))
  225. {
  226. if (prcPopUp->right > mi.rcWork.right)
  227. {
  228. OffsetRect(prcPopUp, mi.rcWork.right - prcPopUp->right, 0);
  229. }
  230. }
  231. }
  232. return TRUE;
  233. }
  234. static BOOL SetPopUpPos(HWND hwnd, UINT flags)
  235. {
  236. RECT rc, rw;
  237. if (!hwnd) return FALSE;
  238. HWND hwndParent = GetParent(hwnd);
  239. hwndParent = GetAdaptedParent(hwndParent);
  240. if (GetClientRect(hwndParent, &rc) && GetWindowRect(hwnd, &rw))
  241. {
  242. HWND hwndFound;
  243. wchar_t szClass[2048] = {0};
  244. UINT flags = CENTER_LEFT;
  245. MapWindowPoints(hwndParent, HWND_DESKTOP, (POINT*)&rc, 2);
  246. if (hwndParent == line.hMainWindow ||
  247. (GetClassNameW(hwndParent, szClass, sizeof(szClass)/sizeof(wchar_t)) &&
  248. CSTR_EQUAL == CompareStringW(INVARIANT_LCID, 0, szClass, -1, WASABI_WND_CLASSW, -1) &&
  249. NULL != (hwndFound = FindWindowEx(NULL, hwndParent, WASABI_WND_CLASSW, NULL)))
  250. && IsWindowVisible(hwndFound))
  251. {
  252. OffsetRect(&rc, (rc.right - rc.left + 4), 0);
  253. flags &= ~ CENTER_LEFT;
  254. }
  255. if (CalculatePopUpPos(&rc, &rw, flags))
  256. {
  257. SetWindowPos(hwnd, HWND_TOP, rw.left, rw.top, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE);
  258. }
  259. }
  260. return TRUE;
  261. }
  262. static void Cddb_ProcessResult(MEDIALOOKUP *pLookup, ICddbDisc *pDisc, DWORD *pdwAutoCloseDelay)
  263. {
  264. if (pLookup && pLookup->callback) pLookup->callback(pLookup->result, pDisc, pdwAutoCloseDelay, pLookup->user);
  265. }
  266. static void CALLBACK Execute_ProcessResult(INVOKEDATA *pData)
  267. {
  268. #ifndef IGNORE_API_GRACENOTE
  269. INVOKEDATA data;
  270. if (!pData) return;
  271. CopyMemory(&data, &pData->lookup, sizeof(INVOKEDATA));
  272. if (data.pDisc)
  273. data.pDisc->AddRef();
  274. Cddb_ProcessResult(&data.lookup, data.pDisc, data.pdwAutoCloseDelay);
  275. if (data.pDisc)
  276. data.pDisc->Release();
  277. if (data.evntDone)
  278. SetEvent(data.evntDone);
  279. #endif
  280. }
  281. static LRESULT CALLBACK HookMessageProc(int code, WPARAM wParam, LPARAM lParam)
  282. {
  283. THREADDATA *pData;
  284. if (TLS_OUT_OF_INDEXES == tlsSlot) return 0;
  285. pData = (THREADDATA*)TlsGetValue(tlsSlot);
  286. if (!pData) return 0;
  287. if (code < 0) return CallNextHookEx(pData->hhook, code, wParam, lParam);
  288. if (NULL == ((MSG*)lParam)->hwnd) // thread message
  289. {
  290. switch(((MSG*)lParam)->message)
  291. {
  292. case TM_INVOKERESULTS:
  293. Execute_ProcessResult((INVOKEDATA*)((MSG*)lParam)->lParam);
  294. return TRUE;
  295. case TM_UNINITTHREAD:
  296. Cddb_UninitializeThread();
  297. return TRUE;
  298. }
  299. }
  300. return CallNextHookEx(pData->hhook, code, wParam, lParam);
  301. }
  302. void Cddb_Initialize(void)
  303. {
  304. InitializeCriticalSection(&lockThread);
  305. }
  306. void Cddb_Uninitialize(void)
  307. {
  308. DeleteCriticalSection(&lockThread);
  309. }
  310. static HRESULT Cddb_IsThreadInitialized(void)
  311. {
  312. THREADDATA *pData;
  313. if (TLS_OUT_OF_INDEXES == tlsSlot) return E_OUTOFMEMORY;
  314. pData = (THREADDATA*)TlsGetValue(tlsSlot);
  315. return (pData) ? S_OK : S_FALSE;
  316. }
  317. static HANDLE GetMainThreadHandle(void)
  318. {
  319. HANDLE hThread = NULL;
  320. api_application *pApp = NULL;
  321. waServiceFactory *sf = NULL;
  322. if (!line.service) return NULL;
  323. if (NULL == (sf = line.service->service_getServiceByGuid(applicationApiServiceGuid))) return NULL;
  324. if (NULL == (pApp = (api_application*)sf->getInterface())) { return NULL; }
  325. hThread = pApp->main_getMainThreadHandle();
  326. sf->releaseInterface(pApp);
  327. return hThread;
  328. }
  329. static HRESULT Cddb_InvokeProcessResult(MEDIALOOKUP *pLookup, ICddbDisc *pDisc, DWORD *pdwAutoCloseDelay)
  330. {
  331. HRESULT hr = E_FAIL;
  332. #ifndef IGNORE_API_GRACENOTE
  333. INVOKEDATA data;
  334. if (!pLookup) return E_INVALIDARG;
  335. CopyMemory(&data.lookup, pLookup, sizeof(MEDIALOOKUP));
  336. data.pDisc = pDisc;
  337. data.pdwAutoCloseDelay = pdwAutoCloseDelay;
  338. if (NULL != pDisc)
  339. pDisc->AddRef();
  340. if (!data.lookup.threadId) data.lookup.threadId = GetWindowThreadProcessId(line.hMainWindow, NULL);
  341. if (data.lookup.threadId != GetCurrentThreadId())
  342. {
  343. data.evntDone = CreateEvent(NULL, FALSE, FALSE, NULL);
  344. if (data.evntDone)
  345. {
  346. if (PostThreadMessage(data.lookup.threadId, TM_INVOKERESULTS, 0, (LPARAM)&data))
  347. {
  348. MSG msg;
  349. msg.message = NULL;
  350. for(;;)
  351. {
  352. DWORD status = MsgWaitForMultipleObjectsEx(1, &data.evntDone,
  353. INFINITE, QS_ALLINPUT, MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
  354. if (status == WAIT_OBJECT_0+1)
  355. {
  356. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
  357. {
  358. if (WM_QUIT == msg.message)
  359. {
  360. break;
  361. }
  362. if (0 == CallMsgFilter(&msg, 0))
  363. {
  364. TranslateMessage(&msg);
  365. DispatchMessage(&msg);
  366. }
  367. }
  368. }
  369. else if (status == WAIT_OBJECT_0)
  370. {
  371. break;
  372. }
  373. }
  374. if (WM_QUIT == msg.message)
  375. PostQuitMessage((int)msg.wParam);
  376. hr = S_OK;
  377. }
  378. CloseHandle(data.evntDone);
  379. }
  380. }
  381. else
  382. {
  383. Cddb_ProcessResult(pLookup, pDisc, pdwAutoCloseDelay);
  384. hr = S_OK;
  385. }
  386. if (NULL != pDisc)
  387. pDisc->Release();
  388. #endif
  389. return hr;
  390. }
  391. #ifndef IGNORE_API_GRACENOTE
  392. bool GetRole(ICddbTrack *track, BSTR roleId, BSTR *str)
  393. {
  394. if (!roleId || !*roleId)
  395. return false;
  396. if (!track)
  397. return false;
  398. ICddbCreditsPtr credits;
  399. track->get_Credits(&credits);
  400. if (credits)
  401. {
  402. long creditCount;
  403. credits->get_Count(&creditCount);
  404. for (long c = 0;c < creditCount;c++)
  405. {
  406. ICddbCreditPtr credit;
  407. credits->GetCredit(c + 1, &credit);
  408. if (credit)
  409. {
  410. BSTR thisRole;
  411. credit->get_Id(&thisRole);
  412. if (!wcscmp(thisRole, roleId))
  413. {
  414. credit->get_Name(str);
  415. return true;
  416. }
  417. }
  418. }
  419. }
  420. return false;
  421. }
  422. bool GetRole(ICddbDisc *track, BSTR roleId, BSTR *str)
  423. {
  424. if (!roleId || !*roleId)
  425. return false;
  426. if (!track)
  427. return false;
  428. ICddbCreditsPtr credits;
  429. track->get_Credits(&credits);
  430. if (credits)
  431. {
  432. long creditCount;
  433. credits->get_Count(&creditCount);
  434. for (long c = 0;c < creditCount;c++)
  435. {
  436. ICddbCreditPtr credit;
  437. credits->GetCredit(c + 1, &credit);
  438. if (credit)
  439. {
  440. BSTR thisRole;
  441. credit->get_Id(&thisRole);
  442. if (!wcscmp(thisRole, roleId))
  443. {
  444. credit->get_Name(str);
  445. return true;
  446. }
  447. }
  448. }
  449. }
  450. return false;
  451. }
  452. #endif
  453. HRESULT Cddb_InitializeThread(void)
  454. {
  455. HRESULT hr = S_OK;
  456. EnterCriticalSection(&lockThread);
  457. if (!evntBusy)
  458. {
  459. evntBusyRef = 0;
  460. evntBusy = CreateEvent(NULL, TRUE, FALSE, NULL);
  461. if (NULL == evntBusy)
  462. hr = E_UNEXPECTED;
  463. }
  464. if (SUCCEEDED(hr))
  465. {
  466. evntBusyRef++;
  467. if (TLS_OUT_OF_INDEXES == tlsSlot &&
  468. TLS_OUT_OF_INDEXES == (tlsSlot = TlsAlloc()))
  469. hr = E_OUTOFMEMORY;
  470. if (SUCCEEDED(hr))
  471. {
  472. THREADDATA *pData = (THREADDATA*)TlsGetValue(tlsSlot);
  473. if (pData)
  474. {
  475. pData->ref++;
  476. }
  477. else
  478. {
  479. pData = (THREADDATA*)calloc(1, sizeof(THREADDATA));
  480. if (!pData)
  481. {
  482. hr = E_OUTOFMEMORY;
  483. }
  484. if (SUCCEEDED(hr))
  485. {
  486. pData->hhook = SetWindowsHookExW(WH_MSGFILTER, HookMessageProc, NULL, GetCurrentThreadId());
  487. if (!pData->hhook || !TlsSetValue(tlsSlot, pData))
  488. {
  489. if (pData->hhook) UnhookWindowsHookEx(pData->hhook);
  490. free(pData);
  491. pData = NULL;
  492. hr = E_FAIL;
  493. }
  494. else
  495. {
  496. pData->ref = 1;
  497. pData->uninitCom = (S_OK == CoInitializeEx(NULL, COINIT_APARTMENTTHREADED));
  498. hr = S_OK;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. LeaveCriticalSection(&lockThread);
  505. return hr;
  506. }
  507. HRESULT Cddb_UninitializeThread(void)
  508. {
  509. EnterCriticalSection(&lockThread);
  510. if (TLS_OUT_OF_INDEXES != tlsSlot)
  511. {
  512. THREADDATA *pData = (THREADDATA*)TlsGetValue(tlsSlot);
  513. if (NULL != pData &&
  514. pData->ref && 0 == --pData->ref)
  515. {
  516. TlsSetValue(tlsSlot, NULL);
  517. if (pData->hhook)
  518. UnhookWindowsHookEx(pData->hhook);
  519. if (pData->uninitCom)
  520. CoUninitialize();
  521. free(pData);
  522. }
  523. }
  524. if (evntBusyRef && 0 == --evntBusyRef)
  525. {
  526. CloseHandle(evntBusy);
  527. evntBusy = NULL;
  528. }
  529. LeaveCriticalSection(&lockThread);
  530. return S_OK;
  531. }
  532. LPCWSTR Cddb_CalculateTOC(DINFO *pDisc, LPWSTR pszTOC, size_t cchTOC)
  533. {
  534. LPWSTR p;
  535. if (!pszTOC || !pDisc || !cchTOC) return NULL;
  536. pszTOC[0] = 0x00;
  537. p = pszTOC;
  538. for (int x = 0; x < pDisc->ntracks + 1; x++)
  539. {
  540. if (S_OK != StringCchPrintfExW(p, cchTOC, &p, &cchTOC,
  541. STRSAFE_NULL_ON_FAILURE, L"%lu ", pDisc->pnFrames[x])) return NULL;
  542. }
  543. if (p != pszTOC) *(p - 1) = 0x00;
  544. return pszTOC;
  545. }
  546. // 2 author (???)
  547. // 3 composer
  548. // 6 lyricist
  549. // 7 publisher (how is this different from label?)
  550. // 9 songwriter (music & lyrics)
  551. // 10 conductor/arranger
  552. // 11 Arranger
  553. // 12 Conductor
  554. // 13 Director
  555. // 72 Engineer
  556. // 74 Mastering
  557. // 75 Mastering Location
  558. // 76 Mixing
  559. // 77 Mixing Location
  560. // 78 Producer
  561. // 79 Programming (???)
  562. // 80 Recording Location
  563. // 147 remixer
  564. #ifndef IGNORE_API_GRACENOTE
  565. void GetDiscInfo(ICddbDiscPtr pDisc, DINFO *ps)
  566. {
  567. CComBSTR str, disc_artist, disc_composer, disc_conductor, disc_remixing;
  568. BSTR composerRole=L"3", conductorRole=L"12", remixingRole=L"147";
  569. /*
  570. for (int i=0;i<100;i++)
  571. {
  572. wchar_t id[256] = {0};
  573. _itow(i, id, 10);
  574. ICddbRolePtr role;
  575. pCDDBControl->GetRoleInfo(id, &role);
  576. if (role)
  577. {
  578. BSTR name, description;
  579. role->get_Name(&name);
  580. role->get_Description(&description);
  581. wchar_t str[4096] = {0};
  582. wsprintf(str, L"ID: %s\r\nName: %s\r\nDescription: %s\r\n", id, name, description);
  583. MessageBoxW(NULL, str, L"CDDB Role", MB_OK);
  584. }
  585. }
  586. */
  587. ps->Reset();
  588. if (pDisc == NULL) // yikes!
  589. return;
  590. ICddbDisc2Ptr pDisc2;
  591. pDisc->QueryInterface(&pDisc2);
  592. ICddbDisc2_5Ptr pDisc2_5;
  593. pDisc->QueryInterface(&pDisc2_5);
  594. if (GetRole(pDisc, conductorRole, &disc_conductor) && disc_conductor && disc_conductor.m_str[0])
  595. ps->conductor = ndestring_wcsdup(disc_conductor.m_str);
  596. if (GetRole(pDisc, composerRole, &disc_composer) && disc_composer && disc_composer.m_str[0])
  597. ps->composer = ndestring_wcsdup(disc_composer.m_str);
  598. if (GetRole(pDisc, remixingRole, &disc_remixing) && disc_remixing && disc_remixing.m_str[0])
  599. ps->remixing = ndestring_wcsdup(disc_remixing.m_str);
  600. if (SUCCEEDED(pDisc->get_Artist(&disc_artist)) && disc_artist && disc_artist.m_str[0])
  601. ps->artist = ndestring_wcsdup(disc_artist.m_str);
  602. if (SUCCEEDED(pDisc->get_Year(&str)) && str && str.m_str[0])
  603. ps->year = ndestring_wcsdup(str.m_str);
  604. if (pDisc2_5 == NULL
  605. || (FAILED(pDisc2_5->get_V2GenreStringPrimaryByLevel(3, &str))
  606. && FAILED(pDisc2_5->get_V2GenreStringPrimaryByLevel(2, &str))
  607. && FAILED(pDisc2_5->get_V2GenreStringPrimaryByLevel(1, &str))
  608. && FAILED(pDisc2_5->get_V2GenreStringPrimary(&str)))
  609. )
  610. {
  611. pDisc->get_GenreId(&str);
  612. ICddbGenre *poop = 0;
  613. if (SUCCEEDED(pCDDBControl->GetGenreInfo(str, &poop)) && poop)
  614. {
  615. poop->get_Name(&str);
  616. poop->Release();
  617. }
  618. else
  619. str.Empty();
  620. }
  621. if (str && str.m_str[0])
  622. ps->genre = ndestring_wcsdup(str.m_str);
  623. if (SUCCEEDED(pDisc->get_Title(&str)) && str && str.m_str[0])
  624. ps->title = ndestring_wcsdup(str.m_str);
  625. if (SUCCEEDED(pDisc->get_TitleUId(&str)) && str && str.m_str[0])
  626. ps->tuid = ndestring_wcsdup(str.m_str);
  627. if (SUCCEEDED(pDisc->get_Label(&str)) && str && str.m_str[0])
  628. ps->label = ndestring_wcsdup(str.m_str);
  629. if (SUCCEEDED(pDisc->get_Notes(&str)) && str && str.m_str[0])
  630. ps->notes = ndestring_wcsdup(str.m_str);
  631. long val;
  632. if (SUCCEEDED(pDisc->get_Compilation(&val)))
  633. ps->compilation = !!val;
  634. if (SUCCEEDED(pDisc->get_TotalInSet(&str)) && str && str.m_str[0])
  635. ps->numdiscs = _wtoi(str.m_str);
  636. if (SUCCEEDED(pDisc->get_NumberInSet(&str)) && str && str.m_str[0])
  637. ps->discnum = _wtoi(str.m_str);
  638. for (int x = 0; x < ps->ntracks; x ++)
  639. {
  640. TRACKINFO &trackInfo = ps->tracks[x];
  641. ICddbTrack *t;
  642. ICddbTrack2_5Ptr track2_5;
  643. pDisc->GetTrack(x + 1, &t);
  644. if (!t) break;
  645. t->QueryInterface(&track2_5);
  646. if (SUCCEEDED(t->get_Artist(&str)) && str && str.m_str[0] && (!disc_artist || !disc_artist.m_str[0] || wcscmp(str.m_str, disc_artist.m_str)))
  647. trackInfo.artist = ndestring_wcsdup(str.m_str);
  648. if (SUCCEEDED(t->get_Title(&str)) && str && str.m_str[0])
  649. trackInfo.title = ndestring_wcsdup(str.m_str);
  650. if (SUCCEEDED(t->get_ISRC(&str)) && str && str.m_str[0])
  651. trackInfo.isrc = ndestring_wcsdup(str.m_str);
  652. if (SUCCEEDED(pCDDBControl->GetDiscTagId(pDisc, x + 1, &str)) && str && str.m_str[0])
  653. trackInfo.tagID = ndestring_wcsdup(str.m_str);
  654. if (GetRole(t, conductorRole, &str) && str && str.m_str[0] && (!disc_conductor || !disc_conductor.m_str[0] || wcscmp(str.m_str, disc_conductor.m_str)))
  655. trackInfo.conductor = ndestring_wcsdup(str.m_str);
  656. if (GetRole(t, composerRole, &str) && str && str.m_str[0] && (!disc_composer || !disc_composer.m_str[0] || wcscmp(str.m_str, disc_composer.m_str)))
  657. trackInfo.composer = ndestring_wcsdup(str.m_str);
  658. if (GetRole(t, remixingRole, &str) && str && str.m_str[0] && (!disc_remixing || !disc_remixing.m_str[0] || wcscmp(str.m_str, disc_remixing.m_str)))
  659. trackInfo.remixing = ndestring_wcsdup(str.m_str);
  660. if (track2_5 != NULL && (SUCCEEDED(track2_5->get_ExtDataSerialized(&str)) && str && str.m_str[0]) // try track first
  661. || (pDisc2_5 != NULL && SUCCEEDED(pDisc2_5->get_ExtDataSerialized(&str)) && str && str.m_str[0])) // then disc
  662. trackInfo.extData = ndestring_wcsdup(str.m_str);
  663. t->Release();
  664. }
  665. ps->populated = true;
  666. }
  667. #endif
  668. HRESULT Cddb_GetIUIOptions(void **ppUIOptions)
  669. {
  670. HRESULT hr;
  671. if (!ppUIOptions) return E_INVALIDARG;
  672. *ppUIOptions = NULL;
  673. hr = SetupCDDB(FALSE);
  674. if (SUCCEEDED(hr))
  675. {
  676. #ifndef IGNORE_API_GRACENOTE
  677. hr = CoCreateInstance(CLSID_CddbUIOptions, NULL,
  678. CLSCTX_INPROC_SERVER, IID_ICddbUIOptions, ppUIOptions);
  679. #endif
  680. }
  681. return hr;
  682. }
  683. HRESULT Cddb_GetIControl(void **ppControl)
  684. {
  685. HRESULT hr;
  686. if (!ppControl) return E_INVALIDARG;
  687. *ppControl = NULL;
  688. hr = SetupCDDB(FALSE);
  689. if(SUCCEEDED(hr))
  690. {
  691. #ifndef IGNORE_API_GRACENOTE
  692. pCDDBControl->AddRef();
  693. *ppControl = pCDDBControl;
  694. #endif
  695. }
  696. return hr;
  697. }
  698. HRESULT Cddb_GetICacheManger(void **ppCache)
  699. {
  700. HRESULT hr;
  701. if (!ppCache) return E_INVALIDARG;
  702. *ppCache = NULL;
  703. hr = SetupCDDB(FALSE);
  704. if (SUCCEEDED(hr))
  705. {
  706. #ifndef IGNORE_API_GRACENOTE
  707. hr = CoCreateInstance(CLSID_CddbCacheManager, NULL,
  708. CLSCTX_INPROC_SERVER, IID_ICddbCacheManager, ppCache);
  709. #endif
  710. }
  711. return hr;
  712. }
  713. static HRESULT SetupCDDB(BOOL resetError)
  714. {
  715. static HRESULT result(S_FALSE);
  716. #ifndef IGNORE_API_GRACENOTE
  717. if (S_FALSE == result || (FAILED(result) && resetError))
  718. {
  719. if (AGAVE_API_GRACENOTE)
  720. pCDDBControl = AGAVE_API_GRACENOTE->GetCDDB();
  721. result = (NULL == pCDDBControl) ? CDDBCTLNotInitialized : S_OK;
  722. if (SUCCEEDED(result))
  723. {
  724. HRESULT hr;
  725. ICddbOptionsPtr pOptions;
  726. hr = pCDDBControl->GetOptions(&pOptions);
  727. if (SUCCEEDED(hr))
  728. {
  729. LONG lVal;
  730. BOOL bUpdate(FALSE);
  731. hr = pOptions->get_AsyncCompletion(&lVal);
  732. if (SUCCEEDED(hr) && !lVal) { pOptions->put_AsyncCompletion(1); bUpdate = TRUE; }
  733. hr = pOptions->get_ProgressEvents(&lVal);
  734. if (SUCCEEDED(hr) && !lVal) { pOptions->put_ProgressEvents(1); bUpdate = TRUE; }
  735. #if defined(BETA) || defined(INTERNAL) || defined(NIGHT)
  736. pOptions->put_TestSubmitMode(TRUE); // BETA
  737. #endif
  738. if (bUpdate) pCDDBControl->SetOptions(pOptions);
  739. }
  740. if (SUCCEEDED(eventMngr.Advise(pCDDBControl)))
  741. {
  742. eventMngr.RegisterCallback(CDDB_CB_CMDCOMPLETED, Cddb_OnCommandCompleted);
  743. eventMngr.RegisterCallback(CDDB_CB_CMDPROGRESS, Cddb_OnCommandProgress);
  744. }
  745. }
  746. }
  747. #endif
  748. return result;
  749. }
  750. #ifndef IGNORE_API_GRACENOTE
  751. HRESULT Cddb_GetDiscFromCache(BSTR bstrTOC, ICddbDisc **ppDisc)
  752. {
  753. HRESULT hr;
  754. ICddbCacheManager *pCache;
  755. if (!ppDisc) return E_INVALIDARG;
  756. *ppDisc = NULL;
  757. if (!bstrTOC) return CDDB_E_BADTOC;
  758. hr = Cddb_GetICacheManger((void**)&pCache);
  759. if (SUCCEEDED(hr))
  760. {
  761. hr = pCache->FetchDiscByToc(bstrTOC, ppDisc);
  762. pCache->Release();
  763. }
  764. return hr;
  765. }
  766. #endif
  767. static LRESULT CALLBACK DisplayDiscInfoWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  768. {
  769. LRESULT result;
  770. HWND hwndListener;
  771. WNDPROC fnOldProc = (WNDPROC)GetPropW(hwnd, L"WNDPROC");
  772. switch(uMsg)
  773. {
  774. case WM_INITDIALOG:
  775. result = (IsWindowUnicode(hwnd)) ? CallWindowProcW(fnOldProc, hwnd, uMsg, wParam, lParam) : CallWindowProcA(fnOldProc, hwnd, uMsg, wParam, lParam);
  776. hwndListener = (HWND)GetPropW(hwnd, L"LISTENER");
  777. if (hwndListener) CddbProgressDlg_SetStatus(hwndListener, MAKEINTRESOURCEW(IDS_OPENING), -1);
  778. return result;
  779. case WM_DESTROY:
  780. {
  781. RemovePropW(hwnd, L"WNDPROC");
  782. hwndListener = (HWND)GetPropW(hwnd, L"LISTENER");
  783. if (hwndListener)
  784. {
  785. RECT rc, rw;
  786. if (GetWindowRect(hwndListener, &rc) && GetWindowRect(hwnd, &rw))
  787. {
  788. CalculatePopUpPos(&rw, &rc, CENTER_LEFT);
  789. SetWindowPos(hwndListener, NULL, rc.left, rc.top, 0, 0,
  790. SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
  791. }
  792. }
  793. HWND *list = (HWND*)GetPropW(hwnd, L"WNDLIST");
  794. RemovePropW(hwnd, L"WNDLIST");
  795. if (list)
  796. {
  797. for (HWND *p = list; *p != NULL; p++) ShowWindowAsync(*p, SW_SHOWNA);
  798. free(list);
  799. }
  800. RemovePropW(hwnd, L"LISTENER");
  801. if (fnOldProc) SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)fnOldProc);
  802. }
  803. break;
  804. case WM_SHOWWINDOW:
  805. if (wParam)
  806. {
  807. hwndListener = (HWND)GetPropW(hwnd, L"LISTENER");
  808. if(hwndListener)
  809. {
  810. HWND *list = (HWND*)calloc(24, sizeof(HWND));
  811. if (!FindAllOwnedWindows(hwndListener, list, 24, 0) || !SetPropW(hwnd, L"WNDLIST", list)) free(list);
  812. else for (HWND *p = list; *p != NULL; p++) ShowWindowAsync(*p, SW_HIDE);
  813. ShowWindowAsync(hwndListener, SW_HIDE);
  814. }
  815. }
  816. break;
  817. }
  818. if (!fnOldProc)
  819. {
  820. return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  821. }
  822. return (IsWindowUnicode(hwnd)) ? CallWindowProcW(fnOldProc, hwnd, uMsg, wParam, lParam) : CallWindowProcA(fnOldProc, hwnd, uMsg, wParam, lParam);
  823. }
  824. static void CALLBACK OnGrabbed_DisplayDiscInfo(HWND hwnd, CREATESTRUCT *lpcs, HWND *phwndInsertAfter, ULONG_PTR user)
  825. {
  826. WNDPROC oldProc = (WNDPROC)(LONG_PTR)SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)DisplayDiscInfoWndProc);
  827. if (oldProc)
  828. {
  829. SetPropW(hwnd, L"WNDPROC", oldProc);
  830. SetPropW(hwnd, L"LISTENER", (HANDLE)user);
  831. }
  832. if(user)
  833. {
  834. RECT rc, rw;
  835. if (GetClientRect((HWND)user, &rc))
  836. {
  837. MapWindowPoints((HWND)user, HWND_DESKTOP, (POINT*)&rc, 2);
  838. SetRect(&rw, lpcs->x, lpcs->y, lpcs->x + lpcs->cx, lpcs->y + lpcs->cy);
  839. CalculatePopUpPos(&rc, &rw, CENTER_LEFT);
  840. lpcs->x = rw.left;
  841. lpcs->y = rw.top;
  842. }
  843. }
  844. else ShowWindow((HWND)user, SW_HIDE);
  845. }
  846. void Cddb_GetResultText(HRESULT hr, LPWSTR pszResult, INT cchResult, LPWSTR pszReason, INT cchReason)
  847. {
  848. INT nResult, nReason;
  849. nResult = (S_OK == hr) ? IDS_SUCCESS : IDS_NOT_FOUND;
  850. nReason = 0;
  851. if (FAILED(hr))
  852. {
  853. switch(hr)
  854. {
  855. #ifndef IGNORE_API_GRACENOTE
  856. case CDDBCTLNotInitialized: nReason = IDS_CDDB_NOT_INSTALLED; break;
  857. case CDDBCTLBusy: nReason = IDS_CDDB_E_BUSY; break;
  858. case CDDB_E_BADTOC: nReason = IDS_CDDB_E_BADTOC; break;
  859. case E_ABORT:
  860. case CDDBTRNCancelled:
  861. nReason = IDS_CDDB_E_ABORT; break;
  862. #endif
  863. default: nReason = IDS_CDDB_E_FAIL; break;
  864. }
  865. }
  866. if (pszReason && cchReason)
  867. {
  868. if(nReason)
  869. { INT len;
  870. WASABI_API_LNGSTRINGW_BUF(IDS_REASON, pszReason, cchReason);
  871. len = lstrlenW(pszReason);
  872. cchReason -= len;
  873. WASABI_API_LNGSTRINGW_BUF(nReason, pszReason + len, cchReason);
  874. }
  875. else pszReason[0] = 0x00;
  876. }
  877. if (pszResult && cchResult)
  878. {
  879. WASABI_API_LNGSTRINGW_BUF(nResult, pszResult, cchResult);
  880. }
  881. }
  882. static HRESULT Cddb_FinishLookup(MEDIALOOKUP *pLookup, ICddbDisc *pDisc)
  883. {
  884. HRESULT hr = E_FAIL;
  885. #ifndef IGNORE_API_GRACENOTE
  886. MEDIALOOKUP lookup_cpy;
  887. DWORD delay = AUTOCLOSE_NOW;
  888. if (!pLookup) hr = E_INVALIDARG;
  889. if (!evntBusy) hr = E_FAIL;
  890. else hr = S_OK;
  891. if (NULL != pDisc)
  892. pDisc->AddRef();
  893. if (NULL != pLookup)
  894. {
  895. CopyMemory(&lookup_cpy, pLookup, sizeof(MEDIALOOKUP));
  896. SecureZeroMemory(pLookup, sizeof(MEDIALOOKUP));
  897. }
  898. else
  899. SecureZeroMemory(&lookup_cpy, sizeof(MEDIALOOKUP));
  900. if (SUCCEEDED(hr))
  901. {
  902. HRESULT hrInvoke;
  903. if(S_OK != lookup_cpy.result)
  904. {
  905. if (NULL != pDisc)
  906. pDisc->Release();
  907. pDisc = NULL;
  908. }
  909. if (lookup_cpy.hwndInfo)
  910. {
  911. CddbProgressDlg_SetExtendedMode(lookup_cpy.hwndInfo, FALSE);
  912. CddbProgressDlg_EnableAbortButton(lookup_cpy.hwndInfo, FALSE);
  913. CddbProgressDlg_SetStatus(lookup_cpy.hwndInfo, MAKEINTRESOURCEW(IDS_PROCESSING), -1);
  914. }
  915. if (FAILED(lookup_cpy.result))
  916. delay = 5000;
  917. hrInvoke = Cddb_InvokeProcessResult(&lookup_cpy, pDisc, &delay);
  918. if (FAILED(hrInvoke) && S_OK == lookup_cpy.result)
  919. lookup_cpy.result = hrInvoke;
  920. }
  921. SET_BUSY(FALSE);
  922. if (SUCCEEDED(hr))
  923. {
  924. if (lookup_cpy.hwndInfo)
  925. {
  926. RECT rc;
  927. if (GetWindowRect(lookup_cpy.hwndInfo, &rc))
  928. {
  929. g_lastcddbpos.x = rc.left;
  930. g_lastcddbpos.y = rc.top;
  931. }
  932. lookup_cpy.result = Cddb_DisplayResultDlg(lookup_cpy.hwndInfo, lookup_cpy.result, delay,
  933. CDDB_UI_USE_PARENT | lookup_cpy.flags);
  934. if (delay > 10 && 0 == (CDDB_UI_RESULT_MODAL & lookup_cpy.flags) || FAILED(lookup_cpy.result))
  935. {
  936. CddbProgressDlg_ExitModal(lookup_cpy.hwndInfo, lookup_cpy.result, FALSE);
  937. }
  938. }
  939. hr = lookup_cpy.result;
  940. }
  941. if (NULL != lookup_cpy.bstrTOC)
  942. SysFreeString(lookup_cpy.bstrTOC);
  943. if (lookup_cpy.threadId == GetCurrentThreadId())
  944. Cddb_UninitializeThread();
  945. else
  946. {
  947. if (PostThreadMessage(lookup_cpy.threadId, TM_UNINITTHREAD, 0, 0L))
  948. {
  949. /* MSG msg;
  950. DWORD status;
  951. msg.message = NULL;
  952. for(;;)
  953. {
  954. status = MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
  955. if (status == WAIT_OBJECT_0+1)
  956. {
  957. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
  958. {
  959. if (WM_QUIT == msg.message) break;
  960. if (!CallMsgFilter(&msg, 0)) DispatchMessage(&msg);
  961. }
  962. }
  963. else if (status == WAIT_OBJECT_0)
  964. {
  965. break;
  966. }
  967. }
  968. if (WM_QUIT == msg.message) PostQuitMessage((int)msg.wParam);*/
  969. }
  970. }
  971. if (NULL != pDisc)
  972. pDisc->Release();
  973. #endif
  974. return hr;
  975. }
  976. static void CALLBACK CddbProgressDlg_OnAbort(HWND hwndDlg, BSTR bstrUser)
  977. {
  978. #ifndef IGNORE_API_GRACENOTE
  979. ICDDBControl *pControl;
  980. HRESULT hr = Cddb_GetIControl((void**)&pControl);
  981. if (SUCCEEDED(hr))
  982. {
  983. LONG lVal;
  984. pControl->Cancel(&lVal);
  985. CddbProgressDlg_EnableAbortButton(hwndDlg, FALSE);
  986. pControl->Release();
  987. }
  988. //CddbProgressDlg_Completed(hwndDlg, L"Aborted !!!", 4600);
  989. #endif
  990. }
  991. typedef struct _MODALDATA
  992. {
  993. ICddbDisc *pDisc;
  994. UINT flags;
  995. BSTR bstrTOC;
  996. } MODALDATA;
  997. #ifndef IGNORE_API_GRACENOTE
  998. HRESULT Cddb_DisplayDiscInfo(ICddbDisc *pDisc, CDDBUIFlags *pUIFlags, HWND hwndParent)
  999. {
  1000. HRESULT hr;
  1001. ICDDBControl *pControl;
  1002. BOOL bManual = FALSE;
  1003. hr = Cddb_GetIControl((void**)&pControl);
  1004. if (FAILED(hr)) return hr;
  1005. if (hwndParent)
  1006. {
  1007. if (!BeginGrabCreateWindow(L"#32770", NULL, NULL, OnGrabbed_DisplayDiscInfo, (ULONG_PTR)hwndParent))
  1008. {
  1009. ShowWindow(hwndParent, SW_HIDE);
  1010. bManual = TRUE;
  1011. }
  1012. }
  1013. hr = pControl->DisplayDiscInfo(pDisc, *pUIFlags, pUIFlags);
  1014. if (hwndParent)
  1015. {
  1016. EndGrabCreateWindow();
  1017. if (bManual) ShowWindow(hwndParent, SW_SHOW);
  1018. }
  1019. return hr;
  1020. }
  1021. static void CALLBACK CddbProgressDlg_OnSubmitNew(HWND hwndDlg, BSTR bstrUser)
  1022. {
  1023. HRESULT hr;
  1024. ICDDBControl *pControl;
  1025. CDDBUIFlags uiflags;
  1026. MODALDATA *pData;
  1027. wchar_t szText[256] = {0};
  1028. pData = (MODALDATA*)CddbProgressDlg_GetUserData(hwndDlg);
  1029. if (!pData)
  1030. {
  1031. CddbProgressDlg_ExitModal(hwndDlg, E_INVALIDARG, TRUE);
  1032. return;
  1033. }
  1034. CddbProgressDlg_ShowButton1(hwndDlg, NULL, NULL, NULL);
  1035. SetWindowText(hwndDlg, WASABI_API_LNGSTRINGW_BUF(IDS_SUBMITDISC_TITLE, szText, sizeof(szText)/sizeof(wchar_t)));
  1036. CddbProgressDlg_Initialize(hwndDlg, MAKEINTRESOURCEW(IDS_QUERYING), CddbProgressDlg_OnAbort, NULL);
  1037. CddbProgressDlg_SetStatus(hwndDlg, MAKEINTRESOURCEW(IDS_INITIALIZING), -1);
  1038. hr = Cddb_GetIControl((void**)&pControl);
  1039. if (SUCCEEDED(hr))
  1040. {
  1041. hr = pControl->GetSubmitDisc(pData->bstrTOC, NULL, NULL, &pData->pDisc);
  1042. pControl->Release();
  1043. }
  1044. if (!pData->pDisc)
  1045. {
  1046. CddbProgressDlg_ExitModal(hwndDlg, hr, TRUE);
  1047. return;
  1048. }
  1049. uiflags = (CDDBUIFlags)(UI_SUBMITNEW | UI_EDITMODE);
  1050. hr = Cddb_DisplayDiscInfo(pData->pDisc, &uiflags, hwndDlg);
  1051. if (SUCCEEDED(hr) && (0 == ((uiflags & (UI_OK | UI_DATA_CHANGED)) == (UI_OK | UI_DATA_CHANGED))))
  1052. hr = S_FALSE;
  1053. if (S_OK != hr)
  1054. {
  1055. pData->pDisc->Release();
  1056. pData->pDisc = NULL;
  1057. }
  1058. CddbProgressDlg_ExitModal(hwndDlg, hr, TRUE);
  1059. return;
  1060. }
  1061. static void CALLBACK CddbProgressDlg_OnAcceptMatch(HWND hwndDlg, BSTR bstrUser)
  1062. {
  1063. MODALDATA *pData;
  1064. pData = (MODALDATA*)CddbProgressDlg_GetUserData(hwndDlg);
  1065. if (!pData)
  1066. {
  1067. CddbProgressDlg_ExitModal(hwndDlg, E_INVALIDARG, TRUE);
  1068. return;
  1069. }
  1070. pData->pDisc = (ICddbDisc*)(LONG_PTR)(LONG)(CddbProgressDlg_GetSelRecordIndex(hwndDlg) + 1);
  1071. CddbProgressDlg_ExitModal(hwndDlg, S_OK, TRUE);
  1072. return;
  1073. }
  1074. #endif
  1075. HRESULT Cddb_DoLookup(LPCWSTR pszTOC, HWND hwndParent, CDDB_CB callback, UINT flags, ULONG_PTR user)
  1076. {
  1077. #ifndef IGNORE_API_GRACENOTE
  1078. HRESULT hr;
  1079. ICDDBControl *pControl;
  1080. CDDBMatchCode matchCode;
  1081. if (!callback) return E_INVALIDARG;
  1082. if (IS_BUSY(0))
  1083. {
  1084. return (CSTR_EQUAL == CompareStringW(INVARIANT_LCID, 0, g_lookup.bstrTOC, -1, pszTOC, -1)) ? E_PENDING : CDDBCTLBusy;
  1085. }
  1086. hr = Cddb_InitializeThread();
  1087. if (FAILED(hr)) return hr;
  1088. SET_BUSY(TRUE);
  1089. CLEARLOOKUP();
  1090. g_lookup.callback = callback;
  1091. g_lookup.user = user;
  1092. g_lookup.flags = flags;
  1093. g_lookup.threadId = GetCurrentThreadId();
  1094. g_lookup.bstrTOC = SysAllocString(pszTOC);
  1095. eventMngr.SetUserParam((ULONG_PTR)&g_lookup);
  1096. if (0 == (CDDB_NOCACHE & flags))
  1097. {
  1098. ICddbDisc *pDisc;
  1099. g_lookup.result = Cddb_GetDiscFromCache(g_lookup.bstrTOC, &pDisc);
  1100. if (CDDBTRNDataStoreNotCached != g_lookup.result)
  1101. {
  1102. Cddb_FinishLookup(&g_lookup, pDisc);
  1103. if (pDisc) pDisc->Release();
  1104. return S_OK;
  1105. }
  1106. }
  1107. if (CDDB_NOINET == (CDDB_NOINET & flags))
  1108. {
  1109. g_lookup.result = S_FALSE;
  1110. Cddb_FinishLookup(&g_lookup, NULL);
  1111. return S_OK;
  1112. }
  1113. hwndParent = GetAdaptedParent(hwndParent);
  1114. g_lookup.hwndInfo = CddbProgressDlg_Create(hwndParent, SW_HIDE);
  1115. hwndProgressListener = g_lookup.hwndInfo;
  1116. CddbProgressDlg_Initialize(g_lookup.hwndInfo, MAKEINTRESOURCEW(IDS_QUERYING), CddbProgressDlg_OnAbort, NULL);
  1117. CddbProgressDlg_SetStatus(g_lookup.hwndInfo, MAKEINTRESOURCEW(IDS_INITIALIZING), -1);
  1118. if (g_lookup.hwndInfo)
  1119. {
  1120. SetPopUpPos(g_lookup.hwndInfo, CENTER_LEFT);
  1121. ShowWindow(g_lookup.hwndInfo, SW_SHOWNA);
  1122. }
  1123. g_lookup.result = Cddb_GetIControl((void**)&pControl);
  1124. if (FAILED(g_lookup.result))
  1125. {
  1126. Cddb_FinishLookup(&g_lookup, NULL);
  1127. return S_OK;
  1128. }
  1129. g_lookup.result = pControl->LookupMediaByToc(g_lookup.bstrTOC, TRUE, &matchCode);
  1130. pControl->Release();
  1131. if (FAILED(g_lookup.result))
  1132. {
  1133. Cddb_FinishLookup(&g_lookup, NULL);
  1134. return S_OK;
  1135. }
  1136. if (CDDB_UI_MODAL & flags)
  1137. {
  1138. RECT rc;
  1139. hr = CddbProgressDlg_DoModal(g_lookup.hwndInfo, &rc);
  1140. if (SUCCEEDED(hr))
  1141. {
  1142. SET_BUSY(FALSE);
  1143. CLEARLOOKUP();
  1144. g_lastcddbpos.x = rc.left;
  1145. g_lastcddbpos.y = rc.top;
  1146. }
  1147. }
  1148. return S_OK;
  1149. #else
  1150. return E_FAIL;
  1151. #endif
  1152. }
  1153. static HRESULT Cddb_DoSubmitNewDlg(HWND hwndCaller, BSTR bstrTOC, UINT flags, ICddbDisc **ppDisc)
  1154. {
  1155. RECT rw;
  1156. HRESULT hr;
  1157. HWND hwndInfo, hwndOldListener, hwndParent;
  1158. MODALDATA data;
  1159. wchar_t szText[256] = {0};
  1160. if (!ppDisc) return E_INVALIDARG;
  1161. data.pDisc = NULL;
  1162. data.bstrTOC = bstrTOC;
  1163. data.flags = flags;
  1164. hwndParent = (hwndCaller) ? GetParent(hwndCaller) : NULL;
  1165. if (!hwndParent) hwndParent = line.hMainWindow;
  1166. hwndInfo = CddbProgressDlg_Create(hwndParent, SW_HIDE);
  1167. if (!hwndInfo || !CddbProgressDlg_SetUserData(hwndInfo, &data))
  1168. {
  1169. if (hwndInfo)
  1170. DestroyWindow(hwndInfo);
  1171. if (NULL != data.bstrTOC)
  1172. SysFreeString(data.bstrTOC);
  1173. return E_FAIL;
  1174. }
  1175. SetWindowText(hwndInfo, WASABI_API_LNGSTRINGW_BUF(IDS_LOOKUPRESULT_TITLE, szText, sizeof(szText)/sizeof(wchar_t)));
  1176. CddbProgressDlg_Initialize(hwndInfo, MAKEINTRESOURCEW(IDS_NOT_FOUND), NULL, NULL);
  1177. CddbProgressDlg_Completed(hwndInfo, MAKEINTRESOURCEW(IDS_SUBMIT_OFFER), NULL, AUTOCLOSE_NEVER, S_OK);
  1178. #ifndef IGNORE_API_GRACENOTE
  1179. CddbProgressDlg_ShowButton1(hwndInfo, MAKEINTRESOURCEW(IDS_SUBMITNEW), CddbProgressDlg_OnSubmitNew, NULL);
  1180. #endif
  1181. hwndOldListener = hwndProgressListener;
  1182. if (hwndCaller)
  1183. {
  1184. GetWindowRect(hwndCaller, &rw);
  1185. SetWindowPos(hwndInfo, HWND_TOP, rw.left, rw.top, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
  1186. ShowWindowAsync(hwndCaller, SW_HIDE);
  1187. }
  1188. else ShowWindow(hwndInfo, SW_SHOW);
  1189. RECT rc;
  1190. hr = CddbProgressDlg_DoModal(hwndInfo, &rc);
  1191. *ppDisc = data.pDisc;
  1192. if (hwndCaller)
  1193. {
  1194. SetWindowPos(hwndCaller, HWND_TOP, rc.left, rc.top, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOSIZE | SWP_SHOWWINDOW);
  1195. }
  1196. hwndProgressListener = hwndOldListener;
  1197. return hr;
  1198. }
  1199. #ifndef IGNORE_API_GRACENOTE
  1200. static HRESULT Cddb_DoFuzzyMatchDlg(HWND hwndCaller, UINT flags, ICddbDiscs *pDiscs, LONG *plVal)
  1201. {
  1202. HRESULT hr;
  1203. LONG count;
  1204. ICddbDisc *pDisc(NULL);
  1205. BSTR bstrArtist, bstrTitle;
  1206. RECT rw;
  1207. HWND hwndInfo, hwndOldListener, hwndParent;
  1208. MODALDATA data;
  1209. wchar_t szText[256] = {0};
  1210. data.pDisc = NULL;
  1211. data.flags = flags;
  1212. if (!plVal || !pDiscs) return E_INVALIDARG;
  1213. *plVal = 0L;
  1214. hwndParent = (hwndCaller) ? GetParent(hwndCaller) : NULL;
  1215. if (!hwndParent) hwndParent = line.hMainWindow;
  1216. hwndInfo = CddbProgressDlg_Create(hwndParent, SW_HIDE);
  1217. if (!hwndInfo || !CddbProgressDlg_SetUserData(hwndInfo, &data))
  1218. {
  1219. if (hwndInfo) DestroyWindow(hwndInfo);
  1220. return E_FAIL;
  1221. }
  1222. hwndOldListener = hwndProgressListener;
  1223. SetWindowText(hwndInfo, WASABI_API_LNGSTRINGW_BUF(IDS_LOOKUPRESULT_TITLE, szText, sizeof(szText)/sizeof(wchar_t)));
  1224. CddbProgressDlg_Initialize(hwndInfo, MAKEINTRESOURCEW(IDS_FUZZYDISC_TITLE), NULL, NULL);
  1225. CddbProgressDlg_SetStatus(hwndInfo, MAKEINTRESOURCEW(IDS_INITIALIZING), -1);
  1226. if (hwndCaller)
  1227. {
  1228. GetWindowRect(hwndCaller, &rw);
  1229. SetWindowPos(hwndInfo, HWND_TOP, rw.left, rw.top, 0, 0, SWP_NOSIZE);
  1230. ShowWindowAsync(hwndCaller, SW_HIDE);
  1231. }
  1232. ShowWindow(hwndInfo, SW_SHOWNA);
  1233. if (FAILED(pDiscs->get_Count(&count))) count = 0;
  1234. for(long i = 1; i <= count; i++)
  1235. {
  1236. if (SUCCEEDED(pDiscs->GetDisc(i, &pDisc)))
  1237. {
  1238. if (FAILED(pDisc->get_Artist(&bstrArtist))) bstrArtist = NULL;
  1239. if (FAILED(pDisc->get_Title(&bstrTitle))) bstrTitle = NULL;
  1240. CddbProgressDlg_AddRecord(hwndInfo, bstrArtist, bstrTitle, NULL);
  1241. if (bstrArtist) SysFreeString(bstrArtist);
  1242. if (bstrTitle) SysFreeString(bstrTitle);
  1243. pDisc->Release();
  1244. }
  1245. }
  1246. CddbProgressDlg_ShowButton1(hwndInfo, MAKEINTRESOURCEW(IDS_ACCEPT), CddbProgressDlg_OnAcceptMatch, NULL);
  1247. CddbProgressDlg_SetExtendedMode(hwndInfo, TRUE);
  1248. CddbProgressDlg_Completed(hwndInfo, MAKEINTRESOURCEW(IDS_FOUND_MULTIPLE), NULL, AUTOCLOSE_NEVER, S_OK);
  1249. UpdateWindow(hwndInfo);
  1250. SetForegroundWindow(hwndInfo);
  1251. RECT rc;
  1252. hr = CddbProgressDlg_DoModal(hwndInfo, &rc);
  1253. g_lastcddbpos.x = rc.left;
  1254. g_lastcddbpos.y = rc.top;
  1255. *plVal = ((LONG)(LONG_PTR)data.pDisc);
  1256. if (hwndCaller)
  1257. {
  1258. SetWindowPos(hwndCaller, HWND_TOP, rc.left, rc.top, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOSIZE | SWP_SHOWWINDOW);
  1259. }
  1260. hwndProgressListener = hwndOldListener;
  1261. return hr;
  1262. }
  1263. #endif
  1264. static void CALLBACK Cddb_OnCommandCompleted(LONG lCommandCode, HRESULT hCommandResult, VARIANT *pCommandData, UINT_PTR user)
  1265. {
  1266. #ifndef IGNORE_API_GRACENOTE
  1267. switch(lCommandCode)
  1268. {
  1269. case CMD_LookupMediaByToc: Cddb_OnMediaLookupCompleted(hCommandResult, (CDDBMatchCode)pCommandData->lVal, (MEDIALOOKUP*)user); break;
  1270. case CMD_GetFullDiscInfo: Cddb_OnGetFullDiscInfoCompleted(hCommandResult, (ICddbDisc*)pCommandData->byref, (MEDIALOOKUP*)user); break;
  1271. case CMD_SubmitDisc: Cddb_OnSubmitDiscCompleted(hCommandResult, (MEDIALOOKUP*)user); break;
  1272. }
  1273. #endif
  1274. }
  1275. static void CALLBACK Cddb_OnCommandProgress(LONG lCommandCode, LONG lProgressCode, LONG lBytesDone, LONG lBytesTotal, UINT_PTR user)
  1276. {
  1277. #ifndef IGNORE_API_GRACENOTE
  1278. if (hwndProgressListener)
  1279. {
  1280. INT ids(0), nComplete(-1);
  1281. switch(lProgressCode)
  1282. {
  1283. case CMD_CONNECTING: ids = IDS_CDDB_PROGRESS_CONNECTING; break;
  1284. case CMD_SENDING: ids = IDS_CDDB_PROGRESS_SENDING; nComplete = lBytesTotal?((100 * lBytesDone)/lBytesTotal):100; break;
  1285. case CMD_RECEIVING: ids = IDS_CDDB_PROGRESS_RECEIVING; nComplete = lBytesTotal?((100 * lBytesDone)/lBytesTotal):100; break;
  1286. case CMD_CANCELLED: ids = IDS_CDDB_PROGRESS_CANCELLED; break;
  1287. case CMD_WAITING: ids = IDS_CDDB_PROGRESS_WAITING; lBytesTotal?((100 * lBytesDone)/lBytesTotal):100; break;
  1288. case CMD_COMPLETED: ids = IDS_CDDB_PROGRESS_COMPLETED; break;
  1289. }
  1290. if (ids) CddbProgressDlg_SetStatus(hwndProgressListener, MAKEINTRESOURCEW(ids), nComplete);
  1291. }
  1292. #endif
  1293. }
  1294. #ifndef IGNORE_API_GRACENOTE
  1295. static void Cddb_OnMediaLookupCompleted(HRESULT result, CDDBMatchCode matchCode, MEDIALOOKUP *pLookup)
  1296. {
  1297. ICDDBControl *pControl;
  1298. ICddbDisc *pDisc(NULL), *pfuzzyDisc(NULL);
  1299. ICddbDiscs *pDiscs(NULL);
  1300. LONG lVal;
  1301. wchar_t szText[256] = {0};
  1302. if (!pLookup) return;
  1303. SetWindowText(pLookup->hwndInfo, WASABI_API_LNGSTRINGW_BUF(IDS_LOOKUPRESULT_TITLE, szText, sizeof(szText)/sizeof(wchar_t)));
  1304. pLookup->result = result;
  1305. if (FAILED(pLookup->result))
  1306. {
  1307. switch(pLookup->result)
  1308. {
  1309. case CDDBSVCServiceError: pLookup->result = CDDB_E_BADTOC; break;//
  1310. }
  1311. Cddb_FinishLookup(pLookup, NULL);
  1312. return;
  1313. }
  1314. pLookup->result = Cddb_GetIControl((void**)&pControl);
  1315. if (FAILED(pLookup->result))
  1316. {
  1317. Cddb_FinishLookup(pLookup, NULL);
  1318. return;
  1319. }
  1320. switch(matchCode)
  1321. {
  1322. case MATCH_EXACT:
  1323. CddbProgressDlg_SetStatus(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_FOUND_EXACT), -1);
  1324. pLookup->result = pControl->GetMatchedDiscInfo((IUnknown**)&pDisc);
  1325. break;
  1326. case MATCH_MULTIPLE:
  1327. pLookup->result = S_MULTIPLE;
  1328. CddbProgressDlg_SetStatus(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_FOUND_MULTIPLE), -1);
  1329. if (0 != ((CDDB_UI_MULTIPLE | CDDB_RESOLVE_MULTIPLE) & pLookup->flags))
  1330. {
  1331. lVal = 0;
  1332. pLookup->result = pControl->GetMatchedDiscInfo((IUnknown**)&pDiscs);
  1333. if (SUCCEEDED(pLookup->result))
  1334. {
  1335. if (0 != (CDDB_UI_MULTIPLE & pLookup->flags))
  1336. {
  1337. pLookup->result = Cddb_DoFuzzyMatchDlg(pLookup->hwndInfo, 0, pDiscs, &lVal);
  1338. if (S_OK != pLookup->result) lVal = 0;
  1339. }
  1340. if (0 == lVal && 0 != (CDDB_RESOLVE_MULTIPLE & pLookup->flags)) lVal = 1;
  1341. if (lVal)
  1342. {
  1343. CddbProgressDlg_Initialize(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_QUERYING), CddbProgressDlg_OnAbort, NULL);
  1344. CddbProgressDlg_SetStatus(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_INITIALIZING), -1);
  1345. pLookup->result = pDiscs->GetDisc(lVal, &pfuzzyDisc);
  1346. if (SUCCEEDED(pLookup->result))
  1347. {
  1348. if (!pfuzzyDisc) pLookup->result = S_FALSE;
  1349. else
  1350. {
  1351. pLookup->result = pfuzzyDisc->IsSubmit(&lVal);
  1352. if (FAILED(pLookup->result) || !lVal)
  1353. {
  1354. pLookup->result = pControl->GetFullDiscInfo(pfuzzyDisc, TRUE, &pDisc);
  1355. pfuzzyDisc->Release();
  1356. if (SUCCEEDED(pLookup->result))
  1357. {
  1358. pDiscs->Release();
  1359. pControl->Release();
  1360. return;
  1361. }
  1362. }
  1363. else pDisc = pfuzzyDisc;
  1364. }
  1365. }
  1366. }
  1367. pDiscs->Release();
  1368. }
  1369. }
  1370. break;
  1371. case MATCH_NONE:
  1372. pLookup->result = S_FALSE;
  1373. CddbProgressDlg_SetStatus(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_NOT_FOUND), -1);
  1374. if (CDDB_UI_NOMATCH & pLookup->flags)
  1375. {
  1376. pLookup->result = Cddb_DoSubmitNewDlg(pLookup->hwndInfo, pLookup->bstrTOC, 0, &pDisc);
  1377. if (S_OK == pLookup->result)
  1378. {
  1379. LONG lVal;
  1380. CddbProgressDlg_Initialize(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_SUBMITTING), CddbProgressDlg_OnAbort, NULL);
  1381. CddbProgressDlg_SetStatus(pLookup->hwndInfo, MAKEINTRESOURCEW(IDS_INITIALIZING), -1);
  1382. pSubmitDisc = pDisc;
  1383. pLookup->result = pControl->SubmitDisc(pSubmitDisc, TRUE, &lVal);
  1384. if (SUCCEEDED(pLookup->result))
  1385. {
  1386. pControl->Release();
  1387. return;
  1388. }
  1389. }
  1390. }
  1391. break;
  1392. }
  1393. pControl->Release();
  1394. Cddb_FinishLookup(pLookup, pDisc);
  1395. if (pDisc) pDisc->Release();
  1396. }
  1397. #endif
  1398. static void Cddb_OnGetFullDiscInfoCompleted(HRESULT result, ICddbDisc *pDisc, MEDIALOOKUP *pLookup)
  1399. {
  1400. if (!pLookup) return;
  1401. pLookup->result = result;
  1402. Cddb_FinishLookup(pLookup, pDisc);
  1403. }
  1404. static void Cddb_OnSubmitDiscCompleted(HRESULT result, MEDIALOOKUP *pLookup)
  1405. {
  1406. #ifndef IGNORE_API_GRACENOTE
  1407. if (!pLookup) return;
  1408. pLookup->result = result;
  1409. if (FAILED(result))
  1410. {
  1411. if (pSubmitDisc) pSubmitDisc->Release();
  1412. pSubmitDisc = NULL;
  1413. }
  1414. Cddb_FinishLookup(pLookup, pSubmitDisc);
  1415. if (pSubmitDisc)
  1416. {
  1417. pSubmitDisc->Release();
  1418. pSubmitDisc = NULL;
  1419. }
  1420. #endif
  1421. }
  1422. HWND Cddb_GetInfoWindow(void)
  1423. {
  1424. #ifndef IGNORE_API_GRACENOTE
  1425. return (IS_BUSY(0)) ? g_lookup.hwndInfo : NULL;
  1426. #else
  1427. return NULL;
  1428. #endif
  1429. }
  1430. void Cdbb_DisplayInfo(LPCWSTR pszTitle, LPCWSTR pszCaption, LPCWSTR pszStatus, INT percentCompleted)
  1431. {
  1432. HWND hwndInfo;
  1433. hwndInfo = Cddb_GetInfoWindow();
  1434. if (!hwndInfo) return;
  1435. BOOL CddbProgressDlg_SetStatus(HWND hwnd, LPCWSTR pszStatus, INT nPercentCompleted);
  1436. }
  1437. HRESULT Cddb_DisplayResultDlg(HWND hwndParent, HRESULT result, DWORD dwAutoCloseDelay, UINT flags)
  1438. {
  1439. #ifndef IGNORE_API_GRACENOTE
  1440. HWND hwndInfo;
  1441. wchar_t szReason[256] = {0}, szResult[256] = {0};
  1442. if (CDDB_UI_USE_PARENT & flags) hwndInfo = hwndParent;
  1443. else
  1444. {
  1445. wchar_t szText[256] = {0};
  1446. hwndParent = GetAdaptedParent(hwndParent);
  1447. hwndInfo = CddbProgressDlg_Create(hwndParent, SW_HIDE);
  1448. SetPopUpPos(hwndInfo, CENTER_LEFT);
  1449. SetWindowText(hwndInfo, WASABI_API_LNGSTRINGW_BUF(IDS_LOOKUPRESULT_TITLE, szText, sizeof(szText)/sizeof(wchar_t)));
  1450. CddbProgressDlg_Initialize(hwndInfo, NULL, NULL, NULL);
  1451. CddbProgressDlg_Completed(hwndInfo, NULL, NULL, AUTOCLOSE_NEVER, S_OK);
  1452. }
  1453. if (!hwndInfo || !IsWindow(hwndInfo)) return E_FAIL;
  1454. if (AUTOCLOSE_NOW == dwAutoCloseDelay && GetWindowThreadProcessId(hwndInfo, NULL) != GetCurrentThreadId())
  1455. {
  1456. dwAutoCloseDelay = 1; // DestroyWindow is not working on other thread
  1457. ShowWindowAsync(hwndInfo, SW_HIDE); // in case we in modal loop on other
  1458. }
  1459. if (dwAutoCloseDelay > 10)
  1460. {
  1461. Cddb_GetResultText(result, szResult, sizeof(szResult)/sizeof(wchar_t),
  1462. szReason, sizeof(szReason)/sizeof(wchar_t));
  1463. ShowWindow(hwndInfo, SW_SHOW);
  1464. }
  1465. CddbProgressDlg_Completed(hwndInfo, szResult, szReason, dwAutoCloseDelay, result);
  1466. if (dwAutoCloseDelay > 10 && CDDB_UI_RESULT_MODAL == ((CDDB_UI_MODAL | CDDB_UI_RESULT_MODAL) & flags))
  1467. {
  1468. CddbProgressDlg_DoModal(hwndInfo, NULL);
  1469. }
  1470. #endif
  1471. return S_OK;
  1472. }