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,8 +29,8 @@
#include <QFile>
#include <QTextStream>
#include "utilities/timeconstants.h"
#include "core/logging.h"
#include "core/timeconstants.h"
#include "core/messagehandler.h"
#include "tagreaderbase.h"
#include "tagreadertaglib.h"

View File

@ -92,7 +92,7 @@
#include "core/logging.h"
#include "core/messagehandler.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
class FileRefFactory {
public:

View File

@ -35,14 +35,28 @@ set(SOURCES
core/taskmanager.cpp
core/thread.cpp
core/urlhandler.cpp
core/utilities.cpp
core/imageutils.cpp
core/iconloader.cpp
core/standarditemiconloader.cpp
core/scopedtransaction.cpp
core/translations.cpp
core/systemtrayicon.cpp
utilities/strutils.cpp
utilities/envutils.cpp
utilities/colorutils.cpp
utilities/cryptutils.cpp
utilities/fileutils.cpp
utilities/diskutils.cpp
utilities/imageutils.cpp
utilities/macaddrutils.cpp
utilities/mimeutils.cpp
utilities/randutils.cpp
utilities/threadutils.cpp
utilities/timeutils.cpp
utilities/transliterate.cpp
utilities/xmlutils.cpp
utilities/filemanagerutils.cpp
engine/enginetype.cpp
engine/enginebase.cpp
engine/devicefinders.cpp
@ -782,8 +796,8 @@ optional_source(HAVE_AUDIOCD
# Platform specific - macOS
optional_source(APPLE
SOURCES
utilities/macosutils.mm
core/scoped_nsautorelease_pool.mm
core/mac_utilities.mm
core/mac_startup.mm
core/macsystemtrayicon.mm
core/macfslistener.mm
@ -804,8 +818,10 @@ optional_source(APPLE
# Platform specific - Windows
optional_source(WIN32
SOURCES
utilities/winutils.cpp
engine/directsounddevicefinder.cpp
engine/mmdevicefinder.cpp
core/scopedwchararray.cpp
core/windows7thumbbar.cpp
HEADERS
core/windows7thumbbar.h

View File

@ -34,9 +34,9 @@
#include "core/player.h"
#include "core/tagreaderclient.h"
#include "core/thread.h"
#include "core/utilities.h"
#include "core/song.h"
#include "core/logging.h"
#include "utilities/threadutils.h"
#include "collection.h"
#include "collectionwatcher.h"
#include "collectionbackend.h"

View File

@ -31,7 +31,7 @@
#include <QThread>
#include "core/song.h"
#include "core/utilities.h"
#include "utilities/threadutils.h"
class Application;
class Thread;

View File

@ -29,7 +29,7 @@
#include "core/filesystemmusicstorage.h"
#include "core/iconloader.h"
#include "core/musicstorage.h"
#include "core/utilities.h"
#include "utilities/diskutils.h"
#include "directory.h"
#include "collectionbackend.h"
#include "collectiondirectorymodel.h"

View File

@ -50,9 +50,9 @@
#include "core/application.h"
#include "core/iconloader.h"
#include "core/mimedata.h"
#include "core/utilities.h"
#include "core/musicstorage.h"
#include "core/deletefiles.h"
#include "utilities/filemanagerutils.h"
#include "collection.h"
#include "collectionbackend.h"
#include "collectiondirectorymodel.h"

View File

@ -46,10 +46,10 @@
#include "core/filesystemwatcherinterface.h"
#include "core/logging.h"
#include "core/timeconstants.h"
#include "core/tagreaderclient.h"
#include "core/taskmanager.h"
#include "core/imageutils.h"
#include "utilities/imageutils.h"
#include "utilities/timeconstants.h"
#include "directory.h"
#include "collectionbackend.h"
#include "collectionwatcher.h"

View File

@ -37,7 +37,7 @@
#include <QContextMenuEvent>
#include <QPaintEvent>
#include "core/imageutils.h"
#include "utilities/imageutils.h"
#include "covermanager/albumcoverchoicecontroller.h"
#include "contextview.h"

View File

@ -51,8 +51,9 @@
#include "core/application.h"
#include "core/player.h"
#include "core/song.h"
#include "core/utilities.h"
#include "core/iconloader.h"
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "widgets/resizabletextedit.h"
#include "engine/engine_fwd.h"
#include "engine/enginebase.h"

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,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

@ -0,0 +1,40 @@
/*
* 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 PLATFORMINTERFACE_H
#define PLATFORMINTERFACE_H
#include <QObject>
#include <QString>
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)
};
#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

@ -0,0 +1,29 @@
/*
* 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/>.
*
*/
#include <QString>
#include "scopedwchararray.h"
ScopedWCharArray::ScopedWCharArray(const QString &str)
: chars_(str.length()), data_(new wchar_t[chars_ + 1]) {
str.toWCharArray(data_.get());
data_[chars_] = '\0';
}

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;

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

View File

@ -53,8 +53,9 @@
#include <QSettings>
#include <QtEvents>
#include "core/utilities.h"
#include "core/imageutils.h"
#include "utilities/strutils.h"
#include "utilities/mimeutils.h"
#include "utilities/imageutils.h"
#include "core/application.h"
#include "core/song.h"
#include "core/iconloader.h"

View File

@ -36,10 +36,10 @@
#include <QNetworkReply>
#include "core/logging.h"
#include "core/utilities.h"
#include "core/imageutils.h"
#include "core/networkaccessmanager.h"
#include "core/networktimeouts.h"
#include "utilities/imageutils.h"
#include "utilities/mimeutils.h"
#include "albumcoverfetcher.h"
#include "albumcoverfetchersearch.h"
#include "coverprovider.h"

View File

@ -46,8 +46,10 @@
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "core/tagreaderclient.h"
#include "core/utilities.h"
#include "core/imageutils.h"
#include "utilities/transliterate.h"
#include "utilities/mimeutils.h"
#include "utilities/cryptutils.h"
#include "utilities/imageutils.h"
#include "settings/collectionsettingspage.h"
#include "organize/organizeformat.h"
#include "albumcoverloader.h"

View File

@ -64,10 +64,12 @@
#include <QSize>
#include <QtEvents>
#include "utilities/strutils.h"
#include "utilities/fileutils.h"
#include "utilities/imageutils.h"
#include "utilities/mimeutils.h"
#include "core/application.h"
#include "core/iconloader.h"
#include "core/utilities.h"
#include "core/imageutils.h"
#include "core/tagreaderclient.h"
#include "core/database.h"
#include "core/sqlrow.h"

View File

@ -48,7 +48,9 @@
#include <QtEvents>
#include "core/application.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "utilities/mimeutils.h"
#include "widgets/busyindicator.h"
#include "widgets/forcescrollperpixel.h"
#include "widgets/groupediconview.h"

View File

@ -31,7 +31,7 @@
#include <QNetworkRequest>
#include <QUrl>
#include "core/utilities.h"
#include "utilities/mimeutils.h"
#include "core/networkaccessmanager.h"
#include "widgets/busyindicator.h"
#include "albumcoverimageresult.h"

View File

@ -30,7 +30,7 @@
#include <QLabel>
#include <QFrame>
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "coversearchstatistics.h"
#include "coversearchstatisticsdialog.h"
#include "ui_coversearchstatisticsdialog.h"

View File

@ -44,7 +44,7 @@
#include "core/application.h"
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/utilities.h"
#include "utilities/cryptutils.h"
#include "albumcoverfetcher.h"
#include "jsoncoverprovider.h"
#include "discogscoverprovider.h"

View File

@ -44,8 +44,8 @@
#include "core/application.h"
#include "core/networkaccessmanager.h"
#include "core/logging.h"
#include "core/utilities.h"
#include "core/timeconstants.h"
#include "utilities/randutils.h"
#include "utilities/timeconstants.h"
#include "internet/localredirectserver.h"
#include "albumcoverfetcher.h"
#include "jsoncoverprovider.h"

View File

@ -39,7 +39,7 @@
#include "cddasongloader.h"
#include "core/logging.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
CddaSongLoader::CddaSongLoader(const QUrl &url, QObject *parent)
: QObject(parent),

View File

@ -50,8 +50,8 @@
#include "core/logging.h"
#include "core/musicstorage.h"
#include "core/taskmanager.h"
#include "core/utilities.h"
#include "core/simpletreemodel.h"
#include "utilities/strutils.h"
#include "filesystemdevice.h"
#include "connecteddevice.h"
#include "devicelister.h"

View File

@ -54,7 +54,7 @@
#include "core/mergedproxymodel.h"
#include "core/mimedata.h"
#include "core/musicstorage.h"
#include "core/utilities.h"
#include "utilities/colorutils.h"
#include "organize/organizedialog.h"
#include "organize/organizeerrordialog.h"
#include "collection/collectiondirectorymodel.h"

View File

@ -42,7 +42,7 @@
#include <QJsonArray>
#include "core/logging.h"
#include "core/utilities.h"
#include "utilities/diskutils.h"
#include "udisks2lister.h"

View File

@ -73,8 +73,10 @@
#include "core/iconloader.h"
#include "core/logging.h"
#include "core/tagreaderclient.h"
#include "core/utilities.h"
#include "core/imageutils.h"
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "utilities/imageutils.h"
#include "utilities/cryptutils.h"
#include "widgets/busyindicator.h"
#include "widgets/lineedit.h"
#include "collection/collectionbackend.h"

View File

@ -30,8 +30,8 @@
#include <QUrl>
#include <QSettings>
#include "core/utilities.h"
#include "core/timeconstants.h"
#include "utilities/envutils.h"
#include "utilities/timeconstants.h"
#include "core/networkproxyfactory.h"
#include "engine_fwd.h"
#include "enginebase.h"

View File

@ -49,8 +49,8 @@
#include "core/logging.h"
#include "core/taskmanager.h"
#include "core/timeconstants.h"
#include "core/signalchecker.h"
#include "utilities/timeconstants.h"
#include "enginebase.h"
#include "enginetype.h"
#include "gstengine.h"

View File

@ -38,7 +38,7 @@
#include <QString>
#include <QUrl>
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "engine_fwd.h"
#include "enginebase.h"
#include "gststartup.h"

View File

@ -48,7 +48,7 @@
#include "core/logging.h"
#include "core/signalchecker.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "settings/backendsettingspage.h"
#include "enginebase.h"
#include "gstengine.h"

View File

@ -36,7 +36,7 @@
#include <QAbstractEventDispatcher>
#include "core/logging.h"
#include "core/utilities.h"
#include "utilities/envutils.h"
#ifdef HAVE_MOODBAR
# include "ext/gstmoodbar/gstmoodbarplugin.h"

View File

@ -29,9 +29,9 @@
#include <QByteArray>
#include <QUrl>
#include "core/timeconstants.h"
#include "core/taskmanager.h"
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "engine_fwd.h"
#include "enginebase.h"
#include "enginetype.h"

View File

@ -34,7 +34,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "jsonlyricsprovider.h"
#include "lyricsfetcher.h"
#include "lyricsprovider.h"

View File

@ -31,7 +31,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
#include "chartlyricsprovider.h"

View File

@ -44,8 +44,9 @@
#include <QMessageBox>
#include "core/logging.h"
#include "core/utilities.h"
#include "core/networkaccessmanager.h"
#include "utilities/strutils.h"
#include "utilities/randutils.h"
#include "internet/localredirectserver.h"
#include "jsonlyricsprovider.h"
#include "lyricsfetcher.h"

View File

@ -32,7 +32,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
#include "lololyricsprovider.h"

View File

@ -23,7 +23,7 @@
#include <QString>
#include <QRegularExpression>
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "core/networkaccessmanager.h"
#include "lyricsprovider.h"

View File

@ -35,7 +35,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "jsonlyricsprovider.h"
#include "lyricsfetcher.h"
#include "musixmatchlyricsprovider.h"

View File

@ -29,7 +29,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "lyricsfetcher.h"
#include "jsonlyricsprovider.h"
#include "ovhlyricsprovider.h"

View File

@ -77,10 +77,14 @@
# endif
#endif // HAVE_QTSPARKLE
#ifdef Q_OS_MACOS
# include "utilities/macosutils.h"
# include "core/mac_startup.h"
#endif
#ifdef HAVE_DBUS
# include "core/mpris2.h"
#endif
#include "core/utilities.h"
#include "core/metatypes.h"
#include "core/iconloader.h"
#include "core/mainwindow.h"
@ -175,7 +179,9 @@ int main(int argc, char *argv[]) {
qsrand(t);
#endif
#ifdef Q_OS_MACOS
Utilities::IncreaseFDLimit();
#endif
QGuiApplication::setQuitOnLastWindowClosed(false);

View File

@ -29,7 +29,7 @@
#include "core/logging.h"
#include "core/signalchecker.h"
#include "core/utilities.h"
#include "utilities/threadutils.h"
#include "moodbar/moodbarbuilder.h"
#include "ext/gstmoodbar/gstfastspectrum.h"

View File

@ -43,8 +43,8 @@
#include "acoustidclient.h"
#include "core/networkaccessmanager.h"
#include "core/networktimeouts.h"
#include "core/timeconstants.h"
#include "core/logging.h"
#include "utilities/timeconstants.h"
const char *AcoustidClient::kClientId = "0qjUoxbowg";
const char *AcoustidClient::kUrl = "https://api.acoustid.org/v2/lookup";

View File

@ -46,7 +46,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/networktimeouts.h"
#include "core/utilities.h"
#include "utilities/xmlutils.h"
#include "musicbrainzclient.h"
const char *MusicBrainzClient::kTrackUrl = "https://musicbrainz.org/ws/2/recording/";

View File

@ -29,7 +29,7 @@
#include <QFutureWatcher>
#include <QString>
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "engine/chromaprinter.h"
#include "acoustidclient.h"
#include "musicbrainzclient.h"

View File

@ -34,11 +34,11 @@
#include <QImage>
#include "core/logging.h"
#include "core/utilities.h"
#include "core/taskmanager.h"
#include "core/musicstorage.h"
#include "core/tagreaderclient.h"
#include "core/song.h"
#include "utilities/strutils.h"
#include "organize.h"
#ifdef HAVE_GSTREAMER
# include "transcoder/transcoder.h"

View File

@ -59,7 +59,7 @@
#include "core/iconloader.h"
#include "core/musicstorage.h"
#include "core/tagreaderclient.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "widgets/freespacebar.h"
#include "widgets/linetextedit.h"
#include "collection/collectionbackend.h"

View File

@ -38,9 +38,9 @@
#include <QTextFormat>
#include "core/arraysize.h"
#include "core/timeconstants.h"
#include "core/utilities.h"
#include "core/song.h"
#include "utilities/transliterate.h"
#include "utilities/timeconstants.h"
#include "organizeformat.h"

View File

@ -33,12 +33,12 @@
#include "core/application.h"
#include "core/logging.h"
#include "core/utilities.h"
#ifdef Q_OS_MACOS
# include "core/macsystemtrayicon.h"
#else
# include "core/qtsystemtrayicon.h"
#endif
#include "utilities/strutils.h"
#include "covermanager/currentalbumcoverloader.h"
const char *OSDBase::kSettingsGroup = "OSD";

View File

@ -64,7 +64,9 @@
# include <windows.h>
#endif
#include "core/utilities.h"
#ifdef Q_OS_WIN
# include "utilities/winutils.h"
#endif
using namespace std::chrono_literals;

View File

@ -63,7 +63,7 @@
#include "core/mimedata.h"
#include "core/tagreaderclient.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "collection/collection.h"
#include "collection/collectionbackend.h"
#include "collection/collectionplaylistitem.h"

View File

@ -65,7 +65,8 @@
#include "core/logging.h"
#include "core/song.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "collection/collectionbackend.h"
#include "playlist/playlist.h"
#include "playlistdelegates.h"

View File

@ -44,7 +44,7 @@
#include "core/application.h"
#include "core/player.h"
#include "core/utilities.h"
#include "utilities/timeutils.h"
#include "collection/collectionbackend.h"
#include "covermanager/currentalbumcoverloader.h"
#include "organize/organizeformat.h"

View File

@ -30,7 +30,7 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include "core/utilities.h"
#include "utilities/xmlutils.h"
#include "settings/playlistsettingspage.h"
#include "xmlparser.h"
#include "asxparser.h"

View File

@ -33,7 +33,7 @@
#include <QTextStream>
#include "core/logging.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "settings/playlistsettingspage.h"
#include "parserbase.h"
#include "cueparser.h"

View File

@ -28,7 +28,7 @@
#include <QSettings>
#include "core/logging.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "settings/playlistsettingspage.h"
#include "parserbase.h"
#include "m3uparser.h"

View File

@ -29,7 +29,7 @@
#include <QRegularExpressionMatch>
#include <QTextStream>
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "settings/playlistsettingspage.h"
#include "parserbase.h"
#include "plsparser.h"

View File

@ -29,7 +29,7 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include "core/utilities.h"
#include "utilities/xmlutils.h"
#include "settings/playlistsettingspage.h"
#include "xmlparser.h"
#include "wplparser.h"

View File

@ -29,8 +29,8 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include "core/timeconstants.h"
#include "core/utilities.h"
#include "utilities/xmlutils.h"
#include "utilities/timeconstants.h"
#include "settings/playlistsettingspage.h"
#include "xmlparser.h"
#include "xspfparser.h"

View File

@ -35,9 +35,9 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/application.h"
#include "core/imageutils.h"
#include "utilities/imageutils.h"
#include "utilities/timeconstants.h"
#include "qobuzservice.h"
#include "qobuzurlhandler.h"
#include "qobuzbaserequest.h"

View File

@ -44,7 +44,7 @@
#include "core/networkaccessmanager.h"
#include "core/database.h"
#include "core/song.h"
#include "core/utilities.h"
#include "utilities/macaddrutils.h"
#include "internet/internetsearchview.h"
#include "collection/collectionbackend.h"
#include "collection/collectionmodel.h"

View File

@ -36,7 +36,7 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "utilities/timeconstants.h"
#include "qobuzservice.h"
#include "qobuzbaserequest.h"
#include "qobuzstreamurlrequest.h"

View File

@ -35,7 +35,7 @@
#include <QAbstractItemModel>
#include <QAbstractProxyModel>
#include "core/utilities.h"
#include "utilities/timeutils.h"
#include "playlist/playlist.h"
#include "queue.h"

View File

@ -43,8 +43,8 @@
#include "core/application.h"
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "internet/localredirectserver.h"
#include "settings/scrobblersettingspage.h"

View File

@ -47,8 +47,8 @@
#include "core/application.h"
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "internet/localredirectserver.h"
#include "settings/scrobblersettingspage.h"

View File

@ -27,8 +27,8 @@
#include "core/application.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "internet/internetservices.h"
#include "settings/subsonicsettingspage.h"
#include "subsonic/subsonicservice.h"

View File

@ -41,7 +41,7 @@
#include <QSettings>
#include "appearancesettingspage.h"
#include "core/utilities.h"
#include "utilities/colorutils.h"
#include "core/appearance.h"
#include "core/iconloader.h"
#include "core/stylehelper.h"

View File

@ -44,7 +44,8 @@
#include "core/application.h"
#include "core/iconloader.h"
#include "core/utilities.h"
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "collection/collection.h"
#include "collection/collectionmodel.h"
#include "collection/collectiondirectorymodel.h"

View File

@ -40,7 +40,7 @@
#include "core/iconloader.h"
#include "core/logging.h"
#include "core/utilities.h"
#include "utilities/envutils.h"
#include "globalshortcuts/globalshortcutgrabber.h"
#include "globalshortcuts/globalshortcutsmanager.h"
#include "settingspage.h"

View File

@ -34,7 +34,7 @@
#include <QKeyEvent>
#include <QEnterEvent>
#include "core/utilities.h"
#include "utilities/colorutils.h"
#include "core/iconloader.h"
#include "playlist/playlist.h"
#include "playlist/playlistdelegates.h"

View File

@ -39,7 +39,7 @@
#include <QJsonObject>
#include <QJsonValue>
#include "core/utilities.h"
#include "utilities/randutils.h"
#include "subsonicservice.h"
#include "subsonicbaserequest.h"

View File

@ -41,9 +41,9 @@
#include "core/application.h"
#include "core/logging.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/imageutils.h"
#include "core/networktimeouts.h"
#include "utilities/imageutils.h"
#include "utilities/timeconstants.h"
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
#include "subsonicbaserequest.h"

View File

@ -42,7 +42,7 @@
#include <QSettings>
#include <QSortFilterProxyModel>
#include "core/utilities.h"
#include "utilities/randutils.h"
#include "core/application.h"
#include "core/player.h"
#include "core/logging.h"

View File

@ -35,9 +35,9 @@
#include "core/logging.h"
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/application.h"
#include "core/imageutils.h"
#include "utilities/timeconstants.h"
#include "utilities/imageutils.h"
#include "tidalservice.h"
#include "tidalurlhandler.h"
#include "tidalbaserequest.h"

View File

@ -47,8 +47,8 @@
#include "core/networkaccessmanager.h"
#include "core/database.h"
#include "core/song.h"
#include "core/utilities.h"
#include "core/timeconstants.h"
#include "utilities/randutils.h"
#include "utilities/timeconstants.h"
#include "internet/internetsearchview.h"
#include "collection/collectionbackend.h"
#include "collection/collectionmodel.h"

View File

@ -0,0 +1,41 @@
/*
* Strawberry Music Player
* 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/>.
*
*/
#include <QString>
#include <QColor>
#include "colorutils.h"
namespace Utilities {
QString ColorToRgba(const QColor &c) {
return QString("rgba(%1, %2, %3, %4)")
.arg(c.red())
.arg(c.green())
.arg(c.blue())
.arg(c.alpha());
}
bool IsColorDark(const QColor &color) {
return ((30 * color.red() + 59 * color.green() + 11 * color.blue()) / 100) <= 130;
}
} // namespace Utilities

View File

@ -0,0 +1,33 @@
/*
* Strawberry Music Player
* 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 COLORUTILS_H
#define COLORUTILS_H
#include <QString>
#include <QColor>
namespace Utilities {
QString ColorToRgba(const QColor &color);
bool IsColorDark(const QColor &color);
} // namespace Utilities
#endif // COLORUTILS_H

View File

@ -0,0 +1,75 @@
/*
* Strawberry Music Player
* 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/>.
*
*/
#include <QByteArray>
#include <QString>
#include <QCryptographicHash>
#include "cryptutils.h"
namespace Utilities {
QByteArray Hmac(const QByteArray &key, const QByteArray &data, const QCryptographicHash::Algorithm method) {
constexpr int block_size = 64;
Q_ASSERT(key.length() <= block_size);
QByteArray inner_padding(block_size, static_cast<char>(0x36));
QByteArray outer_padding(block_size, static_cast<char>(0x5c));
for (int i = 0; i < key.length(); ++i) {
inner_padding[i] = static_cast<char>(inner_padding[i] ^ key[i]);
outer_padding[i] = static_cast<char>(outer_padding[i] ^ key[i]);
}
QByteArray part;
part.append(inner_padding);
part.append(data);
QByteArray total;
total.append(outer_padding);
total.append(QCryptographicHash::hash(part, method));
return QCryptographicHash::hash(total, method);
}
QByteArray HmacSha256(const QByteArray &key, const QByteArray &data) {
return Hmac(key, data, QCryptographicHash::Sha256);
}
QByteArray HmacMd5(const QByteArray &key, const QByteArray &data) {
return Hmac(key, data, QCryptographicHash::Md5);
}
QByteArray HmacSha1(const QByteArray &key, const QByteArray &data) {
return Hmac(key, data, QCryptographicHash::Sha1);
}
QByteArray Sha1CoverHash(const QString &artist, const QString &album) {
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(artist.toLower().toUtf8());
hash.addData(album.toLower().toUtf8());
return hash.result();
}
} // namespace Utilities

View File

@ -0,0 +1,37 @@
/*
* Strawberry Music Player
* 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 CRYPTUTILS_H
#define CRYPTUTILS_H
#include <QByteArray>
#include <QString>
#include <QCryptographicHash>
namespace Utilities {
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);
} // namespace Utilities
#endif // CRYPTUTILS_H

View File

@ -0,0 +1,76 @@
/*
* 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/>.
*
*/
#include <QtGlobal>
#ifdef Q_OS_UNIX
# include <sys/statvfs.h>
#endif
#ifdef Q_OS_WIN
# include <windows.h>
#endif
#include <QString>
#include <QDir>
#include "diskutils.h"
#ifdef Q_OS_WIN32
# include "core/scopedwchararray.h"
#endif
namespace Utilities {
quint64 FileSystemCapacity(const QString &path) {
#if defined(Q_OS_UNIX)
struct statvfs fs_info {};
if (statvfs(path.toLocal8Bit().constData(), &fs_info) == 0)
return static_cast<quint64>(fs_info.f_blocks) * static_cast<quint64>(fs_info.f_bsize);
#elif defined(Q_OS_WIN32)
_ULARGE_INTEGER ret;
ScopedWCharArray wchar(QDir::toNativeSeparators(path));
if (GetDiskFreeSpaceEx(wchar.get(), nullptr, &ret, nullptr) != 0)
return ret.QuadPart;
#endif
return 0;
}
quint64 FileSystemFreeSpace(const QString &path) {
#if defined(Q_OS_UNIX)
struct statvfs fs_info {};
if (statvfs(path.toLocal8Bit().constData(), &fs_info) == 0)
return static_cast<quint64>(fs_info.f_bavail) * static_cast<quint64>(fs_info.f_bsize);
#elif defined(Q_OS_WIN32)
_ULARGE_INTEGER ret;
ScopedWCharArray wchar(QDir::toNativeSeparators(path));
if (GetDiskFreeSpaceEx(wchar.get(), &ret, nullptr, nullptr) != 0)
return ret.QuadPart;
#endif
return 0;
}
} // namespace Utilities

33
src/utilities/diskutils.h Normal file
View File

@ -0,0 +1,33 @@
/*
* 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 DISKUTILS_H
#define DISKUTILS_H
#include <QString>
namespace Utilities {
quint64 FileSystemCapacity(const QString &path);
quint64 FileSystemFreeSpace(const QString &path);
} // namespace Utilities
#endif // DISKUTILS_H

View File

@ -0,0 +1,73 @@
/*
* Strawberry Music Player
* 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/>.
*
*/
#include <cstdlib>
#include <QtGlobal>
#include <QByteArray>
#include <QString>
#include <QSettings>
#include "envutils.h"
namespace Utilities {
QString GetEnv(const QString &key) {
const QByteArray key_data = key.toLocal8Bit();
return QString::fromLocal8Bit(qgetenv(key_data.constData()));
}
void SetEnv(const char *key, const QString &value) {
#ifdef Q_OS_WIN32
_putenv(QString("%1=%2").arg(key, value).toLocal8Bit().constData());
#else
setenv(key, value.toLocal8Bit().constData(), 1);
#endif
}
QString DesktopEnvironment() {
const QString de = GetEnv("XDG_CURRENT_DESKTOP");
if (!de.isEmpty()) return de;
if (!qEnvironmentVariableIsEmpty("KDE_FULL_SESSION")) return "KDE";
if (!qEnvironmentVariableIsEmpty("GNOME_DESKTOP_SESSION_ID")) return "Gnome";
QString session = GetEnv("DESKTOP_SESSION");
qint64 slash = session.lastIndexOf('/');
if (slash != -1) {
QSettings desktop_file(QString(session + ".desktop"), QSettings::IniFormat);
desktop_file.beginGroup("Desktop Entry");
QString name = desktop_file.value("DesktopNames").toString();
desktop_file.endGroup();
if (!name.isEmpty()) return name;
session = session.mid(slash + 1);
}
if (session == "kde") return "KDE";
else if (session == "gnome") return "Gnome";
else if (session == "xfce") return "XFCE";
return "Unknown";
}
} // namespace Utilities

33
src/utilities/envutils.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Strawberry Music Player
* 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 ENVUTILS_H
#define ENVUTILS_H
#include <QString>
namespace Utilities {
QString GetEnv(const QString &key);
void SetEnv(const char *key, const QString &value);
QString DesktopEnvironment();
} // namespace Utilities
#endif // ENVUTILS_H

View File

@ -0,0 +1,167 @@
/*
* Strawberry Music Player
* 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/>.
*
*/
#include <QObject>
#include <QList>
#include <QMap>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QRegularExpression>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QSettings>
#include <QProcess>
#include <QDesktopServices>
#include <QMessageBox>
#include "filemanagerutils.h"
namespace Utilities {
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
void OpenInFileManager(const QString &path, const QUrl &url);
void OpenInFileManager(const QString &path, const QUrl &url) {
if (!url.isLocalFile()) return;
QProcess proc;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
proc.startCommand("xdg-mime query default inode/directory");
#else
proc.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory");
#endif
proc.waitForFinished();
QString desktop_file = proc.readLine().simplified();
QStringList data_dirs = QString(qgetenv("XDG_DATA_DIRS")).split(":");
QString command;
QStringList command_params;
for (const QString &data_dir : data_dirs) {
QString desktop_file_path = QString("%1/applications/%2").arg(data_dir, desktop_file);
if (!QFile::exists(desktop_file_path)) continue;
QSettings setting(desktop_file_path, QSettings::IniFormat);
setting.beginGroup("Desktop Entry");
if (setting.contains("Exec")) {
QString cmd = setting.value("Exec").toString();
if (cmd.isEmpty()) break;
cmd = cmd.remove(QRegularExpression("[%][a-zA-Z]*( |$)", QRegularExpression::CaseInsensitiveOption));
# if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
command_params = cmd.split(' ', Qt::SkipEmptyParts);
# else
command_params = cmd.split(' ', QString::SkipEmptyParts);
# endif
command = command_params.first();
command_params.removeFirst();
}
setting.endGroup();
if (!command.isEmpty()) break;
}
if (command.startsWith("/usr/bin/")) {
command = command.split("/").last();
}
if (command.isEmpty() || command == "exo-open") {
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}
else if (command.startsWith("nautilus")) {
proc.startDetached(command, QStringList() << command_params << "--select" << url.toLocalFile());
}
else if (command.startsWith("dolphin") || command.startsWith("konqueror") || command.startsWith("kfmclient")) {
proc.startDetached(command, QStringList() << command_params << "--select" << "--new-window" << url.toLocalFile());
}
else if (command.startsWith("caja")) {
proc.startDetached(command, QStringList() << command_params << "--no-desktop" << path);
}
else if (command.startsWith("pcmanfm") || command.startsWith("thunar") || command.startsWith("spacefm")) {
proc.startDetached(command, QStringList() << command_params << path);
}
else {
proc.startDetached(command, QStringList() << command_params << url.toLocalFile());
}
}
#endif
#ifdef Q_OS_MACOS
// Better than openUrl(dirname(path)) - also highlights file at path
void RevealFileInFinder(const QString &path) {
QProcess::execute("/usr/bin/open", QStringList() << "-R" << path);
}
#endif // Q_OS_MACOS
#ifdef Q_OS_WIN
void ShowFileInExplorer(const QString &path);
void ShowFileInExplorer(const QString &path) {
QProcess::execute("explorer.exe", QStringList() << "/select," << QDir::toNativeSeparators(path));
}
#endif
void OpenInFileBrowser(const QList<QUrl> &urls) {
QMap<QString, QUrl> dirs;
for (const QUrl &url : urls) {
if (!url.isLocalFile()) {
continue;
}
QString path = url.toLocalFile();
if (!QFile::exists(path)) continue;
const QString directory = QFileInfo(path).dir().path();
if (dirs.contains(directory)) continue;
dirs.insert(directory, url);
}
if (dirs.count() > 50) {
QMessageBox messagebox(QMessageBox::Critical, QObject::tr("Show in file browser"), QObject::tr("Too many songs selected."));
messagebox.exec();
return;
}
if (dirs.count() > 5) {
QMessageBox messagebox(QMessageBox::Information, QObject::tr("Show in file browser"), QObject::tr("%1 songs in %2 different directories selected, are you sure you want to open them all?").arg(urls.count()).arg(dirs.count()), QMessageBox::Open|QMessageBox::Cancel);
messagebox.setTextFormat(Qt::RichText);
int result = messagebox.exec();
switch (result) {
case QMessageBox::Open:
break;
case QMessageBox::Cancel:
default:
return;
}
}
QMap<QString, QUrl>::iterator i;
for (i = dirs.begin(); i != dirs.end(); ++i) {
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
OpenInFileManager(i.key(), i.value());
#elif defined(Q_OS_MACOS)
// Revealing multiple files in the finder only opens one window, so it also makes sense to reveal at most one per directory
RevealFileInFinder(i.value().toLocalFile());
#elif defined(Q_OS_WIN32)
ShowFileInExplorer(i.value().toLocalFile());
#endif
}
}
}

View File

@ -0,0 +1,32 @@
/*
* Strawberry Music Player
* 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 FILEMANAGERUTILS_H
#define FILEMANAGERUTILS_H
#include <QList>
#include <QUrl>
namespace Utilities {
void OpenInFileBrowser(const QList<QUrl> &urls);
} // namespace Utilities
#endif // FILEMANAGERUTILS_H

127
src/utilities/fileutils.cpp Normal file
View File

@ -0,0 +1,127 @@
/*
* 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/>.
*
*/
#include <memory>
#include <QtGlobal>
#include <QByteArray>
#include <QString>
#include <QIODevice>
#include <QDir>
#include <QFile>
#include "core/logging.h"
#include "fileutils.h"
namespace Utilities {
QByteArray ReadDataFromFile(const QString &filename) {
QFile file(filename);
QByteArray data;
if (file.open(QIODevice::ReadOnly)) {
data = file.readAll();
file.close();
}
else {
qLog(Error) << "Failed to open file" << filename << "for reading:" << file.errorString();
}
return data;
}
bool Copy(QIODevice *source, QIODevice *destination) {
if (!source->open(QIODevice::ReadOnly)) return false;
if (!destination->open(QIODevice::WriteOnly)) return false;
const qint64 bytes = source->size();
std::unique_ptr<char[]> data(new char[bytes]);
qint64 pos = 0;
qint64 bytes_read = 0;
do {
bytes_read = source->read(data.get() + pos, bytes - pos);
if (bytes_read == -1) return false;
pos += bytes_read;
} while (bytes_read > 0 && pos != bytes);
pos = 0;
qint64 bytes_written = 0;
do {
bytes_written = destination->write(data.get() + pos, bytes - pos);
if (bytes_written == -1) return false;
pos += bytes_written;
} while (bytes_written > 0 && pos != bytes);
return true;
}
bool CopyRecursive(const QString &source, const QString &destination) {
// Make the destination directory
QString dir_name = source.section('/', -1, -1);
QString dest_path = destination + "/" + dir_name;
QDir().mkpath(dest_path);
QDir dir(source);
for (const QString &child : dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs)) {
if (!CopyRecursive(source + "/" + child, dest_path)) {
qLog(Warning) << "Failed to copy dir" << source + "/" + child << "to" << dest_path;
return false;
}
}
for (const QString &child : dir.entryList(QDir::NoDotAndDotDot | QDir::Files)) {
if (!QFile::copy(source + "/" + child, dest_path + "/" + child)) {
qLog(Warning) << "Failed to copy file" << source + "/" + child << "to" << dest_path;
return false;
}
}
return true;
}
bool RemoveRecursive(const QString &path) {
QDir dir(path);
for (const QString &child : dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Hidden)) {
if (!RemoveRecursive(path + "/" + child)) {
return false;
}
}
for (const QString &child : dir.entryList(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden)) {
if (!QFile::remove(path + "/" + child)) {
return false;
}
}
return dir.rmdir(path);
}
} // namespace Utilities

37
src/utilities/fileutils.h Normal file
View File

@ -0,0 +1,37 @@
/*
* 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 FILEUTILS_H
#define FILEUTILS_H
#include <QString>
class QIODevice;
namespace Utilities {
QByteArray ReadDataFromFile(const QString &filename);
bool Copy(QIODevice *source, QIODevice *destination);
bool CopyRecursive(const QString &source, const QString &destination);
bool RemoveRecursive(const QString &path);
} // namespace Utilities
#endif // FILEUTILS_H

View File

@ -17,8 +17,6 @@
*
*/
#include "config.h"
#include <QList>
#include <QBuffer>
#include <QByteArray>
@ -32,7 +30,8 @@
#include <QSize>
#include "imageutils.h"
#include "core/utilities.h"
#include "fileutils.h"
#include "mimeutils.h"
#include "core/tagreaderclient.h"
QStringList ImageUtils::kSupportedImageMimeTypes;

Some files were not shown because too many files have changed in this diff Show More