1 #ifndef DATA_UTILS_H
2 #define DATA_UTILS_H
3 
4 #include "./global.h"
5 
6 #include <c++utilities/misc/traits.h>
7 
8 #include <QJsonValue>
9 #include <QStringList>
10 #include <QUrl>
11 
12 #include <limits>
13 #include <vector>
14 
15 QT_FORWARD_DECLARE_CLASS(QJsonObject)
QT_FORWARD_DECLARE_CLASS(QHostAddress)16 QT_FORWARD_DECLARE_CLASS(QHostAddress)
17 
18 namespace CppUtilities {
19 class DateTime;
20 }
21 
22 namespace Data {
23 
24 namespace Traits = CppUtilities::Traits;
25 
26 struct SyncthingStatistics;
27 struct SyncthingDir;
28 struct SyncthingDev;
29 
30 LIB_SYNCTHING_CONNECTOR_EXPORT QString agoString(CppUtilities::DateTime dateTime);
31 LIB_SYNCTHING_CONNECTOR_EXPORT QString trafficString(std::uint64_t total, double rate);
32 LIB_SYNCTHING_CONNECTOR_EXPORT QString directoryStatusString(const Data::SyncthingStatistics &stats);
33 LIB_SYNCTHING_CONNECTOR_EXPORT QString syncCompleteString(
34     const std::vector<const SyncthingDir *> &completedDirs, const SyncthingDev *remoteDevice = nullptr);
35 LIB_SYNCTHING_CONNECTOR_EXPORT QString rescanIntervalString(int rescanInterval, bool fileSystemWatcherEnabled);
36 LIB_SYNCTHING_CONNECTOR_EXPORT bool isLocal(const QString &hostName);
37 LIB_SYNCTHING_CONNECTOR_EXPORT bool isLocal(const QString &hostName, const QHostAddress &hostAddress);
38 LIB_SYNCTHING_CONNECTOR_EXPORT bool setDirectoriesPaused(QJsonObject &syncthingConfig, const QStringList &dirIds, bool paused);
39 LIB_SYNCTHING_CONNECTOR_EXPORT bool setDevicesPaused(QJsonObject &syncthingConfig, const QStringList &dirs, bool paused);
40 
41 /*!
42  * \brief Returns whether the host specified by the given \a url is the local machine.
43  */
isLocal(const QUrl & url)44 inline bool isLocal(const QUrl &url)
45 {
46     return isLocal(url.host());
47 }
48 
49 template <typename IntType = quint64, Traits::EnableIf<std::is_integral<IntType>> * = nullptr>
50 inline LIB_SYNCTHING_CONNECTOR_EXPORT IntType jsonValueToInt(const QJsonValue &value, double defaultValue = 0.0)
51 {
52     return static_cast<IntType>(value.toDouble(defaultValue));
53 }
54 
trQuandity(quint64 quandity)55 constexpr LIB_SYNCTHING_CONNECTOR_EXPORT int trQuandity(quint64 quandity)
56 {
57     return quandity > std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : static_cast<int>(quandity);
58 }
59 
things(const Objects & objects,Accessor accessor)60 template <class Objects, class Accessor> LIB_SYNCTHING_CONNECTOR_EXPORT QStringList things(const Objects &objects, Accessor accessor)
61 {
62     QStringList things;
63     things.reserve(static_cast<QStringList::size_type>(objects.size()));
64     for (const auto &object : objects) {
65         things << accessor(object);
66     }
67     return things;
68 }
69 
ids(const Objects & objects)70 template <class Objects> LIB_SYNCTHING_CONNECTOR_EXPORT QStringList ids(const Objects &objects)
71 {
72     return things(objects, [](const auto &object) { return Traits::dereferenceMaybe(object).id; });
73 }
74 
displayNames(const Objects & objects)75 template <class Objects> LIB_SYNCTHING_CONNECTOR_EXPORT QStringList displayNames(const Objects &objects)
76 {
77     return things(objects, [](const auto &object) { return Traits::dereferenceMaybe(object).displayName(); });
78 }
79 
80 } // namespace Data
81 
82 #endif // DATA_UTILS_H
83