00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00023 #include "networkinput.h"
00024
00025 #include <QtGui/QHBoxLayout>
00026 #include <QtGui/QPixmap>
00027 #include <QtNetwork/QNetworkAccessManager>
00028 #include <QtNetwork/QNetworkReply>
00029 #include <QtGui/QMessageBox>
00030
00031
00032 NetworkInput::NetworkInput():QWidget(){
00033 QHBoxLayout *layout = new QHBoxLayout();
00034
00035 layout->setContentsMargins(0,0,0,0);
00036 edit = new QLineEdit();
00037 edit->setToolTip(tr("Adresse de l'agent réseau. http://toto.tata"));
00038 img = new QLabel();
00039 ready = false;
00040
00041 setOrange();
00042
00043 layout->addWidget(edit);
00044 layout->addWidget(img);
00045
00046 setLayout(layout);
00047 timer = new QTimer(this);
00048 QObject::connect(timer, SIGNAL(timeout()), this, SLOT(s_checkReady()));
00049 QObject::connect(edit, SIGNAL(editingFinished ()), this, SLOT(s_checkReady()));
00050 }
00051
00052
00053 QString NetworkInput::getUrl(){
00054 return edit->text();
00055 }
00056
00057 void NetworkInput::setUrl(QString u){
00058 edit->setText(u);
00059 s_checkReady();
00060 }
00061
00062 bool NetworkInput::isReady(){
00063 return ready;
00064 }
00065
00066 void NetworkInput::checkIfReady(){
00067
00068 }
00069
00070 void NetworkInput::s_checkReady(){
00071 QNetworkAccessManager * manager = new QNetworkAccessManager();
00072
00073 connect(manager, SIGNAL(finished(QNetworkReply*)),
00074 this, SLOT(s_onReceiveReady(QNetworkReply*)));
00075
00076 if (getUrl() != QString(""))
00077 manager->get(QNetworkRequest(QUrl(getUrl() + "/ready.php")));
00078 }
00079
00080 void NetworkInput::s_onReceiveReady(QNetworkReply *reply){
00081 QVariant statusCodeV =
00082 reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
00083
00084
00085 if (reply->error() == QNetworkReply::NoError)
00086 {
00087 QByteArray bytes = reply->readAll();
00088 QString string(bytes);
00089
00090 if (string == QString("ok")){
00091 setGreen();
00092 }else{
00093 setRed();
00094 emit message(QString(tr("Il n'y a pas d'agent réseau à cette adresse")));
00095 }
00096 }
00097
00098 else
00099 {
00100
00101 setRed();
00102 emit message(reply->errorString());
00103 }
00104
00105
00106 timer->start(5000);
00107 }
00108
00109 void NetworkInput::setGreen(){
00110 ready = true;
00111 QPixmap map(QString("data/images/ok.png"));
00112 img->setPixmap(map);
00113 img->setToolTip(tr("L'agent réseau est prêt"));
00114 }
00115
00116 void NetworkInput::setOrange(){
00117 ready = false;
00118 QPixmap map(QString("data/images/question.gif"));
00119 img->setPixmap(map);
00120 img->setToolTip(tr("L'agent réseau n'est pas encore reconnu"));
00121 }
00122
00123 void NetworkInput::setRed(){
00124 ready = false;
00125 QPixmap map(QString("data/images/fail.gif"));
00126 img->setPixmap(map);
00127 img->setToolTip(tr("L'agent réseau n'est pas disponible, accessible ou prêt"));
00128 }