playlistsplitter.cpp 23.2 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 201 202
    if(m_playingItem) {
	int offset =  static_cast<Playlist *>(m_playingItem->listView())->columnOffset();
	return m_playingItem->text(PlaylistItem::ArtistColumn + offset);
    }
203
    else
204
	return QString::null;
205 206 207 208
}

QString PlaylistSplitter::playingTrack() const
{
209 210 211 212
    if(m_playingItem) {
	int offset =  static_cast<Playlist *>(m_playingItem->listView())->columnOffset();
	return m_playingItem->text(PlaylistItem::TrackColumn + offset);
    }
213
    else
214
	return QString::null;
215 216 217 218
}

QString PlaylistSplitter::playingList() const
{
219 220
    if(m_playingItem)
	return static_cast<Playlist *>(m_playingItem->listView())->name();
221
    else
222
	return QString::null;
223 224
}

225
void PlaylistSplitter::open(const QString &file)
226
{
227 228 229
    if(file.isEmpty())
	return;

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

243
void PlaylistSplitter::open(const QStringList &files)
244
{
245 246
    if(files.isEmpty())
	return;
247 248 249 250 251 252 253

    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)
254
    {
255
	slotAddToPlaylist(files, m_collection);
256
    }
257
    else
258
	slotAddToPlaylist(files, visiblePlaylist());
259 260 261 262
}

Playlist *PlaylistSplitter::createPlaylist(const QString &name)
{
263
    Playlist *p = new Playlist(m_playlistStack, name);
264 265
    setupPlaylist(p, true);
    return p;
266 267
}

268 269 270 271
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

272
void PlaylistSplitter::slotOpen()
273
{
274
    open(MediaFiles::openDialog(this));
275 276
}

277
void PlaylistSplitter::slotOpenDirectory()
278
{
279
    DirectoryList *l = new DirectoryList(m_directoryList, this, "directoryList");
280

281
    m_directoryQueue.clear();
282
    m_directoryQueueRemove.clear();
283

284
    connect(l, SIGNAL(signalDirectoryAdded(const QString &)),
285
	    this, SLOT(slotQueueDirectory(const QString &)));
286
    connect(l, SIGNAL(signalDirectoryRemoved(const QString &)),
287
	    this, SLOT(slotQueueDirectoryRemove(const QString &)));
288 289

    if(l->exec() == QDialog::Accepted) {
290
	open(m_directoryQueue);
291 292
	for(QStringList::Iterator it = m_directoryQueue.begin(); it !=  m_directoryQueue.end(); it++)
	    m_dirWatch->addDir(*it, false, true);
293

294
	m_directoryList += m_directoryQueue;
295 296 297

	QStringList::Iterator it = m_directoryQueueRemove.begin();
	for(; it !=  m_directoryQueueRemove.end(); it++) {
298
	    m_dirWatch->removeDir(*it);
299
	    m_directoryList.remove(*it);
300
	}
301 302
    }
}
303

304
Playlist *PlaylistSplitter::slotCreatePlaylist()
305 306 307
{
    bool ok;

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

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

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

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

329
    slotAddToPlaylist(dirName, playlist);
330 331 332 333

    return playlist;
}

334
void PlaylistSplitter::slotSelectPlaying()
335
{
336
    if(!m_playingItem)
337 338
	return;

339
    Playlist *l = static_cast<Playlist *>(m_playingItem->listView());
340

341 342
    if(!l)
	return;
343

344
    l->clearSelection();
345 346
    l->setSelected(m_playingItem, true);
    l->ensureItemVisible(m_playingItem);
Nadeem Hasan's avatar
Nadeem Hasan committed
347

348 349
    if(l != visiblePlaylist())
	m_playlistBox->raise(l);
350 351
}

352
void PlaylistSplitter::slotDeleteSelectedItems()
353 354 355
{
    Playlist *p = visiblePlaylist();
    if(p)
356
	p->slotRemoveSelectedItems();
357 358
}

359
void PlaylistSplitter::slotAddToPlaylist(const QString &file, Playlist *list, PlaylistItem *after)
360
{
361 362 363
    if(!after)
	after = static_cast<PlaylistItem *>(list->lastItem());

364
    KApplication::setOverrideCursor(Qt::waitCursor);
365
    addImpl(file, list, after);
366
    list->emitCountChanged();
367
    KApplication::restoreOverrideCursor();
Nadeem Hasan's avatar
Nadeem Hasan committed
368

369 370 371 372
    if(m_editor)
	m_editor->slotUpdateCollection();
}

