From 41ac3c482982600609409adef7a18e7805f8530d Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Fri, 14 Aug 2020 10:33:42 +0200 Subject: [PATCH] Bring own profile picture to the about page --- qml/components/ImageThumbnail.qml | 76 +++++++++++++++++++++++++++++++ qml/pages/AboutPage.qml | 19 +++++--- src/tdlibreceiver.cpp | 11 +++++ src/tdlibreceiver.h | 2 + src/tdlibwrapper.cpp | 30 ++++++++++++ src/tdlibwrapper.h | 10 +++- 6 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 qml/components/ImageThumbnail.qml diff --git a/qml/components/ImageThumbnail.qml b/qml/components/ImageThumbnail.qml new file mode 100644 index 0000000..3be655c --- /dev/null +++ b/qml/components/ImageThumbnail.qml @@ -0,0 +1,76 @@ +/* + Copyright (C) 2020 Sebastian J. Wolf + + This file is part of Fernschreiber. + + Fernschreiber is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Fernschreiber is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fernschreiber. If not, see . +*/ +import QtQuick 2.5 +import QtGraphicalEffects 1.0 +import Sailfish.Silica 1.0 + +Item { + + id: imageTumbnail + + property variant imageData; + + Image { + + Component.onCompleted: { + if (imageData.local.is_downloading_completed) { + singleImage.source = imageData.local.path; + } else { + tdLibWrapper.downloadFile(imageData.id); + } + } + + Connections { + target: tdLibWrapper + onFileUpdated: { + if (id === imageData.id) { + console.log("File updated, completed? " + fileInformation.local.is_downloading_completed); + imageTumbnail.imageData = fileInformation; + if (imageTumbnail.imageData.local.is_downloading_completed) { + singleImage.source = imageTumbnail.imageData.local.path; + } + } + } + } + + id: singleImage + width: parent.width - Theme.paddingSmall + height: parent.height - Theme.paddingSmall + anchors.centerIn: parent + + fillMode: Image.PreserveAspectCrop + autoTransform: true + asynchronous: true + } + + Image { + id: imageLoadingBackgroundImage + source: "../../images/background" + ( Theme.colorScheme ? "-black" : "-white" ) + ".png" + anchors { + centerIn: parent + } + width: parent.width - 2 * Theme.paddingLarge + height: parent.height - 2 * Theme.paddingLarge + visible: singleImage.status !== Image.Ready + + fillMode: Image.PreserveAspectFit + opacity: 0.15 + } + +} diff --git a/qml/pages/AboutPage.qml b/qml/pages/AboutPage.qml index bfb42ca..2536b7d 100644 --- a/qml/pages/AboutPage.qml +++ b/qml/pages/AboutPage.qml @@ -18,16 +18,14 @@ */ import QtQuick 2.0 import Sailfish.Silica 1.0 +import "../components" import "../js/twemoji.js" as Emoji Page { id: aboutPage allowedOrientations: Orientation.All - function getUserName() { - var userInformation = tdLibWrapper.getUserInformation(); - return Emoji.emojify(userInformation.first_name + " " + userInformation.last_name, Theme.fontSizeSmall); - } + property variant userInformation : tdLibWrapper.getUserInformation(); SilicaFlickable { id: aboutContainer @@ -152,7 +150,7 @@ Page { x: Theme.horizontalPageMargin width: parent.width - ( 2 * Theme.horizontalPageMargin ) horizontalAlignment: Text.AlignHCenter - text: qsTr("Logged in as %1").arg(aboutPage.getUserName()) + text: qsTr("Logged in as %1").arg(Emoji.emojify(aboutPage.userInformation.first_name + " " + aboutPage.userInformation.last_name, Theme.fontSizeSmall)) font.pixelSize: Theme.fontSizeSmall wrapMode: Text.Wrap color: Theme.primaryColor @@ -162,11 +160,20 @@ Page { } } + ImageThumbnail { + imageData: aboutPage.userInformation.profile_photo.big + width: Theme.itemSizeExtraLarge + height: Theme.itemSizeExtraLarge + anchors { + horizontalCenter: parent.horizontalCenter + } + } + Label { x: Theme.horizontalPageMargin width: parent.width - ( 2 * Theme.horizontalPageMargin ) horizontalAlignment: Text.AlignHCenter - text: qsTr("Phone number: +%1").arg(tdLibWrapper.getUserInformation().phone_number) + text: qsTr("Phone number: +%1").arg(aboutPage.userInformation.phone_number) font.pixelSize: Theme.fontSizeSmall wrapMode: Text.Wrap anchors { diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index 6f6abc7..c78395a 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -69,6 +69,10 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc if (objectTypeName == "updateUser") { this->processUpdateUser(receivedInformation); } + + if (objectTypeName == "updateFile") { + this->processUpdateFile(receivedInformation); + } } void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) @@ -105,3 +109,10 @@ void TDLibReceiver::processUpdateUser(const QVariantMap &receivedInformation) qDebug() << "[TDLibReceiver] User was updated: " << userInformation.value("username").toString() << userInformation.value("first_name").toString() << userInformation.value("last_name").toString(); emit userUpdated(userInformation); } + +void TDLibReceiver::processUpdateFile(const QVariantMap &receivedInformation) +{ + QVariantMap fileInformation = receivedInformation.value("file").toMap(); + qDebug() << "[TDLibReceiver] File was updated: " << fileInformation.value("id").toString(); + emit fileUpdated(fileInformation); +} diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 1e1d016..3fee49e 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -41,6 +41,7 @@ signals: void optionUpdated(const QString &optionName, const QVariant &optionValue); void connectionStateChanged(const QString &connectionState); void userUpdated(const QVariantMap &userInformation); + void fileUpdated(const QVariantMap &fileInformation); private: void *tdLibClient; @@ -52,6 +53,7 @@ private: void processUpdateAuthorizationState(const QVariantMap &receivedInformation); void processUpdateConnectionState(const QVariantMap &receivedInformation); void processUpdateUser(const QVariantMap &receivedInformation); + void processUpdateFile(const QVariantMap &receivedInformation); }; #endif // TDLIBRECEIVER_H diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 98cd5b2..85657c7 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -41,8 +41,11 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) connect(this->tdLibReceiver, SIGNAL(optionUpdated(QString, QVariant)), this, SLOT(handleOptionUpdated(QString, QVariant))); connect(this->tdLibReceiver, SIGNAL(connectionStateChanged(QString)), this, SLOT(handleConnectionStateChanged(QString))); connect(this->tdLibReceiver, SIGNAL(userUpdated(QVariantMap)), this, SLOT(handleUserUpdated(QVariantMap))); + connect(this->tdLibReceiver, SIGNAL(fileUpdated(QVariantMap)), this, SLOT(handleFileUpdated(QVariantMap))); this->tdLibReceiver->start(); + + this->setLogVerbosityLevel(); } TDLibWrapper::~TDLibWrapper() @@ -108,6 +111,19 @@ void TDLibWrapper::getChats() this->sendRequest(requestObject); } +void TDLibWrapper::downloadFile(const QString &fileId) +{ + qDebug() << "[TDLibWrapper] Downloading file " << fileId; + QVariantMap requestObject; + requestObject.insert("@type", "downloadFile"); + requestObject.insert("file_id", fileId); + requestObject.insert("synchronous", false); + requestObject.insert("offset", 0); + requestObject.insert("limit", 0); + requestObject.insert("priority", 1); + this->sendRequest(requestObject); +} + QVariantMap TDLibWrapper::getUserInformation() { return this->userInformation; @@ -208,6 +224,11 @@ void TDLibWrapper::handleUserUpdated(const QVariantMap &userInformation) } } +void TDLibWrapper::handleFileUpdated(const QVariantMap &userInformation) +{ + emit fileUpdated(userInformation.value("id").toInt(), userInformation); +} + void TDLibWrapper::setInitialParameters() { qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib"; @@ -240,3 +261,12 @@ void TDLibWrapper::setEncryptionKey() this->sendRequest(requestObject); } +void TDLibWrapper::setLogVerbosityLevel() +{ + qDebug() << "[TDLibWrapper] Setting log verbosity level to something less chatty"; + QVariantMap requestObject; + requestObject.insert("@type", "setLogVerbosityLevel"); + requestObject.insert("new_verbosity_level", 2); + this->sendRequest(requestObject); +} + diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 69086df..d9b5abe 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -58,20 +58,24 @@ public: }; Q_ENUM(ConnectionState) - Q_INVOKABLE void sendRequest(const QVariantMap &requestObject); Q_INVOKABLE QString getVersion(); Q_INVOKABLE TDLibWrapper::AuthorizationState getAuthorizationState(); Q_INVOKABLE TDLibWrapper::ConnectionState getConnectionState(); + Q_INVOKABLE QVariantMap getUserInformation(); + + // Direct TDLib functions + Q_INVOKABLE void sendRequest(const QVariantMap &requestObject); Q_INVOKABLE void setAuthenticationPhoneNumber(const QString &phoneNumber); Q_INVOKABLE void setAuthenticationCode(const QString &authenticationCode); Q_INVOKABLE void getChats(); - Q_INVOKABLE QVariantMap getUserInformation(); + Q_INVOKABLE void downloadFile(const QString &fileId); signals: void versionDetected(const QString &version); void authorizationStateChanged(const TDLibWrapper::AuthorizationState &authorizationState); void optionUpdated(const QString &optionName, const QVariant &optionValue); void connectionStateChanged(const TDLibWrapper::ConnectionState &connectionState); + void fileUpdated(const int fileId, const QVariantMap fileInformation); public slots: void handleVersionDetected(const QString &version); @@ -79,6 +83,7 @@ public slots: void handleOptionUpdated(const QString &optionName, const QVariant &optionValue); void handleConnectionStateChanged(const QString &connectionState); void handleUserUpdated(const QVariantMap &userInformation); + void handleFileUpdated(const QVariantMap &userInformation); private: void *tdLibClient; @@ -91,6 +96,7 @@ private: void setInitialParameters(); void setEncryptionKey(); + void setLogVerbosityLevel(); };