-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.cpp
141 lines (125 loc) · 4.15 KB
/
updater.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* This software is a part to the amonsoft solution
*
* Copyright Amon & Sesam Micro
* Developper contact [email protected]
*
* $Id: updater.cpp,v 1.1 2006/08/27 15:42:00 pascal Exp $
* $Log: updater.cpp,v $
* Revision 1.1 2006/08/27 15:42:00 pascal
* - Système de mise à jour automatique
*
*/
#include <process.h>
#include <qdir.h>
#include <qmessagebox.h>
#include <qstringlist.h>
#include "main.h"
#include "updater.h"
//----------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------
CUpdater::CUpdater( QWidget &main ) : QObject(), url_( URL_UPDATE, 80 ), main_( main )
{
file_ = 0;
state_ = 0;
connect(&url_, SIGNAL(done(bool)), this, SLOT(done(bool)));
}
//----------------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------------
CUpdater::~CUpdater()
{
if( file_ != 0 )
delete file_;
}
//----------------------------------------------------------------------------
// Check for update available
//----------------------------------------------------------------------------
void CUpdater::updateAvailable()
{
url_.get( "/savane/amonsoft/last.txt" );
}
//----------------------------------------------------------------------------
// Download update
//----------------------------------------------------------------------------
void CUpdater::downloadUpdate( QString sVersion )
{
QString sUrl = "/savane/amonsoft/amonsoft-" + sVersion + ".exe";
QString sFile = "amonsoft-" + sVersion + ".exe";
unlink( sFile );
file_ = new QFile( sFile );
bool bErr = file_->open( IO_WriteOnly );
if( !bErr )
{
qDebug( QString( "Impossible de creer" ) + sFile );
return ;
}
state_ = 1;
url_.get( sUrl, file_ );
main_.setDisabled( true );
// system( QString("explore.exe http://URL_UPDATE") + sUrl );
}
//----------------------------------------------------------------------------
// Check diff version
// Version form : Major.Minor.Build
//----------------------------------------------------------------------------
int CUpdater::diffVersion( QString sCurrent, QString sNet )
{
QStringList listStr, listStr2;
listStr = QStringList::split( ".", sCurrent );
listStr2 = QStringList::split( ".", sNet );
if( listStr[0].toUInt() > listStr2[0].toUInt() )
return -1;
if( listStr[0].toUInt() < listStr2[0].toUInt() )
return 1;
if( listStr[1].toUInt() > listStr2[1].toUInt() )
return -1;
if( listStr[1].toUInt() < listStr2[1].toUInt() )
return 1;
if( listStr[2].toUInt() > listStr2[2].toUInt() )
return -1;
if( listStr[2].toUInt() < listStr2[2].toUInt() )
return 1;
return 0;
}
//----------------------------------------------------------------------------
// Signal
//----------------------------------------------------------------------------
void CUpdater::done( bool bError )
{
if( bError )
{
qDebug( "Error HTTP" );
return ;
}
switch( state_ )
{
case 0:
{ // Check Update available
int iRep = 0;
QString sVersion = QString(url_.readAll());
qDebug( "Last online version available " + sVersion );
if( diffVersion( APP_VERSION, sVersion ) >= 1 )
{
QString str = "Une nouvelle version est disponible : " + sVersion;
iRep = QMessageBox::information( &main_,
"Mise à jour",
str + "\n Souhaitez-vous la télécharger ?",
QMessageBox::Yes,
QMessageBox::No );
if( iRep == QMessageBox::Yes )
downloadUpdate( sVersion );
}
break ;
}
case 1:
{
file_->close();
qDebug( "Fichier telecharger" );
qDebug( QDir::currentDirPath() + "\\" + file_->name() );
execl( QDir::currentDirPath() + "\\" + file_->name(), file_->name(), 0 );
break ;
}
}
}