playlist.h 9.23 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          playlist.h  -  description
                             -------------------
    begin                : Sat Feb 16 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <klistview.h>

#include <qstringlist.h>
24
#include <qvaluevector.h>
25
#include <qptrstack.h>
26 27

#include "playlistitem.h"
28
#include "sortedstringlist.h"
29

30 31 32
class KPopupMenu;

class KPopupMenu;
33
class KActionMenu;
34 35

class QEvent;
36

37 38
typedef QValueList<Playlist *> PlaylistList;

39 40 41
class Playlist : public KListView
{
    Q_OBJECT
42

43
public:
44 45
    /**
     * Before creating a playlist directly, please see
46 47
     * PlaylistSplitter::createPlaylist().
     */
48
    Playlist(QWidget *parent, const QString &name = QString::null);
49

50 51
    /**
     * Before creating a playlist directly, please see
52 53
     * PlaylistSplitter::openPlaylist().
     */
54
    Playlist(const QFileInfo &playlistFile, QWidget *parent, const char *name = 0);
55

56 57
    virtual ~Playlist();

58 59 60
    /**
     * Saves the file to the currently set file name.  If there is no filename
     * currently set, the default behavior is to prompt the user for a file
61
     * name.
62
     */
63
    virtual void save();
64 65
    virtual void saveAs();
    virtual void refresh();
66
    virtual void clearItem(PlaylistItem *item, bool emitChanged = true);
67
    virtual void clearItems(const PlaylistItemList &items);
68

69 70
    /**
     * All of the (media) files in the list.
71
     */
72
    QStringList files() const;
73

74
    /**
75 76 77 78
     * Returns a list of all of the \e visible items in the playlist.
     */
    PlaylistItemList items();

79
    /**
80
     * Returns a list of all of the items in the playlist.
81
     */
82
    PlaylistItemList visibleItems() const;
83

84 85 86
    /**
     * Returns a list of the currently selected items.
     */
87
    PlaylistItemList selectedItems() const;
88

89 90 91 92 93
    /**
     * Returns a list of the last 10 played items.
     */
    PlaylistItemList historyItems(PlaylistItem *current, bool random) const;

94 95
    /**
     * Allow duplicate files in the playlist.
96
     */
97 98
    void setAllowDuplicates(bool allow);

99
    /**
100
     * This is being used as a mini-factory of sorts to make the construction
101 102
     * of PlaylistItems virtual.  In this case it allows for the creation of
     * both PlaylistItems and CollectionListItems.
103
     */
104 105
    virtual PlaylistItem *createItem(const QFileInfo &file,
				     const QString &absFilePath = QString::null,
106 107
				     QListViewItem *after = 0);

108 109
    void createItems(const PlaylistItemList &siblings);

110 111
    QString fileName() const { return m_fileName; }
    void setFileName(const QString &n) { m_fileName = n; }
112

113 114 115 116
    void hideColumn(int c);
    void showColumn(int c);
    bool isColumnVisible(int c) const;

117
    /**
118
     * If m_playlistName has no value -- i.e. the name has not been set to
119
     * something other than the filename, this returns the filename less the
120
     * extension.  If m_playlistName does have a value, this returns that.
121
     */
122
    QString name() const;
123

124 125
    /**
     * This sets a name for the playlist that is \e different from the file name.
126
     */
127
    void setName(const QString &n);
128

129
    int count() const { return childCount(); }
130

131
    /**
132
     * This gets the next item to be played.
133
     */
134 135
    PlaylistItem *nextItem(PlaylistItem *current, bool random = false);
    PlaylistItem *previousItem(PlaylistItem *current, bool random = false);
136

137
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
138

139 140 141 142 143 144 145 146 147
    /**
     * Set item to be the playing item; also set this list to be the playing list.
     */
    static void setPlaying(PlaylistItem *item, bool p = true);

