collectionlist.cpp 6.93 KB
Newer Older
1
/***************************************************************************
2
                          collectionlist.cpp  -  description
3
                             -------------------
4
    begin                : Fri Sep 13 2002
5
    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 <kurl.h>
#include <kurldrag.h>
20
#include <klocale.h>
21 22
#include <kapplication.h>
#include <kmessagebox.h>
23 24
#include <kdebug.h>

25
#include <qclipboard.h>
26
#include <qtimer.h>
27

28
#include "collectionlist.h"
29
#include "playlistsplitter.h"
30
#include "cache.h"
31
#include "splashscreen.h"
32

33 34 35 36 37 38 39 40
////////////////////////////////////////////////////////////////////////////////
// static methods
////////////////////////////////////////////////////////////////////////////////

CollectionList *CollectionList::list = 0;

CollectionList *CollectionList::instance()
{
41
    return list;
42 43
}

44
void CollectionList::initialize(PlaylistSplitter *s, QWidget *parent, bool restoreOnLoad)
45
{
46
    list = new CollectionList(s, parent);
47

48 49
    if(restoreOnLoad)
	for(QDictIterator<Tag>it(*Cache::instance()); it.current(); ++it)
50
	    new CollectionListItem(it.current()->fileInfo(), it.current()->absFilePath());
51 52 53 54 55 56
}

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

57
QStringList CollectionList::artists() const
58
{
59
    return m_artistList.values();
60 61
}

62
QStringList CollectionList::albums() const
63
{
64
    return m_albumList.values();
65 66 67 68
}

CollectionListItem *CollectionList::lookup(const QString &file)
{
69
    return m_itemsDict.find(file);
70 71
}

72
PlaylistItem *CollectionList::createItem(const QFileInfo &file, QListViewItem *)
73
{
74
    QString path = file.absFilePath();
75
    if(m_itemsDict.find(path))
76
	return 0;
77

78
    return new CollectionListItem(file, path);
79 80
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void CollectionList::paste()
{
    decode(kapp->clipboard()->data());
}

void CollectionList::clear()
{
    int result = KMessageBox::warningYesNo(this, 
			      i18n("Removing an item from the collection will also remove it from "
				   "all of your playlists.  Are you sure you want to continue?  \n\n"
				   "Note however that if the directory that these files are in are in "
				   "your scan on startup list, then they will be readded on startup."));
    if(result == KMessageBox::Yes)			      
	Playlist::clear();
}

Scott Wheeler's avatar
Scott Wheeler committed
101 102 103 104 105 106 107 108
void CollectionList::slotCheckCache()
{
    for(QDictIterator<CollectionListItem>it(m_itemsDict); it.current(); ++it) {
	it.current()->checkCurrent();
	kapp->processEvents();
    }
}

109 110 111 112
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
113
CollectionList::CollectionList(PlaylistSplitter *s, QWidget *parent) : Playlist(s, parent, i18n("Collection List")) //, m_finishedLoading(false)
114
{
115

116 117
}

118
CollectionList::~CollectionList()
119
{
120

121
}
122

123
void CollectionList::decode(QMimeSource *s)
124
{
125 126 127 128 129 130 131 132 133
    KURL::List urls;
    
    if(!KURLDrag::decode(s, urls) || urls.isEmpty())
	return;
	
    QStringList files;
	
    for(KURL::List::Iterator it = urls.begin(); it != urls.end(); it++)
	files.append((*it).path());
134
	
135
    if(playlistSplitter())
136
	playlistSplitter()->addToPlaylist(files, this);
137 138 139 140 141 142
}

void CollectionList::contentsDropEvent(QDropEvent *e)
{
    if(e->source() == this)
	return;
143 144
    else
	decode(e);
145 146 147 148 149 150 151 152 153 154
}

void CollectionList::contentsDragMoveEvent(QDragMoveEvent *e)
{
    if(KURLDrag::canDecode(e) && e->source() != this)
	e->accept(true);
    else
	e->accept(false);
}

155 156
void CollectionList::addToDict(const QString &file, CollectionListItem *item)
{
157
    m_itemsDict.replace(file, item);
158 159 160 161
}

void CollectionList::removeFromDict(const QString &file)
{
162
    m_itemsDict.remove(file);
163 164
}

165 166
void CollectionList::addArtist(const QString &artist)
{
167 168 169
    // Do a bit of caching since there will very often be "two in a row" insertions.
    static QString previousArtist;

170
    if(artist != previousArtist && !m_artistList.insert(artist))
171
	previousArtist = artist;
172 173 174 175
}

void CollectionList::addAlbum(const QString &album)
{
176 177 178
    // Do a bit of caching since there will very often be "two in a row" insertions.
    static QString previousAlbum;

179
    if(album != previousAlbum && !m_albumList.insert(album))
180
	previousAlbum = album;
181
}
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
////////////////////////////////////////////////////////////////////////////////
// CollectionListItem public slots
////////////////////////////////////////////////////////////////////////////////

void CollectionListItem::slotRefresh()
{
    slotRefreshImpl();
    
    if(CollectionList::instance()) {
	CollectionList::instance()->addArtist(text(ArtistColumn));
	CollectionList::instance()->addAlbum(text(AlbumColumn));	
    }
    // This is connected to slotRefreshImpl() for all of the items children.
    emit(signalRefreshed());
}
197

198
////////////////////////////////////////////////////////////////////////////////
199
// CollectionListItem protected methods
200 201
////////////////////////////////////////////////////////////////////////////////

202
CollectionListItem::CollectionListItem(const QFileInfo &file, const QString &path) : PlaylistItem(CollectionList::instance())
203 204 205
{
    CollectionList *l = CollectionList::instance();
    if(l) {
206 207
	l->addToDict(path, this);
	setData(Data::newUser(file, path));
208 209
	slotRefresh();
	connect(this, SIGNAL(signalRefreshed()), l, SIGNAL(signalDataChanged()));
210
	l->emitNumberOfItemsChanged();
211 212 213 214
    }
    else
	kdError() << "CollectionListItems should not be created before"
		  << "CollectionList::initialize() has been called." << endl;
215 216

    SplashScreen::increment();
217 218 219 220 221 222
}

CollectionListItem::~CollectionListItem()
{
    CollectionList *l = CollectionList::instance();
    if(l)
223
	l->removeFromDict(data()->absFilePath());
224 225 226 227
}

void CollectionListItem::addChildItem(PlaylistItem *child)
{
228 229
    connect(child, SIGNAL(signalRefreshed()), this, SLOT(slotRefresh()));
    connect(this, SIGNAL(signalRefreshed()), child, SLOT(slotRefreshImpl()));
230 231
}

Scott Wheeler's avatar
Scott Wheeler committed
232
void CollectionListItem::checkCurrent()
233
{
234 235 236 237
    if(!data()->exists() || !data()->isFile())
	CollectionList::instance()->clearItem(this);
    else if(!data()->tag()->current())
	data()->refresh();
238 239
}

240
#include "collectionlist.moc"