playlistsplitter.cpp 17.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 <klocale.h>
19
#include <kiconloader.h>
20
#include <kapplication.h>
21
#include <kstandarddirs.h>
22
#include <kmessagebox.h>
23 24
#include <kdebug.h>

25
#include <qinputdialog.h>
26
#include <qtimer.h>
27

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

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

////////////////////////////////////////////////////////////////////////////////
38
// helper functions
39 40
////////////////////////////////////////////////////////////////////////////////

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

49
////////////////////////////////////////////////////////////////////////////////
50
// public methods
51 52
////////////////////////////////////////////////////////////////////////////////

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

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

    setupLayout();
    readConfig();
67
    connect(this, SIGNAL(signalDoubleClicked()), this, SLOT(slotClearNextItem()));
68
}
69

70
PlaylistSplitter::~PlaylistSplitter()
71
{
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 84 85 86

    int playlistNumber = 1;

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

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

102
QString PlaylistSplitter::playNextFile(bool random)
103
{
104 105 106
    Playlist *p;
    PlaylistItem *i;

107 108
    if(m_playingItem) {
	m_playingItem->setPixmap(0, 0);
109

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

    if(i) {
	i->setPixmap(0, QPixmap(UserIcon("playing")));
129
	m_playingItem = i;
130
	return i->absFilePath();
131 132
    }
    else
133
	return QString::null;
134 135
}

136
QString PlaylistSplitter::playPreviousFile(bool random)
137
{
138 139 140
    if(m_playingItem) {
	Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
	PlaylistItem *i = p->previousItem(m_playingItem, random);
141

142
	m_playingItem->setPixmap(0, 0);
143 144
	i->setPixmap(0, QPixmap(UserIcon("playing")));
	
145
	m_playingItem = i;
146
	return i->absFilePath();
147 148
    }
    else
149
	return QString::null;
150 151
}

152
QString PlaylistSplitter::playSelectedFile()
153
{
154
    stop();
155

156
    PlaylistItemList items = playlistSelection();
157

158 159 160 161
    if(!items.isEmpty()) {
	PlaylistItem *i = items.first();
	i->setPixmap(0, QPixmap(UserIcon("playing")));
	
162
	m_playingItem = i;
163
	return i->absFilePath();
164 165
    }
    else
166
	return QString::null;
167
}
168

169 170 171 172
QString PlaylistSplitter::playSelectedFileNext()
{
    PlaylistItemList items = playlistSelection();

173 174 175
    if(items.isEmpty()) {
	m_nextPlaylistItem = 0L;
	return QString::null;
176 177 178 179 180 181
    }

    m_nextPlaylistItem = items.first();
    return m_nextPlaylistItem->absFilePath();
}

182 183 184
QString PlaylistSplitter::playFirstFile()
{
    stop();
185

186 187
    Playlist *p = visiblePlaylist();
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());
188

189 190
    if(i) {
	i->setPixmap(0, QPixmap(UserIcon("playing")));
191
	m_playingItem = i;
192

193
	return i->absFilePath();
194 195
    }
    else
196
	return QString::null;
197 198
}

199 200
void PlaylistSplitter::stop()
{
201 202 203
    if(m_playingItem) {
	m_playingItem->setPixmap(0, 0);
	m_playingItem = 0;
204 205 206 207 208
    }
}

QString PlaylistSplitter::playingArtist() const
{
209 210
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::ArtistColumn);
211
    else
212
	return QString::null;
213 214 215 216
}

QString PlaylistSplitter::playingTrack() const
{
217 218
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::TrackColumn);
219
    else
220
	return QString::null;
221 222 223 224
}

QString PlaylistSplitter::playingList() const
{
225 226
    if(m_playingItem)
	return static_cast<Playlist *>(m_playingItem->listView())->name();
227
    else
228
	return QString::null;
229 230
}

231
void PlaylistSplitter::addToPlaylist(const QString &file, Playlist *list)
232
{
233 234 235 236
    KApplication::setOverrideCursor(Qt::waitCursor);
    addImpl(file, list);
    KApplication::restoreOverrideCursor();
    
237
    if(m_editor)
238
	m_editor->slotUpdateCollection();
239 240
}

241
void PlaylistSplitter::addToPlaylist(const QStringList &files, Playlist *list)
242
{
243 244 245 246 247
    KApplication::setOverrideCursor(Qt::waitCursor);
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
        addImpl(*it, list);
    KApplication::restoreOverrideCursor();

248
    if(m_editor)
249
	m_editor->slotUpdateCollection();
250 251
}

252
QString PlaylistSplitter::extensionsString(const QStringList &extensions, const QString &type) // static
253
{
254 255 256 257 258 259 260 261 262 263 264 265
    QStringList l;

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

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

    QString s = l.join(" ");

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

266
    return s;
267 268
}

