playlistsplitter.cpp 17.3 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          playlistsplitter.cpp  -  description
                             -------------------
    begin                : Fri Sep 13 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7 8 9 10 11 12 13 14 15 16 17
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

18
#include <klocale.h>
19
#include <kiconloader.h>
20
#include <kapplication.h>
21
#include <kstandarddirs.h>
22
#include <kmessagebox.h>
23
#include <kdirwatch.h>
Nadeem Hasan's avatar
Nadeem Hasan committed
24
#include <klineeditdlg.h>
25 26
#include <kdebug.h>

27
#include <qtimer.h>
28

29
#include "playlistitem.h"
30
#include "playlistsplitter.h"
31
#include "collectionlist.h"
32
#include "directorylist.h"
33 34
#include "playlist.h"

35 36
QStringList *PlaylistSplitter::m_mediaExtensions = 0;
QStringList *PlaylistSplitter::m_listExtensions = 0;
37 38

////////////////////////////////////////////////////////////////////////////////
39
// helper functions
40 41
////////////////////////////////////////////////////////////////////////////////

42
void processEvents()
43
{
44 45 46
    static int processed = 0;
    if(processed == 0)
        kapp->processEvents();
47
    processed = (processed + 1) % 5;
48 49
}

50
////////////////////////////////////////////////////////////////////////////////
51
// public methods
52 53
////////////////////////////////////////////////////////////////////////////////

54
PlaylistSplitter::PlaylistSplitter(QWidget *parent, bool restore, const char *name) : QSplitter(Qt::Horizontal, parent, name), 
55
										      m_playingItem(0), m_restore(restore), m_nextPlaylistItem(0)
56
{
57 58 59 60 61 62 63 64
    if(!m_mediaExtensions && !m_listExtensions) {
	m_mediaExtensions = new QStringList();
	m_listExtensions = new QStringList();

	m_mediaExtensions->append("mp3");
	m_mediaExtensions->append("ogg");
	m_listExtensions->append("m3u");
    }
65 66 67

    setupLayout();
    readConfig();
68 69

    m_editor->slotUpdateCollection();
70
}
71

72
PlaylistSplitter::~PlaylistSplitter()
73
{
Scott Wheeler's avatar
Scott Wheeler committed
74
    delete m_dirWatch;
75
    saveConfig();
76 77
}

78
QString PlaylistSplitter::uniquePlaylistName(const QString &startingWith, bool useParenthesis)
79
{
80
    if(!m_playlistBox)
81
	return QString::null;
82

83
    QStringList names = m_playlistBox->names();
84 85 86 87 88 89

    int playlistNumber = 1;

    // while the list contains more than zero instances of the generated 
    // string...

90 91 92 93
    if(useParenthesis) {
	while(names.contains(startingWith + " (" + QString::number(playlistNumber) + ")") != 0)
	    playlistNumber++;
	
94
	return startingWith + " (" + QString::number(playlistNumber) + ")";	
95 96 97 98 99 100
    }
    else
    {
	while(names.contains(startingWith + ' ' + QString::number(playlistNumber)) != 0)
	    playlistNumber++;
	
101
	return startingWith + " " + QString::number(playlistNumber);
102
    }
103 104
}

105
QString PlaylistSplitter::playNextFile(bool random, bool loopPlaylist)
106
{
107 108
    PlaylistItem *i;

109 110 111
    // Four basic cases here:  (1) We've asked for a specific next item, (2) play
    // the item that's after the currently playing item, (3) play the selected 
    // item or (4) play the first item in the list.
112

113
    if(m_nextPlaylistItem && m_nextPlaylistItem != m_playingItem) {
114 115 116
        i = m_nextPlaylistItem;
        m_nextPlaylistItem = 0;
    }
117
    else if(m_playingItem) {
118 119
        Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
        i = p->nextItem(m_playingItem, random);
120
        if(!i && loopPlaylist)
121
            i = static_cast<PlaylistItem *>(p->firstChild());
122 123
    }
    else {
124
        i = playlistSelection().first();
125 126
        if(!i)
            i = static_cast<PlaylistItem *>(visiblePlaylist()->firstChild());
127 128
    }

129
    return play(i);
130 131
}

