Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
280 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module org.nemomobile.contentaction | ||
plugin contentaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters