1
0

ExtendedInfo.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "main.h"
  2. #include "Metadata.h"
  3. #include "../Winamp/wa_ipc.h"
  4. #include "../nu/ns_wc.h"
  5. #include "uvox_3901.h"
  6. #include "uvox_3902.h"
  7. #include "Stopper.h"
  8. #include <shlwapi.h>
  9. #include "../Agave/Language/api_language.h"
  10. #include <strsafe.h>
  11. extern CGioFile *g_playing_file;
  12. static FILETIME ftLastWriteTime;
  13. // is used to determine if the last write time of the file has changed when
  14. // asked to get the metadata for the same cached file so we can update things
  15. BOOL HasFileTimeChanged(const wchar_t *fn)
  16. {
  17. WIN32_FILE_ATTRIBUTE_DATA fileData = {0};
  18. if (GetFileAttributesExW(fn, GetFileExInfoStandard, &fileData) == TRUE)
  19. {
  20. if(CompareFileTime(&ftLastWriteTime, &fileData.ftLastWriteTime))
  21. {
  22. ftLastWriteTime = fileData.ftLastWriteTime;
  23. return TRUE;
  24. }
  25. }
  26. return FALSE;
  27. }
  28. void UpdateFileTimeChanged(const wchar_t *fn)
  29. {
  30. WIN32_FILE_ATTRIBUTE_DATA fileData;
  31. if (GetFileAttributesExW(fn, GetFileExInfoStandard, &fileData) == TRUE)
  32. {
  33. ftLastWriteTime = fileData.ftLastWriteTime;
  34. }
  35. }
  36. Metadata *m_ext_set_mp3info = NULL;
  37. Metadata *m_ext_get_mp3info = NULL;
  38. extern "C" __declspec(dllexport)
  39. int winampGetExtendedFileInfoW(const wchar_t *fn, const char *data, wchar_t *dest, size_t destlen)
  40. {
  41. if (!_stricmp(data, "type"))
  42. {
  43. dest[0] = '0';
  44. dest[1] = 0;
  45. return 1;
  46. }
  47. if (!_stricmp(data, "rateable"))
  48. {
  49. dest[0] = '1';
  50. dest[1] = 0;
  51. return 1;
  52. }
  53. else if (!_stricmp(data, "family"))
  54. {
  55. if (!fn || !fn[0]) return 0;
  56. int len = lstrlenW(fn);
  57. if (len < 4 || L'.' != fn[len - 4]) return 0;
  58. const wchar_t *p = &fn[len - 3];
  59. if (!_wcsicmp(p, L"MP3")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MP3, dest, destlen); return 1; }
  60. if (!_wcsicmp(p, L"MP2")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MP2, dest, destlen); return 1; }
  61. if (!_wcsicmp(p, L"MP1")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MP1, dest, destlen); return 1; }
  62. if (!_wcsicmp(p, L"AAC")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MPEG2_AAC, dest, destlen); return 1; }
  63. if (!_wcsicmp(p, L"VLB")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_DOLBY, dest, destlen); return 1; }
  64. return 0;
  65. }
  66. else if (!_stricmp(data, "mime"))
  67. {
  68. if (!fn || !fn[0]) return 0;
  69. int len = lstrlenW(fn);
  70. if (len < 4 || L'.' != fn[len - 4]) return 0;
  71. const wchar_t *p = &fn[len - 3];
  72. if (!_wcsicmp(p, L"MP3")) { StringCchCopyW(dest, destlen, L"audio/mpeg"); return 1; }
  73. if (!_wcsicmp(p, L"MP2")) { StringCchCopyW(dest, destlen, L"audio/mpeg"); return 1; }
  74. if (!_wcsicmp(p, L"MP1")) { StringCchCopyW(dest, destlen, L"audio/mpeg"); return 1; }
  75. if (!_wcsicmp(p, L"AAC")) { StringCchCopyW(dest, destlen, L"audio/aac"); return 1; }
  76. if (!_wcsicmp(p, L"VLB")) { StringCchCopyW(dest, destlen, L"audio/vlb"); return 1; }
  77. return 0;
  78. }
  79. else if (!_strnicmp(data, "uvox/", 5))
  80. {
  81. EnterCriticalSection(&streamInfoLock);
  82. if (g_playing_file)
  83. {
  84. if (g_playing_file->uvox_3901) // check again now that we've acquired the lock
  85. {
  86. Ultravox3901 uvox_metadata;
  87. if (uvox_metadata.Parse(g_playing_file->uvox_3901) != API_XML_FAILURE)
  88. {
  89. LeaveCriticalSection(&streamInfoLock);
  90. return uvox_metadata.GetExtendedData(data, dest, (int)destlen);
  91. }
  92. }
  93. else if (g_playing_file->uvox_3902)
  94. {
  95. Ultravox3902 uvox_metadata;
  96. if (uvox_metadata.Parse(g_playing_file->uvox_3902) != API_XML_FAILURE)
  97. {
  98. LeaveCriticalSection(&streamInfoLock);
  99. return uvox_metadata.GetExtendedData(data, dest, (int)destlen);
  100. }
  101. }
  102. }
  103. LeaveCriticalSection(&streamInfoLock);
  104. return 0;
  105. }
  106. else if (_stricmp(data, "0x3901") == 0)
  107. {
  108. EnterCriticalSection(&streamInfoLock);
  109. if (g_playing_file && g_playing_file->uvox_3901) // check again now that we've acquired the lock
  110. {
  111. if (dest == NULL) // It's empty, he's looking for the size of the 0x3901
  112. {
  113. int size = MultiByteToWideChar(CP_UTF8, 0, g_playing_file->uvox_3901, -1, 0, 0);
  114. LeaveCriticalSection(&streamInfoLock);
  115. return size;
  116. }
  117. else
  118. {
  119. MultiByteToWideCharSZ(CP_UTF8, 0, g_playing_file->uvox_3901, -1, dest, (int)destlen);
  120. LeaveCriticalSection(&streamInfoLock);
  121. return 1;
  122. }
  123. }
  124. LeaveCriticalSection(&streamInfoLock);
  125. return 0;
  126. }
  127. else if (!_stricmp(data, "streamtype"))
  128. {
  129. if (lstrcmpW(lastfn, fn))
  130. return 0;
  131. if (g_playing_file)
  132. {
  133. EnterCriticalSection(&streamInfoLock);
  134. if (g_playing_file) // check again now that we've acquired the lock
  135. {
  136. StringCchPrintfW(dest, destlen, L"%d", g_playing_file->IsStream());
  137. LeaveCriticalSection(&streamInfoLock);
  138. return 1;
  139. }
  140. LeaveCriticalSection(&streamInfoLock);
  141. }
  142. return 0;
  143. }
  144. else if (!_stricmp(data, "streammetadata"))
  145. {
  146. if (lstrcmpW(lastfn, fn))
  147. return 0;
  148. if (g_playing_file)
  149. {
  150. uint32_t len=0;
  151. EnterCriticalSection(&streamInfoLock);
  152. if (g_playing_file && g_playing_file->GetID3v2(&len) && len > 0) // check again now that we've acquired the lock
  153. {
  154. lstrcpynW(dest, L"1", (int)destlen);
  155. LeaveCriticalSection(&streamInfoLock);
  156. return 1; // always return 1 to ensure we can do title lookups
  157. }
  158. LeaveCriticalSection(&streamInfoLock);
  159. }
  160. return 0;
  161. }
  162. else if (!_stricmp(data, "streamtitle"))
  163. {
  164. EnterCriticalSection(&streamInfoLock);
  165. if (g_playing_file) // check again now that we've acquired the lock
  166. ConvertTryUTF8(g_playing_file->stream_current_title, dest, destlen);
  167. else
  168. dest[0]=0;
  169. LeaveCriticalSection(&streamInfoLock);
  170. return 1;
  171. }
  172. else if (!_stricmp(data, "streamname"))
  173. {
  174. EnterCriticalSection(&streamInfoLock);
  175. if (g_playing_file) // check again now that we've acquired the lock
  176. ConvertTryUTF8(g_playing_file->stream_name, dest, destlen);
  177. else
  178. dest[0]=0;
  179. LeaveCriticalSection(&streamInfoLock);
  180. return 1;
  181. }
  182. else if (!_stricmp(data, "streamurl"))
  183. {
  184. EnterCriticalSection(&streamInfoLock);
  185. if (g_playing_file) // check again now that we've acquired the lock
  186. ConvertTryUTF8(g_playing_file->stream_url, dest, destlen);
  187. else
  188. dest[0]=0;
  189. LeaveCriticalSection(&streamInfoLock);
  190. return 1;
  191. }
  192. else if (!_stricmp(data, "streamgenre"))
  193. {
  194. EnterCriticalSection(&streamInfoLock);
  195. if (g_playing_file) // check again now that we've acquired the lock
  196. ConvertTryUTF8(g_playing_file->stream_genre, dest, destlen);
  197. else
  198. dest[0]=0;
  199. LeaveCriticalSection(&streamInfoLock);
  200. return 1;
  201. }
  202. else if (!_stricmp(data, "streaminformation"))
  203. {
  204. EnterCriticalSection(&streamInfoLock);
  205. if (g_playing_file)
  206. g_playing_file->GetStreamInfo(dest, destlen);
  207. else
  208. dest[0]=0;
  209. LeaveCriticalSection(&streamInfoLock);
  210. return 1;
  211. }
  212. if (!fn || !fn[0]) return 0;
  213. if (!_wcsnicmp(fn, L"uvox://", 7))
  214. return 0;
  215. if (g_playing_file && PathIsURL(fn) && !lstrcmpW(lastfn, fn))
  216. {
  217. EnterCriticalSection(&streamInfoLock);
  218. uint32_t len = 0;
  219. if (g_playing_file && g_playing_file->GetID3v2(&len) && len > 0) // check again now that we've acquired the lock
  220. {
  221. Metadata meta(g_playing_file, fn);
  222. int ret = meta.GetExtendedData(data, dest, (int)destlen);
  223. LeaveCriticalSection(&streamInfoLock);
  224. return ret;
  225. }
  226. LeaveCriticalSection(&streamInfoLock);
  227. }
  228. if (PathIsURL(fn))
  229. return 0;
  230. if (m_ext_get_mp3info && (!m_ext_get_mp3info->IsMe(fn) || HasFileTimeChanged(fn)))
  231. {
  232. m_ext_get_mp3info->Release();
  233. m_ext_get_mp3info=0;
  234. }
  235. if (!m_ext_get_mp3info)
  236. {
  237. m_ext_get_mp3info = new Metadata;
  238. m_ext_get_mp3info->Open(fn);
  239. }
  240. return m_ext_get_mp3info->GetExtendedData(data, dest, (int)destlen);
  241. }
  242. extern "C"
  243. __declspec(dllexport) int winampSetExtendedFileInfoW(const wchar_t *fn, const char *data, const wchar_t *val)
  244. {
  245. if (!m_ext_set_mp3info || (m_ext_set_mp3info && !m_ext_set_mp3info->IsMe(fn)))
  246. {
  247. if(m_ext_set_mp3info) m_ext_set_mp3info->Release();
  248. m_ext_set_mp3info = new Metadata;
  249. m_ext_set_mp3info->Open(fn);
  250. }
  251. return m_ext_set_mp3info->SetExtendedData(data, val);
  252. }
  253. extern "C"
  254. __declspec(dllexport) int winampWriteExtendedFileInfo()
  255. {
  256. // flush our read cache too :)
  257. if(m_ext_get_mp3info) m_ext_get_mp3info->Release();
  258. m_ext_get_mp3info = NULL;
  259. if (!m_ext_set_mp3info) return 0;
  260. Stopper stopper;
  261. if (m_ext_set_mp3info->IsMe(lastfn))
  262. stopper.Stop();
  263. // just in-case something changed
  264. if (!m_ext_set_mp3info) return 0;
  265. int ret = m_ext_set_mp3info->Save();
  266. stopper.Play();
  267. m_ext_set_mp3info->Release();
  268. m_ext_set_mp3info = NULL;
  269. // update last modified so we're not re-queried on our own updates
  270. UpdateFileTimeChanged(lastfn);
  271. return !ret;
  272. }
  273. extern "C" __declspec(dllexport)
  274. int winampClearExtendedFileInfoW(const wchar_t *fn)
  275. {
  276. Metadata meta;
  277. if (meta.Open(fn)==METADATA_SUCCESS)
  278. {
  279. meta.id3v2.Clear();
  280. Stopper stopper;
  281. if (meta.IsMe(lastfn))
  282. stopper.Stop();
  283. meta.Save();
  284. stopper.Play();
  285. // update last modified so we're not re-queried on our own updates
  286. UpdateFileTimeChanged(fn);
  287. return 1;
  288. }
  289. return 0;
  290. }