File indexing completed on 2024-04-21 05:50:37

0001 /*
0002     SPDX-FileCopyrightText: 2008, 2009, 2010, 2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "KGpgUatNode.h"
0007 
0008 #include "gpgproc.h"
0009 #include "KGpgKeyNode.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QDir>
0014 #include <QFile>
0015 #include <QFileInfo>
0016 
0017 class KGpgUatNodePrivate {
0018 public:
0019     KGpgUatNodePrivate(const KGpgKeyNode *parent, const unsigned int index, const QStringList &sl);
0020 
0021     const QString m_idx;
0022     const QPixmap m_pixmap;
0023     QDateTime m_creation;
0024 
0025 private:
0026     static QPixmap loadImage(const KGpgKeyNode *parent, const QString &index);
0027 };
0028 
0029 KGpgUatNodePrivate::KGpgUatNodePrivate(const KGpgKeyNode *parent, const unsigned int index, const QStringList &sl)
0030     : m_idx(QString::number(index)),
0031     m_pixmap(loadImage(parent, m_idx))
0032 {
0033     if (sl.count() < 6)
0034         return;
0035     m_creation = QDateTime::fromSecsSinceEpoch(sl.at(5).toUInt());
0036 }
0037 
0038 QPixmap
0039 KGpgUatNodePrivate::loadImage(const KGpgKeyNode *parent, const QString &index)
0040 {
0041     QPixmap pixmap;
0042 #ifdef Q_OS_WIN     //krazy:exclude=cpp
0043     const QString pgpgoutput = QLatin1String("cmd /C \"echo %I\"");
0044 #else
0045     const QString pgpgoutput = QLatin1String("echo %I");
0046 #endif
0047 
0048     GPGProc workProcess;
0049     workProcess <<
0050             QLatin1String("--no-greeting") <<
0051             QLatin1String("--status-fd=2") <<
0052             QLatin1String("--photo-viewer") << pgpgoutput <<
0053             QLatin1String("--edit-key") << parent->getFingerprint() <<
0054             QLatin1String( "uid" ) << index <<
0055             QLatin1String( "showphoto" ) <<
0056             QLatin1String( "quit" );
0057 
0058     workProcess.start();
0059     workProcess.waitForFinished();
0060     if (workProcess.exitCode() != 0)
0061         return pixmap;
0062 
0063     QString tmpfile;
0064     if (workProcess.readln(tmpfile) < 0)
0065         return pixmap;
0066 
0067     QFile fname(tmpfile);
0068     pixmap.load(fname.fileName());
0069     fname.remove();
0070     QDir dir;
0071     dir.rmdir(QFileInfo(fname).path());
0072 
0073     return pixmap;
0074 }
0075 
0076 KGpgUatNode::KGpgUatNode(KGpgKeyNode *parent, const unsigned int index, const QStringList &sl)
0077     : KGpgSignableNode(parent),
0078     d_ptr(new KGpgUatNodePrivate(parent, index, sl))
0079 {
0080 }
0081 
0082 KGpgUatNode::~KGpgUatNode()
0083 {
0084     delete d_ptr;
0085 }
0086 
0087 QString
0088 KGpgUatNode::getName() const
0089 {
0090     return i18n("Photo id");
0091 }
0092 
0093 QString
0094 KGpgUatNode::getSize() const
0095 {
0096     const Q_D(KGpgUatNode);
0097 
0098     return QString::number(d->m_pixmap.width()) + QLatin1Char( 'x' ) + QString::number(d->m_pixmap.height());
0099 }
0100 
0101 QDateTime
0102 KGpgUatNode::getCreation() const
0103 {
0104     const Q_D(KGpgUatNode);
0105 
0106     return d->m_creation;
0107 }
0108 
0109 KGpgKeyNode *
0110 KGpgUatNode::getParentKeyNode() const
0111 {
0112     return m_parent->toKeyNode();
0113 }
0114 
0115 void
0116 KGpgUatNode::readChildren()
0117 {
0118 }
0119 
0120 KgpgCore::KgpgItemType
0121 KGpgUatNode::getType() const
0122 {
0123     return KgpgCore::ITYPE_UAT;
0124 }
0125 
0126 KgpgCore::KgpgKeyTrust
0127 KGpgUatNode::getTrust() const
0128 {
0129     return KgpgCore::TRUST_NOKEY;
0130 }
0131 
0132 const QPixmap &
0133 KGpgUatNode::getPixmap() const
0134 {
0135     const Q_D(KGpgUatNode);
0136 
0137     return d->m_pixmap;
0138 }
0139 
0140 QString
0141 KGpgUatNode::getId() const
0142 {
0143     const Q_D(KGpgUatNode);
0144 
0145     return d->m_idx;
0146 }
0147 
0148 KGpgKeyNode *
0149 KGpgUatNode::getKeyNode(void)
0150 {
0151     return getParentKeyNode()->toKeyNode();
0152 }
0153 
0154 const KGpgKeyNode *
0155 KGpgUatNode::getKeyNode(void) const
0156 {
0157     return getParentKeyNode()->toKeyNode();
0158 }