Skip to content

Commit

Permalink
[libcontentaction] Expose mimeType property. Contributes to JB#41147
Browse files Browse the repository at this point in the history
Aftering triggering an ContenAction the mimeType can be read through
singleton ContentAction.
  • Loading branch information
rainemak committed Jun 12, 2018
1 parent c31e6c9 commit 888c23f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
52 changes: 43 additions & 9 deletions declarative/declarativecontentaction.cpp
Expand Up @@ -26,54 +26,86 @@
#include <QtCore/QUrl>
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include <QtCore/QMimeDatabase>

DeclarativeContentAction::DeclarativeContentAction(QObject *parent)
: QObject(parent)
, m_error(NoError)
, m_mimeDatabase(nullptr)
{
}

DeclarativeContentAction::~DeclarativeContentAction()
{
delete m_mimeDatabase;
}

DeclarativeContentAction::Error DeclarativeContentAction::error() const
{
return m_error;
}

QString DeclarativeContentAction::mimeType() const
{
return m_mimeType;
}

void DeclarativeContentAction::updateMimeType(const QUrl &url)
{
if (!m_mimeDatabase) {
m_mimeDatabase = new QMimeDatabase;
}

QString mimeType = m_mimeDatabase->mimeTypeForUrl(url).name();
if (m_mimeType != mimeType) {
m_mimeType = mimeType;
emit mimeTypeChanged();
}
}

bool DeclarativeContentAction::trigger(const QUrl &url)
{
m_error = NoError;
QString oldMimeType = m_mimeType;
m_mimeType.clear();

if (!url.isValid()) {
qWarning() << Q_FUNC_INFO << "Invalid URL!";
m_error = InvalidUrl;
emit errorChanged();
if (m_mimeType != oldMimeType) {
emit mimeTypeChanged();
}
return false;
}

if (url.isLocalFile()) {
QString localFile = url.toLocalFile();
if (!QFile::exists(localFile)) {
QFile file(url.toLocalFile());
if (!file.exists()) {
qWarning() << Q_FUNC_INFO << "File doesn't exist!";
m_error = FileDoesNotExist;
emit errorChanged();
if (m_mimeType != oldMimeType) {
emit mimeTypeChanged();
}

return false;
}

QFile file(localFile);
if (file.open(QIODevice::ReadOnly) && file.size() == 0) {
if (file.size() == 0) {
m_error = FileIsEmpty;
emit errorChanged();
file.close();
if (m_mimeType != oldMimeType) {
emit mimeTypeChanged();
}
return false;
}

if (file.openMode() != QIODevice::NotOpen) {
file.close();
}

ContentAction::Action action = ContentAction::Action::defaultActionForFile(url);
if (!action.isValid()) {
m_error = FileTypeNotSupported;
emit errorChanged();
updateMimeType(url);
return false;
}

Expand All @@ -82,13 +114,15 @@ bool DeclarativeContentAction::trigger(const QUrl &url)
ContentAction::Action action = ContentAction::Action::defaultActionForScheme(url.toString());
if (!action.isValid()) {
m_error = UrlSchemeNotSupported;
updateMimeType(url);
emit errorChanged();
return false;
}

action.trigger();
}

updateMimeType(url);
return true;
}

13 changes: 13 additions & 0 deletions declarative/declarativecontentaction.h
Expand Up @@ -24,11 +24,17 @@

#include <QtCore/QObject>

QT_BEGIN_NAMESPACE
class QMimeDatabase;
QT_END_NAMESPACE


class DeclarativeContentAction: public QObject
{
Q_OBJECT

Q_PROPERTY(Error error READ error NOTIFY errorChanged)
Q_PROPERTY(QString mimeType READ mimeType NOTIFY mimeTypeChanged)

Q_ENUMS(Error)

Expand All @@ -43,16 +49,23 @@ class DeclarativeContentAction: public QObject
};

explicit DeclarativeContentAction(QObject *parent = 0);
virtual ~DeclarativeContentAction();

Q_INVOKABLE bool trigger(const QUrl &url);

Error error() const;
QString mimeType() const;

signals:
void errorChanged();
void mimeTypeChanged();

private:
void updateMimeType(const QUrl &url);

Error m_error;
QString m_mimeType;
QMimeDatabase *m_mimeDatabase;
};

#endif // DECLARATIVECONTENTACTION_H

0 comments on commit 888c23f

Please sign in to comment.