269 270
void PlaylistSplitter::open(const QString &file) 
{
271 272 273
    if(file.isEmpty())
	return;

274
    if(visiblePlaylist() == m_collection || 
275 276
       KMessageBox::questionYesNo(this, i18n("Do you want to add this item to the current list or to the collection list?"), 
				  QString::null, KGuiItem(i18n("Current")), KGuiItem(i18n("Collection"))) == KMessageBox::No)
277
	addToPlaylist(file, m_collection);
278
    else
279
	addToPlaylist(file, visiblePlaylist());
280 281 282 283
}

void PlaylistSplitter::open(const QStringList &files) 
{
284 285 286
    if(files.isEmpty())
	return;
    
287
    if(visiblePlaylist() == m_collection || 
288 289
       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)
290
	addToPlaylist(files, m_collection);
291
    else
292 293 294 295 296 297 298 299
	addToPlaylist(files, visiblePlaylist());
}

Playlist *PlaylistSplitter::createPlaylist(const QString &name)
{
    Playlist *p = new Playlist(this, m_playlistStack, name.latin1());
    setupPlaylist(p, true);
    return p;
300 301
}

302 303 304 305
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

306
void PlaylistSplitter::slotOpen()
307
{
308
    QStringList files = KFileDialog::getOpenFileNames(QString::null, 
309
						      extensionsString((*m_mediaExtensions + *m_listExtensions), i18n("Media Files")));
310 311 312
    open(files);
}

313
void PlaylistSplitter::slotOpenDirectory()
314
{ 
315
    DirectoryList *l = new DirectoryList(m_directoryList, this, "directoryList");
316

317
    m_directoryQueue.clear();
318

319 320
    connect(l, SIGNAL(signalDirectoryAdded(const QString &)), this, SLOT(slotQueueDirectory(const QString &)));
    connect(l, SIGNAL(signalDirectoryRemoved(const QString &)), this, SLOT(slotQueueDirectoryRemove(const QString &)));
321 322

    if(l->exec() == QDialog::Accepted) {
323 324 325 326
	open(m_directoryQueue);
	m_directoryList += m_directoryQueue;
	for(QStringList::Iterator it = m_directoryQueueRemove.begin(); it !=  m_directoryQueueRemove.end(); it++)
	    m_directoryList.remove(*it);
327 328
    }
}
329
void PlaylistSplitter::slotSetEditorVisible(bool visible)
330 331
{
    if(visible)
332
	m_editor->show();
333
    else
334
	m_editor->hide();
335 336
}

337
Playlist *PlaylistSplitter::slotCreatePlaylist()
338 339 340
{
    bool ok;

341
    // If this text is changed, please also change it in PlaylistBox::duplicate().
342 343 344 345

    QString name = QInputDialog::getText(i18n("New Playlist..."), i18n("Please enter a name for the new playlist:"),
					 QLineEdit::Normal, uniquePlaylistName(), &ok);
    if(ok)
346
	return createPlaylist(name);
347
    else
348
	return 0;
349 350
}

351
void PlaylistSplitter::slotSelectPlaying()
352
{
353
    if(!m_playingItem)
354 355
	return;

356
    Playlist *l = static_cast<Playlist *>(m_playingItem->listView());
357
	
358 359
    if(!l)
	return;
360

361
    l->clearSelection();
362 363
    l->setSelected(m_playingItem, true);
    l->ensureItemVisible(m_playingItem);
364
    
365
    m_playlistBox->raise(l);
366 367
}

368
void PlaylistSplitter::slotDeleteSelectedItems()
369 370 371
{
    Playlist *p = visiblePlaylist();
    if(p)
372
	p->slotDeleteSelectedItems();
373 374
}

375
void PlaylistSplitter::slotToggleColumnVisible(int column)
376
{
377 378 379
    m_visibleColumns[column] = ! m_visibleColumns[column];
    if(visiblePlaylist())
	setupColumns(visiblePlaylist());
380 381
}

382 383 384 385 386 387
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
388
    m_playlistBox = new PlaylistBox(this, "playlistBox");
389 390 391 392 393 394 395

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

396 397
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
398 399 400

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

401
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
402 403 404 405

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

406 407
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(Playlist *)), 
	    this, SLOT(slotChangePlaylist(Playlist *)));
408

409
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
410 411 412 413

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

416 417 418
    CollectionList::initialize(this, m_playlistStack, m_restore);
    m_collection = CollectionList::instance();
    setupPlaylist(m_collection, true, "folder_sound");
419 420

    // Show the collection on startup.
421
    m_playlistBox->setSelected(0, true);
422 423 424 425
}

void PlaylistSplitter::readConfig()
{
426 427 428 429
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

430 431 432 433 434 435 436
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
	
437
	if(m_restore) {
438 439 440 441

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

	    QFile f(playlistsFile);
442
	    
443 444 445
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
446
		    Playlist *p = new Playlist(this, m_playlistStack);
447 448 449 450
		    s >> *p;

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

451
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
452 453 454 455
			setupPlaylist(p);
		    else
			delete p;
		}
456
	    }
457

458
	    m_directoryList = config->readListEntry("DirectoryList");
459
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
460
	}
461 462 463

	// restore the list of hidden and shown columns

