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 "TheInstances.h"
22 #include "PodcastCollection.h"
23 #include "PodcastMeta.h"
27 namespace PlaylistBrowserNS
{
29 PodcastModel::PodcastModel()
30 : QAbstractItemModel()
33 connect( The::podcastCollection(), SIGNAL(updated()), SLOT(slotUpdate()));
37 PodcastModel::~PodcastModel()
42 PlaylistBrowserNS::PodcastModel::data(const QModelIndex
& index
, int role
) const
46 debug() << k_funcinfo
<< " index: " << index
.row() << ":" << index
.column();
47 switch( index
.column() )
49 case 0: return QString("data 0"); break;
50 case 1: return QString("data 1"); break;
51 case 2: return QString("data 2"); break;
52 case 3: return QString("data 3"); break;
53 case 4: return QString("data 4"); break;
54 default: return QString("data ?"); break;
60 PlaylistBrowserNS::PodcastModel::index(int row
, int column
, const QModelIndex
& parent
) const
63 debug() << k_funcinfo
<< " parent: " << parent
.row() << ":" << parent
.column();
64 debug() << k_funcinfo
<< " row: " << row
<< " column: " << column
;
65 if (!hasIndex(row
, column
, parent
))
67 debug() << k_funcinfo
<< "!hasIndex(row, column, parent)";
71 PodcastChannelPtr channel
;
72 PodcastEpisodePtr episode
;
74 if (!parent
.isValid())
76 debug() << k_funcinfo
<< "!parent.isValid()";
77 channel
= The::podcastCollection()->channels()[row
];
81 debug() << k_funcinfo
<< "parent.isValid()";
82 channel
= static_cast<PodcastChannel
*>(parent
.internalPointer());
83 if( !channel
.isNull() )
85 episode
= channel
->episodes()[row
];
89 debug() << k_funcinfo
<< "but channel == null";
94 if( !episode
.isNull() )
96 debug() << k_funcinfo
<< "create index for Episode: " << episode
->title();
97 debug() << k_funcinfo
<< "data: " << episode
.data();
98 return createIndex( row
, column
, episode
.data() );
100 else if( !channel
.isNull() )
102 debug() << k_funcinfo
<< "create index for Channel: " << channel
->title();
103 debug() << k_funcinfo
<< "data: " << channel
.data();
104 return createIndex( row
, column
, channel
.data() );
107 return QModelIndex();
111 PlaylistBrowserNS::PodcastModel::parent(const QModelIndex
& index
) const
114 debug() << k_funcinfo
<< " index: " << index
.row() << ":" << index
.column();
115 if (!index
.isValid())
117 debug() << k_funcinfo
<< "!index.isValid()";
118 return QModelIndex();
121 PodcastMetaCommon
*podcastMetaCommon
= static_cast<PodcastMetaCommon
*>(index
.internalPointer());
123 if ( typeid( * podcastMetaCommon
) == typeid( PodcastChannel
) )
125 debug() << k_funcinfo
<< "podcastType() == ChannelType";
126 return QModelIndex();
128 else if ( typeid( * podcastMetaCommon
) == typeid( PodcastEpisode
) )
130 PodcastEpisode
*episode
= static_cast<PodcastEpisode
*>( podcastMetaCommon
);
131 debug() << k_funcinfo
<< "podcastType() == EpisodeType";
132 return createIndex( The::podcastCollection()->channels().indexOf( episode
->channel() ),
133 0, episode
->channel().data() );
137 debug() << k_funcinfo
<< "podcastType() == ?";
138 return QModelIndex();
143 PlaylistBrowserNS::PodcastModel::rowCount(const QModelIndex
& parent
) const
147 debug() << k_funcinfo
<< " parent: " << parent
.row() << ":" << parent
.column();
148 if (parent
.column() > 0)
150 debug() << k_funcinfo
<< " parent.column() > 0";
154 if (!parent
.isValid())
156 debug() << k_funcinfo
<< " !parent.isValid()";
157 debug() << k_funcinfo
<< The::podcastCollection()->channels().count() << " channels";
158 return The::podcastCollection()->channels().count();
162 debug() << k_funcinfo
<< " parent.isValid() return episode count";
163 PodcastChannel
*channel
= static_cast<PodcastChannel
*>(parent
.internalPointer());
164 debug() << k_funcinfo
<< channel
->episodes().count() << " episodes";
165 return channel
->episodes().count();
171 PlaylistBrowserNS::PodcastModel::columnCount(const QModelIndex
& parent
) const
173 debug() << k_funcinfo
<< " parent: " << parent
.row() << ":" << parent
.column();
178 PlaylistBrowserNS::PodcastModel::flags(const QModelIndex
& index
) const
180 if (!index
.isValid())
183 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
187 PodcastModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
189 Q_UNUSED( orientation
)
192 debug() << k_funcinfo
<< "section = " << section
;
194 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
197 case 0: return QString("Type");
198 case 1: return QString("Title");
199 case 2: return QString("Summary");
200 default: return QString( "Section ") + QString::number( section
);
210 PodcastModel::slotUpdate()
213 //emit dataChanged( QModelIndex(), QModelIndex() );
214 emit
layoutChanged();
219 #include "PodcastModel.moc"