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
20 #include "PodcastModel.h"
21 #include "playlistmanager/PlaylistManager.h"
22 #include "TheInstances.h"
23 #include "PodcastCollection.h"
24 #include "PodcastMeta.h"
26 #include <QListIterator>
31 namespace PlaylistBrowserNS
{
33 PodcastModel::PodcastModel()
34 : QAbstractItemModel()
36 QList
<PlaylistPtr
> playlists
=
37 The::playlistManager()->playlistsOfCategory( PlaylistManager::PodcastChannel
);
38 QListIterator
<PlaylistPtr
> i(playlists
);
40 m_channels
<< PodcastChannelPtr::staticCast( i
.next() );
42 connect( The::playlistManager(), SIGNAL(updated()), SLOT(slotUpdate()));
46 PodcastModel::~PodcastModel()
51 PodcastModel::data(const QModelIndex
& index
, int role
) const
53 if ( !index
.isValid() )
56 if ( role
== Qt::DisplayRole
)
58 PodcastMetaCommon
* pmc
= static_cast<PodcastMetaCommon
*>( index
.internalPointer() );
60 bool isChannel
= false;
63 if ( typeid( * pmc
) == typeid( PodcastChannel
) )
65 PodcastChannel
*channel
= static_cast<PodcastChannel
*>(index
.internalPointer());
66 title
= channel
->title();
67 description
= channel
->description();
70 else if ( typeid( * pmc
) == typeid( PodcastEpisode
) )
72 PodcastEpisode
*episode
= static_cast<PodcastEpisode
*>(index
.internalPointer());
73 title
= episode
->title();
74 description
= episode
->description();
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;
90 PodcastModel::index(int row
, int column
, const QModelIndex
& parent
) const
92 if (!hasIndex(row
, column
, parent
))
95 PodcastChannelPtr channel
;
96 PodcastEpisodePtr episode
;
98 if (!parent
.isValid())
99 channel
= m_channels
[row
];
102 channel
= static_cast<PodcastChannel
*>(parent
.internalPointer());
103 if( !channel
.isNull() )
105 episode
= channel
->episodes()[row
];
113 if( !episode
.isNull() )
115 return createIndex( row
, column
, episode
.data() );
117 else if( !channel
.isNull() )
119 return createIndex( row
, column
, channel
.data() );
122 return 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() );
146 return QModelIndex();
151 PodcastModel::rowCount(const QModelIndex
& parent
) const
153 if (parent
.column() > 0)
156 if (!parent
.isValid())
158 return m_channels
.count();
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
) )
181 PodcastModel::columnCount(const QModelIndex
& /*parent*/) const
187 PodcastModel::flags(const QModelIndex
& index
) const
189 if (!index
.isValid())
192 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
196 PodcastModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
198 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
201 case 0: return QString("Type");
202 case 1: return QString("Title");
203 case 2: return QString("Summary");
204 default: return QVariant();
212 PodcastModel::slotUpdate()
214 //TODO: emit dataChanged( QModelIndex(), QModelIndex() );
216 QList
<PlaylistPtr
> playlists
=
217 The::playlistManager()->playlistsOfCategory( PlaylistManager::PodcastChannel
);
218 QListIterator
<PlaylistPtr
> i(playlists
);
222 PodcastChannelPtr channel
= PodcastChannelPtr::staticCast( i
.next() );
223 m_channels
<< channel
;
226 emit
layoutAboutToBeChanged();
227 emit
layoutChanged();
232 #include "PodcastModel.moc"