playlist.h 20.2 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 40
class PlaylistSearch;

class PlaylistItem;
41
class PlaylistToolTip;
Scott Wheeler's avatar
Scott Wheeler committed
42
typedef QValueList<PlaylistItem *> PlaylistItemList;
43

44 45
typedef QValueList<Playlist *> PlaylistList;

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

50
public:
51

52 53 54 55 56 57
    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");
58

59
    /**
60 61 62
     * 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.
63
     */
64
    Playlist(PlaylistCollection *collection, bool delaySetup);
65

66 67
    virtual ~Playlist();

68 69 70 71 72 73 74 75 76 77 78

    // 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();

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

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

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

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

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

113 114
    /**
     * All of the (media) files in the list.
115
     */
116
    QStringList files();
117

118
    /**
Scott Wheeler's avatar
Scott Wheeler committed
119
     * Returns a list of all of the items in the playlist.
120
     */
121
    virtual PlaylistItemList items();
122

123
    /**
Scott Wheeler's avatar
Scott Wheeler committed
124
     * Returns a list of all of the \e visible items in the playlist.
125
     */
126
    PlaylistItemList visibleItems();
127

128 129 130
    /**
     * Returns a list of the currently selected items.
     */
131
    PlaylistItemList selectedItems();
132

133 134 135 136 137
    /**
     * Returns a list of the last 10 played items.
     */
    PlaylistItemList historyItems(PlaylistItem *current, bool random) const;

138 139
    /**
     * Allow duplicate files in the playlist.
140
     */
141
    void setAllowDuplicates(bool allow) { m_allowDuplicates = allow; }
142

143
    /**
144
     * This is being used as a mini-factory of sorts to make the construction
145 146
     * of PlaylistItems virtual.  In this case it allows for the creation of
     * both PlaylistItems and CollectionListItems.
147
     */
148
    virtual PlaylistItem *createItem(const FileHandle &file,
149 150
				     QListViewItem *after = 0,
				     bool emitChanged = true);
151

Scott Wheeler's avatar
Scott Wheeler committed
152 153 154 155 156 157 158
    /**
     * 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.     
     */
159
    template <class ItemType, class CollectionItemType, class CollectionListType>
160
    ItemType *createItem(const FileHandle &file,
161 162 163
			 QListViewItem *after = 0,
			 bool emitChanged = true);

164
    virtual void createItems(const PlaylistItemList &siblings);
165

166 167 168 169
    /**
     * 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.
170 171 172 173
     *
     * 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.
174 175 176 177
     */
    void addFiles(const QStringList &files, bool importPlaylists,
		  PlaylistItem *after = 0);

178 179 180 181
    /**
     * Returns the file name associated with this playlist (an m3u file) or
     * QString::null if no such file exists.
     */
182
    QString fileName() const { return m_fileName; }
183 184 185 186 187

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

190
    /**
191
     * Hides column \a c.  If \a updateSearch is true then a signal that the
192 193 194
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
195
    void hideColumn(int c, bool updateSearch = true);
196 197

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

205 206 207 208
    /**
     * This sets a name for the playlist that is \e different from the file name.
     */
    void setName(const QString &n);
209

210 211 212 213
    /**
     * Returns the KActionMenu that allows this to be embedded in menus outside
     * of the playlist.
     */
214
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
215

216
    /**
217 218
     * Set item to be the playing item.  If \a item is null then this will clear
     * the playing indicator.
219
     */
220
    static void setPlaying(PlaylistItem *item);
221 222 223 224

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

Scott Wheeler's avatar
Scott Wheeler committed
227 228 229
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
230
     */
Scott Wheeler's avatar
Scott Wheeler committed
231
    void updateLeftColumn();
232

233 234 235 236
    /**
     * Sets the items in the list to be either visible based on the value of
     * visible.  This is useful for search operations and such.
     */
237 238
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

239 240 241 242
    /**
     * 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
243
    PlaylistSearch search() const { return m_search; }
244 245 246 247

    /**
     * Set the search associtated with this playlist.
     */
248 249 250 251 252 253 254
    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
