Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

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

/***************************************************************************
 *                                                                         *
 *   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>
20
#include <kurldrag.h>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
21
#include <kdebug.h>
22
#include <kglobalsettings.h>
23

24
#include <qvaluevector.h>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
25
#include <qfileinfo.h>
26

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

class KPopupMenu;
33
class KActionMenu;
34 35

class QEvent;
Scott Wheeler's avatar
Scott Wheeler committed
36

37
class PlaylistCollection;
Scott Wheeler's avatar
Scott Wheeler committed
38 39

class PlaylistItem;
40
class PlaylistToolTip;
41 42
class UpcomingPlaylist;

Scott Wheeler's avatar
Scott Wheeler committed
43
typedef QValueList<PlaylistItem *> PlaylistItemList;
44

45 46
typedef QValueList<Playlist *> PlaylistList;

47
class Playlist : public KListView, public PlaylistInterface
48 49
{
    Q_OBJECT
50

51
public:
52

53 54 55 56 57 58
    Playlist(PlaylistCollection *collection, const QString &name = QString::null,
	     const QString &iconName = "midi");
    Playlist(PlaylistCollection *collection, const PlaylistItemList &items,
	     const QString &name = QString::null, const QString &iconName = "midi");
    Playlist(PlaylistCollection *collection, const QFileInfo &playlistFile,
	     const QString &iconName = "midi");
59

60
    /**
61 62 63
     * This constructor should generally only be used either by the cache
     * restoration methods or by subclasses that want to handle calls to
     * PlaylistCollection::setupPlaylist() differently.
64
     */
65
    Playlist(PlaylistCollection *collection, bool delaySetup);
66

67 68
    virtual ~Playlist();

69 70 71 72 73 74 75 76 77 78 79

    // The following group of functions implement the PlaylistInterface API.

    virtual QString name() const;
    virtual FileHandle currentFile() const;
    virtual int count() const { return childCount(); }
    virtual int time() const;
    virtual void playNext();
    virtual void playPrevious();
    virtual void stop();

80 81 82
    // Plays the top item of the playlist.
    void playFirst();

83 84 85
    /**
     * 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
86
     * name.
87
     */
88
    virtual void save();
Scott Wheeler's avatar
Scott Wheeler committed
89 90 91 92 93

    /**
     * Standard "save as".  Prompts the user for a location where to save the
     * playlist to.
     */
94
    virtual void saveAs();
Scott Wheeler's avatar
Scott Wheeler committed
95 96 97 98 99 100 101 102 103 104 105

    /**
     * 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.
     */
106
    virtual void clearItem(PlaylistItem *item, bool emitChanged = true);
Scott Wheeler's avatar
Scott Wheeler committed
107 108 109 110 111

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

114 115 116 117 118 119
    /**
     * Accessor function to return a pointer to the currently playing file.
     *
     * @return 0 if no file is playing, otherwise a pointer to the PlaylistItem
     *     of the track that is currently playing.
     */
120
    static PlaylistItem *playingItem() { return m_playingItem; }
121

122 123
    /**
     * All of the (media) files in the list.
124
     */
125
    QStringList files() const;
126

127
    /**
Scott Wheeler's avatar
Scott Wheeler committed
128
     * Returns a list of all of the items in the playlist.
129
     */
130
    virtual PlaylistItemList items();
131

132
    /**
Scott Wheeler's avatar
Scott Wheeler committed
133
     * Returns a list of all of the \e visible items in the playlist.
134
     */
135
    PlaylistItemList visibleItems();
136

137 138 139
    /**
     * Returns a list of the currently selected items.
     */
140
    PlaylistItemList selectedItems();
141

142 143 144 145 146
    /**
     * Returns properly casted first child item in list.
     */
    PlaylistItem *firstChild() const;

147 148
    /**
     * Allow duplicate files in the playlist.
149
     */
150
    void setAllowDuplicates(bool allow) { m_allowDuplicates = allow; }
151

