itunesdb.h

00001  /*****************************************************************************
00002  *   Copyright (C) 2004 by Michael Schulze                                    *
00003  *   mike.s@genion.de                                                         *
00004  *                                                                            *
00005  *  The code contained in this file is free software; you can redistribute    *
00006  *  it and/or modify it under the terms of the GNU Lesser General Public      *
00007  *  License as published by the Free Software Foundation; either version      *
00008  *  2.1 of the License, or (at your option) any later version.                *
00009  *                                                                            *
00010  *  This file is distributed in the hope that it will be useful,              *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00013  *  Lesser General Public License for more details.                           *
00014  *                                                                            *
00015  *  You should have received a copy of the GNU Lesser General Public          *
00016  *  License along with this code; if not, write to the Free Software          *
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
00018  *                                                                            *
00019  *  iTunes and iPod are trademarks of Apple                                   *
00020  *                                                                            *
00021  *  This product is not supported/written/published by Apple!                 *
00022  *****************************************************************************/
00023 
00024 #ifndef ITUNESDB_H
00025 #define ITUNESDB_H
00026 
00027 #include <qmap.h>
00028 #include <qdatetime.h>
00029 #include <qfile.h>
00030 #include <qptrlist.h>
00031 #include <qstringlist.h>
00032 
00033 #include <vector>
00034 
00035 #include "playlist.h"
00036 #include "playlistitem.h"
00037 #include "track.h"
00038 #include "ipodsysinfo.h"
00039 
00040 #define RECENTLY_PLAYED_LIST_NAME "KPOD:Recently Played"
00041 
00046 class ITunesDBTrack : public itunesdb::Track, itunesdb::utils::NonCopyAble {
00047 
00048     friend class ITunesDB;
00049 
00050     class ITunesDB& m_itunesdb;
00051 
00052     ITunesDBTrack( Q_UINT32 trackid, ITunesDB& itunesdb );
00053     ITunesDBTrack( const ITunesDBTrack& src );
00054 
00055 public:
00056     virtual ~ITunesDBTrack();
00057 
00058     void setDateAdded( Q_UINT32 date );
00059 
00063     QString getFilePath() const;
00064 
00065 protected:
00066     virtual void setDirty( bool dirtyFlag = true );
00067 };
00068 
00069 
00074 class ITunesDBSPLRuleSet
00075     : public itunesdb::SmartPlaylistRuleSet,
00076       public itunesdb::TrackPredicate,
00077       itunesdb::utils::NonCopyAble
00078 {
00079 
00080     const class ITunesDB& mItunesdb;
00081     class ITunesDBPlaylist& mPlaylist;
00082 
00083     itunesdb::TrackComparatorPtr getComparatorFor( itunesdb::SPLLimitSort limitSort ) const;
00084 
00085     void applyLimits( itunesdb::TrackPtrList& trackList ) const;
00086 
00087     uint mLastSeenItunesDBVersion;
00088     uint mLastSeenRuleSetVersion;
00089     uint mRuleSetVersion;
00090 
00091 protected:
00092 
00093     virtual itunesdb::SmartPlaylistRule * createStringRule(
00094         itunesdb::SPLStringField field,
00095         itunesdb::SPLStringAction action,
00096         const QString& testString );
00097 
00098     virtual itunesdb::SmartPlaylistRule * createUIntRule(
00099         itunesdb::SPLUIntField field, itunesdb::SPLUIntAction action,
00100         Q_UINT64 fromValue, Q_UINT64 toValue,
00101         Q_INT64  fromDate,  Q_INT64  toDate,
00102         Q_UINT64 fromUnits, Q_UINT64 toUnits );
00103 
00104     virtual bool operator() ( const itunesdb::Track& track );
00105 
00106 public:
00107 
00108     ITunesDBSPLRuleSet( const itunesdb::SmartPlaylistRuleSet& src,
00109                         ITunesDBPlaylist& playlist, const ITunesDB& itunesdb );
00110 
00111     ITunesDBSPLRuleSet( ITunesDBPlaylist& playlist, const ITunesDB& itunesdb );
00112 
00113     virtual ~ITunesDBSPLRuleSet();
00114 
00118     virtual bool addStringRule( itunesdb::SPLStringField field,
00119                  itunesdb::SPLStringAction action, const QString& testString, int position = -1 );
00120 
00124     virtual bool addUIntRule( itunesdb::SPLUIntField field,
00125                               itunesdb::SPLUIntAction action,
00126                               Q_UINT64 fromValue, Q_UINT64 toValue,
00127                                Q_INT64 fromDate,   Q_INT64 toDate,
00128                               Q_UINT64 fromUnits, Q_UINT64 toUnits,
00129                               int position = -1 );
00130 
00136     void refreshPlaylist( bool force = false );
00137 
00138 };
00139 
00140 
00172 class ITunesDBPlaylist : public itunesdb::Playlist {
00173 
00174     friend class ITunesDB;
00175 
00176     ITunesDB& m_itunesdb;
00177     ITunesDBPlaylist( const ITunesDBPlaylist& );
00178     ITunesDBPlaylist( ITunesDB& itunesdb );
00179     ITunesDBPlaylist( ITunesDB& itunesdb, const itunesdb::Playlist& playlist );
00180     itunesdb::TrackComparatorPtr getTrackComparatorFor( Q_UINT32 sortOrder );
00181 
00185     void doPostParsingChecks();
00186 
00187 public:
00188 
00193     class Entry : public itunesdb::PlaylistItem {
00194         uint m_mhitTrackPos;
00195     public:
00196         Entry( Q_UINT32 id, uint trackpos );
00197         virtual ~Entry();
00198         void setMHITTrackPos( uint trackpos );
00199         uint getMHITTrackPos();
00200     };
00201 
00202     virtual ~ITunesDBPlaylist();
00203 
00207     virtual itunesdb::PlaylistItem * createNewItem( Q_UINT32 itemid );
00208 
00215     void updateSmartPlaylist( bool force = false );
00216 
00221     void setSortOrder( Sortorder sortOrder );
00222 
00226     itunesdb::Track * getTrackAt( uint position );
00227 
00228 protected:
00229 
00233     virtual void readNonStringMHOD( QDataStream& stream, Q_UINT32 type, Q_UINT32 blocklen );
00234 
00235     virtual void setDirty( bool dirtyFlag = true );
00236 
00237     virtual itunesdb::SmartPlaylistRuleSet * createNewSplRuleSet();
00238 
00239 };
00240 
00241 
00245 class ITunesDB : itunesdb::utils::NonCopyAble {
00246     class ItunesDBReaderAdapter;    // Adapter class for reading the itunesdb file
00247     class ItunesDBWriterAdapter;    // Adapter class for writing the itunesdb file
00248 
00249     // friends
00250     friend class ITunesDBPlaylist;
00251     friend class ITunesDBSPLRuleSet;
00252     friend class ITunesDBTrack;
00253 
00254     typedef QMap<Q_UINT32,uint> TrackIDMap;
00255     typedef std::vector<ITunesDBTrack *> TrackList;
00256 
00257     bool mDoDirtyFlagHandling;
00258     uint mInternalVersion;
00259 
00260     // FIXME implement shared PImpl thus making it copyable
00261 
00262 public:
00263 
00267     class TrackIterator {
00268     public:
00269         TrackIterator( const TrackList& trackList, itunesdb::TrackPredicate * predicate);
00270         ~TrackIterator();
00271 
00272         bool hasNext();
00273         ITunesDBTrack * next();
00274     private:
00275         void calcNext();
00276         TrackList::const_iterator m_pos;
00277         TrackList::const_iterator m_end;
00278         TrackList::const_iterator m_next;
00279         itunesdb::TrackPredicate * m_predicate;
00280     };
00281 
00282     QString error;
00283 
00284     ITunesDB( IPodSysInfo& ipodSysInfo );
00285 
00286     virtual ~ITunesDB();
00287 
00292     bool open();
00293 
00298     bool isOpen();
00299 
00305     bool writeDatabase(const QString& filename = QString());
00306 
00310     bool dbFileChanged() const;
00311 
00315     QString getFilename() const { return itunesdbfile.name(); }
00316 
00320     ITunesDBPlaylist * createNewPlaylist( const QString& title );
00321 
00325     ITunesDBTrack * createEmptyTrack( Q_UINT32 trackid );
00326 
00334     ITunesDBTrack * createNewTrack( const QString& fileExtension = QString::null );
00335 
00342     bool addTrack( ITunesDBTrack * track );
00343 
00349     ITunesDBTrack * getTrackByID( const Q_UINT32 id ) const;
00350 
00354     ITunesDBTrack * findTrackByDBID( const Q_UINT64 dbid ) const;
00355 
00361     int getMHITTrackPosFor( Q_UINT32 id ) const;
00362 
00368     ITunesDBTrack * getTrackAt( uint mhitTrackPos) const;
00369 
00377     ITunesDBTrack * findFirstTrackBy( itunesdb::TrackPredicate& predicate ) const;
00378 
00379 
00386     TrackIterator getTracksBy( itunesdb::TrackPredicate * predicate ) const;
00387 
00388 
00393     itunesdb::TrackPtrList * getTracksBy( itunesdb::TrackPredicate& predicate, itunesdb::TrackPtrList& buffer ) const;
00394 
00403     ITunesDBTrack * findTrack(const QString& artistname, const QString& albumname, const QString& title, Q_UINT32 trackNum = 0 ) const;
00404 
00409     QStringList * getArtists( QStringList& buffer ) const;
00410 
00414     QStringList * getAllAlbums( QStringList& buffer ) const;
00415 
00422     QStringList * getAlbumsByArtist( const QString& artistname, QStringList& buffer ) const;
00423 
00429     bool containsArtist( const QString& artistname ) const;
00430 
00437     bool containsAlbum( const QString& artistname, const QString& albumname ) const;
00438 
00444     itunesdb::TrackPtrList * getAllTracks( itunesdb::TrackPtrList& buffer ) const;
00445 
00449     TrackIterator getAllTracks() const;
00450 
00458     itunesdb::TrackPtrList *
00459         getAlbum( const QString& artistname, const QString& albumname, itunesdb::TrackPtrList& buffer ) const;
00460 
00461 
00468     itunesdb::TrackPtrList *
00469         getTracksByArtist( const QString& artistname, itunesdb::TrackPtrList& buffer ) const;
00470 
00471 
00479     itunesdb::TrackPtrList *
00480         getTracksContaining( const QString& someString, itunesdb::TrackPtrList& buffer, bool caseSensitive = true ) const;
00481 
00482 
00488     QStringList * getPlaylistTitles( QStringList& buffer ) const;
00489 
00495     ITunesDBPlaylist * getPlaylistByTitle(const QString& playlisttitle) const;
00496 
00502     ITunesDBPlaylist * getPlaylistByID( Q_UINT64 playlistID ) const;
00503 
00510     bool removeArtist(const QString& artistname);
00511 
00522     bool renameAlbum(const QString& artist, const QString& title, const QString& newartist, const QString& newtitle);
00523 
00527     bool addPlaylist( ITunesDBPlaylist * playlist );
00528 
00535     bool removePlaylist( const QString& title, bool delete_instance);
00536 
00540     void updateSmartPlaylists();
00541 
00549     Q_UINT32 removeTrack(Q_UINT32 trackid, bool delete_instance = true);
00550 
00554     void clear();
00555 
00560     bool isDirty() const;
00561 
00565     Q_UINT32 getNumTracks() const;
00566 
00570     Q_UINT32 getNumPlaylists() const;
00571 
00575     void lock(bool write_lock);
00576     void unlock();
00577     bool isLocked() const;
00578 
00579     uint lastModified() { return timestamp.toTime_t(); }
00580 
00584     void setTitle( const QString& title );
00585 
00589     const QString& getTitle() const { return m_Title; }
00590 
00591     bool hasPodcasts() const { return hasPodcastsFlag; }
00592 
00596     Q_UINT32 getNextFreeTrackID() const;
00597 
00601     Q_UINT64 getNextFreeDBID() const;
00602 
00609     QString getFileForPathInfo( QString pathinfo ) const;
00610 
00614     const QString& getIPodBase() const {
00615         return m_ipodSysInfo.getDeviceMountpoint();
00616     }
00617 
00618 protected:
00619 
00620     TrackIDMap m_TrackIdMap;
00621     TrackList m_TrackList;
00622     QString m_Title;
00623 
00627     class PlaylistContainer : public QPtrList<ITunesDBPlaylist> {
00628     public:
00629         PlaylistContainer() : QPtrList<ITunesDBPlaylist>() {}
00630         virtual ~PlaylistContainer() { clear(); }
00631         virtual int compareItems( QPtrCollection::Item s1, QPtrCollection::Item s2 ) {
00632             return (*(ITunesDBPlaylist*)s1).getTitle().lower().localeAwareCompare( (*(ITunesDBPlaylist*)s2).getTitle().lower() );
00633         }
00634     } * playlists;
00635 
00636     bool m_dirty;
00637     void setDirty( bool dirtyFlag = true );
00638 
00639     Q_UINT32 maxtrackid;
00640     Q_UINT64 mMaxDBID;
00641 
00642     ITunesDBTrack * insertTrackToDataBase( ITunesDBTrack * track );
00643 
00644 private:
00645 
00646     void removeFromAllPlaylists( Q_UINT32 trackid );
00647     IPodSysInfo& m_ipodSysInfo;
00648     QFile itunesdbfile;
00649     QFile itunessdfile;
00650     QDateTime timestamp;
00651 
00652     bool hasPodcastsFlag;
00653     bool locked;
00654 };
00655 
00656 
00657 #endif

Generated on Tue Dec 12 16:39:26 2006 for libqtpod.kdevelop by  doxygen 1.5.1