PageRenderTime 97ms CodeModel.GetById 53ms app.highlight 16ms RepoModel.GetById 25ms app.codeStats 0ms

/thirdparty/liblastfm2/src/scrobble/ScrobbleCache.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 163 lines | 106 code | 32 blank | 25 comment | 15 complexity | e093ec2aa30991ee3ae431c9cfde5e0e MD5 | raw file
  1/*
  2   Copyright 2009 Last.fm Ltd. 
  3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4
  5   This file is part of liblastfm.
  6
  7   liblastfm is free software: you can redistribute it and/or modify
  8   it under the terms of the GNU General Public License as published by
  9   the Free Software Foundation, either version 3 of the License, or
 10   (at your option) any later version.
 11
 12   liblastfm is distributed in the hope that it will be useful,
 13   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License
 18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 19*/
 20#include "ScrobbleCache.h"
 21#include "ScrobblePoint.h"
 22#include <lastfm/misc.h>
 23#include <QCoreApplication>
 24#include <QFile>
 25#include <QDomElement>
 26#include <QDomDocument>
 27
 28#if LASTFM_VERSION >= 0x00010000
 29using lastfm::ScrobbleCache;
 30#endif
 31
 32ScrobbleCache::ScrobbleCache( const QString& username )
 33{
 34    Q_ASSERT( username.length() );
 35
 36    m_path = lastfm::dir::runtimeData().filePath( username + "_subs_cache.xml" );
 37    m_username = username;
 38
 39    QDomDocument xml;
 40    read( xml );
 41}
 42
 43
 44bool
 45ScrobbleCache::isValid( const Track& track, Invalidity* v )
 46{
 47    #define TEST( test, x ) \
 48        if (test) { \
 49            if (v) *v = x; \
 50            return false; \
 51        }
 52
 53    TEST( track.duration() < ScrobblePoint::kScrobbleMinLength, TooShort );
 54
 55    TEST( !track.timestamp().isValid(), NoTimestamp );
 56
 57    // actual spam prevention is something like 12 hours, but we are only
 58    // trying to weed out obviously bad data, server side criteria for
 59    // "the future" may change, so we should let the server decide, not us
 60    TEST( track.timestamp() > QDateTime::currentDateTime().addMonths( 1 ), FromTheFuture );
 61
 62    TEST( track.timestamp() < QDateTime::fromString( "2003-01-01", Qt::ISODate ), FromTheDistantPast );
 63
 64    // Check if any required fields are empty
 65    TEST( track.artist().isNull(), ArtistNameMissing );
 66    TEST( track.title().isEmpty(), TrackNameMissing );
 67
 68    TEST( (QStringList() << "unknown artist"
 69                         << "unknown"
 70                         << "[unknown]"
 71                         << "[unknown artist]").contains( track.artist().name().toLower() ),
 72           ArtistInvalid );
 73
 74    return true;
 75}
 76
 77
 78void
 79ScrobbleCache::read( QDomDocument& xml )
 80{
 81    m_tracks.clear();
 82
 83    QFile file( m_path );
 84    file.open( QFile::Text | QFile::ReadOnly );
 85    QTextStream stream( &file );
 86    stream.setCodec( "UTF-8" );
 87
 88    xml.setContent( stream.readAll() );
 89
 90    for (QDomNode n = xml.documentElement().firstChild(); !n.isNull(); n = n.nextSibling())
 91        if (n.nodeName() == "track")
 92            m_tracks += Track( n.toElement() );
 93}
 94
 95
 96void
 97ScrobbleCache::write()
 98{
 99    if (m_tracks.isEmpty())
100    {
101        QFile::remove( m_path );
102    }
103    else {
104        QDomDocument xml;
105        QDomElement e = xml.createElement( "submissions" );
106        e.setAttribute( "product", QCoreApplication::applicationName() );
107        e.setAttribute( "version", "2" );
108
109        foreach (Track i, m_tracks)
110            e.appendChild( i.toDomElement( xml ) );
111
112        xml.appendChild( e );
113
114        QFile file( m_path );
115        file.open( QIODevice::WriteOnly | QIODevice::Text );
116
117        QTextStream stream( &file );
118        stream.setCodec( "UTF-8" );
119        stream << "<?xml version='1.0' encoding='utf-8'?>\n";
120        stream << xml.toString( 2 );
121        file.close();
122    }
123}
124
125
126void
127ScrobbleCache::add( const QList<Track>& tracks )
128{
129    foreach (const Track& track, tracks)
130    {
131        ScrobbleCache::Invalidity invalidity;
132        
133        if ( !isValid( track, &invalidity ) )
134        {
135            qWarning() << invalidity;
136        }
137        else if (track.isNull()) 
138            qDebug() << "Will not cache an empty track";
139        else 
140            m_tracks += track;
141    }
142
143    write();
144}
145
146
147int
148ScrobbleCache::remove( const QList<Track>& toremove )
149{
150    QMutableListIterator<Track> i( m_tracks );
151    while (i.hasNext()) {
152        Track t = i.next();
153        for (int x = 0; x < toremove.count(); ++x)
154            if (toremove[x] == t)
155                i.remove();
156    }
157
158    write();
159
160    // yes we return # remaining, rather # removed, but this is an internal 
161    // function and the behaviour is documented so it's alright imo --mxcl
162    return m_tracks.count();
163}