255

Scott Wheeler's avatar
Scott Wheeler committed
256 257 258
    /**
     * Marks \a item as either selected or deselected based.
     */
259 260
    void markItemSelected(PlaylistItem *item, bool selected);

Scott Wheeler's avatar
Scott Wheeler committed
261 262 263 264 265 266
    /**
     * 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.
     */
267
    virtual int columnOffset() const { return 0; }
Scott Wheeler's avatar
Scott Wheeler committed
268 269 270 271 272 273

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

276 277
    void setColumnWidthUpdatesDisabled(bool disabled) { m_disableColumnWidthUpdates = disabled; }

278 279 280 281 282 283 284
    /**
     * 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();

285 286 287 288 289 290 291 292 293 294 295 296
    /**
     * Returns true if full path sort is currently enabled for the file column.
     */
    bool fileColumnFullPathSort() const { return m_fileColumnFullPathSort; }

    /**
     * Reimplemented to add toggling of the file column sorting mode.
     *
     * \see fileColumnFullPathSort()
     */
    virtual void setSorting(int column, bool ascending = true);

297 298
    void read(QDataStream &s);

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

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

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

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

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

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

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

342
    void slotGuessTagInfo(TagGuesser::Type type);
Scott Wheeler's avatar
Scott Wheeler committed
343 344 345 346 347 348

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

351 352 353
    /**
     * Reload the playlist contents from the m3u file.
     */
354
    virtual void slotReload();
355

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

363 364
    void slotShowPlaying();

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

372 373 374
    // the following are all reimplemented from base classes

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

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

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

396 397 398 399 400 401 402 403
    /**
     * 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);

404 405 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>
    void createItems(const QValueList<SiblingType *> &siblings);

412
signals:
413

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

420
    /**
421
     * This signal is emitted just before a playlist item is removed from the
422 423 424
     * 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.
425
     */
426
    void signalAboutToRemove(PlaylistItem *item);
427

428 429
private:
    void setup();
430 431 432 433 434 435 436

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

439
    /**
Scott Wheeler's avatar
Scott Wheeler committed
440 441
     * Writes \a text to \a item in \a column.  This is used by the inline tag
     * editor.
442
     */
Scott Wheeler's avatar
Scott Wheeler committed
443
    void editTag(PlaylistItem *item, const QString &text, int column);
444 445 446 447 448 449

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

452 453 454 455 456 457 458 459 460 461
    /**
     * 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);

462 463 464 465 466
    /**
     * Build the column "weights" for the weighted width mode.
     */
    void calculateColumnWeights();

467 468 469 470
    PlaylistItem *addFile(const QString &file, bool importPlaylists, PlaylistItem *after);

    void redisplaySearch() { setSearch(m_search); }

471 472 473 474 475 476
    /**
     * 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;

477
private slots:
478

479 480
    void slotUpdateColumnWidths();

481 482 483 484
    /**
     * Show the RMB menu.  Matches the signature for the signal 
     * QListView::contextMenuRequested().
     */
485
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
486 487

    /**
Scott Wheeler's avatar
Scott Wheeler committed
488 489
     * This slot is called when the inline tag editor has completed its editing
     * and starts the process of renaming the values.
490
     *
Scott Wheeler's avatar
Scott Wheeler committed
491
     * \see editTag()
492
     */
Scott Wheeler's avatar
Scott Wheeler committed
493
    void slotInlineEditDone(QListViewItem *, const QString &, int column);
494 495 496 497 498

    /**
     * This starts the renaming process by displaying a line edit if the mouse is in 
     * an appropriate position.
     */
499
    void slotRenameTag();
500 501 502 503 504

    /**
     * Moves the column \a from to the position \a to.  This matches the signature
     * for the signal QHeader::indexChange().
     */
505
    void slotColumnOrderChanged(int, int from, int to);
506 507 508 509 510 511 512

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

515 516 517
    /**
     * Prompts the user to create a new playlist with from the selected items.
     */
518
    void slotCreateGroup() { new Playlist(m_collection, selectedItems()); }
519

