swift
remotefile.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 <QJsonValue>
7 #include <QtGlobal>
8 
9 #include "misc/stringutils.h"
10 
11 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CRemoteFile)
12 
13 namespace swift::misc::network
14 {
15  CRemoteFile::CRemoteFile(const QString &name, const QString &description) : m_name(name), m_description(description)
16  {}
17 
18  CRemoteFile::CRemoteFile(const QString &name, qint64 size) : m_name(name), m_size(size) {}
19 
20  CRemoteFile::CRemoteFile(const QString &name, qint64 size, const QString &url)
21  : m_name(name), m_url(url), m_size(size)
22  {}
23 
25  {
26  if (!this->hasName()) { return {}; }
27  return QStringLiteral("%1 (%2)").arg(this->getBaseName(), this->getSizeHumanReadable());
28  }
29 
30  bool CRemoteFile::matchesBaseName(const QString &name) const
31  {
32  if (name.isEmpty()) { return false; }
33  if (caseInsensitiveStringCompare(name, this->getBaseName())) { return true; }
34  if (name.startsWith(this->getBaseName(), Qt::CaseInsensitive)) { return true; }
35  if (this->getBaseName().startsWith(name, Qt::CaseInsensitive)) { return true; }
36  return false;
37  }
38 
40  {
41  if (!this->hasName()) { return this->getUrl(); }
42  if (!this->hasUrl()) { return this->getUrl(); }
43  return this->getUrl().withAppendedPath(this->getName());
44  }
45 
47 
49 
50  void CRemoteFile::setUrl(const QString &url) { this->setUrl(CUrl(url)); }
51 
53 
55  {
56  if (m_created < 1) { return {}; }
57  const QDateTime dt = QDateTime::fromMSecsSinceEpoch(m_created);
58  return dt.toString("yyyy-MM-dd HH:mm:ss");
59  }
60 
61  QString CRemoteFile::convertToQString(bool i18n) const
62  {
63  Q_UNUSED(i18n);
64  return "Name: " + m_name + " description: " + m_description + " size: " + QString::number(m_size) +
65  " modified: " + this->getFormattedUtcTimestampYmdhms() +
66  " created: " + this->getFormattedUtcTimestampYmdhms() + " URL: " + m_url.getFullUrl();
67  }
68 
70  {
71  if (index.isMyself()) { return QVariant::fromValue(*this); }
73  const ColumnIndex i = index.frontCasted<ColumnIndex>();
74  switch (i)
75  {
76  case IndexName: return QVariant::fromValue(m_name);
77  case IndexDescription: return QVariant::fromValue(m_description);
78  case IndexUrl: return QVariant::fromValue(m_url);
79  case IndexSize: return QVariant::fromValue(m_size);
80  default: return CValueObject::propertyByIndex(index);
81  }
82  }
83 
84  void CRemoteFile::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
85  {
86  if (index.isMyself())
87  {
88  (*this) = variant.value<CRemoteFile>();
89  return;
90  }
92  {
94  return;
95  }
96  const ColumnIndex i = index.frontCasted<ColumnIndex>();
97  switch (i)
98  {
99  case IndexName: this->setName(variant.value<QString>()); break;
100  case IndexDescription: this->setDescription(variant.value<QString>()); break;
101  case IndexUrl: m_url.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
102  case IndexSize: this->setSize(variant.toInt()); break;
103  default: CValueObject::setPropertyByIndex(index, variant); break;
104  }
105  }
106 
108  {
109  CRemoteFile file;
110  file.setName(json.value("name").toString());
111  file.setDescription(json.value("description").toString());
112  file.setUrl(json.value("url").toString());
113  const qint64 size = json.value("size").toInt();
114  file.setSize(size);
115  const qint64 created = json.value("tsCreated").toInt();
116  const qint64 lastUpdated = json.value("tsLastUpdated").toInt();
117  file.m_created = created;
118  file.setMSecsSinceEpoch(lastUpdated);
119  return file;
120  }
121 } // namespace swift::misc::network
static QString humanReadableFileSize(qint64 size)
Human readable (GB, MB, ..) file size.
Definition: fileutils.cpp:516
static bool isExecutableFile(const QString &fileName)
Executable file (decided by appendix)
Definition: fileutils.cpp:543
static bool isSwiftInstaller(const QString &fileName)
swift installer
Definition: fileutils.cpp:552
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.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
QString getFormattedUtcTimestampYmdhms() const
As yyyy MM dd HH mm ss.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
void setMSecsSinceEpoch(qint64 mSecsSinceEpoch)
Timestamp as ms value.
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
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: remotefile.cpp:61
QString getFormattedCreatedYmdhms() const
Created timestamp.
Definition: remotefile.cpp:54
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: remotefile.cpp:69
bool matchesBaseName(const QString &baseName) const
Matching name?
Definition: remotefile.cpp:30
bool isSwiftInstaller() const
swift installer
Definition: remotefile.cpp:48
void setDescription(const QString &description)
Description.
Definition: remotefile.h:73
QString getBaseName() const
Name with directory stripped.
Definition: remotefile.h:55
CUrl getSmartUrl() const
Automatically concatenates the name if missing.
Definition: remotefile.cpp:39
void setSize(qint64 size)
Set size.
Definition: remotefile.h:106
bool isExecutableFile() const
Executable file (decided by appendix)
Definition: remotefile.cpp:46
void setName(const QString &name)
Name.
Definition: remotefile.h:64
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: remotefile.cpp:84
qint64 getSize() const
Get size.
Definition: remotefile.h:100
QString getSizeHumanReadable() const
Human readable (GB, MB, ..) file size.
Definition: remotefile.cpp:52
const CUrl & getUrl() const
Get URL.
Definition: remotefile.h:76
bool hasName() const
Has name?
Definition: remotefile.h:58
CRemoteFile()=default
Constructor.
bool hasUrl() const
Has an URL.
Definition: remotefile.h:79
QString getBaseNameAndSize() const
Name + human readable size.
Definition: remotefile.cpp:24
static CRemoteFile fromDatabaseJson(const QJsonObject &json)
Role from DB JSON.
Definition: remotefile.cpp:107
void setUrl(const CUrl &url)
Set URL.
Definition: remotefile.h:94
const QString & getName() const
Name.
Definition: remotefile.h:52
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
CUrl withAppendedPath(const QString &path) const
Append path.
Definition: url.cpp:115
QString getFullUrl(bool withQuery=true) const
Qualified name.
Definition: url.cpp:84
SWIFT_MISC_EXPORT bool caseInsensitiveStringCompare(const QString &c1, const QString &c2)
Case insensitive string compare.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67