Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

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

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

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

////////////////////////////////////////////////////////////////////////////////
34
// helper functions
35 36
////////////////////////////////////////////////////////////////////////////////

37
void processEvents()
38
{
39 40 41
    static int processed = 0;
    if(processed == 0)
        kapp->processEvents();
42
    processed = (processed + 1) % 5;
43 44
}

45
////////////////////////////////////////////////////////////////////////////////
46
// public methods
47 48
////////////////////////////////////////////////////////////////////////////////

49 50
PlaylistSplitter::PlaylistSplitter(QWidget *parent, bool restoreOnLoad, const char *name) : QSplitter(Qt::Horizontal, parent, name), 
											    restore(restoreOnLoad)
51
{
52 53 54 55 56
    setupLayout();
    readConfig();
    mediaExtensions.append("mp3");
    mediaExtensions.append("ogg");
    listExtensions.append("m3u");
57
}
58

59
PlaylistSplitter::~PlaylistSplitter()
60
{
61
    saveConfig();
62 63
}

64
QString PlaylistSplitter::uniquePlaylistName()
65
{
66
    return(uniquePlaylistName(i18n("Playlist")));
67 68
}

69
QString PlaylistSplitter::uniquePlaylistName(const QString &startingWith, bool useParenthesis)
70 71 72 73 74 75 76 77 78 79 80
{
    if(!playlistBox)
	return(QString::null);

    QStringList names = playlistBox->names();

    int playlistNumber = 1;

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

81 82 83 84 85 86 87 88 89 90 91 92 93
    if(useParenthesis) {
	while(names.contains(startingWith + " (" + QString::number(playlistNumber) + ")") != 0)
	    playlistNumber++;
	
	return(startingWith + " (" + QString::number(playlistNumber) + ")");	
    }
    else
    {
	while(names.contains(startingWith + ' ' + QString::number(playlistNumber)) != 0)
	    playlistNumber++;
	
	return(startingWith + " " + QString::number(playlistNumber));
    }
94 95
}

96
PlaylistItemList PlaylistSplitter::playlistSelection() const
97 98 99 100 101 102 103 104 105 106 107 108
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
    return(p->selectedItems());
}

PlaylistItem *PlaylistSplitter::playlistFirstItem() const
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
    PlaylistItem *i = static_cast<PlaylistItem *>(p->firstChild());
    return(i);
}

109 110 111 112 113 114 115 116 117 118 119 120
QString PlaylistSplitter::extensionsString(const QStringList &extensions, const QString &type)
{
    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)
121
	s += "|" + type + " (" + l.join(", ") + ")";
122 123 124 125

    return(s);
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
QString PlaylistSplitter::selectedPlaylistName() const
{
    if(playlistStack->visibleWidget()) {
	Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
	return(p->name());
    }
    return(QString::null);
}

int PlaylistSplitter::selectedPlaylistCount() const
{
    if(playlistStack->visibleWidget()) {
	Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
	return(p->childCount());
    }
    return(0);
}

144
QStringList PlaylistSplitter::playlistExtensions() const
145
{
146
    return(listExtensions);
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
void PlaylistSplitter::setSelected(PlaylistItem *i)
{
    // Hu hu!  See how much crap I can pack into just one pointer to a 
    // PlaylistItem!  Sensitive viewers may want to close their eyes for the
    // next few lines.
    
    if(i) {
	
	// Get the playlist associated with the playing item and make set the
	// playing item to be both selected and visible.
	
	Playlist *l = dynamic_cast<Playlist *>(i->listView());
	
	if(l) {
	    l->clearSelection();
	    l->setSelected(i, true);
	    l->ensureItemVisible(i);
	    
	    // Now move on to the PlaylistBox.  The Playlist knows which
	    // PlaylistBoxItem that it is associated with, so we'll just get
	    // that and then figure out the PlaylistBox from there.
	    // 
	    // Once we have that we can set the appropriate Playlist to be
	    // visible.
	    
	    if(l->playlistBoxItem() && l->playlistBoxItem()->listBox()) {
		QListBox *b = l->playlistBoxItem()->listBox();
		
		b->clearSelection();
		b->setSelected(l->playlistBoxItem(), true);

		b->setCurrentItem(l->playlistBoxItem());
		b->ensureCurrentVisible();
	    }
	}
    }    
}

187 188 189 190
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

191 192
void PlaylistSplitter::open()
{
193 194 195
//    QStringList files = KFileDialog::getOpenFileNames(QString::null, 
//						      extensionsString((mediaExtensions + listExtensions), i18n("Media Files")));
    QStringList files = KFileDialog::getOpenFileNames();
196 197 198 199 200 201 202 203
    open(files);
}

void PlaylistSplitter::openDirectory()
{
    open(KFileDialog::getExistingDirectory());
}

204 205
void PlaylistSplitter::open(const QStringList &files)
{
206
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
207
    if(p)
208
	add(files, p);
209 210 211 212
}

void PlaylistSplitter::open(const QString &file)
{
213
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
214
    if(p)
215
	add(file, p);
216
}
217

218 219 220 221
void PlaylistSplitter::save()
{
    if(editor)
	editor->save();
222 223 224 225 226
}

void PlaylistSplitter::remove()
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
227 228 229 230 231 232 233 234 235
    if(p)
	p->remove();
}

