You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. ITunesLibraryParser::ITunesLibraryParser (const File& iTunesLibraryFileToUse, const ValueTree& elementToFill,
  25. const CriticalSection& lockToUse)
  26. : Thread ("iTunesLibraryParser"),
  27. lock (lockToUse),
  28. iTunesLibraryFile (iTunesLibraryFileToUse),
  29. treeToFill (elementToFill),
  30. numAdded (0),
  31. finished (false)
  32. {
  33. startThread (1);
  34. }
  35. ITunesLibraryParser::~ITunesLibraryParser()
  36. {
  37. stopThread (5000);
  38. }
  39. void ITunesLibraryParser::run()
  40. {
  41. // parse the iTunes xml first
  42. iTunesDatabase = XmlDocument::parse (iTunesLibraryFile);
  43. if (! iTunesDatabase->hasTagName ("plist")
  44. || iTunesDatabase->getStringAttribute ("version") != "1.0")
  45. {
  46. jassertfalse; // not a vlid iTunesLibrary file!
  47. finished = true;
  48. return;
  49. }
  50. // find start of tracks library
  51. iTunesLibraryTracks = iTunesDatabase->getFirstChildElement()->getChildByName ("dict");
  52. currentElement = iTunesLibraryTracks->getFirstChildElement();
  53. // find any existing elements
  54. Array<int> existingIds;
  55. Array<ValueTree> existingItems;
  56. if (! threadShouldExit())
  57. {
  58. const ScopedLock sl (lock);
  59. if (treeToFill.hasType (MusicColumns::libraryIdentifier))
  60. {
  61. for (int i = 0; i < treeToFill.getNumChildren(); ++i)
  62. {
  63. ValueTree currentItem (treeToFill.getChild (i));
  64. int idOfChild = int (currentItem.getProperty (MusicColumns::columnNames[MusicColumns::ID]));
  65. existingIds.add (idOfChild);
  66. existingItems.add (currentItem);
  67. }
  68. }
  69. }
  70. while (! threadShouldExit())
  71. {
  72. int currentItemId = -1;
  73. ValueTree newElement;
  74. bool alreadyExists = false;
  75. bool needToModify = false;
  76. bool isAudioFile = false;
  77. while (currentElement != nullptr)
  78. {
  79. if (currentElement->getTagName() == "key")
  80. {
  81. currentItemId = currentElement->getAllSubText().getIntValue(); // e.g. <key>13452</key>
  82. alreadyExists = existingIds.contains (currentItemId);
  83. if (alreadyExists)
  84. {
  85. // first get the relevant tree item
  86. lock.enter();
  87. const int index = existingIds.indexOf (currentItemId);
  88. ValueTree existingElement (existingItems.getUnchecked (index));
  89. lock.exit();
  90. // and then check the modification dates
  91. XmlElement* trackDetails = currentElement->getNextElement(); // move on to the <dict>
  92. forEachXmlChildElement (*trackDetails, e)
  93. {
  94. if (e->getAllSubText() == MusicColumns::iTunesNames[MusicColumns::Modified])
  95. {
  96. const int64 newModifiedTime = parseITunesDateString (e->getNextElement()->getAllSubText()).toMilliseconds();
  97. const int64 currentModifiedTime = int64 (existingElement.getProperty (MusicColumns::columnNames[MusicColumns::Modified]));
  98. if (newModifiedTime > currentModifiedTime)
  99. {
  100. const ScopedLock sl (lock);
  101. treeToFill.removeChild (existingElement, nullptr);
  102. needToModify = true;
  103. }
  104. break;
  105. }
  106. }
  107. }
  108. newElement = ValueTree (MusicColumns::libraryItemIdentifier);
  109. newElement.setProperty (MusicColumns::columnNames[MusicColumns::ID], currentItemId, nullptr);
  110. }
  111. currentElement = currentElement->getNextElement(); // move on to the <dict>
  112. if (! alreadyExists || needToModify)
  113. break;
  114. }
  115. // check to see if we have reached the end
  116. if (currentElement == nullptr)
  117. {
  118. finished = true;
  119. signalThreadShouldExit();
  120. break;
  121. }
  122. if (currentElement->getTagName() == "dict")
  123. {
  124. // cycle through items of each track
  125. forEachXmlChildElement (*currentElement, e2)
  126. {
  127. const String elementKey (e2->getAllSubText());
  128. //const String elementValue (e2->getNextElement()->getAllSubText());
  129. if (elementKey == "Kind")
  130. {
  131. if (e2->getNextElement()->getAllSubText().contains ("audio file"))
  132. {
  133. isAudioFile = true;
  134. newElement.setProperty (MusicColumns::columnNames[MusicColumns::LibID], numAdded, nullptr);
  135. numAdded++;
  136. }
  137. }
  138. else if (elementKey == "Track Type")
  139. {
  140. // this is a file in iCloud, not a local, readable one
  141. if (e2->getNextElement()->getAllSubText().contains ("Remote"))
  142. {
  143. isAudioFile = false;
  144. break;
  145. }
  146. }
  147. // and check the entry against each column
  148. for(int i = 2; i < MusicColumns::numColumns; i++)
  149. {
  150. if (elementKey == MusicColumns::iTunesNames[i])
  151. {
  152. const String elementValue = e2->getNextElement()->getAllSubText();
  153. if (i == MusicColumns::Length
  154. || i == MusicColumns::BPM
  155. || i == MusicColumns::LibID)
  156. {
  157. newElement.setProperty (MusicColumns::columnNames[i], elementValue.getIntValue(), nullptr);
  158. }
  159. else if (i == MusicColumns::Added
  160. || i == MusicColumns::Modified)
  161. {
  162. const int64 timeInMilliseconds (parseITunesDateString (elementValue).toMilliseconds());
  163. newElement.setProperty (MusicColumns::columnNames[i], timeInMilliseconds, nullptr);
  164. }
  165. else
  166. {
  167. String textEntry (elementValue);
  168. if (i == MusicColumns::Location)
  169. textEntry = stripFileProtocolForLocal (elementValue);
  170. newElement.setProperty (MusicColumns::columnNames[i], textEntry, nullptr);
  171. }
  172. }
  173. }
  174. }
  175. if (isAudioFile == true)
  176. {
  177. const ScopedLock sl (lock);
  178. treeToFill.addChild (newElement, -1, nullptr);
  179. }
  180. currentElement = currentElement->getNextElement(); // move to next track
  181. }
  182. }
  183. }