int main(int argc, char *argv[]){ QApplication a(argc, argv); Login login; Controller controller; FriendsView friendsView; login.show(); QObject::connect(&login, SIGNAL(connected(QString)), &controller, SLOT(sendRequest(const QString&))); QObject::connect(&controller, SIGNAL(parseDone(QLinkedList<FriendInfo*>)), &friendsView, SLOT(getFrindsList(const QLinkedList<FriendInfo*>&))); return a.exec();}
#ifndef LOGIN_H#define LOGIN_H#include <QWebView>class Login : public QWebView{ Q_OBJECTprivate: QString appId; QString display; QString scope; QString redirectUri; QString responseType; void initialize(); void createUrl();private slots: void urlChanged(QUrl);signals: void connected(QString);public: Login(QWidget *parent = 0);};#endif // LOGINWDG_H
#include <QDebug>#include "Login.h"Login::Login(QWidget *parent) : QWebView(parent){ initialize(); createUrl(); connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(urlChanged(QUrl)));}void Login::initialize() { appId = "********"; display = "popup"; scope = "friends"; redirectUri = "http://api.vkontakte.ru/blank.html"; responseType = "token";}void Login::createUrl() { QUrl url("http://api.vkontakte.ru/oauth/authorize"); url.addQueryItem("client_id", appId); url.addQueryItem("display", display); url.addQueryItem("scope", scope); url.addQueryItem("redirect_uri", redirectUri); url.addQueryItem("response_type", responseType); load(url);}void Login::urlChanged(QUrl url) { url = url.toString().replace("#","?"); if ( url.queryItemValue("error_description") == "User denied your request" ) close(); if ( !url.hasQueryItem("access_token") ) return; QString token = url.queryItemValue("access_token"); qDebug() << "token sended"; emit connected(token);}
#ifndef CONTROLLER_H#define CONTROLLER_H#include <QThread>#include <QNetworkAccessManager>#include <QNetworkRequest>#include <QNetworkReply>#include <QLinkedList>#include "FriendInfo.h"class Controller : public QThread { Q_OBJECTprivate: QString token; QNetworkAccessManager *manager; QLinkedList<FriendInfo*> friendsList; void parseReply(const QByteArray& reply);private slots: void requestFinished(QNetworkReply* reply);protected: virtual void run();public: Controller(QObject * parent = 0);public slots: void sendRequest(const QString&);signals: void parseDone(QLinkedList<FriendInfo*>);};#endif // CONTROLLER_H
#include <QtXml>#include "Controller.h"Controller::Controller(QObject * parent) : QThread(parent) {}void Controller::run() { manager = new QNetworkAccessManager(this); manager->moveToThread(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*))); QNetworkRequest request; QUrl url; url.setUrl("https://api.vkontakte.ru/method/friends.get.xml"); url.addQueryItem("fields", "online, photo"); url.addQueryItem("access_token", token); request.setUrl(url); manager->get(request); exec();}void Controller::sendRequest(const QString& token) { this->token = token; moveToThread(this); qDebug() << "token added"; this->start();}void Controller::requestFinished(QNetworkReply* reply) { parseReply(reply->readAll());}void Controller::parseReply(const QByteArray &reply) { QDomDocument domDoc; domDoc.setContent(reply); QDomElement repsonse = domDoc.firstChildElement(); QDomNode user = repsonse.firstChildElement(); for ( ; !user.isNull(); ) { QDomElement field = user.firstChildElement("first_name"); QString firstName = field.text(); field = field.nextSiblingElement("last_name"); QString lastName = field.text(); field = field.nextSiblingElement("online"); int online = field.text().toInt(); field = field.nextSiblingElement("photo"); QUrl photoUrl = field.text(); if ( online ) { friendsList.prepend(new FriendInfo(firstName, lastName, online, photoUrl)); } else { friendsList.append(new FriendInfo(firstName, lastName, online, photoUrl)); } user = user.nextSibling(); } emit parseDone(friendsList);}