collectionlist.h 5.81 KB
Newer Older
1
/***************************************************************************
2
                          collectionlist.h  -  description
3
                             -------------------
4
    begin                : Fri Sep 13 2002
5
    copyright            : (C) 2002, 2003 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 <qdict.h>
25
#include <qclipboard.h>
26

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 52 53 54 55 56
    /**
     * A variety of unique value lists will be kept in the collection.  This
     * enum can be used as an index into those structures.
     */
    enum UniqueSetType { Artists = 0, Albums = 1, Genres = 2 };

57
    static CollectionList *instance();
58
    static void initialize(QWidget *parent, bool restoreOnLoad = true);
59

60 61 62 63 64 65 66 67 68 69 70
    /**
     * Returns a unique set of values associated with the type specified.
     */
    QStringList uniqueSet(UniqueSetType t) const { return m_uniqueSets[t].values(); }

    /**
     * This is similar to uniqueSet() but in this case only returns values for
     * which there are two or more occurances in the collection list.  This is
     * useful when grouping items.
     */
    QStringList viewModeItems(UniqueSetType t) const { return m_viewModeItems[t].values(); }
71

72
    CollectionListItem *lookup(const QString &file) { return m_itemsDict.find(file); }
73 74
    virtual PlaylistItem *createItem(const QFileInfo &file, 
				     const QString &absFilePath = QString::null, 
75 76
				     QListViewItem * = 0,
				     bool = false);
77

78 79
    void emitVisibleColumnsChanged() { emit signalVisibleColumnsChanged(); }

80
public slots:
81
    virtual void paste() { decode(kapp->clipboard()->data()); }
82
    virtual void clear();
Scott Wheeler's avatar
Scott Wheeler committed
83
    void slotCheckCache();
84 85 86

    void slotRemoveItem(const QString &file);
    void slotRefreshItem(const QString &file);
87 88
    
protected:
89
    CollectionList(QWidget *parent);
90
    virtual ~CollectionList();
91

92 93 94
    virtual void contentsDropEvent(QDropEvent *e);
    virtual void contentsDragMoveEvent(QDragMoveEvent *e);

95
    // These methods are used by CollectionListItem, which is a friend class.
96

97 98
    void addToDict(const QString &file, CollectionListItem *item) { m_itemsDict.replace(file, item); }
    void removeFromDict(const QString &file) { m_itemsDict.remove(file); }
99

100
    /**
101
     * Add a value to one of the unique value lists; use the UniqueSetType as a key.
102
     */
103
    void addUnique(UniqueSetType t, const QString &value);
104

105 106 107
    void addWatched(const QString &file) { m_dirWatch->addFile(file); }
    void removeWatched(const QString &file) { m_dirWatch->removeFile(file); }

108 109
signals:
    void signalCollectionChanged();
110

111 112 113 114 115 116 117 118 119
    /**
     * This is emitted when the set of columns that is visible is changed.
     *
     * \see Playlist::hideColumn()
     * \see Playlist::showColumn()
     * \see Playlsit::isColumnVisible()
     */
    void signalVisibleColumnsChanged();

120
private:
121 122 123 124 125 126
    /**
     * Just the size of the above enum to keep from hard coding it in several
     * locations.
     */
    static const int m_uniqueSetCount = 3;

127
    static CollectionList *m_list;
128
    QDict<CollectionListItem> m_itemsDict;
129
    KDirWatch *m_dirWatch;
130 131 132
    QValueVector<SortedStringList> m_viewModeItems;
    QValueVector<SortedStringList> m_uniqueSets;
    QValueVector<QString> m_uniqueSetLast;
133 134
};

135
class CollectionListItem : /* public QObject, */ public PlaylistItem
136
{
137 138 139 140
    friend class Playlist;
    friend class CollectionList;
    friend class PlaylistItem;

141
    /** 
Mirko Boehm's avatar
Mirko Boehm committed
142
     * Needs access to the destructor, even though the destructor isn't used by QDict.
143 144
     */
    friend class QDict<CollectionListItem>;
145

146
    // Q_OBJECT
147

148 149
public:
    virtual void refresh();
150

151
protected:
152
    CollectionListItem(const QFileInfo &file, const QString &path);
153 154
    virtual ~CollectionListItem();

155
    void addChildItem(PlaylistItem *child);
156
    void removeChildItem(PlaylistItem *child);
157 158 159 160 161 162

    /**
     * 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.
163 164 165
     *
     * Returns true if the item is now up to date (even if this required a refresh) or
     * false if the item is invalid.
166
     */
167
    bool checkCurrent();
168

169 170
    virtual CollectionListItem *collectionItem() { return this; }

171
private:
172
    bool m_shuttingDown;
173
    QString m_path;
174
    PlaylistItemList m_children;
175 176 177
};

#endif