forked from owncloud-archive/News-Qt-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedsmodel.cpp
150 lines (125 loc) · 3.63 KB
/
feedsmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "feedsmodel.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include "json.h"
FeedsModel::FeedsModel(QObject *parent) :
QAbstractListModel(parent)
{
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
setRoleNames(roleNames());
#endif
}
QHash<int, QByteArray> FeedsModel::roleNames() const
{
QHash<int, QByteArray> names;
names[FeedId] = "feedid";
names[FeedTitle] = "feedtitle";
names[FeedURL] = "feedurl";
names[FeedIcon] = "feedicon";
return names;
}
QList<int> FeedsModel::feedIds()
{
QList<int> ids;
foreach(QVariantMap feed, m_feeds) {
ids << feed["id"].toInt();
}
return ids;
}
void FeedsModel::addFeed(int id, const QString &title, const QString &url, const QString &icon)
{
if (m_db->isOpen()) {
QSqlQuery qry;
qry.prepare("INSERT OR REPLACE INTO feeds(id, title, url, icon) VALUES(:id, :title, :url, :icon)");
qry.bindValue(":id", id);
qry.bindValue(":title", title);
qry.bindValue(":url", url);
qry.bindValue(":icon", icon);
bool ret = qry.exec();
if(!ret)
qDebug() << qry.lastError();
else {
qDebug() << "feed inserted!";
QVariantMap feed;
feed["id"] = id;
feed["title"] = title;
feed["url"] = url;
feed["icon"] = icon;
m_feeds << feed;
}
}
}
void FeedsModel::loadData()
{
if (m_db->isOpen()) {
qDebug() << "Loading feed data";
QSqlQuery qry;
qry.prepare("SELECT id, title, url, icon FROM feeds");
bool ret = qry.exec();
if(!ret) {
qDebug() << qry.lastError();
} else {
beginResetModel();
while (qry.next()) {
QVariantMap feed;
feed["id"] = qry.value(0).toInt();
feed["title"] = qry.value(1).toString();
feed["url"] = qry.value(2).toString();
feed["icon"] = qry.value(3).toString();
m_feeds << feed;
}
endResetModel();
}
}
}
QVariant FeedsModel::data(const QModelIndex &index, int role) const
{
if (index.row() < m_feeds.count()) {
if (role == FeedId) {
return m_feeds.at(index.row())["id"];
} else if (role == FeedTitle) {
return m_feeds.at(index.row())["title"];
} else if (role == FeedURL) {
return m_feeds.at(index.row())["url"];
} else if (role == FeedIcon) {
return m_feeds.at(index.row())["icon"];
}
}
return QVariant();
}
int FeedsModel::rowCount(const QModelIndex &parent) const
{
return m_feeds.count();
}
void FeedsModel::parseFeeds(const QByteArray &json)
{
bool ok;
QVariant data = QtJson::parse(json, ok);
qDebug() << json;
QList<QVariant> feeds = data.toMap()["feeds"].toList();
qDebug() << "Feed Count" << feeds.length();
beginResetModel();
m_feeds.clear();
foreach(QVariant feed, feeds) {
QVariantMap map = feed.toMap();
addFeed(map["id"].toInt(), map["title"].toString(), map["url"].toString(), map["faviconLink"].toString());
}
endResetModel();
}
void FeedsModel::setDatabase(QSqlDatabase *db)
{
m_db = db;
if (m_db->isOpen()) {
QSqlQuery qry;
qry.prepare( "CREATE TABLE IF NOT EXISTS feeds (id INTEGER UNIQUE PRIMARY KEY, title VARCHAR(1024), url VARCHAR(2048), icon VARCHAR(2048))" );
bool ret = qry.exec();
if(!ret) {
qDebug() << qry.lastError();
} else {
qDebug() << "Feed table created!";
}
loadData();
}
}