class DownloadCommand : public ICommand
{
Q_OBJECT
public:
DownloadCommand
(QObject *parent
= 0);
DownloadCommand
(const QString
& url,
QObject* parent
=0);
void setUrl
(const QString
& url
) { m_url
= QUrl(url
);
}
signals:
public slots:
void onNetworkReply(QNetworkReply* reply);
void downloadCompleted(qint64 d, qint64 size);
private:
QNetworkAccessManager manager;
QNetworkReply* m_reply;
};
DownloadCommand
::DownloadCommand(QObject *parent
) : ICommand(parent)
{
}
DownloadCommand
::DownloadCommand(const QString &url,
QObject *parent
) : ICommand
(parent
){
m_url
= QUrl::fromUserInput(url
);
}
{
m_reply = manager.get(QNetworkRequest(m_url));
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadCompleted(qint64,qint64)));
return tr("Download Started...");
}
void DownloadCommand::onNetworkReply(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError)
return;
if(data.length() < 2) return;
QString fileName
= fileInfo.
fileName();
if(fileName.isEmpty())
fileName = "unknown.file";
if(!m_localpath.isNull())
fileName
= m_localpath
+ QDir::separator() + fileName;
if(QFile::exists(fileName
)) {
int i = 0;
QString newfileName
= fileName
+ ".";
++i;
fm.rename(newfileName);
}
if(file.
open(QFile::WriteOnly)) file.write(data);
reply->deleteLater();
m_reply->deleteLater();
}
void DownloadCommand::downloadCompleted(qint64 d, qint64 size)
{
if((d == 0 && size == 0) || (m_reply->error() != QNetworkReply::NoError))
emit processFinished(tr("Cant download file error is %1").arg(m_reply->errorString()));
else
{
if(size != -1)
emit processChanges(100 * d / size);
else emit processChanges(-1);
if(size == d)
emit processFinished(tr("Download is complete"));
}
}
class DownloadCommand : public ICommand
{
Q_OBJECT
public:
DownloadCommand(QObject *parent = 0);
DownloadCommand(const QString& url, QObject* parent=0);
void setUrl(const QString& url) { m_url = QUrl(url); }
signals:
public slots:
void onNetworkReply(QNetworkReply* reply);
void downloadCompleted(qint64 d, qint64 size);
QString execute();
private:
QUrl m_url;
QString m_localpath;
QNetworkAccessManager manager;
QNetworkReply* m_reply;
};
DownloadCommand::DownloadCommand(QObject *parent) :
ICommand(parent)
{
}
DownloadCommand::DownloadCommand(const QString &url, QObject *parent) : ICommand(parent)
{
m_url = QUrl::fromUserInput(url);
}
QString DownloadCommand::execute()
{
m_reply = manager.get(QNetworkRequest(m_url));
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadCompleted(qint64,qint64)));
return tr("Download Started...");
}
void DownloadCommand::onNetworkReply(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError)
return;
QByteArray data = reply->readAll();
if(data.length() < 2) return;
QFileInfo fileInfo(m_url.path());
QString fileName = fileInfo.fileName();
if(fileName.isEmpty())
fileName = "unknown.file";
if(!m_localpath.isNull())
fileName = m_localpath + QDir::separator() + fileName;
if(QFile::exists(fileName))
{
int i = 0;
QString newfileName = fileName + ".";
while(QFile::exists(newfileName + QString::number(i)))
++i;
newfileName += QString::number(i);
QFile fm(fileName);
fm.rename(newfileName);
}
QFile file(fileName);
if(file.open(QFile::WriteOnly))
file.write(data);
reply->deleteLater();
m_reply->deleteLater();
}
void DownloadCommand::downloadCompleted(qint64 d, qint64 size)
{
if((d == 0 && size == 0) || (m_reply->error() != QNetworkReply::NoError))
emit processFinished(tr("Cant download file error is %1").arg(m_reply->errorString()));
else
{
if(size != -1)
emit processChanges(100 * d / size);
else emit processChanges(-1);
if(size == d)
emit processFinished(tr("Download is complete"));
}
}
To copy to clipboard, switch view to plain text mode
(i copy this code for dont say me give us some code man
Bookmarks