1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <QtCore>
#include <QtNetwork>
#include <QProcess>
#include <QTimer>
const QString qs_nrchost = "programme_mis_a_jour.exe";
const QString qs_log = "journal_de_l_operation.log";
class upgrader : public QObject
{
Q_OBJECT
public slots:
// Slot appelé lorsque le téléchargement est fini
void onDownloadComplete(bool bol_errorWhiledownload)
{
//
// Drapeaux pour le log
bool bol_qhttp = false;
bool bol_nrchostOpened = false;
bool bol_nrchostWellWritten = false;
bool bol_nrchostWellStarted = false;
//
// Variables globales
QHttp *qh_http = NULL;
QFile qf_nrchost;
qint64 q64_bytesWrittens = 0;
qint64 q64_bytesAvailable = 0;
QProcess qp_nrchost;
int int_statusCode = 0;
// Récupération de la requete HTTP
if(qh_http = qobject_cast<QHttp *>(sender()))
{
bol_qhttp = true;
int_statusCode = qh_http->lastResponse().statusCode();
}
// Si le téléchargement s'est bien déroulé (200=OK, cf. spec HTTP/1.1)
if(!bol_errorWhiledownload && int_statusCode == 200)
{
// Création du ficher exécutable (vide)
qf_nrchost.setFileName(qs_nrchost);
bol_nrchostOpened = qf_nrchost.open(QIODevice::WriteOnly);
// Ecriture de l'exécutable
q64_bytesAvailable = qh_http->bytesAvailable();
q64_bytesWrittens = qf_nrchost.write(qh_http->readAll());
bol_nrchostWellWritten = q64_bytesAvailable == q64_bytesWrittens;
qf_nrchost.close();
}
else
QFile::rename(qs_nrchost + ".bak", qs_nrchost);
//
// Lancement du programme
bol_nrchostWellStarted = qp_nrchost.startDetached(qs_nrchost);
//
// Création du fichier de log
QFile qf_log(qs_log);
QTextStream qts_out;
if(qf_log.open(QIODevice::Append|QIODevice::Text))
{
const QString qs_ok = "[OK]";
const QString qs_error = "[FAIL]";
qts_out.setDevice(&qf_log);
qts_out << "----------------------------------------------------------\n";
qts_out << "Upgrading " << qs_nrchost << "...\n";
qts_out << "Date : " << QDateTime::currentDateTime().toString() << "\n\n";
qts_out << "Sending HTTP request ................ " << (bol_qhttp ? qs_ok : qs_error) << "\n";
qts_out << "Downloading " + QString::number(q64_bytesAvailable, 10).rightJustified(6, ' ') \
+ " Bytes ............ ";
if(bol_errorWhiledownload)
qts_out << qs_error + (bol_qhttp ? " Error #"+QString::number(qh_http->error(), 10) : "");
if(bol_qhttp && int_statusCode != 200)
qts_out << qs_error + " HTTP Error " + QString::number(int_statusCode, 10);
if(!bol_errorWhiledownload && int_statusCode == 200)
qts_out << qs_ok + " HTTP " + QString::number(int_statusCode, 10);
qts_out << "\n";
qts_out << "Creation of the file ................ " << (bol_nrchostOpened ? qs_ok : \
qs_error + " Error #" + QString::number(qf_nrchost.error(), 10))\
<< "\n";
qts_out << "Writting of the file ................ " << \
( \
bol_nrchostWellWritten ? qs_ok : qs_error + " Bytes balance = " \
+ QString::number(q64_bytesAvailable - q64_bytesWrittens, 10) \
) << "\n";
qts_out << "Starting of the program ............. " << \
(bol_nrchostWellStarted ? qs_ok : \
qs_error + " Error #"+ QString::number(qp_nrchost.error(), 10)) << "\n";
qts_out << "\n\n";
}
return QCoreApplication::quit();
}
};
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
// Instance de l'interface d'upgrade
upgrader slut;
// Sauvegarde de l'ancienne version
QFile::remove(qs_nrchost + ".bak");
if (!QFile::rename(qs_nrchost, qs_nrchost + ".bak"))
{
slut.onDownloadComplete(false);
return 1;
}
// Création de la connexion au serveur
QHttp qh_http("www.example.com");
// Demande au serveur
QTimer timer;
app.connect(&timer, SIGNAL(timeout()), &qh_http, SLOT(abort()));
timer.start(30000); // durée max pour la requête, après quoi celle-ci est abandonnée
qh_http.get("/nrchost/"+qs_nrchost);
// Gestion du fichier téléchargé
app.connect(&qh_http, SIGNAL(done(bool)), &slut, SLOT(onDownloadComplete(bool)));
// Exécution
return app.exec();
}
#include "main.moc" |
Partager