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;
74 return !Token_.isEmpty () &&
75 (!ValidFor_ || ReceivedAt_.secsTo (QDateTime::currentDateTime ()) < ValidFor_);
80 return !Token_.isEmpty () || !Cookies_->allCookies ().isEmpty ();
103 for (
const auto& queue : PrioManagedQueues_)
105 for (
const auto& queue : ManagedQueues_)
112 InvokeQueues (Token_);
119 iface.reportStarted ();
131 return iface.future ();
139 <<
"cannot manage request queue if queue manager wasn't set";
143 ManagedQueues_ << queue;
153 <<
"cannot manage request queue if queue manager wasn't set";
157 PrioManagedQueues_ << queue;
169 ScheduleTrack (
token);
171 for (
auto queue : PrioManagedQueues_)
174 const auto& pair = queue->takeFirst ();
175 const auto& f = pair.first;
179 for (
auto queue : ManagedQueues_)
182 const auto& f = queue->takeFirst ();
187 void VkAuthManager::RequestURL (
const QUrl&
url)
192 &QNetworkReply::finished,
197 void VkAuthManager::RequestAuthKey ()
199 if (IsRequestScheduled_ && ScheduleTimer_->isActive ())
200 ScheduleTimer_->stop ();
206 IsRequesting_ =
true;
218 ReceivedAt_ = QDateTime::currentDateTime ();
220 IsRequesting_ =
false;
222 InvokeQueues (Token_);
229 bool VkAuthManager::CheckError (
const QUrl&
url)
231 if (
url.path () !=
"/error"_ql)
236 IsRequesting_ =
false;
250 tr (
"VK.com authentication for %1 failed because of error %2. "
251 "Report upstream please.")
260 void VkAuthManager::ScheduleTrack (
const QString&
key)
271 Util::UrlOperator {
url }
276 &QNetworkReply::finished,
278 &QNetworkReply::deleteLater);
292 class CloseEventFilter :
public QObject
300 handlee->installEventFilter (
this);
303 bool eventFilter (QObject*,
QEvent *
event)
override
305 if (
event->type () == QEvent::Close)
318 tr (
"Could not authenticate %1 since authentication requires a browser plugin. "
319 "Consider installing one like Poshuku.")
329 viewWidget->setWindowTitle (
tr (
"VK.com authentication for %1")
339 SLOT (handleUrlChanged (
const QUrl&)));
344 void VkAuthManager::HandleGotForm (QNetworkReply *
reply)
346 reply->deleteLater ();
348 if (
reply->error () != QNetworkReply::NoError &&
349 reply->error () != QNetworkReply::AuthenticationRequiredError)
353 <<
reply->errorString ();
355 IsRequesting_ =
false;
357 if (!IsRequestScheduled_)
359 IsRequestScheduled_ =
true;
360 ScheduleTimer_->start (30000);
366 const auto&
location =
reply->header (QNetworkRequest::LocationHeader).toUrl ();
379 void VkAuthManager::handleUrlChanged (
const QUrl&
url)
381 if (!CheckReply (
url))
385 sender ()->deleteLater ();
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
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.