filedownloader.cpp
175 lines
| 4.8 KiB
| text/x-c
|
CppLexer
r11 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the QLop Software | ||||
-- Copyright (C) 2015, Plasma Physics Laboratory - CNRS | ||||
-- | ||||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 2 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
-------------------------------------------------------------------------------*/ | ||||
/*-- Author : Alexis Jeandet | ||||
-- Mail : alexis.jeandet@member.fsf.org | ||||
----------------------------------------------------------------------------*/ | ||||
r4 | #include "filedownloader.h" | |||
#include <QFile> | ||||
FileDownloader* FileDownloader::_self=NULL; | ||||
QNetworkAccessManager* FileDownloader::m_WebCtrl=NULL; | ||||
QList<FileDownloaderTask*>* FileDownloader::m_pendingTasks=NULL; | ||||
r5 | QDockWidget* FileDownloader::m_gui=NULL; | |||
DownLoadHistory* FileDownloader::m_DownLoadHistory=NULL; | ||||
r4 | ||||
#define _INIT \ | ||||
if(Q_UNLIKELY(_self==NULL))\ | ||||
{\ | ||||
init();\ | ||||
}\ | ||||
int FileDownloader::downloadFile(QUrl fileUrl, const QString &name) | ||||
{ | ||||
_INIT | ||||
if(QFile::exists(name)|| QFile::exists(name+".part")) | ||||
{ | ||||
return -1; | ||||
} | ||||
FileDownloaderTask* task=NULL; | ||||
int ID=_self->getTaskId(); | ||||
if(ID!=-1) | ||||
{ | ||||
QNetworkRequest request(fileUrl); | ||||
QNetworkReply* reply = m_WebCtrl->get(request); | ||||
if(reply && (reply->error()==QNetworkReply::NoError)) | ||||
{ | ||||
task=new FileDownloaderTask(reply,ID,name,_self); | ||||
m_pendingTasks->append(task); | ||||
r5 | if(!_self->m_noGui) | |||
r4 | { | |||
r5 | m_DownLoadHistory->addElement(new DownloadHistoryElement(task)); | |||
r4 | } | |||
} | ||||
else | ||||
{ | ||||
return -1; | ||||
} | ||||
} | ||||
return ID; | ||||
} | ||||
int FileDownloader::downloadFile(QString fileUrl, const QString &name) | ||||
{ | ||||
return downloadFile(QUrl(fileUrl),name); | ||||
} | ||||
r5 | QDockWidget *FileDownloader::getGUI() | |||
r4 | { | |||
r5 | if(!_self->m_noGui && (m_gui==NULL)) | |||
{ | ||||
m_DownLoadHistory=new DownLoadHistory(); | ||||
m_gui=new QDockWidget("Download History"); | ||||
m_gui->setWidget(m_DownLoadHistory); | ||||
m_gui->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable); | ||||
} | ||||
return (QDockWidget*) m_gui; | ||||
r4 | } | |||
const QString &FileDownloader::serviceName() | ||||
{ | ||||
return m_serviceName; | ||||
} | ||||
r5 | int FileDownloader::download_file(QUrl fileUrl, const QString &name) | |||
{ | ||||
return downloadFile(fileUrl,name); | ||||
} | ||||
int FileDownloader::download_file(QString fileUrl, const QString &name) | ||||
{ | ||||
return downloadFile(fileUrl,name); | ||||
} | ||||
r4 | FileDownloaderTask* FileDownloader::getDownloadTask(int ID) | |||
{ | ||||
_INIT | ||||
for(int i=0;i<m_pendingTasks->count();i++) | ||||
{ | ||||
if(m_pendingTasks->at(i)->ID()==ID) | ||||
return m_pendingTasks->at(i); | ||||
} | ||||
return NULL; | ||||
} | ||||
bool FileDownloader::taskIsCompleted(int ID) | ||||
{ | ||||
return getDownloadTask(ID)->downloadComplete(); | ||||
} | ||||
FileDownloader *FileDownloader::self() | ||||
{ | ||||
_INIT | ||||
return _self; | ||||
} | ||||
int FileDownloader::getTaskId() | ||||
{ | ||||
for(unsigned int i=0;i<INT_MAX;i++) | ||||
{ | ||||
bool idValid=true; | ||||
for(int j=0;j<m_pendingTasks->count();j++) | ||||
{ | ||||
if(m_pendingTasks->at(j)->ID()==(int)i) | ||||
idValid=false; | ||||
} | ||||
if(idValid) | ||||
return (int)i; | ||||
} | ||||
return -1; | ||||
} | ||||
void FileDownloader::init(bool noGUI, QObject *parent) | ||||
{ | ||||
if(Q_UNLIKELY(_self==NULL)) | ||||
{ | ||||
r5 | _self=new FileDownloader(noGUI,parent); | |||
r4 | } | |||
} | ||||
r5 | FileDownloader::FileDownloader(bool noGUI,QObject *parent) : QLopService(parent) | |||
r4 | { | |||
QNetworkProxyQuery q(QUrl("http://www.google.com")); | ||||
q.setQueryType(QNetworkProxyQuery::UrlRequest); | ||||
q.setProtocolTag("http"); | ||||
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery(q); | ||||
if( proxies.size() > 0 && proxies[0].type() != QNetworkProxy::NoProxy ) | ||||
QNetworkProxy::setApplicationProxy(proxies[0]); | ||||
else | ||||
qDebug("No proxy server selected"); | ||||
m_WebCtrl = new QNetworkAccessManager(this); | ||||
m_pendingTasks = new QList<FileDownloaderTask*>(); | ||||
r5 | m_noGui=noGUI; | |||
m_serviceName="FileDownloader"; | ||||
r4 | } | |||
FileDownloader::~FileDownloader() | ||||
{ | ||||
if(!m_noGui) | ||||
delete m_gui; | ||||
while (m_pendingTasks->count()) | ||||
{ | ||||
FileDownloaderTask* task=m_pendingTasks->last(); | ||||
m_pendingTasks->removeLast(); | ||||
delete task; | ||||
} | ||||
delete m_WebCtrl; | ||||
} | ||||