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