Move mime type checking to my own class

This commit is contained in:
Anton Thomasson 2020-02-18 20:40:31 +01:00
parent 6180c3733d
commit 544bd0945d
5 changed files with 67 additions and 24 deletions

View file

@ -25,7 +25,8 @@ SOURCES += src/harbour-seaprint.cpp \
src/ippdiscovery.cpp \
src/bytestream.cpp \
src/ippmsg.cpp \
src/ippprinter.cpp
src/ippprinter.cpp \
src/mimer.cpp
DISTFILES += qml/harbour-seaprint.qml \
@ -61,4 +62,5 @@ HEADERS += \
src/ippdiscovery.h \
src/bytestream.h \
src/ippmsg.h \
src/ippprinter.h
src/ippprinter.h \
src/mimer.h

View file

@ -3,6 +3,7 @@ import Sailfish.Silica 1.0
import Sailfish.Pickers 1.0
import seaprint.ippdiscovery 1.0
import seaprint.ippprinter 1.0
import seaprint.mimer 1.0
import "utils.js" as Utils
import "../components"
import Nemo.DBus 2.0
@ -46,23 +47,10 @@ Page {
Component.onCompleted: {
IppDiscovery.discover();
if(selectedFile != "")
{ // Until i can convince FilePickerPage to do its magic without user interaction
if(Utils.endsWith(".pdf", selectedFile))
{
selectedFileType = "application/pdf"
}
else if(Utils.endsWith(".ps", selectedFile))
{
selectedFileType = "application/postscript"
}
else if(Utils.endsWith(".jpg", selectedFile) || Utils.endsWith(".jpeg", selectedFile))
{
selectedFileType = "image/jpeg"
}
else
{
selectedFile = ""
}
{
var type = Mimer.get_type(selectedFile);
console.log(type);
selectedFileType = type;
}
}
@ -288,7 +276,7 @@ Page {
onSelectedContentPropertiesChanged: {
page.selectedFile = selectedContentProperties.filePath
page.selectedFileType = selectedContentProperties.mimeType
page.selectedFileType = Mimer.get_type(selectedContentProperties.filePath)
}
}
}

View file

@ -4,14 +4,16 @@
#include <seaprint_version.h>
#include <src/ippdiscovery.h>
#include <src/ippprinter.h>
#include <src/mimer.h>
static QObject* ippdiscovery_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
template <class T>
static QObject* singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
IppDiscovery *ippdiscovery = IppDiscovery::instance();
return ippdiscovery;
T *inst = T::instance();
return inst;
}
@ -21,7 +23,8 @@ int main(int argc, char *argv[])
app->setApplicationVersion(QStringLiteral(SEAPRINT_VERSION));
qmlRegisterSingletonType<IppDiscovery>("seaprint.ippdiscovery", 1, 0, "IppDiscovery", ippdiscovery_singletontype_provider);
qmlRegisterSingletonType<IppDiscovery>("seaprint.ippdiscovery", 1, 0, "IppDiscovery", singletontype_provider<IppDiscovery>);
qmlRegisterSingletonType<Mimer>("seaprint.mimer", 1, 0, "Mimer", singletontype_provider<Mimer>);
qmlRegisterType<IppPrinter>("seaprint.ippprinter", 1, 0, "IppPrinter");
QQuickView* view = SailfishApp::createView();

28
src/mimer.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "mimer.h"
Mimer::Mimer()
{
}
Mimer* Mimer::m_Instance = 0;
Mimer* Mimer::instance()
{
static QMutex mutex;
if (!m_Instance)
{
mutex.lock();
if (!m_Instance)
m_Instance = new Mimer;
mutex.unlock();
}
return m_Instance;
}
QString Mimer::get_type(QString filename) {
return _db.mimeTypeForFile(filename).name();
}

22
src/mimer.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef MIMER_H
#define MIMER_H
#include <QString>
#include <QObject>
#include <QMutex>
#include <QMimeDatabase>
class Mimer : public QObject
{
Q_OBJECT
public:
static Mimer* instance();
Q_INVOKABLE QString get_type(QString filename);
private:
Mimer();
static Mimer* m_Instance;
QMimeDatabase _db;
};
#endif // MIMER_H