playlistsplitter.cpp 22 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 <kstandarddirs.h>
19
#include <kmessagebox.h>
Nadeem Hasan's avatar
Nadeem Hasan committed
20
#include <klineeditdlg.h>
21
#include <kcmdlineargs.h>
22 23
#include <kdebug.h>

24
#include <qpopupmenu.h>
25

26
#include "playlistsplitter.h"
27
#include "searchwidget.h"
28
#include "directorylist.h"
29
#include "playlistsearch.h"
30
#include "dynamicplaylist.h"
31
#include "searchplaylist.h"
32
#include "mediafiles.h"
33 34

////////////////////////////////////////////////////////////////////////////////
35
// helper functions
36 37
////////////////////////////////////////////////////////////////////////////////

38
void processEvents()
39
{
40 41 42
    static int processed = 0;
    if(processed == 0)
        kapp->processEvents();
43
    processed = (processed + 1) % 5;
44 45
}

46
////////////////////////////////////////////////////////////////////////////////
47
// public methods
48 49
////////////////////////////////////////////////////////////////////////////////

50
PlaylistSplitter::PlaylistSplitter(QWidget *parent, const char *name) :
51
    QSplitter(Qt::Horizontal, parent, name),
52
    m_playingItem(0), m_searchWidget(0), m_dynamicList(0),
53
    m_nextPlaylistItem(0)
54
{
55 56 57 58 59 60 61 62
#ifndef NO_DEBUG
    m_restore = KCmdLineArgs::parsedArgs()->isSet("restore");
#else
    m_restore = true;
#endif

    m_dirWatch = new KDirWatch();

63 64
    setupLayout();
    readConfig();
65 66

    m_editor->slotUpdateCollection();
67
}
68

69
PlaylistSplitter::~PlaylistSplitter()
70
{
Scott Wheeler's avatar
Scott Wheeler committed
71
    delete m_dirWatch;
72
    saveConfig();
73 74
}

75
QString PlaylistSplitter::uniquePlaylistName(const QString &startingWith, bool useParenthesis)
76
{
77
    if(!m_playlistBox)
78
	return QString::null;
79

80
    QStringList names = m_playlistBox->names();
81 82 83

    int playlistNumber = 1;

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

87 88 89
    if(useParenthesis) {
	while(names.contains(startingWith + " (" + QString::number(playlistNumber) + ")") != 0)
	    playlistNumber++;
90 91

	return startingWith + " (" + QString::number(playlistNumber) + ")";
92
    }
93
    else {
94 95
	while(names.contains(startingWith + ' ' + QString::number(playlistNumber)) != 0)
	    playlistNumber++;
96

97
	return startingWith + " " + QString::number(playlistNumber);
98
    }
99 100
}

101
QString PlaylistSplitter::playNextFile(bool random, bool loopPlaylist)
102
{
103 104
    PlaylistItem *i;

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

109
    // (1) we've asked for a specific next item
110
    if(m_nextPlaylistItem && m_nextPlaylistItem != m_playingItem) {
111 112 113
        i = m_nextPlaylistItem;
        m_nextPlaylistItem = 0;
    }
114
    // (2) play the item after the currently selected item
115
    else if(m_playingItem) {
116 117
        Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
        i = p->nextItem(m_playingItem, random);
118
        if(!i && loopPlaylist)
119
            i = static_cast<PlaylistItem *>(p->firstChild());
120
    }
121 122
    // (3) play the selected item
    else if(playlistSelection().size() > 0) {
123
        i = playlistSelection().first();
124 125
        if(!i)
            i = static_cast<PlaylistItem *>(visiblePlaylist()->firstChild());
126
    }
127 128 129
    // (4) play the first item in the list
    else
        i = static_cast<PlaylistItem *>(visiblePlaylist()->firstChild());
130

131
    return play(i);
132 133
}

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

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

142
    return play(i);
143 144
}

145 146 147 148 149 150
void PlaylistSplitter::populatePlayHistoryMenu(QPopupMenu* menu, bool random)
{
    Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
    PlaylistItemList list = p->historyItems(m_playingItem, random);
    menu->clear();
    int i = 0;
151
    for(PlaylistItemList::Iterator it = list.begin(); it != list.end(); ++it)
152 153 154
        menu->insertItem((*it)->tag()->track(), ++i);
}

155 156 157 158 159 160 161 162
QString PlaylistSplitter::playSelectedFile()
{
    if(playlistSelection().isEmpty())
	return QString::null;
    else
	return play(playlistSelection().first());
}