464
	if(m_collection) {
465
	    // the last column is just a filler
466
	    m_visibleColumns.resize(m_collection->columns() - 1, true);
467 468 469 470 471
	    QValueList<int> l = config->readIntListEntry("VisibleColumns");

	    uint i = 0;
	    for(QValueList<int>::Iterator it = l.begin(); it != l.end(); ++it) {
		if(! bool(*it)) {
472
		    m_visibleColumns[i] = bool(*it);
473
		    m_collection->hideColumn(i);
474
		}
475

476
		// while we're looping go ahead and populate m_columnNames
477
		
478
		m_columnNames.append(m_collection->columnText(i));
479

480 481
		i++;
	    }
482
	    setupColumns(m_collection);
483
	}
484 485 486 487 488 489 490
    }
}	


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

492 493
    // Save the list of open playlists.
    
494
    if(m_restore && m_playlistBox) {
495 496 497

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

498 499 500 501 502 503 504
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
	
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

505
	    QPtrList<Playlist> l = m_playlistBox->playlists();
506

507 508
	    for(Playlist *p = l.first(); p; p = l.next())
		s << *p;
509

510
	    f.close();
511 512 513
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
514
	    config->writeEntry("DirectoryList", m_directoryList);
515 516

	    QValueList<int> l;
517 518
	    for(uint i = 0; i < m_visibleColumns.size(); i++)
		l.append(int(m_visibleColumns[i]));
519 520
	    
	    config->writeEntry("VisibleColumns", l);
521 522

	    config->writeEntry("PlaylistSplitterSizes", sizes());
523 524
	}
    }
525 526
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540
void PlaylistSplitter::addImpl(const QString &file, Playlist *list)
{
    processEvents();
    QFileInfo fileInfo(QDir::cleanDirPath(file));
    if(fileInfo.exists()) {
        if(fileInfo.isDir()) {
            QDir dir(fileInfo.filePath());
            QStringList dirContents=dir.entryList();
            for(QStringList::Iterator it = dirContents.begin(); it != dirContents.end(); ++it)
                if(*it != "." && *it != "..")
                    addImpl(fileInfo.filePath() + QDir::separator() + *it, list);
        }
        else {
            QString extension = fileInfo.extension(false);
541
            if(m_mediaExtensions->contains(extension) > 0)
542
		list->createItem(fileInfo);
543
	    else if(m_listExtensions->contains(extension) > 0)
544 545 546 547 548
		openPlaylist(fileInfo.absFilePath());
        }
    }    
}

549
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon)
550
{
551 552 553 554 555
    connect(p, SIGNAL(signalSelectionChanged(const PlaylistItemList &)), m_editor, SLOT(slotSetItems(const PlaylistItemList &)));
    connect(p, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalDoubleClicked()));
    connect(p, SIGNAL(signalCollectionChanged()), m_editor, SLOT(slotUpdateCollection()));
    connect(p, SIGNAL(signalNumberOfItemsChanged(Playlist *)), this, SLOT(slotPlaylistCountChanged(Playlist *)));
    connect(p, SIGNAL(signalAboutToRemove(PlaylistItem *)), this, SLOT(slotPlaylistItemRemoved(PlaylistItem *)));
556

557 558
    connect(p, SIGNAL(signalToggleColumnVisible(int)), this, SLOT(slotToggleColumnVisible(int)));

559
    m_playlistBox->createItem(p, icon, raise);
560

561
    if(raise) {
562
	m_playlistStack->raiseWidget(p);
563
	setupColumns(p);
564
    }
565 566
}

567 568 569
Playlist *PlaylistSplitter::openPlaylist(const QString &file)
{
    QFileInfo fileInfo(file);
570
    if(!fileInfo.exists() || !fileInfo.isFile() || !fileInfo.isReadable() || m_playlistFiles.insert(fileInfo.absFilePath()))
571
	return 0;
572

573
    Playlist *p = new Playlist(this, file, m_playlistStack, fileInfo.baseName(true).latin1());
574
    setupPlaylist(p);
575
    return p;
576 577
}

578 579 580 581 582
void PlaylistSplitter::setupColumns(Playlist *p)
{
    if(!p)
	return;
    
583 584
    for(uint i = 0; i < m_visibleColumns.size(); i++) {
	if(m_visibleColumns[i] && ! p->isColumnVisible(i))
585
	    p->showColumn(i);
586
	else if(! m_visibleColumns[i] && p->isColumnVisible(i))
587 588 589 590
	    p->hideColumn(i);
    }
}

591 592 593 594
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

595
void PlaylistSplitter::slotChangePlaylist(Playlist *p)
596
{
597 598
    if(!p)
	return;
599 600

    m_nextPlaylistItem = 0; 
601
    m_playlistStack->raiseWidget(p);
602
    m_editor->slotSetItems(playlistSelection());
603
    setupColumns(p);
604
    emit signalPlaylistChanged();
605 606
}

607
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
608
{
609 610
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
611 612
}

613
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
614
{
615 616
    if(item == m_playingItem)
	m_playingItem = 0;
617 618 619

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
620 621
}

622
#include "playlistsplitter.moc"