15 namespace swift::misc::network
19 if (!fullUrl.isEmpty()) {
setFullUrl(fullUrl); }
26 CUrl::CUrl(
const QString &address,
int port) :
CUrl(
"", address, port,
"") {}
28 CUrl::CUrl(
const QString &scheme,
const QString &address,
int port,
const QString &path)
29 : m_host(address.trimmed()), m_port(port), m_path(path.trimmed())
34 void CUrl::setScheme(
const QString &protocol) { m_scheme = protocol.trimmed().toLower().replace(
"://",
""); }
40 if (pathToAppend.isEmpty()) {
return m_path; }
48 if (m_port > 0) {
return m_port; }
60 const QString q(stripQueryString(query));
68 if (queryToAppend.isEmpty()) {
return; }
69 const QString q(stripQueryString(queryToAppend));
70 if (q.isEmpty()) {
return; }
76 if (key.isEmpty()) {
return; }
86 if (m_host.isEmpty()) {
return {}; }
90 if (
hasPath()) { qn = qn.append(
"/").append(m_path).replace(
"//",
"/"); }
91 if (
hasFragment()) { qn = qn.append(
"#").append(m_fragment).replace(
"//",
"/"); }
92 if (
hasQuery() && withQuery) { qn = qn.append(
"?").append(m_query); }
117 if (path.isEmpty()) {
return *
this; }
126 QUrl qurl(this->
toQUrl());
128 qurl.setScheme(scheme);
129 const CUrl url(qurl);
133 bool CUrl::pathEndsWith(
const QString &ending, Qt::CaseSensitivity cs)
const {
return m_path.endsWith(ending, cs); }
144 if (suffix.isEmpty()) {
return f.contains(
'.'); }
145 return f.endsWith(suffix, Qt::CaseInsensitive);
155 const int i = f.lastIndexOf(
'.');
156 if (i < 0)
return {};
157 if (f.length() <= i + 1)
return {};
164 const int i = f.lastIndexOf(
'.');
165 if (i < 0)
return {};
166 if (f.length() <= i + 1)
return {};
172 static const QString h1(
".html");
173 static const QString h2(
".htm");
179 if (query.isEmpty()) {
return *
this; }
193 const QPair<QString, QJsonValue> v(
"url",
getFullUrl());
194 const QJsonObject json({ v });
200 const QJsonValue value = json.value(
"url");
201 if (value.isUndefined()) {
throw CJsonException(
"Missing 'url'"); }
207 const QString p(protocol.trimmed().toLower());
208 if (p ==
"ftp")
return 20;
209 if (p ==
"https")
return 443;
210 if (p ==
"http")
return 80;
217 if (p < 0) {
return false; }
223 QString CUrl::stripQueryString(
const QString &query)
225 QString q(query.trimmed());
226 if (q.startsWith(
"?") || q.startsWith(
"&")) { q = q.mid(1); }
227 if (q.endsWith(
"?") || q.endsWith(
"&")) { q = q.left(q.size() - 1); }
233 if (index.
isMyself()) {
return QVariant::fromValue(*
this); }
237 case IndexHost:
return QVariant::fromValue(m_host);
238 case IndexPort:
return QVariant::fromValue(m_port);
239 case IndexScheme:
return QVariant::fromValue(m_scheme);
240 case IndexPath:
return QVariant::fromValue(m_path);
249 (*this) = variant.value<
CUrl>();
255 case IndexHost: this->
setHost(variant.value<QString>());
break;
256 case IndexPort: this->
setPort(variant.value<qint32>());
break;
257 case IndexPath: this->
setPath(variant.value<QString>());
break;
258 case IndexScheme: this->
setScheme(variant.value<QString>());
break;
static bool isExecutableFile(const QString &fileName)
Executable file (decided by appendix)
static bool isSwiftInstaller(const QString &fileName)
swift installer
static QString appendFilePaths(const QString &path1, const QString &path2)
Append file paths.
Thrown when a convertFromJson method encounters an unrecoverable error in JSON data.
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
ColumnIndex
Base class enums.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static QNetworkRequest getSwiftNetworkRequest(const QUrl &url, RequestType type=Get, const QString &userAgentDetails={})
Our tweaked network request with swift header.
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
QString convertToQString(bool i18n=false) const
Cast as QString.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
bool isSwiftInstaller() const
swift installer
CUrl withSwitchedScheme(const QString &protocol, int port) const
Switch protocol.
QString getFileSuffix() const
File appendix if any, otherwise empty, does not include the ".".
bool hasPath() const
Has path?
void setQUrl(const QUrl &url)
To QUrl.
QString getFileName() const
File name.
int getPort() const
Get port.
bool isFile() const
A path ends with "/", and file is anythingy beyond that, e.g. "path/file".
static int protocolToDefaultPort(const QString &protocol)
Protocol to default port.
bool hasScheme() const
Protocol.
QString appendPath(const QString &pathToAppend)
Append path.
QString getFileSuffixPlusDot() const
Suffix plus dot.
CUrl(const QString &fullUrl=QString())
Default constructor.
const QString & getScheme() const
Get protocl.
void setScheme(const QString &protocol)
Set protocol.
bool isEmpty() const
Empty.
void setQuery(const QString &query)
Set query.
CUrl withAppendedPath(const QString &path) const
Append path.
CUrl withAppendedQuery(const QString &query) const
Append path.
bool isHavingHtmlSuffix() const
Likely a HTM file?
bool isFileWithSuffix(const QString &suffix={}) const
Stricter version of isFile()
bool hasPort() const
Port?
bool hasQuery() const
Query string?
void appendQuery(const QString &queryToAppend)
Append query.
bool isExecutableFile() const
Executable file (decided by appendix)
void setHost(const QString &address)
Set address (e.g. myserver.foo.com)
void setPort(int port)
Set port.
QNetworkRequest toNetworkRequest() const
To request.
QJsonObject toJson() const
Cast to JSON object.
void setPath(const QString &path)
Set path.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
const QString & getPath() const
Get path.
bool pathEndsWith(const QString &ending, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Path ending with?
bool hasDefaultPort() const
Default port.
static bool isDefaultPort(const QString &protocol, int port)
Default port for given protocol.
static CUrl fromLocalFile(const QString &localFile)
From local file path.
void setFullUrl(const QString &fullUrl)
Set full URL.
QUrl toQUrl() const
To QUrl.
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
void setFragment(const QString &fragment)
Set fragment.
bool hasFragment() const
Fragment string?
QString getFullUrl(bool withQuery=true) const
Qualified name.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.