132
QString PlaylistSplitter::playPreviousFile(bool random)
133
{
134
    if(!m_playingItem)
135
	return QString::null;
136

137 138
    Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
    PlaylistItem *i = p->previousItem(m_playingItem, random);
139

140
    return play(i);
141 142
}

143 144 145 146 147 148 149 150
QString PlaylistSplitter::playSelectedFile()
{
    if(playlistSelection().isEmpty())
	return QString::null;
    else
	return play(playlistSelection().first());
}

151 152 153 154
QString PlaylistSplitter::playFirstFile()
{
    Playlist *p = visiblePlaylist();
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());
155

156
    return play(i);
157 158
}

159 160
void PlaylistSplitter::stop()
{
161 162
    m_nextPlaylistItem = 0;

163 164 165 166 167 168 169
    if(!m_playingItem)
	return;

    Playlist *p = static_cast<Playlist *>(m_playingItem->listView());

    p->setPlaying(m_playingItem, false);
    m_playingItem = 0;
170 171 172 173
}

QString PlaylistSplitter::playingArtist() const
{
174 175
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::ArtistColumn);
176
    else
177
	return QString::null;
178 179 180 181
}

QString PlaylistSplitter::playingTrack() const
{
182 183
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::TrackColumn);
184
    else
185
	return QString::null;
186 187 188 189
}

QString PlaylistSplitter::playingList() const
{
190 191
    if(m_playingItem)
	return static_cast<Playlist *>(m_playingItem->listView())->name();
192
    else
193
	return QString::null;
194 195
}

196
QString PlaylistSplitter::extensionsString(const QStringList &extensions, const QString &type) // static
197
{
198 199 200 201 202 203 204 205 206 207 208 209
    QStringList l;

    for(QStringList::ConstIterator it = extensions.begin(); it != extensions.end(); ++it)
	l.append(QString("*." + (*it)));

    // i.e. "*.m3u, *.mp3|Media Files"

    QString s = l.join(" ");

    if(type != QString::null)
	s += "|" + type + " (" + l.join(", ") + ")";

210
    return s;
211 212
}

213 214
void PlaylistSplitter::open(const QString &file) 
{
215 216 217
    if(file.isEmpty())
	return;

218
    if(visiblePlaylist() == m_collection || 
219 220
       KMessageBox::questionYesNo(this, i18n("Do you want to add this item to the current list or to the collection list?"), 
				  QString::null, KGuiItem(i18n("Current")), KGuiItem(i18n("Collection"))) == KMessageBox::No)
221
	slotAddToPlaylist(file, m_collection);
222
    else
223
	slotAddToPlaylist(file, visiblePlaylist());
224 225 226 227
}

void PlaylistSplitter::open(const QStringList &files) 
{
228 229 230
    if(files.isEmpty())
	return;
    
231
    if(visiblePlaylist() == m_collection || 
232 233
       KMessageBox::questionYesNo(this, i18n("Do you want to add these items to the current list or to the collection list?"), 
				  QString::null, KGuiItem(i18n("Current")), KGuiItem(i18n("Collection"))) == KMessageBox::No)
234
	slotAddToPlaylist(files, m_collection);
235
    else
236
	slotAddToPlaylist(files, visiblePlaylist());
237 238 239 240
}

Playlist *PlaylistSplitter::createPlaylist(const QString &name)
{
241
    Playlist *p = new Playlist(m_playlistStack, name.latin1());
242 243
    setupPlaylist(p, true);
    return p;
244 245
}

246 247 248 249
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