void PlaylistSplitter::refresh()
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
    if(p)
	p->refresh();
236 237 238 239 240 241 242 243 244 245 246 247 248
}

void PlaylistSplitter::setEditorVisible(bool visible)
{
    if(visible)
	editor->show();
    else
	editor->hide();
}

void PlaylistSplitter::clearSelectedItems()
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
249 250
    if(p)
	p->clearItems(p->selectedItems()); 
251 252 253 254 255
}

void PlaylistSplitter::selectAll(bool select)
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
256 257 258 259 260 261 262 263
    if(p)
	p->selectAll(select);
}

Playlist *PlaylistSplitter::createPlaylist()
{
    bool ok;

264
    // If this text is changed, please also change it in PlaylistBox::duplicate().
265 266 267 268 269 270 271 272 273 274 275

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

Playlist *PlaylistSplitter::createPlaylist(const QString &name)
{
276
    Playlist *p = new Playlist(this, playlistStack, name.latin1());
277 278
    PlaylistBoxItem *i = new PlaylistBoxItem(playlistBox, SmallIcon("midi", 32), name, p);
    p->setPlaylistBoxItem(i);
279

280 281 282
    connect(p, SIGNAL(selectionChanged(const PlaylistItemList &)), editor, SLOT(setItems(const PlaylistItemList &)));
    connect(p, SIGNAL(doubleClicked(QListViewItem *)), this, SIGNAL(playlistDoubleClicked(QListViewItem *)));
    connect(p, SIGNAL(collectionChanged()), editor, SLOT(updateCollection()));
283
    connect(p, SIGNAL(numberOfItemsChanged(Playlist *)), this, SLOT(playlistCountChanged(Playlist *)));
284
    playlistBox->sort();
285 286 287 288 289 290 291 292
    return(p);
}

void PlaylistSplitter::openPlaylist()
{
    QStringList files = KFileDialog::getOpenFileNames(QString::null, "*.m3u");

    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
293
	openPlaylist(*it);
294 295 296 297 298
}

Playlist *PlaylistSplitter::openPlaylist(const QString &playlistFile)
{
    QFileInfo file(playlistFile);
299
    if(!file.exists() || !file.isFile() || !file.isReadable() || playlistFiles.contains(file.absFilePath()))
300
	return(0);
301 302 303

    playlistFiles.append(file.absFilePath());
    
304
    Playlist *p = new Playlist(this, playlistFile, playlistStack, file.baseName(true).latin1());
305 306 307
    connect(p, SIGNAL(selectionChanged(const PlaylistItemList &)), editor, SLOT(setItems(const PlaylistItemList &)));
    connect(p, SIGNAL(doubleClicked(QListViewItem *)), this, SIGNAL(playlistDoubleClicked(QListViewItem *)));
    connect(p, SIGNAL(collectionChanged()), editor, SLOT(updateCollection()));
308
    connect(p, SIGNAL(numberOfItemsChanged(Playlist *)), this, SLOT(playlistCountChanged(Playlist *)));
309 310
    PlaylistBoxItem *i = new PlaylistBoxItem(playlistBox, SmallIcon("midi", 32), p->name(), p);
    p->setPlaylistBoxItem(i);
311
    playlistBox->sort();
312
    return(p);
313 314
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
void PlaylistSplitter::add(const QString &file, Playlist *list)
{
    KApplication::setOverrideCursor(Qt::waitCursor);
    addImpl(file, list);
    KApplication::restoreOverrideCursor();
    
    if(editor)
	editor->updateCollection();
}

void PlaylistSplitter::add(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(editor)
	editor->updateCollection();
}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setupLayout()
{
    playlistBox = new PlaylistBox(this, "playlistBox");

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

    playlistStack = new QWidgetStack(editorSplitter, "playlistStack");
    editor = new TagEditor(editorSplitter, "tagEditor");

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

    editorSplitter->setResizeMode(editor, QSplitter::FollowSizeHint);

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

360 361 362 363 364
    connect(playlistBox, SIGNAL(currentChanged(PlaylistBoxItem *)), 
	    this, SLOT(changePlaylist(PlaylistBoxItem *)));

    connect(playlistBox, SIGNAL(doubleClicked(PlaylistBoxItem *)), 
	    this, SLOT(playlistBoxDoubleClicked(PlaylistBoxItem *)));
365 366 367 368 369 370

    // 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 
    // createPlaylist().

371
    CollectionList::initialize(this, playlistStack, restore);
372 373 374 375
    collection = CollectionList::instance();

    PlaylistBoxItem *collectionBoxItem = new PlaylistBoxItem(playlistBox, SmallIcon("folder_sound", 32), 
							     i18n("Music Collection"), collection);
376
    collection->setPlaylistBoxItem(collectionBoxItem);
377

378 379
    connect(collection, SIGNAL(selectionChanged(const PlaylistItemList &)), 
	    editor, SLOT(setItems(const PlaylistItemList &)));
380
    connect(collection, SIGNAL(doubleClicked(QListViewItem *)), this, SIGNAL(playlistDoubleClicked(QListViewItem *)));
381
    connect(collection, SIGNAL(collectionChanged()), editor, SLOT(updateCollection()));
382
    connect(collection, SIGNAL(numberOfItemsChanged(Playlist *)), this, SLOT(playlistCountChanged(Playlist *)));
383 384 385 386 387 388 389

    // Show the collection on startup.
    playlistBox->setSelected(collectionBoxItem, true);
}

void PlaylistSplitter::readConfig()
{
390 391 392 393
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

394 395 396 397 398 399 400 401 402 403 404
	if(restore) {
	    QStringList external = config->readListEntry("ExternalPlaylists");
	    for(QStringList::Iterator it = external.begin(); it != external.end(); ++it)
		openPlaylist(*it);
	    
	    QStringList internal = config->readListEntry("InternalPlaylists");
	    for(QStringList::Iterator it = internal.begin(); it != internal.end(); ++it) {
		Playlist *p = openPlaylist(*it);
		if(p)
		    p->setInternal(true);
	    }
405
	}
406 407 408 409 410 411 412
    }
}	


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

414 415
    // Save the list of open playlists.
    
416
    if(restore && playlistBox) {
417 418
	QStringList internalPlaylists;
	QStringList externalPlaylists;
419 420 421 422

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

	for(uint i = 1; i < playlistBox->count(); i++) {
423 424 425 426
	    PlaylistBoxItem *item = static_cast<PlaylistBoxItem *>(playlistBox->item(i));
	    if(item && item->playlist()) {
		Playlist *p = item->playlist();
		if(p->isInternalFile()) {
427 428
		    p->save(true);
		    internalPlaylists.append(p->fileName());
429 430 431 432 433 434 435
		}
		else
		    externalPlaylists.append(p->fileName());
	    }		
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
436 437
	    config->writeEntry("InternalPlaylists", internalPlaylists);
	    config->writeEntry("ExternalPlaylists", externalPlaylists);
438 439
	}
    }
440 441
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
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);
            if(mediaExtensions.contains(extension) > 0)
		list->createItem(fileInfo);
	    else if(listExtensions.contains(extension) > 0)
		openPlaylist(fileInfo.absFilePath());
        }
    }    
}

464 465 466 467 468 469 470 471 472
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::changePlaylist(PlaylistBoxItem *item)
{
    if(item && item->playlist()) {
	playlistStack->raiseWidget(item->playlist());
	editor->setItems(playlistSelection());
473
	emit(playlistChanged(item->playlist()));
474
	emit(playlistChanged());
475 476 477
    }
}

478 479 480 481 482 483
void PlaylistSplitter::playlistBoxDoubleClicked(PlaylistBoxItem *item)
{
    if(item && item->playlist() && item->playlist()->firstChild())
	emit(playlistDoubleClicked(item->playlist()->firstChild()));
}

484 485 486 487 488 489
void PlaylistSplitter::playlistCountChanged(Playlist *p)
{
    if(p && p == playlistStack->visibleWidget())
	emit(selectedPlaylistCountChanged(p->childCount()));
}

490
#include "playlistsplitter.moc"