Skip to content

Commit

Permalink
Add QML plugin.
Browse files Browse the repository at this point in the history
Add a QML plugin which provides a singleton type for performing the
default action on a url.
  • Loading branch information
Aaron McCarthy committed Mar 10, 2015
1 parent 99569c8 commit 2be2611
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 2 deletions.
34 changes: 34 additions & 0 deletions declarative/declarative.pro
@@ -0,0 +1,34 @@
TEMPLATE = lib
TARGET = contentaction
QT = core qml
CONFIG += plugin

TARGET = $$qtLibraryTarget($$TARGET)
uri = org.nemomobile.contentaction
installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /)

INCLUDEPATH += ../src
LIBS += -L../src -lcontentaction5

SOURCES += \
plugin.cpp \
declarativecontentaction.cpp

HEADERS += \
plugin.h \
declarativecontentaction.h

!equals(_PRO_FILE_PWD_, $$OUT_PWD) {
copy_qmldir.target = $$OUT_PWD/qmldir
copy_qmldir.depends = $$_PRO_FILE_PWD_/qmldir
copy_qmldir.commands = $(COPY_FILE) \"$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)\"
QMAKE_EXTRA_TARGETS += copy_qmldir
PRE_TARGETDEPS += $$copy_qmldir.target
}

qmldir.files = qmldir

qmldir.path = $$installPath
target.path = $$installPath

INSTALLS += target qmldir
80 changes: 80 additions & 0 deletions declarative/declarativecontentaction.cpp
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2015 Jolla Ltd.
*
* Contact: Aaron McCarthy <aaron.mccarthy@jollamobile.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/

#include "declarativecontentaction.h"

#include "contentaction.h"

#include <QtCore/QUrl>
#include <QtCore/QFile>
#include <QtCore/QDebug>

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

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

bool DeclarativeContentAction::trigger(const QUrl &url)
{
m_error = NoError;

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

if (url.isLocalFile()) {
if (!QFile::exists(url.toLocalFile())) {
qWarning() << Q_FUNC_INFO << "File doesn't exist!";
m_error = FileDoesNotExist;
emit errorChanged();
return false;
}

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

action.trigger();
} else {
ContentAction::Action action = ContentAction::Action::defaultActionForScheme(url.toString());
if (!action.isValid()) {
m_error = UrlSchemeNotSupported;
emit errorChanged();
return false;
}

action.trigger();
}

return true;
}

57 changes: 57 additions & 0 deletions declarative/declarativecontentaction.h
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2015 Jolla Ltd.
*
* Contact: Aaron McCarthy <aaron.mccarthy@jollamobile.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/

#ifndef DECLARATIVECONTENTACTION_H
#define DECLARATIVECONTENTACTION_H

#include <QtCore/QObject>

class DeclarativeContentAction: public QObject
{
Q_OBJECT

Q_PROPERTY(Error error READ error NOTIFY errorChanged)

Q_ENUMS(Error)

public:
enum Error {
NoError,
FileTypeNotSupported,
FileDoesNotExist,
UrlSchemeNotSupported,
InvalidUrl
};

explicit DeclarativeContentAction(QObject *parent = 0);

Q_INVOKABLE bool trigger(const QUrl &url);

Error error() const;

signals:
void errorChanged();

private:
Error m_error;
};

#endif // DECLARATIVECONTENTACTION_H
52 changes: 52 additions & 0 deletions declarative/plugin.cpp
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2015 Jolla Ltd.
*
* Contact: Aaron McCarthy <aaron.mccarthy@jollamobile.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/

#include "plugin.h"
#include "declarativecontentaction.h"

#include <QtQml/qqml.h>

namespace
{

QObject *content_action(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
return new DeclarativeContentAction;
}

}

void DeclarativePlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
Q_UNUSED(engine)
Q_UNUSED(uri)

Q_ASSERT(QLatin1String(uri) == QLatin1String("org.nemomobile.contentaction"));
}

void DeclarativePlugin::registerTypes(const char *uri)
{
// @uri org.nemomobile.contentaction
qmlRegisterSingletonType<DeclarativeContentAction>(uri, 1, 0, "ContentAction", content_action);

}
38 changes: 38 additions & 0 deletions declarative/plugin.h
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2015 Jolla Ltd.
*
* Contact: Aaron McCarthy <aaron.mccarthy@jollamobile.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/

#ifndef DECLARATIVEPLUGIN_H
#define DECLARATIVEPLUGIN_H

#include <QtQml/QQmlExtensionPlugin>

class DeclarativePlugin : public QQmlExtensionPlugin
{
Q_OBJECT

Q_PLUGIN_METADATA(IID "org.nemomobile.contentaction")

public:
void initializeEngine(QQmlEngine *engine, const char *uri);
void registerTypes(const char *uri);
};

#endif // DECLARATIVEPLUGIN_H
2 changes: 2 additions & 0 deletions declarative/qmldir
@@ -0,0 +1,2 @@
module org.nemomobile.contentaction
plugin contentaction
4 changes: 3 additions & 1 deletion libcontentaction.pro
Expand Up @@ -2,9 +2,11 @@ TEMPLATE = subdirs
SUBDIRS += src \
data \
tests \
tools
tools \
declarative

tests.depends = src
tools.depends = src
declarative.depends = src

# TODO: fix tests, doc
15 changes: 14 additions & 1 deletion rpm/libcontentaction-qt5.spec
@@ -1,6 +1,6 @@
Name: libcontentaction-qt5
Summary: Library for associating content with actions
Version: 0.2.0
Version: 0.2.3
Release: 1
Group: System/Desktop
License: LGPLv2.1
Expand Down Expand Up @@ -48,6 +48,15 @@ Requires: qt5-qttools-qdbus
This package contains the tests for libcontentaction library.


%package -n nemo-qml-plugin-contentaction
Summary: Content Action QML plugin
Group: System/Desktop
Requires: %{name} = %{version}-%{release}

%description -n nemo-qml-plugin-contentaction
This package contains the Content Action QML plugin.


%prep
%setup -q -n %{name}-%{version}

Expand Down Expand Up @@ -82,3 +91,7 @@ rm -rf %{buildroot}
%defattr(-,root,root,-)
%attr(0755, root, root) /opt/tests/libcontentaction5/bin/lca-cita-test
/opt/tests/libcontentaction5/*

%files -n nemo-qml-plugin-contentaction
%defattr(-,root,root,-)
%{_libdir}/qt5/qml/org/nemomobile/contentaction/*

0 comments on commit 2be2611

Please sign in to comment.