void
MediaDeviceTrack::setYear( int newYear )
{
    if( !m_collection )
        return;

    MediaDeviceYearPtr yearPtr;
    MediaDeviceTrackPtr track( this );
    YearMap yearMap = m_collection.data()->memoryCollection()->yearMap();

    // do cleanup of soon to be previous year

    yearPtr = m_year;
    if ( !yearPtr.isNull() )
    {
        // remove track from previous year's tracklist
        yearPtr->remTrack( track );
        // if year's tracklist is empty, remove year from yearmap
        if( yearPtr->tracks().isEmpty() )
            yearMap.remove( yearPtr->year() );
    }

    // change to a new year

    // check for the existence of the year to be set to,
    // if year exists, reuse, else create

    if ( yearMap.contains( newYear ) )
    {
        yearPtr = MediaDeviceYearPtr::staticCast( yearMap.value( newYear ) );
    }
    else
    {
        yearPtr = MediaDeviceYearPtr( new MediaDeviceYear( QString::number(newYear) ) );
        yearMap.insert( newYear, YearPtr::staticCast( yearPtr ) );
    }

    // add track to year's tracklist
    yearPtr->addTrack( track );
    // set track's year to the new year
    setYear( yearPtr );

    m_collection.data()->memoryCollection()->acquireWriteLock();
    m_collection.data()->memoryCollection()->setYearMap( yearMap );
    m_collection.data()->memoryCollection()->releaseLock();
}
Exemple #2
0
bool
Reader::parseSongList( const QByteArray &data )
{
    QDataStream raw( data );
    Map songResults = parse( raw, 0, true );

    TrackMap trackMap;
    ArtistMap artistMap;
    AlbumMap albumMap;
    GenreMap genreMap;
    ComposerMap composerMap;
    YearMap yearMap;

    if( songResults["adbs"].toList().size() < 1 ||
        songResults["adbs"].toList()[0].toMap()["mlcl"].toList().count() < 1 ) {
        emit httpError( "Invalid response" ); //it's not a real http error, but the effect is the same
        deleteLater();
        return false;
    }

    QList<QVariant> songList;
    songList = songResults["adbs"].toList()[0].toMap()["mlcl"].toList()[0].toMap()["mlit"].toList();
    debug() << "songList.count() = " << songList.count();
    foreach( const QVariant &var, songList )
    {
        //debug() << "begin iteration...";
        QString itemId = QString::number( var.toMap()["miid"].toList()[0].toInt() );
        QString format = var.toMap()["asfm"].toList().size() ? var.toMap()["asfm"].toList()[0].toString() : QString();
        DaapTrackPtr track( new DaapTrack( m_memColl, m_host, m_port, m_databaseId, itemId, format ) );
        track->setTitle( var.toMap()["minm"].toList().size() ? var.toMap()["minm"].toList()[0].toString() : QString() );
        track->setLength( var.toMap()["astm"].toList().size() ? var.toMap()["astm"].toList()[0].toInt()/1000 : 0 );
        track->setTrackNumber( var.toMap()["astn"].toList().size() ? var.toMap()["astn"].toList()[0].toInt() : 0);
        QString album = var.toMap()["asal"].toList().size() ? var.toMap()["asal"].toList()[0].toString() : QString();
        DaapAlbumPtr albumPtr;
        if ( albumMap.contains( album ) )
            albumPtr = DaapAlbumPtr::staticCast( albumMap.value( album ) );
        else
        {
            albumPtr = DaapAlbumPtr( new DaapAlbum( album ) );
            albumMap.insert( album, AlbumPtr::staticCast( albumPtr ) );
        }
        albumPtr->addTrack( track );
        track->setAlbum( albumPtr );

        QString artist = var.toMap()["asar"].toList().size() ? var.toMap()["asar"].toList()[0].toString() : QString();
        DaapArtistPtr artistPtr;
        if ( artistMap.contains( artist ) )
            artistPtr = DaapArtistPtr::staticCast( artistMap.value( artist ) );
        else
        {
            artistPtr = DaapArtistPtr( new DaapArtist( artist ) );
            artistMap.insert( artist, ArtistPtr::staticCast( artistPtr ) );
        }
        artistPtr->addTrack( track );
        track->setArtist( artistPtr );

        QString year = var.toMap()["asyr"].toList().size() ? var.toMap()["asyr"].toList()[0].toString() : QString();
        DaapYearPtr yearPtr;
        if ( yearMap.contains( year ) )
            yearPtr = DaapYearPtr::staticCast( yearMap.value( year ) );
        else
        {
            yearPtr = DaapYearPtr( new DaapYear( year ) );
            yearMap.insert( year, YearPtr::staticCast( yearPtr ) );
        }
        yearPtr->addTrack( track );
        track->setYear( yearPtr );

        QString genre = var.toMap()["asgn"].toList().size() ? var.toMap()["asgn"].toList()[0].toString() : QString();
        DaapGenrePtr genrePtr;
        if ( genreMap.contains( genre ) )
            genrePtr = DaapGenrePtr::staticCast( genreMap.value( genre ) );
        else
        {
            genrePtr = DaapGenrePtr( new DaapGenre( genre ) );
            genreMap.insert( genre, GenrePtr::staticCast( genrePtr ) );
        }
        genrePtr->addTrack( track );
        track->setGenre( genrePtr );
        trackMap.insert( track->uidUrl(), TrackPtr::staticCast( track ) );
    }