Skip to content

Commit

Permalink
#1 Change timer from pointer to member.
Browse files Browse the repository at this point in the history
  • Loading branch information
parsley72 committed Mar 5, 2014
1 parent c57ade3 commit c7b07a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
18 changes: 2 additions & 16 deletions downloadmanagerFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ DownloadManagerFTP::DownloadManagerFTP(QObject *parent) :
, _bAcceptRanges(false)
, _nDownloadSize(0)
, _nDownloadSizeAtPause(0)
, _pTimer(NULL)
, _pFTP(NULL)
{
}
Expand Down Expand Up @@ -82,12 +81,6 @@ void DownloadManagerFTP::download(QUrl url)

_pCurrentReply = _pManager->head(_CurrentRequest);

_pTimer = new QTimer(this);
_pTimer->setInterval(5000);
_pTimer->setSingleShot(true);
connect(_pTimer, SIGNAL(timeout()), this, SLOT(timeout()));
_pTimer->start();

connect(_pCurrentReply, SIGNAL(finished()), this, SLOT(finishedHead()));
connect(_pCurrentReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
}
Expand All @@ -101,11 +94,10 @@ void DownloadManagerFTP::pause()
{
return;
}
_pTimer->stop();
disconnect(_pTimer, SIGNAL(timeout()), this, SLOT(timeout()));
disconnect(_pCurrentReply,SIGNAL(finished()),this,SLOT(finished()));
disconnect(_pCurrentReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64)));
disconnect(_pCurrentReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
_Timer.stop();

_pCurrentReply->abort();
// _pFile->write( _pCurrentReply->readAll());
Expand Down Expand Up @@ -140,10 +132,6 @@ void DownloadManagerFTP::download()

_pCurrentReply = _pManager->get(_CurrentRequest);

_pTimer->setInterval(5000);
_pTimer->setSingleShot(true);
connect(_pTimer, SIGNAL(timeout()), this, SLOT(timeout()));
_pTimer->start();

connect(_pCurrentReply, SIGNAL(finished()), this, SLOT(finished()));
connect(_pCurrentReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
Expand Down Expand Up @@ -191,7 +179,7 @@ void DownloadManagerFTP::finished()
{
qDebug() << __FUNCTION__;

_pTimer->stop();
_Timer.stop();
_pFile->close();
QFile::remove(_qsFileName);
_pFile->rename(_qsFileName + ".part", _qsFileName);
Expand All @@ -203,7 +191,6 @@ void DownloadManagerFTP::finished()

void DownloadManagerFTP::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
_pTimer->stop();
_nDownloadSize = _nDownloadSizeAtPause + bytesReceived;
qDebug() << "Download Progress: Received=" << _nDownloadSize <<": Total=" << _nDownloadSizeAtPause + bytesTotal;

Expand All @@ -212,7 +199,6 @@ void DownloadManagerFTP::downloadProgress(qint64 bytesReceived, qint64 bytesTota
qDebug() << percentage;
emit progress(percentage);

_pTimer->start(5000);
}


Expand Down
2 changes: 1 addition & 1 deletion downloadmanagerFTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private slots:
bool _bAcceptRanges;
int _nDownloadSize;
int _nDownloadSizeAtPause;
QTimer *_pTimer;
QFtp *_pFTP;
QTimer _Timer;
};

#endif // QT_VERSION < 0x050000
Expand Down
30 changes: 14 additions & 16 deletions downloadmanagerHTTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ DownloadManagerHTTP::DownloadManagerHTTP(QObject *parent) :
, _bAcceptRanges(false)
, _nDownloadSize(0)
, _nDownloadSizeAtPause(0)
, _pTimer(NULL)
{
}

Expand All @@ -37,11 +36,10 @@ void DownloadManagerHTTP::download(QUrl url)

_pCurrentReply = _pManager->head(_CurrentRequest);

_pTimer = new QTimer(this);
_pTimer->setInterval(5000);
_pTimer->setSingleShot(true);
connect(_pTimer, SIGNAL(timeout()), this, SLOT(timeout()));
_pTimer->start();
_Timer.setInterval(5000);
_Timer.setSingleShot(true);
connect(&_Timer, SIGNAL(timeout()), this, SLOT(timeout()));
_Timer.start();

connect(_pCurrentReply, SIGNAL(finished()), this, SLOT(finishedHead()));
connect(_pCurrentReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
Expand All @@ -55,8 +53,8 @@ void DownloadManagerHTTP::pause()
{
return;
}
_pTimer->stop();
disconnect(_pTimer, SIGNAL(timeout()), this, SLOT(timeout()));
_Timer.stop();
disconnect(&_Timer, SIGNAL(timeout()), this, SLOT(timeout()));
disconnect(_pCurrentReply, SIGNAL(finished()), this, SLOT(finished()));
disconnect(_pCurrentReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
disconnect(_pCurrentReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
Expand Down Expand Up @@ -94,10 +92,10 @@ void DownloadManagerHTTP::download()

_pCurrentReply = _pManager->get(_CurrentRequest);

_pTimer->setInterval(5000);
_pTimer->setSingleShot(true);
connect(_pTimer, SIGNAL(timeout()), this, SLOT(timeout()));
_pTimer->start();
_Timer.setInterval(5000);
_Timer.setSingleShot(true);
connect(&_Timer, SIGNAL(timeout()), this, SLOT(timeout()));
_Timer.start();

connect(_pCurrentReply, SIGNAL(finished()), this, SLOT(finished()));
connect(_pCurrentReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
Expand All @@ -107,7 +105,7 @@ void DownloadManagerHTTP::download()

void DownloadManagerHTTP::finishedHead()
{
_pTimer->stop();
_Timer.stop();
_bAcceptRanges = false;

QList<QByteArray> list = _pCurrentReply->rawHeaderList();
Expand Down Expand Up @@ -145,7 +143,7 @@ void DownloadManagerHTTP::finished()
{
qDebug() << __FUNCTION__;

_pTimer->stop();
_Timer.stop();
_pFile->close();
QFile::remove(_qsFileName);
_pFile->rename(_qsFileName + ".part", _qsFileName);
Expand All @@ -157,7 +155,7 @@ void DownloadManagerHTTP::finished()

void DownloadManagerHTTP::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
_pTimer->stop();
_Timer.stop();
_nDownloadSize = _nDownloadSizeAtPause + bytesReceived;
qDebug() << "Download Progress: Received=" << _nDownloadSize <<": Total=" << _nDownloadSizeAtPause + bytesTotal;

Expand All @@ -166,7 +164,7 @@ void DownloadManagerHTTP::downloadProgress(qint64 bytesReceived, qint64 bytesTot
qDebug() << nPercentage;
emit progress(nPercentage);

_pTimer->start(5000);
_Timer.start(5000);
}


Expand Down
2 changes: 1 addition & 1 deletion downloadmanagerHTTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private slots:
bool _bAcceptRanges;
int _nDownloadSize;
int _nDownloadSizeAtPause;
QTimer *_pTimer;
QTimer _Timer;
};

#endif // DOWNLOADMANAGERHTTP_H

0 comments on commit c7b07a2

Please sign in to comment.