10#include <QNetworkRequest>
11#include <QNetworkReply>
12#include <QNetworkCookie>
17#include <QFutureInterface>
28#include <xmlsettingsdialog/basesettingsmanager.h>
36 auto url = QUrl::fromEncoded (
"https://oauth.vk.com/authorize?redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&state=");
58 AuthNAM_->setCookieJar (Cookies_);
61 ScheduleTimer_->setSingleShot (
true);
67 IsRequestScheduled_ =
false;
76 return !Token_.isEmpty () &&
77 (!ValidFor_ || ReceivedAt_.secsTo (QDateTime::currentDateTime ()) < ValidFor_);
82 return !Token_.isEmpty () || !Cookies_->allCookies ().isEmpty ();
105 for (
const auto& queue : PrioManagedQueues_)
107 for (
const auto& queue : ManagedQueues_)
114 InvokeQueues (Token_);
121 iface.reportStarted ();
133 return iface.future ();
141 <<
"cannot manage request queue if queue manager wasn't set";
145 ManagedQueues_ << queue;
155 <<
"cannot manage request queue if queue manager wasn't set";
159 PrioManagedQueues_ << queue;
171 ScheduleTrack (
token);
173 for (
auto queue : PrioManagedQueues_)
176 const auto& pair = queue->takeFirst ();
177 const auto& f = pair.first;
181 for (
auto queue : ManagedQueues_)
184 const auto& f = queue->takeFirst ();
189 void VkAuthManager::RequestURL (
const QUrl&
url)
194 &QNetworkReply::finished,
199 void VkAuthManager::RequestAuthKey ()
201 if (IsRequestScheduled_ && ScheduleTimer_->isActive ())
202 ScheduleTimer_->stop ();
208 IsRequesting_ =
true;
220 ReceivedAt_ = QDateTime::currentDateTime ();
222 IsRequesting_ =
false;
224 InvokeQueues (Token_);
231 bool VkAuthManager::CheckError (
const QUrl&
url)
233 if (
url.path () !=
"/error"_ql)
238 IsRequesting_ =
false;
252 tr (
"VK.com authentication for %1 failed because of error %2. "
253 "Report upstream please.")
262 void VkAuthManager::ScheduleTrack (
const QString&
key)
273 Util::UrlOperator {
url }
278 &QNetworkReply::finished,
280 &QNetworkReply::deleteLater);
294 class CloseEventFilter :
public QObject
302 handlee->installEventFilter (
this);
305 bool eventFilter (QObject*,
QEvent *
event)
override
307 if (
event->type () == QEvent::Close)
320 tr (
"Could not authenticate %1 since authentication requires a browser plugin. "
321 "Consider installing one like Poshuku.")
330 Browser_ =
browsers.value (0)->CreateWidget ();
332 viewWidget->setWindowTitle (
tr (
"VK.com authentication for %1")
338 Browser_->
Load (URL_);
342 SLOT (handleUrlChanged (
const QUrl&)));
347 void VkAuthManager::HandleGotForm (QNetworkReply *
reply)
349 reply->deleteLater ();
351 if (
reply->error () != QNetworkReply::NoError &&
352 reply->error () != QNetworkReply::AuthenticationRequiredError)
356 <<
reply->errorString ();
358 IsRequesting_ =
false;
360 if (!IsRequestScheduled_)
362 IsRequestScheduled_ =
true;
363 ScheduleTimer_->start (30000);
369 const auto&
location =
reply->header (QNetworkRequest::LocationHeader).toUrl ();
382 void VkAuthManager::handleUrlChanged (
const QUrl&
url)
384 if (!CheckReply (
url))
virtual LC::Util::BaseSettingsManager * GetSettingsManager() const =0
Returns the LeechCraft's settings manager.
virtual IPluginsManager * GetPluginsManager() const =0
Returns the application's plugin manager.
virtual IEntityManager * GetEntityManager() const =0
Returns the entity manager object.
virtual bool HandleEntity(LC::Entity entity, QObject *desired=nullptr)=0
Handles the given entity.
QList< T > GetAllCastableTo() const
Similar to GetAlLCastableRoots() and provided for convenience.
Base class for plugins that provide a web browser.
A customized cookie jar with additional features.
void Load(const QByteArray &data)
A simple scheduling manager for a queue of functors.
void Schedule(std::function< void()> functor, QObject *dependent=nullptr, QueuePriority prio=QueuePriority::Normal)
Adds the given functor.
ScheduleGuard_t ManageQueue(RequestQueue_ptr)
bool HadAuthentication() const
~VkAuthManager() override
QFuture< AuthKeyResult_t > GetAuthKeyFuture()
bool IsAuthenticated() const
void UpdateScope(const QStringList &)
std::variant< SilentMode > AuthKeyError_t
void cookiesChanged(const QByteArray &)
void gotAuthKey(const QString &)
VkAuthManager(const QString &accountName, const QString &clientId, const QStringList &scope, const QByteArray &cookies, const ICoreProxy_ptr &, QueueManager *=nullptr, QObject *=nullptr)
Manipulates query part of an QUrl object.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Container< T > Filter(const Container< T > &c, F f)
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.