playlistsplitter.cpp 16.6 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)
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
}
68

69
PlaylistSplitter::~PlaylistSplitter()
70
{
71
    saveConfig();
72 73
}

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

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

    int playlistNumber = 1;

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

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

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

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

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

    if(i) {
	i->setPixmap(0, QPixmap(UserIcon("playing")));
124
	m_playingItem = i;
125
	return i->absFilePath();
126 127
    }
    else
128
	return QString::null;
129 130
}

131
QString PlaylistSplitter::playPreviousFile(bool random)
132
{
133 134 135
    if(m_playingItem) {
	Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
	PlaylistItem *i = p->previousItem(m_playingItem, random);
136

137
	m_playingItem->setPixmap(0, 0);
138 139
	i->setPixmap(0, QPixmap(UserIcon("playing")));
	
140
	m_playingItem = i;
141
	return i->absFilePath();
142 143
    }
    else
144
	return QString::null;
145 146
}

147
QString PlaylistSplitter::playSelectedFile()
148
{
149
    stop();
150

151
    PlaylistItemList items = playlistSelection();
152

153 154 155 156
    if(!items.isEmpty()) {
	PlaylistItem *i = items.first();
	i->setPixmap(0, QPixmap(UserIcon("playing")));
	
157
	m_playingItem = i;
158
	return i->absFilePath();
159 160
    }
    else
161
	return QString::null;
162
}
163

164 165 166
QString PlaylistSplitter::playFirstFile()
{
    stop();
167

168 169
    Playlist *p = visiblePlaylist();
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());
170

171 172 173
    if(i) {
	i->setPixmap(0, QPixmap(UserIcon("playing")));
	i->setPixmap(0, QPixmap(UserIcon("playing")));
174
	m_playingItem = i;
175

176
	return i->absFilePath();
177 178
    }
    else
179
	return QString::null;
180 181
}

182 183
void PlaylistSplitter::stop()
{
184 185 186
    if(m_playingItem) {
	m_playingItem->setPixmap(0, 0);
	m_playingItem = 0;
187 188 189 190 191
    }
}

QString PlaylistSplitter::playingArtist() const
{
192 193
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::ArtistColumn);
194
    else
195
	return QString::null;
196 197 198 199
}

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

QString PlaylistSplitter::playingList() const
{
208 209
    if(m_playingItem)
	return static_cast<Playlist *>(m_playingItem->listView())->name();
210
    else
211
	return QString::null;
212 213
}

214
void PlaylistSplitter::addToPlaylist(const QString &file, Playlist *list)
215
{
216 217 218 219
    KApplication::setOverrideCursor(Qt::waitCursor);
    addImpl(file, list);
    KApplication::restoreOverrideCursor();
    
220
    if(m_editor)
221
	m_editor->slotUpdateCollection();
222 223
}

224
void PlaylistSplitter::addToPlaylist(const QStringList &files, Playlist *list)
225
{
226 227 228 229 230
    KApplication::setOverrideCursor(Qt::waitCursor);
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
        addImpl(*it, list);
    KApplication::restoreOverrideCursor();

231
    if(m_editor)
232
	m_editor->slotUpdateCollection();
233 234
}

235
QString PlaylistSplitter::extensionsString(const QStringList &extensions, const QString &type) // static
236
{
237 238 239 240 241 242 243 244 245 246 247 248
    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(", ") + ")";

249
    return s;
250 251
}

252 253
void PlaylistSplitter::open(const QString &file) 
{
254 255 256
    if(file.isEmpty())
	return;

257
    if(visiblePlaylist() == m_collection || 
258 259
       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)
260
	addToPlaylist(file, m_collection);
261
    else
262
	addToPlaylist(file, visiblePlaylist());
263 264 265 266
}

void PlaylistSplitter::open(const QStringList &files) 
{
267 268 269
    if(files.isEmpty())
	return;
    
270
    if(visiblePlaylist() == m_collection || 
271 272
       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)
273
	addToPlaylist(files, m_collection);
274
    else
275 276 277 278 279 280 281 282
	addToPlaylist(files, visiblePlaylist());
}

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

285 286 287 288
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

289
void PlaylistSplitter::slotOpen()
290
{
291
    QStringList files = KFileDialog::getOpenFileNames(QString::null, 
292
						      extensionsString((*m_mediaExtensions + *m_listExtensions), i18n("Media Files")));
293 294 295
    open(files);
}

296
void PlaylistSplitter::slotOpenDirectory()
297
{ 
298
    DirectoryList *l = new DirectoryList(m_directoryList, this, "directoryList");
299

300
    m_directoryQueue.clear();
301

302 303
    connect(l, SIGNAL(signalDirectoryAdded(const QString &)), this, SLOT(slotQueueDirectory(const QString &)));
    connect(l, SIGNAL(signalDirectoryRemoved(const QString &)), this, SLOT(slotQueueDirectoryRemove(const QString &)));
304 305

    if(l->exec() == QDialog::Accepted) {
306 307 308 309
	open(m_directoryQueue);
	m_directoryList += m_directoryQueue;
	for(QStringList::Iterator it = m_directoryQueueRemove.begin(); it !=  m_directoryQueueRemove.end(); it++)
	    m_directoryList.remove(*it);
310 311
    }
}
312
void PlaylistSplitter::slotSetEditorVisible(bool visible)
313 314
{
    if(visible)
315
	m_editor->show();
316
    else
317
	m_editor->hide();
318 319
}