163 164 165 166
QString PlaylistSplitter::playFirstFile()
{
    Playlist *p = visiblePlaylist();
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());
167

168
    return play(i);
169 170
}

171 172 173 174 175 176 177 178 179
QString PlaylistSplitter::playRandomFile()
{
    Playlist *p = visiblePlaylist();
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());

    // Not exactly random (the first item won't be taken into account)
    return play(p->nextItem(i, true));
}

180 181
void PlaylistSplitter::stop()
{
182 183
    m_nextPlaylistItem = 0;

184 185 186 187 188
    if(!m_playingItem)
	return;

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

189 190 191
    if(p)
	p->setPlaying(m_playingItem, false);

192
    m_playingItem = 0;
193 194 195 196
}

QString PlaylistSplitter::playingArtist() const
{
197 198
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::ArtistColumn);
199
    else
200
	return QString::null;
201 202 203 204
}

QString PlaylistSplitter::playingTrack() const
{
205 206
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::TrackColumn);
207
    else
208
	return QString::null;
209 210 211 212
}

QString PlaylistSplitter::playingList() const
{
213 214
    if(m_playingItem)
	return static_cast<Playlist *>(m_playingItem->listView())->name();
215
    else
216
	return QString::null;
217 218
}

219
void PlaylistSplitter::open(const QString &file)
220
{
221 222 223
    if(file.isEmpty())
	return;

224 225 226 227 228
    if(visiblePlaylist() == m_collection ||
       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")),
229 230
				  KGuiItem(i18n("Collection"))) == KMessageBox::No)
    {
231
	slotAddToPlaylist(file, m_collection);
232
    }
233
    else
234
	slotAddToPlaylist(file, visiblePlaylist());
235 236
}

237
void PlaylistSplitter::open(const QStringList &files)
238
{
239 240
    if(files.isEmpty())
	return;
241 242 243 244 245 246 247

    if(visiblePlaylist() == m_collection ||
       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)
248
    {
249
	slotAddToPlaylist(files, m_collection);
250
    }
251
    else
252
	slotAddToPlaylist(files, visiblePlaylist());
253 254 255 256
}

Playlist *PlaylistSplitter::createPlaylist(const QString &name)
{
257
    Playlist *p = new Playlist(m_playlistStack, name);
258 259
    setupPlaylist(p, true);
    return p;
260 261
}

262 263 264 265
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

266
void PlaylistSplitter::slotOpen()
267
{
268
    open(MediaFiles::openDialog(this));
269 270
}

271
void PlaylistSplitter::slotOpenDirectory()
272
{
273
    DirectoryList *l = new DirectoryList(m_directoryList, this, "directoryList");
274

275
    m_directoryQueue.clear();
276
    m_directoryQueueRemove.clear();
277

278
    connect(l, SIGNAL(signalDirectoryAdded(const QString &)),
279
	    this, SLOT(slotQueueDirectory(const QString &)));
280
    connect(l, SIGNAL(signalDirectoryRemoved(const QString &)),
281
	    this, SLOT(slotQueueDirectoryRemove(const QString &)));
282 283

    if(l->exec() == QDialog::Accepted) {
284
	open(m_directoryQueue);
285 286
	for(QStringList::Iterator it = m_directoryQueue.begin(); it !=  m_directoryQueue.end(); it++)
	    m_dirWatch->addDir(*it, false, true);
287

288
	m_directoryList += m_directoryQueue;
289 290 291

	QStringList::Iterator it = m_directoryQueueRemove.begin();
	for(; it !=  m_directoryQueueRemove.end(); it++) {
292
	    m_dirWatch->removeDir(*it);
293
	    m_directoryList.remove(*it);
294
	}
295 296
    }
}
297

