playlistsplitter.cpp 23 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 "historyplaylist.h"
33
#include "mediafiles.h"
34
#include "advancedsearchdialog.h"
35 36

////////////////////////////////////////////////////////////////////////////////
37
// helper functions
38 39
////////////////////////////////////////////////////////////////////////////////

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

48
////////////////////////////////////////////////////////////////////////////////
49
// public methods
50 51
////////////////////////////////////////////////////////////////////////////////

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

    m_dirWatch = new KDirWatch();

65 66
    setupLayout();
    readConfig();
67 68

    m_editor->slotUpdateCollection();
69
}
70

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

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

82
    QStringList names = m_playlistBox->names();
83 84 85

    int playlistNumber = 1;

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

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

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

99
	return startingWith + " " + QString::number(playlistNumber);
100
    }
101 102
}

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

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

111
    // (1) we've asked for a specific next item
112
    if(m_nextPlaylistItem && m_nextPlaylistItem != m_playingItem) {
113 114 115
        i = m_nextPlaylistItem;
        m_nextPlaylistItem = 0;
    }
116
    // (2) play the item after the currently selected item
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 124
    // (3) play the selected item
    else if(playlistSelection().size() > 0) {
125
        i = playlistSelection().first();
126 127
        if(!i)
            i = static_cast<PlaylistItem *>(visiblePlaylist()->firstChild());
128
    }
129 130 131
    // (4) play the first item in the list
    else
        i = static_cast<PlaylistItem *>(visiblePlaylist()->firstChild());
132

133
    return play(i);
134 135
}

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

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

144
    return play(i);
145 146
}

147 148 149 150 151 152
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;
153
    for(PlaylistItemList::Iterator it = list.begin(); it != list.end(); ++it)
154 155 156
        menu->insertItem((*it)->tag()->track(), ++i);
}

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

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

170
    return play(i);
171 172
}

173 174 175 176 177 178 179 180 181
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));
}

182 183
void PlaylistSplitter::stop()
{
184 185
    m_nextPlaylistItem = 0;

186 187 188 189 190
    if(!m_playingItem)
	return;

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

191 192 193
    if(p)
	p->setPlaying(m_playingItem, false);

194
    m_playingItem = 0;
195 196 197 198
}

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

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

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

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

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

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

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

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

264 265 266 267
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

277
    m_directoryQueue.clear();
278
    m_directoryQueueRemove.clear();
279

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

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

290
	m_directoryList += m_directoryQueue;
291 292 293

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

300
Playlist *PlaylistSplitter::slotCreatePlaylist()
301 302 303
{
    bool ok;

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

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

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

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

325
    slotAddToPlaylist(dirName, playlist);
326 327 328 329

    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->slotRemoveSelectedItems();
353 354
}

355
void PlaylistSplitter::slotAddToPlaylist(const QString &file, Playlist *list, PlaylistItem *after)
356
{
357 358 359
    if(!after)
	after = static_cast<PlaylistItem *>(list->lastItem());

360
    KApplication::setOverrideCursor(Qt::waitCursor);
361
    addImpl(file, list, after);
362
    list->emitCountChanged();
363
    KApplication::restoreOverrideCursor();
Nadeem Hasan's avatar
Nadeem Hasan committed
364

365 366 367 368
    if(m_editor)
	m_editor->slotUpdateCollection();
}

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

374
    m_playlistBox->deleteItems();
375 376
}

377
void PlaylistSplitter::slotAddToPlaylist(const QStringList &files, Playlist *list, PlaylistItem *after)
378
{
379 380 381
    if(!after)
	after = static_cast<PlaylistItem *>(list->lastItem());

382
    KApplication::setOverrideCursor(Qt::waitCursor);
383

384
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
385 386
        after = addImpl(*it, list, after);

387
    list->emitCountChanged();
388

389 390 391 392 393 394
    KApplication::restoreOverrideCursor();

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

395 396 397 398 399 400
void PlaylistSplitter::slotSetSearchVisible(bool visible)
{
    m_searchWidget->setShown(visible);
    redisplaySearch();
}

401 402
void PlaylistSplitter::slotAdvancedSearch()
{
403 404
    AdvancedSearchDialog *d =
	new AdvancedSearchDialog(uniquePlaylistName(i18n("Search Playlist")), this);
405
    AdvancedSearchDialog::Result r = d->exec();
406
    delete d;
407 408 409 410 411

    if(r.result == AdvancedSearchDialog::Accepted) {
	SearchPlaylist *p = new SearchPlaylist(r.search, m_playlistStack, r.playlistName);
	setupPlaylist(p, true, "find");
    }
412
}
413

414
void PlaylistSplitter::slotGuessTagInfo(TagGuesser::Type type)
415
{
416
    visiblePlaylist()->slotGuessTagInfo(type);
417 418 419 420
    if(m_editor)
        m_editor->slotRefresh();
}

421 422 423 424 425 426 427
void PlaylistSplitter::slotRenameFile()
{
    visiblePlaylist()->slotRenameFile();
    if(m_editor)
        m_editor->slotRefresh();
}

428 429 430 431 432 433
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
434 435
    setOpaqueResize(true);

436
    m_playlistBox = new PlaylistBox(this, "playlistBox");
437

438 439 440
    connect(m_playlistBox, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)),
            this, SLOT(slotCreateSearchList(const PlaylistSearch &, const QString &, const QString &)));

