forked from minimoog/qtwitdget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchqmllistmodel.cpp
More file actions
150 lines (126 loc) · 4.13 KB
/
searchqmllistmodel.cpp
File metadata and controls
150 lines (126 loc) · 4.13 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
/* Copyright (c) 2010, 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 "searchqmllistmodel.h"
#include <QDateTime>
#include "qtweetlib/oauthtwitter.h"
#include "qtweetlib/qtweetsearchresult.h"
#include "qtweetlib/qtweetsearch.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
*/
SearchQmlListModel::SearchQmlListModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles[SinceTimeRole] = "sinceTimeRole";
roles[ScreenNameRole] = "screenNameRole";
roles[AvatarUrlRole] = "avatarUrlRole";
roles[StatusTextRole] = "statusTextRole";
roles[StatusIdRole] = "statusIdRole";
setRoleNames(roles);
}
/**
* Constructor
*/
SearchQmlListModel::SearchQmlListModel(OAuthTwitter *oauthTwitter, QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles[SinceTimeRole] = "sinceTimeRole";
roles[ScreenNameRole] = "screenNameRole";
roles[AvatarUrlRole] = "avatarUrlRole";
roles[StatusTextRole] = "statusTextRole";
roles[StatusIdRole] = "statusIdRole";
setRoleNames(roles);
m_oauthTwitter = oauthTwitter;
}
/**
* Sets ouathtwitter object
*/
void SearchQmlListModel::setOAuthTwitter(OAuthTwitter *oauthTwitter)
{
m_oauthTwitter = oauthTwitter;
}
/**
* @reimp
*/
int SearchQmlListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_searchPageResult.results().count();
}
/**
* @reimp
*/
QVariant SearchQmlListModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() > m_searchPageResult.results().count()) // ### TODO
return QVariant();
const QTweetSearchResult& searchResult = m_searchPageResult.results().at(index.row());
if (role == SinceTimeRole)
return SinceTimeString(searchResult.createdAt());
else if (role == ScreenNameRole)
return searchResult.fromUser();
else if (role == AvatarUrlRole)
return searchResult.profileImageUrl();
else if (role == StatusTextRole)
return searchResult.text();
else if (role == StatusIdRole)
return QString::number(searchResult.id());
return QVariant();
}
/**
* Starts searching
* @param query search query
*/
void SearchQmlListModel::startSearch(const QString &query)
{
QTweetSearch *twitterSearch = new QTweetSearch(m_oauthTwitter, this);
twitterSearch->setAuthenticationEnabled(false);
twitterSearch->start(query);
connect(twitterSearch, SIGNAL(parsedPageResults(QTweetSearchPageResults)),
this, SLOT(finishedSearch(QTweetSearchPageResults)));
}
/**
* Called when twitter search is finished
*/
void SearchQmlListModel::finishedSearch(const QTweetSearchPageResults &pageResults)
{
QTweetSearch *twitterSearch = qobject_cast<QTweetSearch*>(sender());
if (twitterSearch) {
beginResetModel();
m_searchPageResult = pageResults;
endResetModel();
twitterSearch->deleteLater();
}
}