KIO

authinfo.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000-2001 Dawit Alemayehu <adawit@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "authinfo.h"
9
10#ifdef WITH_QTDBUS
11#include <QDBusArgument>
12#include <QDBusMetaType>
13#endif
14#include <QDataStream>
15#include <QDir>
16#include <QFile>
17#include <QTextStream>
18
19#include <QStandardPaths>
20
21using namespace KIO;
22
23//////
24
25class ExtraField
26{
27public:
28 ExtraField()
29 : flags(AuthInfo::ExtraFieldNoFlags)
30 {
31 }
32
33 ExtraField(const ExtraField &other)
34 : customTitle(other.customTitle)
35 , flags(other.flags)
36 , value(other.value)
37 {
38 }
39
40 ExtraField &operator=(const ExtraField &other)
41 {
42 customTitle = other.customTitle;
43 flags = other.flags;
44 value = other.value;
45 return *this;
46 }
47
48 QString customTitle; // reserved for future use
50 QVariant value;
51};
52Q_DECLARE_METATYPE(ExtraField)
53
54static QDataStream &operator<<(QDataStream &s, const ExtraField &extraField)
55{
56 s << extraField.customTitle;
57 s << static_cast<int>(extraField.flags);
58 s << extraField.value;
59 return s;
60}
61
62static QDataStream &operator>>(QDataStream &s, ExtraField &extraField)
63{
64 s >> extraField.customTitle;
65 int i;
66 s >> i;
67 extraField.flags = AuthInfo::FieldFlags(i);
68 s >> extraField.value;
69 return s;
70}
71
72#ifdef WITH_QTDBUS
73static QDBusArgument &operator<<(QDBusArgument &argument, const ExtraField &extraField)
74{
75 argument.beginStructure();
76 argument << extraField.customTitle << static_cast<int>(extraField.flags) << QDBusVariant(extraField.value);
77 argument.endStructure();
78 return argument;
79}
80
81static const QDBusArgument &operator>>(const QDBusArgument &argument, ExtraField &extraField)
82{
83 QDBusVariant value;
84 int flag;
85
86 argument.beginStructure();
87 argument >> extraField.customTitle >> flag >> value;
88 argument.endStructure();
89
90 extraField.value = value.variant();
91 extraField.flags = KIO::AuthInfo::FieldFlags(flag);
92 return argument;
93}
94#endif
95
96class KIO::AuthInfoPrivate
97{
98public:
99 QMap<QString, ExtraField> extraFields;
100};
101
102//////
103
105 : d(new AuthInfoPrivate())
106{
107 modified = false;
108 readOnly = false;
109 verifyPath = false;
110 keepPassword = false;
112}
113
115 : d(new AuthInfoPrivate())
116{
117 (*this) = info;
119}
120
121AuthInfo::~AuthInfo() = default;
122
124{
125 url = info.url;
126 username = info.username;
127 password = info.password;
128 prompt = info.prompt;
129 caption = info.caption;
130 comment = info.comment;
132 realmValue = info.realmValue;
133 digestInfo = info.digestInfo;
134 verifyPath = info.verifyPath;
135 readOnly = info.readOnly;
137 modified = info.modified;
138 d->extraFields = info.d->extraFields;
139 return *this;
140}
141
143{
144 return modified;
145}
146
148{
149 modified = flag;
150}
151
152/////
153
154void AuthInfo::setExtraField(const QString &fieldName, const QVariant &value)
155{
156 d->extraFields[fieldName].value = value;
157}
158
159void AuthInfo::setExtraFieldFlags(const QString &fieldName, const FieldFlags flags)
160{
161 d->extraFields[fieldName].flags = flags;
162}
163
165{
166 const auto it = d->extraFields.constFind(fieldName);
167 if (it == d->extraFields.constEnd()) {
168 return QVariant();
169 }
170 return it->value;
171}
172
174{
175 const auto it = d->extraFields.constFind(fieldName);
176 if (it == d->extraFields.constEnd()) {
177 return AuthInfo::ExtraFieldNoFlags;
178 }
179 return it->flags;
180}
181
183{
184 qRegisterMetaType<ExtraField>();
185 qRegisterMetaType<KIO::AuthInfo>();
186#ifdef WITH_QTDBUS
187 qDBusRegisterMetaType<ExtraField>();
188 qDBusRegisterMetaType<KIO::AuthInfo>();
189#endif
190}
191
192/////
193
194QDataStream &KIO::operator<<(QDataStream &s, const AuthInfo &a)
195{
196 s << quint8(1) << a.url << a.username << a.password << a.prompt << a.caption << a.comment << a.commentLabel << a.realmValue << a.digestInfo << a.verifyPath
197 << a.readOnly << a.keepPassword << a.modified << a.d->extraFields;
198 return s;
199}
200
201QDataStream &KIO::operator>>(QDataStream &s, AuthInfo &a)
202{
203 quint8 version;
204 s >> version >> a.url >> a.username >> a.password >> a.prompt >> a.caption >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo >> a.verifyPath
205 >> a.readOnly >> a.keepPassword >> a.modified >> a.d->extraFields;
206 return s;
207}
208
209#ifdef WITH_QTDBUS
210QDBusArgument &KIO::operator<<(QDBusArgument &argument, const AuthInfo &a)
211{
212 argument.beginStructure();
213 argument << quint8(1) << a.url.toString() << a.username << a.password << a.prompt << a.caption << a.comment << a.commentLabel << a.realmValue
214 << a.digestInfo << a.verifyPath << a.readOnly << a.keepPassword << a.modified << a.d->extraFields;
215 argument.endStructure();
216 return argument;
217}
218
219const QDBusArgument &KIO::operator>>(const QDBusArgument &argument, AuthInfo &a)
220{
221 QString url;
222 quint8 version;
223
224 argument.beginStructure();
225 argument >> version >> url >> a.username >> a.password >> a.prompt >> a.caption >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
226 >> a.verifyPath >> a.readOnly >> a.keepPassword >> a.modified >> a.d->extraFields;
227 argument.endStructure();
228
229 a.url = QUrl(url);
230 return argument;
231}
232#endif
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
Definition authinfo.h:50
bool isModified() const
Use this method to check if the object was modified.
Definition authinfo.cpp:142
void setExtraFieldFlags(const QString &fieldName, const FieldFlags flags)
Set Extra Field Flags.
Definition authinfo.cpp:159
QString digestInfo
Field to store any extra authentication information for protocols that need it.
Definition authinfo.h:189
QString realmValue
A unique identifier that allows caching of multiple passwords for different resources in the same ser...
Definition authinfo.h:179
void setModified(bool flag)
Use this method to indicate that this object has been modified.
Definition authinfo.cpp:147
AuthInfo & operator=(const AuthInfo &info)
Custom assignment operator.
Definition authinfo.cpp:123
bool verifyPath
Flag that, if set, indicates whether a path match should be performed when requesting for cached auth...
Definition authinfo.h:202
~AuthInfo()
Destructor.
QUrl url
The URL for which authentication is to be stored.
Definition authinfo.h:100
FieldFlags
Flags for extra fields.
Definition authinfo.h:227
bool keepPassword
Flag to indicate the persistence of the given password.
Definition authinfo.h:222
AuthInfo()
Default constructor.
Definition authinfo.cpp:104
QVariant getExtraField(const QString &fieldName) const
Get Extra Field Value Check QVariant::isValid() to find out if the field exists.
Definition authinfo.cpp:164
QString comment
Additional comment to be displayed when prompting the user for authentication information.
Definition authinfo.h:156
bool readOnly
Flag which if set forces the username field to be read-only.
Definition authinfo.h:209
QString caption
The text to displayed in the title bar of the password prompting dialog.
Definition authinfo.h:132
AuthInfo::FieldFlags getExtraFieldFlags(const QString &fieldName) const
Get Extra Field Flags.
Definition authinfo.cpp:173
void setExtraField(const QString &fieldName, const QVariant &value)
Set Extra Field Value.
Definition authinfo.cpp:154
QString username
This is required for caching.
Definition authinfo.h:105
static void registerMetaTypes()
Register the meta-types for AuthInfo.
Definition authinfo.cpp:182
QString password
This is required for caching.
Definition authinfo.h:110
QString prompt
Information to be displayed when prompting the user for authentication information.
Definition authinfo.h:121
QString commentLabel
Descriptive label to be displayed in front of the comment when prompting the user for password.
Definition authinfo.h:165
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
A namespace for KIO globals.
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
NETWORKMANAGERQT_EXPORT QString version()
void beginStructure()
void endStructure()
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:51:26 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.