/* * Cantata * * Copyright (c) 2011 Craig Drummond * */ /* * Copyright (c) 2008 Sander Knopper (sander AT knopper DOT tk) and * Roeland Douma (roeland AT rullzer DOT com) * * This file is part of QtMPC. * * QtMPC 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. * * QtMPC 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 QtMPC. If not, see . */ #include #ifdef ENABLE_KDE_SUPPORT #include #endif #include "playlistsmodel.h" #include "mpdparseutils.h" #include "mpdstats.h" #include "mpdconnection.h" PlaylistsModel::PlaylistsModel(QObject *parent) : QAbstractListModel(parent) { connect(MPDConnection::self(), SIGNAL(playlistsRetrieved(const QList &)), this, SLOT(setPlaylists(const QList &))); connect(MPDConnection::self(), SIGNAL(playlistInfoRetrieved(const QString &, const QList &)), this, SLOT(playlistInfoRetrieved(const QString &, const QList &))); connect(this, SIGNAL(listPlaylists()), MPDConnection::self(), SLOT(listPlaylists())); connect(this, SIGNAL(playlistInfo(const QString &)), MPDConnection::self(), SLOT(playlistInfo(const QString &))); } PlaylistsModel::~PlaylistsModel() { } QVariant PlaylistsModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const { return QVariant(); } // QModelIndex PlaylistsModel::index(int row, int column, const QModelIndex &parent) const // { // Q_UNUSED(parent) // // if(row= items.size()) return QVariant(); const Playlist &pl=items.at(index.row()); switch(role) { case Qt::DisplayRole: return pl.name; case Qt::ToolTipRole: return 0==pl.songs.count() ? pl.name : #ifdef ENABLE_KDE_SUPPORT i18np("%1\n1 Song", "%1\n%2 Songs", pl.name, pl.songs.count()); #else (pl.songs.count()>1 ? tr("%1\n%2 Songs").arg(pl.name).arg(pl.songs.count()) : tr("%1\n1 Song").arg(pl.name); #endif #if 0 QString duration=MPDParseUtils::formatDuration(static_cast(item)->time()); if (duration.startsWith(QLatin1String("00:"))) { duration=duration.mid(3); } if (duration.startsWith(QLatin1String("00:"))) { duration=duration.mid(1); } return song.name+!Char('\n')+duration; #endif default: break; } return QVariant(); } void PlaylistsModel::getPlaylists() { emit listPlaylists(); } void PlaylistsModel::clear() { beginResetModel(); items=QList(); endResetModel(); } bool PlaylistsModel::exists(const QString &n) const { foreach (const Playlist &p, items) { if (p.name==n) { return true; } } return false; } void PlaylistsModel::setPlaylists(const QList &playlists) { bool diff=playlists.count()!=items.count(); if (!diff) { foreach (const Playlist &p, playlists) { if (!items.contains(p)) { diff=true; break; } } } if (diff) { beginResetModel(); items=playlists; } foreach (const Playlist &p, items) { emit playlistInfo(p.name); } if (diff) { endResetModel(); } } #include void PlaylistsModel::playlistInfoRetrieved(const QString &name, const QList &songs) { int index=items.indexOf(Playlist(name)); if (index>=0 && index