441 442 443
    connect(m_playlistBox, SIGNAL(signalCreatePlaylist(const QStringList &)),
	    this, SLOT(slotCreatePlaylist(const QStringList &)));

444 445 446 447 448
    // 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.
449

450 451
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
452 453 454

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

455
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
456

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

460
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(const PlaylistList &)),
461
	    this, SLOT(slotChangePlaylist(const PlaylistList &)));
462

463
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
464

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

470
    CollectionList::initialize(m_playlistStack, m_restore);
471
    m_collection = CollectionList::instance();
472
    setupPlaylist(m_collection, true, "folder_sound", true);
473
    connect(m_collection, SIGNAL(signalCollectionChanged()), m_editor, SLOT(slotUpdateCollection()));
474

475 476 477
    m_history = new HistoryPlaylist(m_playlistStack);
    setupPlaylist(m_history, false, "history", true);

478
    // Create the search widget -- this must be done after the CollectionList is created.
479
    m_searchWidget = new SearchWidget(editorSplitter, "searchWidget");
480
    editorSplitter->moveToFirst(m_searchWidget);
481
    connect(m_searchWidget, SIGNAL(signalQueryChanged()), this, SLOT(slotShowSearchResults()));
482 483 484
    connect(CollectionList::instance(), SIGNAL(signalVisibleColumnsChanged()),
	    this, SLOT(slotVisibleColumnsChanged()));

485
    // Show the collection on startup.
486
    m_playlistBox->setSelected(0, true);
487 488 489 490
}

void PlaylistSplitter::readConfig()
{
491 492 493 494
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

495 496 497 498 499 500
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
501

502
	if(m_restore) {
503 504 505 506

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

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

508 509 510
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
511
		    Playlist *p = new Playlist(m_playlistStack);
512 513 514 515
		    s >> *p;

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

516
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
517 518 519 520
			setupPlaylist(p);
		    else
			delete p;
		}
521
	    }
522

523
	    m_directoryList = config->readListEntry("DirectoryList");
524
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
525

526
	    connect(m_dirWatch, SIGNAL(dirty(const QString &)),
527
		    this, SLOT(slotDirChanged(const QString &)));
528

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

	    m_dirWatch->startScan();
534
	}
535 536 537

	// restore the list of hidden and shown columns

538
	if(m_collection) {
539
	    for(int i = 0; i < m_collection->columns(); i++)
540
		m_columnNames.append(m_collection->columnText(i));
541
	}
542

543
    }
544
}
545 546 547 548

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

550
    // Save the list of open playlists.
Nadeem Hasan's avatar
Nadeem Hasan committed
551

552
    if(m_restore && m_playlistBox) {
553 554 555

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

556 557
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
558

559 560 561 562
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

563
	    PlaylistList l = m_playlistBox->playlists();
564

565 566
	    for(PlaylistList::Iterator it = l.begin(); it != l.end(); it++)
		s << *(*it);
567

568
	    f.close();
569 570 571
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
572
	    config->writeEntry("DirectoryList", m_directoryList);
573
	    config->writeEntry("SortColumn", m_collection->sortColumn());
574
	    config->writeEntry("PlaylistSplitterSizes", sizes());
575 576
	}
    }
577 578
}

579
PlaylistItem *PlaylistSplitter::addImpl(const QString &file, Playlist *list, PlaylistItem *after)
580 581 582 583 584 585
{
    processEvents();
    QFileInfo fileInfo(QDir::cleanDirPath(file));
    if(fileInfo.exists()) {
        if(fileInfo.isDir()) {
            QDir dir(fileInfo.filePath());
586
            QStringList dirContents = dir.entryList();
587 588
            for(QStringList::Iterator it = dirContents.begin(); it != dirContents.end(); ++it)
                if(*it != "." && *it != "..")
589
                    after = addImpl(fileInfo.filePath() + QDir::separator() + *it, list, after);
590 591
        }
        else {
592
            if(MediaFiles::isMediaFile(file))
593
		after = list->createItem(fileInfo, QString::null, after, false);
594
	    else if(MediaFiles::isPlaylistFile(file))
595 596
		openPlaylist(fileInfo.absFilePath());
        }
Nadeem Hasan's avatar
Nadeem Hasan committed
597
    }
598
    return after;
599 600
}

