openUrl for http urls when http extension is requested

This commit is contained in:
Aaron Seigo 2011-04-09 20:56:02 +02:00
parent 98396208dc
commit 4d2bd9e799
2 changed files with 29 additions and 5 deletions

View File

@ -213,11 +213,23 @@ QScriptValue ScriptEnv::openUrl(QScriptContext *context, QScriptEngine *engine)
QScriptValue v = context->argument(0); QScriptValue v = context->argument(0);
KUrl url = v.isString() ? KUrl(v.toString()) : qscriptvalue_cast<KUrl>(v); KUrl url = v.isString() ? KUrl(v.toString()) : qscriptvalue_cast<KUrl>(v);
if (url.isValid()) {
new KRun(url, 0); if (!url.isValid()) {
return false;
} }
return false; ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
if (!env) {
return false;
}
if (!(env->m_allowedUrls & AppLaunching) &&
!((env->m_allowedUrls & HttpUrls) && (url.protocol() == "http" || url.protocol() == "https"))) {
return false;
}
new KRun(url, 0);
return true;
} }
// TODO these should throw an exception // TODO these should throw an exception
@ -261,6 +273,14 @@ void ScriptEnv::registerGetUrl(QScriptValue &obj)
} }
} }
void ScriptEnv::registerOpenUrl(QScriptValue &obj)
{
QScriptValue value = obj.property("openUrl");
if (!value.isValid()) {
obj.setProperty("openUrl", m_engine->newFunction(ScriptEnv::openUrl));
}
}
bool ScriptEnv::importBuiltinExtension(const QString &extension, QScriptValue &obj) bool ScriptEnv::importBuiltinExtension(const QString &extension, QScriptValue &obj)
{ {
kDebug() << extension; kDebug() << extension;
@ -270,13 +290,15 @@ bool ScriptEnv::importBuiltinExtension(const QString &extension, QScriptValue &o
return true; return true;
#endif #endif
} else if ("launchapp" == extension) { } else if ("launchapp" == extension) {
m_allowedUrls |= AppLaunching;
obj.setProperty("runApplication", m_engine->newFunction(ScriptEnv::runApplication)); obj.setProperty("runApplication", m_engine->newFunction(ScriptEnv::runApplication));
obj.setProperty("runCommand", m_engine->newFunction(ScriptEnv::runCommand)); obj.setProperty("runCommand", m_engine->newFunction(ScriptEnv::runCommand));
obj.setProperty("openUrl", m_engine->newFunction(ScriptEnv::openUrl)); registerOpenUrl(obj);
return true; return true;
} else if ("http" == extension) { } else if ("http" == extension) {
m_allowedUrls |= HttpUrls; m_allowedUrls |= HttpUrls;
registerGetUrl(obj); registerGetUrl(obj);
registerOpenUrl(obj);
return true; return true;
} else if ("networkio" == extension) { } else if ("networkio" == extension) {
m_allowedUrls |= HttpUrls | NetworkUrls; m_allowedUrls |= HttpUrls | NetworkUrls;

View File

@ -34,7 +34,8 @@ public:
enum AllowedUrl { NoUrls = 0, enum AllowedUrl { NoUrls = 0,
HttpUrls = 1, HttpUrls = 1,
NetworkUrls = 2, NetworkUrls = 2,
LocalUrls = 4 }; LocalUrls = 4,
AppLaunching = 8};
Q_DECLARE_FLAGS(AllowedUrls, AllowedUrl) Q_DECLARE_FLAGS(AllowedUrls, AllowedUrl)
ScriptEnv(QObject *parent, QScriptEngine *engine); ScriptEnv(QObject *parent, QScriptEngine *engine);
@ -74,6 +75,7 @@ Q_SIGNALS:
private: private:
void registerGetUrl(QScriptValue &obj); void registerGetUrl(QScriptValue &obj);
void registerOpenUrl(QScriptValue &obj);
bool importBuiltinExtension(const QString &extension, QScriptValue &obj); bool importBuiltinExtension(const QString &extension, QScriptValue &obj);
static QScriptValue debug(QScriptContext *context, QScriptEngine *engine); static QScriptValue debug(QScriptContext *context, QScriptEngine *engine);