diff --git a/includes/CMakeLists.txt b/includes/CMakeLists.txt index 374548c8..3fc8898f 100644 --- a/includes/CMakeLists.txt +++ b/includes/CMakeLists.txt @@ -505,7 +505,6 @@ install( KParts/FileInfoExtension KParts/GUIActivateEvent KParts/HistoryProvider - KParts/HtmlExtension KParts/LiveConnectExtension KParts/ListingExtension KParts/MainWindow diff --git a/includes/KGraphicsWebView b/includes/KGraphicsWebView deleted file mode 100644 index 6b15efa2..00000000 --- a/includes/KGraphicsWebView +++ /dev/null @@ -1,2 +0,0 @@ -#include "../kgraphicswebview.h" - diff --git a/includes/KParts/HtmlExtension b/includes/KParts/HtmlExtension deleted file mode 100644 index 35b6524d..00000000 --- a/includes/KParts/HtmlExtension +++ /dev/null @@ -1 +0,0 @@ -#include "../../kparts/htmlextension.h" diff --git a/kparts/CMakeLists.txt b/kparts/CMakeLists.txt index d21722bf..a703a334 100644 --- a/kparts/CMakeLists.txt +++ b/kparts/CMakeLists.txt @@ -21,9 +21,7 @@ set(kparts_LIB_SRCS browserrun.cpp browseropenorsavequestion.cpp statusbarextension.cpp - scriptableextension.cpp textextension.cpp - htmlextension.cpp fileinfoextension.cpp listingextension.cpp ) @@ -75,9 +73,7 @@ install( browserrun.h statusbarextension.h browseropenorsavequestion.h - scriptableextension.h textextension.h - htmlextension.h fileinfoextension.h listingextension.h DESTINATION ${INCLUDE_INSTALL_DIR}/kparts diff --git a/kparts/htmlextension.cpp b/kparts/htmlextension.cpp deleted file mode 100644 index 0025473e..00000000 --- a/kparts/htmlextension.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2010 David Faure - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. - */ - -#include "htmlextension.h" - -#include "part.h" - -#include -#include - - -using namespace KParts; - -KParts::HtmlExtension::HtmlExtension(KParts::ReadOnlyPart* parent) - : QObject(parent), d(0) -{ -} - -KParts::HtmlExtension::~HtmlExtension() -{ -} - -bool HtmlExtension::hasSelection() const -{ - return false; -} - -HtmlExtension * KParts::HtmlExtension::childObject( QObject *obj ) -{ - return KGlobal::findDirectChild(obj); -} - -SelectorInterface::QueryMethods SelectorInterface::supportedQueryMethods() const -{ - return KParts::SelectorInterface::None; -} - -class SelectorInterface::ElementPrivate : public QSharedData -{ -public: - QString tag; - QHash attributes; -}; - -SelectorInterface::Element::Element() - : d(new ElementPrivate) -{ -} - -SelectorInterface::Element::Element(const SelectorInterface::Element& other) - : d(other.d) -{ -} - -SelectorInterface::Element::~Element() -{ -} - -bool SelectorInterface::Element::isNull() const -{ - return d->tag.isNull(); -} - -void SelectorInterface::Element::setTagName(const QString& tag) -{ - d->tag = tag; -} - -QString SelectorInterface::Element::tagName() const -{ - return d->tag; -} - -void SelectorInterface::Element::setAttribute(const QString& name, const QString& value) -{ - d->attributes[name] = value; // insert or replace -} - -QStringList SelectorInterface::Element::attributeNames() const -{ - return d->attributes.keys(); -} - -QString SelectorInterface::Element::attribute(const QString& name, const QString& defaultValue) const -{ - return d->attributes.value(name, defaultValue); -} - -bool SelectorInterface::Element::hasAttribute(const QString& name) const -{ - return d->attributes.contains(name); -} - -const char* HtmlSettingsInterface::javascriptAdviceToText(HtmlSettingsInterface::JavaScriptAdvice advice) -{ - // NOTE: The use of I18N_NOOP below is intended to allow GUI code to call - // i18n on the returned text without affecting use of untranslated text in - // config files. - switch (advice) { - case JavaScriptAccept: - return I18N_NOOP("Accept"); - case JavaScriptReject: - return I18N_NOOP("Reject"); - default: - break; - } - - return 0; -} - -HtmlSettingsInterface::JavaScriptAdvice HtmlSettingsInterface::textToJavascriptAdvice(const QString& text) -{ - JavaScriptAdvice ret = JavaScriptDunno; - - if (!text.isEmpty()) { - if (text.compare(QLatin1String("accept"), Qt::CaseInsensitive) == 0) { - ret = JavaScriptAccept; - } else if (text.compare(QLatin1String("reject"), Qt::CaseInsensitive) == 0) { - ret = JavaScriptReject; - } - } - - return ret; -} - -void HtmlSettingsInterface::splitDomainAdvice(const QString& adviceStr, QString& domain, HtmlSettingsInterface::JavaScriptAdvice& javaAdvice, HtmlSettingsInterface::JavaScriptAdvice& javaScriptAdvice) -{ - const QString tmp(adviceStr); - const int splitIndex = tmp.indexOf(':'); - - if (splitIndex == -1) { - domain = adviceStr.toLower(); - javaAdvice = JavaScriptDunno; - javaScriptAdvice = JavaScriptDunno; - } else { - domain = tmp.left(splitIndex).toLower(); - const QString adviceString = tmp.mid(splitIndex+1, tmp.length()); - const int splitIndex2 = adviceString.indexOf(QLatin1Char(':')); - if (splitIndex2 == -1) { - // Java advice only - javaAdvice = textToJavascriptAdvice(adviceString); - javaScriptAdvice = JavaScriptDunno; - } else { - // Java and JavaScript advice - javaAdvice = textToJavascriptAdvice(adviceString.left(splitIndex2)); - javaScriptAdvice = textToJavascriptAdvice(adviceString.mid(splitIndex2+1, - adviceString.length())); - } - } -} diff --git a/kparts/htmlextension.h b/kparts/htmlextension.h deleted file mode 100644 index 1cb785e2..00000000 --- a/kparts/htmlextension.h +++ /dev/null @@ -1,398 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2010 David Faure - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. - */ - -#ifndef KPARTS_HTMLEXTENSION_H -#define KPARTS_HTMLEXTENSION_H - -#include -#include - -#include - -class KUrl; - -namespace KParts -{ - -class ReadOnlyPart; -class HtmlExtensionPrivate; -class SelectorInterfacePrivate; - -/** - * @short an extension for KParts to provide HTML-related features - * - * Use qobject_cast to cast the extension to interesting interfaces, like - * qobject_cast. - * - * @since 4.6 - */ -class KPARTS_EXPORT HtmlExtension : public QObject -{ - Q_OBJECT -public: - HtmlExtension(KParts::ReadOnlyPart* parent); - ~HtmlExtension(); - - /** - * Queries @p obj for a child object which inherits from this - * HtmlExtension class. - */ - static HtmlExtension *childObject( QObject *obj ); - - /** - * Returns the current base url of the part that implements this extension. - * - * This function is mostly used to resolve any relative URLs that might be - * returned when querying the part for links. - */ - virtual KUrl baseUrl() const = 0; - - /** - * Returns true if portions of the content in the part that implements - * this extension are selected. - * - * By default this function returns false. - */ - virtual bool hasSelection() const; - -private: - // for future extensions - HtmlExtensionPrivate* const d; -}; - -/** - * Optional base class for HtmlExtension-derived classes - * Provides DOM Selector like API: querySelector and querySelectorAll, - * in order to find specific elements in an HTML document. - * - * Example: - * - * const QList elements = selectorInterface->querySelectorAll("head > link[rel=\"alternate\"]"); - * - */ -class KPARTS_EXPORT SelectorInterface -{ -public: - class ElementPrivate; - class Element; - - /** - * Query methods. - */ - enum QueryMethod { - None = 0x00, /*!< Quering is not possible. */ - EntireContent = 0x01, /*!< Query or can query the entire content. */ - SelectedContent = 0x02 /*!< Query or can query only the user selected content, if any. */ - }; - Q_DECLARE_FLAGS(QueryMethods, QueryMethod) - - /** - * Destructor - */ - virtual ~SelectorInterface() {} - - /** - * Returns the supported query methods. - * - * By default this function returns None. - * - * @see QueryMethod - */ - virtual QueryMethods supportedQueryMethods() const; - - /** - * Returns the first (in document order) element in this fragment matching - * the given CSS selector @p query and querying method @p method. - * - * Note that since the returned item is static snapshot, i.e. not live, it - * will not be updated when the document changes. - * - * If the quering method specified by @p method is not supported or cannot be - * handled, then a null element is returned. - * - * @see supportedQueryMethods - * @see QueryMethod - */ - virtual Element querySelector(const QString& query, QueryMethod method) const = 0; - - /** - * Returns all (in document order) elements in this fragment matching the - * given CSS selector @p query and querying method @p method. - * - * Note that since the returned list is static snapshot, i.e. not live, it - * will not be updated when the document changes. - * - * If the quering method specified by @p method is not supported or cannot be - * handled, then an empty list is returned. - * - * @see supportedQueryMethods - * @see QueryMethod - */ - virtual QList querySelectorAll(const QString& query, QueryMethod method) const = 0; - - class KPARTS_EXPORT Element { - public: - /** - * Constructor - */ - Element(); - - /** - * Copy constructor - */ - Element(const Element& other); - - /** - * Destructor - */ - ~Element(); - - /** - * Returns true if the element is null ; otherwise returns false. - */ - bool isNull() const; - - /** - * Sets the tag name of this element. - */ - void setTagName(const QString& tag); - - /** - * Returns the tag name of this element. - */ - QString tagName() const; - - /** - * Adds an attribute with the given name and value. - * If an attribute with the same name exists, its value is replaced by value. - */ - void setAttribute(const QString& name, const QString& value); - - /** - * Returns the list of attributes in this element. - */ - QStringList attributeNames() const; - - /** - * Returns the attribute with the given name. If the attribute does not exist, defaultValue is returned. - */ - QString attribute(const QString& name, const QString& defaultValue = QString()) const; - - /** - * Returns true if the attribute with the given @p name exists. - */ - bool hasAttribute(const QString& name) const; - - // No namespace support yet, could be added with attributeNS, setAttributeNS - - /** - * Swaps the contents of @p other with the contents of this. - */ - void swap( Element& other ) { - d.swap( other.d ); - } - - /** - * Assignment operator - */ - Element& operator=(const Element& other) { - if ( this != &other ) { - Element copy( other ); - swap( copy ); - } - return *this; - } - - private: - QSharedDataPointer d; - }; -}; - -/** - * @short An interface for modifying the settings of browser engines. - * - * This interface provides a generic means for querying or changing the - * settings of browser engines that implement it. - * - * To use this class simply cast an instance of the HTMLExtension object - * using qobject_cast. - * - * Example: - * - * KParts::HTMLExtension* extension = KParts::HTMLExtension::childObject(part); - * KParts::HtmlSettingsInterface* settings = qobject_cast<KParts::HtmlSettingsInterface>(extension); - * const bool autoLoadImages = settings->attribute(KParts::AutoLoadImages); - * - * - * @since 4.8.1 - */ -class KPARTS_EXPORT HtmlSettingsInterface -{ -public: - /** - * Settings attribute types. - */ - enum HtmlSettingsType { - AutoLoadImages, - DnsPrefetchEnabled, - JavaEnabled, - JavascriptEnabled, - MetaRefreshEnabled, - PluginsEnabled, - PrivateBrowsingEnabled, - OfflineStorageDatabaseEnabled, - OfflineWebApplicationCacheEnabled, - LocalStorageEnabled, - UserDefinedStyleSheetURL - }; - - /** - * This enum specifies whether Java/JavaScript execution is allowed. - * - * @since 4.8.2 - */ - enum JavaScriptAdvice { - JavaScriptDunno=0, - JavaScriptAccept, - JavaScriptReject - }; - - /** - * This enum specifies the policy for window.open - * - * @since 4.8.2 - */ - enum JSWindowOpenPolicy { - JSWindowOpenAllow=0, - JSWindowOpenAsk, - JSWindowOpenDeny, - JSWindowOpenSmart - }; - - /** - * This enum specifies the policy for window.status and .defaultStatus - * - * @since 4.8.2 - */ - enum JSWindowStatusPolicy { - JSWindowStatusAllow=0, - JSWindowStatusIgnore - }; - - /** - * This enum specifies the policy for window.moveBy and .moveTo - * - * @since 4.8.2 - */ - enum JSWindowMovePolicy { - JSWindowMoveAllow=0, - JSWindowMoveIgnore - }; - - /** - * This enum specifies the policy for window.resizeBy and .resizeTo - * - * @since 4.8.2 - */ - enum JSWindowResizePolicy { - JSWindowResizeAllow=0, - JSWindowResizeIgnore - }; - - /** - * This enum specifies the policy for window.focus - * - * @since 4.8.2 - */ - enum JSWindowFocusPolicy { - JSWindowFocusAllow=0, - JSWindowFocusIgnore - }; - - /** - * Destructor - */ - virtual ~HtmlSettingsInterface() {} - - /** - * Returns the value of the browser engine's attribute @p type. - */ - virtual QVariant htmlSettingsProperty(HtmlSettingsType type) const = 0; - - /** - * Sets the value of the browser engine's attribute @p type to @p value. - */ - virtual bool setHtmlSettingsProperty(HtmlSettingsType type, const QVariant& value) = 0; - - /** - * A convenience function that returns the javascript advice for @p text. - * - * If text is not either "accept" or "reject", this function returns - * @ref JavaScriptDunno. - * - * @since 4.8.2 - */ - static JavaScriptAdvice textToJavascriptAdvice(const QString& text); - - /** - * A convenience function Returns the text for the given JavascriptAdvice @p advice. - * - * If @p advice is not either JavaScriptAccept or JavaScriptReject, this - * function returns a NULL string. - * - * @since 4.8.2 - */ - static const char* javascriptAdviceToText(JavaScriptAdvice advice); - - /** - * A convenience function that splits @p text into @p domain, @p javaAdvice - * and @p jScriptAdvice. - * - * If @p text is empty or does not contain the proper delimiter (':'), this - * function will set @p domain to @p text and the other two parameters to - * JavaScriptDunno. - * - * @since 4.8.2 - */ - static void splitDomainAdvice(const QString& text, - QString& domain, - JavaScriptAdvice& javaAdvice, - JavaScriptAdvice& javaScriptAdvice); -}; - -} // namespace KParts - -inline void qSwap( KParts::SelectorInterface::Element & lhs, KParts::SelectorInterface::Element & rhs ) -{ - lhs.swap( rhs ); -} - -Q_DECLARE_OPERATORS_FOR_FLAGS(KParts::SelectorInterface::QueryMethods) - -QT_BEGIN_NAMESPACE -Q_DECLARE_TYPEINFO(KParts::SelectorInterface::Element, Q_MOVABLE_TYPE); -QT_END_NAMESPACE - -Q_DECLARE_INTERFACE(KParts::SelectorInterface, - "org.kde.KParts.SelectorInterface") -Q_DECLARE_INTERFACE(KParts::HtmlSettingsInterface, - "org.kde.KParts.HtmlSettingsInterface") - - -#endif /* KPARTS_HTMLEXTENSION_H */ diff --git a/kparts/scriptableextension.cpp b/kparts/scriptableextension.cpp deleted file mode 100644 index 1edd8a99..00000000 --- a/kparts/scriptableextension.cpp +++ /dev/null @@ -1,453 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2010 Maksim Orlovich - Copyright (C) 2002, 2004 Koos Vriezen - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ -#include "scriptableextension.h" -#include "scriptableextension_p.h" -#include -#include - -namespace KParts { - -struct ScriptableExtensionPrivate { - ScriptableExtension* hostContext; - - ScriptableExtensionPrivate(): hostContext(0) - {} -}; - -ScriptableExtension::ScriptableExtension(QObject* parent): - QObject(parent), d(new ScriptableExtensionPrivate) -{} - -ScriptableExtension::~ScriptableExtension() -{ - delete d; -} - -ScriptableExtension* ScriptableExtension::childObject(QObject* obj) -{ - return KGlobal::findDirectChild(obj); -} - -ScriptableExtension* ScriptableExtension::adapterFromLiveConnect(QObject* parentObj, - LiveConnectExtension* oldApi) -{ - return new ScriptableLiveConnectExtension(parentObj, oldApi); -} - -void ScriptableExtension::setHost(ScriptableExtension* host) -{ - d->hostContext = host; -} - -ScriptableExtension* ScriptableExtension::host() const -{ - return d->hostContext; -} - -QVariant ScriptableExtension::rootObject() -{ - return QVariant::fromValue(Null()); -} - -QVariant ScriptableExtension::enclosingObject() -{ - if (d->hostContext) - return d->hostContext->encloserForKid(this); - else - return QVariant::fromValue(Null()); -} - -QVariant ScriptableExtension::encloserForKid(KParts::ScriptableExtension* kid) -{ - Q_UNUSED(kid); - return QVariant::fromValue(Null()); -} - -static QVariant unimplemented() -{ - ScriptableExtension::Exception except(QString::fromLatin1("[unimplemented]")); - return QVariant::fromValue(except); -} - -QVariant ScriptableExtension::callAsFunction(ScriptableExtension* callerPrincipal, - quint64 objId, const ArgList& args) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(args); - return unimplemented(); -} - -QVariant ScriptableExtension::callFunctionReference(ScriptableExtension* callerPrincipal, - quint64 objId, const QString& f, - const ArgList& args) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(args); - Q_UNUSED(f); - return unimplemented(); -} - -QVariant ScriptableExtension::callAsConstructor(ScriptableExtension* callerPrincipal, - quint64 objId, const ArgList& args) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(args); - return unimplemented(); -} - -bool ScriptableExtension::hasProperty(ScriptableExtension* callerPrincipal, - quint64 objId, const QString& propName) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(propName); - return false; -} - -QVariant ScriptableExtension::get(ScriptableExtension* callerPrincipal, - quint64 objId, const QString& propName) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(propName); - return unimplemented(); -} - -bool ScriptableExtension::put(ScriptableExtension* callerPrincipal, quint64 objId, - const QString& propName, const QVariant& value) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(propName); - Q_UNUSED(value); - return false; -} - -bool ScriptableExtension::removeProperty(ScriptableExtension* callerPrincipal, - quint64 objId, const QString& propName) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(propName); - return false; -} - -bool ScriptableExtension::enumerateProperties(ScriptableExtension* callerPrincipal, - quint64 objId, QStringList* result) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(objId); - Q_UNUSED(result); - return false; -} - -bool ScriptableExtension::setException(ScriptableExtension* callerPrincipal, - const QString& message) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(message); - return false; -} - -QVariant ScriptableExtension::evaluateScript(ScriptableExtension* callerPrincipal, - quint64 contextObjectId, - const QString& code, - ScriptLanguage language) -{ - Q_UNUSED(callerPrincipal); - Q_UNUSED(contextObjectId); - Q_UNUSED(code); - Q_UNUSED(language); - return unimplemented(); -} - -bool ScriptableExtension::isScriptLanguageSupported(ScriptLanguage lang) const -{ - Q_UNUSED(lang); - return false; -} - -void ScriptableExtension::acquire(quint64 objId) -{ - Q_UNUSED(objId); -} - -QVariant ScriptableExtension::acquireValue(const QVariant& v) -{ - if (v.canConvert()) { - Object o = v.value(); - o.owner->acquire(o.objId); - } else if (v.canConvert()) { - FunctionRef fr = v.value(); - fr.base.owner->acquire(fr.base.objId); - } - return v; -} - -void ScriptableExtension::release(quint64 objId) -{ - Q_UNUSED(objId); -} - -QVariant ScriptableExtension::releaseValue(const QVariant& v) -{ - if (v.canConvert()) { - Object o = v.value(); - o.owner->release(o.objId); - } else if (v.canConvert()) { - FunctionRef fr = v.value(); - fr.base.owner->release(fr.base.objId); - } - return v; -} - -// LiveConnectExtension -> ScriptableExtension adapter. We use -// lc object IDs as our own object IDs. -// ---------------------------------------------------------------------------- -ScriptableLiveConnectExtension::ScriptableLiveConnectExtension(QObject* p, LiveConnectExtension* old): - ScriptableExtension(p), wrapee(old) -{ - connect(wrapee, - SIGNAL(partEvent(ulong,QString,KParts::LiveConnectExtension::ArgList)), - this, - SLOT(liveConnectEvent(ulong,QString,KParts::LiveConnectExtension::ArgList))); -} - -QVariant ScriptableLiveConnectExtension::rootObject() -{ - // Plugin root is always LC object #0. - return acquireValue(QVariant::fromValue(ScriptableExtension::Object(this, 0))); -} - -bool ScriptableLiveConnectExtension::hasProperty(ScriptableExtension*, quint64 objId, const QString& propName) -{ - QVariant val = get(0, objId, propName); - bool ok = !val.canConvert(); - releaseValue(val); - return ok; -} - -// Note that since we wrap around a plugin, and do not implement the browser, -// we do not perform XSS checks ourselves. -QVariant ScriptableLiveConnectExtension::callFunctionReference(ScriptableExtension*, - quint64 o, const QString& f, const ScriptableExtension::ArgList& a) -{ - QStringList qargs; - // Convert args to strings for LC use. - for (int i = 0; i < a.size(); ++i) { - bool ok; - qargs.append(toLC(a[i], &ok)); - if (!ok) - return unimplemented(); - } - - LiveConnectExtension::Type retType; - unsigned long retObjId; - QString retVal; - if (wrapee->call((unsigned long)o, f, qargs, retType, retObjId, retVal)) { - return acquireValue(fromLC(QString(), retType, retObjId, retVal)); - } else { - return unimplemented(); - } -} - -QVariant ScriptableLiveConnectExtension::get(ScriptableExtension*, - quint64 objId, const QString& propName) -{ - LiveConnectExtension::Type retType; - unsigned long retObjId; - QString retVal; - if (wrapee->get((unsigned long)objId, propName, retType, retObjId, retVal)) { - return acquireValue(fromLC(propName, retType, retObjId, retVal)); - } else { - // exception signals failure. ### inellegant - return unimplemented(); - } -} - -bool ScriptableLiveConnectExtension::put(ScriptableExtension*, quint64 objId, - const QString& propName, const QVariant& value) -{ - bool ok; - QString val = toLC(value, &ok); - if (!ok) - return false; - - return wrapee->put((unsigned long)objId, propName, val); -} - -QVariant ScriptableLiveConnectExtension::fromLC(const QString& name, - LiveConnectExtension::Type type, - unsigned long objId, - const QString& value) -{ - switch (type) { - case KParts::LiveConnectExtension::TypeBool: { - bool ok; - int i = value.toInt(&ok); - if (ok) - return QVariant(bool(i)); - return QVariant(value.toLower() == QLatin1String("true")); - } - case KParts::LiveConnectExtension::TypeObject: - case KParts::LiveConnectExtension::TypeFunction: { - if (!refCounts.contains(objId)) - refCounts[objId] = 0; - - Object o = ScriptableExtension::Object(this, objId); - if (type == KParts::LiveConnectExtension::TypeObject) - return QVariant::fromValue(o); - else - return QVariant::fromValue(FunctionRef(o, name)); - } - - case KParts::LiveConnectExtension::TypeNumber: - return QVariant(value.toDouble()); - - case KParts::LiveConnectExtension::TypeString: - return QVariant(value); - - case KParts::LiveConnectExtension::TypeVoid: - default: - return QVariant::fromValue(ScriptableExtension::Undefined()); - } -} - -QString ScriptableLiveConnectExtension::toLC(const QVariant& in, bool* ok) -{ - *ok = true; // most of the time. - - // Objects (or exceptions) can't be converted - if (in.canConvert() || - in.canConvert() || - in.canConvert()) { - - *ok = false; - return QString(); - } - - // Convert null and undefined to appropriate strings - // ### this matches old KHTML behavior, but is this sensible? - if (in.canConvert()) - return QString::fromLatin1("null"); - - if (in.canConvert()) - return QString::fromLatin1("undefined"); - - if (in.type() == QVariant::Bool) - return in.toBool() ? QString::fromLatin1("true") : QString::fromLatin1("false"); - - // Just stringify everything else, makes sense for nums as well. - if (in.canConvert()) - return in.toString(); - - // something really icky... - *ok = false; - return QString(); -} - -void ScriptableLiveConnectExtension::acquire(quint64 objId) -{ - ++refCounts[objId]; -} - -void ScriptableLiveConnectExtension::release(quint64 objId) -{ - int newRC = --refCounts[objId]; - if (!newRC) { - if (objId != 0) - wrapee->unregister((unsigned long)objId); - refCounts.remove(objId); - } -} - -void ScriptableLiveConnectExtension::liveConnectEvent(const unsigned long, const QString& event, - const LiveConnectExtension::ArgList& args) -{ - // We want to evaluate in the enclosure's context. - QVariant enclosure = enclosingObject(); - if (!enclosure.canConvert()) { - releaseValue(enclosure); - kDebug(1000) << "No enclosure, can't evaluate"; - return; - } - - Object enclosureObj = enclosure.value(); - - if (!host()->isScriptLanguageSupported(ECMAScript)) { - releaseValue(enclosure); - kDebug(1000) << "Host can't evaluate ECMAScript"; - } - - // Compute a string to evaluate. We ned to escape a lot of stuff - // since we're composing a bunch of strings into one. - QString script; - script.sprintf("%s(", event.toLatin1().constData()); - - LiveConnectExtension::ArgList::const_iterator i = args.begin(); - const LiveConnectExtension::ArgList::const_iterator argsBegin = i; - const LiveConnectExtension::ArgList::const_iterator argsEnd = args.end(); - - for ( ; i != argsEnd; ++i) { - if (i != argsBegin) - script += ","; - if ((*i).first == KParts::LiveConnectExtension::TypeString) { - script += "\""; - script += QString((*i).second).replace('\\', "\\\\").replace('"', "\\\""); - script += "\""; - } else - script += (*i).second; - } - script += ")"; - - kDebug(1000) << script; - - // Ask host to evaluate.. (unfortunately, we can't do anything with the result, - // but anything that uses this interface isn't expective one in the first place) - QVariant result = host()->evaluateScript(this, enclosureObj.objId, script); - - releaseValue(result); - releaseValue(enclosure); -} - -// hash functions -// ---------------------------------------------------------------------------- - -unsigned int qHash(const KParts::ScriptableExtension::Object& o) -{ - return qHash(qMakePair(o.owner, o.objId)); -} - -unsigned int qHash(const KParts::ScriptableExtension::FunctionRef& f) -{ - return qHash(qMakePair(f.base, f.field)); -} - -} // namespace KParts - - - -#include "moc_scriptableextension.cpp" -#include "moc_scriptableextension_p.cpp" -// kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on; - diff --git a/kparts/scriptableextension.h b/kparts/scriptableextension.h deleted file mode 100644 index a5334d11..00000000 --- a/kparts/scriptableextension.h +++ /dev/null @@ -1,318 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2010 Maksim Orlovich - Copyright (C) 2002 Koos Vriezen - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KPARTS_SCRIPTABLEEXTENSION_H -#define KPARTS_SCRIPTABLEEXTENSION_H - -#include -#include -#include -#include - -namespace KParts { - -class ScriptableExtension; -struct ScriptableExtensionPrivate; -class LiveConnectExtension; - -/** - * An extension class that permits KParts to be scripted (such as when embedded - * inside a KHTMLPart) and to access the host's scriptable objects as well. - * - * See @ref ScriptValueTypes for how values are passed to/from various methods here. - * - * @since 4.5 - * \nosubgrouping - */ -class KPARTS_EXPORT ScriptableExtension: public QObject -{ - Q_OBJECT -public: - /** @defgroup ScriptValueTypes Script Value Types - * @{ - * Values are passed to and from scriptable methods or properties as QVariants. - * Valid values may be bools, strings, and numbers (doubles), as well as the - * following custom types: - * \li @ref Null - * \li @ref Undefined - * \li @ref Exception - * \li @ref Object - * \li @ref FunctionRef - */ - - /// Corresponds to 'null' in JavaScript - struct Null {}; - - /// Corresponds to 'undefined' in JavaScript - struct Undefined {}; - - /// Returned from operations to denote a failure. May not be passed in as - /// a parameter, only returned - struct Exception { - /// Error message returned from the callee. This should be assumed to be - /// low-level (in particular, it might not be translated) and should - /// only be displayed in low-level debugging tools and the like. - QString message; - - Exception() {} - Exception(const QString& msg): message(msg) {} - }; - - /// Objects are abstracted away as a pair of the ScriptableExtension - /// the performs operations on it, and an implementation-specific Id, - /// which gets passed to the extension's methods. - /// - /// Objects are reference-counted, with the following protocol: - /// 1) Return values from methods, rootObject(), enclosingObject(), - /// and get() are already acquired by the producer, so the consumer - /// should release them when done. - /// 2) During a call, the caller guarantees that all the arguments - /// will be live for the calls duration, but the callee must - /// acquire them if it stores it for longer than that. - /// - /// @see acquire, acquireValue, release, releaseValue - struct Object { - ScriptableExtension* owner; - quint64 objId; - - Object(): owner(0), objId(0) {} - Object(ScriptableExtension* o, quint64 id): owner(o), objId(id) {} - bool operator==(const Object& other) const { return owner == other.owner && objId == other.objId; } - }; - - /// Function references are a pair of an object and a field in it. - /// Essentially, if you have a base.field(something) call, the - /// 'base' needs to be passed as the 'this' to the function, and - /// these references can be used to resolve that. - struct FunctionRef { - Object base; - QString field; - - FunctionRef() {} - FunctionRef(const Object& b, const QString&f): base(b), field(f) {} - bool operator==(const FunctionRef& other) const { return base == other.base && field == other.field; } - }; - - //@} - - - ///@name lifetime - //@{ -protected: - ScriptableExtension(QObject* parent); -public: - virtual ~ScriptableExtension(); - - /** - * Queries @p obj for a child object which inherits from this - * ScriptableExtension class. Convenience method. - */ - static ScriptableExtension* childObject(QObject* obj); - - /** - * This returns a bridge object that permits KParts implementing the older - * LiveConnectExtension to be used via the ScriptableExtension API. - * The bridge's parent will be the @p parentObj. - */ - static ScriptableExtension* adapterFromLiveConnect(QObject* parentObj, - LiveConnectExtension* oldApi); - - //@} - - - ///@name Object Hierarchy - //@{ - - /** - * Reports the hosting ScriptableExtension to a child. It's the responsibility - * of a parent part to call this method on all of its kids' ScriptableExtensions - * as soon as possible. - */ - void setHost(ScriptableExtension* host); - - /** - * Returns any registered parent scripting context. May be 0 if setHost - * was not called (or not call yet). - */ - ScriptableExtension* host() const; - - /** - * Return the root scriptable object of this KPart. - * For example for an HTML part, it would represent a Window object. - * May be undefined or null - */ - virtual QVariant rootObject(); - - /** - * Returns an object that represents the host()'s view of us. - * For example, if the host is an HTML part, it would return - * a DOM node of an <object> handled by this part. - * May be undefined or null - * - * Implemented in terms of objectForKid - * - */ - QVariant enclosingObject(); - //@} - - ///@name Object Operations - /// All these methods share the following conventions: - /// \li Values are passed and returned encoded as defined in - /// @ref ScriptValueTypes - /// \li All methods may return an exception if unsupported - /// \li All callers \b must provide an accurate callerPrincipal - /// argument describing which ScriptableExtension (and hence which KPart) - /// they're acting as. This is used to implement security checks. This - /// is \b not the same as the owner of an object. For example, if a plugin is - /// calling an operation on a KHTMLPart object, - /// then the 'this' parameter would be the object owner, a ScriptableExtension - /// provided by the KHTMLPart, while the callerPrincipal would be the - /// ScriptableExtension of the \em plugin. The extension is expected - /// to do appropriate cross-site scripting checks on this argument - /// if it is acting as a host. - //@{ - - typedef QList ArgList; - - /** - Try to use the object @p objId associated with 'this' as a function. - */ - virtual QVariant callAsFunction(ScriptableExtension* callerPrincipal, quint64 objId, const ArgList& args); - - /** - Try to use a function reference to field @p f of object @objId as a function - */ - virtual QVariant callFunctionReference(ScriptableExtension* callerPrincipal, quint64 objId, - const QString& f, const ArgList& args); - - /** - Try to use the object @p objId associated with 'this' as a constructor - (corresponding to ECMAScript's new foo(bar, baz, glarch) expression). - */ - virtual QVariant callAsConstructor(ScriptableExtension* callerPrincipal, quint64 objId, const ArgList& args); - - /** - Returns true if the object @p objId associated with 'this' has the property - @p propName. - */ - virtual bool hasProperty(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName); - - /** - Tries to get field @p propName from object @p objId associated with 'this'. - */ - virtual QVariant get(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName); - - /** - Tries to set the field @p propName from object @p objId associated with 'this' - to @p value. Returns true on success - */ - virtual bool put(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName, const QVariant& value); - - /** - Tries to remove the field d @p propName from object @p objId associated with 'this'. - Returns true on success - */ - virtual bool removeProperty(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName); - - /** - Tries to enumerate all fields of object @p objId associated with this to - @p result. Returns true on success - */ - virtual bool enumerateProperties(ScriptableExtension* callerPrincipal, quint64 objId, QStringList* result); - - /** - Tries to raise an exception with given message in this extension's scripting - context. Returns true on success - */ - virtual bool setException(ScriptableExtension* callerPrincipal, const QString& message); - - - enum ScriptLanguage { - ECMAScript, /// < also known as JavaScript - EnumLimit = 0xFFFF - }; - - /** - Tries to evaluate a script @p code with the given object as its context. - The parameter @p language specifies the language to execute it as. - Use isScriptLanguageSupported to check for support. - */ - virtual QVariant evaluateScript(ScriptableExtension* callerPrincipal, - quint64 contextObjectId, - const QString& code, - ScriptLanguage language = ECMAScript); - - /** returns true if this extension can execute scripts in the given - language */ - virtual bool isScriptLanguageSupported(ScriptLanguage lang) const; - - /** - increases reference count of object @p objId - */ - virtual void acquire(quint64 objid); - - /** - Helper that calls @ref acquire on any object or function reference base - stored in @p v. - - @return a copy of the passed in value - */ - static QVariant acquireValue(const QVariant& v); - - /** - decreases reference count of object @p objId - */ - virtual void release(quint64 objid); - - /** - Helper that calls @ref release on any object or function reference base - stored in @p v. - - @return a copy of the passed in value - */ - static QVariant releaseValue(const QVariant& v); - - //@} -private: - /** - * If this extension is a host that provides an object corresponding - * to each kid, override this method to provide it. - * @see enclosingObject - */ - virtual QVariant encloserForKid(KParts::ScriptableExtension* kid); - - ScriptableExtensionPrivate* const d; -}; - -KPARTS_EXPORT unsigned int qHash(const KParts::ScriptableExtension::Object& o); - -KPARTS_EXPORT unsigned int qHash(const KParts::ScriptableExtension::FunctionRef& f); - -} // namespace KParts - -Q_DECLARE_METATYPE(KParts::ScriptableExtension::Null) -Q_DECLARE_METATYPE(KParts::ScriptableExtension::Undefined) -Q_DECLARE_METATYPE(KParts::ScriptableExtension::Exception) -Q_DECLARE_METATYPE(KParts::ScriptableExtension::Object) -Q_DECLARE_METATYPE(KParts::ScriptableExtension::FunctionRef) - -#endif - -// kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on; diff --git a/kparts/scriptableextension_p.h b/kparts/scriptableextension_p.h deleted file mode 100644 index 9c17a2de..00000000 --- a/kparts/scriptableextension_p.h +++ /dev/null @@ -1,77 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2010 Maksim Orlovich - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KPARTS_SCRIPTABLEEXTENSION_P_H -#define KPARTS_SCRIPTABLEEXTENSION_P_H - -#include "browserextension.h" - -namespace KParts { - -// LiveConnectExtension -> ScriptableExtension adapter. -class ScriptableLiveConnectExtension: public ScriptableExtension -{ - Q_OBJECT -public: - ScriptableLiveConnectExtension(QObject* parent, LiveConnectExtension* old); - - QVariant rootObject(); - // enclosingObject: not applicable, plugins wouldn't have children - - // callAsFunction: we only have function rereferences. - QVariant callFunctionReference(ScriptableExtension* callerPrincipal, quint64 objId, - const QString& f, const ArgList& args); - - // callAsConstructor: unsupported by LC - - bool hasProperty(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName); - - QVariant get(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName); - - bool put(ScriptableExtension* callerPrincipal, quint64 objId, const QString& propName, const QVariant& value); - - // removeProperty: unsupported by LC - // enumerateProperties: unsupported by LC - // setException: unsupported by LC - // evaluateScript: unsupported by LC, though we have to - // route LC evaluation requests to our parent - // as appropriate - - void acquire(quint64 objid); - void release(quint64 objid); -private: - // LC uses 0-1 refcounting, we use arbitrary, so we need to call - // unregister when done. - QHash refCounts; - LiveConnectExtension* wrapee; - - // also registers when needed - QVariant fromLC(const QString& name, LiveConnectExtension::Type type, - unsigned long objId, const QString& value); - - QString toLC(const QVariant& in, bool* ok); -public Q_SLOTS: - void liveConnectEvent(const unsigned long, const QString&, const KParts::LiveConnectExtension::ArgList&); -}; - - -} // namespace KParts - -#endif -// kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;