250
void PlaylistSplitter::slotOpen()
251
{
252
    QStringList files = KFileDialog::getOpenFileNames(QString::null, 
253
						      extensionsString((*m_mediaExtensions + *m_listExtensions), i18n("Media Files")));
254 255 256
    open(files);
}

257
void PlaylistSplitter::slotOpenDirectory()
258
{ 
259
    DirectoryList *l = new DirectoryList(m_directoryList, this, "directoryList");
260

261
    m_directoryQueue.clear();
262
    m_directoryQueueRemove.clear();
263

264 265
    connect(l, SIGNAL(signalDirectoryAdded(const QString &)), this, SLOT(slotQueueDirectory(const QString &)));
    connect(l, SIGNAL(signalDirectoryRemoved(const QString &)), this, SLOT(slotQueueDirectoryRemove(const QString &)));
266 267

    if(l->exec() == QDialog::Accepted) {
268
	open(m_directoryQueue);
269 270 271
	for(QStringList::Iterator it = m_directoryQueue.begin(); it !=  m_directoryQueue.end(); it++)
	    m_dirWatch->addDir(*it, false, true);
	    
272
	m_directoryList += m_directoryQueue;
273 274
	for(QStringList::Iterator it = m_directoryQueueRemove.begin(); it !=  m_directoryQueueRemove.end(); it++) {
	    m_dirWatch->removeDir(*it);
275
	    m_directoryList.remove(*it);
276
	}
277 278
    }
}
279
void PlaylistSplitter::slotSetEditorVisible(bool visible)
280 281
{
    if(visible)
282
	m_editor->show();
283
    else
284
	m_editor->hide();
285 286
}

287
Playlist *PlaylistSplitter::slotCreatePlaylist()
288 289 290
{
    bool ok;

291
    // If this text is changed, please also change it in PlaylistBox::duplicate().
292

Nadeem Hasan's avatar
Nadeem Hasan committed
293 294 295
    QString name = KLineEditDlg::getText(i18n("Create New Playlist"), 
	i18n("Please enter a name for the new playlist:"),
	uniquePlaylistName(), &ok);
296
    if(ok)
297
	return createPlaylist(name);
298
    else
299
	return 0;
300 301
}

302
void PlaylistSplitter::slotSelectPlaying()
303
{
304
    if(!m_playingItem)
305 306
	return;

307
    Playlist *l = static_cast<Playlist *>(m_playingItem->listView());
308
	
309 310
    if(!l)
	return;
311

312
    l->clearSelection();
313 314
    l->setSelected(m_playingItem, true);
    l->ensureItemVisible(m_playingItem);
Nadeem Hasan's avatar
Nadeem Hasan committed
315

316
    m_playlistBox->raise(l);
317 318
}

319
void PlaylistSplitter::slotDeleteSelectedItems()
320 321 322
{
    Playlist *p = visiblePlaylist();
    if(p)
323
	p->slotDeleteSelectedItems();
324 325
}

326 327 328 329 330
void PlaylistSplitter::slotAddToPlaylist(const QString &file, Playlist *list)
{
    KApplication::setOverrideCursor(Qt::waitCursor);
    addImpl(file, list);
    KApplication::restoreOverrideCursor();
Nadeem Hasan's avatar
Nadeem Hasan committed
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    if(m_editor)
	m_editor->slotUpdateCollection();
}

void PlaylistSplitter::slotAddToPlaylist(const QStringList &files, Playlist *list)
{
    KApplication::setOverrideCursor(Qt::waitCursor);
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
        addImpl(*it, list);
    KApplication::restoreOverrideCursor();

    if(m_editor)
	m_editor->slotUpdateCollection();
}

347 348 349 350 351 352 353
void PlaylistSplitter::slotGuessTagInfo()
{
    visiblePlaylist()->slotGuessTagInfo();
    if(m_editor)
        m_editor->slotRefresh();
}

354 355 356 357 358 359
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
360
    m_playlistBox = new PlaylistBox(this, "playlistBox");
