-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthorization.cpp
170 lines (143 loc) · 5.49 KB
/
authorization.cpp
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
/*
Copyright (c) 2013 by Ruslan Nazarov <818151@gmail.com>
***************************************************************************
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
*/
#include "authorization.h"
Authorization::Authorization()
{
connect(Client::instance()->connection(), &Connection::authorized, this, &Authorization::onAuthorized);
connect(Client::instance()->connection(), &Connection::logout, this, &Authorization::onLogout);
connect(Client::instance()->connection(), &Connection::error, this, &Authorization::onError);
connect(Client::instance()->connection(), &Connection::sessionChanged, this, &Authorization::saveSession);
connect(Client::instance()->connection(), &Connection::authorized, this, &Authorization::authorized);
connect(Client::instance()->connection(), &Connection::logout, this, &Authorization::logout);
connect(Client::instance()->connection(), &Connection::captcha, this, &Authorization::captcha);
connect(Client::instance()->connection(), &Connection::validation, this, &Authorization::validation);
connect(Client::instance()->connection(), &Connection::verification, this, &Authorization::verification);
}
void Authorization::connectToVk()
{
if (Settings::instance()->lastUid().isEmpty())
{
emit showAuthPage();
}
else
{
emit showMainPage();
QString uid = Settings::instance()->lastUid();
Settings::instance()->setCurrentUid(uid);
QString token = Settings::instance()->loadProfile("main/token", "none").toString();
QString secret = Settings::instance()->loadProfile("main/secret", "").toString();
Client::instance()->connection()->connectToVk(uid.toInt(), token, secret);
}
}
void Authorization::connectToVk(const QString &username, const QString &password)
{
Client::instance()->connection()->connectToVk(username, password);
}
void Authorization::disconnectVk()
{
Client::instance()->connection()->disconnectVk();
}
void Authorization::setCaptcha(const QString &sid, const QString &text)
{
Client::instance()->connection()->setCaptcha(sid, text);
}
void Authorization::cancelCaptcha()
{
Client::instance()->connection()->cancelCaptcha();
}
void Authorization::setVerificationCode(const QString &sid, const QString &code)
{
Client::instance()->connection()->setVerificationCode(sid, code);
}
void Authorization::checkUrlValidation(const QUrl &url)
{
if (url.fileName() == "blank.html")
{
QString fragment = url.fragment();
if (fragment.contains("success=1"))
{
SessionVars session;
QUrlQuery urlQuery = QUrlQuery(fragment);
session.access_token = urlQuery.queryItemValue("access_token");
session.user_id = urlQuery.queryItemValue("user_id").toInt();
session.expires_in = 0;
session.secret = fragment.contains("secret") ? urlQuery.queryItemValue("secret") : "";
Client::instance()->connection()->setSession(session);
emit showMainPage();
}
else if (fragment.contains("cancel=1"))
{
SessionVars session = Client::instance()->connection()->session();
Client::instance()->connection()->setSession(session);
emit showMainPage();
}
else
{
disconnectVk();
}
}
}
void Authorization::showValidation(const QString &validationUri)
{
emit validation(validationUri);
}
void Authorization::keepOnline(const bool on)
{
Client::instance()->keepOnline(on);
if (!on)
{
Client::instance()->setOffline();
}
}
void Authorization::onAuthorized(const int uid, const QString &token, const QString &secret)
{
saveSession(uid, token, secret);
emit showMainPage();
bool keepOnline = Settings::instance()->loadProfile("main/keepOnline", true).toBool();
Client::instance()->keepOnline(keepOnline);
}
void Authorization::saveSession(const int uid, const QString &token, const QString &secret)
{
Settings::instance()->setCurrentUid(QString::number(uid));
Settings::instance()->saveProfile("main/uid", uid);
Settings::instance()->saveProfile("main/token", token);
Settings::instance()->saveProfile("main/secret", secret);
Settings::instance()->saveMain("profiles/last", uid);
}
void Authorization::onLogout(const int uid)
{
Settings::instance()->saveProfile("main/token", "", QString::number(uid));
Settings::instance()->saveProfile("main/secret", "", QString::number(uid));
Settings::instance()->saveMain("profiles/last", "");
emit showAuthPage();
}
void Authorization::onError(const ErrorResponse::Error &error, const QString &text, const bool global, const bool fatal)
{
switch (error) {
case ErrorResponse::UserAuthorizationFailed:
case ErrorResponse::LoadTokenFailed:
{
//todo remove token and secret
emit showAuthPage();
break;
}
case ErrorResponse::UnknownErrorOccured:
{
emit incorrectCode();
break;
}
default:
{
break;
}
}
}