• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.14
  • kdenetwork
  • kget
  • transfer-plugins
  • kio
transferKio.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
4  Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 */
11 
12 #include "transferKio.h"
13 #include "core/verifier.h"
14 #include "core/signature.h"
15 #include "settings.h"
16 
17 #include <utime.h>
18 
19 #include <kiconloader.h>
20 #include <kio/scheduler.h>
21 #include <KIO/DeleteJob>
22 #include <KIO/CopyJob>
23 #include <KIO/NetAccess>
24 #include <KLocale>
25 #include <KMessageBox>
26 #include <KDebug>
27 
28 #include <QtCore/QFile>
29 #include <QDomElement>
30 
31 TransferKio::TransferKio(TransferGroup * parent, TransferFactory * factory,
32  Scheduler * scheduler, const KUrl & source, const KUrl & dest,
33  const QDomElement * e)
34  : Transfer(parent, factory, scheduler, source, dest, e),
35  m_copyjob(0),
36  m_movingFile(false),
37  m_verifier(0),
38  m_signature(0)
39 {
40  setCapabilities(Transfer::Cap_Moving | Transfer::Cap_Renaming | Transfer::Cap_Resuming);//TODO check if it really can resume
41 }
42 
43 bool TransferKio::setDirectory(const KUrl& newDirectory)
44 {
45  KUrl newDest = newDirectory;
46  newDest.addPath(m_dest.fileName());
47  return setNewDestination(newDest);
48 }
49 
50 bool TransferKio::setNewDestination(const KUrl &newDestination)
51 {
52  if (newDestination.isValid() && (newDestination != dest())) {
53  KUrl oldPath = KUrl(m_dest.path() + ".part");
54  if (oldPath.isValid() && QFile::exists(oldPath.pathOrUrl())) {
55  m_movingFile = true;
56  stop();
57  setStatus(Job::Moving);
58  setTransferChange(Tc_Status, true);
59 
60  m_dest = newDestination;
61 
62  if (m_verifier) {
63  m_verifier->setDestination(newDestination);
64  }
65  if (m_signature) {
66  m_signature->setDestination(newDestination);
67  }
68 
69  KIO::Job *move = KIO::file_move(oldPath, KUrl(newDestination.path() + ".part"), -1, KIO::HideProgressInfo);
70  connect(move, SIGNAL(result(KJob*)), this, SLOT(newDestResult(KJob*)));
71  connect(move, SIGNAL(infoMessage(KJob*,QString)), this, SLOT(slotInfoMessage(KJob*,QString)));
72  connect(move, SIGNAL(percent(KJob*,ulong)), this, SLOT(slotPercent(KJob*,ulong)));
73 
74  return true;
75  }
76  }
77  return false;
78 }
79 
80 void TransferKio::newDestResult(KJob *result)
81 {
82  Q_UNUSED(result)//TODO handle errors etc.!
83  m_movingFile = false;
84  start();
85  setTransferChange(Tc_FileName);
86 }
87 
88 void TransferKio::start()
89 {
90  if (!m_movingFile && (status() != Finished)) {
91  m_stopped = false;
92  if(!m_copyjob)
93  createJob();
94 
95  kDebug(5001) << "TransferKio::start";
96  setStatus(Job::Running, i18nc("transfer state: connecting", "Connecting...."), SmallIcon("network-connect")); // should be "network-connecting", but that doesn't exist for KDE 4.0 yet
97  setTransferChange(Tc_Status, true);
98  }
99 }
100 
101 void TransferKio::stop()
102 {
103  if ((status() == Stopped) || (status() == Finished)) {
104  return;
105  }
106 
107  m_stopped = true;
108 
109  if(m_copyjob)
110  {
111  m_copyjob->kill(KJob::EmitResult);
112  m_copyjob=0;
113  }
114 
115  kDebug(5001) << "Stop";
116  setStatus(Job::Stopped);
117  m_downloadSpeed = 0;
118  setTransferChange(Tc_Status | Tc_DownloadSpeed, true);
119 }
120 
121 void TransferKio::deinit(Transfer::DeleteOptions options)
122 {
123  if (options & DeleteFiles)//if the transfer is not finished, we delete the *.part-file
124  {
125  KIO::Job *del = KIO::del(QString(m_dest.path() + ".part"), KIO::HideProgressInfo);
126  KIO::NetAccess::synchronousRun(del, 0);
127  }//TODO: Ask the user if he/she wants to delete the *.part-file? To discuss (boom1992)
128 }
129 
130 //NOTE: INTERNAL METHODS
131 
132 void TransferKio::createJob()
133 {
134  if(!m_copyjob)
135  {
136  KIO::Scheduler::checkSlaveOnHold(true);
137  m_copyjob = KIO::file_copy(m_source, m_dest, -1, KIO::HideProgressInfo);
138 
139  connect(m_copyjob, SIGNAL(result(KJob*)),
140  this, SLOT(slotResult(KJob*)));
141  connect(m_copyjob, SIGNAL(infoMessage(KJob*,QString)),
142  this, SLOT(slotInfoMessage(KJob*,QString)));
143  connect(m_copyjob, SIGNAL(percent(KJob*,ulong)),
144  this, SLOT(slotPercent(KJob*,ulong)));
145  connect(m_copyjob, SIGNAL(totalSize(KJob*,qulonglong)),
146  this, SLOT(slotTotalSize(KJob*,qulonglong)));
147  connect(m_copyjob, SIGNAL(processedSize(KJob*,qulonglong)),
148  this, SLOT(slotProcessedSize(KJob*,qulonglong)));
149  connect(m_copyjob, SIGNAL(speed(KJob*,ulong)),
150  this, SLOT(slotSpeed(KJob*,ulong)));
151  }
152 }
153 
154 void TransferKio::slotResult( KJob * kioJob )
155 {
156  kDebug(5001) << "slotResult (" << kioJob->error() << ")";
157  switch (kioJob->error()) {
158  case 0: //The download has finished
159  case KIO::ERR_FILE_ALREADY_EXIST: //The file has already been downloaded.
160  setStatus(Job::Finished);
161  // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
162  m_percent = 100;
163  m_downloadSpeed = 0;
164  m_downloadedSize = m_totalSize;
165  setTransferChange(Tc_Percent | Tc_DownloadSpeed);
166  break;
167  default:
168  //There has been an error
169  kDebug(5001) << "-- E R R O R (" << kioJob->error() << ")--";
170  if (!m_stopped)
171  setStatus(Job::Aborted);
172  break;
173  }
174  // when slotResult gets called, the m_copyjob has already been deleted!
175  m_copyjob=0;
176 
177  // If it is an ftp file, there's still work to do
178  Transfer::ChangesFlags flags = (m_source.protocol() != "ftp") ? Tc_Status : Tc_None;
179  if (status() == Job::Finished) {
180  if (!m_totalSize) {
181  //downloaded elsewhere already, e.g. Konqueror
182  if (!m_downloadedSize) {
183  QFile file(m_dest.toLocalFile() + ".part");
184  m_downloadedSize = file.size();
185  if (!m_downloadedSize) {
186  QFile file(m_dest.toLocalFile());
187  m_downloadedSize = file.size();
188  }
189  }
190  m_totalSize = m_downloadedSize;
191  flags |= Tc_DownloadedSize;
192  }
193  if (m_verifier && Settings::checksumAutomaticVerification()) {
194  m_verifier->verify();
195  }
196  if (m_signature && Settings::signatureAutomaticVerification()) {
197  m_signature->verify();
198  }
199  }
200 
201  if (m_source.protocol() == "ftp") {
202  KIO::StatJob * statJob = KIO::stat(m_source);
203  connect(statJob, SIGNAL(result(KJob*)), this, SLOT(slotStatResult(KJob*)));
204  statJob->start();
205  }
206 
207  setTransferChange(flags, true);
208 }
209 
210 void TransferKio::slotInfoMessage( KJob * kioJob, const QString & msg )
211 {
212  Q_UNUSED(kioJob)
213  m_log.append(QString(msg));
214 }
215 
216 void TransferKio::slotPercent( KJob * kioJob, unsigned long percent )
217 {
218  kDebug(5001) << "slotPercent";
219  Q_UNUSED(kioJob)
220  m_percent = percent;
221  setTransferChange(Tc_Percent, true);
222 }
223 
224 void TransferKio::slotTotalSize( KJob * kioJob, qulonglong size )
225 {
226  Q_UNUSED(kioJob)
227 
228  kDebug(5001) << "slotTotalSize";
229 
230  setStatus(Job::Running);
231 
232  m_totalSize = size;
233  setTransferChange(Tc_Status | Tc_TotalSize, true);
234 }
235 
236 void TransferKio::slotProcessedSize( KJob * kioJob, qulonglong size )
237 {
238  Q_UNUSED(kioJob)
239 
240 // kDebug(5001) << "slotProcessedSize";
241 
242  if(status() != Job::Running)
243  {
244  setStatus(Job::Running);
245  setTransferChange(Tc_Status);
246  }
247  m_downloadedSize = size;
248  setTransferChange(Tc_DownloadedSize, true);
249 }
250 
251 void TransferKio::slotSpeed( KJob * kioJob, unsigned long bytes_per_second )
252 {
253  Q_UNUSED(kioJob)
254 
255 // kDebug(5001) << "slotSpeed";
256 
257  if(status() != Job::Running)
258  {
259  if (m_movingFile)
260  setStatus(Job::Moving);
261  else
262  setStatus(Job::Running);
263  setTransferChange(Tc_Status);
264 
265  }
266 
267  m_downloadSpeed = bytes_per_second;
268  setTransferChange(Tc_DownloadSpeed, true);
269 }
270 
271 void TransferKio::slotVerified(bool isVerified)
272 {
273  if (!isVerified) {
274  QString text = i18n("The download (%1) could not be verified. Do you want to repair it?", m_dest.fileName());
275 
276  if (!verifier()->partialChunkLength()) {
277  text = i18n("The download (%1) could not be verified. Do you want to redownload it?", m_dest.fileName());
278  }
279  if (KMessageBox::warningYesNo(0,
280  text,
281  i18n("Verification failed.")) == KMessageBox::Yes) {
282  repair();
283  }
284  }
285 }
286 
287 void TransferKio::slotStatResult(KJob* kioJob)
288 {
289  KIO::StatJob * statJob = qobject_cast<KIO::StatJob *>(kioJob);
290 
291  if (!statJob->error()) {
292  const KIO::UDSEntry entryResult = statJob->statResult();
293  struct utimbuf time;
294 
295  time.modtime = entryResult.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME);
296  time.actime = QDateTime::currentDateTime().toTime_t();
297  utime(m_dest.toLocalFile().toUtf8().constData(), &time);
298  }
299 
300  setStatus(Job::Finished);
301  setTransferChange(Tc_Status, true);
302 }
303 
304 
305 bool TransferKio::repair(const KUrl &file)
306 {
307  Q_UNUSED(file)
308 
309  if (verifier()->status() == Verifier::NotVerified)
310  {
311  m_downloadedSize = 0;
312  m_percent = 0;
313  if(m_copyjob)
314  {
315  m_copyjob->kill(KJob::Quietly);
316  m_copyjob = 0;
317  }
318  setTransferChange(Tc_DownloadedSize | Tc_Percent, true);
319 
320  start();
321 
322  return true;
323  }
324 
325  return false;
326 }
327 
328 Verifier *TransferKio::verifier(const KUrl &file)
329 {
330  Q_UNUSED(file)
331 
332  if (!m_verifier)
333  {
334  m_verifier = new Verifier(m_dest, this);
335  connect(m_verifier, SIGNAL(verified(bool)), this, SLOT(slotVerified(bool)));
336  }
337 
338  return m_verifier;
339 }
340 
341 Signature *TransferKio::signature(const KUrl &file)
342 {
343  Q_UNUSED(file)
344 
345  if (!m_signature) {
346  m_signature = new Signature(m_dest, this);
347  }
348 
349  return m_signature;
350 }
351 
352 #include "transferKio.moc"
TransferKio::start
void start()
Definition: transferKio.cpp:88
TransferKio::signature
Signature * signature(const KUrl &file=KUrl())
Definition: transferKio.cpp:341
Transfer::ChangesFlags
int ChangesFlags
Definition: transfer.h:100
TransferKio::verifier
Verifier * verifier(const KUrl &file=KUrl())
Definition: transferKio.cpp:328
Transfer::Tc_FileName
Definition: transfer.h:52
Transfer::m_dest
KUrl m_dest
Definition: transfer.h:357
TransferGroup
class TransferGroup:
Definition: transfergroup.h:46
Scheduler
Scheduler class: what handle all the jobs in kget.
Definition: scheduler.h:32
Job::Finished
The job is stopped, but this also indicates that it stopped because an error occurred.
Definition: job.h:47
Transfer::totalSize
KIO::filesize_t totalSize() const
Definition: transfer.h:169
TransferKio::deinit
void deinit(Transfer::DeleteOptions options)
Definition: transferKio.cpp:121
Transfer::m_downloadedSize
KIO::filesize_t m_downloadedSize
Definition: transfer.h:361
Signature::verify
void verify()
Definition: signature.cpp:196
Verifier::verify
void verify(const QModelIndex &index=QModelIndex())
Call this method if you want to verify() in its own thread, then signals with the result are emitted...
Definition: verifier.cpp:364
Transfer::Cap_Renaming
Definition: transfer.h:75
Verifier::partialChunkLength
KIO::filesize_t partialChunkLength() const
Returns the length of the "best" partialChecksums.
Definition: verifier.cpp:532
Transfer::Cap_Resuming
Definition: transfer.h:74
Job
Definition: job.h:35
Transfer::percent
int percent() const
Definition: transfer.h:178
Settings::checksumAutomaticVerification
static bool checksumAutomaticVerification()
Get ChecksumAutomaticVerification.
Definition: settings.h:1019
QFile::exists
bool exists() const
QFile
Verifier::setDestination
void setDestination(const KUrl &destination)
Definition: verifier.cpp:201
Transfer::m_totalSize
KIO::filesize_t m_totalSize
Definition: transfer.h:360
Transfer::Tc_Status
Definition: transfer.h:53
Transfer::m_downloadSpeed
int m_downloadSpeed
Definition: transfer.h:364
Transfer::Tc_None
Definition: transfer.h:49
Job::Running
Definition: job.h:43
TransferKio::TransferKio
TransferKio(TransferGroup *parent, TransferFactory *factory, Scheduler *scheduler, const KUrl &src, const KUrl &dest, const QDomElement *e=0)
Definition: transferKio.cpp:31
Transfer::Cap_Moving
Definition: transfer.h:76
QString
signature.h
verifier.h
TransferKio
Definition: transferKio.h:27
Job::Aborted
The job is stopped.
Definition: job.h:45
QFile::size
virtual qint64 size() const
Transfer::Tc_DownloadSpeed
Definition: transfer.h:56
Transfer::setTransferChange
virtual void setTransferChange(ChangesFlags change, bool postEvent=false)
Makes the TransferHandler associated with this transfer know that a change in this transfer has occur...
Definition: transfer.cpp:338
TransferKio::setDirectory
virtual bool setDirectory(const KUrl &newDirectory)
Move the download to the new destination.
Definition: transferKio.cpp:43
QDateTime::toTime_t
uint toTime_t() const
transferKio.h
Job::Moving
The job exited from its Running state successfully but wants to be restarted by the scheduler...
Definition: job.h:50
Transfer::m_log
QStringList m_log
Definition: transfer.h:359
Job::Stopped
The job is being executed.
Definition: job.h:44
Settings::signatureAutomaticVerification
static bool signatureAutomaticVerification()
Get SignatureAutomaticVerification.
Definition: settings.h:1057
QDateTime::currentDateTime
QDateTime currentDateTime()
Verifier
Definition: verifier.h:68
settings.h
Job::status
Status status() const
Definition: job.h:93
Transfer::dest
const KUrl & dest() const
Definition: transfer.h:149
TransferKio::setNewDestination
bool setNewDestination(const KUrl &newDestination)
Definition: transferKio.cpp:50
Transfer::setStatus
void setStatus(Job::Status jobStatus, const QString &text=QString(), const QPixmap &pix=QPixmap())
Sets the Job status to jobStatus, the status text to text and the status pixmap to pix...
Definition: transfer.cpp:292
Transfer::Tc_TotalSize
Definition: transfer.h:54
Signature
Class to verify signatures.
Definition: signature.h:38
Transfer::Tc_Percent
Definition: transfer.h:55
Transfer::Tc_DownloadedSize
Definition: transfer.h:63
Transfer::m_percent
int m_percent
Definition: transfer.h:363
Transfer::DeleteFiles
Definition: transfer.h:97
TransferKio::repair
bool repair(const KUrl &file=KUrl())
Tries to repair file.
Definition: transferKio.cpp:305
Signature::setDestination
void setDestination(const KUrl &destination)
Definition: signature.cpp:119
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
TransferFactory
TransferFactory.
Definition: transferfactory.h:52
Transfer::m_source
KUrl m_source
Definition: transfer.h:356
Transfer::setCapabilities
void setCapabilities(Capabilities capabilities)
Sets the capabilities and automatically emits capabilitiesChanged.
Definition: transfer.cpp:68
QDomElement
Verifier::NotVerified
Definition: verifier.h:79
KJob
TransferKio::stop
void stop()
Definition: transferKio.cpp:101
Transfer
Definition: transfer.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:28:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal