1/****************************************************************************
2**
3** Copyright (C) 2016 Intel Corporation.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QURLQUERY_H
41#define QURLQUERY_H
42
43#include <QtCore/qpair.h>
44#include <QtCore/qshareddata.h>
45#include <QtCore/qurl.h>
46
47#if QT_DEPRECATED_SINCE(5,0)
48#include <QtCore/qstringlist.h>
49#endif
50
51#include <initializer_list>
52
53QT_BEGIN_NAMESPACE
54
55Q_CORE_EXPORT uint qHash(const QUrlQuery &key, uint seed = 0) noexcept;
56
57class QUrlQueryPrivate;
58class Q_CORE_EXPORT QUrlQuery
59{
60public:
61 QUrlQuery();
62 explicit QUrlQuery(const QUrl &url);
63 explicit QUrlQuery(const QString &queryString);
64 QUrlQuery(std::initializer_list<QPair<QString, QString>> list)
65 : QUrlQuery()
66 {
67 for (const QPair<QString, QString> &item : list)
68 addQueryItem(item.first, item.second);
69 }
70
71 QUrlQuery(const QUrlQuery &other);
72 QUrlQuery &operator=(const QUrlQuery &other);
73 QUrlQuery &operator=(QUrlQuery &&other) noexcept { swap(other); return *this; }
74 ~QUrlQuery();
75
76 bool operator==(const QUrlQuery &other) const;
77 bool operator!=(const QUrlQuery &other) const
78 { return !(*this == other); }
79
80 void swap(QUrlQuery &other) noexcept { qSwap(d, other.d); }
81
82 bool isEmpty() const;
83 bool isDetached() const;
84 void clear();
85
86 QString query(QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const;
87 void setQuery(const QString &queryString);
88 QString toString(QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const
89 { return query(encoding); }
90
91 void setQueryDelimiters(QChar valueDelimiter, QChar pairDelimiter);
92 QChar queryValueDelimiter() const;
93 QChar queryPairDelimiter() const;
94
95 void setQueryItems(const QList<QPair<QString, QString> > &query);
96 QList<QPair<QString, QString> > queryItems(QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const;
97
98 bool hasQueryItem(const QString &key) const;
99 void addQueryItem(const QString &key, const QString &value);
100 void removeQueryItem(const QString &key);
101 QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const;
102 QStringList allQueryItemValues(const QString &key, QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const;
103 void removeAllQueryItems(const QString &key);
104
105 static QChar defaultQueryValueDelimiter()
106 { return QChar(ushort('=')); }
107 static QChar defaultQueryPairDelimiter()
108 { return QChar(ushort('&')); }
109
110private:
111 friend class QUrl;
112 friend Q_CORE_EXPORT uint qHash(const QUrlQuery &key, uint seed) noexcept;
113 QSharedDataPointer<QUrlQueryPrivate> d;
114public:
115 typedef QSharedDataPointer<QUrlQueryPrivate> DataPtr;
116 inline DataPtr &data_ptr() { return d; }
117};
118
119Q_DECLARE_SHARED(QUrlQuery)
120
121#if QT_DEPRECATED_SINCE(5,0)
122inline void QUrl::setQueryItems(const QList<QPair<QString, QString> > &qry)
123{ QUrlQuery q(*this); q.setQueryItems(qry); setQuery(q); }
124inline void QUrl::addQueryItem(const QString &key, const QString &value)
125{ QUrlQuery q(*this); q.addQueryItem(key, value); setQuery(q); }
126inline QList<QPair<QString, QString> > QUrl::queryItems() const
127{ return QUrlQuery(*this).queryItems(); }
128inline bool QUrl::hasQueryItem(const QString &key) const
129{ return QUrlQuery(*this).hasQueryItem(key); }
130inline QString QUrl::queryItemValue(const QString &key) const
131{ return QUrlQuery(*this).queryItemValue(key); }
132inline QStringList QUrl::allQueryItemValues(const QString &key) const
133{ return QUrlQuery(*this).allQueryItemValues(key); }
134inline void QUrl::removeQueryItem(const QString &key)
135{ QUrlQuery q(*this); q.removeQueryItem(key); setQuery(q); }
136inline void QUrl::removeAllQueryItems(const QString &key)
137{ QUrlQuery q(*this); q.removeAllQueryItems(key); setQuery(q); }
138
139inline void QUrl::addEncodedQueryItem(const QByteArray &key, const QByteArray &value)
140{ QUrlQuery q(*this); q.addQueryItem(fromEncodedComponent_helper(key), fromEncodedComponent_helper(value)); setQuery(q); }
141inline bool QUrl::hasEncodedQueryItem(const QByteArray &key) const
142{ return QUrlQuery(*this).hasQueryItem(fromEncodedComponent_helper(key)); }
143inline QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const
144{ return QUrlQuery(*this).queryItemValue(fromEncodedComponent_helper(key), QUrl::FullyEncoded).toLatin1(); }
145inline void QUrl::removeEncodedQueryItem(const QByteArray &key)
146{ QUrlQuery q(*this); q.removeQueryItem(fromEncodedComponent_helper(key)); setQuery(q); }
147inline void QUrl::removeAllEncodedQueryItems(const QByteArray &key)
148{ QUrlQuery q(*this); q.removeAllQueryItems(fromEncodedComponent_helper(key)); setQuery(q); }
149
150inline void QUrl::setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &qry)
151{
152 QUrlQuery q;
153 QList<QPair<QByteArray, QByteArray> >::ConstIterator it = qry.constBegin();
154 for ( ; it != qry.constEnd(); ++it)
155 q.addQueryItem(fromEncodedComponent_helper(it->first), fromEncodedComponent_helper(it->second));
156 setQuery(q);
157}
158inline QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const
159{
160 QList<QPair<QString, QString> > items = QUrlQuery(*this).queryItems(QUrl::FullyEncoded);
161 QList<QPair<QString, QString> >::ConstIterator it = items.constBegin();
162 QList<QPair<QByteArray, QByteArray> > result;
163 result.reserve(items.size());
164 for ( ; it != items.constEnd(); ++it)
165 result << qMakePair(it->first.toLatin1(), it->second.toLatin1());
166 return result;
167}
168inline QList<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const
169{
170 const QStringList items = QUrlQuery(*this).allQueryItemValues(fromEncodedComponent_helper(key), QUrl::FullyEncoded);
171 QList<QByteArray> result;
172 result.reserve(items.size());
173 for (const QString &item : items)
174 result << item.toLatin1();
175 return result;
176}
177#endif
178
179QT_END_NAMESPACE
180
181#endif // QURLQUERY_H
182