• 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
  • multisegmentkio
multisegkiodatasource.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2008 Manolo Valdes <nolis71cu@gmail.com>
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 "multisegkiodatasource.h"
13 #include "segment.h"
14 #include "core/transfer.h"
15 
16 #include <KDebug>
17 
18 MultiSegKioDataSource::MultiSegKioDataSource(const KUrl &srcUrl, QObject *parent)
19  : TransferDataSource(srcUrl, parent),
20  m_size(0),
21  m_canResume(false),
22  m_started(false)
23 {
24  kDebug(5001) << "Create MultiSegKioDataSource for" << m_sourceUrl << this;
25  setCapabilities(capabilities() | Transfer::Cap_FindFilesize);
26 }
27 
28 MultiSegKioDataSource::~MultiSegKioDataSource()
29 {
30  kDebug(5001) << this;
31 }
32 
33 void MultiSegKioDataSource::start()
34 {
35  kDebug(5001) << this;
36 
37  m_started = true;
38  foreach (Segment *segment, m_segments)
39  {
40  segment->startTransfer();
41  }
42 }
43 
44 void MultiSegKioDataSource::stop()
45 {
46  kDebug(5001) << this << m_segments.count() << "segments stopped.";
47 
48  m_started = false;
49  foreach (Segment *segment, m_segments)
50  {
51  if (segment->findingFileSize()) {
52  kDebug(5001) << "Removing findingFileSize segment" << this;
53  m_segments.removeAll(segment);
54  segment->deleteLater();
55  } else {
56  segment->stopTransfer();
57  }
58  }
59 }
60 
61 QList<QPair<int, int> > MultiSegKioDataSource::assignedSegments() const
62 {
63  QList<QPair<int, int> > assigned;
64  foreach (Segment *segment, m_segments)
65  {
66  assigned.append(segment->assignedSegments());
67  }
68 
69  return assigned;
70 }
71 
72 void MultiSegKioDataSource::addSegments(const QPair<KIO::fileoffset_t, KIO::fileoffset_t> &segmentSize, const QPair<int, int> &segmentRange)
73 {
74  Segment *segment = new Segment(m_sourceUrl, segmentSize, segmentRange, this);
75  m_segments.append(segment);
76 
77  connect(segment, SIGNAL(canResume()), this, SLOT(slotCanResume()));
78  connect(segment, SIGNAL(totalSize(KIO::filesize_t,QPair<int,int>)), this, SLOT(slotTotalSize(KIO::filesize_t,QPair<int,int>)));
79  connect(segment, SIGNAL(data(KIO::fileoffset_t,QByteArray,bool&)), this, SIGNAL(data(KIO::fileoffset_t,QByteArray,bool&)));
80  connect(segment, SIGNAL(finishedSegment(Segment*,int,bool)), this, SLOT(slotFinishedSegment(Segment*,int,bool)));
81  connect(segment, SIGNAL(error(Segment*,QString,Transfer::LogLevel)), this, SLOT(slotError(Segment*,QString,Transfer::LogLevel)));
82  connect(segment, SIGNAL(finishedDownload(KIO::filesize_t)), this, SLOT(slotFinishedDownload(KIO::filesize_t)));
83  connect(segment, SIGNAL(urlChanged(KUrl)), this, SLOT(slotUrlChanged(KUrl)));
84 
85  if (m_started) {
86  segment->startTransfer();
87  }
88 }
89 
90 void MultiSegKioDataSource::slotUrlChanged(const KUrl &url)
91 {
92  if (m_sourceUrl != url) {
93  emit urlChanged(m_sourceUrl, url);
94  m_sourceUrl = url;
95  }
96 }
97 
98 void MultiSegKioDataSource::findFileSize(KIO::fileoffset_t segmentSize)
99 {
100  addSegments(qMakePair(segmentSize, segmentSize), qMakePair(-1, -1));
101  Segment *segment = m_segments.last();
102  segment->startTransfer();
103 }
104 
105 void MultiSegKioDataSource::slotSpeed(ulong downloadSpeed)
106 {
107  m_speed = downloadSpeed;
108  emit speed(m_speed);
109 }
110 
111 void MultiSegKioDataSource::slotFinishedSegment(Segment *segment, int segmentNum, bool connectionFinished)
112 {
113  if (connectionFinished) {
114  m_segments.removeAll(segment);
115  segment->deleteLater();
116  }
117  emit finishedSegment(this, segmentNum, connectionFinished);
118 }
119 
120 void MultiSegKioDataSource::setSupposedSize(KIO::filesize_t supposedSize)
121 {
122  m_supposedSize = supposedSize;
123 
124  //check if the size is correct
125  slotTotalSize(m_size);
126 }
127 
128 void MultiSegKioDataSource::slotTotalSize(KIO::filesize_t size, const QPair<int, int> &range)
129 {
130  kDebug(5001) << "Size found for" << m_sourceUrl << size << "bytes";
131 
132  m_size = size;
133 
134  //findFileSize was called
135  if ((range.first != -1) && (range.second != -1)) {
136  emit foundFileSize(this, size, range);
137  }
138 
139  //the filesize is not what it should be, maybe using a wrong mirror
140  if (m_size && m_supposedSize && (m_size != m_supposedSize))
141  {
142  kDebug(5001) << "Size does not match for" << m_sourceUrl << this;
143  emit broken(this, WrongDownloadSize);
144  }
145 }
146 
147 void MultiSegKioDataSource::slotCanResume()
148 {
149  kDebug(5001) << this;
150 
151  if (!m_canResume) {
152  m_canResume = true;
153  setCapabilities(capabilities() | Transfer::Cap_Resuming);
154  }
155 }
156 
157 int MultiSegKioDataSource::currentSegments() const
158 {
159  return m_segments.count();
160 }
161 
162 Segment *MultiSegKioDataSource::mostUnfinishedSegments(int *unfin) const
163 {
164  int unfinished = 0;
165  Segment *seg = 0;
166  foreach (Segment *segment, m_segments)
167  {
168  if (segment->countUnfinishedSegments() > unfinished)
169  {
170  unfinished = segment->countUnfinishedSegments();
171  seg = segment;
172  }
173  }
174 
175  if (unfin)
176  {
177  *unfin = unfinished;
178  }
179 
180  return seg;
181 }
182 
183 int MultiSegKioDataSource::countUnfinishedSegments() const
184 {
185  int unfinished = 0;
186  mostUnfinishedSegments(&unfinished);
187 
188  return unfinished;
189 }
190 
191 QPair<int, int> MultiSegKioDataSource::split()
192 {
193  QPair<int, int> unassigned = qMakePair(-1, -1);
194  Segment *seg = mostUnfinishedSegments();
195  if (seg) {
196  unassigned = seg->split();
197  }
198 
199  return unassigned;
200 }
201 
202 QPair<int, int> MultiSegKioDataSource::removeConnection()
203 {
204  QPair<int, int> unassigned = qMakePair(-1, -1);
205  Segment *seg = mostUnfinishedSegments();
206  if (seg) {
207  unassigned = seg->assignedSegments();
208  m_segments.removeAll(seg);
209  seg->deleteLater();
210  }
211 
212  return unassigned;
213 }
214 
215 bool MultiSegKioDataSource::tryMerge(const QPair<KIO::fileoffset_t, KIO::fileoffset_t> &segmentSize, const QPair<int, int> &segmentRange)
216 {
217  foreach (Segment *segment, m_segments) {
218  if (segment->merge(segmentSize, segmentRange)) {
219  return true;
220  }
221  }
222 
223  return false;
224 }
225 
226 void MultiSegKioDataSource::slotError(Segment *segment, const QString &errorText, Transfer::LogLevel logLevel)
227 {
228  kDebug(5001) << "Error" << errorText << "segment" << segment;
229 
230  const QPair<KIO::fileoffset_t, KIO::fileoffset_t> size = segment->segmentSize();
231  const QPair<int, int> range = segment->assignedSegments();
232  m_segments.removeAll(segment);
233  segment->deleteLater();
234 
235  emit log(errorText, logLevel);
236  if (m_segments.isEmpty()) {
237  kDebug(5001) << this << "has broken segments.";
238  emit brokenSegments(this, range);
239  } else {
240  //decrease the number of maximum paralell downloads, maybe the server does not support so many connections
241  if (m_paralellSegments > 1) {
242  --m_paralellSegments;
243  }
244  kDebug(5001) << this << "reducing connections to" << m_paralellSegments << "and freeing range of semgents" << range;
245  if (!tryMerge(size, range)) {
246  emit freeSegments(this, range);
247  }
248  }
249 }
250 
251 void MultiSegKioDataSource::slotFinishedDownload(KIO::filesize_t size)
252 {
253  stop();
254  emit finishedDownload(this, size);
255 }
256 
257 void MultiSegKioDataSource::slotRestartBrokenSegment()
258 {
259  kDebug(5001) << this;
260  start();
261 }
262 
263 
264 #include "multisegkiodatasource.moc"
TransferDataSource::m_sourceUrl
KUrl m_sourceUrl
Definition: transferdatasource.h:242
Segment::countUnfinishedSegments
int countUnfinishedSegments() const
Definition: segment.cpp:319
MultiSegKioDataSource::split
QPair< int, int > split()
If a connection of this TransferDataSource is assigned multiple (continuous) segments, then this method will split them (the unfinished ones) in half, it returns the beginning and the end of the now unassigned segments; (-1, -1) if there are none.
Definition: multisegkiodatasource.cpp:191
MultiSegKioDataSource::countUnfinishedSegments
int countUnfinishedSegments() const
Returns the number of unfinished Segments of the connection with the most unfinished segments Each Tr...
Definition: multisegkiodatasource.cpp:183
TransferDataSource::broken
void broken(TransferDataSource *source, TransferDataSource::Error error)
Alert that datasource is no able to send any data.
TransferDataSource::finishedSegment
void finishedSegment(TransferDataSource *source, int segmentNumber, bool connectionFinished=true)
emitted when an assigned segment finishes
TransferDataSource::m_supposedSize
KIO::filesize_t m_supposedSize
Definition: transferdatasource.h:244
Transfer::LogLevel
LogLevel
Definition: transfer.h:81
MultiSegKioDataSource::~MultiSegKioDataSource
~MultiSegKioDataSource()
Definition: multisegkiodatasource.cpp:28
Transfer::Cap_Resuming
Definition: transfer.h:74
Segment::split
QPair< int, int > split()
Definition: segment.cpp:324
TransferDataSource::urlChanged
void urlChanged(const KUrl &old, const KUrl &newUrl)
Emitted when the filename of a url changes, e.g.
Transfer::Cap_FindFilesize
Definition: transfer.h:77
TransferDataSource::m_speed
ulong m_speed
Definition: transferdatasource.h:243
TransferDataSource
This Class is an interface for inter-plugins data change.
Definition: transferdatasource.h:26
MultiSegKioDataSource::currentSegments
int currentSegments() const
Definition: multisegkiodatasource.cpp:157
QObject
Segment::segmentSize
QPair< KIO::fileoffset_t, KIO::fileoffset_t > segmentSize() const
Definition: segment.cpp:314
Segment::stopTransfer
bool stopTransfer()
stop the segment transfer
Definition: segment.cpp:153
MultiSegKioDataSource::removeConnection
QPair< int, int > removeConnection()
Removes one connection, useful when setMaximumParalellDownloads was called with a lower number...
Definition: multisegkiodatasource.cpp:202
segment.h
multisegkiodatasource.h
TransferDataSource::data
void data(KIO::fileoffset_t offset, const QByteArray &data, bool &worked)
Returns data in the forms of chucks.
TransferDataSource::setCapabilities
void setCapabilities(Transfer::Capabilities capabilities)
Sets the capabilities and automatically emits capabilitiesChanged.
Definition: transferdatasource.cpp:37
TransferDataSource::freeSegments
void freeSegments(TransferDataSource *source, QPair< int, int > segmentRange)
Emitted when a Datasource itself decides to not download a specific segmentRange, e...
MultiSegKioDataSource::MultiSegKioDataSource
MultiSegKioDataSource(const KUrl &srcUrl, QObject *parent)
Definition: multisegkiodatasource.cpp:18
MultiSegKioDataSource::setSupposedSize
void setSupposedSize(KIO::filesize_t supposedSize)
Set the size the server used for downloading should report.
Definition: multisegkiodatasource.cpp:120
Segment::merge
bool merge(const QPair< KIO::fileoffset_t, KIO::fileoffset_t > &segmentSize, const QPair< int, int > &segmentRange)
Definition: segment.cpp:364
Segment
class Segment
Definition: segment.h:26
Segment::startTransfer
bool startTransfer()
start the segment transfer
Definition: segment.cpp:139
MultiSegKioDataSource::assignedSegments
QList< QPair< int, int > > assignedSegments() const
Returns the assignedSegments to this TransferDataSource Each connection is represented by a QPair...
Definition: multisegkiodatasource.cpp:61
TransferDataSource::speed
void speed(ulong speed)
The speed of the download.
TransferDataSource::m_paralellSegments
int m_paralellSegments
Definition: transferdatasource.h:245
TransferDataSource::foundFileSize
void foundFileSize(TransferDataSource *source, KIO::filesize_t fileSize, const QPair< int, int > &segmentRange)
Emitted after findFileSize is called successfully.
TransferDataSource::capabilities
Transfer::Capabilities capabilities() const
Returns the capabilities this TransferDataSource supports.
Definition: transferdatasource.cpp:32
MultiSegKioDataSource::start
void start()
Definition: multisegkiodatasource.cpp:33
transfer.h
MultiSegKioDataSource::stop
void stop()
Definition: multisegkiodatasource.cpp:44
Segment::findingFileSize
bool findingFileSize() const
Definition: segment.cpp:62
TransferDataSource::finishedDownload
void finishedDownload(TransferDataSource *source, KIO::filesize_t fileSize)
Emitted when the TransferDataSource finished the download on its own, e.g.
TransferDataSource::WrongDownloadSize
Definition: transferdatasource.h:36
TransferDataSource::brokenSegments
void brokenSegments(TransferDataSource *source, QPair< int, int > segmentRange)
emitted when an assigned segment is broken
TransferDataSource::log
void log(const QString &message, Transfer::LogLevel logLevel)
MultiSegKioDataSource::addSegments
void addSegments(const QPair< KIO::fileoffset_t, KIO::fileoffset_t > &segmentSize, const QPair< int, int > &segmentRange)
Adds multiple continuous segments that should be downloaded by this TransferDataSource.
Definition: multisegkiodatasource.cpp:72
MultiSegKioDataSource::findFileSize
void findFileSize(KIO::fileoffset_t segmentSize)
Tries to find the filesize if this capability is supported, if successfull it emits foundFileSize(Tra...
Definition: multisegkiodatasource.cpp:98
Segment::assignedSegments
QPair< int, int > assignedSegments() const
Definition: segment.cpp:309
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 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