Skip to content

Commit

Permalink
minor cleanup of relogin form
Browse files Browse the repository at this point in the history
this doesn't yet fix the major issue we are facing with session
expiration, but it may help with the relogin
  • Loading branch information
benapetr committed Jul 19, 2024
1 parent 30e8c60 commit 5f621e1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/huggle_core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QtXml>
#include <QFile>
#include <QPluginLoader>
#include <QNetworkCookieJar>
#include <huggle_l10n/huggle_l10n.hpp>
#include "configuration.hpp"
#include "exception.hpp"
Expand Down Expand Up @@ -67,6 +68,7 @@ void Core::Init()
this->gc = new Huggle::GC();
GC::gc = this->gc;
Query::NetworkManager = new QNetworkAccessManager();
Query::NetworkManager->setCookieJar(new QNetworkCookieJar(Query::NetworkManager));
QueryPool::HugglePool = new QueryPool();
this->HGQP = QueryPool::HugglePool;
this->HuggleSyslog = Syslog::HuggleLogs;
Expand Down
2 changes: 0 additions & 2 deletions src/huggle_core/editquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ bool EditQuery::IsProcessed()
{
reason = "Not logged in";
hec = HUGGLE_ENOTLOGGEDIN;
// this is some fine hacking here :)
// we use this later in main form
HUGGLE_DEBUG1("Session expired requesting a new login");
this->Suspend();
Configuration::Logout(this->Page->GetSite());
Expand Down
52 changes: 28 additions & 24 deletions src/huggle_ui/reloginform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "reloginform.hpp"
#include "mainwindow.hpp"
#include <QNetworkAccessManager>
#include <QNetworkCookieJar>
#include <huggle_core/apiquery.hpp>
#include <huggle_core/apiqueryresult.hpp>
#include <huggle_core/core.hpp>
Expand All @@ -29,46 +31,51 @@ ReloginForm::ReloginForm(WikiSite *site, QWidget *parent) : QDialog(parent), ui(
{
this->ui->setupUi(this);
this->loginSite = site;
this->little_cute_timer = new QTimer();
this->reloginTimer = new QTimer();
if (hcfg->SystemConfig_StorePassword)
this->ui->lineEdit->setText(hcfg->SystemConfig_RememberedPassword);
this->ui->checkBox->setChecked(hcfg->SystemConfig_Autorelog);
this->ui->checkBox_RemeberPw->setChecked(hcfg->SystemConfig_StorePassword);
if (!hcfg->SystemConfig_StorePassword)
this->ui->checkBox->setEnabled(false);
this->Localize();
connect(this->little_cute_timer, SIGNAL(timeout()), this, SLOT(LittleTick()));
connect(this->reloginTimer, SIGNAL(timeout()), this, SLOT(ReloginTick()));
if (hcfg->SystemConfig_Autorelog && hcfg->SystemConfig_StorePassword)
this->ui->pushButton_2->click();
this->ui->pushButton_Relog->click();
}

ReloginForm::~ReloginForm()
{
delete this->little_cute_timer;
delete this->reloginTimer;
delete this->ui;
}

void Huggle::ReloginForm::on_pushButton_clicked()
void Huggle::ReloginForm::on_pushButton_ForceExit_clicked()
{
program_terminated = true;
MainWindow::HuggleMain->ShutdownForm();
Core::HuggleCore->Shutdown();
}

void Huggle::ReloginForm::on_pushButton_2_clicked()
void Huggle::ReloginForm::on_pushButton_Relog_clicked()
{
// Reinitialize network manager in order to rid of all cookies, session data or cached info
delete Query::NetworkManager;
Query::NetworkManager = new QNetworkAccessManager();
Query::NetworkManager->setCookieJar(new QNetworkCookieJar(Query::NetworkManager));

hcfg->SystemConfig_StorePassword = this->ui->checkBox_RemeberPw->isChecked();
this->ui->pushButton_2->setEnabled(false);
this->ui->pushButton_Relog->setEnabled(false);
this->ui->lineEdit->setEnabled(false);
this->qReloginTokenReq = new ApiQuery(ActionLogin, this->loginSite);
this->little_cute_timer->start(HUGGLE_TIMER);
this->reloginTimer->start(HUGGLE_TIMER);
this->qReloginTokenReq->Parameters = "lgname=" + QUrl::toPercentEncoding(Configuration::HuggleConfiguration->SystemConfig_UserName);
this->qReloginTokenReq->HiddenQuery = true;
this->qReloginTokenReq->UsingPOST = true;
this->qReloginTokenReq->Process();
}

void ReloginForm::LittleTick()
void ReloginForm::ReloginTick()
{
if (this->qReloginPw != nullptr)
{
Expand Down Expand Up @@ -100,7 +107,7 @@ void ReloginForm::LittleTick()
this->loginSite->ProjectConfig->IsLoggedIn = true;
this->loginSite->ProjectConfig->RequestingLogin = false;
this->close();
this->little_cute_timer->stop();
this->reloginTimer->stop();
WikiUtil::RetrieveTokens(this->loginSite);
this->qReloginPw.Delete();
return;
Expand All @@ -121,60 +128,57 @@ void ReloginForm::LittleTick()
return;
}
this->Fail(_l("login-api", Result));
return;
}
if (this->qReloginTokenReq != nullptr && this->qReloginTokenReq->IsProcessed())
} else if (this->qReloginTokenReq != nullptr && this->qReloginTokenReq->IsProcessed())
{
if (this->qReloginTokenReq->IsFailed())
{
this->Fail(this->qReloginTokenReq->GetFailureReason());
return;
}
ApiQueryResultNode *login_ = this->qReloginTokenReq->GetApiQueryResult()->GetNode("login");
if (login_ == nullptr)
ApiQueryResultNode *query_result = this->qReloginTokenReq->GetApiQueryResult()->GetNode("login");
if (query_result == nullptr)
{
this->Fail("No data returned by login query");
HUGGLE_DEBUG1(this->qReloginTokenReq->Result->Data);
return;
}
if (!login_->Attributes.contains("result"))
if (!query_result->Attributes.contains("result"))
{
this->Fail("No result was provided by login query");
HUGGLE_DEBUG1(this->qReloginTokenReq->Result->Data);
return;
}
QString reslt_ = login_->GetAttribute("result");
if (reslt_ != "NeedToken")
QString result_code = query_result->GetAttribute("result");
if (result_code != "NeedToken")
{
this->Fail("Result returned " + reslt_ + " NeedToken expected");
this->Fail("Result returned " + result_code + " NeedToken expected");
HUGGLE_DEBUG1(this->qReloginTokenReq->Result->Data);
return;
}
if (!login_->Attributes.contains("token"))
if (!query_result->Attributes.contains("token"))
{
this->Fail(_l("no-token"));
HUGGLE_DEBUG1(this->qReloginTokenReq->Result->Data);
return;
}
QString token = login_->GetAttribute("token");
QString token = query_result->GetAttribute("token");
this->qReloginPw = new ApiQuery(ActionLogin, this->loginSite);
this->qReloginPw->HiddenQuery = true;
this->qReloginPw->Parameters = "lgname=" + QUrl::toPercentEncoding(Configuration::HuggleConfiguration->SystemConfig_UserName)
+ "&lgpassword=" + QUrl::toPercentEncoding(this->ui->lineEdit->text()) + "&lgtoken=" + QUrl::toPercentEncoding(token);
this->qReloginPw->UsingPOST = true;
this->qReloginPw->Process();
return;
}
}

