File indexing completed on 2024-04-21 07:41:52

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000-2001 Dawit Alemayehu <adawit@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "authinfo.h"
0009 
0010 #ifndef KIO_ANDROID_STUB
0011 #include <QDBusArgument>
0012 #include <QDBusMetaType>
0013 #endif
0014 #include <QDataStream>
0015 #include <QDir>
0016 #include <QFile>
0017 #include <QTextStream>
0018 
0019 #include <QStandardPaths>
0020 
0021 using namespace KIO;
0022 
0023 //////
0024 
0025 class ExtraField
0026 {
0027 public:
0028     ExtraField()
0029         : flags(AuthInfo::ExtraFieldNoFlags)
0030     {
0031     }
0032 
0033     ExtraField(const ExtraField &other)
0034         : customTitle(other.customTitle)
0035         , flags(other.flags)
0036         , value(other.value)
0037     {
0038     }
0039 
0040     ExtraField &operator=(const ExtraField &other)
0041     {
0042         customTitle = other.customTitle;
0043         flags = other.flags;
0044         value = other.value;
0045         return *this;
0046     }
0047 
0048     QString customTitle; // reserved for future use
0049     AuthInfo::FieldFlags flags;
0050     QVariant value;
0051 };
0052 Q_DECLARE_METATYPE(ExtraField)
0053 
0054 static QDataStream &operator<<(QDataStream &s, const ExtraField &extraField)
0055 {
0056     s << extraField.customTitle;
0057     s << static_cast<int>(extraField.flags);
0058     s << extraField.value;
0059     return s;
0060 }
0061 
0062 static QDataStream &operator>>(QDataStream &s, ExtraField &extraField)
0063 {
0064     s >> extraField.customTitle;
0065     int i;
0066     s >> i;
0067     extraField.flags = AuthInfo::FieldFlags(i);
0068     s >> extraField.value;
0069     return s;
0070 }
0071 
0072 #ifndef KIO_ANDROID_STUB
0073 static QDBusArgument &operator<<(QDBusArgument &argument, const ExtraField &extraField)
0074 {
0075     argument.beginStructure();
0076     argument << extraField.customTitle << static_cast<int>(extraField.flags) << QDBusVariant(extraField.value);
0077     argument.endStructure();
0078     return argument;
0079 }
0080 
0081 static const QDBusArgument &operator>>(const QDBusArgument &argument, ExtraField &extraField)
0082 {
0083     QDBusVariant value;
0084     int flag;
0085 
0086     argument.beginStructure();
0087     argument >> extraField.customTitle >> flag >> value;
0088     argument.endStructure();
0089 
0090     extraField.value = value.variant();
0091     extraField.flags = KIO::AuthInfo::FieldFlags(flag);
0092     return argument;
0093 }
0094 #endif
0095 
0096 class KIO::AuthInfoPrivate
0097 {
0098 public:
0099     QMap<QString, ExtraField> extraFields;
0100 };
0101 
0102 //////
0103 
0104 AuthInfo::AuthInfo()
0105     : d(new AuthInfoPrivate())
0106 {
0107     modified = false;
0108     readOnly = false;
0109     verifyPath = false;
0110     keepPassword = false;
0111     AuthInfo::registerMetaTypes();
0112 }
0113 
0114 AuthInfo::AuthInfo(const AuthInfo &info)
0115     : d(new AuthInfoPrivate())
0116 {
0117     (*this) = info;
0118     AuthInfo::registerMetaTypes();
0119 }
0120 
0121 AuthInfo::~AuthInfo() = default;
0122 
0123 AuthInfo &AuthInfo::operator=(const AuthInfo &info)
0124 {
0125     url = info.url;
0126     username = info.username;
0127     password = info.password;
0128     prompt = info.prompt;
0129     caption = info.caption;
0130     comment = info.comment;
0131     commentLabel = info.commentLabel;
0132     realmValue = info.realmValue;
0133     digestInfo = info.digestInfo;
0134     verifyPath = info.verifyPath;
0135     readOnly = info.readOnly;
0136     keepPassword = info.keepPassword;
0137     modified = info.modified;
0138     d->extraFields = info.d->extraFields;
0139     return *this;
0140 }
0141 
0142 bool AuthInfo::isModified() const
0143 {
0144     return modified;
0145 }
0146 
0147 void AuthInfo::setModified(bool flag)
0148 {
0149     modified = flag;
0150 }
0151 
0152 /////
0153 
0154 void AuthInfo::setExtraField(const QString &fieldName, const QVariant &value)
0155 {
0156     d->extraFields[fieldName].value = value;
0157 }
0158 
0159 void AuthInfo::setExtraFieldFlags(const QString &fieldName, const FieldFlags flags)
0160 {
0161     d->extraFields[fieldName].flags = flags;
0162 }
0163 
0164 QVariant AuthInfo::getExtraField(const QString &fieldName) const
0165 {
0166     const auto it = d->extraFields.constFind(fieldName);
0167     if (it == d->extraFields.constEnd()) {
0168         return QVariant();
0169     }
0170     return it->value;
0171 }
0172 
0173 AuthInfo::FieldFlags AuthInfo::getExtraFieldFlags(const QString &fieldName) const
0174 {
0175     const auto it = d->extraFields.constFind(fieldName);
0176     if (it == d->extraFields.constEnd()) {
0177         return AuthInfo::ExtraFieldNoFlags;
0178     }
0179     return it->flags;
0180 }
0181 
0182 void AuthInfo::registerMetaTypes()
0183 {
0184     qRegisterMetaType<ExtraField>();
0185     qRegisterMetaType<KIO::AuthInfo>();
0186 #ifndef KIO_ANDROID_STUB
0187     qDBusRegisterMetaType<ExtraField>();
0188     qDBusRegisterMetaType<KIO::AuthInfo>();
0189 #endif
0190 }
0191 
0192 /////
0193 
0194 QDataStream &KIO::operator<<(QDataStream &s, const AuthInfo &a)
0195 {
0196     s << quint8(1) << a.url << a.username << a.password << a.prompt << a.caption << a.comment << a.commentLabel << a.realmValue << a.digestInfo << a.verifyPath
0197       << a.readOnly << a.keepPassword << a.modified << a.d->extraFields;
0198     return s;
0199 }
0200 
0201 QDataStream &KIO::operator>>(QDataStream &s, AuthInfo &a)
0202 {
0203     quint8 version;
0204     s >> version >> a.url >> a.username >> a.password >> a.prompt >> a.caption >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo >> a.verifyPath
0205         >> a.readOnly >> a.keepPassword >> a.modified >> a.d->extraFields;
0206     return s;
0207 }
0208 
0209 #ifndef KIO_ANDROID_STUB
0210 QDBusArgument &KIO::operator<<(QDBusArgument &argument, const AuthInfo &a)
0211 {
0212     argument.beginStructure();
0213     argument << quint8(1) << a.url.toString() << a.username << a.password << a.prompt << a.caption << a.comment << a.commentLabel << a.realmValue
0214              << a.digestInfo << a.verifyPath << a.readOnly << a.keepPassword << a.modified << a.d->extraFields;
0215     argument.endStructure();
0216     return argument;
0217 }
0218 
0219 const QDBusArgument &KIO::operator>>(const QDBusArgument &argument, AuthInfo &a)
0220 {
0221     QString url;
0222     quint8 version;
0223 
0224     argument.beginStructure();
0225     argument >> version >> url >> a.username >> a.password >> a.prompt >> a.caption >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
0226         >> a.verifyPath >> a.readOnly >> a.keepPassword >> a.modified >> a.d->extraFields;
0227     argument.endStructure();
0228 
0229     a.url = QUrl(url);
0230     return argument;
0231 }
0232 #endif