Split utilities functions into separate files

This commit is contained in:
Jonas Kvinge
2022-12-28 03:12:00 +01:00
parent f7d10f9530
commit 205b7f2401
127 changed files with 2269 additions and 1361 deletions

View File

@@ -29,7 +29,7 @@
#include <QString>
#include "core/logging.h"
#include "utilities.h"
#include "utilities/fileutils.h"
#include "musicstorage.h"
#include "filesystemmusicstorage.h"

View File

@@ -1,211 +0,0 @@
/*
* Strawberry Music Player
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#include "config.h"
#include <QList>
#include <QBuffer>
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QImage>
#include <QImageReader>
#include <QPixmap>
#include <QPainter>
#include <QSize>
#include "imageutils.h"
#include "core/utilities.h"
#include "core/tagreaderclient.h"
QStringList ImageUtils::kSupportedImageMimeTypes;
QStringList ImageUtils::kSupportedImageFormats;
QStringList ImageUtils::SupportedImageMimeTypes() {
if (kSupportedImageMimeTypes.isEmpty()) {
for (const QByteArray &mimetype : QImageReader::supportedMimeTypes()) {
kSupportedImageMimeTypes << mimetype;
}
}
return kSupportedImageMimeTypes;
}
QStringList ImageUtils::SupportedImageFormats() {
if (kSupportedImageFormats.isEmpty()) {
for (const QByteArray &filetype : QImageReader::supportedImageFormats()) {
kSupportedImageFormats << filetype;
}
}
return kSupportedImageFormats;
}
QList<QByteArray> ImageUtils::ImageFormatsForMimeType(const QByteArray &mimetype) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
return QImageReader::imageFormatsForMimeType(mimetype);
#else
if (mimetype == "image/bmp") return QList<QByteArray>() << "BMP";
else if (mimetype == "image/gif") return QList<QByteArray>() << "GIF";
else if (mimetype == "image/jpeg") return QList<QByteArray>() << "JPG";
else if (mimetype == "image/png") return QList<QByteArray>() << "PNG";
else return QList<QByteArray>();
#endif
}
QPixmap ImageUtils::TryLoadPixmap(const QUrl &art_automatic, const QUrl &art_manual, const QUrl &url) {
QPixmap ret;
if (!art_manual.path().isEmpty()) {
if (art_manual.path() == Song::kManuallyUnsetCover) return ret;
else if (art_manual.isLocalFile()) {
ret.load(art_manual.toLocalFile());
}
else if (art_manual.scheme().isEmpty()) {
ret.load(art_manual.path());
}
}
if (ret.isNull() && !art_automatic.path().isEmpty()) {
if (art_automatic.path() == Song::kEmbeddedCover && !url.isEmpty() && url.isLocalFile()) {
ret = QPixmap::fromImage(TagReaderClient::Instance()->LoadEmbeddedArtAsImageBlocking(url.toLocalFile()));
}
else if (art_automatic.isLocalFile()) {
ret.load(art_automatic.toLocalFile());
}
else if (art_automatic.scheme().isEmpty()) {
ret.load(art_automatic.path());
}
}
return ret;
}
QByteArray ImageUtils::SaveImageToJpegData(const QImage &image) {
if (image.isNull()) return QByteArray();
QByteArray image_data;
QBuffer buffer(&image_data);
if (buffer.open(QIODevice::WriteOnly)) {
image.save(&buffer, "JPEG");
buffer.close();
}
return image_data;
}
QByteArray ImageUtils::FileToJpegData(const QString &filename) {
if (filename.isEmpty()) return QByteArray();
QByteArray image_data = Utilities::ReadDataFromFile(filename);
if (Utilities::MimeTypeFromData(image_data) == "image/jpeg") return image_data;
else {
QImage image;
if (image.loadFromData(image_data)) {
if (!image.isNull()) {
image_data = SaveImageToJpegData(image);
}
}
}
return image_data;
}
QImage ImageUtils::ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height) {
if (image.isNull()) return image;
// Scale the image down
QImage image_scaled;
if (scale) {
image_scaled = image.scaled(QSize(desired_height, desired_height), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
else {
image_scaled = image;
}
// Pad the image to height x height
if (pad) {
QImage image_padded(desired_height, desired_height, QImage::Format_ARGB32);
image_padded.fill(0);
QPainter p(&image_padded);
p.drawImage((desired_height - image_scaled.width()) / 2, (desired_height - image_scaled.height()) / 2, image_scaled);
p.end();
image_scaled = image_padded;
}
return image_scaled;
}
QImage ImageUtils::CreateThumbnail(const QImage &image, const bool pad, const QSize size) {
if (image.isNull()) return image;
QImage image_thumbnail;
if (pad) {
image_thumbnail = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QImage image_padded(size, QImage::Format_ARGB32_Premultiplied);
image_padded.fill(0);
QPainter p(&image_padded);
p.drawImage((image_padded.width() - image_thumbnail.width()) / 2, (image_padded.height() - image_thumbnail.height()) / 2, image_thumbnail);
p.end();
image_thumbnail = image_padded;
}
else {
image_thumbnail = image.scaledToHeight(size.height(), Qt::SmoothTransformation);
}
return image_thumbnail;
}
QImage ImageUtils::GenerateNoCoverImage(const QSize size) {
QImage image(":/pictures/cdcase.png");
// Get a square version of the nocover image with some transparency:
QImage image_scaled = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QImage image_square(size, QImage::Format_ARGB32);
image_square.fill(0);
QPainter p(&image_square);
p.setOpacity(0.4);
p.drawImage((size.width() - image_scaled.width()) / 2, (size.height() - image_scaled.height()) / 2, image_scaled);
p.end();
return image_square;
}

View File

@@ -1,52 +0,0 @@
/*
* Strawberry Music Player
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef IMAGEUTILS_H
#define IMAGEUTILS_H
#include "config.h"
#include <QList>
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QImage>
#include <QPixmap>
class ImageUtils {
private:
static QStringList kSupportedImageMimeTypes;
static QStringList kSupportedImageFormats;
public:
static QStringList SupportedImageMimeTypes();
static QStringList SupportedImageFormats();
static QList<QByteArray> ImageFormatsForMimeType(const QByteArray &mimetype);
static QByteArray SaveImageToJpegData(const QImage &image = QImage());
static QByteArray FileToJpegData(const QString &filename);
static QPixmap TryLoadPixmap(const QUrl &automatic, const QUrl &manual, const QUrl &url = QUrl());
static QImage ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height);
static QImage CreateThumbnail(const QImage &image, const bool pad, const QSize size);
static QImage GenerateNoCoverImage(const QSize size = QSize());
};
#endif // IMAGEUTILS_H

View File

@@ -1,29 +1,44 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef MAC_STARTUP_H
#define MAC_STARTUP_H
#include "config.h"
#include <CoreFoundation/CFDictionary.h>
#include <QString>
#include <QWidget>
#include <QKeySequence>
class QObject;
class QWidget;
#ifdef __OBJC__
@class NSEvent;
#else
class NSEvent;
#endif
class PlatformInterface;
class GlobalShortcutsBackendMacOS;
class PlatformInterface {
public:
PlatformInterface() = default;
virtual ~PlatformInterface() {}
// Called when the application should show itself.
virtual void Activate() = 0;
virtual bool LoadUrl(const QString &url) = 0;
private:
Q_DISABLE_COPY(PlatformInterface)
};
namespace mac {
void MacMain();
@@ -32,6 +47,9 @@ void SetApplicationHandler(PlatformInterface *handler);
void EnableFullScreen(const QWidget &main_window);
QKeySequence KeySequenceFromNSEvent(NSEvent *event);
void DumpDictionary(CFDictionaryRef dict);
} // namespace mac
#endif

View File

@@ -45,10 +45,10 @@
#include "config.h"
#include "platforminterface.h"
#include "mac_delegate.h"
#include "mac_startup.h"
#include "mac_utilities.h"
#include "utilities.h"
#include "scoped_cftyperef.h"
#include "core/logging.h"
#include "core/scoped_nsautorelease_pool.h"

View File

@@ -74,13 +74,10 @@
#include <QClipboard>
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "utilities.h"
#include "timeconstants.h"
#include "commandlineoptions.h"
#include "mimedata.h"
#include "iconloader.h"
@@ -95,10 +92,15 @@
#include "filesystemmusicstorage.h"
#include "deletefiles.h"
#ifdef Q_OS_MACOS
# include "mac_startup.h"
# include "macsystemtrayicon.h"
#else
# include "qtsystemtrayicon.h"
#endif
#include "networkaccessmanager.h"
#include "utilities/envutils.h"
#include "utilities/filemanagerutils.h"
#include "utilities/timeconstants.h"
#include "engine/enginetype.h"
#include "engine/enginebase.h"
#include "engine/engine_fwd.h"

View File

@@ -48,12 +48,12 @@
#include <QSettings>
#include <QtEvents>
#include "core/lazy.h"
#include "core/tagreaderclient.h"
#include "core/song.h"
#include "lazy.h"
#include "platforminterface.h"
#include "song.h"
#include "tagreaderclient.h"
#include "engine/enginetype.h"
#include "engine/engine_fwd.h"
#include "mac_startup.h"
#include "osd/osdbase.h"
#include "collection/collectionmodel.h"
#include "playlist/playlist.h"

View File

@@ -46,10 +46,10 @@
#include "mpris_common.h"
#include "mpris2.h"
#include "timeconstants.h"
#include "song.h"
#include "application.h"
#include "player.h"
#include "utilities/timeconstants.h"
#include "engine/enginebase.h"
#include "playlist/playlist.h"
#include "playlist/playlistitem.h"

View File

@@ -1,7 +1,6 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2011, David Sansome <me@davidsansome.com>
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
@@ -19,26 +18,23 @@
*
*/
#include "config.h"
#ifndef PLATFORMINTERFACE_H
#define PLATFORMINTERFACE_H
#include <QWidget>
#include <QKeySequence>
#include <QObject>
#include <QString>
#include <CoreFoundation/CFDictionary.h>
class PlatformInterface {
public:
PlatformInterface() = default;
virtual ~PlatformInterface() {}
#ifdef __OBJC__
@class NSEvent;
#else
class NSEvent;
#endif
// Called when the application should show itself.
virtual void Activate() = 0;
virtual bool LoadUrl(const QString &url) = 0;
namespace mac {
private:
Q_DISABLE_COPY(PlatformInterface)
};
QKeySequence KeySequenceFromNSEvent(NSEvent *event);
void DumpDictionary(CFDictionaryRef dict);
} // namespace mac
namespace Utilities {
qint32 GetMacOsVersion();
}
#endif // PLATFORMINTERFACE_H

