swift
remotefilelist.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 
8 #include "misc/json.h"
10 
11 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::network, CRemoteFile, CRemoteFileList)
12 
13 namespace swift::misc::network
14 {
16 
18  {
19  QStringList fileNames;
20  for (const CRemoteFile &rf : *this) { fileNames.append(rf.getName()); }
21  if (sorted) { fileNames.sort(); }
22  return fileNames;
23  }
24 
26  {
27  QStringList fileNames;
28  for (const CRemoteFile &rf : *this) { fileNames.append(rf.getBaseNameAndSize()); }
29  if (sorted) { fileNames.sort(); }
30  return fileNames;
31  }
32 
34  {
35  if (name.isEmpty()) { return {}; }
36  return this->findFirstByOrDefault(&CRemoteFile::getName, name);
37  }
38 
40  {
41  if (name.isEmpty()) { return {}; }
42  for (const CRemoteFile &rf : *this)
43  {
44  if (rf.getName().contains(name, cs)) { return rf; }
45  }
46  return {};
47  }
48 
50  {
51  if (baseName.isEmpty()) { return {}; }
52  for (const CRemoteFile &rf : *this)
53  {
54  if (rf.matchesBaseName(baseName)) { return rf; }
55  }
56  return {};
57  }
58 
60  {
61  CRemoteFileList files;
62  for (const CRemoteFile &rf : *this)
63  {
64  if (CFileUtils::isExecutableFile(rf.getName())) { files.push_back(rf); }
65  }
66  return files;
67  }
68 
70  {
71  qint64 s = 0;
72  for (const CRemoteFile &rf : *this) { s += rf.getSize(); }
73  return s;
74  }
75 
77  {
79  }
80 
82  {
83  CRemoteFileList roles;
84  for (const QJsonValue &value : array) { roles.push_back(CRemoteFile::fromDatabaseJson(value.toObject())); }
85  return roles;
86  }
87 
89  {
90  if (json.isEmpty()) { return {}; }
92  }
93 } // namespace swift::misc::network
static QString humanReadableFileSize(qint64 size)
Human readable (GB, MB, ..) file size.
Definition: fileutils.cpp:510
static bool isExecutableFile(const QString &fileName)
Executable file (decided by appendix)
Definition: fileutils.cpp:537
auto findFirstByOrDefault(Predicate p, const Value &def) const
Return a copy of the first element for which a given predicate returns true, or a default value if th...
Definition: range.h:70
Q_REQUIRED_RESULT CSequence sorted(Predicate p) const
Return a copy sorted by a given comparator predicate.
Definition: sequence.h:583
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
bool matchesBaseName(const QString &baseName) const
Matching name?
Definition: remotefile.cpp:30
qint64 getSize() const
Get size.
Definition: remotefile.h:100
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
const QString & getName() const
Name.
Definition: remotefile.h:52
Value object encapsulating a list of remote files.
CRemoteFile findFirstContainingNameOrDefault(const QString &name, Qt::CaseSensitivity cs) const
First by name contained of default.
QStringList getBaseNamesPlusSize(bool sorted=true) const
All file names plus size.
QString getTotalFileSizeHumanReadable() const
Size formatted.
QStringList getNames(bool sorted=true) const
All file names.
CRemoteFileList findExecutableFiles() const
Find all executable files (decided by appendix)
CRemoteFileList()=default
Default constructor.
static CRemoteFileList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
CRemoteFile findFirstByNameOrDefault(const QString &name) const
First by name of default.
CRemoteFile findFirstByMatchingBaseNameOrDefault(const QString &name) const
Find first matching name of default.
qint64 getTotalFileSize() const
Size of all files.
QJsonArray jsonArrayFromString(const QString &json)
JSON Array from string.
Definition: json.cpp:431
void append(QList< T > &&value)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
void sort(Qt::CaseSensitivity cs)
CaseSensitivity
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63