520 521 522 523 524 525
    /**
     * 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);

526 527 528 529 530 531 532 533
    /**
     * 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);

534 535
    void slotPlayCurrent();

536
private:
537 538
    PlaylistCollection *m_collection;

539
    StringHash m_members;
540

541
    int m_currentColumn;
542
    int m_processed;
543 544 545
    int m_rmbEditID;
    int m_selectedCount;

546
    bool m_allowDuplicates;
547
    bool m_polished;
548
    bool m_applySharedSettings;
549
    bool m_fileColumnFullPathSort;
550

551
    QValueList<int> m_weightDirty;
552
    bool m_disableColumnWidthUpdates;
553
    /**
554
     * The average minimum widths of columns to be used in balancing calculations.
555
     */
556
    QValueVector<int> m_columnWeights;
557 558
    QValueVector<int> m_columnFixedWidths;
    bool m_widthsDirty;
559 560

    PlaylistItemList m_randomList;
Scott Wheeler's avatar
Scott Wheeler committed
561
    PlaylistItemList m_history;
562
    PlaylistSearch m_search;
563

564 565
    bool m_searchEnabled;

566
    PlaylistItem *m_lastSelected;
567

568
    /**
569
     * Used to store the text for inline editing before it is changed so that
570 571 572 573
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
574 575 576 577 578

    /**
     * This is only defined if the playlist name is something other than the
     * file name.
     */
579
    QString m_playlistName;
580
    QString m_fileName;
581

582 583 584
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
585
    PlaylistToolTip *m_toolTip;
586

587 588 589 590 591
    /**
     * 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;
592
    static int m_leftColumn;
593
    static PlaylistItem *m_playingItem;
594
    static PlaylistItem *m_playNextItem;
595 596
};

597 598
void processEvents();

599
QDataStream &operator<<(QDataStream &s, Playlist &p);
600 601
QDataStream &operator>>(QDataStream &s, Playlist &p);

602 603
// template method implementations

604
template <class ItemType, class CollectionItemType, class CollectionListType>
605 606
ItemType *Playlist::createItem(const FileHandle &file, QListViewItem *after,
			       bool emitChanged)
607
{
608
    CollectionItemType *item = CollectionListType::instance()->lookup(file.absFilePath());
609 610

    if(!item) {
611
	item = new CollectionItemType(file);
612
	setupItem(item);
613 614

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

616 617
	if(!item->isValid()) {
	    kdError(65432) << "Playlist::createItem() -- A valid tag was not created for \""
618
			   << file.absFilePath() << "\"" << endl;
619 620 621 622 623
	    delete item;
	    return 0;
	}
    }

624
    if(item && !m_members.insert(file.absFilePath()) || m_allowDuplicates) {
625

626
	ItemType *i = after ? new ItemType(item, this, after) : new ItemType(item, this);
627 628
	setupItem(i);

629 630
        if(!m_randomList.isEmpty() && !m_visibleChanged)
            m_randomList.append(i);
631

632
	if(emitChanged)
633
	    PlaylistInterface::update();
634 635 636 637

	return i;
    }
    else
638
	return 0;
639 640
}

641 642 643
template <class CollectionItemType, class ItemType, class SiblingType>
void Playlist::createItems(const QValueList<SiblingType *> &siblings)
{
644 645 646 647
    if(siblings.isEmpty())
	return;

    m_disableColumnWidthUpdates = true;
648
    ItemType *newItem = 0;
649 650 651

    QValueListConstIterator<SiblingType *> it = siblings.begin();
    for(; it != siblings.end(); ++it) {
652
	if(!m_members.insert((*it)->file().absFilePath()) || m_allowDuplicates) {
653 654 655 656
	    newItem = new ItemType((*it)->collectionItem(), this, newItem);
	    setupItem(newItem);
	    if(!m_randomList.isEmpty() && !m_visibleChanged)
		m_randomList.append(newItem);
657 658 659
	}
    }

660
    PlaylistInterface::update();
661
    m_disableColumnWidthUpdates = false;
662
    slotWeightDirty();
663 664
}

665
#endif