File indexing completed on 2024-04-28 05:19:26

0001 /*
0002     ktnefproperty.cpp
0003 
0004     SPDX-FileCopyrightText: 2002 Michael Goffioul <kdeprint@swing.be>
0005 
0006     This file is part of KTNEF, the KDE TNEF support library/program.
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 /**
0011  * @file
0012  * This file is part of the API for handling TNEF data and
0013  * defines the KTNEFProperty class.
0014  *
0015  * @author Michael Goffioul
0016  */
0017 
0018 #include "ktnefproperty.h"
0019 #include "mapi.h"
0020 
0021 #include <cctype>
0022 
0023 using namespace KTnef;
0024 
0025 class KTNEFPropertyPrivate
0026 {
0027 public:
0028     int _key = 0;
0029     int _type = 0;
0030     QVariant _value;
0031     QVariant _name;
0032 };
0033 
0034 KTNEFProperty::KTNEFProperty()
0035     : d(new KTNEFPropertyPrivate)
0036 {
0037 }
0038 
0039 KTNEFProperty::KTNEFProperty(int key_, int type_, const QVariant &value_, const QVariant &name_)
0040     : d(new KTNEFPropertyPrivate)
0041 {
0042     d->_key = key_;
0043     d->_type = type_;
0044     d->_value = value_;
0045     d->_name = name_;
0046 }
0047 
0048 KTNEFProperty::KTNEFProperty(const KTNEFProperty &p)
0049     : d(new KTNEFPropertyPrivate)
0050 {
0051     *d = *p.d;
0052 }
0053 
0054 KTNEFProperty::~KTNEFProperty() = default;
0055 
0056 KTNEFProperty &KTNEFProperty::operator=(const KTNEFProperty &other)
0057 {
0058     if (this != &other) {
0059         *d = *other.d;
0060     }
0061 
0062     return *this;
0063 }
0064 
0065 QString KTNEFProperty::keyString() const
0066 {
0067     if (d->_name.isValid()) {
0068         if (d->_name.metaType().id() == QMetaType::QString) {
0069             return d->_name.toString();
0070         } else {
0071             return mapiNamedTagString(d->_name.toUInt(), d->_key);
0072         }
0073     } else {
0074         return mapiTagString(d->_key);
0075     }
0076 }
0077 
0078 QString KTNEFProperty::formatValue(const QVariant &value, bool beautify)
0079 {
0080     if (value.metaType().id() == QMetaType::QByteArray) {
0081         // check the first bytes (up to 8) if they are
0082         // printable characters
0083         QByteArray arr = value.toByteArray();
0084         bool printable = true;
0085         for (int i = qMin(arr.size(), 8) - 1; i >= 0 && printable; i--) {
0086             printable = (isprint(arr[i]) != 0);
0087         }
0088         if (!printable) {
0089             QString s;
0090             int i;
0091             int txtCount = beautify ? qMin(arr.size(), 32) : arr.size();
0092             for (i = 0; i < txtCount; ++i) {
0093                 s.append(QString::asprintf("%02X", (uchar)arr[i]));
0094                 if (beautify) {
0095                     s.append(QLatin1Char(' '));
0096                 }
0097             }
0098             if (i < arr.size()) {
0099                 s.append(QLatin1StringView("... (size=") + QString::number(arr.size()) + QLatin1Char(')'));
0100             }
0101             return s;
0102         }
0103     }
0104     // else if ( value.type() == QVariant::DateTime )
0105     //   return value.toDateTime().toString();
0106     return value.toString();
0107 }
0108 
0109 QString KTNEFProperty::valueString() const
0110 {
0111     return formatValue(d->_value);
0112 }
0113 
0114 int KTNEFProperty::key() const
0115 {
0116     return d->_key;
0117 }
0118 
0119 int KTNEFProperty::type() const
0120 {
0121     return d->_type;
0122 }
0123 
0124 QVariant KTNEFProperty::value() const
0125 {
0126     return d->_value;
0127 }
0128 
0129 QVariant KTNEFProperty::name() const
0130 {
0131     return d->_name;
0132 }
0133 
0134 bool KTNEFProperty::isVector() const
0135 {
0136     return d->_value.metaType().id() == QMetaType::QVariantList;
0137 }