View File

@@ -36,9 +36,9 @@
#include <QSettings>
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "song.h"
#include "timeconstants.h"
#include "urlhandler.h"
#include "application.h"

View File

@@ -1,6 +1,7 @@
/*
* Strawberry Music Player
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,20 +18,12 @@
*
*/
#include "config.h"
#include <QString>
#include <QtGlobal>
#import <Foundation/Foundation.h>
#import <Foundation/NSProcessInfo.h>
namespace Utilities {
qint32 GetMacOsVersion() {
NSOperatingSystemVersion version = [ [NSProcessInfo processInfo] operatingSystemVersion];
return version.minorVersion;
#include "scopedwchararray.h"
ScopedWCharArray::ScopedWCharArray(const QString &str)
: chars_(str.length()), data_(new wchar_t[chars_ + 1]) {
str.toWCharArray(data_.get());
data_[chars_] = '\0';
}
} // namespace Utilities

View File

@@ -0,0 +1,48 @@
/*
* Strawberry Music Player
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef SCOPEDWCHARARRAY_H
#define SCOPEDWCHARARRAY_H
#include <memory>
#include <QObject>
#include <QString>
class ScopedWCharArray {
public:
explicit ScopedWCharArray(const QString &str);
QString ToString() const { return QString::fromWCharArray(data_.get()); }
wchar_t *get() const { return data_.get(); }
explicit operator wchar_t *() const { return get(); }
qint64 characters() const { return chars_; }
qint64 bytes() const { return (chars_ + 1) * sizeof(wchar_t); }
private:
Q_DISABLE_COPY(ScopedWCharArray)
qint64 chars_;
std::unique_ptr<wchar_t[]> data_;
};
#endif // SCOPEDWCHARARRAY_H

View File

@@ -50,8 +50,10 @@
#include "core/iconloader.h"
#include "engine/enginebase.h"
#include "timeconstants.h"
#include "utilities.h"
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "utilities/cryptutils.h"
#include "utilities/timeconstants.h"
#include "song.h"
#include "application.h"
#include "sqlquery.h"

View File

@@ -20,7 +20,7 @@
#include <QThread>
#include "thread.h"
#include "utilities.h"
#include "utilities/threadutils.h"
void Thread::run() {

View File

@@ -22,7 +22,7 @@
#include <QThread>
#include "utilities.h"
#include "utilities/threadutils.h"
class QObject;

View File

@@ -1,32 +0,0 @@
/* This file was part of Clementine.
Copyright 2011, David Sansome <me@davidsansome.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef TIMECONSTANTS_H
#define TIMECONSTANTS_H
#include <QtGlobal>
// Use these to convert between time units
constexpr qint64 kMsecPerSec = 1000ll;
constexpr qint64 kUsecPerMsec = 1000ll;
constexpr qint64 kUsecPerSec = 1000000ll;
constexpr qint64 kNsecPerUsec = 1000ll;
constexpr qint64 kNsecPerMsec = 1000000ll;
constexpr qint64 kNsecPerSec = 1000000000ll;
constexpr qint64 kSecsPerDay = 24 * 60 * 60;
#endif // TIMECONSTANTS_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,171 +0,0 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef UTILITIES_H
#define UTILITIES_H
#include "config.h"
#include <memory>
#include <QtGlobal>
#include <QWindow>
#include <QByteArray>
#include <QFile>
#include <QSize>
#include <QDateTime>
#include <QLocale>
#include <QList>
#include <QMetaObject>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QColor>
#include <QRegion>
#include <QtEvents>
#include <QCryptographicHash>
#include "core/song.h"
class QWidget;
class QIODevice;
class QXmlStreamReader;
namespace Utilities {
QString PrettyTime(int seconds);
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 quint64 nanoseconds);
QString Ago(const qint64 seconds_since_epoch, const QLocale &locale);
QString PrettyFutureDate(const QDate date);
QString ColorToRgba(const QColor &color);
quint64 FileSystemCapacity(const QString &path);
quint64 FileSystemFreeSpace(const QString &path);
bool RemoveRecursive(const QString &path);
bool CopyRecursive(const QString &source, const QString &destination);
bool Copy(QIODevice *source, QIODevice *destination);
void OpenInFileBrowser(const QList<QUrl> &urls);
QByteArray Hmac(const QByteArray &key, const QByteArray &data, const QCryptographicHash::Algorithm method);
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);
// 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.
void ConsumeCurrentElement(QXmlStreamReader *reader);
// Advances the stream reader until it finds an element with the given name.
// Returns false if the end of the document was reached before finding a matching element.
bool ParseUntilElement(QXmlStreamReader *reader, const QString &name);
bool ParseUntilElementCI(QXmlStreamReader *reader, const QString &name);
// 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.
// Pass in the QMetaObject of the class that owns the enum, the string name of the enum and a valid value from that enum.
const char *EnumToString(const QMetaObject &meta, const char *name, int value);
QStringList Prepend(const QString &text, const QStringList &list);
QStringList Updateify(const QStringList &list);
// Get the path without the filename extension
QString PathWithoutFilenameExtension(const QString &filename);
QString FiddleFileExtension(const QString &filename, const QString &new_extension);
QString GetEnv(const QString &key);
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;
long SetThreadIOPriority(const IoPriority priority);
long GetThreadId();
QString GetRandomStringWithChars(const int len);
QString GetRandomStringWithCharsAndNumbers(const int len);
QString CryptographicRandomString(const int len);
QString GetRandomString(const int len, const QString &UseCharacters);
QString DesktopEnvironment();
QString Transliterate(const QString &accented_str);
QString MacAddress();
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);
bool IsColorDark(const QColor &color);
QByteArray ReadDataFromFile(const QString &filename);
QString MimeTypeFromData(const QByteArray &data);
#ifdef Q_OS_WIN
void enableBlurBehindWindow(QWindow *window, const QRegion &region);
#endif
} // namespace Utilities
class ScopedWCharArray {
public:
explicit ScopedWCharArray(const QString &str);
QString ToString() const { return QString::fromWCharArray(data_.get()); }
wchar_t *get() const { return data_.get(); }
explicit operator wchar_t *() const { return get(); }
qint64 characters() const { return chars_; }
qint64 bytes() const { return (chars_ + 1) * sizeof(wchar_t); }
private:
Q_DISABLE_COPY(ScopedWCharArray)
qint64 chars_;
std::unique_ptr<wchar_t[]> data_;
};
#endif // UTILITIES_H