File indexing completed on 2026-01-05 10:22:50
0001
0002
0003
0004
0005
0006 #ifndef QURL_H
0007 #define QURL_H
0008
0009 #include <QtCore/qbytearray.h>
0010 #include <QtCore/qcompare.h>
0011 #include <QtCore/qobjectdefs.h>
0012 #include <QtCore/qstring.h>
0013 #include <QtCore/qlist.h>
0014 #include <QtCore/qglobal.h>
0015
0016 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0017 Q_FORWARD_DECLARE_CF_TYPE(CFURL);
0018 Q_FORWARD_DECLARE_OBJC_CLASS(NSURL);
0019 #endif
0020
0021 QT_BEGIN_NAMESPACE
0022
0023
0024 class QUrlQuery;
0025 class QUrlPrivate;
0026 class QDataStream;
0027
0028 template <typename E1, typename E2>
0029 class QUrlTwoFlags
0030 {
0031 int i;
0032 public:
0033 constexpr inline QUrlTwoFlags() : i(0) {}
0034 constexpr inline QUrlTwoFlags(E1 f) : i(f) {}
0035 constexpr inline QUrlTwoFlags(E2 f) : i(f) {}
0036 constexpr inline QUrlTwoFlags(QFlag f) : i(f) {}
0037 constexpr inline QUrlTwoFlags(QFlags<E1> f) : i(f.operator typename QFlags<E1>::Int()) {}
0038 constexpr inline QUrlTwoFlags(QFlags<E2> f) : i(f.operator typename QFlags<E2>::Int()) {}
0039
0040 inline QUrlTwoFlags &operator&=(int mask) { i &= mask; return *this; }
0041 inline QUrlTwoFlags &operator&=(uint mask) { i &= mask; return *this; }
0042 inline QUrlTwoFlags &operator&=(QFlags<E1> mask) { i &= mask.toInt(); return *this; }
0043 inline QUrlTwoFlags &operator&=(QFlags<E2> mask) { i &= mask.toInt(); return *this; }
0044 inline QUrlTwoFlags &operator|=(QUrlTwoFlags f) { i |= f.i; return *this; }
0045 inline QUrlTwoFlags &operator|=(E1 f) { i |= f; return *this; }
0046 inline QUrlTwoFlags &operator|=(E2 f) { i |= f; return *this; }
0047 inline QUrlTwoFlags &operator|=(QFlags<E1> mask) { i |= mask.toInt(); return *this; }
0048 inline QUrlTwoFlags &operator|=(QFlags<E2> mask) { i |= mask.toInt(); return *this; }
0049 inline QUrlTwoFlags &operator^=(QUrlTwoFlags f) { i ^= f.i; return *this; }
0050 inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; }
0051 inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; }
0052 inline QUrlTwoFlags &operator^=(QFlags<E1> mask) { i ^= mask.toInt(); return *this; }
0053 inline QUrlTwoFlags &operator^=(QFlags<E2> mask) { i ^= mask.toInt(); return *this; }
0054
0055 constexpr inline operator QFlags<E1>() const { return QFlag(i); }
0056 constexpr inline operator QFlags<E2>() const { return QFlag(i); }
0057 constexpr inline operator int() const { return i; }
0058 constexpr inline bool operator!() const { return !i; }
0059
0060 constexpr inline QUrlTwoFlags operator|(QUrlTwoFlags f) const
0061 { return QUrlTwoFlags(QFlag(i | f.i)); }
0062 constexpr inline QUrlTwoFlags operator|(E1 f) const
0063 { return QUrlTwoFlags(QFlag(i | f)); }
0064 constexpr inline QUrlTwoFlags operator|(E2 f) const
0065 { return QUrlTwoFlags(QFlag(i | f)); }
0066 constexpr inline QUrlTwoFlags operator^(QUrlTwoFlags f) const
0067 { return QUrlTwoFlags(QFlag(i ^ f.i)); }
0068 constexpr inline QUrlTwoFlags operator^(E1 f) const
0069 { return QUrlTwoFlags(QFlag(i ^ f)); }
0070 constexpr inline QUrlTwoFlags operator^(E2 f) const
0071 { return QUrlTwoFlags(QFlag(i ^ f)); }
0072 constexpr inline QUrlTwoFlags operator&(int mask) const
0073 { return QUrlTwoFlags(QFlag(i & mask)); }
0074 constexpr inline QUrlTwoFlags operator&(uint mask) const
0075 { return QUrlTwoFlags(QFlag(i & mask)); }
0076 constexpr inline QUrlTwoFlags operator&(E1 f) const
0077 { return QUrlTwoFlags(QFlag(i & f)); }
0078 constexpr inline QUrlTwoFlags operator&(E2 f) const
0079 { return QUrlTwoFlags(QFlag(i & f)); }
0080 constexpr inline QUrlTwoFlags operator~() const
0081 { return QUrlTwoFlags(QFlag(~i)); }
0082
0083 constexpr inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
0084 constexpr inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
0085 };
0086
0087 template<typename E1, typename E2>
0088 class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {};
0089
0090 class QUrl;
0091
0092 Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed = 0) noexcept;
0093
0094 class Q_CORE_EXPORT QUrl
0095 {
0096 public:
0097 enum ParsingMode {
0098 TolerantMode,
0099 StrictMode,
0100 DecodedMode
0101 };
0102
0103
0104 enum UrlFormattingOption : unsigned int {
0105 None = 0x0,
0106 RemoveScheme = 0x1,
0107 RemovePassword = 0x2,
0108 RemoveUserInfo = RemovePassword | 0x4,
0109 RemovePort = 0x8,
0110 RemoveAuthority = RemoveUserInfo | RemovePort | 0x10,
0111 RemovePath = 0x20,
0112 RemoveQuery = 0x40,
0113 RemoveFragment = 0x80,
0114
0115 PreferLocalFile = 0x200,
0116 StripTrailingSlash = 0x400,
0117 RemoveFilename = 0x800,
0118 NormalizePathSegments = 0x1000
0119 };
0120
0121 enum ComponentFormattingOption : unsigned int {
0122 PrettyDecoded = 0x000000,
0123 EncodeSpaces = 0x100000,
0124 EncodeUnicode = 0x200000,
0125 EncodeDelimiters = 0x400000 | 0x800000,
0126 EncodeReserved = 0x1000000,
0127 DecodeReserved = 0x2000000,
0128
0129
0130 FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
0131 FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000
0132 };
0133 Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
0134 #ifdef Q_QDOC
0135 private:
0136
0137
0138 template<typename T> struct QFlags : QUrlTwoFlags<T, ComponentFormattingOption>
0139 { using QUrlTwoFlags<T, ComponentFormattingOption>::QUrlTwoFlags; };
0140 public:
0141 Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
0142 #else
0143 typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
0144 #endif
0145
0146 QUrl();
0147 QUrl(const QUrl ©) noexcept;
0148 QUrl &operator =(const QUrl ©) noexcept;
0149 #ifdef QT_NO_URL_CAST_FROM_STRING
0150 explicit QUrl(const QString &url, ParsingMode mode = TolerantMode);
0151 #else
0152 QUrl(const QString &url, ParsingMode mode = TolerantMode);
0153 QUrl &operator=(const QString &url);
0154 #endif
0155 QUrl(QUrl &&other) noexcept : d(other.d)
0156 { other.d = nullptr; }
0157 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QUrl)
0158 ~QUrl();
0159
0160 void swap(QUrl &other) noexcept { qt_ptr_swap(d, other.d); }
0161
0162 void setUrl(const QString &url, ParsingMode mode = TolerantMode);
0163 QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
0164 QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
0165 QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
0166 [[nodiscard]] QUrl adjusted(FormattingOptions options) const;
0167
0168 QByteArray toEncoded(FormattingOptions options = FullyEncoded) const;
0169 #if QT_CORE_REMOVED_SINCE(6, 7)
0170 static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode);
0171 #endif
0172 static QUrl fromEncoded(QByteArrayView input, ParsingMode mode = TolerantMode);
0173
0174 enum UserInputResolutionOption {
0175 DefaultResolution,
0176 AssumeLocalFile
0177 };
0178 Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption)
0179
0180 static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory = QString(),
0181 UserInputResolutionOptions options = DefaultResolution);
0182
0183 bool isValid() const;
0184 QString errorString() const;
0185
0186 bool isEmpty() const;
0187 void clear();
0188
0189 void setScheme(const QString &scheme);
0190 QString scheme() const;
0191
0192 void setAuthority(const QString &authority, ParsingMode mode = TolerantMode);
0193 QString authority(ComponentFormattingOptions options = PrettyDecoded) const;
0194
0195 void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
0196 QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
0197
0198 void setUserName(const QString &userName, ParsingMode mode = DecodedMode);
0199 QString userName(ComponentFormattingOptions options = FullyDecoded) const;
0200
0201 void setPassword(const QString &password, ParsingMode mode = DecodedMode);
0202 QString password(ComponentFormattingOptions = FullyDecoded) const;
0203
0204 void setHost(const QString &host, ParsingMode mode = DecodedMode);
0205 QString host(ComponentFormattingOptions = FullyDecoded) const;
0206
0207 void setPort(int port);
0208 int port(int defaultPort = -1) const;
0209
0210 void setPath(const QString &path, ParsingMode mode = DecodedMode);
0211 QString path(ComponentFormattingOptions options = FullyDecoded) const;
0212 QString fileName(ComponentFormattingOptions options = FullyDecoded) const;
0213
0214 bool hasQuery() const;
0215 void setQuery(const QString &query, ParsingMode mode = TolerantMode);
0216 void setQuery(const QUrlQuery &query);
0217 QString query(ComponentFormattingOptions = PrettyDecoded) const;
0218
0219 bool hasFragment() const;
0220 QString fragment(ComponentFormattingOptions options = PrettyDecoded) const;
0221 void setFragment(const QString &fragment, ParsingMode mode = TolerantMode);
0222
0223 [[nodiscard]] QUrl resolved(const QUrl &relative) const;
0224
0225 bool isRelative() const;
0226 bool isParentOf(const QUrl &url) const;
0227
0228 bool isLocalFile() const;
0229 static QUrl fromLocalFile(const QString &localfile);
0230 QString toLocalFile() const;
0231
0232 void detach();
0233 bool isDetached() const;
0234
0235 #if QT_CORE_REMOVED_SINCE(6, 8)
0236 bool operator <(const QUrl &url) const;
0237 bool operator ==(const QUrl &url) const;
0238 bool operator !=(const QUrl &url) const;
0239 #endif
0240
0241 bool matches(const QUrl &url, FormattingOptions options) const;
0242
0243 static QString fromPercentEncoding(const QByteArray &);
0244 static QByteArray toPercentEncoding(const QString &,
0245 const QByteArray &exclude = QByteArray(),
0246 const QByteArray &include = QByteArray());
0247 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0248 static QUrl fromCFURL(CFURLRef url);
0249 CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED;
0250 static QUrl fromNSURL(const NSURL *url);
0251 NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED;
0252 #endif
0253
0254 enum AceProcessingOption : unsigned int {
0255 IgnoreIDNWhitelist = 0x1,
0256 AceTransitionalProcessing = 0x2,
0257 };
0258 Q_DECLARE_FLAGS(AceProcessingOptions, AceProcessingOption)
0259
0260 #if QT_CORE_REMOVED_SINCE(6, 3)
0261 static QString fromAce(const QByteArray &);
0262 static QByteArray toAce(const QString &);
0263 #endif
0264 static QString fromAce(const QByteArray &domain, AceProcessingOptions options = {});
0265 static QByteArray toAce(const QString &domain, AceProcessingOptions options = {});
0266
0267 static QStringList idnWhitelist();
0268 static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded));
0269 static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode);
0270
0271 static void setIdnWhitelist(const QStringList &);
0272 friend Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed) noexcept;
0273
0274 private:
0275 friend Q_CORE_EXPORT bool comparesEqual(const QUrl &lhs, const QUrl &rhs);
0276 friend Q_CORE_EXPORT Qt::weak_ordering
0277 compareThreeWay(const QUrl &lhs, const QUrl &rhs);
0278 Q_DECLARE_WEAKLY_ORDERED_NON_NOEXCEPT(QUrl)
0279
0280 void detachToClear();
0281
0282 QUrlPrivate *d;
0283 friend class QUrlQuery;
0284
0285 public:
0286 typedef QUrlPrivate * DataPtr;
0287 inline DataPtr &data_ptr() { return d; }
0288 };
0289
0290 Q_DECLARE_SHARED(QUrl)
0291 Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
0292
0293 Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::AceProcessingOptions)
0294
0295 #ifndef Q_QDOC
0296 constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
0297 { return QUrl::FormattingOptions(f1) | f2; }
0298 constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
0299 { return f2 | f1; }
0300 constexpr inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
0301 { return QIncompatibleFlag(uint(f1) | f2); }
0302
0303
0304 inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
0305 { i |= QUrl::UrlFormattingOption(f.toInt()); return i; }
0306 constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
0307 { return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
0308 constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
0309 { return i | QUrl::UrlFormattingOption(f.toInt()); }
0310 constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
0311 { return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
0312 constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
0313 { return i | QUrl::UrlFormattingOption(f.toInt()); }
0314 constexpr inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
0315 { return i | QUrl::UrlFormattingOption(f.toInt()); }
0316 constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
0317 { return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
0318 constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i)
0319 { return i | QUrl::UrlFormattingOption(f.toInt()); }
0320
0321
0322
0323 #endif
0324
0325 #ifndef QT_NO_DATASTREAM
0326 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
0327 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
0328 #endif
0329
0330 #ifndef QT_NO_DEBUG_STREAM
0331 Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
0332 #endif
0333
0334 QT_END_NAMESPACE
0335
0336 #endif