    /**
     * Returns true if this playlist is currently playing.
     */
    bool playing() const { return m_playingItem && this == static_cast<Playlist *>(m_playingItem->listView()); }
148

Scott Wheeler's avatar
Scott Wheeler committed
149 150 151
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
152
     */
Scott Wheeler's avatar
Scott Wheeler committed
153
    void updateLeftColumn();
154

155 156
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

157
public slots:
158
    /**
159
     * Remove the currently selected items from the playlist and disk.
160
     */
161
    void slotDeleteSelectedItems() { deleteFromDisk(selectedItems()); };
162 163
    void slotSetNext();

164 165 166 167
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
168
    virtual void cut() { copy(); clear(); }
169
    virtual void copy();
170
    virtual void paste();
171
    virtual void clear();
172
    virtual void selectAll() { KListView::selectAll(true); }
173

174 175
    void slotGuessTagInfoFile();
    void slotGuessTagInfoInternet();
176 177
    void slotRenameFile();

178 179 180 181
    /**
     * Reload the playlist contents from the m3u file.
     */
    void slotReload();
182

183
protected:
184 185 186 187 188 189
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
    void deleteFromDisk(const PlaylistItemList &items);

190
    virtual bool eventFilter(QObject* watched, QEvent* e);
191
    virtual QDragObject *dragObject(QWidget *parent);
192
    virtual QDragObject *dragObject() { return dragObject(this); }
193
    virtual bool canDecode(QMimeSource *s);
194
    virtual void decode(QMimeSource *s);
195 196
    virtual void contentsDropEvent(QDropEvent *e);
    virtual void contentsDragMoveEvent(QDragMoveEvent *e);
Scott Wheeler's avatar
Scott Wheeler committed
197
    virtual void showEvent(QShowEvent *e);
198

199 200 201 202
    /**
     * Though it's somewhat obvious, this function will stat the file, so only use it when
     * you're out of a performance critical loop.
     */
203
    static QString resolveSymLinks(const QFileInfo &file);
Scott Wheeler's avatar
Scott Wheeler committed
204

205 206 207
    KPopupMenu *rmbMenu() { return m_rmbMenu; }
    const KPopupMenu *rmbMenu() const { return m_rmbMenu; }

208
signals:
209 210 211
    /**
     * This signal is connected to PlaylistItem::refreshed() in the
     * PlaylistItem class.
212
     */
213
    void signalDataChanged();
214

215
    /**
216
     * This is emitted when the playlist selection is changed.  This is used
217
     * primarily to notify the TagEditor of the new data.
218
     */
219
    void signalSelectionChanged(const PlaylistItemList &selection);
220

221
    /**
222
     * This is connected to the PlaylistBox::Item to let it know when the
223 224
     * playlist's name has changed.
     */
225
    void signalNameChanged(const QString &fileName);
226

227
    void signalNumberOfItemsChanged(Playlist *);
228

229
    void signalDoubleClicked();
230

231
    /**
232
     * This signal is emitted just before a playlist item is removed from the
233 234
     * list.
     */
235
    void signalAboutToRemove(PlaylistItem *item);
236
    void signalFilesDropped(const QStringList &files, Playlist *);
237
    void signalSetNext(PlaylistItem *item);
238
    void signalVisibleColumnsChanged();
239

240 241
private:
    void setup();
242
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
243
    void applyTag(QListViewItem *item, const QString &text, int column);
244
    int leftMostVisibleColumn() const;
245

246 247 248 249 250 251
    /**
     * This class is used internally to store settings that are shared by all
     * of the playlists, such as column order.  It is implemented as a singleton.
     */
    class SharedSettings;

252
private slots:
253 254 255
    void slotEmitSelected() { emit signalSelectionChanged(selectedItems()); }
    void slotEmitDoubleClicked(QListViewItem *) { emit signalDoubleClicked(); }
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
256
    void slotApplyModification(QListViewItem *item, const QString &text, int column);
257
    void slotRenameTag();
258
    void slotColumnOrderChanged(int, int from, int to);
259
    void slotToggleColumnVisible(int column);
260 261

private:
262
    SortedStringList m_members;
263
    int m_currentColumn;
264 265
    int m_processed;
    bool m_allowDuplicates;
266 267 268 269 270
    /**
     * This is used to indicate if the list of visible items has changed (via a 
     * call to setVisibleItems()) while random play is playing.
     */
    static bool m_visibleChanged;
271
    QPtrList<PlaylistItem> m_history;
272

273
    QString m_fileName;
274

275
    /**
276
     * Used to store the text for inline editing before it is changed so that
277 278 279 280
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
281 282 283 284 285

    /**
     * This is only defined if the playlist name is something other than the
     * file name.
     */
286
    QString m_playlistName;
287

288 289 290
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
291

292 293
    int m_rmbPasteID;
    int m_rmbEditID;
294

295 296
    static PlaylistItem *m_playingItem;
    static int m_leftColumn;
297 298

    PlaylistItemList m_randomList;
299 300
};

301 302 303
QDataStream &operator<<(QDataStream &s, const Playlist &p);
QDataStream &operator>>(QDataStream &s, Playlist &p);

304
#endif