playlistitem.cpp 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
16

17 18
#include "playlistitem.h"

19
#include <config-juk.h>
20
#include <kiconloader.h>
21

22
#include <QPixmap>
Laurent Montel's avatar
Laurent Montel committed
23
#include <QFileInfo>
24
#include <QGlobalStatic>
25

26
#include "collectionlist.h"
27
#include "musicbrainzquery.h"
28
#include "tag.h"
29
#include "coverinfo.h"
30
#include "covermanager.h"
31
#include "tagtransactionmanager.h"
32
#include "juk_debug.h"
33

34 35
PlaylistItemList PlaylistItem::m_playingItems; // static

36 37
static void startMusicBrainzQuery(const FileHandle &file)
{
38
#if HAVE_TUNEPIMP
Scott Wheeler's avatar
Scott Wheeler committed
39
    // This deletes itself when finished.
40
    new MusicBrainzLookup(file);
41 42 43 44
#else
    Q_UNUSED(file)
#endif
}
45 46

////////////////////////////////////////////////////////////////////////////////
47
// PlaylistItem public methods
48 49
////////////////////////////////////////////////////////////////////////////////

50
PlaylistItem::~PlaylistItem()
51
{
52 53 54 55 56
    // Although this isn't the most efficient way to accomplish the task of
    // stopping playback when deleting the item being played, it has the
    // stark advantage of working reliably.  I'll tell anyone who tries to
    // optimize this, the timing issues can be *hard*. -- mpyne

57
    m_collectionItem->removeChildItem(this);
58

59
    if(m_playingItems.contains(this)) {
60
        m_playingItems.removeAll(this);
61 62
        if(m_playingItems.isEmpty())
            playlist()->setPlaying(0);
63
    }
64

65 66 67
    playlist()->updateDeletedItem(this);
    emit playlist()->signalAboutToRemove(this);

68
    if(m_watched)
69
        Pointer::clear(this);
70 71
}

72
void PlaylistItem::setFile(const FileHandle &file)
73
{
74
    m_collectionItem->updateCollectionDict(d->fileHandle.absFilePath(), file.absFilePath());
75
    d->fileHandle = file;
76
    refresh();
77 78
}

79 80 81 82 83 84 85 86
void PlaylistItem::setFile(const QString &file)
{
    QString oldPath = d->fileHandle.absFilePath();
    d->fileHandle.setFile(file);
    m_collectionItem->updateCollectionDict(oldPath, d->fileHandle.absFilePath());
    refresh();
}

87
FileHandle PlaylistItem::file() const
88
{
89
    return d->fileHandle;
90 91
}

92 93
Q_GLOBAL_STATIC_WITH_ARGS(QPixmap, globalGenericImage, (SmallIcon("image-x-generic")))
Q_GLOBAL_STATIC_WITH_ARGS(QPixmap, globalPlayingImage, (UserIcon("playing")))
Sebastian Sauer's avatar
Sebastian Sauer committed
94

95
const QPixmap *PlaylistItem::pixmap(int column) const
96 97 98
{
    int offset = playlist()->columnOffset();

99 100 101 102 103 104 105
    // Don't use hasCover here because that may dig into the track itself.
    // Besides, we really just want to know if the cover manager has a cover
    // for the track.

    if((column - offset) == CoverColumn &&
        d->fileHandle.coverInfo()->coverId() != CoverManager::NoMatch)
    {
Sebastian Sauer's avatar
Sebastian Sauer committed
106
        return globalGenericImage;
107
    }
108

109
    if(column == playlist()->leftColumn() &&
Sebastian Sauer's avatar
Sebastian Sauer committed
110 111 112 113
        m_playingItems.contains(const_cast<PlaylistItem *>(this)))
    {
        return globalPlayingImage;
    }
114

115 116
    //return QTreeWidgetItem::pixmap(column);
    return nullptr;
117 118
}

