@@ -73,6 +73,8 @@ class AbstractFileShareDownloader : public QObject {
73
73
Jid selectOnlineJid (const QList<Jid> &jids) const
74
74
{
75
75
for (auto const &j : jids) {
76
+ if (j == acc->client ()->jid ())
77
+ continue ;
76
78
for (UserListItem *u : acc->findRelevant (j)) {
77
79
UserResourceList::Iterator rit = u->userResourceList ().find (j.resource ());
78
80
if (rit != u->userResourceList ().end ())
@@ -84,9 +86,7 @@ class AbstractFileShareDownloader : public QObject {
84
86
85
87
public:
86
88
AbstractFileShareDownloader (PsiAccount *acc, const QString &uri, QObject *parent) :
87
- QObject (parent),
88
- acc (acc),
89
- sourceUri (uri)
89
+ QObject (parent), acc(acc), sourceUri(uri)
90
90
{
91
91
}
92
92
@@ -103,10 +103,7 @@ class AbstractFileShareDownloader : public QObject {
103
103
rangeSize = length;
104
104
}
105
105
inline bool isRanged () const { return rangeSize || rangeStart; }
106
- std::tuple<qint64, qint64> range () const
107
- {
108
- return std::tuple<qint64, qint64>(rangeStart, rangeSize);
109
- }
106
+ std::tuple<qint64, qint64> range () const { return std::tuple<qint64, qint64>(rangeStart, rangeSize); }
110
107
111
108
signals:
112
109
void metaDataChanged ();
@@ -125,12 +122,10 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
125
122
QList<Jid> jids;
126
123
127
124
public:
128
- JingleFileShareDownloader (PsiAccount *acc, const QString &uri,
129
- const XMPP::Jingle::FileTransfer::File &file,
125
+ JingleFileShareDownloader (PsiAccount *acc, const QString &uri, const XMPP::Jingle::FileTransfer::File &file,
130
126
const QList<Jid> &jids, QObject *parent) :
131
127
AbstractFileShareDownloader (acc, uri, parent),
132
- file (file),
133
- jids (jids)
128
+ file (file), jids(jids)
134
129
{
135
130
}
136
131
@@ -163,8 +158,9 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
163
158
return ;
164
159
}
165
160
166
- Jingle::Session *session = acc->client ()->jingleManager ()->newSession (dataSource);
167
- app = static_cast <Jingle::FileTransfer::Application *>(session->newContent (Jingle::FileTransfer::NS, Jingle::Origin::Responder));
161
+ auto *session = acc->client ()->jingleManager ()->newSession (dataSource);
162
+ app = static_cast <Jingle::FileTransfer::Application *>(
163
+ session->newContent (Jingle::FileTransfer::NS, Jingle::Origin::Responder));
168
164
if (!app) {
169
165
downloadError (QString::fromLatin1 (" Jingle file transfer is disabled" ));
170
166
return ;
@@ -186,7 +182,8 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
186
182
rangeStart = qint64 (r.offset );
187
183
rangeSize = qint64 (r.length );
188
184
connection = app->connection ();
189
- connect (connection.data (), &XMPP::Jingle::Connection::readyRead, this , &JingleFileShareDownloader::readyRead);
185
+ connect (connection.data (), &XMPP::Jingle::Connection::readyRead, this ,
186
+ &JingleFileShareDownloader::readyRead);
190
187
emit metaDataChanged ();
191
188
});
192
189
@@ -199,17 +196,13 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
199
196
}
200
197
downloadError (tr (" Jingle download failed" ));
201
198
});
202
- }
203
199
204
- qint64 bytesAvailable () const
205
- {
206
- return connection ? connection->bytesAvailable () : 0 ;
200
+ session->initiate ();
207
201
}
208
202
209
- qint64 read (char *data, qint64 maxSize)
210
- {
211
- return connection ? connection->read (data, maxSize) : 0 ;
212
- }
203
+ qint64 bytesAvailable () const { return connection ? connection->bytesAvailable () : 0 ; }
204
+
205
+ qint64 read (char *data, qint64 maxSize) { return connection ? connection->read (data, maxSize) : 0 ; }
213
206
214
207
void abort (bool isFailure, const QString &reason)
215
208
{
@@ -224,10 +217,7 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
224
217
}
225
218
}
226
219
227
- bool isConnected () const
228
- {
229
- return connection && app && app->state () == XMPP::Jingle::State::Active;
230
- }
220
+ bool isConnected () const { return connection && app && app->state () == XMPP::Jingle::State::Active; }
231
221
};
232
222
233
223
class NAMFileShareDownloader : public AbstractFileShareDownloader {
@@ -248,7 +238,9 @@ class NAMFileShareDownloader : public AbstractFileShareDownloader {
248
238
{
249
239
QNetworkRequest req = QNetworkRequest (QUrl (sourceUri));
250
240
if (isRanged ()) {
251
- QString range = QString (" bytes=%1-%2" ).arg (QString::number (rangeStart), rangeSize ? QString::number (rangeStart + rangeSize - 1 ) : QString ());
241
+ QString range = QString (" bytes=%1-%2" )
242
+ .arg (QString::number (rangeStart),
243
+ rangeSize ? QString::number (rangeStart + rangeSize - 1 ) : QString ());
252
244
req.setRawHeader (" Range" , range.toLatin1 ());
253
245
}
254
246
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
@@ -289,15 +281,9 @@ class NAMFileShareDownloader : public AbstractFileShareDownloader {
289
281
});
290
282
}
291
283
292
- qint64 bytesAvailable () const
293
- {
294
- return reply ? reply->bytesAvailable () : 0 ;
295
- }
284
+ qint64 bytesAvailable () const { return reply ? reply->bytesAvailable () : 0 ; }
296
285
297
- qint64 read (char *data, qint64 maxSize)
298
- {
299
- return reply ? reply->read (data, maxSize) : 0 ;
300
- }
286
+ qint64 read (char *data, qint64 maxSize) { return reply ? reply->read (data, maxSize) : 0 ; }
301
287
302
288
void abort (bool isFailure, const QString &reason)
303
289
{
@@ -310,10 +296,7 @@ class NAMFileShareDownloader : public AbstractFileShareDownloader {
310
296
}
311
297
}
312
298
313
- bool isConnected () const
314
- {
315
- return reply && reply->isRunning ();
316
- }
299
+ bool isConnected () const { return reply && reply->isRunning (); }
317
300
};
318
301
319
302
class BOBFileShareDownloader : public AbstractFileShareDownloader {
@@ -326,10 +309,8 @@ class BOBFileShareDownloader : public AbstractFileShareDownloader {
326
309
bool connected = false ;
327
310
328
311
public:
329
- BOBFileShareDownloader (PsiAccount *acc, const QString &uri,
330
- const QList<Jid> &jids, QObject *parent) :
331
- AbstractFileShareDownloader (acc, uri, parent),
332
- jids (jids)
312
+ BOBFileShareDownloader (PsiAccount *acc, const QString &uri, const QList<Jid> &jids, QObject *parent) :
313
+ AbstractFileShareDownloader (acc, uri, parent), jids(jids)
333
314
{
334
315
}
335
316
@@ -341,29 +322,27 @@ class BOBFileShareDownloader : public AbstractFileShareDownloader {
341
322
return ;
342
323
}
343
324
// skip cid: from uri and request it
344
- acc->loadBob (jids.first (), sourceUri.mid (4 ), this , [this ](bool success, const QByteArray &data, const QByteArray & /* media type */ ) {
345
- if (destroyed)
346
- return ; // should not happen but who knows.
347
- connected = true ;
348
- if (!success) {
349
- downloadError (tr (" Download using \" Bits Of Binary\" failed" ));
350
- return ;
351
- }
352
- receivedData = data;
353
- if (isRanged ()) { // there is not such a thing like ranged bob
354
- rangeStart = 0 ;
355
- rangeSize = 0 ; // make it not-ranged
356
- }
357
- emit metaDataChanged ();
358
- connected = false ;
359
- emit disconnected ();
360
- });
361
- }
362
-
363
- qint64 bytesAvailable () const
364
- {
365
- return receivedData.size ();
366
- }
325
+ acc->loadBob (jids.first (), sourceUri.mid (4 ), this ,
326
+ [this ](bool success, const QByteArray &data, const QByteArray & /* media type */ ) {
327
+ if (destroyed)
328
+ return ; // should not happen but who knows.
329
+ connected = true ;
330
+ if (!success) {
331
+ downloadError (tr (" Download using \" Bits Of Binary\" failed" ));
332
+ return ;
333
+ }
334
+ receivedData = data;
335
+ if (isRanged ()) { // there is not such a thing like ranged bob
336
+ rangeStart = 0 ;
337
+ rangeSize = 0 ; // make it not-ranged
338
+ }
339
+ emit metaDataChanged ();
340
+ connected = false ;
341
+ emit disconnected ();
342
+ });
343
+ }
344
+
345
+ qint64 bytesAvailable () const { return receivedData.size (); }
367
346
368
347
qint64 read (char *data, qint64 maxSize)
369
348
{
@@ -386,10 +365,7 @@ class BOBFileShareDownloader : public AbstractFileShareDownloader {
386
365
destroyed = true ;
387
366
}
388
367
389
- bool isConnected () const
390
- {
391
- return connected;
392
- }
368
+ bool isConnected () const { return connected; }
393
369
};
394
370
395
371
class FileShareDownloader ::Private : public QObject {
@@ -477,14 +453,19 @@ class FileShareDownloader::Private : public QObject {
477
453
});
478
454
479
455
connect (downloader, &AbstractFileShareDownloader::readyRead, q, &FileShareDownloader::readyRead);
480
- connect (downloader, &AbstractFileShareDownloader::disconnected, q, &FileShareDownloader::disconnected);
456
+ connect (downloader, &AbstractFileShareDownloader::disconnected, q, [this ]() {
457
+ emit q->disconnected ();
458
+ if (!downloader->bytesAvailable ())
459
+ emit q->finished (); // TODO avoid double emit
460
+ });
481
461
482
462
downloader->start ();
483
463
}
484
464
};
485
465
486
- FileShareDownloader::FileShareDownloader (PsiAccount *acc, const QList<XMPP::Hash> &sums, const Jingle::FileTransfer::File &file,
487
- const QList<Jid> &jids, const QStringList &uris, QObject *parent) :
466
+ FileShareDownloader::FileShareDownloader (PsiAccount *acc, const QList<XMPP::Hash> &sums,
467
+ const Jingle::FileTransfer::File &file, const QList<Jid> &jids,
468
+ const QStringList &uris, QObject *parent) :
488
469
QIODevice(parent),
489
470
d(new Private)
490
471
{
@@ -502,10 +483,7 @@ FileShareDownloader::~FileShareDownloader()
502
483
qDebug (" downloader deleted" );
503
484
}
504
485
505
- bool FileShareDownloader::isSuccess () const
506
- {
507
- return d->success ;
508
- }
486
+ bool FileShareDownloader::isSuccess () const { return d->success ; }
509
487
510
488
bool FileShareDownloader::open (QIODevice::OpenMode mode)
511
489
{
@@ -556,10 +534,7 @@ void FileShareDownloader::setRange(qint64 start, qint64 size)
556
534
d->rangeSize = size;
557
535
}
558
536
559
- bool FileShareDownloader::isRanged () const
560
- {
561
- return d->rangeStart > 0 || d->rangeSize > 0 ;
562
- }
537
+ bool FileShareDownloader::isRanged () const { return d->rangeStart > 0 || d->rangeSize > 0 ; }
563
538
564
539
std::tuple<qint64, qint64> FileShareDownloader::range () const
565
540
{
@@ -574,18 +549,16 @@ QString FileShareDownloader::fileName() const
574
549
return QString ();
575
550
}
576
551
577
- const Jingle::FileTransfer::File &FileShareDownloader::jingleFile () const
578
- {
579
- return d->file ;
580
- }
552
+ const Jingle::FileTransfer::File &FileShareDownloader::jingleFile () const { return d->file ; }
581
553
582
554
qint64 FileShareDownloader::readData (char *data, qint64 maxSize)
583
555
{
584
556
if (!d->tmpFile || !d->downloader )
585
557
return 0 ; // wtf?
586
558
587
559
qint64 bytesRead = d->downloader ->read (data, maxSize);
588
- if (d->tmpFile ->write (data, bytesRead) != bytesRead) { // file engine tries to write everything until it's a system error
560
+ if (d->tmpFile ->write (data, bytesRead)
561
+ != bytesRead) { // file engine tries to write everything until it's a system error
589
562
d->downloader ->abort ();
590
563
d->lastError = d->tmpFile ->errorString ();
591
564
return 0 ;
@@ -605,14 +578,8 @@ qint64 FileShareDownloader::writeData(const char *, qint64)
605
578
return 0 ; // not supported
606
579
}
607
580
608
- bool FileShareDownloader::isSequential () const
609
- {
610
- return true ;
611
- }
581
+ bool FileShareDownloader::isSequential () const { return true ; }
612
582
613
- qint64 FileShareDownloader::bytesAvailable () const
614
- {
615
- return d->downloader ? d->downloader ->bytesAvailable () : 0 ;
616
- }
583
+ qint64 FileShareDownloader::bytesAvailable () const { return d->downloader ? d->downloader ->bytesAvailable () : 0 ; }
617
584
618
585
#include " filesharingdownloader.moc"
0 commit comments