playlistsplitter.cpp 16.7 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 68

    m_editor->slotUpdateCollection();
69
}
70

71
PlaylistSplitter::~PlaylistSplitter()
72
{
73
    saveConfig();
74 75
}

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

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

    int playlistNumber = 1;

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

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

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

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

111
    if(m_nextPlaylistItem && m_nextPlaylistItem != m_playingItem) {
112 113 114 115 116
	i = m_nextPlaylistItem;
	m_nextPlaylistItem = 0;
    }  
    else if(m_playingItem) {
	Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
117
	i = p->nextItem(m_playingItem, random);
118 119
    }
    else {
120 121 122
	i = playlistSelection().getFirst();
	if(!i)
	    i = static_cast<PlaylistItem *>(visiblePlaylist()->firstChild());
123 124
    }

125
    return play(i);
126 127
}

128
QString PlaylistSplitter::playPreviousFile(bool random)
129
{
130
    if(!m_playingItem)
131
	return QString::null;
132

133 134
    Playlist *p = static_cast<Playlist *>(m_playingItem->listView());
    PlaylistItem *i = p->previousItem(m_playingItem, random);
135

136
    return play(i);
137 138
}

139 140 141 142
QString PlaylistSplitter::playFirstFile()
{
    Playlist *p = visiblePlaylist();
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());
143

144
    return play(i);
145 146
}

147 148
void PlaylistSplitter::stop()
{
149 150
    m_nextPlaylistItem = 0;

151 152 153
    if(m_playingItem) {
	m_playingItem->setPixmap(0, 0);
	m_playingItem = 0;
154 155 156 157 158
    }
}

QString PlaylistSplitter::playingArtist() const
{
159 160
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::ArtistColumn);
161
    else
162
	return QString::null;
163 164 165 166
}

QString PlaylistSplitter::playingTrack() const
{
167 168
    if(m_playingItem)
	return m_playingItem->text(PlaylistItem::TrackColumn);
169
    else
170
	return QString::null;
171 172 173 174
}

QString PlaylistSplitter::playingList() const
{
175 176
    if(m_playingItem)
	return static_cast<Playlist *>(m_playingItem->listView())->name();
177
    else
178
	return QString::null;
179 180
}

181
QString PlaylistSplitter::extensionsString(const QStringList &extensions, const QString &type) // static
182
{
183 184 185 186 187 188 189 190 191 192 193 194
    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(", ") + ")";

195
    return s;
196 197
}

198 199
void PlaylistSplitter::open(const QString &file) 
{
200 201 202
    if(file.isEmpty())
	return;

203
    if(visiblePlaylist() == m_collection || 
204 205
       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)
206
	slotAddToPlaylist(file, m_collection);
207
    else
208
	slotAddToPlaylist(file, visiblePlaylist());
209 210 211 212
}

void PlaylistSplitter::open(const QStringList &files) 
{
213 214 215
    if(files.isEmpty())
	return;
    
216
    if(visiblePlaylist() == m_collection || 
217 218
       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)
219
	slotAddToPlaylist(files, m_collection);
220
    else
221
	slotAddToPlaylist(files, visiblePlaylist());
222 223 224 225
}

Playlist *PlaylistSplitter::createPlaylist(const QString &name)
{
226
    Playlist *p = new Playlist(m_playlistStack, name.latin1());
227 228
    setupPlaylist(p, true);
    return p;
229 230
}

231 232 233 234
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

235
void PlaylistSplitter::slotOpen()
236
{
237
    QStringList files = KFileDialog::getOpenFileNames(QString::null, 
238
						      extensionsString((*m_mediaExtensions + *m_listExtensions), i18n("Media Files")));
239 240 241
    open(files);
}

242
void PlaylistSplitter::slotOpenDirectory()
243
{ 
244
    DirectoryList *l = new DirectoryList(m_directoryList, this, "directoryList");
245

246
    m_directoryQueue.clear();
247

248 249
    connect(l, SIGNAL(signalDirectoryAdded(const QString &)), this, SLOT(slotQueueDirectory(const QString &)));
    connect(l, SIGNAL(signalDirectoryRemoved(const QString &)), this, SLOT(slotQueueDirectoryRemove(const QString &)));
250 251

    if(l->exec() == QDialog::Accepted) {
252 253 254 255
	open(m_directoryQueue);
	m_directoryList += m_directoryQueue;
	for(QStringList::Iterator it = m_directoryQueueRemove.begin(); it !=  m_directoryQueueRemove.end(); it++)
	    m_directoryList.remove(*it);
256 257
    }
}
258
void PlaylistSplitter::slotSetEditorVisible(bool visible)
259 260
{
    if(visible)
261
	m_editor->show();
262
    else
263
	m_editor->hide();
264 265
}

