playlist.h 21.5 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>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
23
#include <kdebug.h>
24
#include <kglobalsettings.h>
25 26

#include <qstringlist.h>
27
#include <qvaluevector.h>
28
#include <qptrstack.h>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
29
#include <qfileinfo.h>
30

31
#include "stringhash.h"
Scott Wheeler's avatar
Scott Wheeler committed
32
#include "playlistsearch.h"
33
#include "tagguesser.h"
34 35

class KPopupMenu;
36
class KActionMenu;
37 38

class QEvent;
Scott Wheeler's avatar
Scott Wheeler committed
39 40 41 42 43

class PlaylistSearch;

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

45 46
typedef QValueList<Playlist *> PlaylistList;

47 48 49
class Playlist : public KListView
{
    Q_OBJECT
50

51
public:
52

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

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

65 66
    virtual ~Playlist();

67 68 69
    /**
     * 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
70
     * name.
71
     */
72
    virtual void save();
Scott Wheeler's avatar
Scott Wheeler committed
73 74 75 76 77

    /**
     * Standard "save as".  Prompts the user for a location where to save the
     * playlist to.
     */
78
    virtual void saveAs();
Scott Wheeler's avatar
Scott Wheeler committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92

    /**
     * Removes \a item from the Playlist, but not from the disk.  If
     * \a emitChanged is true this will also notify relevant classes
     * that the content of the list has changed.
     *
     * In some situations, for instance when removing items in a loop, it is
     * preferable to delay this notification until after other operations have
     * completed.  In those cases set \a emitChanged to false and call the
     * signal directly.
     *
     * @see signalCountChanged()
     * @see emitCountChanged()
     */
93
    virtual void clearItem(PlaylistItem *item, bool emitChanged = true);
Scott Wheeler's avatar
Scott Wheeler committed
94 95 96 97 98

    /**
     * Remove \a items from the playlist and emit a signal indicating
     * that the number of items in the list has changed.
     */
99
    virtual void clearItems(const PlaylistItemList &items);
100

101 102
    /**
     * All of the (media) files in the list.
103
     */
104
    QStringList files();
105

106
    /**
107 108
     * Returns a list of all of the \e visible items in the playlist.
     */
109
    virtual PlaylistItemList items();
110

111
    /**
112
     * Returns a list of all of the items in the playlist.
113
     */
114
    PlaylistItemList visibleItems();
115

116 117 118
    /**
     * Returns a list of the currently selected items.
     */
119
    PlaylistItemList selectedItems();
120

121 122 123 124 125
    /**
     * Returns a list of the last 10 played items.
     */
    PlaylistItemList historyItems(PlaylistItem *current, bool random) const;

126 127
    /**
     * Allow duplicate files in the playlist.
128
     */
129
    void setAllowDuplicates(bool allow) { m_allowDuplicates = allow; }
130

131
    /**
132
     * This is being used as a mini-factory of sorts to make the construction
133 134
     * of PlaylistItems virtual.  In this case it allows for the creation of
     * both PlaylistItems and CollectionListItems.
135
     */
136 137
    virtual PlaylistItem *createItem(const QFileInfo &file,
				     const QString &absFilePath = QString::null,
138 139
				     QListViewItem *after = 0,
				     bool emitChanged = true);
140

Scott Wheeler's avatar
Scott Wheeler committed
141 142 143 144 145 146 147
    /**
     * This is implemented as a template method to allow subclasses to
     * instantiate their PlaylistItem subclasses using the same method.  Some
     * of the types here are artificially templatized (i.e. CollectionListType and
     * CollectionItemType) to avoid recursive includes, but in fact will always
     * be the same.     
     */
148 149 150 151 152 153
    template <class ItemType, class CollectionItemType, class CollectionListType>
    ItemType *createItem(const QFileInfo &file,
			 const QString &absFilePath = QString::null,
			 QListViewItem *after = 0,
			 bool emitChanged = true);

154
    virtual void createItems(const PlaylistItemList &siblings);
155

156 157 158 159
    /**
     * Returns the file name associated with this playlist (an m3u file) or
     * QString::null if no such file exists.
     */
160
    QString fileName() const { return m_fileName; }
161 162 163 164 165

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

168 169 170 171 172 173 174 175 176 177 178 179 180
    /**
     * Hides column \a c.  If \a emitChanged is true then a signal that the
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
    void hideColumn(int c, bool emitChanged = true);

    /**
     * Shows column \a c.  If \a emitChanged is true then a signal that the
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
    void showColumn(int c, bool emitChanged = true);
181 182
    bool isColumnVisible(int c) const;

183
    /**
184
     * If m_playlistName has no value -- i.e. the name has not been set to
185
     * something other than the filename, this returns the filename less the
186
     * extension.  If m_playlistName does have a value, this returns that.
187
     */
188
    QString name() const;
189

190 191
    /**
     * This sets a name for the playlist that is \e different from the file name.
192
     */
193
    void setName(const QString &n);
194

195 196 197
    /**
     * Returns the number of items in the playlist.
     */
198
    int count() const { return childCount(); }
199

200
    /**
201 202 203 204 205
     * 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).
206
     */
207
    PlaylistItem *nextItem(PlaylistItem *current, bool random = false);
208 209 210 211 212 213