119 120
QString PlaylistItem::text(int column) const
{
121
    if(!d->fileHandle.tag())
122
        return QString();
123

124
    int offset = playlist()->columnOffset();
125 126

    switch(column - offset) {
127
    case TrackColumn:
128
        return d->fileHandle.tag()->title();
129
    case ArtistColumn:
130
        return d->fileHandle.tag()->artist();
131
    case AlbumColumn:
132
        return d->fileHandle.tag()->album();
133
    case CoverColumn:
134
        return QString();
135
    case TrackNumberColumn:
136 137
        return d->fileHandle.tag()->track() > 0
            ? QString::number(d->fileHandle.tag()->track())
138
            : QString();
139
    case GenreColumn:
140
        return d->fileHandle.tag()->genre();
141
    case YearColumn:
142 143
        return d->fileHandle.tag()->year() > 0
            ? QString::number(d->fileHandle.tag()->year())
144
            : QString();
145
    case LengthColumn:
146
        return d->fileHandle.tag()->lengthString();
147
    case BitrateColumn:
148
        return QString::number(d->fileHandle.tag()->bitrate());
149
    case CommentColumn:
150
        return d->fileHandle.tag()->comment();
151
    case FileNameColumn:
152
        return d->fileHandle.fileInfo().fileName();
153
    case FullPathColumn:
154
        return d->fileHandle.fileInfo().absoluteFilePath();
155
    default:
156
        return QTreeWidgetItem::text(column);
157 158 159
    }
}

160 161
void PlaylistItem::setText(int column, const QString &text)
{
162
    QTreeWidgetItem::setText(column, text);
163
    playlist()->slotWeightDirty(column);
164
}
165

166
void PlaylistItem::setPlaying(bool playing, bool master)
167
{
168
    m_playingItems.removeAll(this);
169

170
    if(playing) {
171 172 173 174
        if(master)
            m_playingItems.prepend(this);
        else
            m_playingItems.append(this);
175
    }
176 177
    else {

178 179
        // This is a tricky little recursion, but it
        // in fact does clear the list.
180

181 182
        if(!m_playingItems.isEmpty())
            m_playingItems.front()->setPlaying(false);
183
    }
184

185
    treeWidget()->viewport()->update();
186 187
}

188 189
void PlaylistItem::setSelected(bool selected)
{
190
    playlist()->markItemSelected(this, selected);
191
    QTreeWidgetItem::setSelected(selected);
192 193
}

194
void PlaylistItem::guessTagInfo(TagGuesser::Type type)
195
{
196 197 198
    switch(type) {
    case TagGuesser::FileName:
    {
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        TagGuesser guesser(d->fileHandle.absFilePath());
        Tag *tag = TagTransactionManager::duplicateTag(d->fileHandle.tag());

        if(!guesser.title().isNull())
            tag->setTitle(guesser.title());
        if(!guesser.artist().isNull())
            tag->setArtist(guesser.artist());
        if(!guesser.album().isNull())
            tag->setAlbum(guesser.album());
        if(!guesser.track().isNull())
            tag->setTrack(guesser.track().toInt());
        if(!guesser.comment().isNull())
            tag->setComment(guesser.comment());

        TagTransactionManager::instance()->changeTagOnItem(this, tag);
        break;
215 216
    }
    case TagGuesser::MusicBrainz:
217 218
        startMusicBrainzQuery(d->fileHandle);
        break;
219
    }
220 221
}

222 223
Playlist *PlaylistItem::playlist() const
{
224
    return static_cast<Playlist *>(treeWidget());
225 226
}

227
QVector<int> PlaylistItem::cachedWidths() const
228
{
229
    return d->cachedWidths;
230 231
}

232
void PlaylistItem::refresh()
233
{
234
    m_collectionItem->refresh();
235 236
}

237
void PlaylistItem::refreshFromDisk()
238
{
239
    d->fileHandle.refresh();
240
    refresh();
241 242
}

243
void PlaylistItem::clear()
244
{
245
    playlist()->clearItem(this);
246 247
}

248
////////////////////////////////////////////////////////////////////////////////
249
// PlaylistItem protected methods
250 251
////////////////////////////////////////////////////////////////////////////////

252
PlaylistItem::PlaylistItem(CollectionListItem *item, Playlist *parent) :
253
    QTreeWidgetItem(parent),
254 255
    d(0),
    m_watched(0)
256
{
257
    setup(item);
258 259
}

