Revert previous commit, was incorrect
[amarok.git] / src / playlistbrowser / PodcastModel.cpp
blob9291d4d76a7bad4436a2cdc75c1e741199b385ad
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Bart Cerneels <bart.cerneels@gmail.com>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "debug.h"
20 #include "PodcastModel.h"
21 #include "playlistmanager/PlaylistManager.h"
22 #include "TheInstances.h"
23 #include "PodcastCollection.h"
24 #include "PodcastMeta.h"
26 #include <QListIterator>
27 #include <typeinfo>
29 using namespace Meta;
31 namespace PlaylistBrowserNS {
33 PodcastModel::PodcastModel()
34 : QAbstractItemModel()
36 QList<PlaylistPtr> playlists =
37 The::playlistManager()->playlistsOfCategory( PlaylistManager::PodcastChannel );
38 QListIterator<PlaylistPtr> i(playlists);
39 while (i.hasNext())
40 m_channels << PodcastChannelPtr::staticCast( i.next() );
42 connect( The::playlistManager(), SIGNAL(updated()), SLOT(slotUpdate()));
46 PodcastModel::~PodcastModel()
50 QVariant
51 PodcastModel::data(const QModelIndex & index, int role) const
53 if ( !index.isValid() )
54 return QVariant();
56 if ( role == Qt::DisplayRole )
58 PodcastMetaCommon* pmc = static_cast<PodcastMetaCommon *>( index.internalPointer() );
60 bool isChannel = false;
61 QString title;
62 QString description;
63 if ( typeid( * pmc ) == typeid( PodcastChannel ) )
65 PodcastChannel *channel = static_cast<PodcastChannel *>(index.internalPointer());
66 title = channel->title();
67 description = channel->description();
68 isChannel = true;
70 else if ( typeid( * pmc ) == typeid( PodcastEpisode ) )
72 PodcastEpisode *episode = static_cast<PodcastEpisode *>(index.internalPointer());
73 title = episode->title();
74 description = episode->description();
75 isChannel = false;
78 switch( index.column() )
80 case 0: return isChannel ? QString("Channel") : QString("Episode"); break;
81 case 1: return title; break;
82 case 2: return description; break;
83 default: return QVariant(); break;
86 return QVariant();
89 QModelIndex
90 PodcastModel::index(int row, int column, const QModelIndex & parent) const
92 if (!hasIndex(row, column, parent))
93 return QModelIndex();
95 PodcastChannelPtr channel;
96 PodcastEpisodePtr episode;
98 if (!parent.isValid())
99 channel = m_channels[row];
100 else
102 channel = static_cast<PodcastChannel *>(parent.internalPointer());
103 if( !channel.isNull() )
105 episode = channel->episodes()[row];
107 else
109 channel = 0;
113 if( !episode.isNull() )
115 return createIndex( row, column, episode.data() );
117 else if( !channel.isNull() )
119 return createIndex( row, column, channel.data() );
121 else
122 return QModelIndex();
125 QModelIndex
126 PodcastModel::parent(const QModelIndex & index) const
128 if (!index.isValid())
129 return QModelIndex();
131 PodcastMetaCommon *podcastMetaCommon = static_cast<PodcastMetaCommon *>(index.internalPointer());
133 if ( typeid( * podcastMetaCommon ) == typeid( PodcastChannel ) )
135 return QModelIndex();
137 else if ( typeid( * podcastMetaCommon ) == typeid( PodcastEpisode ) )
139 //BUG: using static_cast on podcastMetaCommon returns wrong address (exact offset of 12 bytes)
140 PodcastEpisode *episode = static_cast<PodcastEpisode *>( index.internalPointer() );
141 int row = m_channels.indexOf( episode->channel() );
142 return createIndex( row , 0, episode->channel().data() );
144 else
146 return QModelIndex();
151 PodcastModel::rowCount(const QModelIndex & parent) const
153 if (parent.column() > 0)
154 return 0;
156 if (!parent.isValid())
158 return m_channels.count();
160 else
162 PodcastMetaCommon *podcastMetaCommon = static_cast<PodcastMetaCommon *>(parent.internalPointer());
164 if ( typeid( * podcastMetaCommon ) == typeid( PodcastChannel ) )
166 PodcastChannel *channel = static_cast<PodcastChannel*>(parent.internalPointer());
167 return channel->episodes().count();
169 else if ( typeid( * podcastMetaCommon ) == typeid( PodcastEpisode ) )
171 return 0;
173 else
175 return 0;
181 PodcastModel::columnCount(const QModelIndex & /*parent*/) const
183 return 3;
186 Qt::ItemFlags
187 PodcastModel::flags(const QModelIndex & index) const
189 if (!index.isValid())
190 return 0;
192 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
195 QVariant
196 PodcastModel::headerData(int section, Qt::Orientation orientation, int role) const
198 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
199 switch( section )
201 case 0: return QString("Type");
202 case 1: return QString("Title");
203 case 2: return QString("Summary");
204 default: return QVariant();
208 return QVariant();
211 void
212 PodcastModel::slotUpdate()
214 //TODO: emit dataChanged( QModelIndex(), QModelIndex() );
216 QList<PlaylistPtr> playlists =
217 The::playlistManager()->playlistsOfCategory( PlaylistManager::PodcastChannel );
218 QListIterator<PlaylistPtr> i(playlists);
219 m_channels.clear();
220 while (i.hasNext())
222 PodcastChannelPtr channel = PodcastChannelPtr::staticCast( i.next() );
223 m_channels << channel;
226 emit layoutAboutToBeChanged();
227 emit layoutChanged();
232 #include "PodcastModel.moc"