bool TemplateEngine::evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage) { if (errorMessage) errorMessage->clear(); if (result) *result = false; const QJSValue value = engine.evaluate(expression); if (value.isError()) { if (errorMessage) *errorMessage = QString::fromLatin1("Error in \"%1\": %2") .arg(expression, value.toString()); return false; } // Try to convert to bool, be that an int or whatever. if (value.isBool()) { if (result) *result = value.toBool(); return true; } if (value.isNumber()) { if (result) *result = !qFuzzyCompare(value.toNumber(), 0); return true; } if (value.isString()) { if (result) *result = !value.toString().isEmpty(); return true; } if (errorMessage) *errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.") .arg(expression, value.toString()); return false; }
QJSValue JSKitPebble::buildAckEventObject(uint transaction, const QString &message) const { QJSEngine *engine = m_mgr->engine(); QJSValue eventObj = engine->newObject(); QJSValue dataObj = engine->newObject(); dataObj.setProperty("transactionId", engine->toScriptValue(transaction)); eventObj.setProperty("data", dataObj); if (!message.isEmpty()) { QJSValue errorObj = engine->newObject(); errorObj.setProperty("message", engine->toScriptValue(message)); eventObj.setProperty("error", errorObj); } return eventObj; }
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ //! [0] QJSEngine myEngine; QJSValue three = myEngine.evaluate("1 + 2"); //! [0] //! [1] QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })"); QJSValueList args; args << 1 << 2; QJSValue threeAgain = fun.call(QJSValue(), args); //! [1] //! [2] QString fileName = "helloworld.qs"; QFile scriptFile(fileName);
void OwnCloudService::checkAppInfo(QNetworkReply *reply) { QString data = QString(reply->readAll()); // qDebug() << data; // we have to add [], so the string can be parsed as JSON data = QString("[") + data + QString("]"); QJSEngine engine; QJSValue result = engine.evaluate(data); bool appIsValid = result.property(0).property("versioning").toBool(); QString appVersion = result.property(0).property("app_version") .toVariant().toString(); QString serverVersion = result.property(0).property("server_version") .toVariant().toString(); // reset to "unknown" in case we can't test if versions // and trash app are enabled settingsDialog->setOKLabelData(6, "unknown", SettingsDialog::Unknown); settingsDialog->setOKLabelData(7, "unknown", SettingsDialog::Unknown); if (serverVersion != "") { VersionNumber serverAppVersion = VersionNumber(appVersion); VersionNumber minAppVersion = VersionNumber(QOWNNOTESAPI_MIN_VERSION); if (minAppVersion > serverAppVersion) { settingsDialog->setOKLabelData(4, "version " + appVersion + " too low", SettingsDialog::Warning); } else { settingsDialog->setOKLabelData(4, "ok", SettingsDialog::OK); } // check if versions and trash app are enabled after QOwnNotesAPI v0.3.1 if (serverAppVersion >= VersionNumber("0.3.1")) { bool versionsAppEnabled = result.property(0).property( "versions_app").toBool(); bool trashAppEnabled = result.property(0).property( "trash_app").toBool(); if (versionsAppEnabled) { settingsDialog->setOKLabelData(6, "ok", SettingsDialog::OK); } else { settingsDialog->setOKLabelData(6, "not enabled", SettingsDialog::Failure); } if (trashAppEnabled) { settingsDialog->setOKLabelData(7, "ok", SettingsDialog::OK); } else { settingsDialog->setOKLabelData(7, "not enabled", SettingsDialog::Failure); } } // check if notes path was found after QOwnNotesAPI v0.4. if (serverAppVersion >= VersionNumber("0.4.1")) { bool notesPathExists = result.property(0).property( "notes_path_exists").toBool(); if (notesPathExists) { settingsDialog->setOKLabelData(8, "ok", SettingsDialog::OK); } else { settingsDialog->setOKLabelData(8, "not found", SettingsDialog::Failure); } } } else { settingsDialog->setOKLabelData(4, "not connected", SettingsDialog::Failure); } // call callback in settings dialog settingsDialog->connectTestCallback(appIsValid, appVersion, serverVersion, reply->errorString()); }
QStringList VideoAnalyzer::analyze(QString pageUrl, int videoClarity) { qDebug()<< pageUrl; //目前使用flvsp.com解析 QEventLoop loop; QByteArray url = pageUrl.replace("//","##").toLatin1(); QFile scriptFile(":/base64.js"); scriptFile.open(QIODevice::ReadOnly); QTextStream out(&scriptFile); QString contents = out.readAll(); scriptFile.close(); QJSEngine myEngine; QJSValue fun = myEngine.evaluate(contents); QJSValueList args; args << QJSValue(QString(url)) ; QJSValue base64_encode = myEngine.globalObject().property("base64_encode"); QJSValue encodeResult = base64_encode.call(args).toString(); //qDebug()<<encodeResult.toString(); //qDebug()<<base64_encode(&a,strlen(&a)); QNetworkRequest request; request.setUrl(QUrl("http://www.flvsp.com/parse/getData.php?url="+encodeResult.toString())); request.setRawHeader("Accept","text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"); request.setRawHeader("Accept-Encoding","gzip, deflate, sdch"); request.setRawHeader("Referer","http://www.flvsp.com/?url=https://www.letv.com/ptv/vplay/22827477.html"); request.setRawHeader("X-Requested-With","XMLHttpRequest"); request.setRawHeader("User-Agent","Mozilla/5.0 (Linux; U; Android 4.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 "); QNetworkReply *reply = manager->get(request); connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); QString data = reply->readAll(); qDebug()<<"get flvsp data start"; QStringList videoClarityList = data.split("panel-heading"); if(videoClarityList.count()>2) { videoClarityList.removeFirst(); videoClarityList.removeFirst(); }else { return QStringList()<<"null"; } //搜寻符合清晰度的视频 int videoClarityIndex = 10-videoClarity;//flvsp的解析结果中排在前面的清晰度高 QString videos; for(int i=0;i<10;i++)//往清晰度低的方向查找合适的 { videos = videoClarityList.value(videoClarityIndex+i,""); if(videos.isEmpty()) { continue; }else { break; } } if(videos.isEmpty())//上述循环找不到,往清晰度高的方向找 { for(int i=0;i<10;i++) { videos = videoClarityList.value(videoClarityIndex-i,""); if(videos.isEmpty()) { continue; }else { break; } } } qDebug()<<"get flvsp data end"; //得到真实地址列表realUrlList QStringList realUrlList; QRegExp rx("class=\"file_url\" href=\".*\""); rx.setMinimal(true); int pos = 0; while ((pos = rx.indexIn(videos, pos)) != -1) { QString href = rx.cap(0); realUrlList.append(href.replace(QRegExp("class=\"file_url\" href=\"|\""),"")); pos += rx.matchedLength(); } qDebug()<<realUrlList; return realUrlList; }
void tst_QJSValueIterator::iterateGetterSetter() { // unified getter/setter function { QJSEngine eng; QJSValue obj = eng.newObject(); obj.setProperty("foo", eng.newFunction(myGetterSetter), QScriptValue::PropertyGetter | QScriptValue::PropertySetter); QJSValue val(&eng, 123); obj.setProperty("foo", val); QVERIFY(obj.property("bar").strictlyEquals(val)); QVERIFY(obj.property("foo").strictlyEquals(val)); QJSValueIterator it(obj); QVERIFY(it.hasNext()); it.next(); QCOMPARE(it.name(), QString::fromLatin1("foo")); QCOMPARE(it.flags(), QScriptValue::PropertyFlags(QScriptValue::PropertyGetter | QScriptValue::PropertySetter)); QVERIFY(it.value().strictlyEquals(val)); QJSValue val2(&eng, 456); it.setValue(val2); QVERIFY(obj.property("bar").strictlyEquals(val2)); QVERIFY(obj.property("foo").strictlyEquals(val2)); QVERIFY(it.hasNext()); it.next(); QCOMPARE(it.name(), QString::fromLatin1("bar")); QVERIFY(!it.hasNext()); QVERIFY(it.hasPrevious()); it.previous(); QCOMPARE(it.name(), QString::fromLatin1("bar")); QVERIFY(it.hasPrevious()); it.previous(); QCOMPARE(it.name(), QString::fromLatin1("foo")); QCOMPARE(it.flags(), QScriptValue::PropertyFlags(QScriptValue::PropertyGetter | QScriptValue::PropertySetter)); QVERIFY(it.value().strictlyEquals(val2)); it.setValue(val); QVERIFY(obj.property("bar").strictlyEquals(val)); QVERIFY(obj.property("foo").strictlyEquals(val)); } // separate getter/setter function for (int x = 0; x < 2; ++x) { QJSEngine eng; QJSValue obj = eng.newObject(); if (x == 0) { obj.setProperty("foo", eng.newFunction(myGetter), QScriptValue::PropertyGetter); obj.setProperty("foo", eng.newFunction(mySetter), QScriptValue::PropertySetter); } else { obj.setProperty("foo", eng.newFunction(mySetter), QScriptValue::PropertySetter); obj.setProperty("foo", eng.newFunction(myGetter), QScriptValue::PropertyGetter); } QJSValue val(&eng, 123); obj.setProperty("foo", val); QVERIFY(obj.property("bar").strictlyEquals(val)); QVERIFY(obj.property("foo").strictlyEquals(val)); QJSValueIterator it(obj); QVERIFY(it.hasNext()); it.next(); QCOMPARE(it.name(), QString::fromLatin1("foo")); QVERIFY(it.value().strictlyEquals(val)); QJSValue val2(&eng, 456); it.setValue(val2); QVERIFY(obj.property("bar").strictlyEquals(val2)); QVERIFY(obj.property("foo").strictlyEquals(val2)); QVERIFY(it.hasNext()); it.next(); QCOMPARE(it.name(), QString::fromLatin1("bar")); QVERIFY(!it.hasNext()); QVERIFY(it.hasPrevious()); it.previous(); QCOMPARE(it.name(), QString::fromLatin1("bar")); QVERIFY(it.hasPrevious()); it.previous(); QCOMPARE(it.name(), QString::fromLatin1("foo")); QVERIFY(it.value().strictlyEquals(val2)); it.setValue(val); QVERIFY(obj.property("bar").strictlyEquals(val)); QVERIFY(obj.property("foo").strictlyEquals(val)); } }
void tst_QJSValueIterator::iterateArray() { QFETCH(QStringList, propertyNames); QFETCH(QStringList, propertyValues); QJSEngine engine; QJSValue array = engine.newArray(); // Fill the array for (int i = 0; i < propertyNames.size(); ++i) { array.setProperty(propertyNames.at(i), propertyValues.at(i)); } // Iterate thru array properties. Note that the QJSValueIterator doesn't guarantee // any order on the iteration! int length = array.property("length").toInt(); QCOMPARE(length, propertyNames.size()); bool iteratedThruLength = false; QHash<QString, QJSValue> arrayProperties; QJSValueIterator it(array); // Iterate forward while (it.hasNext()) { it.next(); const QString name = it.name(); if (name == QString::fromLatin1("length")) { QVERIFY(it.value().isNumber()); QCOMPARE(it.value().toInt(), length); QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); iteratedThruLength = true; continue; } // Storing the properties we iterate in a hash to compare with test data. QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); arrayProperties.insert(name, it.value()); QVERIFY(it.value().strictlyEquals(array.property(name))); } // Verify properties QVERIFY(iteratedThruLength); QCOMPARE(arrayProperties.size(), propertyNames.size()); for (int i = 0; i < propertyNames.size(); ++i) { QVERIFY(arrayProperties.contains(propertyNames.at(i))); QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } #if 0 // Iterate backwards arrayProperties.clear(); iteratedThruLength = false; it.toBack(); while (it.hasPrevious()) { it.previous(); const QString name = it.name(); if (name == QString::fromLatin1("length")) { QVERIFY(it.value().isNumber()); QCOMPARE(it.value().toInt(), length); QCOMPARE(it.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); iteratedThruLength = true; continue; } // Storing the properties we iterate in a hash to compare with test data. QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); arrayProperties.insert(name, it.value()); QCOMPARE(it.flags(), array.propertyFlags(name)); QVERIFY(it.value().strictlyEquals(array.property(name))); } // Verify properties QVERIFY(iteratedThruLength); QCOMPARE(arrayProperties.size(), propertyNames.size()); for (int i = 0; i < propertyNames.size(); ++i) { QVERIFY(arrayProperties.contains(propertyNames.at(i))); QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } // ### Do we still need this test? // Forward test again but as object arrayProperties.clear(); iteratedThruLength = false; QJSValue arrayObject = engine.toObject(array); QJSValueIterator it2(arrayObject); while (it2.hasNext()) { it2.next(); const QString name = it2.name(); if (name == QString::fromLatin1("length")) { QVERIFY(it2.value().isNumber()); QCOMPARE(it2.value().toInt(), length); QCOMPARE(it2.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); iteratedThruLength = true; continue; } // Storing the properties we iterate in a hash to compare with test data. QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); arrayProperties.insert(name, it2.value()); QCOMPARE(it2.flags(), arrayObject.propertyFlags(name)); QVERIFY(it2.value().strictlyEquals(arrayObject.property(name))); } // Verify properties QVERIFY(iteratedThruLength); QCOMPARE(arrayProperties.size(), propertyNames.size()); for (int i = 0; i < propertyNames.size(); ++i) { QVERIFY(arrayProperties.contains(propertyNames.at(i))); QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } #endif }