]> git.sur5r.net Git - minitube/blob - src/channelmodel.cpp
New upstream version 3.8
[minitube] / src / channelmodel.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "channelmodel.h"
22 #include "ytchannel.h"
23
24 static const int channelOffset = 2;
25
26 ChannelModel::ChannelModel(QObject *parent) :
27     QAbstractListModel(parent),
28     hoveredRow(-1) { }
29
30
31 int ChannelModel::rowCount(const QModelIndex &) const {
32     return channels.isEmpty() ? 0 : channelOffset + channels.size();
33 }
34
35 QVariant ChannelModel::data(const QModelIndex &index, int role) const {
36     switch (role) {
37
38     case ChannelModel::ItemTypeRole:
39         return typeForIndex(index);
40
41     case ChannelModel::DataObjectRole:
42         if (typeForIndex(index) == ChannelModel::ItemChannel)
43             return QVariant::fromValue(QPointer<YTChannel>(channelForIndex(index)));
44         break;
45
46     case ChannelModel::HoveredItemRole:
47         return hoveredRow == index.row();
48
49     case Qt::StatusTipRole:
50         if (typeForIndex(index) == ChannelModel::ItemChannel)
51             return channelForIndex(index)->getDescription();
52
53     }
54
55     return QVariant();
56 }
57
58 YTChannel* ChannelModel::channelForIndex(const QModelIndex &index) const {
59     const int row = index.row();
60     if (row < channelOffset) return 0;
61     return channels.at(index.row() - channelOffset);
62 }
63
64 ChannelModel::ItemTypes ChannelModel::typeForIndex(const QModelIndex &index) const {
65     switch (index.row()) {
66     case 0:
67         return ChannelModel::ItemAggregate;
68     case 1:
69         return ChannelModel::ItemUnwatched;
70     default:
71         return ChannelModel::ItemChannel;
72     }
73 }
74
75 void ChannelModel::setQuery(const QString &query, const QSqlDatabase &db) {
76     beginResetModel();
77     channels.clear();
78     sqlError = QSqlError();
79
80     QSqlQuery q(db);
81     q.prepare(query);
82     bool success = q.exec();
83     if (!success) {
84         qWarning() << q.lastQuery() << q.lastError().text();
85         sqlError = q.lastError();
86     }
87     channels.reserve(q.size());
88     while (q.next()) {
89         YTChannel *channel = YTChannel::forId(q.value(0).toString());
90         if (channel) {
91             connect(channel, SIGNAL(thumbnailLoaded()), SLOT(updateSender()), Qt::UniqueConnection);
92             connect(channel, SIGNAL(notifyCountChanged()), SLOT(updateSender()),
93                     Qt::UniqueConnection);
94             connect(channel, SIGNAL(destroyed(QObject *)), SLOT(removeChannel(QObject *)),
95                     Qt::UniqueConnection);
96             channels << channel;
97         }
98     }
99     channels.squeeze();
100     endResetModel();
101 }
102
103 QSqlError ChannelModel::lastError() const {
104     return sqlError;
105 }
106
107 void ChannelModel::updateSender() {
108     YTChannel *channel = static_cast<YTChannel*>(sender());
109     if (!channel) {
110         qWarning() << "Cannot get sender" << __PRETTY_FUNCTION__;
111         return;
112     }
113     updateChannel(channel);
114 }
115
116 void ChannelModel::updateChannel(YTChannel *channel) {
117     int row = channels.indexOf(channel);
118     if (row == -1) return;
119     row += channelOffset;
120     QModelIndex i = createIndex(row, 0);
121     emit dataChanged(i, i);
122 }
123
124 void ChannelModel::updateUnwatched() {
125     QModelIndex i = createIndex(1, 0);
126     emit dataChanged(i, i);
127 }
128
129 void ChannelModel::removeChannel(QObject *obj) {
130     YTChannel *channel = static_cast<YTChannel*>(obj);
131     // qWarning() << "channel" << channel << obj << obj->metaObject()->className();
132     if (!channel) return;
133
134     int row = channels.indexOf(channel);
135     if (row == -1) return;
136
137     int position = row + channelOffset;
138     beginRemoveRows(QModelIndex(), position, position+1);
139     channels.removeAt(row);
140     endRemoveRows();
141 }
142
143 void ChannelModel::setHoveredRow(int row) {
144     int oldRow = hoveredRow;
145     hoveredRow = row;
146     emit dataChanged( createIndex( oldRow, 0 ), createIndex( oldRow, 0 ) );
147     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );
148 }
149
150 void ChannelModel::clearHover() {
151     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );
152     hoveredRow = -1;
153 }