playlist.h 10 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 "sortedstringlist.h"
Scott Wheeler's avatar
Scott Wheeler committed
28
#include "playlistsearch.h"
29 30

class KPopupMenu;
31
class KActionMenu;
32 33

class QEvent;
Scott Wheeler's avatar
Scott Wheeler committed
34 35 36 37 38 39
class QFileInfo;

class PlaylistSearch;

class PlaylistItem;
typedef QValueList<PlaylistItem *> PlaylistItemList;
40

41 42
typedef QValueList<Playlist *> PlaylistList;

43 44 45
class Playlist : public KListView
{
    Q_OBJECT
46

47
public:
48 49
    /**
     * Before creating a playlist directly, please see
50 51
     * PlaylistSplitter::createPlaylist().
     */
52
    Playlist(QWidget *parent, const QString &name = QString::null);
53

54 55
    /**
     * Before creating a playlist directly, please see
56 57
     * PlaylistSplitter::openPlaylist().
     */
58
    Playlist(const QFileInfo &playlistFile, QWidget *parent, const QString &name);
59

60 61
    virtual ~Playlist();

62 63 64
    /**
     * 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
65
     * name.
66
     */
67
    virtual void save();
68 69
    virtual void saveAs();
    virtual void refresh();
70
    virtual void clearItem(PlaylistItem *item, bool emitChanged = true);
71
    virtual void clearItems(const PlaylistItemList &items);
72

73 74
    /**
     * All of the (media) files in the list.
75
     */
76
    QStringList files() const;
77

78
    /**
79 80 81 82
     * Returns a list of all of the \e visible items in the playlist.
     */
    PlaylistItemList items();

83
    /**
84
     * Returns a list of all of the items in the playlist.
85
     */
86
    PlaylistItemList visibleItems() const;
87

88 89 90
    /**
     * Returns a list of the currently selected items.
     */
91
    PlaylistItemList selectedItems() const;
92

93 94 95 96 97
    /**
     * Returns a list of the last 10 played items.
     */
    PlaylistItemList historyItems(PlaylistItem *current, bool random) const;

98 99
    /**
     * Allow duplicate files in the playlist.
100
     */
101 102
    void setAllowDuplicates(bool allow);

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

113 114
    void createItems(const PlaylistItemList &siblings);

115 116
    QString fileName() const { return m_fileName; }
    void setFileName(const QString &n) { m_fileName = n; }
117

118 119 120 121
    void hideColumn(int c);
    void showColumn(int c);
    bool isColumnVisible(int c) const;

122
    /**
123
     * If m_playlistName has no value -- i.e. the name has not been set to
124
     * something other than the filename, this returns the filename less the
125
     * extension.  If m_playlistName does have a value, this returns that.
126
     */
127
    QString name() const;
128

129 130
    /**
     * This sets a name for the playlist that is \e different from the file name.
131
     */
132
    void setName(const QString &n);
133

134
    int count() const { return childCount(); }
135

136
    /**
137
     * This gets the next item to be played.
138
     */
139 140
    PlaylistItem *nextItem(PlaylistItem *current, bool random = false);
    PlaylistItem *previousItem(PlaylistItem *current, bool random = false);
141

142
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
143

144 145 146 147 148 149 150 151
    /**
     * 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.
     */
Scott Wheeler's avatar
Scott Wheeler committed
152
    bool playing() const;
153

Scott Wheeler's avatar
Scott Wheeler committed
154 155 156
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
157
     */
Scott Wheeler's avatar
Scott Wheeler committed
158
    void updateLeftColumn();
159

160 161
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

Scott Wheeler's avatar
Scott Wheeler committed
162 163 164
    PlaylistSearch search() const { return m_search; }
    void setSearch(const PlaylistSearch &s) { m_search = s; }

165 166
    void emitNumberOfItemsChanged() { emit signalNumberOfItemsChanged(this); }

167
public slots:
168
    /**
169
     * Remove the currently selected items from the playlist and disk.
170
     */
171
    void slotDeleteSelectedItems() { deleteFromDisk(selectedItems()); };
172 173
    void slotSetNext();

174 175 176 177
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
178
    virtual void cut() { copy(); clear(); }
179
    virtual void copy();
180
    virtual void paste();
181
    virtual void clear();
182
    virtual void selectAll() { KListView::selectAll(true); }
183

184 185
    void slotGuessTagInfoFile();
    void slotGuessTagInfoInternet();
186 187
    void slotRenameFile();

188 189 190
    /**
     * Reload the playlist contents from the m3u file.
     */
191
    virtual void slotReload();
192

193
protected:
194 195 196 197 198 199
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
    void deleteFromDisk(const PlaylistItemList &items);

200
    virtual bool eventFilter(QObject* watched, QEvent* e);
201
    virtual QDragObject *dragObject(QWidget *parent);
202
    virtual QDragObject *dragObject() { return dragObject(this); }
203
    virtual bool canDecode(QMimeSource *s);
204
    virtual void decode(QMimeSource *s);
205 206
    virtual void contentsDropEvent(QDropEvent *e);
    virtual void contentsDragMoveEvent(QDragMoveEvent *e);
Scott Wheeler's avatar
Scott Wheeler committed
207
    virtual void showEvent(QShowEvent *e);
208

209 210 211 212
    /**
     * Though it's somewhat obvious, this function will stat the file, so only use it when
     * you're out of a performance critical loop.
     */
213
    static QString resolveSymLinks(const QFileInfo &file);
Scott Wheeler's avatar
Scott Wheeler committed
214

215 216 217
    KPopupMenu *rmbMenu() { return m_rmbMenu; }
    const KPopupMenu *rmbMenu() const { return m_rmbMenu; }

218 219
    virtual void polish();

220
signals:
221 222 223
    /**
     * This signal is connected to PlaylistItem::refreshed() in the
     * PlaylistItem class.
224
     */
225
    void signalDataChanged();
226

227
    /**
228
     * This is emitted when the playlist selection is changed.  This is used
229
     * primarily to notify the TagEditor of the new data.
230
     */
231
    void signalSelectionChanged(const PlaylistItemList &selection);
232

233
    /**
234
     * This is connected to the PlaylistBox::Item to let it know when the
235 236
     * playlist's name has changed.
     */
237
    void signalNameChanged(const QString &fileName);
238

239 240 241
    /**
     * This signal is emited when items are added to or removed from the list.
     */
242 243
    void signalNumberOfItemsChanged(Playlist *);
    void signalDoubleClicked();
244

245 246 247 248 249 250 251
    /**
     * This is the union of signalDataChanged() and signalNumberOfItemsChanged().
     * It is emited with either quantity or value of the PlaylistItems are
     * changed.
     */
    void signalChanged();

252
    /**
253
     * This signal is emitted just before a playlist item is removed from the
254 255
     * list.
     */
256
    void signalAboutToRemove(PlaylistItem *item);
257
    void signalFilesDropped(const QStringList &files, Playlist *);
258
    void signalSetNext(PlaylistItem *item);
259
    void signalVisibleColumnsChanged();
260

261 262
private:
    void setup();
263
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
264 265 266
    /**
     * Save the tag for an individual items.
     */
267
    void applyTag(QListViewItem *item, const QString &text, int column);
268
    int leftMostVisibleColumn() const;
269

270 271 272 273 274 275
    /**
     * 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;

276
private slots:
277 278
    void slotEmitSelected() { emit signalSelectionChanged(selectedItems()); }
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
279 280 281 282 283 284 285 286 287 288

    /**
     * This slot applys the tag for 
     */
    void slotApplyModification(QListViewItem *, const QString &text, int column);

    /**
     * This starts the renaming process by displaying a line edit if the mouse is in 
     * an appropriate position.
     */
289
    void slotRenameTag();
290
    void slotColumnOrderChanged(int, int from, int to);
291
    void slotToggleColumnVisible(int column);
292 293

private:
294
    SortedStringList m_members;
295
    int m_currentColumn;
296 297
    int m_processed;
    bool m_allowDuplicates;
298 299 300 301 302
    /**
     * 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;
Scott Wheeler's avatar
Scott Wheeler committed
303
    PlaylistItemList m_history;
304

305
    QString m_fileName;
306

307
    /**
308
     * Used to store the text for inline editing before it is changed so that
309 310 311 312
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
313 314 315 316 317

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

320 321 322
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
323

324 325
    int m_rmbPasteID;
    int m_rmbEditID;
326

327 328
    static PlaylistItem *m_playingItem;
    static int m_leftColumn;
329 330

    PlaylistItemList m_randomList;
Scott Wheeler's avatar
Scott Wheeler committed
331
    PlaylistSearch m_search;
332 333

    bool m_polished;
334 335
};

336 337 338
QDataStream &operator<<(QDataStream &s, const Playlist &p);
QDataStream &operator>>(QDataStream &s, Playlist &p);

339
#endif