playlist.h 21.1 KB
Newer Older
1 2 3 4
/***************************************************************************
                          playlist.h  -  description
                             -------------------
    begin                : Sat Feb 16 2002
5
    copyright            : (C) 2002, 2003 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 <qvaluevector.h>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
27
#include <qfileinfo.h>
28

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

class KPopupMenu;
35
class KActionMenu;
36 37

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

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

class PlaylistItem;
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
     * This constructor should just be used for creating playlists from the Playlist
     * cache.
62
     */
63
    Playlist(PlaylistCollection *collection, bool delaySetup);
64

65 66
    virtual ~Playlist();

67 68 69 70 71 72 73 74 75 76 77

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

78 79 80
    /**
     * 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
81
     * name.
82
     */
83
    virtual void save();
Scott Wheeler's avatar
Scott Wheeler committed
84 85 86 87 88

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

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

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

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

117
    /**
118 119
     * Returns a list of all of the \e visible items in the playlist.
     */
120
    virtual PlaylistItemList items();
121

122
    /**
123
     * Returns a list of all of the items in the playlist.
124
     */
125
    PlaylistItemList visibleItems();
126

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

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

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

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

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

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

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

173 174 175 176
    /**
     * Returns the file name associated with this playlist (an m3u file) or
     * QString::null if no such file exists.
     */
177
    QString fileName() const { return m_fileName; }
178 179 180 181 182

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

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

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

200 201 202 203
    /**
     * This sets a name for the playlist that is \e different from the file name.
     */
    void setName(const QString &n);
204

205 206 207 208
    /**
     * Returns the KActionMenu that allows this to be embedded in menus outside
     * of the playlist.
     */
209
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
210

211
    /**
212 213
     * Set item to be the playing item.  If \a item is null then this will clear
     * the playing indicator.
214
     */
215
    static void setPlaying(PlaylistItem *item);
216 217 218 219

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

Scott Wheeler's avatar
Scott Wheeler committed
222 223 224
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
225
     */
Scott Wheeler's avatar
Scott Wheeler committed
226
    void updateLeftColumn();
227

228 229 230 231
    /**
     * Sets the items in the list to be either visible based on the value of
     * visible.  This is useful for search operations and such.
     */
232 233
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

234 235 236 237
    /**
     * 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
238
    PlaylistSearch search() const { return m_search; }
239 240 241 242

    /**
     * Set the search associtated with this playlist.
     */
243 244 245 246 247 248 249
    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
250

251 252 253 254 255 256 257 258
    /**
     * 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); }
259

Scott Wheeler's avatar
Scott Wheeler committed
260 261 262
    /**
     * Marks \a item as either selected or deselected based.
     */
263 264
    void markItemSelected(PlaylistItem *item, bool selected);

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

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

280 281
    void setColumnWidthUpdatesDisabled(bool disabled) { m_disableColumnWidthUpdates = disabled; }

282 283 284 285 286 287 288
    /**
     * 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();

289 290
    void emitDataChanged() { emit signalDataChanged(); }

291 292 293 294 295 296 297 298 299 300 301 302
    /**
     * 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);

303 304
    void read(QDataStream &s);

305
public slots:
306
    /**
307
     * Remove the currently selected items from the playlist and disk.
308
     */
309
    void slotRemoveSelectedItems() { removeFromDisk(selectedItems()); };
Scott Wheeler's avatar
Scott Wheeler committed
310 311 312 313

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

316 317 318 319
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
320
    virtual void cut() { copy(); clear(); }
Scott Wheeler's avatar
Scott Wheeler committed
321 322 323 324 325

    /**
     * Puts a list of URLs pointing to the files in the current selection on the
     * clipboard.
     */
326
    virtual void copy();
Scott Wheeler's avatar
Scott Wheeler committed
327 328 329 330

    /**
     * Checks the clipboard for local URLs to be inserted into this playlist.
     */
331
    virtual void paste();
Scott Wheeler's avatar
Scott Wheeler committed
332 333 334 335 336 337 338

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

342 343 344 345 346 347
    /**
     * Refreshes the tags of the selection from disk, or all of the files in the
     * list if there is no selection.
     */
    virtual void slotRefresh();

348
    void slotGuessTagInfo(TagGuesser::Type type);
Scott Wheeler's avatar
Scott Wheeler committed
349 350 351 352 353 354

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

357 358 359
    /**
     * Reload the playlist contents from the m3u file.
     */
360
    virtual void slotReload();
361

362 363 364 365 366
    /**
     * 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.
     */
367
    void slotWeightDirty(int column = -1);
368

369 370
    void slotShowPlaying();

371
protected:
372 373 374 375
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
376
    void removeFromDisk(const PlaylistItemList &items);
377

378 379 380
    // the following are all reimplemented from base classes

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

    void addColumn(const QString &label);
Scott Wheeler's avatar
Scott Wheeler committed
392 393 394 395 396 397 398 399

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

402 403 404 405 406 407 408 409
    /**
     * 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);

410 411 412 413 414 415 416 417
    /**
     * 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);

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 428 429 430
     * This signal is emitted when items are added to or removed from the list.
     *
     * \see signalDataChanged()
     * \see signalChanged()
431
     */
432
    void signalCountChanged(Playlist *);
433

