collectionlist.h 4.83 KB
Newer Older
1
/***************************************************************************
2
                          collectionlist.h  -  description
3
                             -------------------
4
    begin                : Fri Sep 13 2002
5
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
 ***************************************************************************/
8 9 10 11 12 13 14 15 16 17

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

18 19 20
#ifndef COLLECTIONLIST_H
#define COLLECTIONLIST_H

21
#include <kapplication.h>
22
#include <kdirwatch.h>
23 24

#include <qclipboard.h>
25 26
#include <qdict.h>

27
#include "playlist.h"
28
#include "playlistitem.h"
29
#include "sortedstringlist.h"
30

31 32
class CollectionListItem;

33 34 35 36 37 38 39 40 41 42
/** 
 * This is the "collection", or all of the music files that have been opened
 * in any playlist and not explicitly removed from the collection.
 *
 * It is being implemented as a "semi-singleton" because I need universal access
 * to just one instance.  However, because the collection needs initialization 
 * parameters (that will not always be available when an instance is needed).  
 * Hence there will be the familiar singleton "instance()" method allong with an
 * "initialize()" method.
 */
43

44
class CollectionList : public Playlist
45
{
46 47
    friend class CollectionListItem;

48
    Q_OBJECT
49

50
public: 
51
    static CollectionList *instance();
52
    static void initialize(QWidget *parent, bool restoreOnLoad = true);
53

54 55
    QStringList artists() const { return m_artists.values(); }
    QStringList albums() const { return m_albums.values(); }
56

57
    CollectionListItem *lookup(const QString &file) { return m_itemsDict.find(file); }
58
    virtual PlaylistItem *createItem(const QFileInfo &file, QListViewItem *);
59 60

public slots:
61
    virtual void paste() { decode(kapp->clipboard()->data()); }
62
    virtual void clear();
Scott Wheeler's avatar
Scott Wheeler committed
63
    void slotCheckCache();
64 65
    
protected:
66
    CollectionList(QWidget *parent);
67
    virtual ~CollectionList();
68

69
    virtual void decode(QMimeSource *s);
70 71 72
    virtual void contentsDropEvent(QDropEvent *e);
    virtual void contentsDragMoveEvent(QDragMoveEvent *e);

73
    // These methods are used by CollectionListItem, which is a friend class.
74 75
    void addToDict(const QString &file, CollectionListItem *item) { m_itemsDict.replace(file, item); }
    void removeFromDict(const QString &file) { m_itemsDict.remove(file); }
76 77 78 79 80 81

    /** 
     * This checks to see if the artist given is in the artist list maintained
     * by the collection list (for use in autocompletion and the TagEditor 
     * combo boxes), and if it is not, it adds it to the list.
     */
82
    void addArtist(const QString &artist);
83 84 85 86

    /** 
     * This is similar to addArtist(), but is for album names. 
     */
87
    void addAlbum(const QString &album);
88

89
    void emitNumberOfItemsChanged() { emit signalNumberOfItemsChanged(this); }
90

91 92 93
    void addWatched(const QString &file) { m_dirWatch->addFile(file); }
    void removeWatched(const QString &file) { m_dirWatch->removeFile(file); }

94 95
signals:
    void signalCollectionChanged();
96
    void signalRequestPlaylistCreation(const PlaylistItemList &items);
97 98 99 100

private slots:
    void slotRemoveItem(const QString &file);
    void slotRefreshItem(const QString &file);
101
    void slotCreateGroup();
102
    
103
private:
104
    static CollectionList *m_list;
105
    QDict<CollectionListItem> m_itemsDict;
106 107
    SortedStringList m_artists;
    SortedStringList m_albums;
108
    KDirWatch *m_dirWatch;
109 110 111 112
};

class CollectionListItem : public PlaylistItem
{
113 114 115 116
    friend class Playlist;
    friend class CollectionList;
    friend class PlaylistItem;

117 118 119 120
    /** 
     * Needs access to the destuctor, even though the destructor isn't used by QDict.
     */
    friend class QDict<CollectionListItem>;
121

122
    Q_OBJECT
123

124
public slots:
125
    virtual void slotRefresh();
126

127
protected:
128
    CollectionListItem(const QFileInfo &file, const QString &path);
129 130
    virtual ~CollectionListItem();

131
    void addChildItem(PlaylistItem *child);
132 133 134 135 136 137 138

    /**
     * This slot, called from a QTimer::singleShot() set in the constructor, allows for
     * delayed consistancy checking for the cache at the cost of a few CPU cycles.  The
     * effect however is that stating files is delayed until after the GUI is shown by
     * moving this action into the event loop.
     */
Scott Wheeler's avatar
Scott Wheeler committed
139
    void checkCurrent();
140

141 142
    virtual CollectionListItem *collectionItem() { return this; }

143 144
private:
    QString m_path;
145 146 147
};

#endif