Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 by Savoir-faire Linux |
| 3 | * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "accountlistmodel.h" |
| 22 | #include "lrcinstance.h" |
| 23 | |
| 24 | #include <QImage> |
| 25 | #include <QObject> |
| 26 | #include <QPair> |
| 27 | #include <QQuickImageProvider> |
| 28 | #include <QString> |
| 29 | |
| 30 | class QrImageProvider : public QObject, public QQuickImageProvider |
| 31 | { |
| 32 | public: |
| 33 | QrImageProvider() |
| 34 | : QQuickImageProvider(QQuickImageProvider::Image, |
| 35 | QQmlImageProviderBase::ForceAsynchronousImageLoading) |
| 36 | {} |
| 37 | |
| 38 | enum class QrType { Account, Contact }; |
| 39 | |
| 40 | /* |
| 41 | * Id should be string like account_0 (account index), |
| 42 | * or contact_xxx (uid). |
| 43 | * Cannot use getCurrentAccId to replace account index, |
| 44 | * since we need to keep each image id unique. |
| 45 | */ |
| 46 | QPair<QrType, QString> |
| 47 | getIndexFromID(const QString &id) |
| 48 | { |
| 49 | auto list = id.split('_', QString::SkipEmptyParts); |
| 50 | if (list.contains("account")) { |
| 51 | return QPair(QrType::Account, list[1]); |
| 52 | } else if (list.contains("contact") && list.size() > 1) { |
| 53 | /* |
| 54 | * For contact_xxx, xxx is "" initially |
| 55 | */ |
| 56 | auto convInfo = LRCInstance::getConversationFromConvUid(list[1]); |
| 57 | auto contact = LRCInstance::getCurrentAccountInfo().contactModel->getContact( |
| 58 | convInfo.participants.at(0)); |
| 59 | return QPair(QrType::Contact, contact.profileInfo.uri); |
| 60 | } |
| 61 | return QPair(QrType::Account, ""); |
| 62 | } |
| 63 | |
| 64 | QImage |
| 65 | requestImage(const QString &id, QSize *size, const QSize &requestedSize) override |
| 66 | { |
| 67 | Q_UNUSED(size); |
| 68 | |
| 69 | QString uri; |
| 70 | auto indexPair = getIndexFromID(id); |
| 71 | |
| 72 | if (indexPair.first == QrType::Contact) { |
| 73 | uri = indexPair.second; |
| 74 | } else { |
| 75 | if (indexPair.second.isEmpty()) |
| 76 | return QImage(); |
| 77 | |
| 78 | auto accountList = LRCInstance::accountModel().getAccountList(); |
| 79 | auto accountIndex = indexPair.second.toInt(); |
| 80 | if (accountList.size() <= accountIndex) |
| 81 | return QImage(); |
| 82 | |
| 83 | auto &accountInfo = LRCInstance::accountModel().getAccountInfo( |
| 84 | accountList.at(accountIndex)); |
| 85 | uri = accountInfo.profileInfo.uri; |
| 86 | } |
| 87 | |
| 88 | if (!requestedSize.isEmpty()) |
| 89 | return Utils::setupQRCode(uri, 0).scaled(requestedSize, Qt::KeepAspectRatio); |
| 90 | else |
| 91 | return Utils::setupQRCode(uri, 0); |
| 92 | } |
| 93 | }; |