    /**
     * 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.
     */
214
    PlaylistItem *previousItem(PlaylistItem *current, bool random = false);
215

216 217 218 219
    /**
     * Returns the KActionMenu that allows this to be embedded in menus outside
     * of the playlist.
     */
220
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
221

222 223 224 225 226 227 228 229
    /**
     * 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
230
    bool playing() const;
231

Scott Wheeler's avatar
Scott Wheeler committed
232 233 234
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
235
     */
Scott Wheeler's avatar
Scott Wheeler committed
236
    void updateLeftColumn();
237

238 239 240 241
    /**
     * Sets the items in the list to be either visible based on the value of
     * visible.  This is useful for search operations and such.
     */
242 243
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

244 245 246 247
    /**
     * 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
248
    PlaylistSearch search() const { return m_search; }
249 250 251 252 253 254

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

257 258 259 260 261 262 263 264
    /**
     * 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); }
265

Scott Wheeler's avatar
Scott Wheeler committed
266 267 268
    /**
     * Marks \a item as either selected or deselected based.
     */
269 270
    void markItemSelected(PlaylistItem *item, bool selected);

Scott Wheeler's avatar
Scott Wheeler committed
271 272 273 274 275 276
    /**
     * Subclasses of Playlist which add new columns will set this value to
     * specify how many of those colums exist.  This allows the Playlist
     * class to do some internal calculations on the number and positions
     * of columns.
     */
277
    virtual int columnOffset() const { return 0; }
Scott Wheeler's avatar
Scott Wheeler committed
278 279 280 281 282 283

    /**
     * Some subclasses of Playlist will be "read only" lists (i.e. the history
     * playlist).  This is a way for those subclasses to indicate that to the
     * Playlist internals.
     */
284
    virtual bool readOnly() const { return false; }
285

286 287
    void setColumnWidthUpdatesDisabled(bool disabled) { m_disableColumnWidthUpdates = disabled; }

288 289 290 291 292 293 294
    /**
     * Playlists have a common set of shared settings such as visible columns
     * that should be applied just before the playlist is shown.  Calling this
     * method applies those.
     */
    void applySharedSettings();

295
public slots:
296
    /**
297
     * Remove the currently selected items from the playlist and disk.
298
     */
299
    void slotRemoveSelectedItems() { removeFromDisk(selectedItems()); };
Scott Wheeler's avatar
Scott Wheeler committed
300 301 302 303

    /**
     * Set the first selected item to be the next item returned by nextItem().
     */
304 305
    void slotSetNext();

306 307 308 309
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
310
    virtual void cut() { copy(); clear(); }
Scott Wheeler's avatar
Scott Wheeler committed
311 312 313 314 315

    /**
     * Puts a list of URLs pointing to the files in the current selection on the
     * clipboard.
     */
316
    virtual void copy();
Scott Wheeler's avatar
Scott Wheeler committed
317 318 319 320

    /**
     * Checks the clipboard for local URLs to be inserted into this playlist.
     */
321
    virtual void paste();
Scott Wheeler's avatar
Scott Wheeler committed
322 323 324 325 326 327 328

    /**
     * Removes the selected items from the list, but not the disk.
     *
     * @see clearItem()
     * @see clearItems()
     */
329
    virtual void clear();
330
    virtual void selectAll() { KListView::selectAll(true); }
331

332 333 334 335 336 337
    /**
     * Refreshes the tags of the selection from disk, or all of the files in the
     * list if there is no selection.
     */
    virtual void slotRefresh();

338
    void slotGuessTagInfo(TagGuesser::Type type);
Scott Wheeler's avatar
Scott Wheeler committed
339 340 341 342 343 344

    /**
     * Renames the selected items' files based on their tags contents.
     *
     * @see PlaylistItem::renameFile()
     */
345 346
    void slotRenameFile();

347 348 349
    /**
     * Reload the playlist contents from the m3u file.
     */
350
    virtual void slotReload();
351

352 353 354 355 356
    /**
     * Tells the listview that the next time that it paints that the weighted
     * column widths must be recalculated.  If this is called without a column
     * all visible columns are marked as dirty.
     */
357
    void slotWeightDirty(int column = -1);
358

359
protected:
360 361 362 363
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
364
    void removeFromDisk(const PlaylistItemList &items);
365

366 367 368
    // the following are all reimplemented from base classes

