From 67e42ba2eb8ab2115e929791cb67a8e9ba413e80 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 12 Nov 2009 18:02:05 +1000 Subject: Qt.md5() method --- doc/src/declarative/globalobject.qdoc | 4 ++++ src/declarative/qml/qmlengine.cpp | 14 ++++++++++++++ src/declarative/qml/qmlengine_p.h | 1 + tests/auto/declarative/qmlqt/tst_qmlqt.cpp | 15 +++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index 06f6bdc..3cf1ca3 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -122,6 +122,10 @@ This function plays the audio file located at \c soundLocation. Only .wav files \section3 Qt.openUrlExternally(url target) This function attempts to open the specified \c target url in an external application, based on the user's desktop preferences. It will return true if it succeeds, and false otherwise. + +\section3 Qt.md5(data) +This function returns a hex string of the md5 hash of \c data. + \endlist \section1 Dynamic Object Creation diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 177818f..53febf5 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -88,6 +88,7 @@ #include #include #include +#include #ifdef Q_OS_WIN // for %APPDATA% #include "qt_windows.h" @@ -150,6 +151,7 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e) qtObject.setProperty(QLatin1String("closestAngle"), scriptEngine.newFunction(QmlEnginePrivate::closestAngle, 2)); qtObject.setProperty(QLatin1String("playSound"), scriptEngine.newFunction(QmlEnginePrivate::playSound, 1)); qtObject.setProperty(QLatin1String("openUrlExternally"),scriptEngine.newFunction(desktopOpenUrl, 1)); + qtObject.setProperty(QLatin1String("md5"),scriptEngine.newFunction(md5, 1)); scriptEngine.globalObject().setProperty(QLatin1String("createQmlObject"), scriptEngine.newFunction(QmlEnginePrivate::createQmlObject, 1)); @@ -816,6 +818,18 @@ QScriptValue QmlEnginePrivate::desktopOpenUrl(QScriptContext *ctxt, QScriptEngin return e->newVariant(QVariant(ret)); } +QScriptValue QmlEnginePrivate::md5(QScriptContext *ctxt, QScriptEngine *e) +{ + QByteArray data; + + if (ctxt->argumentCount() >= 1) + data = ctxt->argument(0).toString().toUtf8(); + + QByteArray result = QCryptographicHash::hash(data, QCryptographicHash::Md5); + + return QScriptValue(QLatin1String(result.toHex())); +} + QScriptValue QmlEnginePrivate::closestAngle(QScriptContext *ctxt, QScriptEngine *e) { if(ctxt->argumentCount() < 2) diff --git a/src/declarative/qml/qmlengine_p.h b/src/declarative/qml/qmlengine_p.h index 3c60b5c..5b4e884 100644 --- a/src/declarative/qml/qmlengine_p.h +++ b/src/declarative/qml/qmlengine_p.h @@ -272,6 +272,7 @@ public: static QScriptValue closestAngle(QScriptContext*, QScriptEngine*); static QScriptValue playSound(QScriptContext*, QScriptEngine*); static QScriptValue desktopOpenUrl(QScriptContext*, QScriptEngine*); + static QScriptValue md5(QScriptContext*, QScriptEngine*); static QScriptEngine *getScriptEngine(QmlEngine *e) { return &e->d_func()->scriptEngine; } static QmlEngine *getEngine(QScriptEngine *e) { return static_cast(e)->p->q_func(); } diff --git a/tests/auto/declarative/qmlqt/tst_qmlqt.cpp b/tests/auto/declarative/qmlqt/tst_qmlqt.cpp index e9c9052..13f4904 100644 --- a/tests/auto/declarative/qmlqt/tst_qmlqt.cpp +++ b/tests/auto/declarative/qmlqt/tst_qmlqt.cpp @@ -46,6 +46,7 @@ #include #include #include +#include class tst_qmlqt : public QObject { @@ -67,6 +68,7 @@ private slots: void closestAngle(); void playSound(); void openUrlExternally(); + void md5(); private: QmlEngine engine; @@ -279,6 +281,19 @@ void tst_qmlqt::openUrlExternally() QVERIFY(false); } +void tst_qmlqt::md5() +{ + QmlComponent component(&engine, TEST_FILE("md5.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test1").toString(), QLatin1String(QCryptographicHash::hash(QByteArray(), QCryptographicHash::Md5).toHex())); + QCOMPARE(object->property("test2").toString(), QLatin1String(QCryptographicHash::hash("Hello World", QCryptographicHash::Md5).toHex())); + + delete object; +} + + QTEST_MAIN(tst_qmlqt) #include "tst_qmlqt.moc" -- cgit v0.12