361 362 363 364 365 366 367

    // Create a splitter to go between the playlists and the editor.

    QSplitter *editorSplitter = new QSplitter(Qt::Vertical, this, "editorSplitter");

    // Create the playlist and the editor.

368 369
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
370 371 372

    // Make the editor as small as possible (or at least as small as recommended)

373
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
374 375 376 377

    // Make the connection that will update the selected playlist when a 
    // selection is made in the playlist box.

378 379
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(const QValueList<Playlist *> &)), 
	    this, SLOT(slotChangePlaylist(const QValueList<Playlist *> &)));
380

381
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
382 383 384 385

    // Create the collection list; this should always exist.  This has a 
    // slightly different creation process than normal playlists (since it in
    // fact is a subclass) so it is created here rather than by using 
386
    // slotCreatePlaylist().
387

388
    CollectionList::initialize(m_playlistStack, m_restore);
389 390
    m_collection = CollectionList::instance();
    setupPlaylist(m_collection, true, "folder_sound");
391
    connect(m_collection, SIGNAL(signalCollectionChanged()), m_editor, SLOT(slotUpdateCollection()));
392
    connect(m_collection, SIGNAL(signalRequestPlaylistCreation(const QValueList<QFileInfo> &)), this, SLOT(slotCreatePlaylist(const QValueList<QFileInfo> &)));
393 394

    // Show the collection on startup.
395
    m_playlistBox->setSelected(0, true);
396 397 398 399
}

void PlaylistSplitter::readConfig()
{
400 401 402 403
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

404 405 406 407 408 409 410
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
	
411
	if(m_restore) {
412 413 414 415

	    QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";

	    QFile f(playlistsFile);
Nadeem Hasan's avatar
Nadeem Hasan committed
416

417 418 419
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
420
		    Playlist *p = new Playlist(m_playlistStack);
421 422 423 424
		    s >> *p;

		    // check to see if we've alredy loaded this item before continuing

425
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
426 427 428 429
			setupPlaylist(p);
		    else
			delete p;
		}
430
	    }
431

432
	    m_directoryList = config->readListEntry("DirectoryList");
433
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
434 435 436 437 438 439 440 441

	    m_dirWatch = new KDirWatch();
	    connect(m_dirWatch, SIGNAL(dirty(const QString &)), this, SLOT(slotDirChanged(const QString &)));

            for(QStringList::Iterator it = m_directoryList.begin(); it != m_directoryList.end(); ++it)
		m_dirWatch->addDir(*it, false, true);

	    m_dirWatch->startScan();
442
	}
443 444 445

	// restore the list of hidden and shown columns

446 447
	if(m_collection)
	    for(int i = 0; i < m_collection->columns(); i++)
448
		m_columnNames.append(m_collection->columnText(i));
449

450
    }
451
}
452 453 454 455

void PlaylistSplitter::saveConfig()
{
    KConfig *config = KGlobal::config();
456

457
    // Save the list of open playlists.
Nadeem Hasan's avatar
Nadeem Hasan committed
458

459
    if(m_restore && m_playlistBox) {
460 461 462

	// Start at item 1.  We want to skip the collection list.

463 464 465 466 467 468 469
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
	
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

470
	    PlaylistList l = m_playlistBox->playlists();
471

472 473
	    for(PlaylistList::Iterator it = l.begin(); it != l.end(); it++)
		s << *(*it);
474

475
	    f.close();
476 477 478
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
479
	    config->writeEntry("DirectoryList", m_directoryList);
480
	    config->writeEntry("SortColumn", m_collection->sortColumn());
481
	    config->writeEntry("PlaylistSplitterSizes", sizes());
482 483
	}
    }
484 485
}

