File indexing completed on 2024-03-24 04:43:40
0001 /* This file is part of the KDE project 0002 Copyright (C) 2018 Jarosław Staniek <staniek@kde.org> 0003 Copyright (C) 2018 Dmitry Baryshev <dmitrymq@gmail.com> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "KPropertyComposedUrl.h" 0022 0023 #include <tuple> // std::tie 0024 0025 class Q_DECL_HIDDEN KPropertyComposedUrl::Private 0026 { 0027 public: 0028 Private() {} 0029 Private(const Private &other) { copy(other); } 0030 Private(const QUrl &_baseUrl, const QUrl &_url) 0031 : baseUrl(_baseUrl) 0032 , url(_url) 0033 { 0034 } 0035 0036 #define KPropertyComposedUrlPrivateArgs(o) std::tie(o.baseUrl, o.url) 0037 void copy(const Private &other) 0038 { 0039 KPropertyComposedUrlPrivateArgs((*this)) = KPropertyComposedUrlPrivateArgs(other); 0040 } 0041 0042 bool operator==(const Private &other) const 0043 { 0044 return KPropertyComposedUrlPrivateArgs((*this)) == KPropertyComposedUrlPrivateArgs(other); 0045 } 0046 #undef KPropertyComposedUrlPrivateArgs 0047 0048 //! Base URL for relative URLs, not used if 'url' is absolute but still stored 0049 //! as it will be needed as soon as 'url' is changed to relative 0050 QUrl baseUrl; 0051 //! contains an absolute URL, or just a path for relative URLs 0052 QUrl url; 0053 }; 0054 0055 KPropertyComposedUrl::KPropertyComposedUrl() 0056 : d(new Private) 0057 { 0058 } 0059 0060 KPropertyComposedUrl::KPropertyComposedUrl(const QUrl &baseUrl, const QString &relativePath) 0061 : KPropertyComposedUrl() 0062 { 0063 setBaseUrl(baseUrl); 0064 setRelativePath(relativePath); 0065 } 0066 0067 KPropertyComposedUrl::KPropertyComposedUrl(const QUrl &baseUrl, const QUrl &absoluteUrl) 0068 : KPropertyComposedUrl() 0069 { 0070 setBaseUrl(baseUrl); 0071 setAbsoluteUrl(absoluteUrl); 0072 } 0073 0074 KPropertyComposedUrl::KPropertyComposedUrl(const KPropertyComposedUrl &other) 0075 : d(new Private(*other.d)) 0076 { 0077 } 0078 0079 KPropertyComposedUrl::~KPropertyComposedUrl() 0080 { 0081 delete d; 0082 } 0083 0084 KPropertyComposedUrl &KPropertyComposedUrl::operator=(const KPropertyComposedUrl &other) 0085 { 0086 if (this != &other) { 0087 d->copy(*other.d); 0088 } 0089 return *this; 0090 } 0091 0092 bool KPropertyComposedUrl::operator==(const KPropertyComposedUrl &other) const 0093 { 0094 return *d == *other.d; 0095 } 0096 0097 QUrl KPropertyComposedUrl::value() const 0098 { 0099 if (!isValid()) { 0100 return QUrl(); 0101 } 0102 0103 if (d->url.isRelative()) { 0104 QUrl baseUrl = d->baseUrl; 0105 0106 //! append necessary '/' 0107 if (!baseUrl.path().endsWith(QLatin1String("/"))) { 0108 baseUrl.setPath(baseUrl.path() + QLatin1String("/")); 0109 } 0110 0111 return baseUrl.resolved(d->url); 0112 } else { 0113 return d->url; 0114 } 0115 } 0116 0117 QUrl KPropertyComposedUrl::baseUrl() const 0118 { 0119 return d->baseUrl; 0120 } 0121 0122 void KPropertyComposedUrl::setBaseUrl(const QUrl &url) 0123 { 0124 if (!url.isValid() || url.isRelative()) { 0125 d->baseUrl.clear(); 0126 return; 0127 } 0128 0129 d->baseUrl = url; 0130 } 0131 0132 QUrl KPropertyComposedUrl::absoluteUrl() const 0133 { 0134 return d->url.isRelative() ? QUrl() : d->url; 0135 } 0136 0137 void KPropertyComposedUrl::setAbsoluteUrl(const QUrl &absoluteUrl) 0138 { 0139 d->url.clear(); 0140 0141 if (absoluteUrl.isValid() && !absoluteUrl.isRelative()) { 0142 d->url = absoluteUrl; 0143 } 0144 } 0145 0146 QString KPropertyComposedUrl::relativePath() const 0147 { 0148 return d->url.isRelative() ? d->url.path() : QString(); 0149 } 0150 0151 void KPropertyComposedUrl::setRelativePath(const QString &relativePath) 0152 { 0153 d->url.clear(); 0154 0155 if (!relativePath.isEmpty()) { 0156 d->url.setPath(relativePath); 0157 } 0158 } 0159 0160 bool KPropertyComposedUrl::isValid() const 0161 { 0162 // we should have both URLs anyways 0163 return d->baseUrl.isValid() && d->url.isValid(); 0164 } 0165 0166 QDebug operator<<(QDebug dbg, const KPropertyComposedUrl &p) 0167 { 0168 dbg.nospace() << "KPropertyComposedUrl(" 0169 << "baseUrl=" << p.baseUrl() << " relativePath=" << p.relativePath() 0170 << " absoluteUrl=" << p.absoluteUrl() << ")"; 0171 return dbg.space(); 0172 }