Skip to content

Commit ff87456

Browse files
committed
wip: proxying shares via http
1 parent 2e694b2 commit ff87456

10 files changed

+252
-286
lines changed

.clang-format

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ AlignAfterOpenBracket: Align
55
AlignConsecutiveAssignments: true
66
AlignConsecutiveDeclarations: true
77
AlignTrailingComments: true
8+
ColumnLimit: 120
9+
CompactNamespaces: true

iris

src/filecache.h

+16-22
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class FileCacheItem : public QObject {
4343
OnDisk = 0x1,
4444
Registered = 0x2,
4545
SessionUndeletable = 0x4 // The item is undeletable by expiration or cache size limits during this session
46-
//Unloadable = 0x8 // another good idea
46+
// Unloadable = 0x8 // another good idea
4747
};
4848

49-
FileCacheItem(FileCache *parent, const XMPP::Hash &itemId, const QVariantMap &metadata,
50-
const QDateTime &dt, unsigned int maxAge, qint64 size,
51-
const QByteArray &data = QByteArray());
49+
FileCacheItem(FileCache *parent, const XMPP::Hash &itemId, const QVariantMap &metadata, const QDateTime &dt,
50+
unsigned int maxAge, qint64 size, const QByteArray &data = QByteArray());
5251

53-
void flushToDisk(); // put data to disk, but not to registry. don't call this directly. FileCache will care about it.
52+
void
53+
flushToDisk(); // put data to disk, but not to registry. don't call this directly. FileCache will care about it.
5454
bool remove() const; // remove file from disk but not from registry. don't call this directly.
5555
void unload(); // drop file to disk, deallocate memory
5656
inline bool inMemory() const;
@@ -106,11 +106,11 @@ class FileCacheItem : public QObject {
106106
class FileCache : public QObject {
107107
Q_OBJECT
108108
public:
109-
static const unsigned int Session = 0; //remove data when application exits
110-
static const unsigned int Forever = -1; //never remove
109+
static constexpr unsigned int Session = 0; // remove data when application exits
110+
static constexpr unsigned int Forever = -1; // never remove
111111

112-
static const unsigned int DefaultMemoryCacheSize = 1 * 1024 * 1024; //1 Mb
113-
static const unsigned int DefaultFileCacheSize = 50 * 1024 * 1024; //50 Mb
112+
static constexpr unsigned int DefaultMemoryCacheSize = 1 * 1024 * 1024; // 1 Mb
113+
static constexpr unsigned int DefaultFileCacheSize = 50 * 1024 * 1024; // 50 Mb
114114

115115
enum SyncPolicy {
116116
InstantFLush, // always flush all data to disk (keeps copy in memory if fit)
@@ -124,19 +124,13 @@ class FileCache : public QObject {
124124

125125
inline QString cacheDir() const { return _cacheDir; }
126126

127-
inline void setMemoryCacheSize(unsigned int size)
128-
{
129-
_memoryCacheSize = size;
130-
}
127+
inline void setMemoryCacheSize(unsigned int size) { _memoryCacheSize = size; }
131128
inline unsigned int memoryCacheSize() const { return _memoryCacheSize; }
132129

133130
inline void setFileCacheSize(unsigned int size) { _fileCacheSize = size; }
134131
inline unsigned int fileCacheSize() const { return _fileCacheSize; }
135132

136-
inline void setDefaultMaxAge(unsigned int maxAge)
137-
{
138-
_defaultMaxAge = maxAge;
139-
}
133+
inline void setDefaultMaxAge(unsigned int maxAge) { _defaultMaxAge = maxAge; }
140134
inline unsigned int defaultMaxAge() const { return _defaultMaxAge; }
141135

142136
inline void setSyncPolicy(SyncPolicy sp) { _syncPolicy = sp; }
@@ -150,10 +144,10 @@ class FileCache : public QObject {
150144
* @param maxAge Session/Forever or just seconds to live
151145
* @return a new cache item. Not yet synchronized to disk
152146
*/
153-
FileCacheItem *append(const XMPP::Hash &id, const QByteArray &data,
154-
const QVariantMap &metadata = QVariantMap(), unsigned int maxAge = Forever);
155-
FileCacheItem *moveToCache(const XMPP::Hash &id, const QFileInfo &file,
156-
const QVariantMap &metadata = QVariantMap(), unsigned int maxAge = Forever);
147+
FileCacheItem *append(const XMPP::Hash &id, const QByteArray &data, const QVariantMap &metadata = QVariantMap(),
148+
unsigned int maxAge = Forever);
149+
FileCacheItem *moveToCache(const XMPP::Hash &id, const QFileInfo &file, const QVariantMap &metadata = QVariantMap(),
150+
unsigned int maxAge = Forever);
157151
void remove(const XMPP::Hash &id, bool needSync = true);
158152

159153
/**
@@ -197,4 +191,4 @@ public slots:
197191
bool _registryChanged;
198192
};
199193

200-
#endif //FILECACHE_H
194+
#endif // FILECACHE_H

src/filesharingdownloader.cpp

+61-94
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class AbstractFileShareDownloader : public QObject {
7373
Jid selectOnlineJid(const QList<Jid> &jids) const
7474
{
7575
for (auto const &j : jids) {
76+
if (j == acc->client()->jid())
77+
continue;
7678
for (UserListItem *u : acc->findRelevant(j)) {
7779
UserResourceList::Iterator rit = u->userResourceList().find(j.resource());
7880
if (rit != u->userResourceList().end())
@@ -84,9 +86,7 @@ class AbstractFileShareDownloader : public QObject {
8486

8587
public:
8688
AbstractFileShareDownloader(PsiAccount *acc, const QString &uri, QObject *parent) :
87-
QObject(parent),
88-
acc(acc),
89-
sourceUri(uri)
89+
QObject(parent), acc(acc), sourceUri(uri)
9090
{
9191
}
9292

@@ -103,10 +103,7 @@ class AbstractFileShareDownloader : public QObject {
103103
rangeSize = length;
104104
}
105105
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); }
110107

111108
signals:
112109
void metaDataChanged();
@@ -125,12 +122,10 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
125122
QList<Jid> jids;
126123

127124
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,
130126
const QList<Jid> &jids, QObject *parent) :
131127
AbstractFileShareDownloader(acc, uri, parent),
132-
file(file),
133-
jids(jids)
128+
file(file), jids(jids)
134129
{
135130
}
136131

@@ -163,8 +158,9 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
163158
return;
164159
}
165160

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));
168164
if (!app) {
169165
downloadError(QString::fromLatin1("Jingle file transfer is disabled"));
170166
return;
@@ -186,7 +182,8 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
186182
rangeStart = qint64(r.offset);
187183
rangeSize = qint64(r.length);
188184
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);
190187
emit metaDataChanged();
191188
});
192189

@@ -199,17 +196,13 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
199196
}
200197
downloadError(tr("Jingle download failed"));
201198
});
202-
}
203199

204-
qint64 bytesAvailable() const
205-
{
206-
return connection ? connection->bytesAvailable() : 0;
200+
session->initiate();
207201
}
208202

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; }
213206

214207
void abort(bool isFailure, const QString &reason)
215208
{
@@ -224,10 +217,7 @@ class JingleFileShareDownloader : public AbstractFileShareDownloader {
224217
}
225218
}
226219

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; }
231221
};
232222

233223
class NAMFileShareDownloader : public AbstractFileShareDownloader {
@@ -248,7 +238,9 @@ class NAMFileShareDownloader : public AbstractFileShareDownloader {
248238
{
249239
QNetworkRequest req = QNetworkRequest(QUrl(sourceUri));
250240
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());
252244
req.setRawHeader("Range", range.toLatin1());
253245
}
254246
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
@@ -289,15 +281,9 @@ class NAMFileShareDownloader : public AbstractFileShareDownloader {
289281
});
290282
}
291283

292-
qint64 bytesAvailable() const
293-
{
294-
return reply ? reply->bytesAvailable() : 0;
295-
}
284+
qint64 bytesAvailable() const { return reply ? reply->bytesAvailable() : 0; }
296285

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; }
301287

302288
void abort(bool isFailure, const QString &reason)
303289
{
@@ -310,10 +296,7 @@ class NAMFileShareDownloader : public AbstractFileShareDownloader {
310296
}
311297
}
312298

313-
bool isConnected() const
314-
{
315-
return reply && reply->isRunning();
316-
}
299+
bool isConnected() const { return reply && reply->isRunning(); }
317300
};
318301

319302
class BOBFileShareDownloader : public AbstractFileShareDownloader {
@@ -326,10 +309,8 @@ class BOBFileShareDownloader : public AbstractFileShareDownloader {
326309
bool connected = false;
327310

328311
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)
333314
{
334315
}
335316

@@ -341,29 +322,27 @@ class BOBFileShareDownloader : public AbstractFileShareDownloader {
341322
return;
342323
}
343324
// 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(); }
367346

368347
qint64 read(char *data, qint64 maxSize)
369348
{
@@ -386,10 +365,7 @@ class BOBFileShareDownloader : public AbstractFileShareDownloader {
386365
destroyed = true;
387366
}
388367

389-
bool isConnected() const
390-
{
391-
return connected;
392-
}
368+
bool isConnected() const { return connected; }
393369
};
394370

395371
class FileShareDownloader::Private : public QObject {
@@ -477,14 +453,19 @@ class FileShareDownloader::Private : public QObject {
477453
});
478454

479455
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+
});
481461

482462
downloader->start();
483463
}
484464
};
485465

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) :
488469
QIODevice(parent),
489470
d(new Private)
490471
{
@@ -502,10 +483,7 @@ FileShareDownloader::~FileShareDownloader()
502483
qDebug("downloader deleted");
503484
}
504485

505-
bool FileShareDownloader::isSuccess() const
506-
{
507-
return d->success;
508-
}
486+
bool FileShareDownloader::isSuccess() const { return d->success; }
509487

510488
bool FileShareDownloader::open(QIODevice::OpenMode mode)
511489
{
@@ -556,10 +534,7 @@ void FileShareDownloader::setRange(qint64 start, qint64 size)
556534
d->rangeSize = size;
557535
}
558536

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; }
563538

564539
std::tuple<qint64, qint64> FileShareDownloader::range() const
565540
{
@@ -574,18 +549,16 @@ QString FileShareDownloader::fileName() const
574549
return QString();
575550
}
576551

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; }
581553

582554
qint64 FileShareDownloader::readData(char *data, qint64 maxSize)
583555
{
584556
if (!d->tmpFile || !d->downloader)
585557
return 0; // wtf?
586558

587559
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
589562
d->downloader->abort();
590563
d->lastError = d->tmpFile->errorString();
591564
return 0;
@@ -605,14 +578,8 @@ qint64 FileShareDownloader::writeData(const char *, qint64)
605578
return 0; // not supported
606579
}
607580

608-
bool FileShareDownloader::isSequential() const
609-
{
610-
return true;
611-
}
581+
bool FileShareDownloader::isSequential() const { return true; }
612582

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; }
617584

618585
#include "filesharingdownloader.moc"

0 commit comments

Comments
 (0)