Skip to content
Snippets Groups Projects
Select Git revision
  • a3aea50985ad94d1d42cda823b7fb172e877883d
  • master default protected
  • beta/202506161038
  • stable/20250613.0
  • nightly/20250613.0
  • beta/202506101658
  • stable/20250610.0
  • nightly/20250610.0
  • beta/202506091027
  • beta/202506061543
  • nightly/20250605.0
  • beta/202506051039
  • beta/202506051002
  • beta/202506041611
  • beta/202506041335
  • beta/202505231812
  • stable/20250523.0
  • nightly/20250523.0
  • nightly/20250515.0
  • nightly/20250510.0
  • nightly/20250509.1
  • nightly/20250509.0
22 results

qrimageprovider.h

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    qrimageprovider.h 3.44 KiB
    /*
     * Copyright (C) 2020 by Savoir-faire Linux
     * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
     *
     * This program 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.
     *
     * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
     */
    
    #pragma once
    
    #include "quickimageproviderbase.h"
    #include "accountlistmodel.h"
    
    #include <QPair>
    #include <QString>
    
    class QrImageProvider : public QuickImageProviderBase
    {
    public:
        QrImageProvider(LRCInstance* instance = nullptr)
            : QuickImageProviderBase(QQuickImageProvider::Image,
                                     QQmlImageProviderBase::ForceAsynchronousImageLoading,
                                     instance)
        {}
    
        enum class QrType { Account, Contact };
    
        /*
         * Id should be string like account_0 (account index),
         * or contact_xxx (uid).
         * Cannot use getCurrentAccId to replace account index,
         * since we need to keep each image id unique.
         */
        QPair<QrType, QString> getIndexFromID(const QString& id)
        {
            auto list = id.split('_', Qt::SkipEmptyParts);
            if (list.size() < 2)
                return {QrType::Account, {}};
            if (list.contains("account") && list.size() > 1) {
                return {QrType::Account, list[1]};
            } else if (list.contains("contact") && list.size() > 1) {
                // For contact_xxx, xxx is "" initially
                try {
                    const auto& convInfo = lrcInstance_->getConversationFromConvUid(list[1]);
                    if (convInfo.mode == conversation::Mode::ONE_TO_ONE
                        || convInfo.mode == conversation::Mode::NON_SWARM) {
                        auto peerUri = lrcInstance_->getCurrentAccountInfo()
                                           .conversationModel->peersForConversation(convInfo.uid)
                                           .at(0);
                        return {QrType::Contact, peerUri};
                    }
                } catch (...) {
                }
                return {QrType::Contact, {}};
            }
            return {QrType::Account, {}};
        }
    
        QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override
        {
            Q_UNUSED(size);
    
            QString uri;
            auto indexPair = getIndexFromID(id);
    
            if (indexPair.first == QrType::Contact) {
                uri = indexPair.second;
            } else {
                if (indexPair.second.isEmpty())
                    return QImage();
    
                auto accountList = lrcInstance_->accountModel().getAccountList();
                auto accountIndex = indexPair.second.toInt();
                if (accountList.size() <= accountIndex)
                    return QImage();
    
                auto& accountInfo = lrcInstance_->accountModel().getAccountInfo(
                    accountList.at(accountIndex));
                uri = accountInfo.profileInfo.uri;
            }
    
            if (!requestedSize.isEmpty())
                return Utils::setupQRCode(uri, 0).scaled(requestedSize, Qt::KeepAspectRatio);
            else
                return Utils::setupQRCode(uri, 0);
        }
    };