Class: ItunesController::CachedController

Inherits:
Object
  • Object
show all
Defined in:
lib/itunesController/cachedcontroller.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, databaseBackend) ⇒ CachedController

Returns a new instance of CachedController.



30
31
32
33
34
# File 'lib/itunesController/cachedcontroller.rb', line 30

def initialize(controller,databaseBackend)
    @controller = controller           
    @database = ItunesController::Database.new(@controller,databaseBackend)            
    @cachedOnCreate=cacheTracks()
end

Instance Method Details

#addTrack(path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/itunesController/cachedcontroller.rb', line 87

def addTrack(path)
    ItunesController::ItunesControllerLogging::debug("Adding track #{path}")
    ids=@controller.addFilesToLibrary([path])            
    if (ids.length==1)
        if (@database.getTrackById(ids[0].databaseId)!=nil)
            ItunesController::ItunesControllerLogging::info("Track '#{path}' allready in the database with the id #{ids[0].databaseId}")
            return nil
        end
        track=ids[0]
        @database.addTrack(track) 
        count=@database.getParam(ItunesController::Database::PARAM_KEY_TRACK_COUNT,0).to_i
        count=count+1
        @database.setParam(ItunesController::Database::PARAM_KEY_TRACK_COUNT,count)
        ItunesController::ItunesControllerLogging::info("Added track '#{path}' with id #{ids[0].databaseId}")
        return track
    else
        ItunesController::ItunesControllerLogging::info("Uable to add track #{path}")
        return nil
    end
end

#cacheTracks(force = false) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/itunesController/cachedcontroller.rb', line 133

def cacheTracks(force=false)
    if (force || needsRecacheTracks())
        ItunesController::ItunesControllerLogging::info("Caching tracks...")
        @database.removeTracks()
        @database.setParam(ItunesController::Database::PARAM_KEY_TRACK_COUNT,@controller.getTrackCount())
        size=@controller.getTracks() { |t,count,size,dead|
            if (dead)
                ItunesController::ItunesControllerLogging::warn("Found dead track with databaseID #{t.databaseId}")
                @database.addDeadTrack(t)
            else
                @database.addTrack(t)
            end
        }
        return true
    else
        ItunesController::ItunesControllerLogging::debug("Tracks uptodate")
    end
    return false
end

#closeObject



171
172
173
# File 'lib/itunesController/cachedcontroller.rb', line 171

def close()
    @database.close()
end

#findDeadTracksObject



153
154
155
# File 'lib/itunesController/cachedcontroller.rb', line 153

def findDeadTracks()
    return @database.getDeadTracks()
end

#getCachedTracksOnCreateObject



36
37
38
# File 'lib/itunesController/cachedcontroller.rb', line 36

def getCachedTracksOnCreate()
    return @cachedOnCreate
end

#getItunesVersionObject



167
168
169
# File 'lib/itunesController/cachedcontroller.rb', line 167

def getItunesVersion
    return @controller.version
end

#getTrack(path) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/itunesController/cachedcontroller.rb', line 108

def getTrack(path)
    trackInfo=@database.getTrack(path)
    if (trackInfo==nil)
        ItunesController::ItunesControllerLogging::debug("Unable to find track with path: "+path)
        return nil
    end
    foundTracks=@controller.searchLibrary(trackInfo.title)
    tracks=[]
    foundTracks.each do | t |
        if (@controller.getTrackDatabaseId(t) == trackInfo.databaseId)
            tracks.push(t)
        end
    end
    if (tracks.length==1)
        return tracks[0]
    else
        return nil
    end
end

#removeDeadTracksObject



157
158
159
160
161
162
163
164
165
# File 'lib/itunesController/cachedcontroller.rb', line 157

def removeDeadTracks()
    count=0
    deadTracks=findDeadTracks()
    deadTracks.each do | track |
        removeTrackByInfo(track)
        count+=1
    end
    return count
end

#removeTrack(path) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/itunesController/cachedcontroller.rb', line 40

def removeTrack(path)
    trackInfo=@database.getTrack(path)
    if (trackInfo==nil)
        ItunesController::ItunesControllerLogging::error("Unable to find track with path: "+path)
        return nil
    end
    removeTrackByInfo(trackInfo)
end

#removeTrackByInfo(trackInfo) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/itunesController/cachedcontroller.rb', line 49

def removeTrackByInfo(trackInfo)            
    ItunesController::ItunesControllerLogging::debug("Removing track: '#{trackInfo}'")
    foundTracks=@controller.searchLibrary(trackInfo.title)
    if (foundTracks==nil || foundTracks.length==0)
        ItunesController::ItunesControllerLogging::error("Unable to find track #{trackInfo}'")
        return nil
    end
    foundTracks.each do | t |
        if (@controller.getTrackDatabaseId(t) == trackInfo.databaseId)                  
           @controller.removeTracksFromLibrary([t])
           count=@database.getParam(ItunesController::Database::PARAM_KEY_TRACK_COUNT,0).to_i
           count=count-1
           @database.setParam(ItunesController::Database::PARAM_KEY_TRACK_COUNT,count)
           if (trackInfo.location!=nil)
               ItunesController::ItunesControllerLogging::info("Remove track '#{trackInfo.location}' from iTunes library")
           else
               ItunesController::ItunesControllerLogging::info("Remove track with databaseId '#{trackInfo.databaseId}' from iTunes library")
           end
           @database.removeTrack(trackInfo)
        end
    end            
end

#trackInLibrary?(path) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/itunesController/cachedcontroller.rb', line 128

def trackInLibrary?(path)
    trackInfo=@database.getTrack(path)
    return (trackInfo!=nil)
end

#updateTrack(path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/itunesController/cachedcontroller.rb', line 72

def updateTrack(path)
    trackInfo=@database.getTrack(path)
    if (trackInfo==nil)
        ItunesController::ItunesControllerLogging::debug("Unable to find track with path: "+path)
        return nil
    end
    foundTracks=@controller.searchLibrary(trackInfo.title)
    foundTracks.each do | t |
        if (@controller.getTrackDatabaseId(t) == trackInfo.databaseId)
           @controller.refreshTracks([t])
           ItunesController::ItunesControllerLogging::info("Refreshed track  '#{trackInfo.location}' metadata")
        end
    end
end