266
Playlist *PlaylistSplitter::slotCreatePlaylist()
267 268 269
{
    bool ok;

270
    // If this text is changed, please also change it in PlaylistBox::duplicate().
271 272 273 274

    QString name = QInputDialog::getText(i18n("New Playlist..."), i18n("Please enter a name for the new playlist:"),
					 QLineEdit::Normal, uniquePlaylistName(), &ok);
    if(ok)
275
	return createPlaylist(name);
276
    else
277
	return 0;
278 279
}

280
void PlaylistSplitter::slotSelectPlaying()
281
{
282
    if(!m_playingItem)
283 284
	return;

285
    Playlist *l = static_cast<Playlist *>(m_playingItem->listView());
286
	
287 288
    if(!l)
	return;
289

290
    l->clearSelection();
291 292
    l->setSelected(m_playingItem, true);
    l->ensureItemVisible(m_playingItem);
293
    
294
    m_playlistBox->raise(l);
295 296
}

297
void PlaylistSplitter::slotDeleteSelectedItems()
298 299 300
{
    Playlist *p = visiblePlaylist();
    if(p)
301
	p->slotDeleteSelectedItems();
302 303
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
void PlaylistSplitter::slotAddToPlaylist(const QString &file, Playlist *list)
{
    KApplication::setOverrideCursor(Qt::waitCursor);
    addImpl(file, list);
    KApplication::restoreOverrideCursor();
    
    if(m_editor)
	m_editor->slotUpdateCollection();
}

void PlaylistSplitter::slotAddToPlaylist(const QStringList &files, Playlist *list)
{
    KApplication::setOverrideCursor(Qt::waitCursor);
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
        addImpl(*it, list);
    KApplication::restoreOverrideCursor();

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

325
void PlaylistSplitter::slotToggleColumnVisible(int column)
326
{
327 328 329
    m_visibleColumns[column] = ! m_visibleColumns[column];
    if(visiblePlaylist())
	setupColumns(visiblePlaylist());
330 331
}

332 333 334 335 336 337
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
338
    m_playlistBox = new PlaylistBox(this, "playlistBox");
339 340 341 342 343 344 345

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

346 347
    m_playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    m_editor = new TagEditor(editorSplitter, "tagEditor");
348 349 350

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

351
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
352 353 354 355

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

356 357
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(Playlist *)), 
	    this, SLOT(slotChangePlaylist(Playlist *)));
358

359
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
360 361 362 363

    // 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 
364
    // slotCreatePlaylist().
365

366
    CollectionList::initialize(m_playlistStack, m_restore);
367 368
    m_collection = CollectionList::instance();
    setupPlaylist(m_collection, true, "folder_sound");
369
    connect(m_collection, SIGNAL(signalCollectionChanged()), m_editor, SLOT(slotUpdateCollection()));
370 371

    // Show the collection on startup.
372
    m_playlistBox->setSelected(0, true);
373 374 375 376
}

void PlaylistSplitter::readConfig()
{
377 378 379 380
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

381 382 383 384 385 386 387
	QValueList<int> splitterSizes = config->readIntListEntry("PlaylistSplitterSizes");
	if(splitterSizes.isEmpty()) {
	    splitterSizes.append(100);
	    splitterSizes.append(640);
	}
	setSizes(splitterSizes);
	
388
	if(m_restore) {
389 390 391 392

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

	    QFile f(playlistsFile);
393
	    
394 395 396
	    if(f.open(IO_ReadOnly)) {
		QDataStream s(&f);
		while(!s.atEnd()) {
397
		    Playlist *p = new Playlist(m_playlistStack);
398 399 400 401
		    s >> *p;

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

402
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
403 404 405 406
			setupPlaylist(p);
		    else
			delete p;
		}
407
	    }
408

409
	    m_directoryList = config->readListEntry("DirectoryList");
410
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
411
	}
412 413 414

	// restore the list of hidden and shown columns

415
	if(m_collection) {
416
	    // the last column is just a filler
417
	    m_visibleColumns.resize(m_collection->columns() - 1, true);
418 419 420 421 422
	    QValueList<int> l = config->readIntListEntry("VisibleColumns");

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

427
		// while we're looping go ahead and populate m_columnNames
428
		
429
		m_columnNames.append(m_collection->columnText(i));
430

431 432
		i++;
	    }
433
	    setupColumns(m_collection);
434
	}
435 436 437 438 439 440 441
    }
}	


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

443 444
    // Save the list of open playlists.
    