260 261
PlaylistItem::PlaylistItem(CollectionListItem *item, Playlist *parent, QTreeWidgetItem *after) :
    QTreeWidgetItem(parent, after),
262 263
    d(0),
    m_watched(0)
264
{
265
    setup(item);
266 267
}

268 269

// This constructor should only be used by the CollectionList subclass.
270

271
PlaylistItem::PlaylistItem(CollectionList *parent) :
272
    QTreeWidgetItem(parent),
273
    m_watched(0)
274
{
275 276
    d = new Data;
    m_collectionItem = static_cast<CollectionListItem *>(this);
277
    setFlags(flags() | Qt::ItemIsDragEnabled);
278 279
}

280
// FIXME paintCell
281
/*void PlaylistItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align)
282
{
283
    if(!m_playingItems.contains(this))
284
        return QTreeWidgetItem::paintCell(p, cg, column, width, align);
285

Stephan Kulow's avatar
Stephan Kulow committed
286
    QPalette colorGroup = cg;
287

Stephan Kulow's avatar
Stephan Kulow committed
288 289
    QColor base = colorGroup.color( QPalette::Base );
    QColor selection = colorGroup.color( QPalette::Highlight );
290 291 292 293 294 295 296

    int r = (base.red() + selection.red()) / 2;
    int b = (base.blue() + selection.blue()) / 2;
    int g = (base.green() + selection.green()) / 2;

    QColor c(r, g, b);

297
    colorGroup.setColor(QPalette::Base, c);
298 299
    QTreeWidgetItem::paintCell(p, colorGroup, column, width, align);
}*/
300

301
int PlaylistItem::compare(QTreeWidgetItem *item, int column, bool ascending) const
302 303 304
{
    // reimplemented from QListViewItem

305
    int offset = playlist()->columnOffset();
306

307
    if(!item)
308
        return 0;
309

310
    PlaylistItem *playlistItem = static_cast<PlaylistItem *>(item);
311 312 313

    // The following statments first check to see if you can sort based on the
    // specified column.  If the values for the two PlaylistItems are the same
314
    // in that column it then tries to sort based on columns 1, 2, 3 and 0,
315 316
    // (artist, album, track number, track name) in that order.

317 318 319
    int c = compare(this, playlistItem, column, ascending);

    if(c != 0)
320
        return c;
321
    else {
322 323 324
        // Loop through the columns doing comparisons until something is differnt.
        // If all else is the same, compare the track name.

Kacper Kasper's avatar
Kacper Kasper committed
325
        int last = !playlist()->isColumnHidden(AlbumColumn + offset) ? TrackNumberColumn : ArtistColumn;
326 327

        for(int i = ArtistColumn; i <= last; i++) {
Kacper Kasper's avatar
Kacper Kasper committed
328
            if(!playlist()->isColumnHidden(i + offset)) {
329 330 331 332 333 334
                c = compare(this, playlistItem, i, ascending);
                if(c != 0)
                    return c;
            }
        }
        return compare(this, playlistItem, TrackColumn + offset, ascending);
335 336 337
    }
}

