QVariant MusicLibraryModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (Qt::CheckStateRole==role) { return static_cast<MusicLibraryItem *>(index.internalPointer())->checkState(); } MusicLibraryItem *item = static_cast<MusicLibraryItem *>(index.internalPointer()); switch (role) { case Qt::DecorationRole: switch (item->itemType()) { case MusicLibraryItem::Type_Root: return static_cast<MusicLibraryItemRoot *>(item)->icon(); case MusicLibraryItem::Type_Artist: return Icons::self()->artistIcon; case MusicLibraryItem::Type_Album: return Icons::self()->albumIcon(32, true); case MusicLibraryItem::Type_Song: return Song::Playlist==static_cast<MusicLibraryItemSong *>(item)->song().type ? Icons::self()->playlistListIcon : Icons::self()->audioListIcon; default: return QVariant(); } case Cantata::Role_BriefMainText: if (MusicLibraryItem::Type_Album==item->itemType()) { return item->data(); } case Cantata::Role_MainText: case Qt::DisplayRole: if (MusicLibraryItem::Type_Song==item->itemType()) { MusicLibraryItemSong *song = static_cast<MusicLibraryItemSong *>(item); if (Song::Playlist==song->song().type) { return song->song().isCueFile() ? tr("Cue Sheet") : tr("Playlist"); } if (MusicLibraryItem::Type_Root==song->parentItem()->itemType()) { return song->song().trackAndTitleStr(); } return song->song().trackAndTitleStr(); } return item->displayData(); case Qt::ToolTipRole: if (!Settings::self()->infoTooltips()) { return QVariant(); } if (MusicLibraryItem::Type_Song==item->itemType()) { return static_cast<MusicLibraryItemSong *>(item)->song().toolTip(); } return parentData(item)+ (0==item->childCount() ? item->displayData(true) : (item->displayData(true)+"<br/>"+data(index, Cantata::Role_SubText).toString())); case Cantata::Role_SubText: switch (item->itemType()) { case MusicLibraryItem::Type_Root: { MusicLibraryItemRoot *collection=static_cast<MusicLibraryItemRoot *>(item); if (collection->flat()) { return tr("%n Track(s)", "", item->childCount()); } return tr("%n Artist(s)", "", item->childCount()); } case MusicLibraryItem::Type_Artist: return tr("%n Album(s)", "", item->childCount()); case MusicLibraryItem::Type_Song: return Utils::formatTime(static_cast<MusicLibraryItemSong *>(item)->time(), true); case MusicLibraryItem::Type_Album: return tr("%n Tracks (%1)", "", static_cast<MusicLibraryItemAlbum *>(item)->trackCount()) .arg(Utils::formatTime(static_cast<MusicLibraryItemAlbum *>(item)->totalTime())); default: return QVariant(); } case Cantata::Role_ListImage: switch (item->itemType()) { case MusicLibraryItem::Type_Album: return true; default: return false; } case Cantata::Role_CoverSong: { QVariant v; switch (item->itemType()) { case MusicLibraryItem::Type_Album: v.setValue<Song>(static_cast<MusicLibraryItemAlbum *>(item)->coverSong()); break; case MusicLibraryItem::Type_Artist: v.setValue<Song>(static_cast<MusicLibraryItemArtist *>(item)->coverSong()); break; default: break; } return v; } case Cantata::Role_TitleText: if (MusicLibraryItem::Type_Album==item->itemType()) { return tr("%1 by %2", "Album by Artist").arg(item->data()).arg(item->parentItem()->data()); } return item->data(); default: break; } return ActionModel::data(index, role); }
QVariant DevicesModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } MusicLibraryItem *item = static_cast<MusicLibraryItem *>(index.internalPointer()); switch (role) { case Qt::DisplayRole: if (MusicLibraryItem::Type_Song==item->itemType()) { MusicLibraryItemSong *song = static_cast<MusicLibraryItemSong *>(item); if (MusicLibraryItem::Type_Root==song->parentItem()->itemType()) { return song->song().trackAndTitleStr(); } } break; #if defined CDDB_FOUND || defined MUSICBRAINZ5_FOUND case Cantata::Role_Image: if (MusicLibraryItem::Type_Root==item->itemType()) { Device *dev=static_cast<Device *>(item); if (Device::AudioCd==dev->devType()) { return static_cast<AudioCdDevice *>(dev)->coverPix(); } } break; #endif case Cantata::Role_SubText: if (MusicLibraryItem::Type_Root==item->itemType()) { Device *dev=static_cast<Device *>(item); if (!dev->statusMessage().isEmpty()) { return dev->statusMessage(); } if (!dev->isConnected()) { QString sub=dev->subText(); return i18n("Not Connected")+(sub.isEmpty() ? QString() : (QString(" - ")+sub)); } if (Device::AudioCd==dev->devType()) { return dev->subText(); } } break; case Cantata::Role_Capacity: if (MusicLibraryItem::Type_Root==item->itemType()) { return static_cast<Device *>(item)->usedCapacity(); } return QVariant(); case Cantata::Role_CapacityText: if (MusicLibraryItem::Type_Root==item->itemType()) { return static_cast<Device *>(item)->capacityString(); } return QVariant(); case Cantata::Role_Actions: { QVariant v; if (MusicLibraryItem::Type_Root==item->itemType()) { QList<Action *> actions; if (Device::AudioCd!=static_cast<Device *>(item)->devType()) { actions << configureAction; } else if (HttpServer::self()->isAlive()) { actions << StdActions::self()->replacePlayQueueAction; } actions << refreshAction; if (static_cast<Device *>(item)->supportsDisconnect()) { actions << (static_cast<Device *>(item)->isConnected() ? disconnectAction : connectAction); } #if defined CDDB_FOUND || defined MUSICBRAINZ5_FOUND if (Device::AudioCd==static_cast<Device *>(item)->devType()) { actions << editAction; } #endif v.setValue<QList<Action *> >(actions); } else if (root(item)->canPlaySongs() && HttpServer::self()->isAlive()) { v.setValue<QList<Action *> >(QList<Action *>() << StdActions::self()->replacePlayQueueAction << StdActions::self()->appendToPlayQueueAction); } return v; } case Cantata::Role_ListImage: return MusicLibraryItem::Type_Album==item->itemType(); default: break; } return MultiMusicModel::data(index, role); }
QVariant MusicModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } MusicLibraryItem *item = static_cast<MusicLibraryItem *>(index.internalPointer()); switch (role) { #ifndef ENABLE_UBUNTU case Qt::DecorationRole: switch (item->itemType()) { case MusicLibraryItem::Type_Root: return static_cast<MusicLibraryItemRoot *>(item)->icon(); case MusicLibraryItem::Type_Artist: return static_cast<MusicLibraryItemArtist *>(item)->isComposer() ? Icons::self()->composerIcon : Icons::self()->artistIcon; #ifdef ENABLE_ONLINE_SERVICES case MusicLibraryItem::Type_Podcast: return Icons::self()->podcastIcon; #endif case MusicLibraryItem::Type_Album: return Icons::self()->albumIcon; case MusicLibraryItem::Type_Song: return Song::Playlist==static_cast<MusicLibraryItemSong *>(item)->song().type ? Icons::self()->playlistIcon : Icons::self()->audioFileIcon; default: return QVariant(); } #endif case Cantata::Role_BriefMainText: if (MusicLibraryItem::Type_Album==item->itemType()) { return item->data(); } case Cantata::Role_MainText: case Qt::DisplayRole: if (MusicLibraryItem::Type_Song==item->itemType()) { MusicLibraryItemSong *song = static_cast<MusicLibraryItemSong *>(item); if (Song::Playlist==song->song().type) { return song->song().isCueFile() ? i18n("Cue Sheet") : i18n("Playlist"); } if (MusicLibraryItem::Type_Root==song->parentItem()->itemType()) { return song->song().artistSong(); } if (static_cast<MusicLibraryItemAlbum *>(song->parentItem())->isSingleTracks()) { return song->song().artistSong(); } #ifdef ENABLE_ONLINE_SERVICES if (MusicLibraryItem::Type_Podcast==song->parentItem()->itemType()) { return item->data(); } #endif return song->song().trackAndTitleStr(); } return item->displayData(); case Qt::ToolTipRole: if (!Settings::self()->infoTooltips()) { return QVariant(); } if (MusicLibraryItem::Type_Song==item->itemType()) { #ifdef ENABLE_ONLINE_SERVICES if (MusicLibraryItem::Type_Podcast==item->parentItem()->itemType()) { return parentData(item)+data(index, Qt::DisplayRole).toString()+QLatin1String("<br/>")+ Utils::formatTime(static_cast<MusicLibraryItemSong *>(item)->time(), true)+ QLatin1String("<br/><small><i>")+static_cast<MusicLibraryItemPodcastEpisode *>(item)->published()+QLatin1String("</i></small>"); } #endif return static_cast<MusicLibraryItemSong *>(item)->song().toolTip(); } return parentData(item)+ (0==item->childCount() ? item->displayData(true) : (item->displayData(true)+"<br/>"+data(index, Cantata::Role_SubText).toString())); case Cantata::Role_SubText: switch (item->itemType()) { case MusicLibraryItem::Type_Root: { MusicLibraryItemRoot *collection=static_cast<MusicLibraryItemRoot *>(item); if (collection->flat()) { return Plurals::tracks(item->childCount()); } return Plurals::artists(item->childCount()); } case MusicLibraryItem::Type_Artist: return Plurals::albums(item->childCount()); case MusicLibraryItem::Type_Podcast: return Plurals::episodes(item->childCount()); case MusicLibraryItem::Type_Song: return Utils::formatTime(static_cast<MusicLibraryItemSong *>(item)->time(), true); case MusicLibraryItem::Type_Album: return Plurals::tracksWithDuration(static_cast<MusicLibraryItemAlbum *>(item)->trackCount(), Utils::formatTime(static_cast<MusicLibraryItemAlbum *>(item)->totalTime())); default: return QVariant(); } case Cantata::Role_ListImage: switch (item->itemType()) { case MusicLibraryItem::Type_Album: return true; #ifdef ENABLE_ONLINE_SERVICES case MusicLibraryItem::Type_Podcast: return true; #endif default: return false; } #ifdef ENABLE_UBUNTU case Cantata::Role_Image: switch (item->itemType()) { case MusicLibraryItem::Type_Album: return static_cast<MusicLibraryItemAlbum *>(item)->cover(); case MusicLibraryItem::Type_Artist: return static_cast<MusicLibraryItemArtist *>(item)->cover(); default: return QString(); } #endif case Cantata::Role_CoverSong: { QVariant v; switch (item->itemType()) { case MusicLibraryItem::Type_Album: v.setValue<Song>(static_cast<MusicLibraryItemAlbum *>(item)->coverSong()); break; case MusicLibraryItem::Type_Artist: v.setValue<Song>(static_cast<MusicLibraryItemArtist *>(item)->coverSong()); break; #ifdef ENABLE_ONLINE_SERVICES case MusicLibraryItem::Type_Podcast: v.setValue<Song>(static_cast<MusicLibraryItemPodcast *>(item)->coverSong()); break; #endif default: break; } return v; } case Cantata::Role_TitleText: if (MusicLibraryItem::Type_Album==item->itemType()) { return i18nc("Album by Artist", "%1 by %2", item->data(), item->parentItem()->data()); } return item->data(); default: break; } return ActionModel::data(index, role); }
QList<MusicLibraryItemArtist *> * MPDParseUtils::parseLibraryItems(const QByteArray * const data) { QList<MusicLibraryItemArtist *> *artists = new QList<MusicLibraryItemArtist *>; QByteArray currentItem; MusicLibraryItemArtist *artistItem = NULL; MusicLibraryItemAlbum *albumItem = NULL; MusicLibraryItemSong *songItem = NULL; Song *currentSong; bool found = false; QList<QByteArray> lines = data->split('\n'); int amountOfLines = lines.size(); for(int i = 0; i < amountOfLines; i++) { currentItem += lines.at(i); currentItem += "\n"; if(i == lines.size() - 1 || lines.at(i + 1).startsWith("file:")) { currentSong = parseSong(¤tItem); currentItem.clear(); if(currentSong->isEmpty()) { delete currentSong; continue; } currentSong->fillEmptyFields(); int amountOfArtists = artists->size(); // Check if artist already exists for(int i = 0; i < amountOfArtists; i++) { if(artists->at(i)->data(0) == currentSong->artist) { artistItem = artists->at(i); found = true; } } if(!found) { artistItem = new MusicLibraryItemArtist(currentSong->artist); artists->append(artistItem); } found = false; int amountOfAlbums = artistItem->childCount(); // Check if album already exists for(int i = 0; i < amountOfAlbums; i++) { if(artistItem->child(i)->data(0) == currentSong->album) { albumItem = static_cast<MusicLibraryItemAlbum *>(artistItem->child(i)); found = true; } } if(!found) { albumItem = new MusicLibraryItemAlbum(currentSong->album, artistItem); artistItem->appendChild(albumItem); } found = false; // Add song to album (possibly in track order) songItem = new MusicLibraryItemSong(currentSong->title, albumItem); songItem->setFile(currentSong->file); songItem->setTrack(currentSong->track); songItem->setDisc(currentSong->disc); albumItem->appendChild(songItem); delete currentSong; } } return artists; }