152
    /**
153
     * This is being used as a mini-factory of sorts to make the construction
154 155
     * of PlaylistItems virtual.  In this case it allows for the creation of
     * both PlaylistItems and CollectionListItems.
156
     */
157
    virtual PlaylistItem *createItem(const FileHandle &file,
158 159
				     QListViewItem *after = 0,
				     bool emitChanged = true);
160

Scott Wheeler's avatar
Scott Wheeler committed
161 162 163 164 165 166 167
    /**
     * 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.     
     */
168
    template <class ItemType, class CollectionItemType, class CollectionListType>
169
    ItemType *createItem(const FileHandle &file,
170 171 172
			 QListViewItem *after = 0,
			 bool emitChanged = true);

173
    virtual void createItems(const PlaylistItemList &siblings, PlaylistItem *after = 0);
174

175 176 177 178
    /**
     * This handles adding files of various types -- music, playlist or directory
     * files.  Music files that are found will be added to this playlist.  New
     * playlist files that are found will result in new playlists being created.
179 180 181 182
     *
     * Note that this should not be used in the case of adding *only* playlist
     * items since it has the overhead of checking to see if the file is a playlist
     * or directory first.
183 184 185 186
     */
    void addFiles(const QStringList &files, bool importPlaylists,
		  PlaylistItem *after = 0);

187 188 189 190
    /**
     * Returns the file name associated with this playlist (an m3u file) or
     * QString::null if no such file exists.
     */
191
    QString fileName() const { return m_fileName; }
192 193 194 195 196

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

199
    /**
200
     * Hides column \a c.  If \a updateSearch is true then a signal that the
201 202 203
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
204
    void hideColumn(int c, bool updateSearch = true);
205 206

    /**
207
     * Shows column \a c.  If \a updateSearch is true then a signal that the
208 209 210
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
211
    void showColumn(int c, bool updateSearch = true);
212 213
    bool isColumnVisible(int c) const;

214 215 216 217
    /**
     * This sets a name for the playlist that is \e different from the file name.
     */
    void setName(const QString &n);
218

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

225
    /**
226 227
     * Set item to be the playing item.  If \a item is null then this will clear
     * the playing indicator.
228
     */
229
    static void setPlaying(PlaylistItem *item, bool addToHistory = true);
230 231 232 233

    /**
     * Returns true if this playlist is currently playing.
     */
Scott Wheeler's avatar
Scott Wheeler committed
234
    bool playing() const;
235

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

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

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

    /**
     * Set the search associtated with this playlist.
     */
257 258 259 260 261 262 263
    void setSearch(const PlaylistSearch &s);

    /**
     * If the search is disabled then all items will be shown, not just those that
     * match the current search.
     */
    void setSearchEnabled(bool searchEnabled);
Scott Wheeler's avatar
Scott Wheeler committed
264

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

Scott Wheeler's avatar
Scott Wheeler committed
270 271 272 273 274 275
    /**
     * 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.
     */
276
    virtual int columnOffset() const { return 0; }
Scott Wheeler's avatar
Scott Wheeler committed
277 278 279 280 281 282

    /**
     * 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.
     */
283
    virtual bool readOnly() const { return false; }
284

285 286 287 288 289
    /**
     * Returns true if it's possible to reload this playlist.
     */
    virtual bool canReload() const { return !m_fileName.isNull(); }

290 291
    void setColumnWidthUpdatesDisabled(bool disabled) { m_disableColumnWidthUpdates = disabled; }

292 293 294 295 296 297 298
    /**
     * 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();

299 300
    void read(QDataStream &s);

301
public slots:
302
    /**
303
     * Remove the currently selected items from the playlist and disk.
304
     */
305
    void slotRemoveSelectedItems() { removeFromDisk(selectedItems()); };
Scott Wheeler's avatar
Scott Wheeler committed
306

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

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

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

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

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

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

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

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

353 354 355 356 357
    /**
     * 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.
     */
358
    void slotWeightDirty(int column = -1);
359

360 361
    void slotShowPlaying();

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

369 370 371
    // the following are all reimplemented from base classes

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

    void addColumn(const QString &label);
