C:/sources/c++/buddhabrot/buddhabrot/networkinput.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2010 Pierre SCHWARTZ
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
00005 documentation files (the "Software"), to deal in the Software without restriction, including without 
00006 limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
00007 of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
00008 conditions: The above copyright notice and this permission notice shall be included in all copies or 
00009 substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00010 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00011 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00012 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 
00013 OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
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     // no error received?
00085     if (reply->error() == QNetworkReply::NoError)
00086     { 
00087         QByteArray bytes = reply->readAll();  // bytes
00088         QString string(bytes); // string
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     // Some http error received
00098     else
00099     {           
00100         // handle errors here
00101                 setRed();
00102                 emit message(reply->errorString());             
00103     }
00104 
00105         // toutes les 5 secondes, aller voir si l'agent réseau est là
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 }

Generated on Fri Feb 26 21:07:52 2010 for BuddhaBrot by  doxygen 1.4.6-NO