445
    if(m_restore && m_playlistBox) {
446 447 448

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

449 450 451 452 453 454 455
	QString playlistsFile = KGlobal::dirs()->saveLocation("appdata") + "playlists";
	QFile f(playlistsFile);
	
	if(f.open(IO_WriteOnly)) {

	    QDataStream s(&f);

456
	    QPtrList<Playlist> l = m_playlistBox->playlists();
457

458 459
	    for(Playlist *p = l.first(); p; p = l.next())
		s << *p;
460

461
	    f.close();
462 463 464
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
465
	    config->writeEntry("DirectoryList", m_directoryList);
466 467

	    QValueList<int> l;
468 469
	    for(uint i = 0; i < m_visibleColumns.size(); i++)
		l.append(int(m_visibleColumns[i]));
470 471
	    
	    config->writeEntry("VisibleColumns", l);
472 473

	    config->writeEntry("PlaylistSplitterSizes", sizes());
474 475
	}
    }
476 477
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491
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);
492
            if(m_mediaExtensions->contains(extension) > 0)
493
		list->createItem(fileInfo);
494
	    else if(m_listExtensions->contains(extension) > 0)
495 496 497 498 499
		openPlaylist(fileInfo.absFilePath());
        }
    }    
}

500
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon)
501
{
502 503 504 505
    connect(p, SIGNAL(signalSelectionChanged(const PlaylistItemList &)), m_editor, SLOT(slotSetItems(const PlaylistItemList &)));
    connect(p, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalDoubleClicked()));
    connect(p, SIGNAL(signalNumberOfItemsChanged(Playlist *)), this, SLOT(slotPlaylistCountChanged(Playlist *)));
    connect(p, SIGNAL(signalAboutToRemove(PlaylistItem *)), this, SLOT(slotPlaylistItemRemoved(PlaylistItem *)));
506 507
    connect(p, SIGNAL(signalFilesDropped(const QStringList &, Playlist *)), this, SLOT(slotAddToPlaylist(const QStringList &, Playlist *)));
    connect(p, SIGNAL(signalSetNext(PlaylistItem *)), this, SLOT(slotSetNextItem(PlaylistItem *)));
508

509 510
    connect(p, SIGNAL(signalToggleColumnVisible(int)), this, SLOT(slotToggleColumnVisible(int)));

511
    m_playlistBox->createItem(p, icon, raise);
512

513
    if(raise) {
514
	m_playlistStack->raiseWidget(p);
515
	setupColumns(p);
516
    }
517 518
}

519 520 521
Playlist *PlaylistSplitter::openPlaylist(const QString &file)
{
    QFileInfo fileInfo(file);
522
    if(!fileInfo.exists() || !fileInfo.isFile() || !fileInfo.isReadable() || m_playlistFiles.insert(fileInfo.absFilePath()))
523
	return 0;
524

525
    Playlist *p = new Playlist(file, m_playlistStack, fileInfo.baseName(true).latin1());
526
    setupPlaylist(p);
527
    return p;
528 529
}

530 531 532 533 534
void PlaylistSplitter::setupColumns(Playlist *p)
{
    if(!p)
	return;
    
535 536
    for(uint i = 0; i < m_visibleColumns.size(); i++) {
	if(m_visibleColumns[i] && ! p->isColumnVisible(i))
537
	    p->showColumn(i);
538
	else if(! m_visibleColumns[i] && p->isColumnVisible(i))
539 540 541 542
	    p->hideColumn(i);
    }
}

543 544 545 546 547 548 549 550 551 552 553 554 555
QString PlaylistSplitter::play(PlaylistItem *item)
{
    stop();

    if(!item)
	return QString::null;

    item->setPixmap(0, QPixmap(UserIcon("playing")));
    m_playingItem = item;

    return item->absFilePath();
}

556 557 558 559
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

560
void PlaylistSplitter::slotChangePlaylist(Playlist *p)
561
{
562 563
    if(!p)
	return;
564 565

    m_nextPlaylistItem = 0; 
566
    m_playlistStack->raiseWidget(p);
567
    m_editor->slotSetItems(playlistSelection());
568
    setupColumns(p);
569
    emit signalPlaylistChanged();
570 571
}

572
void PlaylistSplitter::slotPlaylistCountChanged(Playlist *p)
573
{
574 575
    if(p && p == m_playlistStack->visibleWidget())
	emit signalSelectedPlaylistCountChanged(p->childCount());
576 577
}

578
void PlaylistSplitter::slotPlaylistItemRemoved(PlaylistItem *item)
579
{
580 581
    if(item == m_playingItem)
	m_playingItem = 0;
582 583 584

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
585 586
}

587
#include "playlistsplitter.moc"