2020-09-24 09:57:54 +10:00
|
|
|
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 22:46:16 +11:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-12-18 09:50:21 +11:00
|
|
|
// The Utils class exposes various useful functions for QML that aren't
|
2019-12-27 23:19:56 +11:00
|
|
|
// provided by the `Qt` object.
|
2019-12-18 09:50:21 +11:00
|
|
|
|
2019-09-15 05:42:24 +10:00
|
|
|
#ifndef UTILS_H
|
|
|
|
#define UTILS_H
|
|
|
|
|
2019-12-27 23:19:56 +11:00
|
|
|
#include <QColor>
|
2019-09-15 05:42:24 +10:00
|
|
|
#include <QLocale>
|
2019-12-27 23:19:56 +11:00
|
|
|
#include <QObject>
|
2020-06-13 04:03:15 +10:00
|
|
|
#include <QUuid>
|
2021-03-03 03:11:54 +11:00
|
|
|
#include <QNetworkProxy>
|
2021-03-03 03:32:48 +11:00
|
|
|
#include <QUrl>
|
2021-03-03 03:11:54 +11:00
|
|
|
#include <QDebug>
|
2020-06-13 04:03:15 +10:00
|
|
|
|
2020-07-17 13:03:43 +10:00
|
|
|
#ifdef Q_OS_LINUX
|
2020-07-17 09:09:09 +10:00
|
|
|
#ifndef NO_X11
|
2020-07-17 13:03:43 +10:00
|
|
|
#define USE_LINUX_AUTOAWAY
|
2020-07-17 09:09:09 +10:00
|
|
|
#include <X11/extensions/scrnsaver.h>
|
|
|
|
#endif
|
2020-07-11 14:51:53 +10:00
|
|
|
#endif
|
|
|
|
|
2020-06-13 04:03:15 +10:00
|
|
|
#include "../submodules/hsluv-c/src/hsluv.h"
|
2019-09-15 05:42:24 +10:00
|
|
|
|
|
|
|
|
|
|
|
class Utils : public QObject {
|
2020-07-11 14:51:53 +10:00
|
|
|
Q_OBJECT
|
2019-09-15 05:42:24 +10:00
|
|
|
|
|
|
|
public:
|
2020-06-13 04:03:15 +10:00
|
|
|
Utils() {};
|
2019-09-15 05:42:24 +10:00
|
|
|
|
|
|
|
public slots:
|
2020-06-26 23:56:46 +10:00
|
|
|
QString formattedBytes(qint64 bytes, int precision = 2) {
|
2020-06-13 04:03:15 +10:00
|
|
|
return this->appLocale.formattedDataSize(
|
|
|
|
bytes, precision, QLocale::DataSizeTraditionalFormat
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString uuid() const {
|
|
|
|
return QUuid::createUuid().toString(QUuid::WithoutBraces);
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor hsluv(qreal hue, qreal sat, qreal luv, qreal alpha = 1.0) const {
|
|
|
|
double red, green, blue;
|
2020-10-02 10:06:42 +10:00
|
|
|
|
|
|
|
hsluv2rgb(
|
|
|
|
hue,
|
|
|
|
qMax(0.0, qMin(100.0, sat)),
|
|
|
|
qMax(0.0, qMin(100.0, luv)),
|
|
|
|
&red, &green, &blue
|
|
|
|
);
|
2020-06-13 04:03:15 +10:00
|
|
|
|
|
|
|
return QColor::fromRgbF(
|
|
|
|
qMax(0.0, qMin(1.0, red)),
|
|
|
|
qMax(0.0, qMin(1.0, green)),
|
|
|
|
qMax(0.0, qMin(1.0, blue)),
|
|
|
|
qMax(0.0, qMin(1.0, alpha))
|
|
|
|
);
|
|
|
|
}
|
2019-09-15 05:42:24 +10:00
|
|
|
|
2020-07-11 14:51:53 +10:00
|
|
|
int idleMilliseconds() const {
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
return -1;
|
|
|
|
|
2020-07-17 13:03:43 +10:00
|
|
|
#elif defined(USE_LINUX_AUTOAWAY)
|
2020-11-07 19:47:09 +11:00
|
|
|
if (! this->waylandDisplay.isEmpty()) return -1;
|
|
|
|
|
2020-07-11 14:51:53 +10:00
|
|
|
Display *display = XOpenDisplay(NULL);
|
|
|
|
if (! display) return -1;
|
|
|
|
|
2020-11-06 11:24:40 +11:00
|
|
|
int supportedVersion = 0, error = 0;
|
|
|
|
if (! XScreenSaverQueryExtension(display, &supportedVersion, &error))
|
|
|
|
return -1;
|
|
|
|
|
2020-07-11 14:51:53 +10:00
|
|
|
XScreenSaverInfo *info = XScreenSaverAllocInfo();
|
|
|
|
XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
|
2020-11-06 11:24:40 +11:00
|
|
|
XFree(info);
|
2020-07-11 14:51:53 +10:00
|
|
|
const int idle = info->idle;
|
|
|
|
|
|
|
|
XCloseDisplay(display);
|
|
|
|
return idle;
|
|
|
|
|
|
|
|
#elif defined(Q_OS_WINDOWS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
return -1;
|
2020-11-06 11:24:40 +11:00
|
|
|
|
2020-07-11 14:51:53 +10:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-03 03:11:54 +11:00
|
|
|
void setProxy(QUrl url) const {
|
2021-03-03 03:32:48 +11:00
|
|
|
const QUrl envProxy = QUrl(qEnvironmentVariable("http_proxy"));
|
|
|
|
if (! envProxy.isEmpty()) url = envProxy;
|
2021-03-03 03:11:54 +11:00
|
|
|
if (url.isEmpty()) return;
|
|
|
|
|
|
|
|
const QString scheme = url.scheme();
|
|
|
|
|
|
|
|
if (scheme != "socks5" && scheme != "http") {
|
|
|
|
qCritical() << "Unsupported proxy type on the Qt side:" << scheme;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkProxy proxy;
|
|
|
|
proxy.setType(
|
|
|
|
scheme == "socks5" ?
|
|
|
|
QNetworkProxy::Socks5Proxy :
|
|
|
|
QNetworkProxy::HttpProxy
|
|
|
|
);
|
|
|
|
proxy.setHostName(url.host());
|
|
|
|
proxy.setPort(url.port() == -1 ? 0 : url.port());
|
|
|
|
proxy.setUser(url.userName());
|
|
|
|
proxy.setPassword(url.password());
|
|
|
|
QNetworkProxy::setApplicationProxy(proxy);
|
|
|
|
}
|
|
|
|
|
2019-09-15 05:42:24 +10:00
|
|
|
private:
|
2020-06-13 04:03:15 +10:00
|
|
|
QLocale appLocale;
|
2020-11-07 19:47:09 +11:00
|
|
|
QString waylandDisplay = qEnvironmentVariable("WAYLAND_DISPLAY");
|
2019-09-15 05:42:24 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|