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 CRemoteFileList::getNames(bool sorted) const
20  {
21  QStringList fileNames;
22  for (const CRemoteFile &rf : *this) { fileNames.append(rf.getName()); }
23  if (sorted) { fileNames.sort(); }
24  return fileNames;
25  }
26 
27  QStringList CRemoteFileList::getBaseNamesPlusSize(bool sorted) const
28  {
29  QStringList fileNames;
30  for (const CRemoteFile &rf : *this) { fileNames.append(rf.getBaseNameAndSize()); }
31  if (sorted) { fileNames.sort(); }
32  return fileNames;
33  }
34 
36  {
37  if (name.isEmpty()) { return CRemoteFile(); }
38  return this->findFirstByOrDefault(&CRemoteFile::getName, name);
39  }
40 
41  CRemoteFile CRemoteFileList::findFirstContainingNameOrDefault(const QString &name, Qt::CaseSensitivity cs) const
42  {
43  if (name.isEmpty()) { return CRemoteFile(); }
44  for (const CRemoteFile &rf : *this)
45  {
46  if (rf.getName().contains(name, cs)) { return rf; }
47  }
48  return CRemoteFile();
49  }
50 
52  {
53  if (baseName.isEmpty()) { return CRemoteFile(); }
54  for (const CRemoteFile &rf : *this)
55  {
56  if (rf.matchesBaseName(baseName)) { return rf; }
57  }
58  return CRemoteFile();
59  }
60 
62  {
63  CRemoteFileList files;
64  for (const CRemoteFile &rf : *this)
65  {
66  if (CFileUtils::isExecutableFile(rf.getName())) { files.push_back(rf); }
67  }
68  return files;
69  }
70 
72  {
73  qint64 s = 0;
74  for (const CRemoteFile &rf : *this) { s += rf.getSize(); }
75  return s;
76  }
77 
79  {
81  }
82 
84  {
85  CRemoteFileList roles;
86  for (const QJsonValue &value : array) { roles.push_back(CRemoteFile::fromDatabaseJson(value.toObject())); }
87  return roles;
88  }
89 
91  {
92  if (json.isEmpty()) { return CRemoteFileList(); }
94  }
95 } // 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
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)
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
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63