playlistsplitter.cpp 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/***************************************************************************
                          playlistsplitter.cpp  -  description
                             -------------------
    begin                : Fri Sep 13 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : scott@slackorama.net
 ***************************************************************************/

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

#include <kiconloader.h>
19
#include <kapplication.h>
20
#include <klocale.h>
21 22
#include <kdebug.h>

23 24
#include <qinputdialog.h>

25
#include "playlistsplitter.h"
26 27 28
#include "playlist.h"
#include "collectionlist.h"
#include "tageditor.h"
29 30

////////////////////////////////////////////////////////////////////////////////
31
// helper functions
32 33
////////////////////////////////////////////////////////////////////////////////

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

42 43 44 45 46 47 48
////////////////////////////////////////////////////////////////////////////////
// static public methods
////////////////////////////////////////////////////////////////////////////////

PlaylistSplitter *PlaylistSplitter::splitter = 0;

PlaylistSplitter *PlaylistSplitter::instance()
49
{
50 51
    return(splitter);
}
52

53 54 55
void PlaylistSplitter::initialize(QWidget *parent)
{
    splitter = new PlaylistSplitter(parent);
56 57
}

58 59 60 61
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

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

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

    QStringList names = playlistBox->names();

    int playlistNumber = 1;

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

79 80 81 82 83 84 85 86 87 88 89 90 91
    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));
    }
92 93
}

94
PlaylistItemList PlaylistSplitter::playlistSelection() const
95 96 97 98 99 100 101 102 103 104 105 106
{
    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);
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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)
	s =  + "|" + type;

    return(s);
}

QStringList PlaylistSplitter::playlistExtensions()
{
    if(splitter)
	return(splitter->listExtensions);
    else
	return(QString::null);
}

132 133 134 135
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

136 137
void PlaylistSplitter::open()
{
138 139
    QStringList files = KFileDialog::getOpenFileNames(QString::null, 
						      extensionsString((mediaExtensions + listExtensions), i18n("Media Files")));
140 141 142 143 144 145 146 147
    open(files);
}

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

148 149
void PlaylistSplitter::open(const QStringList &files)
{
150
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
151
    if(p)
152
	add(files, p);
153 154 155 156
}

void PlaylistSplitter::open(const QString &file)
{
157
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
158
    if(p)
159
	add(file, p);
160
}
161

162 163 164 165
void PlaylistSplitter::save()
{
    if(editor)
	editor->save();
166 167 168 169 170
}

void PlaylistSplitter::remove()
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
171 172 173 174 175 176 177 178 179
    if(p)
	p->remove();
}

void PlaylistSplitter::refresh()
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
    if(p)
	p->refresh();
180 181 182 183 184 185 186 187 188 189 190 191 192
}

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

void PlaylistSplitter::clearSelectedItems()
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
193 194
    if(p)
	p->clearItems(p->selectedItems()); 
195 196 197 198 199
}

void PlaylistSplitter::selectAll(bool select)
{
    Playlist *p = static_cast<Playlist *>(playlistStack->visibleWidget());
200 201 202 203 204 205 206 207
    if(p)
	p->selectAll(select);
}

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

208
    // If this text is changed, please also change it in PlaylistBox::duplicate().
209 210 211 212 213 214 215 216 217 218 219 220

    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)
{
    Playlist *p = new Playlist(playlistStack, name.latin1());
221
    new PlaylistBoxItem(playlistBox, SmallIcon("midi", 32), name, p);
222

223 224 225 226 227 228 229 230 231 232 233
    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()));
    return(p);
}

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

    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
234
	openPlaylist(*it);
235 236 237 238 239
}

Playlist *PlaylistSplitter::openPlaylist(const QString &playlistFile)
{
    QFileInfo file(playlistFile);
240
    if(!file.exists() || !file.isFile() || !file.isReadable() || playlistFiles.contains(file.absFilePath()))
241
	return(0);
242 243 244

    playlistFiles.append(file.absFilePath());
    
245 246
    QString name = file.baseName(true);
    Playlist *p = new Playlist(playlistFile, playlistStack, name.latin1());
247 248 249
    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()));
