syncthingtray/connector/utils.h

83 lines
2.8 KiB
C
Raw Normal View History

2016-09-01 16:34:30 +02:00
#ifndef DATA_UTILS_H
#define DATA_UTILS_H
#include "./global.h"
2018-01-25 00:03:31 +01:00
#include <c++utilities/misc/traits.h>
#include <QJsonValue>
2017-02-22 18:16:41 +01:00
#include <QStringList>
#include <QUrl>
2016-09-01 16:34:30 +02:00
2017-08-24 00:00:57 +02:00
#include <limits>
#include <vector>
2017-08-24 00:00:57 +02:00
QT_FORWARD_DECLARE_CLASS(QJsonObject)
QT_FORWARD_DECLARE_CLASS(QHostAddress)
2016-09-01 16:34:30 +02:00
2019-06-10 22:48:26 +02:00
namespace CppUtilities {
2016-09-01 16:34:30 +02:00
class DateTime;
}
namespace Data {
2019-06-10 22:48:26 +02:00
namespace Traits = CppUtilities::Traits;
struct SyncthingStatistics;
struct SyncthingDir;
struct SyncthingDev;
LIB_SYNCTHING_CONNECTOR_EXPORT QString agoString(CppUtilities::DateTime dateTime);
LIB_SYNCTHING_CONNECTOR_EXPORT QString trafficString(std::uint64_t total, double rate);
LIB_SYNCTHING_CONNECTOR_EXPORT QString directoryStatusString(const Data::SyncthingStatistics &stats);
LIB_SYNCTHING_CONNECTOR_EXPORT QString syncCompleteString(
const std::vector<const SyncthingDir *> &completedDirs, const SyncthingDev *remoteDevice = nullptr);
LIB_SYNCTHING_CONNECTOR_EXPORT QString rescanIntervalString(int rescanInterval, bool fileSystemWatcherEnabled);
LIB_SYNCTHING_CONNECTOR_EXPORT bool isLocal(const QString &hostName);
LIB_SYNCTHING_CONNECTOR_EXPORT bool isLocal(const QString &hostName, const QHostAddress &hostAddress);
LIB_SYNCTHING_CONNECTOR_EXPORT bool setDirectoriesPaused(QJsonObject &syncthingConfig, const QStringList &dirIds, bool paused);
LIB_SYNCTHING_CONNECTOR_EXPORT bool setDevicesPaused(QJsonObject &syncthingConfig, const QStringList &dirs, bool paused);
2016-09-01 16:34:30 +02:00
/*!
* \brief Returns whether the host specified by the given \a url is the local machine.
*/
inline bool isLocal(const QUrl &url)
{
return isLocal(url.host());
}
2018-05-08 00:38:49 +02:00
template <typename IntType = quint64, Traits::EnableIf<std::is_integral<IntType>> * = nullptr>
inline LIB_SYNCTHING_CONNECTOR_EXPORT IntType jsonValueToInt(const QJsonValue &value, double defaultValue = 0.0)
{
2018-01-25 00:03:31 +01:00
return static_cast<IntType>(value.toDouble(defaultValue));
}
constexpr LIB_SYNCTHING_CONNECTOR_EXPORT int trQuandity(quint64 quandity)
2017-08-24 00:00:57 +02:00
{
return quandity > std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : static_cast<int>(quandity);
}
template <class Objects, class Accessor> LIB_SYNCTHING_CONNECTOR_EXPORT QStringList things(const Objects &objects, Accessor accessor)
2017-02-22 18:16:41 +01:00
{
QStringList things;
2021-03-20 22:39:40 +01:00
things.reserve(static_cast<QStringList::size_type>(objects.size()));
2017-05-01 03:34:43 +02:00
for (const auto &object : objects) {
things << accessor(object);
2017-02-22 18:16:41 +01:00
}
return things;
}
template <class Objects> LIB_SYNCTHING_CONNECTOR_EXPORT QStringList ids(const Objects &objects)
{
2018-07-05 17:39:35 +02:00
return things(objects, [](const auto &object) { return Traits::dereferenceMaybe(object).id; });
2017-02-22 18:16:41 +01:00
}
2018-07-05 17:39:35 +02:00
template <class Objects> LIB_SYNCTHING_CONNECTOR_EXPORT QStringList displayNames(const Objects &objects)
2018-07-05 17:39:35 +02:00
{
return things(objects, [](const auto &object) { return Traits::dereferenceMaybe(object).displayName(); });
}
} // namespace Data
2016-09-01 16:34:30 +02:00
#endif // DATA_UTILS_H