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

kget

  • sources
  • kde-4.12
  • 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 <kiconloader.h>
18 #include <kio/scheduler.h>
19 #include <KIO/DeleteJob>
20 #include <KIO/CopyJob>
21 #include <KIO/NetAccess>
22 #include <KLocale>
23 #include <KMessageBox>
24 #include <KDebug>
25 
26 #include <QtCore/QFile>
27 #include <QDomElement>
28 
29 TransferKio::TransferKio(TransferGroup * parent, TransferFactory * factory,
30  Scheduler * scheduler, const KUrl & source, const KUrl & dest,
31  const QDomElement * e)
32  : Transfer(parent, factory, scheduler, source, dest, e),
33  m_copyjob(0),
34  m_movingFile(false),
35  m_verifier(0),
36  m_signature(0)
37 {
38  setCapabilities(Transfer::Cap_Moving | Transfer::Cap_Renaming | Transfer::Cap_Resuming);//TODO check if it really can resume
39 }
40 
41 bool TransferKio::setDirectory(const KUrl& newDirectory)
42 {
43  KUrl newDest = newDirectory;
44  newDest.addPath(m_dest.fileName());
45  return setNewDestination(newDest);
46 }
47 
48 bool TransferKio::setNewDestination(const KUrl &newDestination)
49 {
50  if (newDestination.isValid() && (newDestination != dest())) {
51  KUrl oldPath = KUrl(m_dest.path() + ".part");
52  if (oldPath.isValid() && QFile::exists(oldPath.pathOrUrl())) {
53  m_movingFile = true;
54  stop();
55  setStatus(Job::Moving);
56  setTransferChange(Tc_Status, true);
57 
58  m_dest = newDestination;
59 
60  if (m_verifier) {
61  m_verifier->setDestination(newDestination);
62  }
63  if (m_signature) {
64  m_signature->setDestination(newDestination);
65  }
66 
67  KIO::Job *move = KIO::file_move(oldPath, KUrl(newDestination.path() + ".part"), -1, KIO::HideProgressInfo);
68  connect(move, SIGNAL(result(KJob*)), this, SLOT(newDestResult(KJob*)));
69  connect(move, SIGNAL(infoMessage(KJob*,QString)), this, SLOT(slotInfoMessage(KJob*,QString)));
70  connect(move, SIGNAL(percent(KJob*,ulong)), this, SLOT(slotPercent(KJob*,ulong)));
71 
72  return true;
73  }
74  }
75  return false;
76 }
77 
78 void TransferKio::newDestResult(KJob *result)
79 {
80  Q_UNUSED(result)//TODO handle errors etc.!
81  m_movingFile = false;
82  start();
83  setTransferChange(Tc_FileName);
84 }
85 
86 void TransferKio::start()
87 {
88  if (!m_movingFile && (status() != Finished)) {
89  m_stopped = false;
90  if(!m_copyjob)
91  createJob();
92 
93  kDebug(5001) << "TransferKio::start";
94  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
95  setTransferChange(Tc_Status, true);
96  }
97 }
98 
99 void TransferKio::stop()
100 {
101  if ((status() == Stopped) || (status() == Finished)) {
102  return;
103  }
104 
105  m_stopped = true;
106 
107  if(m_copyjob)
108  {
109  m_copyjob->kill(KJob::EmitResult);
110  m_copyjob=0;
111  }
112 
113  kDebug(5001) << "Stop";
114  setStatus(Job::Stopped);
115  m_downloadSpeed = 0;
116  setTransferChange(Tc_Status | Tc_DownloadSpeed, true);
117 }
118 
119 void TransferKio::deinit(Transfer::DeleteOptions options)
120 {
121  if (options & DeleteFiles)//if the transfer is not finished, we delete the *.part-file
122  {
123  KIO::Job *del = KIO::del(QString(m_dest.path() + ".part"), KIO::HideProgressInfo);
124  KIO::NetAccess::synchronousRun(del, 0);
125  }//TODO: Ask the user if he/she wants to delete the *.part-file? To discuss (boom1992)
126 }
127 
128 //NOTE: INTERNAL METHODS
129 
130 void TransferKio::createJob()
131 {
132  if(!m_copyjob)
133  {
134  KIO::Scheduler::checkSlaveOnHold(true);
135  m_copyjob = KIO::file_copy(m_source, m_dest, -1, KIO::HideProgressInfo);
136 
137  connect(m_copyjob, SIGNAL(result(KJob*)),
138  this, SLOT(slotResult(KJob*)));
139  connect(m_copyjob, SIGNAL(infoMessage(KJob*,QString)),
140  this, SLOT(slotInfoMessage(KJob*,QString)));
141  connect(m_copyjob, SIGNAL(percent(KJob*,ulong)),
142  this, SLOT(slotPercent(KJob*,ulong)));
143  connect(m_copyjob, SIGNAL(totalSize(KJob*,qulonglong)),
144  this, SLOT(slotTotalSize(KJob*,qulonglong)));
145  connect(m_copyjob, SIGNAL(processedSize(KJob*,qulonglong)),
146  this, SLOT(slotProcessedSize(KJob*,qulonglong)));
147  connect(m_copyjob, SIGNAL(speed(KJob*,ulong)),
148  this, SLOT(slotSpeed(KJob*,ulong)));
149  }
150 }
151 
152 void TransferKio::slotResult( KJob * kioJob )
153 {
154  kDebug(5001) << "slotResult (" << kioJob->error() << ")";
155  switch (kioJob->error()) {
156  case 0: //The download has finished
157  case KIO::ERR_FILE_ALREADY_EXIST: //The file has already been downloaded.
158  setStatus(Job::Finished);
159  // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
160  m_percent = 100;
161  m_downloadSpeed = 0;
162  m_downloadedSize = m_totalSize;
163  setTransferChange(Tc_Percent | Tc_DownloadSpeed);
164  break;
165  default:
166  //There has been an error
167  kDebug(5001) << "-- E R R O R (" << kioJob->error() << ")--";
168  if (!m_stopped)
169  setStatus(Job::Aborted);
170  break;
171  }
172  // when slotResult gets called, the m_copyjob has already been deleted!
173  m_copyjob=0;
174 
175  Transfer::ChangesFlags flags = Tc_Status;
176  if (status() == Job::Finished) {
177  if (!m_totalSize) {
178  //downloaded elsewhere already, e.g. Konqueror
179  if (!m_downloadedSize) {
180  QFile file(m_dest.toLocalFile() + ".part");
181  m_downloadedSize = file.size();
182  if (!m_downloadedSize) {
183  QFile file(m_dest.toLocalFile());
184  m_downloadedSize = file.size();
185  }
186  }
187  m_totalSize = m_downloadedSize;
188  flags |= Tc_DownloadedSize;
189  }
190  if (m_verifier && Settings::checksumAutomaticVerification()) {
191  m_verifier->verify();
192  }
193  if (m_signature && Settings::signatureAutomaticVerification()) {
194  m_signature->verify();
195  }
196  }
197 
198  setTransferChange(flags, true);
199 }
200 
201 void TransferKio::slotInfoMessage( KJob * kioJob, const QString & msg )
202 {
203  Q_UNUSED(kioJob)
204  m_log.append(QString(msg));
205 }
206 
207 void TransferKio::slotPercent( KJob * kioJob, unsigned long percent )
208 {
209  kDebug(5001) << "slotPercent";
210  Q_UNUSED(kioJob)
211  m_percent = percent;
212  setTransferChange(Tc_Percent, true);
213 }
214 
215 void TransferKio::slotTotalSize( KJob * kioJob, qulonglong size )
216 {
217  Q_UNUSED(kioJob)
218 
219  kDebug(5001) << "slotTotalSize";
220 
221  setStatus(Job::Running);
222 
223  m_totalSize = size;
224  setTransferChange(Tc_Status | Tc_TotalSize, true);
225 }
226 
227 void TransferKio::slotProcessedSize( KJob * kioJob, qulonglong size )
228 {
229  Q_UNUSED(kioJob)
230 
231 // kDebug(5001) << "slotProcessedSize";
232 
233  if(status() != Job::Running)
234  {
235  setStatus(Job::Running);
236  setTransferChange(Tc_Status);
237  }
238  m_downloadedSize = size;
239  setTransferChange(Tc_DownloadedSize, true);
240 }
241 
242 void TransferKio::slotSpeed( KJob * kioJob, unsigned long bytes_per_second )
243 {
244  Q_UNUSED(kioJob)
245 
246 // kDebug(5001) << "slotSpeed";
247 
248  if(status() != Job::Running)
249  {
250  if (m_movingFile)
251  setStatus(Job::Moving);
252  else
253  setStatus(Job::Running);
254  setTransferChange(Tc_Status);
255 
256  }
257 
258  m_downloadSpeed = bytes_per_second;
259  setTransferChange(Tc_DownloadSpeed, true);
260 }
261 
262 void TransferKio::slotVerified(bool isVerified)
263 {
264  if (!isVerified) {
265  QString text = i18n("The download (%1) could not be verified. Do you want to repair it?", m_dest.fileName());
266 
267  if (!verifier()->partialChunkLength()) {
268  text = i18n("The download (%1) could not be verified. Do you want to redownload it?", m_dest.fileName());
269  }
270  if (KMessageBox::warningYesNo(0,
271  text,
272  i18n("Verification failed.")) == KMessageBox::Yes) {
273  repair();
274  }
275  }
276 }
277 
278 bool TransferKio::repair(const KUrl &file)
279 {
280  Q_UNUSED(file)
281 
282  if (verifier()->status() == Verifier::NotVerified)
283  {
284  m_downloadedSize = 0;
285  m_percent = 0;
286  if(m_copyjob)
287  {
288  m_copyjob->kill(KJob::Quietly);
289  m_copyjob = 0;
290  }
291  setTransferChange(Tc_DownloadedSize | Tc_Percent, true);
292 
293  start();
294 
295  return true;
296  }
297 
298  return false;
299 }
300 
301 Verifier *TransferKio::verifier(const KUrl &file)
302 {
303  Q_UNUSED(file)
304 
305  if (!m_verifier)
306  {
307  m_verifier = new Verifier(m_dest, this);
308  connect(m_verifier, SIGNAL(verified(bool)), this, SLOT(slotVerified(bool)));
309  }
310 
311  return m_verifier;
312 }
313 
314 Signature *TransferKio::signature(const KUrl &file)
315 {
316  Q_UNUSED(file)
317 
318  if (!m_signature) {
319  m_signature = new Signature(m_dest, this);
320  }
321 
322  return m_signature;
323 }
324 
325 #include "transferKio.moc"
TransferKio::start
void start()
Definition: transferKio.cpp:86
TransferKio::signature
Signature * signature(const KUrl &file=KUrl())
Definition: transferKio.cpp:314
Transfer::ChangesFlags
int ChangesFlags
Definition: transfer.h:100
TransferKio::verifier
Verifier * verifier(const KUrl &file=KUrl())
Definition: transferKio.cpp:301
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:119
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
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
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:29
Transfer::Cap_Moving
Definition: transfer.h:76
signature.h
verifier.h
TransferKio
Definition: transferKio.h:27
Job::Aborted
The job is stopped.
Definition: job.h:45
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:41
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
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:48
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:278
Signature::setDestination
void setDestination(const KUrl &destination)
Definition: signature.cpp:119
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
Verifier::NotVerified
Definition: verifier.h:79
KJob
TransferKio::stop
void stop()
Definition: transferKio.cpp:99
Transfer
Definition: transfer.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:18 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