250
    new PlaylistBoxItem(playlistBox, SmallIcon("midi", 32), name, p);
251
    return(p);
252 253
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
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();
}

////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlaylistSplitter::PlaylistSplitter(QWidget *parent) : QSplitter(Qt::Horizontal, parent, "playlistSplitter")
{
    setupLayout();
    readConfig();
    mediaExtensions.append("mp3");
284
    mediaExtensions.append("ogg");
285 286 287 288 289
    listExtensions.append("m3u");
}

PlaylistSplitter::~PlaylistSplitter()
{
290
    saveConfig();
291
}
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
////////////////////////////////////////////////////////////////////////////////
// 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.

317 318 319 320 321
    connect(playlistBox, SIGNAL(currentChanged(PlaylistBoxItem *)), 
	    this, SLOT(changePlaylist(PlaylistBoxItem *)));

    connect(playlistBox, SIGNAL(doubleClicked(PlaylistBoxItem *)), 
	    this, SLOT(playlistBoxDoubleClicked(PlaylistBoxItem *)));
322 323 324 325 326 327

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

328 329 330 331 332 333
    CollectionList::initialize(playlistStack);
    collection = CollectionList::instance();

    PlaylistBoxItem *collectionBoxItem = new PlaylistBoxItem(playlistBox, SmallIcon("folder_sound", 32), 
							     i18n("Music Collection"), collection);

334 335
    connect(collection, SIGNAL(selectionChanged(const PlaylistItemList &)), 
	    editor, SLOT(setItems(const PlaylistItemList &)));
336
    connect(collection, SIGNAL(doubleClicked(QListViewItem *)), this, SIGNAL(playlistDoubleClicked(QListViewItem *)));
337
    connect(collection, SIGNAL(collectionChanged()), editor, SLOT(updateCollection()));
338 339 340 341 342 343 344

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

void PlaylistSplitter::readConfig()
{
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    KConfig *config = KGlobal::config();
    { // block for Playlists group
	KConfigGroupSaver saver(config, "Playlists");

	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)
	    openPlaylist(*it);
    }
}	


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

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    // Save the list of open playlists.
    
    if(playlistBox) {
	QStringList internalPlaylists;
	QStringList externalPlaylists;
	for(uint i = 0; i < playlistBox->count(); i++) {
	    PlaylistBoxItem *item = static_cast<PlaylistBoxItem *>(playlistBox->item(i));
	    if(item && item->playlist()) {
		Playlist *p = item->playlist();
		if(p->isInternalFile()) {
		    if(p->fileName().isEmpty())
			; // save the file
		    if(!p->fileName().isEmpty())
			internalPlaylists.append(p->fileName());
		}
		else
		    externalPlaylists.append(p->fileName());
	    }		
	}
	{ // block for Playlists group
	    KConfigGroupSaver saver(config, "Playlists");
	    if(!internalPlaylists.isEmpty())
		config->writeEntry("InternalPlaylists", internalPlaylists);
	    if(!externalPlaylists.isEmpty())
		config->writeEntry("ExternalPlaylists", externalPlaylists);
	}
    }
391 392
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
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());
        }
    }    
}

415 416 417 418 419 420 421 422 423
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::changePlaylist(PlaylistBoxItem *item)
{
    if(item && item->playlist()) {
	playlistStack->raiseWidget(item->playlist());
	editor->setItems(playlistSelection());
424
	emit(playlistChanged(item->playlist()));
425 426 427
    }
}

428 429 430 431 432 433
void PlaylistSplitter::playlistBoxDoubleClicked(PlaylistBoxItem *item)
{
    if(item && item->playlist() && item->playlist()->firstChild())
	emit(playlistDoubleClicked(item->playlist()->firstChild()));
}

434
#include "playlistsplitter.moc"