/
wallmodel.cpp
175 lines (154 loc) · 4.28 KB
/
wallmodel.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "wallmodel.h"
#include <QApplication>
#include <roster.h>
#include <wallpost.h>
#include <QDateTime>
#include <QDebug>
#include <QNetworkReply>
WallModel::WallModel(QObject *parent) :
QAbstractListModel(parent)
{
auto roles = roleNames();
roles[IdRole] = "postId";
roles[BodyRole] = "body";
roles[FromRole] = "from";
roles[ToRole] = "to";
roles[DateRole] = "date";
roles[AttachmentsRole] = "attachments";
roles[LikesRole] = "likes";
roles[RepostsRole] = "reposts";
roles[CommentsRole] = "comments";
setRoleNames(roles);
}
vk::Contact *WallModel::contact() const
{
return m_contact.data();
}
void WallModel::setContact(vk::Contact *contact)
{
if (!m_session.isNull()) {
clear();
m_session.data()->deleteLater();
}
if (!contact)
return;
auto session = new vk::WallSession(contact);
connect(session, SIGNAL(postAdded(vk::WallPost)), SLOT(addPost(vk::WallPost)));
connect(session, SIGNAL(postLikeAdded(int,int,int,bool)), SLOT(onPostLikeAdded(int,int,int,bool)));
connect(session, SIGNAL(postLikeDeleted(int,int)), SLOT(onPostLikeDeleted(int,int)));
m_contact = contact;
m_session = session;
emit contactChanged(contact);
}
QVariant WallModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
auto post = m_posts.at(row);
switch (role) {
case IdRole:
return post.id();
case FromRole:
return qVariantFromValue(post.from());
case ToRole:
return qVariantFromValue(post.to());
case BodyRole:
return post.body();
case DateRole:
return post.date();
case AttachmentsRole:
return vk::Attachment::toVariantMap(post.attachments());
case LikesRole:
return post.property("likes");
case RepostsRole:
return post.property("reposts");
case CommentsRole:
return post.property("comments");
default:
break;
}
return QVariant::Invalid;
}
int WallModel::rowCount(const QModelIndex &) const
{
return count();
}
int WallModel::count() const
{
return m_posts.count();
}
void WallModel::getLastPosts(int count, vk::WallSession::Filter filter)
{
if (m_session.isNull())
qWarning("WallModel: contact is null! Please set a contact!");
else {
auto reply = m_session.data()->getPosts(filter, count, 0, false);
connect(reply, SIGNAL(resultReady(QVariant)), SIGNAL(requestFinished()));
}
}
void WallModel::addLike(int postId, bool retweet, const QString &message)
{
if (m_session.isNull())
qWarning("WallModel: contact is null! Please set a contact!");
else {
m_session.data()->addLike(postId, retweet, message);
}
}
void WallModel::deleteLike(int postId)
{
if (m_session.isNull())
qWarning("WallModel: contact is null! Please set a contact!");
else {
m_session.data()->deleteLike(postId);
}
}
void WallModel::clear()
{
beginRemoveRows(QModelIndex(), 0, m_posts.count());
m_posts.clear();
endRemoveRows();
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
int WallModel::findPost(int id)
{
for (int i = 0; i != m_posts.count(); i++) {
if (data(createIndex(i, 0), IdRole).toInt() == id)
return i;
}
return -1;
}
static bool postLessThan(const vk::WallPost &a, const vk::WallPost &b)
{
return a.id() < b.id();
}
void WallModel::addPost(const vk::WallPost &post)
{
if (findPost(post.id()) != -1)
return;
auto it = qLowerBound(m_posts.end(), m_posts.begin(), post , postLessThan);
auto last = it - m_posts.begin();
beginInsertRows(QModelIndex(), last, last);
m_posts.insert(it, post);
endInsertRows();
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
void WallModel::replacePost(int index, const vk::WallPost &post)
{
}
void WallModel::onPostLikeAdded(int postId, int likesCount, int repostsCount, bool isRetweeted)
{
int id = findPost(postId);
if (id != -1) {
vk::WallPost post = m_posts.at(id);
auto likes = post.likes();
likes.insert("count", likesCount);
post.setLikes(likes);
replacePost(id, post);
}
}
void WallModel::onPostLikeDeleted(int id, int likesCount)
{
}
vk::Roster *WallModel::roster() const
{
return m_contact.data()->client()->roster();
}