Class Collection
In: collection.rb
Parent: Object

Class Collection to control Amarok collection.

Methods

Public Class methods

Creates object

[Source]

# File collection.rb, line 316
        def initialize
                scan_tracks
                scan_albums
                scan_artists
                scan_genres
        end

Public Instance methods

Method to add album name to playlist If name is one, add it, if there is more albums with the same name user can select.

[Source]

# File collection.rb, line 342
        def add_album(name)
                id = albums[name]
                if id.class == Array
                        select_album(id)
                else
                        add_album_id(id)
                end
        end

Method to add artist name to playlist If name is one, add it, if there is more artist with the same name user can select.

[Source]

# File collection.rb, line 356
        def add_artist(name)
                id = @artists[name]
                if id.class == Array
                        select_artist(id)
                else
                        add_artist_id(id)
                end
        end

Method to add genre name to playlist

[Source]

# File collection.rb, line 368
        def add_genre(name)
                id = genres[name]
                if id.class == Array
                        select_genres
                else
                        add_genre_id(id)
                end
        end

Method to add name to playlist. name si string that can be name of

  • artist
  • album
  • interpret
  • genre

If there is more than one option what to play, user will be asked.

[Source]

# File collection.rb, line 386
        def add_to_playlist(name)
                begin
                        album, track, artist, genre = 0,0,0,0
                        album = 1 if @albums.has_key?(name)
                        track = 1 if @tracks.has_key?(name)
                        artist = 1 if @artists.has_key?(name)
                        genre = 1 if @genres.has_key?(name)
                        if (album + track + artist + genre == 0 )
                                $speaker.say("#{name} was not found in collection.")
                                return 1
                        end
                        if (album + track + artist + genre == 1)
                                add_album(name) if album == 1
                                add_track(name) if track == 1
                                add_artist(name) if artist == 1
                                add_genre(name) if genre == 1
                                return
                        end
                        $speaker.say("#{name}" + what_to_play(album,track,artist,genre) + " What do you want to play?")
                        puts "Waiting for answer"
                        answer = $listener.get_answer
                        puts "answer: " + answer
                        case answer
                        when "album"
                                add_album(name)
                        when "track"
                                add_track(name)
                        when "artist"
                                add_artist(name)
                        when "genre"
                                add_genre(name)
                        end
                rescue Exception => e
                        puts "error: #{e}"
                        # you can't play
                        return 1
                end
                # return 0 means everithing is OK, you can play
                return 0
        end

Method to add track name to playlist If name is one, add it, if there is more songs with the same name user can select.

[Source]

# File collection.rb, line 328
        def add_track(name)
                url = @tracks[name]
                if url.class == Array
                        select_track(url)
                else
                        add_track_url(url)
                end
        end

Private Instance methods

Add album with id to playlist. Throws excepition if the album doesn‘t exist in collection.

[Source]

# File collection.rb, line 205
        def add_album_id(id)
                tracks = query("select url from tags where album = #{id}").split("\n")
                # throw exception if the album doesn't exist
                raise "Album doesn't exist!" if tracks.nil?
                add_all_tracks(tracks)
        end

Add all albums from ids. ids is array of album ids.

[Source]

# File collection.rb, line 195
        def add_all_albums(ids)
                ids.each do | id |
                        add_album_id(id)
                end
        end

Add all artists from ids. ids is array of artist ids.

[Source]

# File collection.rb, line 260
        def add_all_artists(ids)
                ids.each do | id |
                        add_artist_id(id)
                end
        end

Add all genres from ids. ids is array of genres ids.

[Source]

# File collection.rb, line 216
        def add_all_genres(ids)
                ids.each do | id |
                        add_genre_id(id)
                end
        end

Add all tracks from tracks. ids is array of track urls.

[Source]

# File collection.rb, line 132
        def add_all_tracks(tracks)
                tracks.each do | a |
                        add_track_url(a)
                end
        end

Add artist with id to playlist. Throws excepition if the artist doesn‘t exist in collection.

[Source]

# File collection.rb, line 281
        def add_artist_id(id)
                tracks = query("select url from tags where artist = #{id}").split("\n")
                # throw exception if the artist doesn't exist
                raise "Artist doesn't exist!" if tracks.nil?
                add_all_tracks(tracks)
        end

Add genre with id to playlist. Throws excepition if the genre doesn‘t exist in collection.

[Source]

# File collection.rb, line 237
        def add_genre_id(id)
                tracks = query("select url from tags where genre = #{id}").split("\n")
                # throw exception if the album doesn't exist
                raise "Genre doesn't exist!" if tracks.nil?
                add_all_tracks(tracks)
        end

Add track with url to playlist. Throws excepition if the album doesn‘t exist in collection.

[Source]

# File collection.rb, line 142
        def add_track_url(url)
                # throw exception if the track doesn't exist
                raise "Track doesn't exist!" if url.nil?
#               need to delete firts dot
#               url = url[1,url.length]
#               need to replace first dot by /home
                url = url.gsub(/^\./,"/home")
                `dcop amarok playlist addMedia "#{url}"`
        end

