In the past two days, I am bored and have written a code poj code submitter,
Record the code, focusing on the use of qnetworkaccessmanager:
#ifndef MYCODE_H#define MYCODE_H#include <QWidget>#include <QNetworkAccessManager>#include <QNetworkReply>#include<QNetworkRequest>#include<QUrl>#include<string>using std::string;namespace Ui { class MyCode;}class MyCode : public QWidget{ Q_OBJECTpublic: explicit MyCode(QWidget *parent = 0); ~MyCode();private: QNetworkAccessManager *manager; QNetworkReply *rep; Ui::MyCode *ui; char cur_lang(); void init_set();public slots: void login(); void post(); void after_post(QNetworkReply*); void updateProgress(qint64,qint64); void deal_poj(QNetworkReply*); void deal_hdoj(QNetworkReply*); void deal_result(QNetworkReply*); void get_result();};#endif // MYCODE_H
#include "mycode.h"#include "ui_mycode.h"#include<QFile>#include<QTextStream>#include<string>#include<QIcon>MyCode::MyCode(QWidget *parent) : QWidget(parent),manager(new QNetworkAccessManager(this)), ui(new Ui::MyCode){ ui->setupUi(this); init_set(); connect(ui->loginPushButton,SIGNAL(clicked()),this,SLOT(login())); connect(ui->postButton,SIGNAL(clicked()),this,SLOT(post())); connect(ui->refreshButton,SIGNAL(clicked()),this,SLOT(get_result()));}MyCode::~MyCode(){ delete ui;}void MyCode::login(){ if(ui->judgeBox->currentText()==tr("POJ")) { connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(deal_poj(QNetworkReply*))); QString append=("user_id1="+ui->userEdit->text()+"&password1="+ui->passEdit->text()+"&B1=login&url=%2F"); rep=manager->post(QNetworkRequest(QUrl("http://poj.org/login")),append.toAscii()); connect(rep,SIGNAL(uploadProgress(qint64,qint64)),SLOT(updateProgress(qint64,qint64))); } else { }}void MyCode::init_set(){ this->setFixedSize(this->width(),this->height()); ui->userEdit->setFocus(); ui->userEdit->setMaxLength(16); ui->probEdit->setText("2013"); ui->passEdit->setEchoMode(QLineEdit::Password); ui->userEdit->setMaxLength(16); ui->progressBar->setFixedSize(this->width(),10); ui->progressBar->setValue(0); ui->userEdit->setText("627377770"); ui->passEdit->setText("aaabbbccc"); QIcon icon(":ioc.jpg"); this->setWindowIcon(icon); //ui->progressBar->hide();}void MyCode::post(){ if(ui->judgeBox->currentText()==tr("POJ")) { connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(after_post(QNetworkReply*))); QString append=("problem_id="+ui->probEdit->text()+"&language="+cur_lang()+"&source="+ui->codeEdit->toPlainText()+"&submit=Submit"); rep=manager->post(QNetworkRequest(QUrl("http://poj.org/submit")),append.toAscii()); connect(rep,SIGNAL(uploadProgress(qint64,qint64)),SLOT(updateProgress(qint64,qint64))); }}void MyCode::updateProgress(qint64 cur, qint64 total){ if(total!=0) ui->progressBar->setValue(cur/total*100);}void MyCode::deal_poj(QNetworkReply *reply){ updateProgress(0,100); if(reply->error()==QNetworkReply::NoError) { if(reply->error()==QNetworkReply::NoError) { ui->loginPushButton->setEnabled(false); ui->userEdit->setEnabled(false); ui->passEdit->setEnabled(false); } reply->deleteLater(); disconnect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(deal_poj(QNetworkReply*))); } else return;}//G++ GCC JAVA PASCAL C++ C FORTRANvoid MyCode::deal_hdoj(QNetworkReply *reply){ reply->deleteLater();}char MyCode::cur_lang(){ if(ui->judgeBox->currentText()=="POJ") { if(ui->langBox->currentText()=="G++") return '0'; else if(ui->langBox->currentText()=="GCC") return '1'; else if(ui->langBox->currentText()=="Java") return '2'; else if(ui->langBox->currentText()=="Pascal") return '3'; else if(ui->langBox->currentText()=="C++") return '4'; else if(ui->langBox->currentText()=="C") return '5'; else if(ui->langBox->currentText()=="Fortran") return '6'; } else if(ui->judgeBox->currentText()=="HDOJ") { }}void MyCode::after_post(QNetworkReply *reply){ updateProgress(0,100); if(reply->error()==QNetworkReply::NoError) { disconnect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(after_post(QNetworkReply*))); get_result(); }}void MyCode::get_result(){ if(ui->judgeBox->currentText()=="POJ") { connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(deal_result(QNetworkReply*))); rep=manager->get(QNetworkRequest(QUrl("http://poj.org/status"))); connect(rep,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateProgress(qint64,qint64))); } else { }}void MyCode::deal_result(QNetworkReply *reply){// QByteArray arr=reply->readAll();// QString string=QString::fromUtf8(arr);// ui->codeEdit->setText(arr); updateProgress(0,100); if(reply->error()==QNetworkReply::NoError) { if(ui->judgeBox->currentText()=="POJ") { QByteArray arr=reply->readAll(); QString data=QString::fromUtf8(arr); data.replace(' ','.'); data.replace('\n','.'); QFile file("/home/Focuson/Desktop/temp.txt"); file.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&file); out<<data; file.close(); int index=data.toStdString().find(ui->userEdit->text().toStdString()); index=data.toStdString().find(ui->userEdit->text().toStdString(),index+1); index=data.toStdString().find(ui->userEdit->text().toStdString(),index+1); index=data.toStdString().find(ui->userEdit->text().toStdString(),index+1); char result[9][30]={"Accepted", "Presentation.Error", "Time.Limit.Exceeded", "Memory.Limit.Exceeded", "Wrong.Answer", "Runtime.Error", "Output.Limit.Exceeded", "Compile.Error", "Compiling"}; int t[9]={INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX}; for(int i=0;i<9;i++) { int a=data.toStdString().find(result[i],index); if((size_t)a!=std::string::npos) { t[i]=a; } } int res=0,min=t[0]; for(int i=0;i<9;i++) { if(min>t[i])min=t[i],res=i; } //QString(result[res]).replace('.',' ').toStdString().c_str(); ui->statuLabel->setText(QString(result[res]).replace('.',' ').toStdString().c_str()); disconnect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(deal_result(QNetworkReply*))); } } reply->deleteLater();}