298
Playlist *PlaylistSplitter::slotCreatePlaylist()
299 300 301
{
    bool ok;

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

304
    QString name = KLineEditDlg::getText(i18n("Create New Playlist"),
Nadeem Hasan's avatar
Nadeem Hasan committed
305 306
	i18n("Please enter a name for the new playlist:"),
	uniquePlaylistName(), &ok);
307
    if(ok)
308
	return createPlaylist(name);
309
    else
310
	return 0;
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
Playlist *PlaylistSplitter::slotCreatePlaylistFromDir()
{
    const QString dirName = KFileDialog::getExistingDirectory();
    if(dirName.isEmpty())
        return 0;

    Playlist *playlist = slotCreatePlaylist();
    if(!playlist)
        return 0;

    const QStringList files = QDir(dirName).entryList(QDir::Files);
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
        slotAddToPlaylist(dirName + "/" + *it, playlist);

    return playlist;
}

330
void PlaylistSplitter::slotSelectPlaying()
331
{
332
    if(!m_playingItem)
333 334
	return;

335
    Playlist *l = static_cast<Playlist *>(m_playingItem->listView());
336

337 338
    if(!l)
	return;
339

340
    l->clearSelection();
341 342
    l->setSelected(m_playingItem, true);
    l->ensureItemVisible(m_playingItem);
Nadeem Hasan's avatar
Nadeem Hasan committed
343

344 345
    if(l != visiblePlaylist())
	m_playlistBox->raise(l);
346 347
}

348
void PlaylistSplitter::slotDeleteSelectedItems()
349 350 351
{
    Playlist *p = visiblePlaylist();
    if(p)
352
	p->slotDeleteSelectedItems();
353 354
}

355 356 357 358
void PlaylistSplitter::slotAddToPlaylist(const QString &file, Playlist *list)
{
    KApplication::setOverrideCursor(Qt::waitCursor);
    addImpl(file, list);
359
    list->emitNumberOfItemsChanged();
360
    KApplication::restoreOverrideCursor();
Nadeem Hasan's avatar
Nadeem Hasan committed
361

362 363 364 365
    if(m_editor)
	m_editor->slotUpdateCollection();
}

366 367 368 369 370 371 372 373
void PlaylistSplitter::slotDeletePlaylist()
{
    if(m_playingItem && m_playingItem->listView() == visiblePlaylist())
	m_playingItem = 0;

    m_playlistBox->deleteItem();
}

374 375 376 377 378
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);
379
    list->emitNumberOfItemsChanged();
380 381 382 383 384 385
    KApplication::restoreOverrideCursor();

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

386 387 388 389 390 391 392
void PlaylistSplitter::slotSetSearchVisible(bool visible)
{
    m_searchWidget->setShown(visible);
    redisplaySearch();
}


393
void PlaylistSplitter::slotGuessTagInfoFile()
394
{
395 396 397 398 399 400 401 402
    visiblePlaylist()->slotGuessTagInfoFile();
    if(m_editor)
        m_editor->slotRefresh();
}

void PlaylistSplitter::slotGuessTagInfoInternet()
{
    visiblePlaylist()->slotGuessTagInfoInternet();
403 404 405 406
    if(m_editor)
        m_editor->slotRefresh();
}

407 408 409 410 411 412 413
void PlaylistSplitter::slotRenameFile()
{
    visiblePlaylist()->slotRenameFile();
    if(m_editor)
        m_editor->slotRefresh();
}

414 415 416 417 418 419
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
420 421
    setOpaqueResize(true);

422
    m_playlistBox = new PlaylistBox(this, "playlistBox");
423

424 425 426
    connect(m_playlistBox, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)),
            this, SLOT(slotCreateSearchList(const PlaylistSearch &, const QString &, const QString &)));

427 428 429 430 431
    // 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.
432

433 434
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
435 436 437

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

438
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
439

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

443
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(const PlaylistList &)),
444
	    this, SLOT(slotChangePlaylist(const PlaylistList &)));
445

446
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
447

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

453
    CollectionList::initialize(m_playlistStack, m_restore);
454
    m_collection = CollectionList::instance();
455
    setupPlaylist(m_collection, true, "folder_sound", true);
456
    connect(m_collection, SIGNAL(signalCollectionChanged()), m_editor, SLOT(slotUpdateCollection()));
457
    connect(m_collection, SIGNAL(signalRequestPlaylistCreation(const PlaylistItemList &)),
458
	    this, SLOT(slotCreatePlaylist(const PlaylistItemList &)));
459

460 461 462 463

    // Create the search widget -- this must be done after the CollectionList is created.
    m_searchWidget = new SearchWidget(editorSplitter, CollectionList::instance(), "searchWidget");
    editorSplitter->moveToFirst(m_searchWidget);
464
    connect(m_searchWidget, SIGNAL(signalQueryChanged()), this, SLOT(slotShowSearchResults()));
465 466 467
    connect(CollectionList::instance(), SIGNAL(signalVisibleColumnsChanged()),
	    this, SLOT(slotVisibleColumnsChanged()));

468
    // Show the collection on startup.
469
    m_playlistBox->setSelected(0, true);
470 471 472 473
}

