importer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "../plist/types.h"
  2. #include "importer.h"
  3. #include "../../General/gen_ml/ml.h"
  4. #include <bfc/string/stringdict.h>
  5. #include <bfc/string/url.h>
  6. BEGIN_STRINGDICTIONARY(_itunesprops)
  7. SDI(L"Track ID", IT_TRACKID);
  8. SDI(L"Name", IT_NAME);
  9. SDI(L"Artist", IT_ARTIST);
  10. SDI(L"Album Artist", IT_ALBUMARTIST);
  11. SDI(L"Album", IT_ALBUM);
  12. SDI(L"Genre", IT_GENRE);
  13. SDI(L"Comments", IT_COMMENTS);
  14. SDI(L"Kind", IT_KIND);
  15. SDI(L"Size", IT_SIZE);
  16. SDI(L"Total Time", IT_TOTALTIME);
  17. SDI(L"Track Number", IT_TRACKNUM);
  18. SDI(L"Track Count", IT_TRACKCOUNT);
  19. SDI(L"Year", IT_YEAR);
  20. SDI(L"Date Modified", IT_DATEMODIFIED);
  21. SDI(L"Date Added", IT_DATEADDED);
  22. SDI(L"Bit Rate", IT_BITRATE);
  23. SDI(L"Bitrate", IT_BITRATE);
  24. SDI(L"Sample Rate", IT_SAMPLERATE);
  25. SDI(L"Rating", IT_RATING);
  26. SDI(L"Location", IT_LOCATION);
  27. SDI(L"File Folder Count", IT_FOLDERCOUNT);
  28. SDI(L"Library Folder Count", IT_LIBFOLDERCOUNT);
  29. SDI(L"Play Count", IT_PLAYCOUNT);
  30. SDI(L"Play Date", IT_PLAYDATE);
  31. SDI(L"Play Date UTC", IT_PLAYDATE_UTC);
  32. SDI(L"Composer", IT_COMPOSER);
  33. SDI(L"Publisher", IT_PUBLISHER);
  34. SDI(L"Disc Number", IT_DISCNUMBER);
  35. SDI(L"Disc Count", IT_DISCCOUNT);
  36. SDI(L"BPM", IT_BPM);
  37. SDI(L"Has Video", IT_HAS_VIDEO);
  38. SDI(L"Grouping", IT_GROUPING);
  39. SDI(L"Producer", IT_PRODUCER);
  40. SDI(L"Director", IT_DIRECTOR);
  41. SDI(L"Artwork Count", IT_ARTWORK_COUNT);
  42. SDI(L"Persistent ID", IT_PERSISTENT_ID);
  43. SDI(L"Track Type", IT_TRACK_TYPE);
  44. SDI(L"HD", IT_HD);
  45. SDI(L"Video Width", IT_VIDEO_WIDTH);
  46. SDI(L"Video Height", IT_VIDEO_HEIGHT);
  47. SDI(L"Movie", IT_MOVIE);
  48. SDI(L"Release Date", IT_RELEASE_DATE);
  49. SDI(L"Normalization", IT_NORMALIZATION);
  50. SDI(L"Sort Name", IT_SORTNAME);
  51. SDI(L"Purchased", IT_PURCHASED);
  52. SDI(L"iTunesU", IT_ITUNESU);
  53. SDI(L"Skip Count", IT_SKIPCOUNT);
  54. SDI(L"Skip Date", IT_SKIPDATE);
  55. SDI(L"Sort Album", IT_SORTALBUM);
  56. SDI(L"Sort Composer", IT_SORTCOMPOSER);
  57. SDI(L"Part Of Gapless Album", IT_PART_OF_GAPLESS_ALBUM);
  58. SDI(L"Compilation", IT_COMPILATION);
  59. SDI(L"Sort Album Artist", IT_SORT_ALBUM_ARTIST);
  60. SDI(L"Sort Artist", IT_SORT_ARTIST);
  61. END_STRINGDICTIONARY(_itunesprops, itunesprops)
  62. void FixPath(const wchar_t *strdata, StringW &f)
  63. {
  64. f = strdata;
  65. // if the file starts with the local filename header, strip it
  66. if (!_wcsnicmp(f, ITUNES_FILENAME_HEADER, wcslen(ITUNES_FILENAME_HEADER))) {
  67. if (f[wcslen(ITUNES_FILENAME_HEADER)] == '/')
  68. f = StringW(f.getValue()+wcslen(ITUNES_FILENAME_HEADER)-1);
  69. else
  70. f = StringW(f.getValue()+wcslen(ITUNES_FILENAME_HEADER));
  71. // and then convert the slashes to backslashes
  72. wchar_t *p = f.getNonConstVal();
  73. while (p && *p) { if (*p == '/') *p = '\\'; p++; }
  74. }
  75. // oddly enough, iTunes XML library filenames have a trailing slash, go figure... and strip it!
  76. if (f.lastChar() == '\\') f.trunc((int)f.len()-1);
  77. else if (f.lastChar() == '/') f.trunc((int)f.len()-1); // if this is a url, there was no / to \ conversion
  78. // decode %XX
  79. Url::decode(f);
  80. }
  81. static void Importer_AddKeyToItemRecord(int t, const plistString *data, itemRecordW &ir)
  82. {
  83. const wchar_t *strdata = data->getString();
  84. // load this property into the appropriate gen_ml field
  85. switch (t)
  86. {
  87. case IT_TRACKID:
  88. // ignored
  89. break;
  90. case IT_NAME:
  91. ir.title = _wcsdup(strdata);
  92. break;
  93. case IT_ARTIST:
  94. ir.artist = _wcsdup(strdata);
  95. break;
  96. case IT_ALBUMARTIST:
  97. ir.albumartist = _wcsdup(strdata);
  98. break;
  99. case IT_ALBUM:
  100. ir.album = _wcsdup(strdata);
  101. break;
  102. case IT_GENRE:
  103. ir.genre = _wcsdup(strdata);
  104. break;
  105. case IT_COMMENTS:
  106. ir.comment = _wcsdup(strdata);
  107. break;
  108. case IT_KIND:
  109. // ignored
  110. break;
  111. case IT_LOCATION:
  112. {
  113. StringW f;
  114. FixPath(strdata, f);
  115. // done
  116. ir.filename = _wcsdup(f);
  117. break;
  118. }
  119. case IT_COMPOSER:
  120. ir.composer = _wcsdup(strdata);
  121. break;
  122. case IT_PUBLISHER:
  123. ir.publisher = _wcsdup(strdata);
  124. break;
  125. case IT_GROUPING:
  126. setRecordExtendedItem(&ir, L"category", strdata);
  127. break;
  128. case IT_PRODUCER:
  129. setRecordExtendedItem(&ir, L"producer", strdata);
  130. break;
  131. case IT_DIRECTOR:
  132. setRecordExtendedItem(&ir, L"director", strdata);
  133. break;
  134. case IT_PERSISTENT_ID:
  135. break;
  136. case IT_TRACK_TYPE:
  137. break;
  138. case IT_SORTNAME:
  139. break;
  140. case IT_SORTALBUM:
  141. break;
  142. case IT_SORTCOMPOSER:
  143. break;
  144. case IT_SORT_ALBUM_ARTIST:
  145. break;
  146. case IT_SORT_ARTIST:
  147. break;
  148. default:
  149. //DebugStringW(L"Unknown property: %s\n", prop->getName());
  150. break;
  151. }
  152. }
  153. static void Importer_AddKeyToItemRecord(int t, const plistInteger *data, itemRecordW &ir)
  154. {
  155. int64_t value = data->getValue();
  156. /* benski> we need to keep the ones that were changed to plistBoolean,
  157. because old exported libraries will still be written with integers */
  158. // load this property into the appropriate gen_ml field
  159. switch (t)
  160. {
  161. case IT_TRACKID:
  162. // ignore
  163. break;
  164. case IT_SIZE:
  165. ir.filesize = (int)(value >> 10);
  166. setRecordExtendedItem(&ir, L"realsize", data->getString());
  167. break;
  168. case IT_TOTALTIME:
  169. if (value)
  170. ir.length = (int)(value / 1000);
  171. break;
  172. case IT_TRACKNUM:
  173. ir.track = (int)value;
  174. break;
  175. case IT_TRACKCOUNT:
  176. if (value)
  177. ir.tracks = (int)value;
  178. break;
  179. case IT_YEAR:
  180. if (value)
  181. ir.year = (int)value;
  182. break;
  183. case IT_BITRATE:
  184. ir.bitrate = (int)value;
  185. break;
  186. case IT_SAMPLERATE:
  187. // ignored
  188. break;
  189. case IT_RATING:
  190. ir.rating = (int)(((double)value / 100.0) * 5.0);
  191. break;
  192. case IT_FOLDERCOUNT:
  193. // ignored
  194. break;
  195. case IT_LIBFOLDERCOUNT:
  196. // ignored
  197. break;
  198. case IT_PLAYCOUNT:
  199. if (value > 0)
  200. ir.playcount = (int)value;
  201. break;
  202. case IT_PLAYDATE:
  203. if (value)
  204. ir.lastplay = value;
  205. break;
  206. case IT_DISCNUMBER:
  207. if (value)
  208. ir.disc = (int)value;
  209. break;
  210. case IT_DISCCOUNT:
  211. if (value)
  212. ir.discs = (int)value;
  213. break;
  214. case IT_BPM:
  215. if (value)
  216. ir.bpm = (int)value;
  217. break;
  218. case IT_HAS_VIDEO:
  219. if (value == 1)
  220. ir.type = 1;
  221. break;
  222. case IT_ARTWORK_COUNT:
  223. break;
  224. case IT_VIDEO_WIDTH:
  225. setRecordExtendedItem(&ir, L"width", data->getString());
  226. break;
  227. case IT_VIDEO_HEIGHT:
  228. setRecordExtendedItem(&ir, L"height", data->getString());
  229. break;
  230. case IT_NORMALIZATION:
  231. // TODO: can we convert this to replay gain?
  232. break;
  233. case IT_SKIPCOUNT:
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. static void Importer_AddKeyToItemRecord(int t, const plistBoolean *data, itemRecordW &ir)
  240. {
  241. int value = !!data->getValue();
  242. // load this property into the appropriate gen_ml field
  243. switch (t)
  244. {
  245. case IT_HAS_VIDEO:
  246. ir.type = value;
  247. break;
  248. case IT_HD:
  249. break;
  250. case IT_MOVIE:
  251. break;
  252. case IT_PURCHASED:
  253. break;
  254. case IT_ITUNESU:
  255. break;
  256. case IT_PART_OF_GAPLESS_ALBUM:
  257. break;
  258. case IT_COMPILATION:
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. static void Importer_AddKeyToItemRecord(int t, const plistDate *data, itemRecordW &ir)
  265. {
  266. time_t date_value= data->getDate();
  267. // load this property into the appropriate gen_ml field
  268. switch (t)
  269. {
  270. case IT_DATEMODIFIED:
  271. if (date_value != -1)
  272. ir.filetime = date_value;
  273. break;
  274. case IT_DATEADDED:
  275. if (date_value != -1)
  276. ir.lastupd = date_value;
  277. break;
  278. case IT_PLAYDATE_UTC:
  279. if (date_value != -1)
  280. ir.lastplay = date_value;
  281. break;
  282. case IT_RELEASE_DATE:
  283. break;
  284. case IT_SKIPDATE:
  285. break;
  286. default:
  287. break;
  288. }
  289. }
  290. void Importer_AddKeyToItemRecord(const plistKey *prop, itemRecordW &ir)
  291. {
  292. const plistData *data = prop->getData();
  293. if (data)
  294. {
  295. int t = itunesprops.getId(prop->getName());
  296. switch(data->getType())
  297. {
  298. case PLISTDATA_STRING:
  299. Importer_AddKeyToItemRecord(t, (const plistString *)data, ir);
  300. break;
  301. case PLISTDATA_INTEGER:
  302. Importer_AddKeyToItemRecord(t, (const plistInteger *)data, ir);
  303. break;
  304. case PLISTDATA_DATE:
  305. Importer_AddKeyToItemRecord(t, (const plistDate *)data, ir);
  306. break;
  307. case PLISTDATA_BOOLEAN:
  308. Importer_AddKeyToItemRecord(t, (const plistBoolean *)data, ir);
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. }