blob: e8822c668e8e2579df4aa3fb16e6e2e071aac4da [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001/*
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
30class QrImageProvider : public QObject, public QQuickImageProvider
31{
32public:
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 */
ababi0b686642020-08-18 17:21:28 +020056
57 auto convModel = LRCInstance::getCurrentConversationModel();
58 auto convInfo = convModel->getConversationForUID(list[1]);
Sébastien Blin1f915762020-08-03 13:27:42 -040059 auto contact = LRCInstance::getCurrentAccountInfo().contactModel->getContact(
60 convInfo.participants.at(0));
61 return QPair(QrType::Contact, contact.profileInfo.uri);
62 }
63 return QPair(QrType::Account, "");
64 }
65
66 QImage
67 requestImage(const QString &id, QSize *size, const QSize &requestedSize) override
68 {
69 Q_UNUSED(size);
70
71 QString uri;
72 auto indexPair = getIndexFromID(id);
73
74 if (indexPair.first == QrType::Contact) {
75 uri = indexPair.second;
76 } else {
77 if (indexPair.second.isEmpty())
78 return QImage();
79
80 auto accountList = LRCInstance::accountModel().getAccountList();
81 auto accountIndex = indexPair.second.toInt();
82 if (accountList.size() <= accountIndex)
83 return QImage();
84
85 auto &accountInfo = LRCInstance::accountModel().getAccountInfo(
86 accountList.at(accountIndex));
87 uri = accountInfo.profileInfo.uri;
88 }
89
90 if (!requestedSize.isEmpty())
91 return Utils::setupQRCode(uri, 0).scaled(requestedSize, Qt::KeepAspectRatio);
92 else
93 return Utils::setupQRCode(uri, 0);
94 }
ababi0b686642020-08-18 17:21:28 +020095};