swift
Loading...
Searching...
No Matches
urllog.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <atomic>
7
8#include <QJsonValue>
9#include <QtGlobal>
10
13#include "misc/stringutils.h"
14
15SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CUrlLog)
16
17namespace swift::misc::network
18{
19 CUrlLog::CUrlLog(const CUrl &url) : m_id(uniqueId()), m_url(url) { ITimestampBased::setCurrentUtcTime(); }
20
22 {
23 m_responseTimeMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
24 m_responseTimeMs = m_responseTimeMSecsSinceEpoch - ITimestampBased::getMSecsSinceEpoch();
25 }
26
27 bool CUrlLog::isPending() const { return m_responseTimeMs < 0; }
28
30 {
31 if (index.isMyself())
32 {
33 return QVariant::fromValue(*this);
34 }
36 {
38 }
39
40 const auto i = index.frontCasted<ColumnIndex>();
41 switch (i)
42 {
43 case IndexId: return QVariant::fromValue(m_id);
44 case IndexSuccess: return QVariant::fromValue(m_success);
45 case IndexUrl: return this->m_url.propertyByIndex(index.copyFrontRemoved());
46 case IndexResponseTimestamp: return QVariant::fromValue(this->getResponseTimestamp());
47 case IndexResponseTime: return QVariant::fromValue(m_responseTimeMs);
48 default: return CValueObject::propertyByIndex(index);
49 }
50 }
51
53 {
54 if (index.isMyself())
55 {
56 (*this) = variant.value<CUrlLog>();
57 return;
58 }
60 {
62 return;
63 }
64
65 const auto i = index.frontCasted<ColumnIndex>();
66 switch (i)
67 {
68 case IndexId: m_id = variant.toInt(); break;
69 case IndexSuccess: m_success = variant.toBool(); break;
70 case IndexUrl: m_url.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
71 case IndexResponseTime: this->setResponseTimestampToNow(); break; // a bit unusual
72 default: CValueObject::setPropertyByIndex(index, variant); break;
73 }
74 }
75
77 {
78 Q_UNUSED(i18n);
79 static const QString s("Id: %1, success: %2 response: %3ms, started: %4 ended: %5");
80 return s.arg(m_id)
81 .arg(boolToYesNo(m_success))
82 .arg(m_responseTimeMs)
83 .arg(this->getMSecsSinceEpoch())
84 .arg(m_responseTimeMSecsSinceEpoch);
85 }
86
88 {
89 static const QByteArray p(QString("urlLogId").toLatin1());
90 return p.constData();
91 }
92
93 int CUrlLog::uniqueId()
94 {
95 static std::atomic_int s_id { 1 }; // 0 means default in property system, so I start with 1
96 const int id = s_id++;
97 return id;
98 }
99} // namespace swift::misc::network
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
qint64 getMSecsSinceEpoch() const
Timestamp as ms value.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
void setCurrentUtcTime()
Set the current time as timestamp.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition url.h:27
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition urllog.cpp:52
QDateTime getResponseTimestamp() const
Response timestamp.
Definition urllog.h:46
static const char * propertyNameId()
Property name used for request.
Definition urllog.cpp:87
ColumnIndex
Properties by index.
Definition urllog.h:28
CUrlLog(const CUrl &url={})
Constructor, setting created to now and getting a valid id.
Definition urllog.cpp:19
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition urllog.cpp:29
bool isPending() const
Pending.
Definition urllog.cpp:27
void setResponseTimestampToNow()
Set response time and response timestamp.
Definition urllog.cpp:21
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition urllog.cpp:76
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
const char * constData() const const
qint64 currentMSecsSinceEpoch()
QString arg(Args &&... args) const const
QVariant fromValue(T &&value)
bool toBool() const const
int toInt(bool *ok) const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67