373 374 375 376 377
void PlaylistSplitter::slotDeletePlaylist()
{
    if(m_playingItem && m_playingItem->listView() == visiblePlaylist())
	m_playingItem = 0;

378
    m_playlistBox->deleteItems();
379 380
}

381
void PlaylistSplitter::slotAddToPlaylist(const QStringList &files, Playlist *list, PlaylistItem *after)
382
{
383 384 385
    if(!after)
	after = static_cast<PlaylistItem *>(list->lastItem());

386
    KApplication::setOverrideCursor(Qt::waitCursor);
387

388
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
389 390
        after = addImpl(*it, list, after);

391
    list->emitCountChanged();
392

393 394 395 396 397 398
    KApplication::restoreOverrideCursor();

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

399 400 401 402 403 404
void PlaylistSplitter::slotSetSearchVisible(bool visible)
{
    m_searchWidget->setShown(visible);
    redisplaySearch();
}

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

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

418
void PlaylistSplitter::slotGuessTagInfo(TagGuesser::Type type)
419
{
420
    visiblePlaylist()->slotGuessTagInfo(type);
421 422 423 424
    if(m_editor)
        m_editor->slotRefresh();
}

425 426 427 428 429 430 431
void PlaylistSplitter::slotRenameFile()
{
    visiblePlaylist()->slotRenameFile();
    if(m_editor)
        m_editor->slotRefresh();
}

432 433 434 435 436 437
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
438 439
    setOpaqueResize(true);

440
    m_playlistBox = new PlaylistBox(this, "playlistBox");
441

442 443 444
    connect(m_playlistBox, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)),
            this, SLOT(slotCreateSearchList(const PlaylistSearch &, const QString &, const QString &)));

445 446 447
    connect(m_playlistBox, SIGNAL(signalCreatePlaylist(const QStringList &)),
	    this, SLOT(slotCreatePlaylist(const QStringList &)));

448 449 450 451 452
    // 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.
453

454 455
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
456 457 458

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

459
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
460

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

464
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(const PlaylistList &)),
465
	    this, SLOT(slotChangePlaylist(const PlaylistList &)));
466

467
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
468

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

474
    CollectionList::initialize(m_playlistStack, m_restore);
475
    m_collection = CollectionList::instance();
476
    setupPlaylist(m_collection, true, "folder_sound", true);
477
    connect(m_collection, SIGNAL(signalCollectionChanged()), m_editor, SLOT(slotUpdateCollection()));
478

479 480 481
    m_history = new HistoryPlaylist(m_playlistStack);
    setupPlaylist(m_history, false, "history", true);

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

489
    // Show the collection on startup.
490
    m_playlistBox->setSelected(0, true);
491 492 493 494
}

void PlaylistSplitter::readConfig()
{
495 496 497 498
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

499 500 501 502 503 504
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
505

506
	if(m_restore) {
507 508 509 510

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

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

512 513 514
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
515
		    Playlist *p = new Playlist(m_playlistStack);
516 517 518 519
		    s >> *p;

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

520
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
521 522 523 524
			setupPlaylist(p);
		    else
			delete p;
		}
525
	    }
526

527
	    m_directoryList = config->readListEntry("DirectoryList");
528
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
529

530
	    connect(m_dirWatch, SIGNAL(dirty(const QString &)),
531
		    this, SLOT(slotDirChanged(const QString &)));
532

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

	    m_dirWatch->startScan();
538
	}
539 540 541

	// restore the list of hidden and shown columns

542
	if(m_collection) {
543
	    for(int i = 0; i < m_collection->columns(); i++)
544
		m_columnNames.append(m_collection->columnText(i));
545
	}
546

547
    }
548
}
549 550 551 552

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

554
    // Save the list of open playlists.
Nadeem Hasan's avatar
Nadeem Hasan committed
555

