kjsembed
eventproxy.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2003,2004,2005,2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2003,2004,2005,2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2003,2004,2005,2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2003,2004,2005,2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 #include "eventproxy.h" 00023 00024 #include <QtCore/QCoreApplication> 00025 00026 #include "qobject_binding.h" 00027 #include <kjs/interpreter.h> 00028 00029 #include "kjseglobal.h" 00030 #include "jseventmapper.h" 00031 #include "jseventutils.h" 00032 00033 using namespace KJSEmbed; 00034 00035 EventProxy::EventProxy( QObjectBinding *watch, KJS::Interpreter *interpreter ) : 00036 QObject(watch->object<QObject>()), m_watch(watch), m_interpreter(interpreter) 00037 { 00038 m_refcount = 0l; 00039 } 00040 00041 EventProxy::~EventProxy() 00042 { 00043 } 00044 00045 bool EventProxy::isFiltered( QEvent::Type t ) const 00046 { 00047 if ( m_eventMask.size() <= t ) 00048 return false; 00049 return m_eventMask.testBit( t ); 00050 } 00051 00052 void EventProxy::addFilter( QEvent::Type t ) 00053 { 00054 if( t == QEvent::None ) 00055 return; 00056 if ( !m_refcount ) 00057 m_watch->object<QObject>()->installEventFilter( this ); 00058 00059 if ( m_eventMask.size() <= t ) 00060 m_eventMask.resize( t + 1); 00061 00062 if ( !m_eventMask.testBit(t) ) 00063 { 00064 m_refcount++; 00065 m_eventMask.setBit( t ); 00066 } 00067 } 00068 00069 void EventProxy::removeFilter( QEvent::Type t ) 00070 { 00071 if( t == QEvent::None ) 00072 return; 00073 if ( m_eventMask.size() <= t ) 00074 return; 00075 m_eventMask.clearBit( t ); 00076 m_refcount--; 00077 if ( !m_refcount ) 00078 { 00079 m_watch->object<QObject>()->removeEventFilter( this ); 00080 deleteLater(); 00081 } 00082 } 00083 00084 bool EventProxy::eventFilter( QObject * /*watched*/, QEvent *e ) 00085 { 00086 if ( isFiltered(e->type()) ) 00087 { 00088 return !callHandler( e ); 00089 } 00090 return false; 00091 } 00092 00093 bool EventProxy::callHandler( QEvent *e ) 00094 { 00095 // Be careful enabling this as if there are a lot of events then the event loop times 00096 // out and the app crashes with 'Alarm Clock'. 00097 // qDebug("JSObjectEventProxy::callHandler() event type %d" , e->type() ); 00098 00099 KJS::ExecState *exec = m_interpreter->globalExec(); 00100 KJS::Identifier id = JSEventMapper::mapper()->findEventHandler( e->type() ); 00101 00102 KJS::JSObject *jsobj(m_watch); 00103 KJS::JSObject *fun = jsobj->get(exec, id )->toObject( exec ); 00104 00105 KJS::JSValue *retValue; 00106 if ( !fun->implementsCall() ) 00107 { 00108 QString msg = i18n( "Bad event handler: Object %1 Identifier %2 Method %3 Type: %4.", 00109 jsobj->className().ascii(), 00110 id.ascii(), 00111 fun->className().ascii(), 00112 e->type()); 00113 retValue = throwError(exec, KJS::TypeError, msg); 00114 } 00115 else 00116 { 00117 // Process args 00118 KJS::List args; 00119 args.append( JSEventUtils::event(exec, e) ); 00120 00121 // Call handler 00122 retValue = fun->call( exec, jsobj, args ); 00123 } 00124 00125 if ( exec->hadException() ) 00126 { 00127 if (m_interpreter->shouldPrintExceptions()) 00128 { 00129 KJS::JSLock lock; 00130 KJS::JSObject* exceptObj = retValue->toObject(exec); 00131 QString message = toQString(exceptObj->toString(exec)); 00132 QString sourceURL = toQString(exceptObj->get(exec, "sourceURL")->toString(exec)); 00133 int sourceId = exceptObj->get(exec, "sourceId")->toUInt32(exec); 00134 int line = exceptObj->get(exec, "line")->toUInt32(exec); 00135 (*KJSEmbed::conerr()) << i18n("Exception calling '%1' function from %2:%3:%4", id.ascii(), !sourceURL.isEmpty() ? sourceURL : QString::number(sourceId), line, message) << endl; 00136 } 00137 00138 00139 // clear it so it doesn't stop other things 00140 exec->clearException(); 00141 return false; 00142 } 00143 00144 return true; 00145 } 00146 00147 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KDE 4.6 API Reference