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 10.2 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          playlist.h  -  description
                             -------------------
    begin                : Sat Feb 16 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <klistview.h>
22
#include <kurldrag.h>
23 24

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

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

class KPopupMenu;
32
class KActionMenu;
33 34

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

class PlaylistSearch;

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

42 43
typedef QValueList<Playlist *> PlaylistList;

44 45 46
class Playlist : public KListView
{
    Q_OBJECT
47

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

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

61 62
    virtual ~Playlist();

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

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

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

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

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

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

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

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

113 114
    void createItems(const PlaylistItemList &siblings);

115 116
    QString fileName() const { return m_fileName; }
    void setFileName(const QString &n) { m_fileName = n; }
117

118 119 120 121
    void hideColumn(int c);
    void showColumn(int c);
    bool isColumnVisible(int c) const;

122
    /**
123
     * If m_playlistName has no value -- i.e. the name has not been set to
124
     * something other than the filename, this returns the filename less the
125
     * extension.  If m_playlistName does have a value, this returns that.
126
     */
127
    QString name() const;
128

129 130
    /**
     * This sets a name for the playlist that is \e different from the file name.
131
     */
132
    void setName(const QString &n);
133

134
    int count() const { return childCount(); }
135

136
    /**
137
     * This gets the next item to be played.
138
     */
139 140
    PlaylistItem *nextItem(PlaylistItem *current, bool random = false);
    PlaylistItem *previousItem(PlaylistItem *current, bool random = false);
141

142
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
143

144 145 146 147 148 149 150 151
    /**
     * 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
152
    bool playing() const;
153

Scott Wheeler's avatar
Scott Wheeler committed
154 155 156
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
157
     */
Scott Wheeler's avatar
Scott Wheeler committed
158
    void updateLeftColumn();
159

160 161
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

Scott Wheeler's avatar
Scott Wheeler committed
162 163 164
    PlaylistSearch search() const { return m_search; }
    void setSearch(const PlaylistSearch &s) { m_search = s; }

165 166
    void emitNumberOfItemsChanged() { emit signalNumberOfItemsChanged(this); }

167
public slots:
168
    /**
169
     * Remove the currently selected items from the playlist and disk.
170
     */
171
    void slotDeleteSelectedItems() { deleteFromDisk(selectedItems()); };
172 173
    void slotSetNext();

174 175 176 177
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
178
    virtual void cut() { copy(); clear(); }
179
    virtual void copy();
180
    virtual void paste();
181
    virtual void clear();
182
    virtual void selectAll() { KListView::selectAll(true); }
183

184 185 186 187 188 189
    /**
     * Refreshes the tags of the selection from disk, or all of the files in the
     * list if there is no selection.
     */
    virtual void slotRefresh();

190 191
    void slotGuessTagInfoFile();
    void slotGuessTagInfoInternet();
192 193
    void slotRenameFile();

194 195 196
    /**
     * Reload the playlist contents from the m3u file.
     */
197
    virtual void slotReload();
198

199
protected:
200 201 202 203 204 205
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
    void deleteFromDisk(const PlaylistItemList &items);

206
    virtual bool eventFilter(QObject* watched, QEvent* e);
207
    virtual QDragObject *dragObject(QWidget *parent);
208
    virtual QDragObject *dragObject() { return dragObject(this); }
209
    virtual bool canDecode(QMimeSource *s);
210
    virtual void decode(QMimeSource *s);
211
    virtual void contentsDropEvent(QDropEvent *e);
Scott Wheeler's avatar
Scott Wheeler committed
212
    virtual void showEvent(QShowEvent *e);
213
    virtual bool acceptDrag(QDropEvent *e) const { return KURLDrag::canDecode(e); }
214

215 216 217 218
    /**
     * Though it's somewhat obvious, this function will stat the file, so only use it when
     * you're out of a performance critical loop.
     */
219
    static QString resolveSymLinks(const QFileInfo &file);
Scott Wheeler's avatar
Scott Wheeler committed
220

221 222 223
    KPopupMenu *rmbMenu() { return m_rmbMenu; }
    const KPopupMenu *rmbMenu() const { return m_rmbMenu; }

224 225
    virtual void polish();

226
signals:
227 228 229
    /**
     * This signal is connected to PlaylistItem::refreshed() in the
     * PlaylistItem class.
230
     */
231
    void signalDataChanged();
232

233
    /**
234
     * This is emitted when the playlist selection is changed.  This is used
235
     * primarily to notify the TagEditor of the new data.
236
     */
237
    void signalSelectionChanged(const PlaylistItemList &selection);
238

239
    /**
240
     * This is connected to the PlaylistBox::Item to let it know when the
241 242
     * playlist's name has changed.
     */
243
    void signalNameChanged(const QString &fileName);
244

245 246 247
    /**
     * This signal is emited when items are added to or removed from the list.
     */
248 249
    void signalNumberOfItemsChanged(Playlist *);
    void signalDoubleClicked();
250

251 252 253 254 255 256 257
    /**
     * This is the union of signalDataChanged() and signalNumberOfItemsChanged().
     * It is emited with either quantity or value of the PlaylistItems are
     * changed.
     */
    void signalChanged();

258
    /**
259
     * This signal is emitted just before a playlist item is removed from the
260 261
     * list.
     */
262
    void signalAboutToRemove(PlaylistItem *item);
263
    void signalFilesDropped(const QStringList &files, Playlist *);
264
    void signalSetNext(PlaylistItem *item);
265
    void signalVisibleColumnsChanged();
266

267 268
private:
    void setup();
269
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
270 271 272
    /**
     * Save the tag for an individual items.
     */
273
    void applyTag(QListViewItem *item, const QString &text, int column);
274
    int leftMostVisibleColumn() const;
275

276 277 278 279 280 281
    /**
     * 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;

282
private slots:
283 284
    void slotEmitSelected() { emit signalSelectionChanged(selectedItems()); }
    void slotShowRMBMenu(QListViewItem *item, const QPoint &point, int column);
285 286 287 288 289 290 291 292 293 294

    /**
     * This slot applys the tag for 
     */
    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.
     */
295
    void slotRenameTag();
296
    void slotColumnOrderChanged(int, int from, int to);
297
    void slotToggleColumnVisible(int column);
298 299

private:
300
    SortedStringList m_members;
301
    int m_currentColumn;
302 303
    int m_processed;
    bool m_allowDuplicates;
304 305 306 307 308
    /**
     * This is used to indicate if the list of visible items has changed (via a 
     * call to setVisibleItems()) while random play is playing.
     */
    static bool m_visibleChanged;
Scott Wheeler's avatar
Scott Wheeler committed
309
    PlaylistItemList m_history;
310

311
    QString m_fileName;
312

313
    /**
314
     * Used to store the text for inline editing before it is changed so that
315 316 317 318
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
319 320 321 322 323

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

326 327 328
    KPopupMenu *m_rmbMenu;
    KPopupMenu *m_headerMenu;
    KActionMenu *m_columnVisibleAction;
329

330 331
    int m_rmbPasteID;
    int m_rmbEditID;
332

333 334
    static PlaylistItem *m_playingItem;
    static int m_leftColumn;
335 336

    PlaylistItemList m_randomList;
Scott Wheeler's avatar
Scott Wheeler committed
337
    PlaylistSearch m_search;
338 339

    bool m_polished;
340 341
};

342 343 344
QDataStream &operator<<(QDataStream &s, const Playlist &p);
QDataStream &operator>>(QDataStream &s, Playlist &p);

345
#endif