void ReloginForm::Fail(QString why)
{
this->little_cute_timer->stop();
this->reloginTimer->stop();
this->qReloginTokenReq.Delete();
this->ui->lineEdit->setEnabled(true);
this->qReloginPw.Delete();
UiGeneric::MessageBox(_l("fail"), _l("relogin-fail", why), MessageBoxStyleWarning, true);
this->ui->pushButton_2->setEnabled(true);
this->ui->pushButton_Relog->setEnabled(true);
}

void ReloginForm::Localize()
Expand Down
11 changes: 5 additions & 6 deletions src/huggle_ui/reloginform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Ui
namespace Huggle
{
class ApiQuery;
//! Relogin form used to login back to mediawiki when session is removed
//! Relogin form used to login back into mediawiki when session is no longer valid
class HUGGLE_EX_UI ReloginForm : public QDialog
{
Q_OBJECT
Expand All @@ -37,9 +37,9 @@ namespace Huggle
~ReloginForm();

private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void LittleTick();
void on_pushButton_ForceExit_clicked();
void on_pushButton_Relog_clicked();
void ReloginTick();

void on_checkBox_RemeberPw_toggled(bool checked);

Expand All @@ -48,8 +48,7 @@ namespace Huggle
void Localize();
void reject();
WikiSite *loginSite;
//! This is just a timer, it's called little and cute because I was bored when writing this piece of code
QTimer *little_cute_timer;
QTimer *reloginTimer;
Collectable_SmartPtr<ApiQuery> qReloginTokenReq;
Collectable_SmartPtr<ApiQuery> qReloginPw;
Ui::ReloginForm *ui;
Expand Down
4 changes: 2 additions & 2 deletions src/huggle_ui/reloginform.ui
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="pushButton_ForceExit">
<property name="text">
<string>Exit huggle without saving your configs</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<widget class="QPushButton" name="pushButton_Relog">
<property name="text">
<string>Relog</string>
</property>
Expand Down

0 comments on commit 5f621e1

Please sign in to comment.