void PlaylistSplitter::readConfig()
{
474 475 476 477
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

478 479 480 481 482 483
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
484

485
	if(m_restore) {
486 487 488 489

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

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

491 492 493
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
494
		    Playlist *p = new Playlist(m_playlistStack);
495 496 497 498
		    s >> *p;

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

499
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
500 501 502 503
			setupPlaylist(p);
		    else
			delete p;
		}
504
	    }
505

506
	    m_directoryList = config->readListEntry("DirectoryList");
507
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
508

509
	    connect(m_dirWatch, SIGNAL(dirty(const QString &)),
510
		    this, SLOT(slotDirChanged(const QString &)));
511

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

	    m_dirWatch->startScan();
517
	}
518 519 520

	// restore the list of hidden and shown columns

521
	if(m_collection) {
522
	    for(int i = 0; i < m_collection->columns(); i++)
523
		m_columnNames.append(m_collection->columnText(i));
524
	}
525

526
    }
527
}
528 529 530 531

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

533
    // Save the list of open playlists.
Nadeem Hasan's avatar
Nadeem Hasan committed
534

535
    if(m_restore && m_playlistBox) {
536 537 538

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

539 540
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
541

542 543 544 545
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

546
	    PlaylistList l = m_playlistBox->playlists();
547

548 549
	    for(PlaylistList::Iterator it = l.begin(); it != l.end(); it++)
		s << *(*it);
550

551
	    f.close();
552 553 554
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
555
	    config->writeEntry("DirectoryList", m_directoryList);
556
	    config->writeEntry("SortColumn", m_collection->sortColumn());
557
	    config->writeEntry("PlaylistSplitterSizes", sizes());
558 559
	}
    }
560 561
}

562 563 564 565 566 567 568
void PlaylistSplitter::addImpl(const QString &file, Playlist *list)
{
    processEvents();
    QFileInfo fileInfo(QDir::cleanDirPath(file));
    if(fileInfo.exists()) {
        if(fileInfo.isDir()) {
            QDir dir(fileInfo.filePath());
569
            QStringList dirContents = dir.entryList();
570 571 572 573 574
            for(QStringList::Iterator it = dirContents.begin(); it != dirContents.end(); ++it)
                if(*it != "." && *it != "..")
                    addImpl(fileInfo.filePath() + QDir::separator() + *it, list);
        }
        else {
575
            if(MediaFiles::isMediaFile(file))
576
		list->createItem(fileInfo, QString::null, 0, false);
577
	    else if(MediaFiles::isPlaylistFile(file))
578 579
		openPlaylist(fileInfo.absFilePath());
        }
Nadeem Hasan's avatar
Nadeem Hasan committed
580
    }
581 582
}

583
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon, bool sortedFirst)
584
{
585 586
    connect(p, SIGNAL(signalSelectionChanged(const PlaylistItemList &)),
	    m_editor, SLOT(slotSetItems(const PlaylistItemList &)));
587 588 589 590 591 592 593

    connect(p, SIGNAL(doubleClicked(QListViewItem *)),
	    this, SIGNAL(signalActivated()));

    connect(p, SIGNAL(returnPressed(QListViewItem *)), 
	    this, SIGNAL(signalActivated()));

594 595
    connect(p, SIGNAL(signalNumberOfItemsChanged(Playlist *)),
	    this, SLOT(slotPlaylistCountChanged(Playlist *)));
596

597
    connect(p, SIGNAL(signalAboutToRemove(PlaylistItem *)),
598
	    this, SLOT(slotPlaylistItemRemoved(PlaylistItem *)));
599

600
    connect(p, SIGNAL(signalFilesDropped(const QStringList &, Playlist *)),
601
	    this, SLOT(slotAddToPlaylist(const QStringList &, Playlist *)));
602

603 604
    connect(p, SIGNAL(signalSetNext(PlaylistItem *)),
	    this, SLOT(slotSetNextItem(PlaylistItem *)));
605

606
    if(icon)
607
	m_playlistBox->createItem(p, icon, raise, sortedFirst);
608

609 610 611 612 613
    if(raise) {
	PlaylistList l;
	l.append(p);
	slotChangePlaylist(l);
    }
614 615
}

616 617 618
Playlist *PlaylistSplitter::openPlaylist(const QString &file)
{
    QFileInfo fileInfo(file);
619 620 621
    if(!fileInfo.exists() ||
       !fileInfo.isFile() ||
       !fileInfo.isReadable() ||
622 623
       m_playlistFiles.insert(fileInfo.absFilePath()))
    {
624
	return 0;
625
    }
626

627
    Playlist *p = new Playlist(file, m_playlistStack, fileInfo.baseName(true));
628
    setupPlaylist(p);
629
    return p;
630 631
}

