This repository was archived by the owner on Sep 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusertimelinelistmodel.cpp
More file actions
169 lines (145 loc) · 4.78 KB
/
usertimelinelistmodel.cpp
File metadata and controls
169 lines (145 loc) · 4.78 KB
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
/* Copyright (c) 2011, Antonie Jovanoski
*
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact e-mail: Antonie Jovanoski <minimoog77_at_gmail.com>
*/
#include "usertimelinelistmodel.h"
#include <QDateTime>
#include "qtweetlib/oauthtwitter.h"
#include "qtweetlib/qtweetstatus.h"
#include "qtweetlib/qtweetusertimeline.h"
static QString SinceTimeString(const QDateTime& from)
{
int passedSeconds = from.secsTo(QDateTime::currentDateTimeUtc());
if (passedSeconds < 0)
return QString("Time travel!");
if (passedSeconds < 60)
return QString("%1 seconds ago").arg(passedSeconds);
if (passedSeconds < 3600)
return QString("%1 minutes ago").arg(passedSeconds / 60);
if (passedSeconds < 86400)
return QString("%1 hours ago").arg(passedSeconds / 3600);
return QString("%1 days ago").arg(passedSeconds / 86400);
}
/**
* Constructor
*/
UserTimelineListModel::UserTimelineListModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles[StatusTextRole] = "statusTextRole";
roles[SinceTimeRole] = "sinceTimeRole";
roles[StatusIdRole] = "statusIdRole";
setRoleNames(roles);
}
/**
* Constructor
*/
UserTimelineListModel::UserTimelineListModel(OAuthTwitter *oauthTwitter, QObject *parent) :
QAbstractListModel(parent),
m_oauthTwitter(oauthTwitter)
{
QHash<int, QByteArray> roles;
roles[StatusTextRole] = "statusTextRole";
roles[SinceTimeRole] = "sinceTimeRole";
roles[StatusIdRole] = "statusIdRole";
setRoleNames(roles);
}
/**
* Sets oauth twitter object
*/
void UserTimelineListModel::setOAuthTwitter(OAuthTwitter *oauthTwitter)
{
m_oauthTwitter = oauthTwitter;
}
/**
* @reimp
*/
int UserTimelineListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_statuses.count();
}
/**
* @reimp
*/
QVariant UserTimelineListModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() > m_statuses.count())
return QVariant();
const QTweetStatus& st = m_statuses.at(index.row());
if (role == StatusTextRole)
return st.text();
else if (role == SinceTimeRole)
return SinceTimeString(st.createdAt());
else if (role == StatusIdRole)
return QString::number(st.id());
return QVariant();
}
/**
* Fetches user timeline
* @param userid id of the user to fetch the timeline
*/
void UserTimelineListModel::fetch(qint64 userid)
{
QTweetUserTimeline *userTimeline = new QTweetUserTimeline(m_oauthTwitter, this);
userTimeline->fetch(userid, QString(), 0, 0, 20);
connect(userTimeline, SIGNAL(parsedStatuses(QList<QTweetStatus>)),
this, SLOT(finishedFetching(QList<QTweetStatus>)));
connect(userTimeline, SIGNAL(error(ErrorCode,QString)),
this, SLOT(errorFetching()));
}
/**
* Fetches user timeline
* @param screenname screenname of the user to fetch the timeline
*/
void UserTimelineListModel::fetch(const QString &screenname)
{
QTweetUserTimeline *userTimeline = new QTweetUserTimeline(m_oauthTwitter, this);
userTimeline->fetch(0, screenname, 0, 0, 20);
connect(userTimeline, SIGNAL(parsedStatuses(QList<QTweetStatus>)),
this, SLOT(finishedFetching(QList<QTweetStatus>)));
connect(userTimeline, SIGNAL(error(ErrorCode,QString)),
this, SLOT(errorFetching()));
}
/**
* Called when fetching user timeline is finished
*/
void UserTimelineListModel::finishedFetching(const QList<QTweetStatus> &statuses)
{
QTweetUserTimeline *userTimeline = qobject_cast<QTweetUserTimeline*>(sender());
if (userTimeline) {
if (!m_statuses.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, m_statuses.count() - 1);
m_statuses.clear();
endRemoveRows();
}
if (!statuses.isEmpty()) {
beginInsertRows(QModelIndex(), 0, statuses.count() - 1);
m_statuses.append(statuses);
endInsertRows();
}
userTimeline->deleteLater();
}
}
void UserTimelineListModel::errorFetching()
{
QTweetUserTimeline *userTimeline = qobject_cast<QTweetUserTimeline*>(sender());
if (userTimeline)
userTimeline->deleteLater();
}