Skip to content

Commit

Permalink
refresh captcha by pressing button or clicking challenge image
Browse files Browse the repository at this point in the history
  • Loading branch information
siavash119 committed Dec 23, 2018
1 parent aa63852 commit a177c2b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
10 changes: 10 additions & 0 deletions captcha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ Captcha::~Captcha(){
disconnect(imageConnection);
}

void Captcha::cancel(){
disconnect(getConnection);
if(loading && replyChallenge){
replyChallenge->abort();
replyChallenge->deleteLater();
}
loading = false;
loaded = false;
}

void Captcha::getCaptcha(){
if(loading || loaded) return;
loading = true;
Expand Down
4 changes: 3 additions & 1 deletion captcha.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Captcha : public QObject
QTimer timer;
bool loaded = false;
bool loading = false;
void getCaptcha();
void getImage(QString challenge);
QString easyCaptcha(QString answer);

private:
QString loadingString = "Loading Captcha...";
QMap<QChar,QChar> ec;
Expand Down Expand Up @@ -64,6 +64,8 @@ private slots:
void fail();
void captchaCode(QString &code);
public slots:
void cancel();
void getCaptcha();
void loadCaptcha();
void loadImage();
};
Expand Down
28 changes: 26 additions & 2 deletions postform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ PostForm::PostForm(QWidget *parent) :
else {
ui->question->hide();
ui->challenge->hide();
ui->refreshCaptcha->hide();
captchaTimer = new QTimer(this);
captchaTimer->setSingleShot(true);
connect(captchaTimer,&QTimer::timeout,[=]{
ui->question->setText("Captcha code expired");
ui->question->show();
ui->refreshCaptcha->show();
ui->response->show();
captchaCode = "";
captcha.loaded = false;
Expand All @@ -52,13 +54,20 @@ PostForm::PostForm(QWidget *parent) :
setShortcuts();
connect(&captcha,&Captcha::questionInfo,this,&PostForm::loadCaptchaQuestion);
connect(&captcha,&Captcha::challengeInfo,this,&PostForm::loadCaptchaImage);
connect(ui->challenge,&ClickableLabel::clicked,&captcha,&Captcha::getCaptcha);
connect(ui->challenge,&ClickableLabel::clicked,[=]{
if(captchaTimer && captchaTimer->isActive()) captchaTimer->stop();
captcha.loading = false;
captcha.loaded = false;
captcha.getCaptcha();
ui->response->setFocus();
});
}

void PostForm::loadCaptchaQuestion(QString &challenge){
ui->question->show();
qDebug() << "setting captcha info/question:" << challenge;
ui->question->setText(challenge);
ui->refreshCaptcha->show();
ui->response->show();
}

Expand Down Expand Up @@ -93,6 +102,7 @@ void PostForm::usePass(bool use4chanPass){
else if(api->usesCaptcha()){
ui->captcha->show();
ui->challenge->hide();
ui->refreshCaptcha->hide();
}
}

Expand Down Expand Up @@ -137,6 +147,7 @@ void PostForm::verifyCaptcha(){
//possible segfault if threadtab/boardtab closes while waiting
captchaConnection = connect(captchaReply,&QNetworkReply::finished,[=]{
captchaReply->deleteLater();
if(captchaTimer && captchaTimer->isActive()) captchaTimer->stop();
if(captchaReply->error()){
ui->question->setText("Error: try again");
qDebug() << "Error:" << captchaReply->errorString();
Expand All @@ -155,8 +166,8 @@ void PostForm::verifyCaptcha(){
qDebug() << captchaCode;
ui->question->setText("Verified");
ui->challenge->hide();
ui->refreshCaptcha->hide();
ui->response->hide();
if(captchaTimer && captchaTimer->isActive()) captchaTimer->stop();
ui->submit->setFocus();
captchaTimer->start(120000);
}
Expand Down Expand Up @@ -319,6 +330,7 @@ void PostForm::postFinished()
ui->response->show();
ui->question->clear();
ui->question->hide();
ui->refreshCaptcha->hide();
}
QTimer::singleShot(1000, this,&PostForm::removeOverlay);
this->installEventFilter(this);
Expand Down Expand Up @@ -392,6 +404,11 @@ bool PostForm::eventFilter(QObject *obj, QEvent *event)
if(isPosting) {
cancelPost();
}
else if(captcha.loading){
captcha.cancel();
ui->question->setText("Captcha load canceled");
ui->response->show();
}
else {
hide();
}
Expand Down Expand Up @@ -523,3 +540,10 @@ void PostForm::on_response_returnPressed()
{
verifyCaptcha();
}

void PostForm::on_refreshCaptcha_clicked()
{
if(captchaTimer && captchaTimer->isActive()) captchaTimer->stop();
captcha.cancel();
captcha.getCaptcha();
}
2 changes: 2 additions & 0 deletions postform.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private slots:
void setShortcuts();
void on_response_returnPressed();

void on_refreshCaptcha_clicked();

protected:
bool eventFilter(QObject *obj, QEvent *event);
void resizeEvent(QResizeEvent *event);
Expand Down
56 changes: 45 additions & 11 deletions postform.ui
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<x>0</x>
<y>0</y>
<width>638</width>
<height>361</height>
<height>332</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -232,17 +232,50 @@
<number>1</number>
</property>
<item>
<widget class="QLabel" name="question">
<property name="text">
<string/>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
<layout class="QHBoxLayout" name="questionLayout" stretch="0,0,0">
<property name="spacing">
<number>20</number>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
<item>
<widget class="QLabel" name="question">
<property name="text">
<string/>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="refreshCaptcha">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>New Captcha</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="ClickableLabel" name="challenge">
Expand Down Expand Up @@ -280,6 +313,7 @@
</customwidgets>
<tabstops>
<tabstop>com</tabstop>
<tabstop>refreshCaptcha</tabstop>
<tabstop>response</tabstop>
<tabstop>submit</tabstop>
<tabstop>browse</tabstop>
Expand Down

0 comments on commit a177c2b

Please sign in to comment.