    virtual bool eventFilter(QObject *watched, QEvent *e);
369
    virtual QDragObject *dragObject(QWidget *parent);
370
    virtual QDragObject *dragObject() { return dragObject(this); }
371
    virtual bool canDecode(QMimeSource *s);
372
    virtual void decode(QMimeSource *s, PlaylistItem *after = 0);
373
    virtual void contentsDropEvent(QDropEvent *e);
Scott Wheeler's avatar
Scott Wheeler committed
374
    virtual void showEvent(QShowEvent *e);
375
    virtual bool acceptDrag(QDropEvent *e) const { return KURLDrag::canDecode(e); }
376 377 378 379
    virtual void viewportPaintEvent(QPaintEvent *pe);
    virtual void viewportResizeEvent(QResizeEvent *re);

    void addColumn(const QString &label);
Scott Wheeler's avatar
Scott Wheeler committed
380 381 382 383 384 385 386 387

    /**
     * Here I'm using delayed setup of some things that aren't quite intuitive.
     * Creating columns and setting up connections are both time consuming if
     * there are a lot of playlists to initialize.  This moves that cost from the
     * startup time to the time when the widget is "polished" -- i.e. just before
     * it's painted the first time.
     */
388
    virtual void polish();
389

390 391 392 393 394 395 396 397
    /**
     * As a template this allows us to use the same code to initialize the items
     * in subclasses.  CollectionItemType should always be CollectionListItem and
     * ItemType should be a PlaylistItem subclass.
     */
    template <class CollectionItemType, class ItemType, class SiblingType>
    void createItems(const QValueList<SiblingType *> &siblings);

398 399 400 401
    /**
     * Though it's somewhat obvious, this function will stat the file, so only use it when
     * you're out of a performance critical loop.
     */
402
    static QString resolveSymLinks(const QFileInfo &file);
Scott Wheeler's avatar
Scott Wheeler committed
403

404
signals:
405
    /**
406
     * This is emitted when the playlist selection is changed.  This is used
407
     * primarily to notify the TagEditor of the new data.
408
     */
409
    void signalSelectionChanged(const PlaylistItemList &selection);
410
    void signalDoubleClicked();
411

412
    /**
413
     * This is connected to the PlaylistBox::Item to let it know when the
414 415
     * playlist's name has changed.
     */
416
    void signalNameChanged(const QString &fileName);
417

418
    /**
419 420 421 422
     * This signal is emitted when items are added to or removed from the list.
     *
     * \see signalDataChanged()
     * \see signalChanged()
423
     */
424
    void signalCountChanged(Playlist *);
425

426
    /**
427 428 429 430 431 432 433 434 435 436 437
     * 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
438 439 440 441
     * changed.
     */
    void signalChanged();

442
    /**
443
     * This signal is emitted just before a playlist item is removed from the
444 445 446
     * 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.
447
     */
448
    void signalAboutToRemove(PlaylistItem *item);
449 450 451 452

    /**
     * This is emitted when \a files are dropped on a specific playlist.
     */
453
    void signalFilesDropped(const QStringList &files, Playlist *, PlaylistItem *after);
454 455 456 457 458

    /**
     * Set the next item to be played in the current playlist.  This is used by
     * the "Play Next" feature.
     */
459
    void signalSetNext(PlaylistItem *item);
460

461 462 463 464 465
    /**
     * Request creation of a playlist based on \a items.
     */
    void signalCreatePlaylist(const PlaylistItemList &items);

466 467
private:
    void setup();
468 469 470 471 472 473 474

    /**
     * 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.
     */
475
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
476

477
    /**
478
     * Save the tag for an individual item.
479
     */
480
    void applyTag(PlaylistItem *item, const QString &text, int column);
481 482 483 484 485 486

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

489 490 491 492 493 494 495 496 497 498
    /**
     * This method is used internally to provide the backend to the other item
     * lists.
     *
     * \see items()
     * \see visibleItems()
     * \see selectedItems()
     */
    PlaylistItemList items(QListViewItemIterator::IteratorFlag flags);

499 500 501 502 503
    /**
     * Build the column "weights" for the weighted width mode.
     */
    void calculateColumnWeights();

504 505 506 507 508 509
    /**
     * 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;

510
private slots:
511

512 513
    void slotUpdateColumnWidths();

514 515 516 517
    /**
     * This is just used to emit the selection as a list of PlaylistItems when
     * the selection changes.
     */
518
    void slotEmitSelected() { emit signalSelectionChanged(selectedItems()); }
519 520 521 522 523

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

    /**
527 528 529
     * This slot applys the tag for a specific item.
     *
     * \see applyTag()
530 531 532 533 534 535 536
     */
    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.
     */
537
    void slotRenameTag();
538 539 540 541 542