Scott Wheeler's avatar
Scott Wheeler committed
384 385 386 387 388 389 390 391

    /**
     * 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.
     */
392
    virtual void polish();
393

394 395 396 397
    UpcomingPlaylist *upcomingPlaylist() const { return m_upcomingPlaylist; }

    void setUpcomingPlaylist(UpcomingPlaylist *playlist) { m_upcomingPlaylist = playlist; }

398 399 400 401 402 403 404 405
    /**
     * Do some finial initialization of created items.  Notably ensure that they
     * are shown or hidden based on the contents of the current PlaylistSearch.
     *
     * This is called by the PlaylistItem constructor.
     */
    void setupItem(PlaylistItem *item);

406 407 408 409 410 411
    /**
     * 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>
412
    void createItems(const QValueList<SiblingType *> &siblings, ItemType *after = 0);
413

414 415 416 417
protected slots:
    void slotPopulateBackMenu() const;
    void slotPlayFromBackMenu(int number) const;

418
signals:
419

420
    /**
421
     * This is connected to the PlaylistBox::Item to let it know when the
422 423
     * playlist's name has changed.
     */
424
    void signalNameChanged(const QString &name);
425

426
    /**
427
     * This signal is emitted just before a playlist item is removed from the
428 429 430
     * 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.
431
     */
432
    void signalAboutToRemove(PlaylistItem *item);
433

434 435
    void signalEnableDirWatch(bool enable);

436 437
private:
    void setup();
438

439 440
    PlaylistItem *nextItem(PlaylistItem *current = 0) const;

441 442 443
    /** Sets up album random play to play songs with the same album as
     * the given PlaylistItem.
     */
444
//    void initAlbumSearch(const PlaylistItem *item);
445

446 447 448 449 450 451
    /**
     * 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.
     */
452
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
453

454
    /**
Scott Wheeler's avatar
Scott Wheeler committed
455
     * Writes \a text to \a item in \a column.  This is used by the inline tag
456
     * editor.  Returns false if the tag update failed.
457
     */
458
    bool editTag(PlaylistItem *item, const QString &text, int column);
459 460 461 462 463 464

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

467 468 469 470 471 472 473 474 475 476
    /**
     * 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);

477 478 479 480 481
    /**
     * Build the column "weights" for the weighted width mode.
     */
    void calculateColumnWeights();

482 483 484 485
    PlaylistItem *addFile(const QString &file, bool importPlaylists, PlaylistItem *after);

    void redisplaySearch() { setSearch(m_search); }

486 487 488 489 490 491
    /**
     * 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;

492
private slots:
493

494 495
    void slotUpdateColumnWidths();

496 497
    void slotAddToUpcoming();

498 499 500 501
    /**
     * Show the RMB menu.  Matches the signature for the signal 
     * QListView::contextMenuRequested().
     */
502
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
503 504

    /**
Scott Wheeler's avatar
Scott Wheeler committed
505 506
     * This slot is called when the inline tag editor has completed its editing
     * and starts the process of renaming the values.
507
     *
Scott Wheeler's avatar
Scott Wheeler committed
508
     * \see editTag()
509
     */
Scott Wheeler's avatar
Scott Wheeler committed
510
    void slotInlineEditDone(QListViewItem *, const QString &, int column);
511 512 513 514 515

    /**
     * This starts the renaming process by displaying a line edit if the mouse is in 
     * an appropriate position.
     */
516
    void slotRenameTag();
517 518 519 520 521

    /**
     * Moves the column \a from to the position \a to.  This matches the signature
     * for the signal QHeader::indexChange().
     */
522
    void slotColumnOrderChanged(int, int from, int to);
523 524 525 526 527 528 529

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

532 533 534
    /**
     * Prompts the user to create a new playlist with from the selected items.
     */
535
    void slotCreateGroup();
536

