~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/Twiddly/IPod.h

  • Committer: Bazaar Package Importer
  • Author(s): Devid Filoni
  • Date: 2008-07-14 16:46:20 UTC
  • mfrom: (1.1.7 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080714164620-67hoz9fs177wpgmr
Tags: 1:1.5.1.31879.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #248100), remaining changes:
  - debian/rules: add dh_icons call
  + debian/control:
    - switch iceweasel to firefox in Recommends field
    - modify debhelper version to >= 5.0.51~
    - modify Maintainer to Ubuntu MOTU Developers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright 2008 Last.fm Ltd.                                           *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "ITunesLibrary.h"
 
21
#include "PlayCountsDatabase.h"
 
22
 
 
23
#include "libMoose/LastFmSettings.h"
 
24
#include "libUnicorn/TrackInfo.h"
 
25
 
 
26
#include <QDir>
 
27
#include <QDomDocument>
 
28
#include <QStringList>
 
29
#include <QSet>
 
30
 
 
31
 
 
32
/** @author <max@last.fm>
 
33
  */
 
34
class IPod
 
35
{
 
36
public:
 
37
    virtual ~IPod()
 
38
    {}
 
39
    
 
40
    /** you own the memory */
 
41
    static IPod* fromCommandLineArguments( const QStringList& );
 
42
    
 
43
    /** figures out the iPod scrobbles */
 
44
    void twiddle();
 
45
 
 
46
    /** allows us to encapsulate the real scrobble count() */
 
47
    class ScrobbleList : private QList<TrackInfo>
 
48
    {
 
49
        int m_realCount;
 
50
 
 
51
    public:
 
52
        ScrobbleList() : m_realCount( 0 )
 
53
        {}
 
54
        using QList<TrackInfo>::isEmpty;
 
55
        QDomDocument xml() const;
 
56
        int count() const { return m_realCount; }
 
57
        ScrobbleList& operator+=( const TrackInfo& i )
 
58
        {
 
59
            m_realCount += i.playCount();
 
60
            append( i );
 
61
            return *this;
 
62
        }
 
63
        ScrobbleList& operator-=( const QString& uniqueId )
 
64
        {
 
65
            QList<TrackInfo>::Iterator iter;
 
66
            for( iter = begin(); iter != end(); ++iter ) 
 
67
            {
 
68
                if( iter->uniqueID() == uniqueId )
 
69
                    break;
 
70
            }
 
71
 
 
72
            if( iter == end() )
 
73
                return *this;
 
74
 
 
75
            m_realCount -= iter->playCount();
 
76
            erase( iter );
 
77
            return *this;
 
78
        }
 
79
        void clear()
 
80
        {
 
81
            m_realCount = 0;
 
82
            QList<TrackInfo>::clear();
 
83
        }
 
84
    };
 
85
 
 
86
    ScrobbleList scrobbles() const { return m_scrobbles; }
 
87
    ScrobbleList& scrobbles() { return m_scrobbles; }
 
88
 
 
89
 
 
90
    enum Type { UnknownType, AutomaticType, ManualType };
 
91
 
 
92
    /** looks over-engineered, but makes Twiddly/main.cpp code read much better */
 
93
    class Settings
 
94
    {
 
95
    public:
 
96
        Settings( IPod const * const ipod )
 
97
        {
 
98
            m_uid = ipod->device + '/' + ipod->serial;
 
99
        }
 
100
 
 
101
        Type type() const { return (Type) value( "type", UnknownType ).toInt(); }
 
102
        void setType( Type t ) { setValue( "type", t ); }
 
103
 
 
104
    private:
 
105
        QVariant value( const QString& key, const QVariant& defaultValue ) const
 
106
        {
 
107
            MediaDeviceSettings s;
 
108
            s.beginGroup( m_uid );
 
109
            return s.value( key, defaultValue );
 
110
        }
 
111
 
 
112
        void setValue( const QString& key, const QVariant& v )
 
113
        {
 
114
            MediaDeviceSettings s;
 
115
            s.beginGroup( m_uid );
 
116
            s.setValue( key, v );
 
117
        }
 
118
 
 
119
        QString m_uid;
 
120
    };
 
121
 
 
122
    Settings settings() const { return Settings( this ); }
 
123
 
 
124
 
 
125
    /** assigned to the mediaDeviceId on TrackInfo objects */
 
126
    QString scrobbleId() const;
 
127
    
 
128
    /** every device has its own directory for storing stuff */
 
129
    QDir saveDir() const;
 
130
 
 
131
    QString device;
 
132
    QString vid;
 
133
    QString pid; //product id, not persistent id
 
134
    QString serial;
 
135
 
 
136
protected:    
 
137
    ScrobbleList m_scrobbles;
 
138
 
 
139
    /** heap allocate and return those relevent to your iPod type */
 
140
    virtual class PlayCountsDatabase* playCountsDatabase() = 0;
 
141
    virtual class ITunesLibrary* iTunesLibrary() = 0;
 
142
 
 
143
};
 
144
 
 
145
 
 
146
 
 
147
class AutomaticIPod : public IPod
 
148
{
 
149
public:
 
150
    class PlayCountsDatabase : public ::PlayCountsDatabase
 
151
    {
 
152
    public:
 
153
        PlayCountsDatabase();
 
154
 
 
155
        /** duplicates the contents of the iTunes Library into our database */
 
156
        void bootstrap();
 
157
        bool isBootstrapNeeded() const;
 
158
    };
 
159
 
 
160
private:
 
161
    virtual PlayCountsDatabase* playCountsDatabase() { return new PlayCountsDatabase; }
 
162
    virtual ITunesLibrary* iTunesLibrary() { return new ITunesLibrary; }
 
163
};
 
164
 
 
165
 
 
166
 
 
167
class ManualIPod : public IPod
 
168
{
 
169
public:
 
170
    ManualIPod();
 
171
 
 
172
    class PlayCountsDatabase : public ::PlayCountsDatabase
 
173
    {
 
174
    public:
 
175
        PlayCountsDatabase( class IPod const * const ipod )
 
176
                : ::PlayCountsDatabase( ipod->saveDir().filePath( "playcounts.db" ) )
 
177
        {}
 
178
    };
 
179
 
 
180
    class Library : public ITunesLibrary
 
181
    {
 
182
    public:
 
183
        Library( const QString& pid ) : ITunesLibrary( pid, true )
 
184
        {}
 
185
    };
 
186
 
 
187
private:
 
188
    virtual PlayCountsDatabase* playCountsDatabase() { return new PlayCountsDatabase( this ); }
 
189
    virtual ITunesLibrary* iTunesLibrary() { return new Library( m_pid ); }
 
190
 
 
191
    /** persistent ID of the iPod source, mac only */
 
192
    QString const m_pid;
 
193
 
 
194
  #ifdef Q_OS_MAC    
 
195
    static QString firstPid();
 
196
  #endif
 
197
};