Skip to content

Commit

Permalink
Many updates for SecretChats
Browse files Browse the repository at this point in the history
Fix ugly cache bugs
Add addContact fuction to the dialogList
  • Loading branch information
Bardia Daneshvar committed Oct 4, 2016
1 parent db5ac2d commit 2ab9e99
Show file tree
Hide file tree
Showing 26 changed files with 753 additions and 111 deletions.
89 changes: 83 additions & 6 deletions private/telegramdownloadhandler.cpp
Expand Up @@ -181,6 +181,20 @@ TelegramFileLocation *TelegramDownloadHandler::findTarget(QObject *source, int *
const ObjectType objectType = findObjectType(source);
switch(static_cast<int>(objectType))
{
case TypeObjectTQmlMessage:
{
TQmlMessageObject *msg = static_cast<TQmlMessageObject*>(source);
if(msg->secretChatMessage())
object = msg->secretChatMessage();
else
if(msg->media()->classType() != MessageMediaObject::TypeMessageMediaEmpty)
object = msg->media();
else
if(msg->action()->classType() != MessageActionObject::TypeMessageActionEmpty)
object = msg->action();
}
break;

case TypeObjectMessage:
{
MessageObject *msg = static_cast<MessageObject*>(source);
Expand All @@ -192,6 +206,14 @@ TelegramFileLocation *TelegramDownloadHandler::findTarget(QObject *source, int *
}
break;

case TypeObjectSecretChatMessage:
{
SecretChatMessageObject *smsg = static_cast<SecretChatMessageObject*>(source);
p->thumb_location = locationOf(smsg, true);
return locationOf(smsg);
}
break;

case TypeObjectDialog:
{
DialogObject *dlg = static_cast<DialogObject*>(source);
Expand Down Expand Up @@ -325,9 +347,15 @@ TelegramDownloadHandler::ObjectType TelegramDownloadHandler::findObjectType(QObj
if(!obj)
return objectType;

if(qobject_cast<TQmlMessageObject*>(obj))
objectType = TypeObjectTQmlMessage;
else
if(qobject_cast<MessageObject*>(obj))
objectType = TypeObjectMessage;
else
if(qobject_cast<SecretChatMessageObject*>(obj))
objectType = TypeObjectSecretChatMessage;
else
if(qobject_cast<PeerObject*>(obj))
objectType = TypeObjectPeer;
else
Expand Down Expand Up @@ -417,18 +445,20 @@ TelegramFileLocation *TelegramDownloadHandler::locationOf(DocumentObject *obj)
{
case DocumentAttribute::typeDocumentAttributeAnimated:
break;
case DocumentAttribute::typeDocumentAttributeAudioSecret23:
case DocumentAttribute::typeDocumentAttributeAudio:
break;
case DocumentAttribute::typeDocumentAttributeFilename:
break;
case DocumentAttribute::typeDocumentAttributeImageSize:
tfl->setImageSize(QSizeF(attr.w(), attr.h()));
break;
case DocumentAttribute::typeDocumentAttributeStickerSecret23:
case DocumentAttribute::typeDocumentAttributeSticker:
break;
case DocumentAttribute::typeDocumentAttributeVideo:
tfl->setImageSize(QSizeF(attr.w(), attr.h()));
break;
case DocumentAttribute::typeDocumentAttributeFilename:
break;
}
}

Expand Down Expand Up @@ -463,16 +493,63 @@ TelegramFileLocation *TelegramDownloadHandler::locationOf(PhotoObject *obj, bool

TelegramFileLocation *TelegramDownloadHandler::locationOf(PhotoSizeObject *obj)
{
TelegramFileLocation *res = locationOf(obj->location());
if(res)
TelegramFileLocation *res = 0;
if(!obj->bytes().isEmpty())
{
res = new TelegramFileLocation(p->engine);
res->setBytes(obj->bytes());
res->setImageSize( QSizeF(obj->w(), obj->h()) );
}
else
{
res->setSize(obj->size());
res->setImageSize(QSizeF(obj->w(), obj->h()));
res = locationOf(obj->location());
if(res)
{
res->setSize(obj->size());
res->setImageSize(QSizeF(obj->w(), obj->h()));
}
}

return res;
}

TelegramFileLocation *TelegramDownloadHandler::locationOf(SecretChatMessageObject *obj, bool thumbnail)
{
if(!p->engine)
return 0;

if(thumbnail) {
DecryptedMessageMediaObject *media = obj->decryptedMessage()->media();
QByteArray bytes = media->thumbBytes();
if(bytes.isEmpty())
return 0;

PhotoSizeObject size(PhotoSize::typePhotoCachedSize);
size.setBytes(media->thumbBytes());
size.setH(media->h());
size.setW(media->w());

return locationOf(&size);
} else {
const QByteArray &hash = obj->attachment()->core().getHash();
TelegramFileLocation *tfl = TelegramDownloadHandlerPrivate::locations.value(p->engine).value(hash);
if(tfl)
return tfl;

EncryptedFileObject *file = obj->attachment();

tfl = new TelegramFileLocation(p->engine);
tfl->setId(file->id());
tfl->setAccessHash(file->accessHash());
tfl->setIv(obj->decryptedMessage()->media()->iv());
tfl->setKey(obj->decryptedMessage()->media()->key());
tfl->setClassType(InputFileLocationObject::TypeInputEncryptedFileLocation);

registerLocation(tfl, hash);
return tfl;
}
}

void TelegramDownloadHandler::registerLocation(TelegramFileLocation *loc, const QByteArray &hash)
{
TelegramDownloadHandlerPrivate::locations[p->engine][hash] = loc;
Expand Down
3 changes: 3 additions & 0 deletions private/telegramdownloadhandler.h
Expand Up @@ -33,7 +33,9 @@ class TELEGRAMQMLSHARED_EXPORT TelegramDownloadHandler : public TqObject
public:
enum ObjectType {
TypeObjectEmpty,
TypeObjectTQmlMessage,
TypeObjectMessage,
TypeObjectSecretChatMessage,
TypeObjectPeer,
TypeObjectInputPeer,
TypeObjectDialog,
Expand Down Expand Up @@ -111,6 +113,7 @@ public Q_SLOTS:
TelegramFileLocation *locationOf(DocumentObject *obj);
TelegramFileLocation *locationOf(PhotoObject *obj, bool thumbnail = false);
TelegramFileLocation *locationOf(PhotoSizeObject *obj);
TelegramFileLocation *locationOf(SecretChatMessageObject *obj, bool thumbnail = false);

protected:
void registerLocation(TelegramFileLocation *loc, const QByteArray &hash);
Expand Down
76 changes: 72 additions & 4 deletions private/telegramfilelocation.cpp
Expand Up @@ -25,6 +25,9 @@ class TelegramFileLocationPrivate
qint32 downloadedSize;
qint32 downloadTotal;
InputFileLocationObject *location;
QByteArray key;
QByteArray iv;
QByteArray bytes;
QPointer<TelegramEngine> engine;
bool downloading;
QString destination;
Expand Down Expand Up @@ -119,6 +122,34 @@ void TelegramFileLocation::setId(const qint64 &id)
p->location->setId(id);
}

QByteArray TelegramFileLocation::key() const
{
return p->key;
}

void TelegramFileLocation::setKey(const QByteArray &key)
{
if(p->key == key)
return;

p->key = key;
Q_EMIT keyChanged();
}

QByteArray TelegramFileLocation::iv() const
{
return p->iv;
}

void TelegramFileLocation::setIv(const QByteArray &iv)
{
if(p->iv == iv)
return;

p->iv = iv;
Q_EMIT ivChanged();
}

qint32 TelegramFileLocation::size() const
{
return p->size;
Expand Down Expand Up @@ -147,6 +178,20 @@ void TelegramFileLocation::setImageSize(const QSizeF &imageSize)
Q_EMIT imageSizeChanged();
}

void TelegramFileLocation::setBytes(const QByteArray &bytes)
{
if(p->bytes == bytes)
return;

p->bytes = bytes;
Q_EMIT bytesChanged();
}

QByteArray TelegramFileLocation::bytes() const
{
return p->bytes;
}

qint32 TelegramFileLocation::downloadedSize() const
{
return p->downloadedSize;
Expand Down Expand Up @@ -234,7 +279,7 @@ QString TelegramFileLocation::getLocation(bool *uploading) const
QList<TelegramUploadHandler*> uploadHandlers = TelegramUploadHandler::getItems(p->engine, 0);
Q_FOREACH(TelegramUploadHandler *handler, uploadHandlers)
{
MessageObject *msg = handler->result();
TQmlMessageObject *msg = handler->result();
if(!msg)
continue;

Expand All @@ -249,6 +294,9 @@ QString TelegramFileLocation::getLocation(bool *uploading) const
const QString profilePath = p->engine->configDirectory() + "/" + p->engine->phoneNumber() + "/downloads/";
QDir().mkpath(profilePath);

if(p->location->core().getHash().toHex() == "558c51d04804ab2dec09d5b18aa2bb4a")
return "";

return profilePath + p->location->core().getHash().toHex();
}

Expand All @@ -261,6 +309,9 @@ bool TelegramFileLocation::download()

bool uploading = false;
const QString location = getLocation(&uploading);
if(location.isEmpty())
return false;

if(QFileInfo::exists(location))
{
const qint64 fsize = QFileInfo(location).size();
Expand All @@ -285,12 +336,23 @@ bool TelegramFileLocation::download()
return false;
}

if(!p->bytes.isEmpty())
{
p->file->write(p->bytes);
p->file->close();
delete p->file;
setDownloadTotal(p->bytes.size());
setDownloadedSize(downloadTotal());
setDestination(location);
Q_EMIT finished();
return true;
}

setDownloadTotal(size());
setDownloading(true);

DEFINE_DIS;
Telegram *tg = p->engine->telegram();
p->downloadFileId = tg->uploadGetFile(p->location->core(), p->size, p->dcId, [this, dis](TG_UPLOAD_GET_FILE_CUSTOM_CALLBACK){
Telegram::Callback<UploadGetFile> callBack = [this, dis](TG_UPLOAD_GET_FILE_CUSTOM_CALLBACK){
Q_UNUSED(msgId)
if(!dis || !p->file)
return;
Expand Down Expand Up @@ -339,7 +401,13 @@ bool TelegramFileLocation::download()
setDownloadedSize(result.downloaded());
break;
}
});
};

Telegram *tg = p->engine->telegram();
if(p->key.isEmpty() || p->iv.isEmpty())
p->downloadFileId = tg->uploadGetFile(p->location->core(), p->size, p->dcId, callBack);
else
p->downloadFileId = tg->uploadGetFile(p->location->core(), p->size, p->dcId, p->key, p->iv, callBack);

return true;
}
Expand Down
15 changes: 15 additions & 0 deletions private/telegramfilelocation.h
Expand Up @@ -18,6 +18,9 @@ class TELEGRAMQMLSHARED_EXPORT TelegramFileLocation : public TqObject
Q_PROPERTY(qint64 volumeId READ volumeId WRITE setVolumeId NOTIFY volumeIdChanged)
Q_PROPERTY(qint64 accessHash READ accessHash WRITE setAccessHash NOTIFY accessHashChanged)
Q_PROPERTY(qint64 id READ id WRITE setId NOTIFY idChanged)
Q_PROPERTY(QByteArray key READ key WRITE setKey NOTIFY keyChanged)
Q_PROPERTY(QByteArray iv READ iv WRITE setIv NOTIFY ivChanged)
Q_PROPERTY(QByteArray bytes READ bytes WRITE setBytes NOTIFY bytesChanged)
Q_PROPERTY(qint32 size READ size WRITE setSize NOTIFY sizeChanged)
Q_PROPERTY(QSizeF imageSize READ imageSize WRITE setImageSize NOTIFY imageSizeChanged)
Q_PROPERTY(qint32 downloadedSize READ downloadedSize NOTIFY downloadedSizeChanged)
Expand Down Expand Up @@ -50,12 +53,21 @@ class TELEGRAMQMLSHARED_EXPORT TelegramFileLocation : public TqObject
qint64 id() const;
void setId(const qint64 &id);

QByteArray key() const;
void setKey(const QByteArray &key);

QByteArray iv() const;
void setIv(const QByteArray &iv);

qint32 size() const;
void setSize(const qint32 &size);

QSizeF imageSize() const;
void setImageSize(const QSizeF &imageSize);

void setBytes(const QByteArray &bytes);
QByteArray bytes() const;

qint32 downloadedSize() const;
qint32 downloadTotal() const;
bool downloading() const;
Expand All @@ -77,6 +89,9 @@ class TELEGRAMQMLSHARED_EXPORT TelegramFileLocation : public TqObject
void volumeIdChanged();
void accessHashChanged();
void idChanged();
void keyChanged();
void ivChanged();
void bytesChanged();
void sizeChanged();
void imageSizeChanged();
void downloadedSizeChanged();
Expand Down
18 changes: 9 additions & 9 deletions private/telegramuploadhandler.cpp
Expand Up @@ -54,9 +54,9 @@ class TelegramUploadHandlerPrivate
qint32 totalSize;
qint32 transfaredSize;

TelegramSharedPointer<MessageObject> result;
TelegramSharedPointer<MessageObject> target;
TelegramSharedPointer<MessageObject> replyTo;
TelegramSharedPointer<TQmlMessageObject> result;
TelegramSharedPointer<TQmlMessageObject> target;
TelegramSharedPointer<TQmlMessageObject> replyTo;
QPointer<ReplyMarkupObject> replyMarkup;

TelegramThumbnailer *thumbnailer;
Expand Down Expand Up @@ -246,12 +246,12 @@ int TelegramUploadHandler::sendFileType() const
PROPERTY_GET_TRY(sendFileType);
}

void TelegramUploadHandler::setReplyTo(MessageObject *replyTo)
void TelegramUploadHandler::setReplyTo(TQmlMessageObject *replyTo)
{
PROPERTY_SET_TRY(replyTo);
}

MessageObject *TelegramUploadHandler::replyTo() const
TQmlMessageObject *TelegramUploadHandler::replyTo() const
{
PROPERTY_GET_TRY(replyTo);
}
Expand Down Expand Up @@ -286,17 +286,17 @@ QByteArray TelegramUploadHandler::fakeKey() const
PROPERTY_GET_TRY(fakeKey);
}

void TelegramUploadHandler::setTarget(MessageObject *target)
void TelegramUploadHandler::setTarget(TQmlMessageObject *target)
{
PROPERTY_SET_TRY(target);
}

MessageObject *TelegramUploadHandler::target() const
TQmlMessageObject *TelegramUploadHandler::target() const
{
PROPERTY_GET_TRY(target);
}

MessageObject *TelegramUploadHandler::result() const
TQmlMessageObject *TelegramUploadHandler::result() const
{
PROPERTY_GET_TRY(result);
}
Expand All @@ -307,7 +307,7 @@ void TelegramUploadHandler::setResult(const Message &message)
return;
TelegramSharedDataManager *tsdm = p->engine->sharedData();
if(!tsdm)
p->result = new MessageObject(message);
p->result = new TQmlMessageObject(message);
else
p->result = tsdm->insertMessage(message);

Expand Down

0 comments on commit 2ab9e99

Please sign in to comment.