537 538 539 540 541 542
    /**
     * 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);

543 544 545 546 547 548 549 550
    /**
     * 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);

551 552
    void slotPlayCurrent();

553
private:
554 555
    PlaylistCollection *m_collection;

556
    StringHash m_members;
557

558
    int m_currentColumn;
559
    int m_processed;
560
    int m_rmbEditID;
561
    int m_rmbUpcomingID;
562 563
    int m_selectedCount;

564
    bool m_allowDuplicates;
565
    bool m_polished;
566
    bool m_applySharedSettings;
567

568
    QValueList<int> m_weightDirty;
569
    bool m_disableColumnWidthUpdates;
570
    /**
571
     * The average minimum widths of columns to be used in balancing calculations.
572
     */
573
    QValueVector<int> m_columnWeights;
574 575
    QValueVector<int> m_columnFixedWidths;
    bool m_widthsDirty;
576 577

    PlaylistItemList m_randomList;
578
    // PlaylistItemList m_history;
579
    static PlaylistItemList m_history;
580
    PlaylistSearch m_search;
581
    // PlaylistSearch m_albumSearch;
582

583 584
    bool m_searchEnabled;

585
    PlaylistItem *m_lastSelected;
586

587
    /**
588
     * Used to store the text for inline editing before it is changed so that
589 590 591 592
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
593 594 595 596 597

    /**
     * This is only defined if the playlist name is something other than the
     * file name.
     */
598
    QString m_playlistName;
599
    QString m_fileName;
600

601 602 603
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
604
    PlaylistToolTip *m_toolTip;
605

606 607 608 609 610
    /**
     * 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;
611
    static int m_leftColumn;
612
    static PlaylistItem *m_playingItem;
613
    static UpcomingPlaylist *m_upcomingPlaylist;
614
    static QMap<int, PlaylistItem *> m_backMenuItems;
615 616
};

617
bool processEvents();
618

619
QDataStream &operator<<(QDataStream &s, const Playlist &p);
620 621
QDataStream &operator>>(QDataStream &s, Playlist &p);

622 623
// template method implementations

624
template <class ItemType, class CollectionItemType, class CollectionListType>
625 626
ItemType *Playlist::createItem(const FileHandle &file, QListViewItem *after,
			       bool emitChanged)
627
{
628
    CollectionItemType *item = CollectionListType::instance()->lookup(file.absFilePath());
629 630

    if(!item) {
631
	item = new CollectionItemType(file);
632
	setupItem(item);
633 634

	// If a valid tag was not created, destroy the CollectionListItem.
635

636 637
	if(!item->isValid()) {
	    kdError(65432) << "Playlist::createItem() -- A valid tag was not created for \""
638
			   << file.absFilePath() << "\"" << endl;
639 640 641 642 643
	    delete item;
	    return 0;
	}
    }

644
    if(item && !m_members.insert(file.absFilePath()) || m_allowDuplicates) {
645

646
	ItemType *i = after ? new ItemType(item, this, after) : new ItemType(item, this);
647 648
	setupItem(i);

649 650
        if(!m_randomList.isEmpty() && !m_visibleChanged)
            m_randomList.append(i);
651

652
	if(emitChanged)
653
	    dataChanged();
654 655 656 657

	return i;
    }
    else
658
	return 0;
659 660
}

661
template <class CollectionItemType, class ItemType, class SiblingType>
662
void Playlist::createItems(const QValueList<SiblingType *> &siblings, ItemType *after)
663
{
664 665 666 667
    if(siblings.isEmpty())
	return;

    m_disableColumnWidthUpdates = true;
668
    ItemType *newItem = after;
669 670 671

    QValueListConstIterator<SiblingType *> it = siblings.begin();
    for(; it != siblings.end(); ++it) {
672
	if(!m_members.insert((*it)->file().absFilePath()) || m_allowDuplicates) {
673 674 675 676
	    newItem = new ItemType((*it)->collectionItem(), this, newItem);
	    setupItem(newItem);
	    if(!m_randomList.isEmpty() && !m_visibleChanged)
		m_randomList.append(newItem);
677 678 679
	}
    }

680
    dataChanged();
681
    m_disableColumnWidthUpdates = false;
682
    slotWeightDirty();
683 684
}

685
#endif