2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UTILITIES_H
|
|
|
|
#define UTILITIES_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
2021-05-12 20:08:06 +02:00
|
|
|
#include <QWindow>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QByteArray>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QSize>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMetaObject>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QString>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QStringList>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QUrl>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QColor>
|
2021-05-12 20:08:06 +02:00
|
|
|
#include <QRegion>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtEvents>
|
2021-07-07 23:54:21 +02:00
|
|
|
#include <QCryptographicHash>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-12-22 12:09:05 +01:00
|
|
|
#include "core/song.h"
|
|
|
|
|
2020-02-08 03:40:30 +01:00
|
|
|
class QWidget;
|
|
|
|
class QIODevice;
|
|
|
|
class QXmlStreamReader;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
namespace Utilities {
|
|
|
|
QString PrettyTime(int seconds);
|
2021-06-20 19:04:08 +02:00
|
|
|
QString PrettyTimeDelta(const int seconds);
|
|
|
|
QString PrettyTimeNanosec(const qint64 nanoseconds);
|
|
|
|
QString PrettySize(const quint64 bytes);
|
|
|
|
QString PrettySize(const QSize size);
|
|
|
|
QString WordyTime(const quint64 seconds);
|
|
|
|
QString WordyTimeNanosec(const qint64 nanoseconds);
|
|
|
|
QString Ago(const qint64 seconds_since_epoch, const QLocale &locale);
|
|
|
|
QString PrettyFutureDate(const QDate date);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QString ColorToRgba(const QColor &color);
|
|
|
|
|
|
|
|
quint64 FileSystemCapacity(const QString &path);
|
|
|
|
quint64 FileSystemFreeSpace(const QString &path);
|
|
|
|
|
2020-08-19 22:02:35 +02:00
|
|
|
bool MoveToTrashRecursive(const QString &path);
|
2018-02-27 18:06:05 +01:00
|
|
|
bool RemoveRecursive(const QString &path);
|
|
|
|
bool CopyRecursive(const QString &source, const QString &destination);
|
|
|
|
bool Copy(QIODevice *source, QIODevice *destination);
|
|
|
|
|
2020-06-14 18:58:24 +02:00
|
|
|
void OpenInFileBrowser(const QList<QUrl> &urls);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-07-07 23:54:21 +02:00
|
|
|
QByteArray Hmac(const QByteArray &key, const QByteArray &data, const QCryptographicHash::Algorithm method);
|
2018-02-27 18:06:05 +01:00
|
|
|
QByteArray HmacMd5(const QByteArray &key, const QByteArray &data);
|
|
|
|
QByteArray HmacSha256(const QByteArray &key, const QByteArray &data);
|
|
|
|
QByteArray HmacSha1(const QByteArray &key, const QByteArray &data);
|
|
|
|
QByteArray Sha1CoverHash(const QString &artist, const QString &album);
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// Reads all children of the current element,
|
|
|
|
// and returns with the stream reader either on the EndElement for the current element, or the end of the file - whichever came first.
|
2018-02-27 18:06:05 +01:00
|
|
|
void ConsumeCurrentElement(QXmlStreamReader *reader);
|
|
|
|
|
|
|
|
// Advances the stream reader until it finds an element with the given name.
|
2018-05-01 00:41:33 +02:00
|
|
|
// Returns false if the end of the document was reached before finding a matching element.
|
2018-02-27 18:06:05 +01:00
|
|
|
bool ParseUntilElement(QXmlStreamReader *reader, const QString &name);
|
2019-11-20 19:34:57 +01:00
|
|
|
bool ParseUntilElementCI(QXmlStreamReader *reader, const QString &name);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Parses a string containing an RFC822 time and date.
|
|
|
|
QDateTime ParseRFC822DateTime(const QString &text);
|
|
|
|
|
|
|
|
// Replaces some HTML entities with their normal characters.
|
|
|
|
QString DecodeHtmlEntities(const QString &text);
|
|
|
|
|
|
|
|
// Shortcut for getting a Qt-aware enum value as a string.
|
2018-05-01 00:41:33 +02:00
|
|
|
// Pass in the QMetaObject of the class that owns the enum, the string name of the enum and a valid value from that enum.
|
2018-02-27 18:06:05 +01:00
|
|
|
const char *EnumToString(const QMetaObject &meta, const char *name, int value);
|
|
|
|
|
|
|
|
QStringList Prepend(const QString &text, const QStringList &list);
|
|
|
|
QStringList Updateify(const QStringList &list);
|
|
|
|
|
2020-10-19 19:56:40 +02:00
|
|
|
// Get the path without the filename extension
|
|
|
|
QString PathWithoutFilenameExtension(const QString &filename);
|
|
|
|
QString FiddleFileExtension(const QString &filename, const QString &new_extension);
|
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
QString GetEnv(const QString &key);
|
2018-02-27 18:06:05 +01:00
|
|
|
void SetEnv(const char *key, const QString &value);
|
|
|
|
void IncreaseFDLimit();
|
|
|
|
|
|
|
|
// Borrowed from schedutils
|
|
|
|
enum IoPriority {
|
|
|
|
IOPRIO_CLASS_NONE = 0,
|
|
|
|
IOPRIO_CLASS_RT,
|
|
|
|
IOPRIO_CLASS_BE,
|
|
|
|
IOPRIO_CLASS_IDLE,
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
IOPRIO_WHO_PROCESS = 1,
|
|
|
|
IOPRIO_WHO_PGRP,
|
|
|
|
IOPRIO_WHO_USER,
|
|
|
|
};
|
|
|
|
static const int IOPRIO_CLASS_SHIFT = 13;
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
int SetThreadIOPriority(const IoPriority priority);
|
2018-02-27 18:06:05 +01:00
|
|
|
int GetThreadId();
|
|
|
|
|
2018-09-22 15:37:42 +02:00
|
|
|
QString GetRandomStringWithChars(const int len);
|
|
|
|
QString GetRandomStringWithCharsAndNumbers(const int len);
|
2019-06-10 02:29:57 +02:00
|
|
|
QString CryptographicRandomString(const int len);
|
2018-09-22 15:37:42 +02:00
|
|
|
QString GetRandomString(const int len, const QString &UseCharacters);
|
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
QString DesktopEnvironment();
|
|
|
|
|
2021-08-24 17:52:08 +02:00
|
|
|
QString UnicodeToAscii(QString unicode);
|
2019-07-14 03:08:19 +02:00
|
|
|
|
2019-09-22 17:06:45 +02:00
|
|
|
QString MacAddress();
|
|
|
|
|
2020-12-11 23:59:38 +01:00
|
|
|
QString ReplaceMessage(const QString &message, const Song &song, const QString &newline, const bool html_escaped = false);
|
|
|
|
QString ReplaceVariable(const QString &variable, const Song &song, const QString &newline, const bool html_escaped = false);
|
2019-12-22 12:09:05 +01:00
|
|
|
|
2020-04-27 00:22:46 +02:00
|
|
|
bool IsColorDark(const QColor &color);
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
QByteArray ReadDataFromFile(const QString &filename);
|
|
|
|
QString MimeTypeFromData(const QByteArray &data);
|
2020-05-12 19:48:37 +02:00
|
|
|
|
2021-05-12 20:08:06 +02:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
void enableBlurBehindWindow(QWindow *window, const QRegion ®ion);
|
|
|
|
#endif
|
|
|
|
|
2021-07-11 09:49:38 +02:00
|
|
|
} // namespace Utilities
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
class ScopedWCharArray {
|
|
|
|
public:
|
|
|
|
explicit ScopedWCharArray(const QString &str);
|
|
|
|
|
|
|
|
QString ToString() const { return QString::fromWCharArray(data_.get()); }
|
|
|
|
|
|
|
|
wchar_t *get() const { return data_.get(); }
|
2020-06-26 22:41:38 +02:00
|
|
|
explicit operator wchar_t*() const { return get(); }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
int characters() const { return chars_; }
|
|
|
|
int bytes() const { return (chars_ + 1) *sizeof(wchar_t); }
|
|
|
|
|
|
|
|
private:
|
2019-09-15 20:27:32 +02:00
|
|
|
Q_DISABLE_COPY(ScopedWCharArray)
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
int chars_;
|
|
|
|
std::unique_ptr<wchar_t[]> data_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // UTILITIES_H
|