syncthingtray/connector/utils.cpp

99 lines
3.4 KiB
C++
Raw Normal View History

#include "./utils.h"
2016-09-01 16:34:30 +02:00
#include <c++utilities/chrono/datetime.h>
2017-05-01 03:34:43 +02:00
#include <QCoreApplication>
#include <QHostAddress>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkInterface>
2017-05-01 03:34:43 +02:00
#include <QString>
#include <QUrl>
2016-09-01 16:34:30 +02:00
using namespace ChronoUtilities;
namespace Data {
/*!
* \brief Returns a string like "2 min 45 s ago" for the specified \a dateTime.
*/
2016-09-01 16:34:30 +02:00
QString agoString(DateTime dateTime)
{
const TimeSpan delta(DateTime::now() - dateTime);
2017-05-01 03:34:43 +02:00
if (!delta.isNegative() && static_cast<uint64>(delta.totalTicks()) > (TimeSpan::ticksPerMinute / 4uL)) {
return QCoreApplication::translate("Data::Utils", "%1 ago")
.arg(QString::fromUtf8(delta.toString(TimeSpanOutputFormat::WithMeasures, true).data()));
2016-09-01 16:34:30 +02:00
} else {
return QCoreApplication::translate("Data::Utils", "right now");
}
}
/*!
* \brief Returns whether the host specified by the given \a url is the local machine.
*/
bool isLocal(const QUrl &url)
{
const QString host(url.host());
const QHostAddress hostAddress(host);
2017-05-01 03:34:43 +02:00
return host.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0 || hostAddress.isLoopback()
|| QNetworkInterface::allAddresses().contains(hostAddress);
}
/*!
* \brief Alters the specified \a syncthingConfig so that the dirs with specified IDs are paused or not.
* \returns Returns whether the config has been altered (all dirs might have been already paused/unpaused).
*/
bool setDirectoriesPaused(QJsonObject &syncthingConfig, const QStringList &dirIds, bool paused)
{
bool altered = false;
QJsonValueRef folders = syncthingConfig.find(QLatin1String("folders")).value();
if (folders.isArray()) {
QJsonArray foldersArray = folders.toArray();
for (QJsonValueRef folder : foldersArray) {
QJsonObject folderObj = folder.toObject();
if (dirIds.isEmpty() || dirIds.contains(folderObj.value(QLatin1String("id")).toString())) {
QJsonValueRef pausedValue = folderObj.find(QLatin1String("paused")).value();
if (pausedValue.toBool(false) != paused) {
pausedValue = paused;
folder = folderObj;
altered = true;
}
}
}
if (altered) {
folders = foldersArray;
}
}
return altered;
}
/*!
* \brief Alters the specified \a syncthingConfig so that the devs with the specified IDs are paused or not.
* \returns Returns whether the config has been altered (all devs might have been already paused/unpaused).
*/
bool setDevicesPaused(QJsonObject &syncthingConfig, const QStringList &devIds, bool paused)
{
bool altered = false;
QJsonValueRef devices = syncthingConfig.find(QLatin1String("devices")).value();
if (devices.isArray()) {
QJsonArray devicesArray = devices.toArray();
for (QJsonValueRef device : devicesArray) {
QJsonObject deviceObj = device.toObject();
if (devIds.isEmpty() || devIds.contains(deviceObj.value(QLatin1String("deviceID")).toString())) {
QJsonValueRef pausedValue = deviceObj.find(QLatin1String("paused")).value();
if (pausedValue.toBool(false) != paused) {
pausedValue = paused;
device = deviceObj;
altered = true;
}
}
}
if (altered) {
devices = devicesArray;
}
}
return altered;
}
2016-09-01 16:34:30 +02:00
}