    /**
     * Moves the column \a from to the position \a to.  This matches the signature
     * for the signal QHeader::indexChange().
     */
543
    void slotColumnOrderChanged(int, int from, int to);
544 545 546 547 548 549 550

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

553 554 555 556 557
    /**
     * Prompts the user to create a new playlist with from the selected items.
     */
    void slotCreateGroup() { emit signalCreatePlaylist(selectedItems()); }

558 559 560 561 562 563
    /**
     * This slot is called when the user drags the slider in the listview header
     * to manually set the size of the column.
     */
    void slotColumnSizeChanged(int column, int oldSize, int newSize);

564 565 566 567 568 569 570 571
    /**
     * The slot is called when the completion mode for the line edit in the
     * inline tag editor is changed.  It saves the settings and through the
     * magic of the SharedSettings class will apply it to the other playlists as
     * well.
     */
    void slotInlineCompletionModeChanged(KGlobalSettings::Completion mode);

572
private:
573
    StringHash m_members;
574

575
    int m_currentColumn;
576
    int m_processed;
577 578 579 580 581 582

    int m_rmbPasteID;
    int m_rmbEditID;

    int m_selectedCount;

583
    bool m_allowDuplicates;
584
    bool m_polished;
585
    bool m_applySharedSettings;
586

587
    QValueList<int> m_weightDirty;
588
    bool m_disableColumnWidthUpdates;
589
    /**
590
     * The average minimum widths of columns to be used in balancing calculations.
591
     */
592
    QValueVector<int> m_columnWeights;
593 594
    QValueVector<int> m_columnFixedWidths;
    bool m_widthsDirty;
595 596

    PlaylistItemList m_randomList;
Scott Wheeler's avatar
Scott Wheeler committed
597
    PlaylistItemList m_history;
598
    PlaylistSearch m_search;
599

600
    PlaylistItem *m_lastSelected;
601

602
    /**
603
     * Used to store the text for inline editing before it is changed so that
604 605 606 607
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
608 609 610 611 612

    /**
     * This is only defined if the playlist name is something other than the
     * file name.
     */
613
    QString m_playlistName;
614
    QString m_fileName;
615

616 617 618
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
619

620 621 622 623 624
    /**
     * 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;
625
    static int m_leftColumn;
626
    static PlaylistItem *m_playingItem;
627 628
};

629
QDataStream &operator<<(QDataStream &s, Playlist &p);
630 631
QDataStream &operator>>(QDataStream &s, Playlist &p);

632 633
// template method implementations

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
template <class ItemType, class CollectionItemType, class CollectionListType>
ItemType *Playlist::createItem(const QFileInfo &file, const QString &absFilePath,
			       QListViewItem *after, bool emitChanged)
{
    QString filePath;

    if(absFilePath.isNull())
	filePath = resolveSymLinks(file);
    else
	filePath = absFilePath;

    CollectionItemType *item = CollectionListType::instance()->lookup(filePath);

    if(!item) {
	item = new CollectionItemType(file, filePath);

	// If a valid tag was not created, destroy the CollectionListItem.
	if(!item->isValid()) {
	    kdError(65432) << "Playlist::createItem() -- A valid tag was not created for \""
			   << file.filePath() << "\"" << endl;
	    delete item;
	    return 0;
	}
    }

    if(item && !m_members.insert(filePath) || m_allowDuplicates) {
	ItemType *i;
	if(after)
	    i = new ItemType(item, this, after);
	else
	    i = new ItemType(item, this);
        if(!m_randomList.isEmpty() && !m_visibleChanged)
            m_randomList.append(i);
	emit signalCountChanged(this);
	connect(item, SIGNAL(destroyed()), i, SLOT(deleteLater()));

	if(emitChanged)
	    emit signalCountChanged(this);

	return i;
    }
    else
676
	return 0;
677 678
}

679 680 681
template <class CollectionItemType, class ItemType, class SiblingType>
void Playlist::createItems(const QValueList<SiblingType *> &siblings)
{
682 683 684 685
    if(siblings.isEmpty())
	return;

    m_disableColumnWidthUpdates = true;
686 687 688 689
    ItemType *previous = 0;

    QValueListConstIterator<SiblingType *> it = siblings.begin();
    for(; it != siblings.end(); ++it) {
690
	if(!m_members.insert(resolveSymLinks((*it)->absFilePath())) || m_allowDuplicates) {
691 692 693 694 695 696
	    previous = new ItemType((*it)->collectionItem(), this, previous);
	    connect((*it)->collectionItem(), SIGNAL(destroyed()), (*it), SLOT(deleteLater()));
	}
    }

    emit signalCountChanged(this);
697
    m_disableColumnWidthUpdates = false;
698
    slotWeightDirty();
699 700
}

701
#endif