summaryrefslogtreecommitdiffstats
path: root/twitterview/twitter.cpp
blob: 5b2d5fb01865901b292087dab9038d35fe5a0f19 (plain)
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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include <QtWebKit>
#include <QtNetwork>
#include <QtGui>
#include <QtXmlPatterns>

#include "twitter.h"

const QUrl Twitter::LOGIN_URL("http://twitter.com/account/verify_credentials.xml");
const QUrl Twitter::GET_FRIENDS_URL("http://twitter.com/statuses/friends.xml");
const QUrl Twitter::GET_FOLLOWERS_URL("http://twitter.com/statuses/followers.xml");
const QUrl Twitter::GET_PUBLIC_STATUS_URL("http://twitter.com/statuses/public_timeline.xml");
const QUrl Twitter::GET_FRIENDS_STATUS_URL("http://twitter.com/statuses/friends_timeline.xml"); // same as /home
const QUrl Twitter::GET_MY_STATUS_URL("http://twitter.com/statuses/user_timeline.xml");
const QString Twitter::GET_USER_STATUS_URL("http://twitter.com/statuses/user_timeline/%1.xml");
const QUrl Twitter::UPDATE_STATUS_URL("http://twitter.com/statuses/update.xml");

Twitter::Twitter(QObject *parent)
    : QObject(parent)
{
    m_nam = new QNetworkAccessManager(this);
    connect(m_nam, SIGNAL(finished(QNetworkReply *)), this, SIGNAL(finished(QNetworkReply *)));
}

Twitter::~Twitter()
{
}

QString Twitter::userName() const
{
    return m_userName;
}

void Twitter::setUserName(const QString &userName)
{
    m_userName = userName;
}

QString Twitter::password() const
{
    return m_password;
}

void Twitter::setPassword(const QString &password)
{
    m_password = password;
}

void Twitter::applyCredentials(QNetworkRequest *request)
{
    QString userpass = QString("%1:%2").arg(m_userName).arg(m_password);
    QByteArray auth("Basic " + userpass.toUtf8().toBase64());
    request->setRawHeader("Authorization", auth);
}

QNetworkReply *Twitter::get(const QUrl &url, const QString &requestType)
{
    QNetworkRequest request(url);
    applyCredentials(&request);
    QNetworkReply *reply = m_nam->get(request);
    reply->setProperty("requestType", requestType);
    connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater()));
    return reply;
}

QByteArray Twitter::credentialsXml() const
{
    return m_credentialsXml;
}

void Twitter::verifyCredentials()
{
    connect(get(LOGIN_URL, "login"), SIGNAL(finished()), this, SLOT(handleLoginReply()));
}

void Twitter::handleLoginReply()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    m_credentialsXml = reply->readAll();
    QString error;
    if (!m_credentialsXml.isEmpty()) {
        QBuffer buffer(&m_credentialsXml);
        buffer.open(QIODevice::ReadOnly);

        QXmlQuery query;
        query.setFocus(&buffer);
        query.setQuery("data(/hash/error)");
        QString error;
        query.evaluateTo(&error);
    } else {
        error = reply->errorString();
    }
    emit credentialsVerified(reply->error() == QNetworkReply::NoError, error);
}

QNetworkReply *Twitter::getFriends()
{
    return get(GET_FRIENDS_URL, "getFriends");
}

QNetworkReply *Twitter::getFollowers()
{
    return get(GET_FOLLOWERS_URL, "getFollowers");
}

QNetworkReply *Twitter::getPublicStatus()
{
    return get(GET_PUBLIC_STATUS_URL, "getPublicStatus");
}

QNetworkReply *Twitter::getFriendsStatus()
{
    return get(GET_FRIENDS_STATUS_URL, "getFriendsStatus");
}

QNetworkReply *Twitter::getMyStatus()
{
    return get(GET_MY_STATUS_URL, "getMyStatus");
}

QNetworkReply *Twitter::getUserStatus(const QString &id)
{
    return get(QUrl(GET_USER_STATUS_URL.arg(id)), "getUserStatus_" + id);
}

QNetworkReply *Twitter::setStatus(const QString &status)
{
    QNetworkRequest request(UPDATE_STATUS_URL);
    applyCredentials(&request);
    QNetworkReply *reply = m_nam->post(request, QByteArray("status=" + QUrl::toPercentEncoding(status.left(140))));
    reply->setProperty("requestType", "setStatus");
    return reply;
}