Archived
1
This commit is contained in:
Kim Wittenburg
2019-02-01 22:59:01 +01:00
parent ba472864df
commit 0a485ff42a
82 changed files with 3975 additions and 1822 deletions

91
TagTunes/ImportedTrack.swift Executable file
View File

@@ -0,0 +1,91 @@
//
// ImportedTrack.swift
// TagTunes
//
// Created by Kim Wittenburg on 17.03.16.
// Copyright © 2016 Kim Wittenburg. All rights reserved.
//
import SearchAPI
public class ImportedTrack: TagTunesTrack {
/// The `iTunesTrack` represented by this instance.
private var track: iTunesTrack
public override func readTrackID() throws -> SearchAPIID {
guard let fileTrack = self.track as? iTunesFileTrack, fileURL = fileTrack.location where fileURL.checkResourceIsReachableAndReturnError(nil) else {
throw TagTunesTrackErrors.FileNotFound
}
let fileHandle = try? NSFileHandle(forReadingFromURL: fileTrack.location)
// Read the first 1024 bytes from the file. The file's ID seems to always
// be at around 600-700 bytes so we don't need to read more than that.
guard let data = fileHandle?.readDataOfLength(1024) else {
throw TagTunesTrackErrors.FileNotReadable
}
let dataToFind = "song".dataUsingEncoding(NSASCIIStringEncoding)!
let dataRange = data.rangeOfData(dataToFind, options: [], range: NSRange(location: 0, length: data.length))
guard dataRange.location != NSNotFound else {
throw TagTunesTrackErrors.NoIDFound
}
var rawID: UInt32 = 0
data.getBytes(&rawID, range: NSRange(location: NSMaxRange(dataRange), length: 4))
return SearchAPIID(UInt32(bigEndian: rawID))
}
/// Initializes the track with the specified `iTunesTrack`.
public init(track: iTunesTrack) {
self.track = track
super.init()
}
public required init?(coder aDecoder: NSCoder) {
if let track = aDecoder.decodeObjectOfClass(iTunesTrack.self, forKey: "track") {
self.track = track
super.init()
} else {
track = iTunesTrack()
super.init()
return nil
}
}
public override func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(track, forKey: "track")
}
public override func valueForTag(tag: Tag) -> AnyObject! {
return track.valueForKey(tag.rawValue)
}
public override func reveal() {
track.reveal()
iTunes.activate()
}
public override var supportsBatchSaving: Bool {
return false
}
public override func saveValue(value: AnyObject?, forTag tag: Tag) throws {
if tag == .Artwork {
track.artworks().removeAllObjects()
if let artwork = value as? NSImage {
track.artworks().objectAtIndex(0).propertyWithCode(AEKeyword("pPCT")).setTo(artwork)
}
} else if let theValue = value {
track.propertyWithCode(tag.code).setTo(theValue)
} else {
track.propertyWithCode(tag.code).setTo("")
}
}
public override var hashValue: Int {
return track.id()
}
}
public func ==(lhs: ImportedTrack, rhs: ImportedTrack) -> Bool {
return lhs.track.id() == rhs.track.id()
}