swift
Loading...
Searching...
No Matches
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
11SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CRemoteFile)
12
13namespace 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())
27 {
28 return {};
29 }
30 return QStringLiteral("%1 (%2)").arg(this->getBaseName(), this->getSizeHumanReadable());
31 }
32
33 bool CRemoteFile::matchesBaseName(const QString &name) const
34 {
35 if (name.isEmpty())
36 {
37 return false;
38 }
40 {
41 return true;
42 }
43 if (name.startsWith(this->getBaseName(), Qt::CaseInsensitive))
44 {
45 return true;
46 }
47 if (this->getBaseName().startsWith(name, Qt::CaseInsensitive))
48 {
49 return true;
50 }
51 return false;
52 }
53
55 {
56 if (!this->hasName())
57 {
58 return this->getUrl();
59 }
60 if (!this->hasUrl())
61 {
62 return this->getUrl();
63 }
64 return this->getUrl().withAppendedPath(this->getName());
65 }
66
68
70
71 void CRemoteFile::setUrl(const QString &url) { this->setUrl(CUrl(url)); }
72
74
76 {
77 if (m_created < 1)
78 {
79 return {};
80 }
81 const QDateTime dt = QDateTime::fromMSecsSinceEpoch(m_created);
82 return dt.toString("yyyy-MM-dd HH:mm:ss");
83 }
84
86 {
87 Q_UNUSED(i18n);
88 return "Name: " + m_name + " description: " + m_description + " size: " + QString::number(m_size) +
89 " modified: " + this->getFormattedUtcTimestampYmdhms() +
90 " created: " + this->getFormattedUtcTimestampYmdhms() + " URL: " + m_url.getFullUrl();
91 }
92
94 {
95 if (index.isMyself())
96 {
97 return QVariant::fromValue(*this);
98 }
100 {
102 }
103 const auto i = index.frontCasted<ColumnIndex>();
104 switch (i)
105 {
106 case IndexName: return QVariant::fromValue(m_name);
107 case IndexDescription: return QVariant::fromValue(m_description);
108 case IndexUrl: return QVariant::fromValue(m_url);
109 case IndexSize: return QVariant::fromValue(m_size);
110 default: return CValueObject::propertyByIndex(index);
111 }
112 }
113
115 {
116 if (index.isMyself())
117 {
118 (*this) = variant.value<CRemoteFile>();
119 return;
120 }
122 {
124 return;
125 }
126 const auto i = index.frontCasted<ColumnIndex>();
127 switch (i)
128 {
129 case IndexName: this->setName(variant.value<QString>()); break;
130 case IndexDescription: this->setDescription(variant.value<QString>()); break;
131 case IndexUrl: m_url.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
132 case IndexSize: this->setSize(variant.toInt()); break;
133 default: CValueObject::setPropertyByIndex(index, variant); break;
134 }
135 }
136
138 {
139 CRemoteFile file;
140 file.setName(json.value("name").toString());
141 file.setDescription(json.value("description").toString());
142 file.setUrl(json.value("url").toString());
143 const qint64 size = json.value("size").toInt();
144 file.setSize(size);
145 const qint64 created = json.value("tsCreated").toInt();
146 const qint64 lastUpdated = json.value("tsLastUpdated").toInt();
147 file.m_created = created;
148 file.setMSecsSinceEpoch(lastUpdated);
149 return file;
150 }
151} // namespace swift::misc::network
static QString humanReadableFileSize(qint64 size)
Human readable (GB, MB, ..) file size.
static bool isExecutableFile(const QString &fileName)
Executable file (decided by appendix).
static bool isSwiftInstaller(const QString &fileName)
swift installer
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.
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
QString convertToQString(bool i18n=false) const
Cast as QString.
QString getFormattedCreatedYmdhms() const
Created timestamp.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
bool matchesBaseName(const QString &baseName) const
Matching name?
const QString & getName() const
Name.
Definition remotefile.h:52
bool isSwiftInstaller() const
swift installer
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.
void setSize(qint64 size)
Set size.
Definition remotefile.h:106
bool isExecutableFile() const
Executable file (decided by appendix).
void setName(const QString &name)
Name.
Definition remotefile.h:64
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
qint64 getSize() const
Get size.
Definition remotefile.h:100
QString getSizeHumanReadable() const
Human readable (GB, MB, ..) file size.
bool hasName() const
Has name?
Definition remotefile.h:58
CRemoteFile()=default
Constructor.
ColumnIndex
Properties by index.
Definition remotefile.h:32
bool hasUrl() const
Has an URL.
Definition remotefile.h:79
QString getBaseNameAndSize() const
Name + human readable size.
static CRemoteFile fromDatabaseJson(const QJsonObject &json)
Role from DB JSON.
const CUrl & getUrl() const
Get URL.
Definition remotefile.h:76
void setUrl(const CUrl &url)
Set URL.
Definition remotefile.h:94
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition url.h:27
CUrl withAppendedPath(const QString &path) const
Append path.
Definition url.cpp:151
SWIFT_MISC_EXPORT bool caseInsensitiveStringCompare(const QString &c1, const QString &c2)
Case insensitive string compare.
QDateTime fromMSecsSinceEpoch(qint64 msecs)
QString toString(QStringView format) const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
CaseInsensitive
QVariant fromValue(T &&value)
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