Generates the list of albums from ids. ids is array of albums ids. Find artist to each album.

[Source]

# File collection.rb, line 157
        def album_list(ids)
                list = ""
                index = 1
                # can be slow here !!!
                iartists = @artists.invert
                ids.each do | id |
                        result = query("select artist from tags where album='#{id}'").split("\n")
                        artist = iartists[result[1]]
                        list += "Number #{index}. Is from #{artist}. "
                        index += 1
                        if index > 10
                                break;
                        end
                end
                return list
        end

Generates the list of artist from ids. ids is array of artist ids.

[Source]

# File collection.rb, line 248
        def artist_list(ids)
                list = ""
                index = 1
                #TODO jak rozlišit 2 inteprety stejného jména?
                
                return list
        end

Creates MyHash from long list (result of sql query). Split the list into pairs and insert in into MyHash data structure.

[Source]

# File collection.rb, line 26
        def parse_pairs(list)
                i  = 0
                hash = MyHash.new
                while (i <= list.length)
                        name = list[i+1].to_s.downcase
                        index = list[i]
                        # downcase of the string for good recognition
                        hash[name] = index
                        i += 2
                end
                return hash
        end

Execute sql_query on amarok collection. Returns the result.

[Source]

# File collection.rb, line 42
        def query(sql_query)
                puts "query: " + "dcop amarok collection query \"#{sql_query}\""
                return `dcop amarok collection query "#{sql_query}"`
        end

Select all albums from music collection and keep it in +@albums+.

[Source]

# File collection.rb, line 58
        def scan_albums
                result = query("select id,name from album").split("\n")
                @albums = parse_pairs(result)
        end

Select all artists from music collection and keep it in +@artists+.

[Source]

# File collection.rb, line 50
        def scan_artists
                result = query("select id,name from artist").split("\n")
                @artists = parse_pairs(result)
        end

Select all genres from music collection and keep it in +@genres+.

[Source]

# File collection.rb, line 74
        def scan_genres
                result = query("select id, from genre").split("\n")
                @genres = parse_pairs(result)
        end

Select all tracks from music collection and keep it in +@tracks+.

[Source]

# File collection.rb, line 66
        def scan_tracks
                result = query("select url,title from tags").split("\n")
                @tracks = parse_pairs(result)
        end

Selection from more albums from ids. ids is array of artist ids.

[Source]

# File collection.rb, line 178
        def select_albums(ids)
                $speaker.say(selection_question(ids.size, "albums"))
                $speaker.say(album_list(ids))
                puts "waiting for answer..."
                answer = $listener.get_answer
                puts "answer: " + answer
                if answer == "all"
                        add_all_albums(ids)
                else
                        add_album_id(ids[numbers(answer)])
                end
        end

Selection from more artists from ids. ids is array of artist ids.

[Source]

# File collection.rb, line 270
        def select_artist(ids)
                # probably case problem in collection data
                # e.g. AC/DC vs ac/dc
                # add all artists without asking
                add_all_artists(ids)
        end

Selection from more genres from ids. This should never happen.

[Source]

# File collection.rb, line 226
        def select_genres(ids)
                # probably case problem in collection data
                # e.g. Rock / rock / ROCK
                # add all genres without asking
                add_all_genres(ids)
        end

Selection from more tracks from urls. urls is array of tracks ids.

[Source]

# File collection.rb, line 115
        def select_track(urls)
                $speaker.say(selection_question(urls.size, "tracks"))
                $speaker.say(track_list(urls))
                puts "waiting for answer..."
                answer = $listener.get_answer
                puts "answer: " + answer
                if answer == "all"
                        add_all_tracks(urls)
                else
                        add_track_url(urls[numbers(answer)])
                end
        end

Generates text information for user, that he has count tracks, albums, interprets or genres (this information contains what) and he can choose what to play.

[Source]

# File collection.rb, line 83
        def selection_question(count,what)
                return "You have #{count} #{what} in collection. You can say all, to play them all,
       or the number of the song in following list."
        end

Generates the list of albums from urls. ids is array of albums ids. Find artist and album to each track.

[Source]

# File collection.rb, line 93
        def track_list(urls)
                list = ""
                index = 1
                # can be slow here !!!
                ialbums = @albums.invert
                iartists = @artists.invert
                urls.each do | url |
                        result = query("select album, artist from tags where url='#{url}'").split("\n")
                        album, artist = ialbums[result[0]], iartists[result[1]]
                        list += "Number #{index}. Is on #{artist}'s album #{album}. "
                        index += 1
                        if index > 10
                                break;
                        end
                end
                return list
        end

Generate the list of options, what is in collection. Return text that is said to user.

[Source]

# File collection.rb, line 292
        def what_to_play(album,track,artist,genre)
                if (album == 0)
                        if (track == 0)
                                text = " is the artist and genre"
                                artist, genre = 0
                        else
                                text = " is the track"
                                track = 0
                        end
                else
                        text = " is the album"
                        album = 0
                end
                text += " and track" if track == 1
                text += " and artist" if artist == 1
                text += " and genre" if genre == 1
                text += "."
                return text
        end

[Validate]