486 487 488 489 490 491 492 493 494 495 496 497 498
void PlaylistSplitter::addImpl(const QString &file, Playlist *list)
{
    processEvents();
    QFileInfo fileInfo(QDir::cleanDirPath(file));
    if(fileInfo.exists()) {
        if(fileInfo.isDir()) {
            QDir dir(fileInfo.filePath());
            QStringList dirContents=dir.entryList();
            for(QStringList::Iterator it = dirContents.begin(); it != dirContents.end(); ++it)
                if(*it != "." && *it != "..")
                    addImpl(fileInfo.filePath() + QDir::separator() + *it, list);
        }
        else {
499
            QString extension = fileInfo.extension(false).lower();
500
            if(m_mediaExtensions->contains(extension) > 0)
501
		list->createItem(fileInfo);
502
	    else if(m_listExtensions->contains(extension) > 0)
503 504
		openPlaylist(fileInfo.absFilePath());
        }
Nadeem Hasan's avatar
Nadeem Hasan committed
505
    }
506 507
}

508
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon)
509
{
510 511 512 513
    connect(p, SIGNAL(signalSelectionChanged(const PlaylistItemList &)), m_editor, SLOT(slotSetItems(const PlaylistItemList &)));
    connect(p, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalDoubleClicked()));
    connect(p, SIGNAL(signalNumberOfItemsChanged(Playlist *)), this, SLOT(slotPlaylistCountChanged(Playlist *)));
    connect(p, SIGNAL(signalAboutToRemove(PlaylistItem *)), this, SLOT(slotPlaylistItemRemoved(PlaylistItem *)));
514 515
    connect(p, SIGNAL(signalFilesDropped(const QStringList &, Playlist *)), this, SLOT(slotAddToPlaylist(const QStringList &, Playlist *)));
    connect(p, SIGNAL(signalSetNext(PlaylistItem *)), this, SLOT(slotSetNextItem(PlaylistItem *)));
516

517
    m_playlistBox->createItem(p, icon, raise);
518

519
    if(raise)
520
	m_playlistStack->raiseWidget(p);
521 522
}

523 524 525
Playlist *PlaylistSplitter::openPlaylist(const QString &file)
{
    QFileInfo fileInfo(file);
526
    if(!fileInfo.exists() || !fileInfo.isFile() || !fileInfo.isReadable() || m_playlistFiles.insert(fileInfo.absFilePath()))
527
	return 0;
528

529
    Playlist *p = new Playlist(file, m_playlistStack, fileInfo.baseName(true).latin1());
530
    setupPlaylist(p);
531
    return p;
532 533
}

534 535 536 537 538 539 540
QString PlaylistSplitter::play(PlaylistItem *item)
{
    stop();

    if(!item)
	return QString::null;

541 542 543 544
    Playlist *p = static_cast<Playlist *>(item->listView());

    p->setPlaying(item, true);

545 546 547 548 549
    m_playingItem = item;

    return item->absFilePath();
}

550 551 552 553
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

554
void PlaylistSplitter::slotChangePlaylist(const QValueList<Playlist *> &l)
555
{
556
    if(l.isEmpty())
557
	return;
558 559

    m_nextPlaylistItem = 0; 
560
    m_playlistStack->raiseWidget(l.first());
561
    m_editor->slotSetItems(playlistSelection());
562
    emit signalPlaylistChanged();    
563 564
}

565
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
566
{
567 568
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
569 570
}

571
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
572
{
573 574
    if(item == m_playingItem)
	m_playingItem = 0;
575 576 577

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
578 579
}

580 581 582
void PlaylistSplitter::slotCreatePlaylist(const QValueList<QFileInfo> &fileInfos)
{
    Playlist *playlist = slotCreatePlaylist();
583
    if(!playlist)
584
        return;
585

586 587 588 589 590 591
    QValueList<QFileInfo>::ConstIterator it = fileInfos.begin();
    QValueList<QFileInfo>::ConstIterator end = fileInfos.end();
    for(; it != end; ++it)
        playlist->createItem(*it);
}

592
#include "playlistsplitter.moc"
Nadeem Hasan's avatar
Nadeem Hasan committed
593 594

// vim:ts=8