playlistsplitter.cpp 22.1 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>
20
#include <kinputdialog.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 = KInputDialog::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
Playlist *PlaylistSplitter::slotCreatePlaylistFromDir()
{
    const QString dirName = KFileDialog::getExistingDirectory();
    if(dirName.isEmpty())
        return 0;

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

323
    slotAddToPlaylist(dirName, playlist);
324 325 326 327

    return playlist;
}

328
void PlaylistSplitter::slotSelectPlaying()
329
{
330
    if(!m_playingItem)
331 332
	return;

333
    Playlist *l = static_cast<Playlist *>(m_playingItem->listView());
334

335 336
    if(!l)
	return;
337

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

342 343
    if(l != visiblePlaylist())
	m_playlistBox->raise(l);
344 345
}

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

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

360 361 362 363
    if(m_editor)
	m_editor->slotUpdateCollection();
}

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

    m_playlistBox->deleteItem();
}

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

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

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


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

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

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

412 413 414 415 416 417
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
418 419
    setOpaqueResize(true);

420
    m_playlistBox = new PlaylistBox(this, "playlistBox");
421

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

425 426 427
    connect(m_playlistBox, SIGNAL(signalCreatePlaylist(const QStringList &)),
	    this, SLOT(slotCreatePlaylist(const QStringList &)));

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

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

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

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

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

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

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

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

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

461 462 463 464

    // 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);
465
    connect(m_searchWidget, SIGNAL(signalQueryChanged()), this, SLOT(slotShowSearchResults()));
466 467 468
    connect(CollectionList::instance(), SIGNAL(signalVisibleColumnsChanged()),
	    this, SLOT(slotVisibleColumnsChanged()));

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

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

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

486
	if(m_restore) {
487 488 489 490

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

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

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

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

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

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

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

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

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

	// restore the list of hidden and shown columns

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

527
    }
528
}
529 530 531 532

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

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

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

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

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

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

	    QDataStream s(&f);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if(!item)
	return QString::null;

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

642 643 644
    if (!p)
	return QString::null;

645 646
    p->setPlaying(item, true);

647 648 649 650 651
    m_playingItem = item;

    return item->absFilePath();
}

652 653 654 655 656 657 658 659 660 661
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);
    }
}

662 663 664 665
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

666
void PlaylistSplitter::slotChangePlaylist(const PlaylistList &l)
667
{
668 669
    if(l.isEmpty()) {
	emit signalPlaylistChanged();
670
	return;
671
    }
672

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    // 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;
688

689
    m_nextPlaylistItem = 0;
690 691 692

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

693 694 695
    if(l.count() == 1) {
	m_playlistStack->raiseWidget(l.first());
	m_editor->slotSetItems(playlistSelection());
696 697 698

	if(m_dynamicList != l.first())
	   m_dynamicList = 0;
699 700 701 702 703

	if(m_searchWidget) {
	    m_searchWidget->setSearch(l.first()->search());
	    redisplaySearch();
	}
704
    }
705 706 707 708

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

709
    else {
710
	m_dynamicList = new DynamicPlaylist(l, m_playlistStack, i18n("Dynamic List"));
711 712 713 714 715

	// 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.
	
716 717 718
	setupPlaylist(m_dynamicList, true, 0);
    }

719
    delete current;
720 721

    emit signalPlaylistChanged();
722 723
}

724
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
725
{
726 727
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
728 729
}

730
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
731
{
732 733
    if(item == m_playingItem)
	m_playingItem = 0;
734 735 736

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
737 738
}

739 740 741 742 743 744 745 746
void PlaylistSplitter::slotCreatePlaylist(const QStringList &files)
{
    Playlist *p = slotCreatePlaylist();
    if(p)
	slotAddToPlaylist(files, p);
}


747
void PlaylistSplitter::slotCreatePlaylist(const PlaylistItemList &items)
748
{
749 750 751
    if(items.isEmpty())
	return;

752
    Playlist *playlist = slotCreatePlaylist();
753

754
    if(!playlist)
755
        return;
756

757
    playlist->createItems(items);
758 759
}

760
void PlaylistSplitter::slotShowSearchResults()
761 762 763 764
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());

765
    PlaylistSearch search = m_searchWidget->search(playlists);
766

767 768
    visiblePlaylist()->setSearch(search);
    redisplaySearch();
769 770
}

771 772 773 774 775
void PlaylistSplitter::slotVisibleColumnsChanged()
{
    m_searchWidget->slotUpdateColumns();
    m_searchWidget->slotQueryChanged();
    if(m_searchWidget->searchedColumns(0).count() > 1)
776
        slotShowSearchResults();
777 778
}

779 780 781 782 783 784 785 786 787
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);
}

788
#include "playlistsplitter.moc"
Nadeem Hasan's avatar
Nadeem Hasan committed
789 790

// vim:ts=8