forked from minimoog/qtwitdget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserinfo.cpp
More file actions
199 lines (172 loc) · 5.49 KB
/
userinfo.cpp
File metadata and controls
199 lines (172 loc) · 5.49 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 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 "userinfo.h"
#include "qtweetlib/qtweetusershow.h"
#include "qtweetlib/qtweetfriendshipcreate.h"
#include "qtweetlib/qtweetfriendshipdestroy.h"
#include "qtweetlib/qtweetfavoritescreate.h"
/**
* Constructor
*/
UserInfo::UserInfo(QObject *parent) :
QObject(parent)
{
}
/**
* Fetches twitter user information
* @param userid id of user to fetch information
*/
void UserInfo::fetch(qint64 userid)
{
QTweetUserShow* userShow = new QTweetUserShow(m_oauthTwitter, this);
userShow->fetch(userid);
connect(userShow, SIGNAL(parsedUserInfo(QTweetUser)), this, SLOT(finishedFetch(QTweetUser)));
}
/**
* Fetches twitter user information
* @param userid id of user to fetch information in string
*/
void UserInfo::fetch(const QString &userid)
{
bool ok;
qint64 id = userid.toLongLong(&ok);
if (ok) {
QTweetUserShow* userShow = new QTweetUserShow(m_oauthTwitter, this);
userShow->fetch(id);
connect(userShow, SIGNAL(parsedUserInfo(QTweetUser)), this, SLOT(finishedFetch(QTweetUser)));
}
}
/**
* Fetches twitter user information
* @param screenName screenname of the user to fetch information
*/
void UserInfo::fetchByName(const QString &screenName)
{
QTweetUserShow* userShow = new QTweetUserShow(m_oauthTwitter, this);
userShow->fetch(screenName);
connect(userShow, SIGNAL(parsedUserInfo(QTweetUser)), this, SLOT(finishedFetch(QTweetUser)));
}
/**
* Favorites tweet
* @param id id of tweet to be favorited in string
*/
// ### TODO: remove this
void UserInfo::createFavorite(const QString &id)
{
bool ok;
qint64 tweetid = id.toLongLong(&ok);
if (ok) {
QTweetFavoritesCreate* favorited = new QTweetFavoritesCreate(m_oauthTwitter, this);
favorited->create(tweetid);
connect(favorited, SIGNAL(parsedStatus(QTweetStatus)),
this, SLOT(finishedCreateFavorite(QTweetStatus)));
}
}
/**
* Called when fetching user information is finished
*/
void UserInfo::finishedFetch(const QTweetUser &userInfo)
{
QTweetUserShow* userShow = qobject_cast<QTweetUserShow*>(sender());
if (userShow) {
m_userinfo = userInfo;
emit userInfoChanged();
userShow->deleteLater();
//check if is in friends list
bool isInFriendsList = m_friends.contains(m_userinfo.id());
if (isInFriendsList != m_isFriend) {
m_isFriend = isInFriendsList;
emit isFriendChanged();
}
}
}
/**
* Connected to user stream, gives the friends list
*/
void UserInfo::onUserStreamFriendsList(const QList<qint64> friends)
{
m_friends = friends;
}
/**
* Follows user
* @param screenName user screenname to follow
*/
void UserInfo::followUser(const QString &screenName)
{
QTweetFriendshipCreate *createFriend = new QTweetFriendshipCreate(m_oauthTwitter, this);
createFriend->create(screenName);
connect(createFriend, SIGNAL(parsedUser(QTweetUser)), this, SLOT(finishedFollowUser(QTweetUser)));
}
/**
* Called when follow user is finished
*/
void UserInfo::finishedFollowUser(const QTweetUser &user)
{
QTweetFriendshipCreate *createFriend = qobject_cast<QTweetFriendshipCreate*>(sender());
// append followed user to friends list
if (createFriend) {
createFriend->deleteLater();
m_friends.append(user.id());
if (m_userinfo.id() == user.id()) {
if (!m_isFriend) {
m_isFriend = true;
emit isFriendChanged();
}
}
}
}
/**
* Unfollows the user
* @param screenName user screenname to unfollow
*/
void UserInfo::unfollowUser(const QString &screenName)
{
QTweetFriendshipDestroy *destroyFriend = new QTweetFriendshipDestroy(m_oauthTwitter, this);
destroyFriend->unfollow(screenName);
connect(destroyFriend, SIGNAL(parsedUser(QTweetUser)), this, SLOT(finishedUnfollowUser(QTweetUser)));
}
/**
* Called when unfollowin user is finisedh
*/
void UserInfo::finishedUnfollowUser(const QTweetUser &user)
{
QTweetFriendshipDestroy *destroyFriend = qobject_cast<QTweetFriendshipDestroy*>(sender());
if (destroyFriend) {
destroyFriend->deleteLater();
//remove from friend list
m_friends.removeOne(user.id());
//if it's the same user info changed the isFriend property
if (m_userinfo.id() == user.id()) {
if (m_isFriend) {
m_isFriend = false;
emit isFriendChanged();
}
}
}
}
// ### TODO: remove this
void UserInfo::finishedCreateFavorite(const QTweetStatus &status)
{
QTweetFavoritesCreate *favorited = qobject_cast<QTweetFavoritesCreate*>(sender());
if (favorited) {
favorited->deleteLater();
// ### TODO: Some kind of confirmation that tweet was favorited
}
}