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), 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

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
    PlaylistItem *i;

105 106 107
    // 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.
108

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

123
    return play(i);
124 125
}

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

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

134
    return play(i);
135 136
}

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

142
    return play(i);
143 144
}

145 146
void PlaylistSplitter::stop()
{
147 148
    m_nextPlaylistItem = 0;

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

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

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

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

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

193
    return s;
194 195
}

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

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

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

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

229 230 231 232
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

244
    m_directoryQueue.clear();
245

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

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

264
Playlist *PlaylistSplitter::slotCreatePlaylist()
265 266 267
{
    bool ok;

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

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

278
void PlaylistSplitter::slotSelectPlaying()
279
{
280
    if(!m_playingItem)
281 282
	return;

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

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

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

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
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();
}

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

330 331 332 333 334 335
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

349
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
350 351 352 353

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

354 355
    connect(m_playlistBox, SIGNAL(signalCurrentChanged(Playlist *)), 
	    this, SLOT(slotChangePlaylist(Playlist *)));
356

357
    connect(m_playlistBox, SIGNAL(signalDoubleClicked()), this, SIGNAL(signalListBoxDoubleClicked()));
358 359 360 361

    // 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 
362
    // slotCreatePlaylist().
363

364
    CollectionList::initialize(m_playlistStack, m_restore);
365 366
    m_collection = CollectionList::instance();
    setupPlaylist(m_collection, true, "folder_sound");
367 368

    // Show the collection on startup.
369
    m_playlistBox->setSelected(0, true);
370 371 372 373
}

void PlaylistSplitter::readConfig()
{
374 375 376 377
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

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

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

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

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

399
		    if(p->fileName().isEmpty() || !m_playlistFiles.insert(p->fileName()))
400 401 402 403
			setupPlaylist(p);
		    else
			delete p;
		}
404
	    }
405

406
	    m_directoryList = config->readListEntry("DirectoryList");
407
	    QTimer::singleShot(0, this, SLOT(slotScanDirectories()));
408
	}
409 410 411

	// restore the list of hidden and shown columns

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

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

424
		// while we're looping go ahead and populate m_columnNames
425
		
426
		m_columnNames.append(m_collection->columnText(i));
427

428 429
		i++;
	    }
430
	    setupColumns(m_collection);
431
	}
432 433 434 435 436 437 438
    }
}	


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

440 441
    // Save the list of open playlists.
    
442
    if(m_restore && m_playlistBox) {
443 444 445

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

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

	    QDataStream s(&f);

453
	    QPtrList<Playlist> l = m_playlistBox->playlists();
454

455 456
	    for(Playlist *p = l.first(); p; p = l.next())
		s << *p;
457

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

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

	    config->writeEntry("PlaylistSplitterSizes", sizes());
471 472
	}
    }
473 474
}

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

497
void PlaylistSplitter::setupPlaylist(Playlist *p, bool raise, const char *icon)
498
{
499 500 501 502 503
    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 *)));
504 505
    connect(p, SIGNAL(signalFilesDropped(const QStringList &, Playlist *)), this, SLOT(slotAddToPlaylist(const QStringList &, Playlist *)));
    connect(p, SIGNAL(signalSetNext(PlaylistItem *)), this, SLOT(slotSetNextItem(PlaylistItem *)));
506

507 508
    connect(p, SIGNAL(signalToggleColumnVisible(int)), this, SLOT(slotToggleColumnVisible(int)));

509
    m_playlistBox->createItem(p, icon, raise);
510

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

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

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

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

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

    if(!item)
	return QString::null;

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

    return item->absFilePath();
}

554 555 556 557
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

    if(item == m_nextPlaylistItem)
	m_nextPlaylistItem = 0;
583 584
}

585
#include "playlistsplitter.moc"