320
Playlist *PlaylistSplitter::slotCreatePlaylist()
321 322 323
{
    bool ok;

324
    // If this text is changed, please also change it in PlaylistBox::duplicate().
325 326 327 328

    QString name = QInputDialog::getText(i18n("New Playlist..."), i18n("Please enter a name for the new playlist:"),
					 QLineEdit::Normal, uniquePlaylistName(), &ok);
    if(ok)
329
	return createPlaylist(name);
330
    else
331
	return 0;
332 333
}

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);
347
    
348
    m_playlistBox->raise(l);
349 350
}

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

358
void PlaylistSplitter::slotToggleColumnVisible(int column)
359
{
360 361 362
    m_visibleColumns[column] = ! m_visibleColumns[column];
    if(visiblePlaylist())
	setupColumns(visiblePlaylist());
363 364
}

365 366 367 368 369 370
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
371
    m_playlistBox = new PlaylistBox(this, "playlistBox");
372 373 374 375 376 377 378

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

379 380
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
381 382 383

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

384
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
385 386 387 388

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

389 390
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(Playlist *)), 
	    this, SLOT(slotChangePlaylist(Playlist *)));
391

392
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
393 394 395 396

    // 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 
397
    // slotCreatePlaylist().
398

399 400 401
    CollectionList::initialize(this, m_playlistStack, m_restore);
    m_collection = CollectionList::instance();
    setupPlaylist(m_collection, true, "folder_sound");
402 403

    // Show the collection on startup.
404
    m_playlistBox->setSelected(0, true);
405 406 407 408
}

void PlaylistSplitter::readConfig()
{
409 410 411 412
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

413 414 415 416 417 418 419
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
	
420
	if(m_restore) {
421 422 423 424

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

	    QFile f(playlistsFile);
425
	    
426 427 428
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
429
		    Playlist *p = new Playlist(this, m_playlistStack);
430 431 432 433
		    s >> *p;

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

434
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
435 436 437 438
			setupPlaylist(p);
		    else
			delete p;
		}
439
	    }
440

441
	    m_directoryList = config->readListEntry("DirectoryList");
442
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
443
	}
444 445 446

	// restore the list of hidden and shown columns

447
	if(m_collection) {
448
	    // the last column is just a filler
449
	    m_visibleColumns.resize(m_collection->columns() - 1, true);
450 451 452 453 454
	    QValueList<int> l = config->readIntListEntry("VisibleColumns");

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

459
		// while we're looping go ahead and populate m_columnNames
460
		
461
		m_columnNames.append(m_collection->columnText(i));
462

463 464
		i++;
	    }
465
	    setupColumns(m_collection);
466
	}
467 468 469 470 471 472 473
    }
}	


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

475 476
    // Save the list of open playlists.
    
477
    if(m_restore && m_playlistBox) {
478 479 480

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

481 482 483 484 485 486 487
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
	
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

488
	    QPtrList<Playlist> l = m_playlistBox->playlists();
489

490 491
	    for(Playlist *p = l.first(); p; p = l.next())
		s << *p;
492

493
	    f.close();
494 495 496
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
497
	    config->writeEntry("DirectoryList", m_directoryList);
498 499

	    QValueList<int> l;
500 501
	    for(uint i = 0; i < m_visibleColumns.size(); i++)
		l.append(int(m_visibleColumns[i]));
502 503
	    
	    config->writeEntry("VisibleColumns", l);
504 505

	    config->writeEntry("PlaylistSplitterSizes", sizes());
506 507
	}
    }
508 509
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523
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);
524
            if(m_mediaExtensions->contains(extension) > 0)
525
		list->createItem(fileInfo);
526
	    else if(m_listExtensions->contains(extension) > 0)
527 528 529 530 531
		openPlaylist(fileInfo.absFilePath());
        }
    }    
}

532
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon)
533
{
534 535 536 537 538
    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 *)));
539

540 541
    connect(p, SIGNAL(signalToggleColumnVisible(int)), this, SLOT(slotToggleColumnVisible(int)));

542
    m_playlistBox->createItem(p, icon, raise);
543

544
    if(raise) {
545
	m_playlistStack->raiseWidget(p);
546
	setupColumns(p);
547
    }
548 549
}

550 551 552
Playlist *PlaylistSplitter::openPlaylist(const QString &file)
{
    QFileInfo fileInfo(file);
553
    if(!fileInfo.exists() || !fileInfo.isFile() || !fileInfo.isReadable() || m_playlistFiles.insert(fileInfo.absFilePath()))
554
	return 0;
555

556
    Playlist *p = new Playlist(this, file, m_playlistStack, fileInfo.baseName(true).latin1());
557
    setupPlaylist(p);
558
    return p;
559 560
}

561 562 563 564 565
void PlaylistSplitter::setupColumns(Playlist *p)
{
    if(!p)
	return;
    
566 567
    for(uint i = 0; i < m_visibleColumns.size(); i++) {
	if(m_visibleColumns[i] && ! p->isColumnVisible(i))
568
	    p->showColumn(i);
569
	else if(! m_visibleColumns[i] && p->isColumnVisible(i))
570 571 572 573
	    p->hideColumn(i);
    }
}

574 575 576 577
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

578
void PlaylistSplitter::slotChangePlaylist(Playlist *p)
579
{
580 581 582
    if(!p)
	return;
    
583
    m_playlistStack->raiseWidget(p);
584
    m_editor->slotSetItems(playlistSelection());
585
    setupColumns(p);
586
    emit signalPlaylistChanged();
587 588
}

589
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
590
{
591 592
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
593 594
}

595
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
596
{
597 598
    if(item == m_playingItem)
	m_playingItem = 0;
599 600
}

601
#include "playlistsplitter.moc"