632 633 634 635 636 637 638
QString PlaylistSplitter::play(PlaylistItem *item)
{
    stop();

    if(!item)
	return QString::null;

639 640 641 642
    Playlist *p = static_cast<Playlist *>(item->listView());

    p->setPlaying(item, true);

643 644 645 646 647
    m_playingItem = item;

    return item->absFilePath();
}

648 649 650 651 652 653 654 655 656 657
void PlaylistSplitter::redisplaySearch()
{
    if(!m_searchWidget->isVisible() || visiblePlaylist()->search().isEmpty())
	visiblePlaylist()->setItemsVisible(visiblePlaylist()->items(), true);
    else {
	Playlist::setItemsVisible(visiblePlaylist()->search().matchedItems(), true);
	Playlist::setItemsVisible(visiblePlaylist()->search().unmatchedItems(), false);
    }
}

658 659 660 661
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

662
void PlaylistSplitter::slotChangePlaylist(const PlaylistList &l)
663
{
664 665
    if(l.isEmpty()) {
	emit signalPlaylistChanged();
666
	return;
667
    }
668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    // Save the current dynamic list so that we can delete it when we're done
    // showing the next list.  The two situations are that we're either showing
    // an existing, non-dynamic list or that we're creating a dynamic list; in 
    // both cases we want to get rid of the current one.
    //
    // If in fact the currently visible list *is not* a dynamic list, then
    // m_dyanmicList will simply be zero, making deleting it at the end of this
    // method just a no-op.
    //
    // And finally, because we will end up doing a recursive call to this method
    // to show the dynamic playlist (second case calls the first case), we want
    // to make sure that in that case we don't delete the very thing we're
    // being asked to show.  (Hence the conditional assignment.)

    Playlist *current = l.first() != m_dynamicList ? m_dynamicList : 0;
684

685
    m_nextPlaylistItem = 0;
686 687 688

    // First case:  We're just showing one, currently existing list.

689 690 691
    if(l.count() == 1) {
	m_playlistStack->raiseWidget(l.first());
	m_editor->slotSetItems(playlistSelection());
692 693 694

	if(m_dynamicList != l.first())
	   m_dynamicList = 0;
695 696 697 698 699

	if(m_searchWidget) {
	    m_searchWidget->setSearch(l.first()->search());
	    redisplaySearch();
	}
700
    }
701 702 703 704

    // Second case: There are multiple playlists in our list, so we need to create
    // a new "dynamic list" that is the union of these playlists.

705
    else {
706
	m_dynamicList = new DynamicPlaylist(l, m_playlistStack, i18n("Dynamic List"));
707 708 709 710 711

	// Note that this call will end up making a recursive call to this
	// method, but in that call since there will only be one list, it will
	// take the "first case" above.
	
712 713 714
	setupPlaylist(m_dynamicList, true, 0);
    }

715
    delete current;
716 717

    emit signalPlaylistChanged();
718 719
}

720
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
721
{
722 723
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
724 725
}

726
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
727
{
728 729
    if(item == m_playingItem)
	m_playingItem = 0;
730 731 732

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
733 734
}

735
void PlaylistSplitter::slotCreatePlaylist(const PlaylistItemList &items)
736
{
737 738 739
    if(items.isEmpty())
	return;

740
    Playlist *playlist = slotCreatePlaylist();
741

742
    if(!playlist)
743
        return;
744

745
    playlist->createItems(items);
746 747
}

748
void PlaylistSplitter::slotShowSearchResults()
749 750 751 752
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());

753
    PlaylistSearch search = m_searchWidget->search(playlists);
754

755 756
    visiblePlaylist()->setSearch(search);
    redisplaySearch();
757 758
}

759 760 761 762 763
void PlaylistSplitter::slotVisibleColumnsChanged()
{
    m_searchWidget->slotUpdateColumns();
    m_searchWidget->slotQueryChanged();
    if(m_searchWidget->searchedColumns(0).count() > 1)
764
        slotShowSearchResults();
765 766
}

767 768 769 770 771 772 773 774 775
void PlaylistSplitter::slotCreateSearchList(const PlaylistSearch &search, 
					    const QString &searchCategory,
					    const QString &name)
{
    SearchPlaylist *p = new SearchPlaylist(search, m_playlistStack, name);
    m_playlistBox->createSearchItem(p, searchCategory);
    setupPlaylist(p, false, 0);
}

776
#include "playlistsplitter.moc"
Nadeem Hasan's avatar
Nadeem Hasan committed
777 778

// vim:ts=8