Add FileType singleton
This singleton is used to get the mimetype info for files as well as supported formats for Images and AnimatedImages
This commit is contained in:
@@ -17,6 +17,7 @@ add_executable(neochat
|
|||||||
sortfilterroomlistmodel.cpp
|
sortfilterroomlistmodel.cpp
|
||||||
chatdocumenthandler.cpp
|
chatdocumenthandler.cpp
|
||||||
devicesmodel.cpp
|
devicesmodel.cpp
|
||||||
|
filetypesingleton.cpp
|
||||||
../res.qrc
|
../res.qrc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
114
src/filetypesingleton.cpp
Normal file
114
src/filetypesingleton.cpp
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
/* SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
|
||||||
|
* SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "filetypesingleton.h"
|
||||||
|
#include <QImageReader>
|
||||||
|
#include <QMovie>
|
||||||
|
|
||||||
|
static QStringList byteArrayListToStringList(const QByteArrayList &byteArrayList)
|
||||||
|
{
|
||||||
|
QStringList stringList;
|
||||||
|
for(const QByteArray &byteArray : byteArrayList) {
|
||||||
|
stringList.append(QString::fromLocal8Bit(byteArray));
|
||||||
|
}
|
||||||
|
return stringList;
|
||||||
|
}
|
||||||
|
|
||||||
|
class FileTypeSingletonPrivate
|
||||||
|
{
|
||||||
|
Q_DECLARE_PUBLIC(FileTypeSingleton)
|
||||||
|
Q_DISABLE_COPY(FileTypeSingletonPrivate)
|
||||||
|
public:
|
||||||
|
FileTypeSingletonPrivate(FileTypeSingleton *qq);
|
||||||
|
FileTypeSingleton * const q_ptr;
|
||||||
|
QMimeDatabase mimetypeDatabase;
|
||||||
|
QStringList supportedImageFormats = byteArrayListToStringList(QImageReader::supportedImageFormats());
|
||||||
|
QStringList supportedAnimatedImageFormats = byteArrayListToStringList(QMovie::supportedFormats());
|
||||||
|
};
|
||||||
|
|
||||||
|
FileTypeSingletonPrivate::FileTypeSingletonPrivate(FileTypeSingleton* qq) : q_ptr(qq)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FileTypeSingleton::FileTypeSingleton(QObject* parent)
|
||||||
|
: QObject(parent)
|
||||||
|
, d_ptr(new FileTypeSingletonPrivate(this))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FileTypeSingleton::~FileTypeSingleton() noexcept
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForName(const QString& nameOrAlias) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForName(nameOrAlias);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForFile(const QString& fileName, MatchMode mode) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForFile(fileName, static_cast<QMimeDatabase::MatchMode>(mode));
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForFile(const QFileInfo& fileInfo, MatchMode mode) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForFile(fileInfo, static_cast<QMimeDatabase::MatchMode>(mode));
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<QMimeType> FileTypeSingleton::mimeTypesForFileName(const QString& fileName) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypesForFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForData(const QByteArray& data) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForData(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForData(QIODevice* device) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForData(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForUrl(const QUrl& url) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForUrl(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForFileNameAndData(const QString& fileName, QIODevice* device) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForFileNameAndData(fileName, device);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeType FileTypeSingleton::mimeTypeForFileNameAndData(const QString& fileName, const QByteArray& data) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.mimeTypeForFileNameAndData(fileName, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString FileTypeSingleton::suffixForFileName(const QString& fileName) const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->mimetypeDatabase.suffixForFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList FileTypeSingleton::supportedImageFormats() const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->supportedImageFormats;
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList FileTypeSingleton::supportedAnimatedImageFormats() const
|
||||||
|
{
|
||||||
|
Q_D(const FileTypeSingleton);
|
||||||
|
return d->supportedAnimatedImageFormats;
|
||||||
|
}
|
||||||
63
src/filetypesingleton.h
Normal file
63
src/filetypesingleton.h
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/* SPDX-FileCopyrightText: 2015 Klaralvdalens Datakonsult AB
|
||||||
|
* SPDX-FileCopyrightText: 2016 The Qt Company Ltd.
|
||||||
|
* SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
|
||||||
|
* SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FILETYPESINGLETON_H
|
||||||
|
#define FILETYPESINGLETON_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <qqml.h>
|
||||||
|
#include <QMimeDatabase>
|
||||||
|
|
||||||
|
class FileTypeSingletonPrivate;
|
||||||
|
|
||||||
|
class FileTypeSingleton : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(QStringList supportedImageFormats READ supportedImageFormats CONSTANT FINAL)
|
||||||
|
Q_PROPERTY(QStringList supportedAnimatedImageFormats READ supportedAnimatedImageFormats CONSTANT FINAL)
|
||||||
|
QML_NAMED_ELEMENT(FileType)
|
||||||
|
QML_SINGLETON
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit FileTypeSingleton(QObject *parent = nullptr);
|
||||||
|
~FileTypeSingleton();
|
||||||
|
|
||||||
|
// Most of the code in this public section was copy/pasted from qmimedatabase.h
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForName(const QString &nameOrAlias) const;
|
||||||
|
|
||||||
|
enum MatchMode {
|
||||||
|
MatchDefault,
|
||||||
|
MatchExtension,
|
||||||
|
MatchContent
|
||||||
|
};
|
||||||
|
Q_ENUM(MatchMode)
|
||||||
|
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForFile(const QString &fileName, MatchMode mode = MatchDefault) const;
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode = MatchDefault) const;
|
||||||
|
Q_INVOKABLE QList<QMimeType> mimeTypesForFileName(const QString &fileName) const;
|
||||||
|
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForData(const QByteArray &data) const;
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForData(QIODevice *device) const;
|
||||||
|
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForUrl(const QUrl &url) const;
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const;
|
||||||
|
Q_INVOKABLE QMimeType mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const;
|
||||||
|
|
||||||
|
Q_INVOKABLE QString suffixForFileName(const QString &fileName) const;
|
||||||
|
|
||||||
|
// These return a list of file extentions, not mimetypes
|
||||||
|
QStringList supportedImageFormats() const;
|
||||||
|
QStringList supportedAnimatedImageFormats() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const QScopedPointer<FileTypeSingletonPrivate> d_ptr;
|
||||||
|
Q_DECLARE_PRIVATE(FileTypeSingleton)
|
||||||
|
Q_DISABLE_COPY(FileTypeSingleton)
|
||||||
|
};
|
||||||
|
|
||||||
|
QML_DECLARE_TYPE(FileTypeSingleton)
|
||||||
|
|
||||||
|
#endif // MIMETYPESINGLETON_H
|
||||||
@@ -31,6 +31,7 @@
|
|||||||
#include "csapi/leaving.h"
|
#include "csapi/leaving.h"
|
||||||
#include "devicesmodel.h"
|
#include "devicesmodel.h"
|
||||||
#include "emojimodel.h"
|
#include "emojimodel.h"
|
||||||
|
#include "filetypesingleton.h"
|
||||||
#include "matriximageprovider.h"
|
#include "matriximageprovider.h"
|
||||||
#include "messageeventmodel.h"
|
#include "messageeventmodel.h"
|
||||||
#include "neochatconfig.h"
|
#include "neochatconfig.h"
|
||||||
@@ -89,10 +90,12 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
Clipboard clipboard;
|
Clipboard clipboard;
|
||||||
auto config = NeoChatConfig::self();
|
auto config = NeoChatConfig::self();
|
||||||
|
FileTypeSingleton fileTypeSingleton;
|
||||||
|
|
||||||
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Controller", &Controller::instance());
|
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Controller", &Controller::instance());
|
||||||
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Clipboard", &clipboard);
|
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Clipboard", &clipboard);
|
||||||
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Config", config);
|
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Config", config);
|
||||||
|
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "FileType", &fileTypeSingleton);
|
||||||
qmlRegisterType<AccountListModel>("org.kde.neochat", 1, 0, "AccountListModel");
|
qmlRegisterType<AccountListModel>("org.kde.neochat", 1, 0, "AccountListModel");
|
||||||
qmlRegisterType<ChatDocumentHandler>("org.kde.neochat", 1, 0, "ChatDocumentHandler");
|
qmlRegisterType<ChatDocumentHandler>("org.kde.neochat", 1, 0, "ChatDocumentHandler");
|
||||||
qmlRegisterType<RoomListModel>("org.kde.neochat", 1, 0, "RoomListModel");
|
qmlRegisterType<RoomListModel>("org.kde.neochat", 1, 0, "RoomListModel");
|
||||||
@@ -116,6 +119,7 @@ int main(int argc, char *argv[])
|
|||||||
qRegisterMetaType<NeoChatRoom *>("NeoChatRoom*");
|
qRegisterMetaType<NeoChatRoom *>("NeoChatRoom*");
|
||||||
qRegisterMetaType<NeoChatUser *>("NeoChatUser*");
|
qRegisterMetaType<NeoChatUser *>("NeoChatUser*");
|
||||||
qRegisterMetaType<GetRoomEventsJob *>("GetRoomEventsJob*");
|
qRegisterMetaType<GetRoomEventsJob *>("GetRoomEventsJob*");
|
||||||
|
qRegisterMetaType<QMimeType>("QMimeType");
|
||||||
|
|
||||||
qRegisterMetaTypeStreamOperators<Emoji>();
|
qRegisterMetaTypeStreamOperators<Emoji>();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user