File indexing completed on 2024-04-14 14:32:19

0001 /***************************************************************************
0002                           cdownloader.cpp  -  download stuff
0003                              -------------------
0004     begin                : Pi mar 28 2003
0005     copyright            : (C) 2003 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "cdownloader.h"
0019 
0020 #include "cmsp.h"
0021 #include <QDir>
0022 #include <QUrl>
0023 
0024 #include <kio/job.h>
0025 
0026 cDownloader::cDownloader (cMSP *mspObject)
0027 {
0028   msp = mspObject;
0029   currentJob = nullptr;
0030 }
0031 
0032 cDownloader::~cDownloader ()
0033 {
0034   reset ();
0035 }
0036 
0037 void cDownloader::reset ()
0038 {
0039   if (currentJob)
0040   {
0041     currentJob->kill ();
0042     currentJob = nullptr;
0043   }
0044 }
0045 
0046 void cDownloader::download (QString remoteURL, QString localFile)
0047 {
0048   if (currentJob)
0049     return;
0050   
0051   //create all missing directories
0052   QString lastDir = localFile.section ("/", 0, -2, QString::SectionSkipEmpty);
0053   QDir().mkpath (lastDir);
0054   
0055   //download!!!
0056   QUrl url1 (remoteURL);
0057   QUrl url2;
0058   url2.setPath (localFile);
0059   currentJob = KIO::file_copy (url1, url2);
0060   connect (currentJob, &KJob::result, this, &cDownloader::slotResult);
0061 }
0062 
0063 void cDownloader::slotResult (KJob *job)
0064 {
0065   //download complete or failed or something
0066   int error = job->error ();
0067   if (error == 0)
0068     msp->downloadCompleted ();
0069   else
0070   {
0071     QString reason = job->errorString ();
0072     msp->downloadFailed (reason);
0073   }
0074   currentJob = nullptr;
0075 }
0076 
0077 #include "moc_cdownloader.cpp"