Create a MimeDatabase component that maps QMimeDatabase

Makes it possible to figure out mimetypes from QML.

REVIEW: 115348
This commit is contained in:
Aleix Pol 2014-01-28 15:04:19 +01:00
parent 6dfb4f240b
commit 87a7db3063
4 changed files with 86 additions and 0 deletions

View File

@ -10,6 +10,7 @@ set(qtextracomponents_SRCS
mouseeventlistener.cpp
columnproxymodel.cpp
clipboard.cpp
mimedatabase.cpp
)
add_library(qtextracomponentsplugin SHARED ${qtextracomponents_SRCS})

View File

@ -0,0 +1,44 @@
/*
* Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "mimedatabase.h"
#include <QVariantMap>
static QVariantMap mimetypeToVariant(const QMimeType& type)
{
QVariantMap ret;
ret["name"] = type.name();
ret["iconName"] = type.iconName();
return ret;
}
MimeDatabase::MimeDatabase(QObject* parent)
: QObject(parent)
{
}
QVariantMap MimeDatabase::mimeTypeForUrl(const QUrl& url) const
{
return mimetypeToVariant(m_db.mimeTypeForUrl(url));
}
QVariantMap MimeDatabase::mimeTypeForName(const QString& name) const
{
return mimetypeToVariant(m_db.mimeTypeForName(name));
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef MIMEDATABASE_H
#define MIMEDATABASE_H
#include <QObject>
#include <QMimeDatabase>
class MimeDatabase : public QObject
{
Q_OBJECT
public:
MimeDatabase(QObject* parent = 0);
Q_SCRIPTABLE QVariantMap mimeTypeForUrl(const QUrl & url) const;
Q_SCRIPTABLE QVariantMap mimeTypeForName(const QString & name) const;
private:
QMimeDatabase m_db;
};
#endif // MIMEDATABASE_H

View File

@ -30,6 +30,7 @@
#include "mouseeventlistener.h"
#include "columnproxymodel.h"
#include "clipboard.h"
#include "mimedatabase.h"
void QtExtraComponentsPlugin::registerTypes(const char *uri)
{
@ -41,6 +42,7 @@ void QtExtraComponentsPlugin::registerTypes(const char *uri)
qmlRegisterType<MouseEventListener>(uri, 2, 0, "MouseEventListener");
qmlRegisterType<ColumnProxyModel>(uri, 2, 0, "ColumnProxyModel");
qmlRegisterType<Clipboard>(uri, 2, 0, "Clipboard");
qmlRegisterType<MimeDatabase>(uri, 2, 0, "MimeDatabase");
qmlRegisterType<QAbstractItemModel>();
qRegisterMetaType<QModelIndex>("QModelIndex");