File indexing completed on 2024-06-23 05:14:16

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/input_p.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include "cached.h"
0013 #include "input.h"
0014 
0015 #include <KLocalizedString>
0016 #include <QIODevice>
0017 #include <QString>
0018 
0019 namespace Kleo
0020 {
0021 
0022 class InputImplBase : public Input
0023 {
0024 public:
0025     InputImplBase()
0026         : Input()
0027         , m_customLabel()
0028         , m_defaultLabel()
0029     {
0030     }
0031 
0032     QString label() const override
0033     {
0034         return m_customLabel.isEmpty() ? m_defaultLabel : m_customLabel;
0035     }
0036 
0037     void setDefaultLabel(const QString &l)
0038     {
0039         m_defaultLabel = l;
0040     }
0041 
0042     void setLabel(const QString &l) override
0043     {
0044         m_customLabel = l;
0045     }
0046 
0047     QString errorString() const override
0048     {
0049         if (m_errorString.dirty()) {
0050             m_errorString = doErrorString();
0051         }
0052         return m_errorString;
0053     }
0054 
0055 private:
0056     virtual QString doErrorString() const
0057     {
0058         if (const std::shared_ptr<QIODevice> io = ioDevice()) {
0059             return io->errorString();
0060         } else {
0061             return i18n("No input device");
0062         }
0063     }
0064 
0065 private:
0066     QString m_customLabel;
0067     QString m_defaultLabel;
0068     mutable cached<QString> m_errorString;
0069 };
0070 
0071 }