swift
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 
4 #include "misc/network/urllog.h"
5 
6 #include <atomic>
7 
8 #include <QJsonValue>
9 #include <QtGlobal>
10 
12 #include "misc/propertyindexref.h"
13 #include "misc/stringutils.h"
14 
15 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CUrlLog)
16 
17 namespace swift::misc::network
18 {
19  CUrlLog::CUrlLog(const CUrl &url) : ITimestampBased(), m_id(uniqueId()), m_url(url)
20  {
22  }
23 
25  {
26  m_responseTimeMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
27  m_responseTimeMs = m_responseTimeMSecsSinceEpoch - ITimestampBased::getMSecsSinceEpoch();
28  }
29 
30  bool CUrlLog::isPending() const { return m_responseTimeMs < 0; }
31 
33  {
34  if (index.isMyself()) { return QVariant::fromValue(*this); }
36 
37  const ColumnIndex i = index.frontCasted<ColumnIndex>();
38  switch (i)
39  {
40  case IndexId: return QVariant::fromValue(m_id);
41  case IndexSuccess: return QVariant::fromValue(m_success);
42  case IndexUrl: return this->m_url.propertyByIndex(index.copyFrontRemoved());
43  case IndexResponseTimestamp: return QVariant::fromValue(this->getResponseTimestamp());
44  case IndexResponseTime: return QVariant::fromValue(m_responseTimeMs);
45  default: return CValueObject::propertyByIndex(index);
46  }
47  }
48 
49  void CUrlLog::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
50  {
51  if (index.isMyself())
52  {
53  (*this) = variant.value<CUrlLog>();
54  return;
55  }
57  {
59  return;
60  }
61 
62  const ColumnIndex i = index.frontCasted<ColumnIndex>();
63  switch (i)
64  {
65  case IndexId: m_id = variant.toInt(); break;
66  case IndexSuccess: m_success = variant.toBool(); break;
67  case IndexUrl: m_url.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
68  case IndexResponseTime: this->setResponseTimestampToNow(); break; // a bit unusual
69  default: CValueObject::setPropertyByIndex(index, variant); break;
70  }
71  }
72 
73  QString CUrlLog::convertToQString(bool i18n) const
74  {
75  Q_UNUSED(i18n);
76  static const QString s("Id: %1, success: %2 response: %3ms, started: %4 ended: %5");
77  return s.arg(m_id)
78  .arg(boolToYesNo(m_success))
79  .arg(m_responseTimeMs)
80  .arg(this->getMSecsSinceEpoch())
81  .arg(m_responseTimeMSecsSinceEpoch);
82  }
83 
85  {
86  static const QByteArray p(QString("urlLogId").toLatin1());
87  return p.constData();
88  }
89 
90  int CUrlLog::uniqueId()
91  {
92  static std::atomic_int s_id { 1 }; // 0 means default in property system, so I start with 1
93  const int id = s_id++;
94  return id;
95  }
96 } // 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.
Entity with timestamp.
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.
ColumnIndex
Base class enums.
Definition: mixinindex.h:44
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
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: url.cpp:245
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: url.cpp:231
Information about accessing one URL over the network.
Definition: urllog.h:24
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: urllog.cpp:49
QDateTime getResponseTimestamp() const
Response timestamp.
Definition: urllog.h:46
static const char * propertyNameId()
Property name used for request.
Definition: urllog.cpp:84
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:32
bool isPending() const
Pending.
Definition: urllog.cpp:30
void setResponseTimestampToNow()
Set response time and response timestamp.
Definition: urllog.cpp:24
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: urllog.cpp:73
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67