1
0

add.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include "main.h"
  2. #include "ml_local.h"
  3. #include "api_mldb.h"
  4. #include <commctrl.h>
  5. #include "resource.h"
  6. #include "../replicant/nu/ns_wc.h"
  7. #include "../nde/nde.h"
  8. #include "../Agave/Language/api_language.h"
  9. #include "..\..\General\gen_ml/config.h"
  10. #include "..\..\General\gen_ml/gaystring.h"
  11. #include "time.h"
  12. #include "../winamp/in2.h"
  13. #include "../Winamp/strutil.h"
  14. #include <shlwapi.h>
  15. #include <strsafe.h>
  16. bool skipTitleInfo=false;
  17. static int getFileInfoW(const wchar_t *filename, const wchar_t *metadata, wchar_t *dest, size_t len)
  18. {
  19. dest[0]=0;
  20. return AGAVE_API_METADATA->GetExtendedFileInfo(filename, metadata, dest, len);
  21. }
  22. static time_t FileTimeToUnixTime(FILETIME *ft)
  23. {
  24. ULARGE_INTEGER end;
  25. memcpy(&end,ft,sizeof(end));
  26. end.QuadPart -= 116444736000000000;
  27. end.QuadPart /= 10000000; // 100ns -> seconds
  28. return (time_t)end.QuadPart;
  29. }
  30. void makeFilename2W(const wchar_t *filename, wchar_t *filename2, int filename2_len)
  31. {
  32. filename2[0]=0;
  33. GetLongPathNameW(filename, filename2, filename2_len);
  34. if (!_wcsicmp(filename,filename2)) filename2[0]=0;
  35. }
  36. void makeFilename2(const char *filename, char *filename2, int filename2_len)
  37. {
  38. filename2[0]=0;
  39. GetLongPathNameA(filename, filename2, filename2_len);
  40. if (!stricmp(filename,filename2)) filename2[0]=0;
  41. }
  42. static __int64 FileSize64(HANDLE file)
  43. {
  44. LARGE_INTEGER position;
  45. position.QuadPart=0;
  46. position.LowPart = GetFileSize(file, (LPDWORD)&position.HighPart);
  47. if (position.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
  48. return INVALID_FILE_SIZE;
  49. else
  50. return position.QuadPart;
  51. }
  52. static void GetFileSizeAndTime(const wchar_t *filename, __int64 *file_size, time_t *file_time)
  53. {
  54. WIN32_FILE_ATTRIBUTE_DATA file_data;
  55. if (GetFileAttributesExW(filename, GetFileExInfoStandard, &file_data) == FALSE)
  56. {
  57. // GetFileAttributesEx failed. that sucks, let's try something else
  58. HANDLE hFile=CreateFileW(filename,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
  59. if (hFile != INVALID_HANDLE_VALUE)
  60. {
  61. FILETIME lt = {0};
  62. if (GetFileTime(hFile,NULL,NULL,&lt))
  63. {
  64. *file_time=FileTimeToUnixTime(&lt);
  65. }
  66. *file_size=FileSize64(hFile);
  67. CloseHandle(hFile);
  68. }
  69. }
  70. else
  71. {
  72. // success
  73. *file_time = FileTimeToUnixTime(&file_data.ftLastWriteTime);
  74. LARGE_INTEGER size64;
  75. size64.LowPart = file_data.nFileSizeLow;
  76. size64.HighPart = file_data.nFileSizeHigh;
  77. *file_size = size64.QuadPart;
  78. }
  79. }
  80. int FindFileInDatabase(nde_scanner_t s, int fieldId, const wchar_t *filename, wchar_t alternate[MAX_PATH])
  81. {
  82. alternate[0]=0;
  83. makeFilename2W(filename,alternate,MAX_PATH);
  84. if (alternate[0])
  85. {
  86. if (NDE_Scanner_LocateFilename(s, fieldId, FIRST_RECORD, alternate))
  87. {
  88. return 2;
  89. }
  90. }
  91. if (NDE_Scanner_LocateFilename(s, fieldId, FIRST_RECORD, filename))
  92. return 1;
  93. return 0;
  94. }
  95. static inline void GetOptionalField(nde_scanner_t s, const wchar_t *filename, const wchar_t *field, unsigned char field_id)
  96. {
  97. wchar_t tmp[1024]={0};
  98. if (getFileInfoW(filename,field,tmp,sizeof(tmp)/sizeof(wchar_t)))
  99. {
  100. if(tmp[0])
  101. {
  102. tmp[1023]=0; // just in case
  103. db_setFieldStringW(s, field_id,tmp);
  104. }
  105. else
  106. {
  107. db_removeField(s, field_id);
  108. }
  109. }
  110. }
  111. static inline void GetOptionalFieldInt(nde_scanner_t s, const wchar_t *filename, const wchar_t *field, unsigned char field_id)
  112. {
  113. wchar_t tmp[128]={0};
  114. if (getFileInfoW(filename,field,tmp,sizeof(tmp)/sizeof(wchar_t)))
  115. {
  116. if(tmp[0])
  117. {
  118. tmp[127]=0; // just in case
  119. db_setFieldInt(s,field_id,_wtoi(tmp));
  120. }
  121. else
  122. {
  123. db_removeField(s, field_id);
  124. }
  125. }
  126. }
  127. static inline void GetNonBlankFieldInt(nde_scanner_t s, const wchar_t *filename, const wchar_t *field, unsigned char field_id)
  128. {
  129. wchar_t tmp[128]={0};
  130. if (getFileInfoW(filename,field,tmp,sizeof(tmp)/sizeof(wchar_t)))
  131. {
  132. if(tmp[0])
  133. {
  134. tmp[127]=0; // just in case
  135. db_setFieldInt(s,field_id,_wtoi(tmp));
  136. }
  137. }
  138. }
  139. // return values
  140. // 0 - error
  141. // 1 - success
  142. // -2 - record not found (in update only mode)
  143. int addFileToDb(const wchar_t *filename, int onlyupdate, int use_metadata, int guess_mode, int playcnt, int lastplay, bool force)
  144. {
  145. if (!_wcsicmp(PathFindExtensionW(filename), L".cda"))
  146. return 0;
  147. __int64 file_size=INVALID_FILE_SIZE;
  148. time_t file_time=0;
  149. GetFileSizeAndTime(filename, &file_size, &file_time);
  150. if (file_size == INVALID_FILE_SIZE || file_size == 0)
  151. return 0;
  152. openDb(); // just in case it's not opened yet (this function will return immediately if it's already open)
  153. EnterCriticalSection(&g_db_cs);
  154. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  155. wchar_t filename2[MAX_PATH] = {0}; // full lfn path if set
  156. int found = FindFileInDatabase(s, MAINTABLE_ID_FILENAME, filename, filename2);
  157. if (found) // For updating
  158. {
  159. // if an update wasn't forced, see if the file's timestamp or filesize have changed
  160. if (!force && NDE_Scanner_GetFieldByID(s, MAINTABLE_ID_FILESIZE))
  161. {
  162. nde_field_t f=NDE_Scanner_GetFieldByID(s, MAINTABLE_ID_FILETIME);
  163. if (f && file_time <= NDE_IntegerField_GetValue(f))
  164. {
  165. f=NDE_Scanner_GetFieldByID(s, MAINTABLE_ID_LASTUPDTIME);
  166. if (f && file_time <= NDE_IntegerField_GetValue(f))
  167. {
  168. NDE_Table_DestroyScanner(g_table, s);
  169. LeaveCriticalSection(&g_db_cs);
  170. return 1;
  171. }
  172. }
  173. }
  174. NDE_Scanner_Edit(s);
  175. if (found == 1 && filename2[0]) db_setFieldStringW(s,MAINTABLE_ID_FILENAME,filename2); // if we have a better filename, update it
  176. nde_field_t f=NDE_Scanner_GetFieldByID(s, MAINTABLE_ID_PLAYCOUNT);
  177. int cnt = f?NDE_IntegerField_GetValue(f):0;
  178. if (!cnt)
  179. db_setFieldInt(s,MAINTABLE_ID_PLAYCOUNT,0);
  180. }
  181. else // Adding an entry from scratch
  182. {
  183. if (onlyupdate)
  184. {
  185. NDE_Table_DestroyScanner(g_table, s);
  186. LeaveCriticalSection(&g_db_cs);
  187. // Issue a wasabi system callback after we have successfully updated a file in the ml database
  188. WASABI_API_SYSCB->syscb_issueCallback(api_mldb::SYSCALLBACK, api_mldb::MLDB_FILE_UPDATED_EXTERNAL, (size_t)filename, 0);
  189. return -2;
  190. }
  191. // new file
  192. NDE_Scanner_New(s);
  193. db_setFieldStringW(s,MAINTABLE_ID_FILENAME,filename2[0]?filename2:filename);
  194. db_setFieldInt(s,MAINTABLE_ID_PLAYCOUNT,playcnt);
  195. if (lastplay)
  196. db_setFieldInt(s,MAINTABLE_ID_LASTPLAY,lastplay);
  197. db_setFieldInt(s,MAINTABLE_ID_DATEADDED, (int)time(NULL));
  198. }
  199. int hasttitle=0, hastartist=0, hastalbum=0, hasttrack=0;
  200. int hastyear=0, hastlength=0;
  201. int hasdisc=0, hasalbumartist=0;
  202. int hasttype=0, hasdiscs=0, hastracks=0, hasbitrate=0;
  203. int the_length_sec=0;
  204. wchar_t m_artist[1024] = {0}, tmp[1024] = {0};
  205. if(getFileInfoW(filename, DB_FIELDNAME_type, tmp, sizeof(tmp) / sizeof(wchar_t)) )
  206. {
  207. if(tmp[0]) { int type=_wtoi(tmp); db_setFieldInt(s,MAINTABLE_ID_TYPE,type); hasttype++; }
  208. }
  209. if (getFileInfoW(filename, DB_FIELDNAME_length,tmp,sizeof(tmp)/sizeof(wchar_t)))
  210. {
  211. if(tmp[0]) { db_setFieldInt(s,MAINTABLE_ID_LENGTH,the_length_sec=(_wtoi(tmp)/1000)); hastlength++; }
  212. }
  213. if (getFileInfoW(filename, DB_FIELDNAME_bitrate,tmp,sizeof(tmp)/sizeof(wchar_t)))
  214. {
  215. if(tmp[0]) { db_setFieldInt(s,MAINTABLE_ID_BITRATE,_wtoi(tmp)); hasbitrate++; }
  216. }
  217. if(use_metadata && getFileInfoW(filename, DB_FIELDNAME_title,tmp,sizeof(tmp)/sizeof(wchar_t)))
  218. {
  219. if(tmp[0])
  220. {
  221. db_setFieldStringW(s,MAINTABLE_ID_TITLE,tmp);
  222. hasttitle++;
  223. }
  224. getFileInfoW( filename, DB_FIELDNAME_artist, tmp, sizeof( tmp ) / sizeof( wchar_t ) );
  225. if(tmp[0])
  226. {
  227. StringCchCopyW(m_artist, 1024, tmp);
  228. db_setFieldStringW(s,MAINTABLE_ID_ARTIST,tmp);
  229. hastartist++;
  230. }
  231. getFileInfoW(filename, DB_FIELDNAME_album,tmp,sizeof(tmp)/sizeof(wchar_t));
  232. if(tmp[0])
  233. {
  234. db_setFieldStringW(s,MAINTABLE_ID_ALBUM,tmp);
  235. hastalbum++;
  236. }
  237. GetOptionalField(s, filename, DB_FIELDNAME_comment, MAINTABLE_ID_COMMENT);
  238. getFileInfoW(filename, DB_FIELDNAME_year,tmp,sizeof(tmp)/sizeof(wchar_t));
  239. if(tmp[0] && !wcsstr(tmp,L"__") && !wcsstr(tmp,L"/") && !wcsstr(tmp,L"\\") && !wcsstr(tmp,L".")) {
  240. wchar_t *p=tmp;
  241. while (p && *p)
  242. {
  243. if (*p == L'_') *p=L'0';
  244. p++;
  245. }
  246. int y=_wtoi(tmp);
  247. if(y!=0) { db_setFieldInt(s,MAINTABLE_ID_YEAR,_wtoi(tmp)); hastyear++; }
  248. }
  249. GetOptionalField(s, filename, DB_FIELDNAME_genre, MAINTABLE_ID_GENRE);
  250. getFileInfoW(filename, DB_FIELDNAME_track,tmp,sizeof(tmp)/sizeof(wchar_t));
  251. if(tmp[0])
  252. {
  253. int track, tracks;
  254. ParseIntSlashInt(tmp, &track, &tracks);
  255. if (track > 0)
  256. {
  257. db_setFieldInt(s,MAINTABLE_ID_TRACKNB,track);
  258. hasttrack++;
  259. }
  260. if (tracks > 0)
  261. {
  262. db_setFieldInt(s,MAINTABLE_ID_TRACKS,tracks);
  263. hastracks++;
  264. }
  265. }
  266. getFileInfoW(filename, DB_FIELDNAME_disc,tmp,sizeof(tmp)/sizeof(wchar_t));
  267. if(tmp[0])
  268. {
  269. int disc, discs;
  270. ParseIntSlashInt(tmp, &disc, &discs);
  271. if (disc > 0)
  272. {
  273. db_setFieldInt(s,MAINTABLE_ID_DISC,disc);
  274. hasdisc++;
  275. }
  276. if (discs > 0)
  277. {
  278. db_setFieldInt(s,MAINTABLE_ID_DISCS,discs);
  279. hasdiscs++;
  280. }
  281. }
  282. getFileInfoW(filename, DB_FIELDNAME_albumartist,tmp,sizeof(tmp)/sizeof(wchar_t));
  283. if(tmp[0]) { db_setFieldStringW(s,MAINTABLE_ID_ALBUMARTIST,tmp); hasalbumartist++; }
  284. GetOptionalField(s, filename, DB_FIELDNAME_publisher, MAINTABLE_ID_PUBLISHER);
  285. GetOptionalField(s, filename, DB_FIELDNAME_composer, MAINTABLE_ID_COMPOSER);
  286. GetOptionalField(s, filename, DB_FIELDNAME_replaygain_album_gain, MAINTABLE_ID_ALBUMGAIN);
  287. GetOptionalField(s, filename, DB_FIELDNAME_replaygain_track_gain, MAINTABLE_ID_TRACKGAIN);
  288. GetOptionalFieldInt(s, filename, DB_FIELDNAME_bpm, MAINTABLE_ID_BPM);
  289. GetOptionalField(s, filename, DB_FIELDNAME_GracenoteFileID, MAINTABLE_ID_GRACENOTEFILEID);
  290. GetOptionalField(s, filename, DB_FIELDNAME_GracenoteExtData, MAINTABLE_ID_GRACENOTEEXTDATA);
  291. GetOptionalFieldInt(s, filename, DB_FIELDNAME_lossless, MAINTABLE_ID_LOSSLESS);
  292. GetOptionalField(s, filename, DB_FIELDNAME_category, MAINTABLE_ID_CATEGORY);
  293. GetOptionalField(s, filename, DB_FIELDNAME_codec, MAINTABLE_ID_CODEC);
  294. GetOptionalField(s, filename, DB_FIELDNAME_director, MAINTABLE_ID_DIRECTOR);
  295. GetOptionalField(s, filename, DB_FIELDNAME_producer, MAINTABLE_ID_PRODUCER);
  296. GetOptionalFieldInt(s, filename, DB_FIELDNAME_width, MAINTABLE_ID_WIDTH);
  297. GetOptionalFieldInt(s, filename, DB_FIELDNAME_height, MAINTABLE_ID_HEIGHT);
  298. if (g_config->ReadInt(L"writeratings", 0))
  299. GetOptionalFieldInt(s, filename, DB_FIELDNAME_rating, MAINTABLE_ID_RATING);
  300. else
  301. GetNonBlankFieldInt(s, filename, DB_FIELDNAME_rating, MAINTABLE_ID_RATING);
  302. GetOptionalField(s, filename, L"mime", MAINTABLE_ID_MIMETYPE);
  303. }
  304. int guessmode = guess_mode;
  305. if (guessmode != 2 && ((!hasttitle) + (!hastartist) + (!hastalbum) + (!hasttrack) >= (g_guessifany ? 1 : 4)))
  306. {
  307. int tn = 0;
  308. wchar_t *artist = 0, *album = 0, *title = 0, *guessbuf = 0;
  309. if (guessmode==1)
  310. {
  311. guessbuf = _wcsdup(filename2[0] ? filename2 : filename);
  312. wchar_t *p=scanstr_backW(guessbuf, L"\\/.", guessbuf);
  313. if (*p == '.')
  314. {
  315. *p = 0;
  316. p = scanstr_backW(guessbuf, L"\\/", guessbuf);
  317. }
  318. if (p > guessbuf)
  319. {
  320. *p = 0;
  321. title = p+1;
  322. p=scanstr_backW(guessbuf, L"\\/", guessbuf);
  323. if (p > guessbuf)
  324. {
  325. *p = 0;
  326. album = p+1;
  327. p=scanstr_backW(guessbuf,L"\\/", guessbuf);
  328. if (p > guessbuf)
  329. {
  330. *p = 0;
  331. artist = p+1;
  332. }
  333. }
  334. }
  335. }
  336. else
  337. guessbuf = guessTitles(filename2[0] ? filename2 : filename, &tn, &artist, &album, &title);
  338. if (guessbuf)
  339. {
  340. if (!hasttitle && title) { hasttitle++; db_setFieldStringW(s,MAINTABLE_ID_TITLE,title); }
  341. if (!hastartist && artist) { hastartist++; db_setFieldStringW(s,MAINTABLE_ID_ARTIST,artist); StringCbCopyW(m_artist, sizeof(m_artist), artist); }
  342. if (!hastalbum && album) { hastalbum++; db_setFieldStringW(s,MAINTABLE_ID_ALBUM,album); }
  343. if (!hasttrack && tn) { hasttrack++; db_setFieldInt(s,MAINTABLE_ID_TRACKNB,tn); }
  344. free(guessbuf);
  345. }
  346. }
  347. if (!hastlength || !hasttitle)
  348. {
  349. // try to query length and title using older GetFileInfo input plugin API
  350. wchar_t ft[1024] = {0};
  351. basicFileInfoStructW bi={0};
  352. bi.filename=filename2[0]?filename2:filename;
  353. bi.length=-1;
  354. bi.title=ft;
  355. bi.titlelen=1024;
  356. skipTitleInfo=true;
  357. LeaveCriticalSection(&g_db_cs); // benski> not actually sure if this is safe, but it prevents a deadlock
  358. SendMessage(plugin.hwndWinampParent,WM_WA_IPC,(WPARAM)&bi,IPC_GET_BASIC_FILE_INFOW);
  359. EnterCriticalSection(&g_db_cs);
  360. skipTitleInfo=false;
  361. if (!hastlength && bi.length >= 0)
  362. {
  363. hastlength=1;
  364. db_setFieldInt(s,MAINTABLE_ID_LENGTH,the_length_sec=bi.length);
  365. }
  366. if (!hasttitle && ft[0])
  367. {
  368. hasttitle=1;
  369. db_setFieldStringW(s,MAINTABLE_ID_TITLE,ft);
  370. }
  371. }
  372. // set up default (empty) strings/values
  373. if (!hasttitle) // title=filename
  374. {
  375. wchar_t *p = PathFindFileNameW(filename), *dup = _wcsdup(p);
  376. if (dup)
  377. {
  378. PathRemoveExtensionW(dup);
  379. db_setFieldStringW(s,MAINTABLE_ID_TITLE,dup);
  380. free(dup);
  381. }
  382. }
  383. if (!hastartist) db_removeField(s,MAINTABLE_ID_ARTIST);
  384. if (!hastalbum) db_removeField(s,MAINTABLE_ID_ALBUM);
  385. if (!hasttrack) db_removeField(s,MAINTABLE_ID_TRACKNB);
  386. if (!hastracks) db_removeField(s,MAINTABLE_ID_TRACKS);
  387. if (!hastyear) db_removeField(s,MAINTABLE_ID_YEAR);
  388. if (!hastlength) db_removeField(s,MAINTABLE_ID_LENGTH);
  389. if (!hasttype) db_setFieldInt(s,MAINTABLE_ID_TYPE,0); //audio
  390. if (!hasdisc) db_removeField(s, MAINTABLE_ID_DISC);
  391. if (!hasdiscs) db_removeField(s, MAINTABLE_ID_DISCS);
  392. if (!hasalbumartist)
  393. {
  394. if (hastartist && g_config->ReadInt(L"artist_as_albumartist", 1))
  395. db_setFieldStringW(s, MAINTABLE_ID_ALBUMARTIST, m_artist);
  396. else
  397. db_removeField(s, MAINTABLE_ID_ALBUMARTIST);
  398. }
  399. if (file_size != INVALID_FILE_SIZE)
  400. {
  401. db_setFieldInt64(s,MAINTABLE_ID_FILESIZE, file_size);
  402. }
  403. else db_removeField(s,MAINTABLE_ID_FILESIZE);
  404. db_setFieldInt(s,MAINTABLE_ID_LASTUPDTIME, (int)time(NULL));
  405. db_setFieldInt(s,MAINTABLE_ID_FILETIME, (int)file_time);
  406. if (!hasbitrate && the_length_sec)
  407. {
  408. __int64 br =(file_size*8LL) / (__int64)the_length_sec;
  409. br /= 1000;
  410. db_setFieldInt(s,MAINTABLE_ID_BITRATE,(int)br);
  411. }
  412. NDE_Scanner_Post(s);
  413. g_table_dirty++;
  414. NDE_Table_DestroyScanner(g_table, s);
  415. LeaveCriticalSection(&g_db_cs);
  416. if (found)
  417. {
  418. // Issue a wasabi system callback after we have successfully updated a file in the ml database
  419. WASABI_API_SYSCB->syscb_issueCallback(api_mldb::SYSCALLBACK, api_mldb::MLDB_FILE_UPDATED, (size_t)filename, 0);
  420. }
  421. else
  422. {
  423. // Issue a wasabi system callback after we have successfully added a file in the ml database
  424. WASABI_API_SYSCB->syscb_issueCallback(api_mldb::SYSCALLBACK, api_mldb::MLDB_FILE_ADDED, (size_t)filename, 0);
  425. }
  426. return 1;
  427. }