601
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon, bool sortedFirst)
602
{
603 604
    connect(p, SIGNAL(signalSelectionChanged(const PlaylistItemList &)),
	    m_editor, SLOT(slotSetItems(const PlaylistItemList &)));
605 606 607 608 609 610 611

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

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

612
    connect(p, SIGNAL(signalCountChanged(Playlist *)),
613
	    this, SLOT(slotPlaylistCountChanged(Playlist *)));
614

615
    connect(p, SIGNAL(signalAboutToRemove(PlaylistItem *)),
616
	    this, SLOT(slotPlaylistItemRemoved(PlaylistItem *)));
617

618 619
    connect(p, SIGNAL(signalFilesDropped(const QStringList &, Playlist *, PlaylistItem *)),
	    this, SLOT(slotAddToPlaylist(const QStringList &, Playlist *, PlaylistItem *)));
620

621 622
    connect(p, SIGNAL(signalSetNext(PlaylistItem *)),
	    this, SLOT(slotSetNextItem(PlaylistItem *)));
623

624 625 626
    connect(p, SIGNAL(itemRenamed(QListViewItem *)),
	    m_editor, SLOT(slotRefresh()));

627 628 629
    connect(p, SIGNAL(signalCreatePlaylist(const PlaylistItemList &)),
	    this, SLOT(slotCreatePlaylist(const PlaylistItemList &)));

630
    if(icon)
631
	m_playlistBox->createItem(p, icon, raise, sortedFirst);
632

633 634 635 636 637
    if(raise) {
	PlaylistList l;
	l.append(p);
	slotChangePlaylist(l);
    }
638 639
}

640 641 642
Playlist *PlaylistSplitter::openPlaylist(const QString &file)
{
    QFileInfo fileInfo(file);
643 644 645
    if(!fileInfo.exists() ||
       !fileInfo.isFile() ||
       !fileInfo.isReadable() ||
646 647
       m_playlistFiles.insert(fileInfo.absFilePath()))
    {
648
	return 0;
649
    }
650

651
    Playlist *p = new Playlist(file, m_playlistStack, fileInfo.baseName(true));
652
    setupPlaylist(p);
653
    return p;
654 655
}

656 657 658 659 660 661 662
QString PlaylistSplitter::play(PlaylistItem *item)
{
    stop();

    if(!item)
	return QString::null;

663 664
    Playlist *p = static_cast<Playlist *>(item->listView());

665
    if(!p)
666 667
	return QString::null;

668 669
    p->setPlaying(item, true);

670 671
    m_playingItem = item;

672
    if(m_history && p != m_history) {
673 674 675 676 677
	PlaylistItemList l;
	l.append(item);
	m_history->createItems(l);
    }

678 679 680
    return item->absFilePath();
}

681 682 683 684 685 686 687 688 689 690
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);
    }
}

691 692 693 694
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

695
void PlaylistSplitter::slotChangePlaylist(const PlaylistList &l)
696
{
697 698
    if(l.isEmpty()) {
	emit signalPlaylistChanged();
699
	return;
700
    }
701

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    // 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;
717

718
    m_nextPlaylistItem = 0;
719 720 721

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

722 723 724
    if(l.count() == 1) {
	m_playlistStack->raiseWidget(l.first());
	m_editor->slotSetItems(playlistSelection());
725 726 727

	if(m_dynamicList != l.first())
	   m_dynamicList = 0;
728 729 730 731 732

	if(m_searchWidget) {
	    m_searchWidget->setSearch(l.first()->search());
	    redisplaySearch();
	}
733
    }
734 735 736 737

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

738
    else {
739
	m_dynamicList = new DynamicPlaylist(l, m_playlistStack, i18n("Dynamic List"));
740 741 742 743 744

	// 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.
	
745 746 747
	setupPlaylist(m_dynamicList, true, 0);
    }

748 749 750 751
    if(current) {
	m_playingItem = 0;
	delete current;
    }
752 753

    emit signalPlaylistChanged();
754 755
}

756
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
757
{
758 759
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
760 761
}

762
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
763
{
764 765
    if(item == m_playingItem)
	m_playingItem = 0;
766 767 768

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
769 770
}

771 772 773 774 775 776 777 778
void PlaylistSplitter::slotCreatePlaylist(const QStringList &files)
{
    Playlist *p = slotCreatePlaylist();
    if(p)
	slotAddToPlaylist(files, p);
}


779
void PlaylistSplitter::slotCreatePlaylist(const PlaylistItemList &items)
780
{
781 782 783
    if(items.isEmpty())
	return;

784
    Playlist *playlist = slotCreatePlaylist();
785

786
    if(!playlist)
787
        return;
788

789
    playlist->createItems(items);
790 791
}

792
void PlaylistSplitter::slotShowSearchResults()
793 794 795 796
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());

797
    PlaylistSearch search = m_searchWidget->search(playlists);
798

799 800
    visiblePlaylist()->setSearch(search);
    redisplaySearch();
801 802
}

803 804
void PlaylistSplitter::slotVisibleColumnsChanged()
{
805 806 807
    m_searchWidget->updateColumns();
    // if(m_searchWidget->searchedColumns(0).count() > 1)
    slotShowSearchResults();
808 809
}

810 811 812 813 814 815 816 817 818
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);
}

819
#include "playlistsplitter.moc"
Nadeem Hasan's avatar
Nadeem Hasan committed
820 821

// vim:ts=8