434
    /**
435 436 437 438 439 440 441 442 443 444 445
     * 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
446 447 448 449
     * changed.
     */
    void signalChanged();

450
    /**
451
     * This signal is emitted just before a playlist item is removed from the
452 453 454
     * 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.
455
     */
456
    void signalAboutToRemove(PlaylistItem *item);
457

458 459
private:
    void setup();
460 461 462 463 464 465 466

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

469
    /**
Scott Wheeler's avatar
Scott Wheeler committed
470 471
     * Writes \a text to \a item in \a column.  This is used by the inline tag
     * editor.
472
     */
Scott Wheeler's avatar
Scott Wheeler committed
473
    void editTag(PlaylistItem *item, const QString &text, int column);
474 475 476 477 478 479

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

482 483 484 485 486 487 488 489 490 491
    /**
     * 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);

492 493 494 495 496
    /**
     * Build the column "weights" for the weighted width mode.
     */
    void calculateColumnWeights();

497 498 499 500
    PlaylistItem *addFile(const QString &file, bool importPlaylists, PlaylistItem *after);

    void redisplaySearch() { setSearch(m_search); }

501 502 503 504 505 506
    /**
     * 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;

507
private slots:
508

509 510
    void slotUpdateColumnWidths();

511 512 513 514
    /**
     * Show the RMB menu.  Matches the signature for the signal 
     * QListView::contextMenuRequested().
     */
515
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
516 517

    /**
Scott Wheeler's avatar
Scott Wheeler committed
518 519
     * This slot is called when the inline tag editor has completed its editing
     * and starts the process of renaming the values.
520
     *
Scott Wheeler's avatar
Scott Wheeler committed
521
     * \see editTag()
522
     */
Scott Wheeler's avatar
Scott Wheeler committed
523
    void slotInlineEditDone(QListViewItem *, const QString &, int column);
524 525 526 527 528

    /**
     * This starts the renaming process by displaying a line edit if the mouse is in 
     * an appropriate position.
     */
529
    void slotRenameTag();
530 531 532 533 534

    /**
     * Moves the column \a from to the position \a to.  This matches the signature
     * for the signal QHeader::indexChange().
     */
535
    void slotColumnOrderChanged(int, int from, int to);
536 537 538 539 540 541 542

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

545 546 547
    /**
     * Prompts the user to create a new playlist with from the selected items.
     */
548
    void slotCreateGroup() { new Playlist(m_collection, selectedItems()); }
549

550 551 552 553 554 555
    /**
     * 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);

556 557 558 559 560 561 562 563
    /**
     * 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);

564 565
    void slotPlayCurrent();

566
private:
567 568
    PlaylistCollection *m_collection;

569
    StringHash m_members;
570

571
    int m_currentColumn;
572
    int m_processed;
573 574 575 576 577 578

    int m_rmbPasteID;
    int m_rmbEditID;

    int m_selectedCount;

579
    bool m_allowDuplicates;
580
    bool m_polished;
581
    bool m_applySharedSettings;
582
    bool m_fileColumnFullPathSort;
583

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

    PlaylistItemList m_randomList;
Scott Wheeler's avatar
Scott Wheeler committed
594
    PlaylistItemList m_history;
595
    PlaylistSearch m_search;
596

597 598
    bool m_searchEnabled;

599
    PlaylistItem *m_lastSelected;
600

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

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

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

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

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

632 633
// template method implementations

634
template <class ItemType, class CollectionItemType, class CollectionListType>
635 636
ItemType *Playlist::createItem(const FileHandle &file, QListViewItem *after,
			       bool emitChanged)
637
{
638
    CollectionItemType *item = CollectionListType::instance()->lookup(file.absFilePath());
639 640

    if(!item) {
641
	item = new CollectionItemType(file);
642
	setupItem(item);
643 644 645 646

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

653
    if(item && !m_members.insert(file.absFilePath()) || m_allowDuplicates) {
654

655
	ItemType *i = after ? new ItemType(item, this, after) : new ItemType(item, this);
656 657
	setupItem(i);

658 659
        if(!m_randomList.isEmpty() && !m_visibleChanged)
            m_randomList.append(i);
660

661 662 663 664 665 666 667 668
	emit signalCountChanged(this);

	if(emitChanged)
	    emit signalCountChanged(this);

	return i;
    }
    else
669
	return 0;
670 671
}

672 673 674
template <class CollectionItemType, class ItemType, class SiblingType>
void Playlist::createItems(const QValueList<SiblingType *> &siblings)
{
675 676 677 678
    if(siblings.isEmpty())
	return;

    m_disableColumnWidthUpdates = true;
679
    ItemType *newItem = 0;
680 681 682

    QValueListConstIterator<SiblingType *> it = siblings.begin();
    for(; it != siblings.end(); ++it) {
683
	if(!m_members.insert((*it)->file().absFilePath()) || m_allowDuplicates) {
684 685 686 687
	    newItem = new ItemType((*it)->collectionItem(), this, newItem);
	    setupItem(newItem);
	    if(!m_randomList.isEmpty() && !m_visibleChanged)
		m_randomList.append(newItem);
688 689 690 691
	}
    }

    emit signalCountChanged(this);
692
    m_disableColumnWidthUpdates = false;
693
    slotWeightDirty();
694 695
}

696
#endif