1
/***************************************************************************
2
* Copyright 2008 Last.fm Ltd. *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
18
***************************************************************************/
20
#include "ITunesLibrary.h"
21
#include "PlayCountsDatabase.h"
23
#include "libMoose/LastFmSettings.h"
24
#include "libUnicorn/TrackInfo.h"
27
#include <QDomDocument>
28
#include <QStringList>
32
/** @author <max@last.fm>
40
/** you own the memory */
41
static IPod* fromCommandLineArguments( const QStringList& );
43
/** figures out the iPod scrobbles */
46
/** allows us to encapsulate the real scrobble count() */
47
class ScrobbleList : private QList<TrackInfo>
52
ScrobbleList() : m_realCount( 0 )
54
using QList<TrackInfo>::isEmpty;
55
QDomDocument xml() const;
56
int count() const { return m_realCount; }
57
ScrobbleList& operator+=( const TrackInfo& i )
59
m_realCount += i.playCount();
63
ScrobbleList& operator-=( const QString& uniqueId )
65
QList<TrackInfo>::Iterator iter;
66
for( iter = begin(); iter != end(); ++iter )
68
if( iter->uniqueID() == uniqueId )
75
m_realCount -= iter->playCount();
82
QList<TrackInfo>::clear();
86
ScrobbleList scrobbles() const { return m_scrobbles; }
87
ScrobbleList& scrobbles() { return m_scrobbles; }
90
enum Type { UnknownType, AutomaticType, ManualType };
92
/** looks over-engineered, but makes Twiddly/main.cpp code read much better */
96
Settings( IPod const * const ipod )
98
m_uid = ipod->device + '/' + ipod->serial;
101
Type type() const { return (Type) value( "type", UnknownType ).toInt(); }
102
void setType( Type t ) { setValue( "type", t ); }
105
QVariant value( const QString& key, const QVariant& defaultValue ) const
107
MediaDeviceSettings s;
108
s.beginGroup( m_uid );
109
return s.value( key, defaultValue );
112
void setValue( const QString& key, const QVariant& v )
114
MediaDeviceSettings s;
115
s.beginGroup( m_uid );
116
s.setValue( key, v );
122
Settings settings() const { return Settings( this ); }
125
/** assigned to the mediaDeviceId on TrackInfo objects */
126
QString scrobbleId() const;
128
/** every device has its own directory for storing stuff */
129
QDir saveDir() const;
133
QString pid; //product id, not persistent id
137
ScrobbleList m_scrobbles;
139
/** heap allocate and return those relevent to your iPod type */
140
virtual class PlayCountsDatabase* playCountsDatabase() = 0;
141
virtual class ITunesLibrary* iTunesLibrary() = 0;
147
class AutomaticIPod : public IPod
150
class PlayCountsDatabase : public ::PlayCountsDatabase
153
PlayCountsDatabase();
155
/** duplicates the contents of the iTunes Library into our database */
157
bool isBootstrapNeeded() const;
161
virtual PlayCountsDatabase* playCountsDatabase() { return new PlayCountsDatabase; }
162
virtual ITunesLibrary* iTunesLibrary() { return new ITunesLibrary; }
167
class ManualIPod : public IPod
172
class PlayCountsDatabase : public ::PlayCountsDatabase
175
PlayCountsDatabase( class IPod const * const ipod )
176
: ::PlayCountsDatabase( ipod->saveDir().filePath( "playcounts.db" ) )
180
class Library : public ITunesLibrary
183
Library( const QString& pid ) : ITunesLibrary( pid, true )
188
virtual PlayCountsDatabase* playCountsDatabase() { return new PlayCountsDatabase( this ); }
189
virtual ITunesLibrary* iTunesLibrary() { return new Library( m_pid ); }
191
/** persistent ID of the iPod source, mac only */
195
static QString firstPid();