TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ksvg_ecma.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. Copyright (C) 2002-2003 KSVG Team
  3. This file is part of the KDE project
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <kdebug.h>
  18. #include <tqvariant.h>
  19. #include <dom/dom2_events.h>
  20. #include "SVGEcma.h"
  21. #include "SVGDocumentImpl.h"
  22. #include "ksvg_ecma.h"
  23. #include "ksvg_window.h"
  24. #include "ksvg_scriptinterpreter.h"
  25. #include "ksvg_ecmaeventlistener.h"
  26. #include "KSVGLoader.h"
  27. using namespace KSVG;
  28. using namespace KJS;
  29. class AsyncStatus : public ObjectImp
  30. {
  31. public:
  32. AsyncStatus() : ObjectImp() { }
  33. virtual bool implementsCall() const { return true; }
  34. virtual Value call(ExecState *exec, Object &thisObj, const List &args);
  35. };
  36. Value AsyncStatus::call(ExecState *exec, Object &, const List &args)
  37. {
  38. kdDebug(26004) << "[AsyncStatus] " << args[0].toString(exec).ascii() << endl;
  39. if(args[0].toString(exec) == "success")
  40. return Number(1);
  41. else
  42. return Undefined();
  43. }
  44. KSVGEcma::KSVGEcma(SVGDocumentImpl *doc) : m_doc(doc)
  45. {
  46. m_init = false;
  47. m_hasListeners = false;
  48. m_window = 0;
  49. m_interpreter = 0;
  50. m_ecmaEventListeners.setAutoDelete(true);
  51. }
  52. KSVGEcma::~KSVGEcma()
  53. {
  54. // We are 0 soon so event listeners may NOT call us
  55. TQPtrListIterator<KSVGEcmaEventListener> it(m_ecmaEventListeners);
  56. for(; it.current(); ++it)
  57. it.current()->forbidRemove();
  58. if(m_interpreter)
  59. delete m_interpreter;
  60. }
  61. bool KSVGEcma::initialized()
  62. {
  63. return m_init;
  64. }
  65. void KSVGEcma::setup()
  66. {
  67. if(m_init)
  68. return;
  69. m_init = true;
  70. // Create handler for js calls
  71. m_window = new KSVG::Window(m_doc);
  72. Object globalObject(m_window);
  73. // Create code interpreter
  74. m_interpreter = new KSVGScriptInterpreter(globalObject, m_doc);
  75. // Set object prototype for global object
  76. m_window->setPrototype(m_interpreter->builtinObjectPrototype());
  77. // Create bridge for document. Could be done on demand now, though
  78. KSVGBridge<SVGDocumentImpl> *documentRequest = new KSVGBridge<SVGDocumentImpl>(m_interpreter->globalExec(), m_doc);
  79. documentRequest->ref();
  80. m_interpreter->putDOMObject(m_doc->handle(), documentRequest);
  81. }
  82. Completion KSVGEcma::evaluate(const UString &code, const Value &thisV)
  83. {
  84. #ifdef KJS_VERBOSE
  85. kdDebug(6070) << "KSVGEcma::evaluate " << code.qstring() << endl;
  86. #endif
  87. return m_interpreter->evaluate(code, thisV);
  88. }
  89. Object KSVGEcma::globalObject()
  90. {
  91. return m_interpreter->globalObject();
  92. }
  93. ExecState *KSVGEcma::globalExec()
  94. {
  95. return m_interpreter->globalExec();
  96. }
  97. SVGEventListener *KSVGEcma::createEventListener(DOM::DOMString type)
  98. {
  99. TQPtrListIterator<KSVGEcmaEventListener> it(m_ecmaEventListeners);
  100. for(; it.current(); ++it)
  101. {
  102. if(it.current()->type() == type.string())
  103. return static_cast<SVGEventListener *>(it.current());
  104. }
  105. setup();
  106. Object constr = m_interpreter->builtinFunction();
  107. List args;
  108. args.append(String("event"));
  109. args.append(String(type.string()));
  110. Object obj = constr.construct(m_interpreter->globalExec(), args);
  111. // Note that the KSVGEcmaEventListener constructor adds itself to the m_ecmaEventListeners list
  112. KSVGEcmaEventListener *event = new KSVGEcmaEventListener(obj, type.string(), this);
  113. event->ref();
  114. // addEventListener() is called by KSVGEcmaListeners ctor, so it's
  115. // safe to check to count of the eventListeners list (Niko)
  116. if(m_ecmaEventListeners.count() > 0)
  117. m_hasListeners = true;
  118. return event;
  119. }
  120. TQString KSVGEcma::valueOfEventListener(SVGEventListener *listener) const
  121. {
  122. KSVGEcmaEventListener *event = static_cast<KSVGEcmaEventListener *>(listener);
  123. if(!event)
  124. return TQString();
  125. return event->type();
  126. }
  127. void KSVGEcma::addEventListener(KSVGEcmaEventListener *listener)
  128. {
  129. m_ecmaEventListeners.append(listener);
  130. }
  131. void KSVGEcma::removeEventListener(KSVGEcmaEventListener *listener)
  132. {
  133. m_ecmaEventListeners.take(m_ecmaEventListeners.find(listener));
  134. if(m_ecmaEventListeners.count() == 0)
  135. m_hasListeners = false;
  136. }
  137. bool KSVGEcma::hasEventListeners()
  138. {
  139. return m_hasListeners;
  140. }
  141. void KSVGEcma::finishedWithEvent(SVGEventImpl *event)
  142. {
  143. KSVGScriptInterpreter *interpreter = static_cast<KSVGScriptInterpreter *>(globalExec()->interpreter());
  144. interpreter->removeDOMObject(event);
  145. }
  146. Value KSVGEcma::getUrl(ExecState *exec, ::KURL url)
  147. {
  148. Object *status = new Object(new AsyncStatus());
  149. // FIXME: Security issue, allows local testing of getURL(), REMOVE BEFORE RELEASE! (Niko)
  150. TQString svgDocument = KSVGLoader::getUrl(url, true);
  151. if(svgDocument.length() > 0)
  152. {
  153. status->put(exec, Identifier("success"), Boolean(true));
  154. status->put(exec, Identifier("content"), String(svgDocument));
  155. }
  156. else
  157. {
  158. status->put(exec, Identifier("success"), Boolean(false));
  159. status->put(exec, Identifier("content"), String(""));
  160. }
  161. return Value(*status);
  162. }
  163. void KSVGEcma::postUrl(ExecState *exec, ::KURL url, const TQString &data, const TQString &mimeType, const TQString &contentEncoding, Object &callBackFunction)
  164. {
  165. Object *status = new Object(new AsyncStatus());
  166. status->put(exec, Identifier("content"), String(""));
  167. status->put(exec, Identifier("success"), Boolean(false));
  168. TQByteArray byteArray;
  169. TQDataStream ds(byteArray, IO_WriteOnly);
  170. ds << data;
  171. // Support gzip compression
  172. if(contentEncoding == "gzip" || contentEncoding == "deflate")
  173. byteArray = tqCompress(byteArray);
  174. KSVGLoader *loader = new KSVGLoader();
  175. loader->postUrl(url, byteArray, mimeType, exec, callBackFunction, *status);
  176. delete loader;
  177. }
  178. // Helpers
  179. Value KSVG::getDOMNode(ExecState *exec, DOM::Node n)
  180. {
  181. ObjectImp *ret = 0;
  182. if(n.isNull())
  183. return Null();
  184. KSVGScriptInterpreter *interpreter = static_cast<KSVGScriptInterpreter *>(exec->interpreter());
  185. ObjectImp *request = interpreter->getDOMObject(n.handle());
  186. if(request)
  187. return Value(request);
  188. SVGElementImpl *elem = 0;
  189. switch(n.nodeType())
  190. {
  191. case DOM::Node::ELEMENT_NODE:
  192. elem = interpreter->document()->getElementFromHandle(n.handle());
  193. if(!elem)
  194. {
  195. // Lookup different document, if possible
  196. SVGDocumentImpl *different = interpreter->document()->getDocumentFromHandle(n.ownerDocument().handle());
  197. if(!different)
  198. return Null();
  199. elem = different->getElementFromHandle(n.handle());
  200. if(!elem)
  201. return Null();
  202. }
  203. // The generated bridge() function does not ref the ret itself
  204. ret = elem->bridge(exec);
  205. ret->ref();
  206. break;
  207. case DOM::Node::TEXT_NODE:
  208. ret = new KSVGRWBridge<SVGDOMTextBridge>(exec, new SVGDOMTextBridge(n));
  209. ret->ref();
  210. break;
  211. default:
  212. ret = new KSVGBridge<SVGDOMNodeBridge>(exec, new SVGDOMNodeBridge(n));
  213. ret->ref();
  214. break;
  215. }
  216. interpreter->putDOMObject(n.handle(), ret);
  217. return Value(ret);
  218. }
  219. Value KSVG::getDOMEvent(ExecState *exec, SVGEventImpl *e)
  220. {
  221. return e->cache(exec);
  222. }
  223. Value KSVG::getString(DOM::DOMString s)
  224. {
  225. if(s.isNull())
  226. return Null();
  227. else
  228. return String(s);
  229. }
  230. DOM::Node KSVG::toNode(const Value &val)
  231. {
  232. Object obj = Object::dynamicCast(val);
  233. if(obj.isNull())
  234. return DOM::Node();
  235. SVGDOMNodeBridge *bridge = toNodeBridge(static_cast<ObjectImp *>(obj.imp()));
  236. if(bridge)
  237. return bridge->impl();
  238. return DOM::Node();
  239. }
  240. TQVariant KSVG::valueToVariant(ExecState *exec, const Value &val)
  241. {
  242. TQVariant res;
  243. switch(val.type())
  244. {
  245. case BooleanType:
  246. res = TQVariant(val.toBoolean(exec));
  247. break;
  248. case NumberType:
  249. res = TQVariant(val.toNumber(exec));
  250. break;
  251. case StringType:
  252. res = TQVariant(val.toString(exec).qstring());
  253. break;
  254. default:
  255. // everything else will be 'invalid'
  256. break;
  257. }
  258. return res;
  259. }
  260. // vim:ts=4:noet