338
int PlaylistItem::compare(const PlaylistItem *firstItem, const PlaylistItem *secondItem, int column, bool) const
339
{
340
    int offset = playlist()->columnOffset();
341

342
    if(column < 0 || column > lastColumn() + offset || !firstItem->d || !secondItem->d)
343
        return 0;
344

345
    if(column < offset) {
Dirk Mueller's avatar
Dirk Mueller committed
346 347
        QString first = firstItem->text(column).toLower();
        QString second = secondItem->text(column).toLower();
348
        return first.localeAwareCompare(second);
349 350
    }

351 352
    switch(column - offset) {
    case TrackNumberColumn:
353
        if(firstItem->d->fileHandle.tag()->track() > secondItem->d->fileHandle.tag()->track())
354
            return 1;
355
        else if(firstItem->d->fileHandle.tag()->track() < secondItem->d->fileHandle.tag()->track())
356
            return -1;
357
        else
358
            return 0;
359
        break;
360
    case LengthColumn:
361
        if(firstItem->d->fileHandle.tag()->seconds() > secondItem->d->fileHandle.tag()->seconds())
362
            return 1;
363
        else if(firstItem->d->fileHandle.tag()->seconds() < secondItem->d->fileHandle.tag()->seconds())
364
            return -1;
365
        else
366
            return 0;
367
        break;
368 369 370 371 372 373 374
    case BitrateColumn:
        if(firstItem->d->fileHandle.tag()->bitrate() > secondItem->d->fileHandle.tag()->bitrate())
            return 1;
        else if(firstItem->d->fileHandle.tag()->bitrate() < secondItem->d->fileHandle.tag()->bitrate())
            return -1;
        else
            return 0;
375
        break;
376
    case CoverColumn:
Michael Pyne's avatar
Michael Pyne committed
377
        if(firstItem->d->fileHandle.coverInfo()->coverId() == secondItem->d->fileHandle.coverInfo()->coverId())
378
            return 0;
Michael Pyne's avatar
Michael Pyne committed
379
        else if (firstItem->d->fileHandle.coverInfo()->coverId() != CoverManager::NoMatch)
380 381 382 383
            return -1;
        else
            return 1;
        break;
384
    default:
385 386
        return QString::localeAwareCompare(firstItem->d->metadata[column - offset],
                                           secondItem->d->metadata[column - offset]);
387
    }
388
}
389

390
bool PlaylistItem::isValid() const
391
{
392
    return bool(d->fileHandle.tag());
393 394
}

395 396 397 398 399
void PlaylistItem::setTrackId(quint32 id)
{
    m_trackId = id;
}

400 401 402 403
////////////////////////////////////////////////////////////////////////////////
// PlaylistItem private methods
////////////////////////////////////////////////////////////////////////////////

404
void PlaylistItem::setup(CollectionListItem *item)
405
{
406 407
    m_collectionItem = item;

408 409
    d = item->d;
    item->addChildItem(this);
410 411 412 413 414 415 416 417
    setFlags(flags() | Qt::ItemIsDragEnabled);

    int offset = playlist()->columnOffset();
    int columns = lastColumn() + offset + 1;

    for(int i = offset; i < columns; i++) {
        setText(i, text(i));
    }
418
}
419 420 421 422 423

////////////////////////////////////////////////////////////////////////////////
// PlaylistItem::Pointer implementation
////////////////////////////////////////////////////////////////////////////////

424
QMap<PlaylistItem *, QList<PlaylistItem::Pointer *> > PlaylistItem::Pointer::m_map; // static
425 426 427 428 429

PlaylistItem::Pointer::Pointer(PlaylistItem *item) :
    m_item(item)
{
    if(!m_item)
430 431
        return;

432 433 434 435 436 437 438 439 440 441 442 443 444
    m_item->m_watched = true;
    m_map[m_item].append(this);
}

PlaylistItem::Pointer::Pointer(const Pointer &p) :
    m_item(p.m_item)
{
    m_map[m_item].append(this);
}

PlaylistItem::Pointer::~Pointer()
{
    if(!m_item)
445
        return;
446

447
    m_map[m_item].removeAll(this);
448
    if(m_map[m_item].isEmpty()) {
449 450
        m_map.remove(m_item);
        m_item->m_watched = false;
451 452 453 454 455 456
    }
}

PlaylistItem::Pointer &PlaylistItem::Pointer::operator=(PlaylistItem *item)
{
    if(item == m_item)
457
        return *this;
458 459

    if(m_item) {
460
        m_map[m_item].removeAll(this);
461 462 463 464
        if(m_map[m_item].isEmpty()) {
            m_map.remove(m_item);
            m_item->m_watched = false;
        }
465 466 467
    }

    if(item) {
468 469
        m_map[item].append(this);
        item->m_watched = true;
470 471 472 473 474 475 476 477 478 479
    }

    m_item = item;

    return *this;
}

void PlaylistItem::Pointer::clear(PlaylistItem *item) // static
{
    if(!item)
480
        return;
481

482 483 484
    QList<Pointer *> l = m_map[item];
    foreach(Pointer *pointer, l)
        pointer->m_item = 0;
485 486 487
    m_map.remove(item);
    item->m_watched = false;
}
488 489

// vim: set et sw=4 tw=0 sta: