playlist.h 13.9 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
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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>
22
#include <kurldrag.h>
23 24

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

28
#include "sortedstringlist.h"
Scott Wheeler's avatar
Scott Wheeler committed
29
#include "playlistsearch.h"
30
#include "tagguesser.h"
31 32

class KPopupMenu;
33
class KActionMenu;
34 35

class QEvent;
Scott Wheeler's avatar
Scott Wheeler committed
36 37 38 39 40 41
class QFileInfo;

class PlaylistSearch;

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

43 44
typedef QValueList<Playlist *> PlaylistList;

45 46 47
class Playlist : public KListView
{
    Q_OBJECT
48

49
public:
50

51 52
    /**
     * Before creating a playlist directly, please see
53 54
     * PlaylistSplitter::createPlaylist().
     */
55
    Playlist(QWidget *parent, const QString &name = QString::null);
56

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

63 64
    virtual ~Playlist();

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

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

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

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

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

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

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

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

115 116
    void createItems(const PlaylistItemList &siblings);

117 118 119 120
    /**
     * Returns the file name associated with this playlist (an m3u file) or
     * QString::null if no such file exists.
     */
121
    QString fileName() const { return m_fileName; }
122 123 124 125 126

    /**
     * Sets the file name to be associated with this playlist; this file should
     * have the "m3u" extension.
     */
127
    void setFileName(const QString &n) { m_fileName = n; }
128

129 130 131 132
    void hideColumn(int c);
    void showColumn(int c);
    bool isColumnVisible(int c) const;

133
    /**
134
     * If m_playlistName has no value -- i.e. the name has not been set to
135
     * something other than the filename, this returns the filename less the
136
     * extension.  If m_playlistName does have a value, this returns that.
137
     */
138
    QString name() const;
139

140 141
    /**
     * This sets a name for the playlist that is \e different from the file name.
142
     */
143
    void setName(const QString &n);
144

145 146 147
    /**
     * Returns the number of items in the playlist.
     */
148
    int count() const { return childCount(); }
149

150
    /**
151 152 153 154 155
     * Returns the next item to be played.  If random is false this is just
     * the next item in the playlist (or null if the current items is the last
     * item in the list).  If random is true, then it will select an item at
     * random from this list (and try to be a bit clever about it to not repeat
     * items before everything has been played at least once).
156
     */
157
    PlaylistItem *nextItem(PlaylistItem *current, bool random = false);
158 159 160 161 162 163

    /**
     * Returns the item played before the currently playing item.  If random is
     * false, this is simply the item above the currently playing item in the
     * list.  If random is true this checks the history of recently played items.
     */
164
    PlaylistItem *previousItem(PlaylistItem *current, bool random = false);
165

166 167 168 169
    /**
     * Returns the KActionMenu that allows this to be embedded in menus outside
     * of the playlist.
     */
170
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
171

172 173 174 175 176 177 178 179
    /**
     * 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
180
    bool playing() const;
181

Scott Wheeler's avatar
Scott Wheeler committed
182 183 184
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
185
     */
Scott Wheeler's avatar
Scott Wheeler committed
186
    void updateLeftColumn();
187

188 189 190 191
    /**
     * Sets the items in the list to be either visible based on the value of
     * visible.  This is useful for search operations and such.
     */
192 193
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

194 195 196 197
    /**
     * Returns the search associated with this list, or an empty search if one
     * has not yet been set.
     */
Scott Wheeler's avatar
Scott Wheeler committed
198
    PlaylistSearch search() const { return m_search; }
199 200 201 202 203 204

    /**
     * Set the search associtated with this playlist.
     *
     * \note This does not cause the search to be rerun.
     */
Scott Wheeler's avatar
Scott Wheeler committed
205 206
    void setSearch(const PlaylistSearch &s) { m_search = s; }

207 208 209 210 211 212 213 214
    /**
     * Emits a signal indicating that the number of items have changed.  This
     * is useful in conjunction with createItem() where emitChanged is false.
     *
     * In many situations it is not practical for speed reasons to trigger the
     * actions associated with signalCountChanged() after each insertion.
     */
    void emitCountChanged() { emit signalCountChanged(this); }
215

216
public slots:
217
    /**
218
     * Remove the currently selected items from the playlist and disk.
219
     */
220
    void slotRemoveSelectedItems() { removeFromDisk(selectedItems()); };
221 222
    void slotSetNext();

223 224 225 226
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
227
    virtual void cut() { copy(); clear(); }
228
    virtual void copy();
229
    virtual void paste();
230
    virtual void clear();
231
    virtual void selectAll() { KListView::selectAll(true); }
232

233 234 235 236 237 238
    /**
     * Refreshes the tags of the selection from disk, or all of the files in the
     * list if there is no selection.
     */
    virtual void slotRefresh();

239
    void slotGuessTagInfo(TagGuesser::Type type);
240 241
    void slotRenameFile();

242 243 244
    /**
     * Reload the playlist contents from the m3u file.
     */
245
    virtual void slotReload();
246

247
protected:
248 249 250 251
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
252
    void removeFromDisk(const PlaylistItemList &items);
253

254 255 256
    // the following are all reimplemented from base classes

    virtual bool eventFilter(QObject *watched, QEvent *e);
257
    virtual QDragObject *dragObject(QWidget *parent);
258
    virtual QDragObject *dragObject() { return dragObject(this); }
259
    virtual bool canDecode(QMimeSource *s);
260
    virtual void decode(QMimeSource *s, PlaylistItem *after = 0);
261
    virtual void contentsDropEvent(QDropEvent *e);
Scott Wheeler's avatar
Scott Wheeler committed
262
    virtual void showEvent(QShowEvent *e);
263
    virtual bool acceptDrag(QDropEvent *e) const { return KURLDrag::canDecode(e); }
264
    virtual void polish();
265

266 267 268 269
    /**
     * Though it's somewhat obvious, this function will stat the file, so only use it when
     * you're out of a performance critical loop.
     */
270
    static QString resolveSymLinks(const QFileInfo &file);
Scott Wheeler's avatar
Scott Wheeler committed
271

272
signals:
273
    /**
274
     * This is emitted when the playlist selection is changed.  This is used
275
     * primarily to notify the TagEditor of the new data.
276
     */
277
    void signalSelectionChanged(const PlaylistItemList &selection);
278
    void signalDoubleClicked();
279

280
    /**
281
     * This is connected to the PlaylistBox::Item to let it know when the
282 283
     * playlist's name has changed.
     */
284
    void signalNameChanged(const QString &fileName);
285

286
    /**
287 288 289 290
     * This signal is emitted when items are added to or removed from the list.
     *
     * \see signalDataChanged()
     * \see signalChanged()
291
     */
292
    void signalCountChanged(Playlist *);
293

294
    /**
295 296 297 298 299 300 301 302 303 304 305
     * This signal is connected to PlaylistItem::refreshed() in the PlaylistItem
     * class.  It is emitted when a playlist item's data has been changed.
     *
     * \see signalCountChanged()
     * \see signalChanged()
     */
    void signalDataChanged();

    /**
     * This is the union of signalDataChanged() and signalCountChanged().
     * It is emitted with either quantity or value of the PlaylistItems are
306 307 308 309
     * changed.
     */
    void signalChanged();

310
    /**
311
     * This signal is emitted just before a playlist item is removed from the
312 313 314
     * list allowing for any cleanup that needs to happen.  Typically this
     * is used to remove the item from the history and safeguard against
     * dangling pointers.
315
     */
316
    void signalAboutToRemove(PlaylistItem *item);
317 318 319 320

    /**
     * This is emitted when \a files are dropped on a specific playlist.
     */
321
    void signalFilesDropped(const QStringList &files, Playlist *, PlaylistItem *after);
322 323 324 325 326

    /**
     * Set the next item to be played in the current playlist.  This is used by
     * the "Play Next" feature.
     */
327
    void signalSetNext(PlaylistItem *item);
328 329 330 331 332 333 334 335

    /**
     * This is emitted when the set of columns that is visible is changed.
     *
     * \see hideColumn()
     * \see showColumn()
     * \see isColumnVisible()
     */
336
    void signalVisibleColumnsChanged();
337

338 339 340 341 342
    /**
     * Request creation of a playlist based on \a items.
     */
    void signalCreatePlaylist(const PlaylistItemList &items);

343 344
private:
    void setup();
345 346 347 348 349 350 351

    /**
     * Load the playlist from a file.  \a fileName should be the absolute path.
     * \a fileInfo should point to the same file as \a fileName.  This is a
     * little awkward API-wise, but keeps us from throwing away useful
     * information.
     */
352
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
353

354
    /**
355
     * Save the tag for an individual item.
356
     */
357
    void applyTag(QListViewItem *item, const QString &text, int column);
358 359 360 361 362 363

    /**
     * Returns the index of the left most visible column in the playlist.
     *
     * \see isColumnVisible()
     */
364
    int leftMostVisibleColumn() const;
365

366 367 368 369 370 371
    /**
     * 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;

372
private slots:
373 374 375 376 377

    /**
     * This is just used to emit the selection as a list of PlaylistItems when
     * the selection changes.
     */
378
    void slotEmitSelected() { emit signalSelectionChanged(selectedItems()); }
379 380 381 382 383

    /**
     * Show the RMB menu.  Matches the signature for the signal 
     * QListView::contextMenuRequested().
     */
384
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
385 386

    /**
387 388 389
     * This slot applys the tag for a specific item.
     *
     * \see applyTag()
390 391 392 393 394 395 396
     */
    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.
     */
397
    void slotRenameTag();
398 399 400 401 402

    /**
     * Moves the column \a from to the position \a to.  This matches the signature
     * for the signal QHeader::indexChange().
     */
403
    void slotColumnOrderChanged(int, int from, int to);
404 405 406 407 408 409 410

    /**
     * Toggles a columns visible status.  Useful for KActions.
     *
     * \see hideColumn()
     * \see showColumn()
     */
411
    void slotToggleColumnVisible(int column);
412

413 414 415 416 417
    /**
     * Prompts the user to create a new playlist with from the selected items.
     */
    void slotCreateGroup() { emit signalCreatePlaylist(selectedItems()); }

418
private:
419
    SortedStringList m_members;
420
    int m_currentColumn;
421 422
    int m_processed;
    bool m_allowDuplicates;
423 424 425 426 427
    /**
     * 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
428
    PlaylistItemList m_history;
429

430
    QString m_fileName;
431

432
    /**
433
     * Used to store the text for inline editing before it is changed so that
434 435 436 437
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
438 439 440 441 442

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

445 446 447
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
448

449 450
    int m_rmbPasteID;
    int m_rmbEditID;
451

452 453
    static PlaylistItem *m_playingItem;
    static int m_leftColumn;
454 455

    PlaylistItemList m_randomList;
Scott Wheeler's avatar
Scott Wheeler committed
456
    PlaylistSearch m_search;
457 458

    bool m_polished;
459 460
};

461 462 463
QDataStream &operator<<(QDataStream &s, const Playlist &p);
QDataStream &operator>>(QDataStream &s, Playlist &p);

464
#endif