556
    if(m_restore && m_playlistBox) {
557 558 559

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

560 561
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
562

563 564 565 566
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

567
	    PlaylistList l = m_playlistBox->playlists();
568

569 570
	    for(PlaylistList::Iterator it = l.begin(); it != l.end(); it++)
		s << *(*it);
571

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

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

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

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

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

616
    connect(p, SIGNAL(signalCountChanged(Playlist *)),
617
	    this, SLOT(slotPlaylistCountChanged(Playlist *)));
618

619
    connect(p, SIGNAL(signalAboutToRemove(PlaylistItem *)),
620
	    this, SLOT(slotPlaylistItemRemoved(PlaylistItem *)));
621

622 623
    connect(p, SIGNAL(signalFilesDropped(const QStringList &, Playlist *, PlaylistItem *)),
	    this, SLOT(slotAddToPlaylist(const QStringList &, Playlist *, PlaylistItem *)));
624

625 626
    connect(p, SIGNAL(signalSetNext(PlaylistItem *)),
	    this, SLOT(slotSetNextItem(PlaylistItem *)));
627

628 629 630
    connect(p, SIGNAL(itemRenamed(QListViewItem *)),
	    m_editor, SLOT(slotRefresh()));

631 632 633
    connect(p, SIGNAL(signalCreatePlaylist(const PlaylistItemList &)),
	    this, SLOT(slotCreatePlaylist(const PlaylistItemList &)));

634
    if(icon)
635
	m_playlistBox->createItem(p, icon, raise, sortedFirst);
636

637 638 639 640 641
    if(raise) {
	PlaylistList l;
	l.append(p);
	slotChangePlaylist(l);
    }
642 643
}

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

655
    Playlist *p = new Playlist(file, m_playlistStack, fileInfo.baseName(true));
656
    setupPlaylist(p);
657
    return p;
658 659
}

660 661 662 663 664 665 666
QString PlaylistSplitter::play(PlaylistItem *item)
{
    stop();

    if(!item)
	return QString::null;

667 668
    Playlist *p = static_cast<Playlist *>(item->listView());

669
    if(!p)
670 671
	return QString::null;

672 673
    p->setPlaying(item, true);

674 675
    m_playingItem = item;

676
    if(m_history && p != m_history) {
677 678 679 680 681
	PlaylistItemList l;
	l.append(item);
	m_history->createItems(l);
    }

682 683 684
    return item->absFilePath();
}

685 686 687 688 689 690 691 692 693 694
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);
    }
}

695 696 697 698
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

699
void PlaylistSplitter::slotChangePlaylist(const PlaylistList &l)
700
{
701 702
    if(l.isEmpty()) {
	emit signalPlaylistChanged();
703
	return;
704
    }
705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    // 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;
721

722
    m_nextPlaylistItem = 0;
723 724 725

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

726 727 728
    if(l.count() == 1) {
	m_playlistStack->raiseWidget(l.first());
	m_editor->slotSetItems(playlistSelection());
729 730 731

	if(m_dynamicList != l.first())
	   m_dynamicList = 0;
732 733 734 735 736

	if(m_searchWidget) {
	    m_searchWidget->setSearch(l.first()->search());
	    redisplaySearch();
	}
737
    }
738 739 740 741

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

742
    else {
743
	m_dynamicList = new DynamicPlaylist(l, m_playlistStack, i18n("Dynamic List"));
744 745 746 747 748

	// 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.
	
749 750 751
	setupPlaylist(m_dynamicList, true, 0);
    }

752 753 754 755
    if(current) {
	m_playingItem = 0;
	delete current;
    }
756 757

    emit signalPlaylistChanged();
758 759
}

760
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
761
{
762 763
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
764 765
}

766
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
767
{
768 769
    if(item == m_playingItem)
	m_playingItem = 0;
770 771 772

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
773 774
}

775 776 777 778 779 780 781 782
void PlaylistSplitter::slotCreatePlaylist(const QStringList &files)
{
    Playlist *p = slotCreatePlaylist();
    if(p)
	slotAddToPlaylist(files, p);
}


783
void PlaylistSplitter::slotCreatePlaylist(const PlaylistItemList &items)
784
{
785 786 787
    if(items.isEmpty())
	return;

788
    Playlist *playlist = slotCreatePlaylist();
789

790
    if(!playlist)
791
        return;
792

793
    playlist->createItems(items);
794 795
}

796
void PlaylistSplitter::slotShowSearchResults()
797 798 799 800
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());

801
    PlaylistSearch search = m_searchWidget->search(playlists);
802

803 804
    visiblePlaylist()->setSearch(search);
    redisplaySearch();
805 806
}

807 808
void PlaylistSplitter::slotVisibleColumnsChanged()
{
809 810 811
    m_searchWidget->updateColumns();
    // if(m_searchWidget->searchedColumns(0).count() > 1)
    slotShowSearchResults();
812 813
}

814 815 816 817 818 819 820 821 822
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);
}

823
#include "playlistsplitter.moc"
Nadeem Hasan's avatar
Nadeem Hasan committed
824 825

// vim:ts=8