]> git.sur5r.net Git - minitube/blob - src/channelmodel.cpp
2aa6c43d351454cd05af4a9739f0960e9f698c2b
[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 "ytuser.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<YTUser>(userForIndex(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 userForIndex(index)->getDescription();
52
53     }
54
55     return QVariant();
56 }
57
58 YTUser* ChannelModel::userForIndex(const QModelIndex &index) const {
59     return channels.at(index.row() - channelOffset);
60 }
61
62 ChannelModel::ItemTypes ChannelModel::typeForIndex(const QModelIndex &index) const {
63     switch (index.row()) {
64     case 0:
65         return ChannelModel::ItemAggregate;
66     case 1:
67         return ChannelModel::ItemUnwatched;
68     default:
69         return ChannelModel::ItemChannel;
70     }
71 }
72
73 void ChannelModel::setQuery(const QString &query, const QSqlDatabase &db) {
74     channels.clear();
75     sqlError = QSqlError();
76
77     QSqlQuery q(db);
78     q.prepare(query);
79     bool success = q.exec();
80     if (!success) {
81         qWarning() << q.lastQuery() << q.lastError().text();
82         sqlError = q.lastError();
83     }
84     while (q.next()) {
85         YTUser *user = YTUser::forId(q.value(0).toString());
86         connect(user, SIGNAL(thumbnailLoaded()), SLOT(updateSender()), Qt::UniqueConnection);
87         channels << user;
88     }
89
90     reset();
91 }
92
93 QSqlError ChannelModel::lastError() const {
94     return sqlError;
95 }
96
97 void ChannelModel::updateSender() {
98     YTUser *user = static_cast<YTUser*>(sender());
99     if (!user) {
100         qWarning() << "Cannot get sender" << __PRETTY_FUNCTION__;
101         return;
102     }
103     updateChannel(user);
104 }
105
106 void ChannelModel::updateChannel(YTUser *user) {
107     int row = channels.indexOf(user);
108     if (row == -1) return;
109     row += channelOffset;
110     QModelIndex i = createIndex(row, 0);
111     emit dataChanged(i, i);
112 }
113
114 void ChannelModel::updateUnwatched() {
115     QModelIndex i = createIndex(1, 0);
116     emit dataChanged(i, i);
117 }
118
119 void ChannelModel::setHoveredRow(int row) {
120     int oldRow = hoveredRow;
121     hoveredRow = row;
122     emit dataChanged( createIndex( oldRow, 0 ), createIndex( oldRow, 0 ) );
123     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );
124 }
125
126 void ChannelModel::clearHover() {
127     emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, 0 ) );
128     hoveredRow = -1;
129 }