diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 01:29:50 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 01:29:50 +0000 |
commit | 8362bf63dea22bbf6736609b0f49c152f975eb63 (patch) | |
tree | 0eea3928e39e50fae91d4e68b21b1e6cbae25604 /lib/kross/api | |
download | koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.tar.gz koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.zip |
Added old abandoned KDE3 version of koffice
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/koffice@1077364 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'lib/kross/api')
33 files changed, 4093 insertions, 0 deletions
diff --git a/lib/kross/api/Makefile.am b/lib/kross/api/Makefile.am new file mode 100644 index 00000000..166e1bfa --- /dev/null +++ b/lib/kross/api/Makefile.am @@ -0,0 +1,47 @@ +include $(top_srcdir)/lib/kross/Makefile.global +apiincludedir=$(includedir)/kross/api + +apiinclude_HEADERS = \ + callable.h \ + class.h \ + dict.h \ + eventaction.h \ + event.h \ + eventscript.h \ + eventsignal.h \ + eventslot.h \ + exception.h \ + function.h \ + interpreter.h \ + list.h \ + module.h \ + object.h \ + proxy.h \ + qtobject.h \ + script.h \ + value.h \ + variant.h + +lib_LTLIBRARIES = libkrossapi.la + +libkrossapi_la_SOURCES = \ + object.cpp \ + variant.cpp \ + list.cpp \ + dict.cpp \ + exception.cpp \ + callable.cpp \ + eventaction.cpp \ + eventsignal.cpp \ + eventslot.cpp \ + eventscript.cpp \ + qtobject.cpp \ + script.cpp \ + interpreter.cpp + +libkrossapi_la_LDFLAGS = $(all_libraries) $(VER_INFO) -Wnounresolved +libkrossapi_la_LIBADD = $(LIB_QT) $(LIB_KDECORE) + +METASOURCES = AUTO +SUBDIRS = . +INCLUDES = $(KROSS_INCLUDES) $(all_includes) diff --git a/lib/kross/api/callable.cpp b/lib/kross/api/callable.cpp new file mode 100644 index 00000000..261a1bf0 --- /dev/null +++ b/lib/kross/api/callable.cpp @@ -0,0 +1,139 @@ +/*************************************************************************** + * callable.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "callable.h" +#include "variant.h" +#include "dict.h" + +#include "../main/krossconfig.h" + +using namespace Kross::Api; + +Callable::Callable(const QString& name) + : Object() + , m_name(name) +{ +} + +Callable::~Callable() +{ +} + +const QString Callable::getName() const +{ + return m_name; +} + +const QString Callable::getClassName() const +{ + return "Kross::Api::Callable"; +} + +bool Callable::hasChild(const QString& name) const +{ + return m_children.contains(name); +} + +Object::Ptr Callable::getChild(const QString& name) const +{ + return m_children[name]; +} + +QMap<QString, Object::Ptr> Callable::getChildren() const +{ + return m_children; +} + +bool Callable::addChild(const QString& name, Object* object) +{ +#ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG + krossdebug( QString("Kross::Api::Callable::addChild() object.name='%1' object.classname='%2'") + .arg(name).arg(object->getClassName()) ); +#endif + m_children.replace(name, Object::Ptr(object)); + return true; +} + +bool Callable::addChild(Callable* object) +{ + return addChild(object->getName(), object); +} + +void Callable::removeChild(const QString& name) +{ +#ifdef KROSS_API_OBJECT_REMCHILD_DEBUG + krossdebug( QString("Kross::Api::Callable::removeChild() name='%1'").arg(name) ); +#endif + m_children.remove(name); +} + +void Callable::removeAllChildren() +{ +#ifdef KROSS_API_OBJECT_REMCHILD_DEBUG + krossdebug( "Kross::Api::Callable::removeAllChildren()" ); +#endif + m_children.clear(); +} + +Object::Ptr Callable::call(const QString& name, List::Ptr args) +{ +#ifdef KROSS_API_CALLABLE_CALL_DEBUG + krossdebug( QString("Kross::Api::Callable::call() name=%1 getName()=%2 arguments=%3").arg(name).arg(getName()).arg(args ? args->toString() : QString("")) ); +#endif + + if(name.isEmpty()) // return a self-reference if no functionname is defined. + return this; + + // if name is defined try to get the matching child and pass the call to it. + Object::Ptr object = getChild(name); + if(object) { + //TODO handle namespace, e.g. "mychild1.mychild2.myfunction" + return object->call(name, args); + } + + if(name == "get") { + QString s = Variant::toString(args->item(0)); + Object::Ptr obj = getChild(s); + if(! obj) + throw Exception::Ptr( new Exception(QString("The object '%1' has no child object '%2'").arg(getName()).arg(s)) ); + return obj; + } + else if(name == "has") { + return new Variant( hasChild( Variant::toString(args->item(0)) ) ); + } + else if(name == "call") { + //TODO should we remove first args-item? + return Object::call(Variant::toString(args->item(0)), args); + } + else if(name == "list") { + QStringList list; + QMap<QString, Object::Ptr> children = getChildren(); + QMap<QString, Object::Ptr>::Iterator it( children.begin() ); + for(; it != children.end(); ++it) + list.append( it.key() ); + return new Variant(list); + } + else if(name == "dict") { + return new Dict( getChildren() ); + } + + // If there exists no such object return NULL. + krossdebug( QString("Object '%1' has no callable object named '%2'.").arg(getName()).arg(name) ); + return 0; +} diff --git a/lib/kross/api/callable.h b/lib/kross/api/callable.h new file mode 100644 index 00000000..4d25bd91 --- /dev/null +++ b/lib/kross/api/callable.h @@ -0,0 +1,147 @@ +/*************************************************************************** + * callable.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_CALLABLE_H +#define KROSS_API_CALLABLE_H + +#include "object.h" +#include "list.h" +//#include "exception.h" + +#include <qstring.h> +#include <qvaluelist.h> +#include <ksharedptr.h> + +namespace Kross { namespace Api { + + /** + * Base class for callable objects. Classes like + * \a Event or \a Class are inherited from this class + * and implement the \a Object::call() method to handle + * the call. + */ + class Callable : public Object + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<Callable> Ptr; + + /** + * Constructor. + * + * \param name The name this callable object has and + * it is reachable as via \a getChild() . + */ + Callable(const QString& name); + + /** + * Destructor. + */ + virtual ~Callable(); + + /** + * \return the name this object has. Each callable object + * has a name which is used e.g. on \a addChild to be able + * to identify the object itself. + */ + const QString getName() const; + + /** + * Return the class name. This could be something + * like "Kross::Api::Callable" for this object. The + * value is mainly used for display purposes. + * + * \return The name of this class. + */ + virtual const QString getClassName() const; + + /** + * Returns if the defined child is avaible. + * + * \return true if child exists else false. + */ + bool hasChild(const QString& name) const; + + /** + * Return the defined child or NULL if there is + * no such object with that name avaible. + * + * \param name The name of the Object to return. + * \return The Object matching to the defined + * name or NULL if there is no such Object. + */ + Object::Ptr getChild(const QString& name) const; + + /** + * Return all children. + * + * \return A \a ObjectMap of children this Object has. + */ + QMap<QString, Object::Ptr> getChildren() const; + + /** + * Add a new child. Replaces a possible already existing + * child with such a name. + * + * \param name the name of the child + * \param object The Object to add. + * \return true if the Object was added successfully + * else false. + */ + bool addChild(const QString& name, Object* object); + + /** + * Same as the \a addChild method above but for callable + * objects which define there own name. + */ + bool addChild(Callable* object); + + /** + * Remove an existing child. + * + * \param name The name of the Object to remove. + * If there doesn't exists an Object with + * such name just nothing will be done. + */ + void removeChild(const QString& name); + + /** + * Remove all children. + */ + void removeAllChildren(); + + /** + * Call the object. + */ + virtual Object::Ptr call(const QString& name, List::Ptr arguments); + + private: + /// Name this callable object has. + QString m_name; + /// A list of childobjects. + QMap<QString, Object::Ptr> m_children; + }; + +}} + +#endif + diff --git a/lib/kross/api/class.h b/lib/kross/api/class.h new file mode 100644 index 00000000..afcfe425 --- /dev/null +++ b/lib/kross/api/class.h @@ -0,0 +1,89 @@ +/*************************************************************************** + * class.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_CLASS_H +#define KROSS_API_CLASS_H + +#include <qstring.h> +//#include <qvaluelist.h> +//#include <qmap.h> +#include <qobject.h> + +//#include "../main/krossconfig.h" +#include "object.h" +#include "event.h" +#include "list.h" +#include "exception.h" +#include "variant.h" + +namespace Kross { namespace Api { + + /** + * From \a Event inherited template-class to represent + * class-structures. Classes implemetating this template + * are able to dynamicly define \a Event methodfunctions + * accessible from within scripts. + */ + template<class T> + class Class : public Event<T> + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<T> Ptr; + + /** + * Constructor. + * + * \param name The name this class has. + */ + Class(const QString& name) + : Event<T>(name) + { + } + + /** + * Destructor. + */ + virtual ~Class() + { + } + + template<typename TYPE> + static Object::Ptr toObject(TYPE t) { return t; } + + operator T* () { return (T*)this; } + //operator Ptr () { return (T*)this; } + + /* + virtual Object::Ptr call(const QString& name, List::Ptr arguments) + { + krossdebug( QString("Class::call(%1)").arg(name) ); + return Event<T>::call(name, arguments); + } + */ + + }; + +}} + +#endif + diff --git a/lib/kross/api/dict.cpp b/lib/kross/api/dict.cpp new file mode 100644 index 00000000..b9fa5ddc --- /dev/null +++ b/lib/kross/api/dict.cpp @@ -0,0 +1,47 @@ +/*************************************************************************** + * dict.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "dict.h" +//#include "exception.h" + +using namespace Kross::Api; + +Dict::Dict(const QMap<QString, Object::Ptr> value) + : Value< List, QMap<QString, Object::Ptr> >(value) +{ +} + +Dict::~Dict() +{ +} + +const QString Dict::getClassName() const +{ + return "Kross::Api::Dict"; +} + +const QString Dict::toString() +{ + QString s = "["; + QMap<QString, Object::Ptr> list = getValue(); + for(QMap<QString, Object::Ptr>::Iterator it = list.begin(); it != list.end(); ++it) + s += "'" + it.key() + "' = '" + it.data()->toString() + "', "; + return (s.endsWith(", ") ? s.left(s.length() - 2) : s) + "]"; +} + diff --git a/lib/kross/api/dict.h b/lib/kross/api/dict.h new file mode 100644 index 00000000..773f36ac --- /dev/null +++ b/lib/kross/api/dict.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * dict.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_DICT_H +#define KROSS_API_DICT_H + +#include <qstring.h> +#include <qmap.h> + +#include "object.h" +#include "value.h" + +namespace Kross { namespace Api { + + /** + * The Dict class implementates \a Value to handle + * key=value base dictonaries/maps. + */ + class Dict : public Value< List, QMap<QString, Object::Ptr> > + { + friend class Value< List, QMap<QString, Object::Ptr> >; + public: + + /** + * Constructor. + * + * @param value The map of \a Object instances accessible + * via there string-identifier that is in this + * \a Dict intialy. + */ + explicit Dict(const QMap<QString, Object::Ptr> value); + + /** + * Destructor. + */ + virtual ~Dict(); + + /// \see Kross::Api::Object::getClassName() + virtual const QString getClassName() const; + + /** + * \return a string representation of the whole dictonary. + * + * \see Kross::Api::Object::toString() + */ + virtual const QString toString(); + + }; + +}} + +#endif + diff --git a/lib/kross/api/event.h b/lib/kross/api/event.h new file mode 100644 index 00000000..2df5a331 --- /dev/null +++ b/lib/kross/api/event.h @@ -0,0 +1,229 @@ +/*************************************************************************** + * event.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_EVENT_H +#define KROSS_API_EVENT_H + +#include "../main/krossconfig.h" +#include "object.h" +#include "callable.h" +#include "list.h" +#include "exception.h" +#include "function.h" +#include "proxy.h" +#include "variant.h" + +#include <qstring.h> +#include <qvaluelist.h> +#include <qmap.h> + +namespace Kross { namespace Api { + + /** + * Template class for all kinds of callable events. An + * event is the abstract base for callable objects like + * methodfunctions in \a Class instances or \a EventSlot + * and \a EventSignal to access Qt signals and slots. + */ + template<class T> + class Event : public Callable + { + private: + + /** + * Definition of function-pointers. + */ + typedef Object::Ptr(T::*FunctionPtr)(List::Ptr); + + /** + * List of memberfunctions. Each function is accessible + * by the functionname. + */ + QMap<QString, Function* > m_functions; + + public: + + /** + * Constructor. + * + * \param name The name this \a Event has. + */ + Event(const QString& name) + : Callable(name) + { + } + + /** + * Destructor. + */ + virtual ~Event() + { + QMapConstIterator<QString, Function* > endit = m_functions.constEnd(); + for(QMapConstIterator<QString, Function* > it = m_functions.constBegin(); it != endit; ++it) + delete it.data(); + } + + /** + * Add a \a Callable methodfunction to the list of functions + * this Object supports. + * + * The FunctionPtr points to the concret + * Object::Ptr myfuncname(List::Ptr) + * method in the class defined with template T. + * + * \param name The functionname. Each function this object + * holds should have an unique name to be + * still accessable. + * \param function A pointer to the methodfunction that + * should handle calls. + * + * \todo Remove this method as soon as there is no code using it + */ + inline void addFunction(const QString& name, FunctionPtr function) + { + m_functions.replace(name, new Function0<T>(static_cast<T*>(this), function)); + } + + /** + * Add a methodfunction to the list of functions this Object + * supports. + * + * \param name The functionname. Each function this object + * holds should have an unique name to be + * still accessable. + * \param function A \a Function instance which defines + * the methodfunction. This \a Event will be the + * owner of the \a Function instance and will take + * care of deleting it if this \a Event got deleted. + */ + inline void addFunction(const QString& name, Function* function) + { + m_functions.replace(name, function); + } + + /** + * Template function to add a \a ProxyFunction as builtin-function + * to this \a Event instance. + */ + template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class ARG4OBJ, class INSTANCE, typename METHOD> + inline void addFunction4(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0, ARG4OBJ* arg4 = 0) + { + m_functions.replace(name, + new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ, ARG4OBJ> + (instance, method, arg1, arg2, arg3, arg4) + ); + } + + /// Same as above with three arguments. + template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class INSTANCE, typename METHOD> + inline void addFunction3(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0) + { + m_functions.replace(name, + new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ> + (instance, method, arg1, arg2, arg3) + ); + } + + /// Same as above with two arguments. + template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class INSTANCE, typename METHOD> + inline void addFunction2(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0) + { + m_functions.replace(name, + new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ> + (instance, method, arg1, arg2) + ); + } + + /// Same as above, but with one argument. + template<class RETURNOBJ, class ARG1OBJ, class INSTANCE, typename METHOD> + inline void addFunction1(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0) + { + m_functions.replace(name, + new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ> + (instance, method, arg1) + ); + } + + /// Same as above with no arguments. + template<class RETURNOBJ, class INSTANCE, typename METHOD> + inline void addFunction0(const QString& name, INSTANCE* instance, METHOD method) + { + m_functions.replace(name, + new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ> + (instance, method) + ); + } + + /** + * Check if a function is a member of this \a Callable + * \param name the function name + * \return true if the function is available in this \a Callable + */ + bool isAFunction(const QString & name) const + { + return m_functions.contains(name); + } + + /** + * Overloaded method to handle function-calls. + * + * \throw AttributeException if argumentparameters + * arn't valid. + * \throw RuntimeException if the functionname isn't + * valid. + * \param name The functionname. Each function this + * Object holds should have a different + * name cause they are access by they name. + * If name is QString::null or empty, a + * self-reference to this instance is + * returned. + * \param arguments The list of arguments. + * \return An Object representing the call result + * or NULL if there doesn't exists such a + * function with defined name. + */ + virtual Object::Ptr call(const QString& name, List::Ptr arguments) + { +#ifdef KROSS_API_EVENT_CALL_DEBUG + krossdebug( QString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) ); +#endif + + Function* function = m_functions[name]; + if(function) { +#ifdef KROSS_API_EVENT_CALL_DEBUG + krossdebug( QString("Event::call() name='%1' is a builtin function.").arg(name) ); +#endif + return function->call(arguments); + } + + if(name.isNull()) { + // If no name is defined, we return a reference to our instance. + return this; + } + + // Redirect the call to the Kross::Api::Callable we are inherited from. + return Callable::call(name, arguments); + } + + }; + +}} + +#endif + diff --git a/lib/kross/api/eventaction.cpp b/lib/kross/api/eventaction.cpp new file mode 100644 index 00000000..fffaec5c --- /dev/null +++ b/lib/kross/api/eventaction.cpp @@ -0,0 +1,77 @@ +/*************************************************************************** + * eventaction.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "eventaction.h" +#include "variant.h" + +//#include <qobject.h> +//#include <kaction.h> + +using namespace Kross::Api; + +EventAction::EventAction(const QString& name, KAction* action) + : Event<EventAction>(name.isEmpty() ? action->name() : name) + , m_action(action) +{ + addFunction("getText", &EventAction::getText); + addFunction("setText", &EventAction::setText); + + addFunction("isEnabled", &EventAction::isEnabled); + addFunction("setEnabled", &EventAction::setEnabled); + + addFunction("activate", &EventAction::activate); +} + +EventAction::~EventAction() +{ +} + +const QString EventAction::getClassName() const +{ + return "Kross::Api::EventAction"; +} + +Object::Ptr EventAction::getText(List::Ptr) +{ + return new Variant(m_action->text()); +} + +Object::Ptr EventAction::setText(List::Ptr args) +{ + m_action->setText( Variant::toString(args->item(0)) ); + return 0; +} + +Object::Ptr EventAction::isEnabled(List::Ptr) +{ + return new Variant(m_action->isEnabled()); +} + +Object::Ptr EventAction::setEnabled(List::Ptr args) +{ + m_action->setEnabled( Variant::toBool(args->item(0)) ); + return 0; +} + +Object::Ptr EventAction::activate(List::Ptr) +{ + m_action->activate(); + return 0; +} + diff --git a/lib/kross/api/eventaction.h b/lib/kross/api/eventaction.h new file mode 100644 index 00000000..8f09c116 --- /dev/null +++ b/lib/kross/api/eventaction.h @@ -0,0 +1,86 @@ +/*************************************************************************** + * eventaction.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_EVENTACTION_H +#define KROSS_API_EVENTACTION_H + +#include <qstring.h> +//#include <qobject.h> +#include <kaction.h> +#include <ksharedptr.h> + +#include "event.h" + +namespace Kross { namespace Api { + + // Forward declarations. + class ScriptContainer; + + /** + * The EventAction class is used to wrap KAction instances + * into the Kross object hierachy and provide access to + * them. + */ + class EventAction : public Event<EventAction> + { + + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<EventAction> Ptr; + + /** + * Constructor. + */ + EventAction(const QString& name, KAction* action); + + /** + * Destructor. + */ + virtual ~EventAction(); + + /// \see Kross::Api::Object::getClassName() + virtual const QString getClassName() const; + + /// \see Kross::Api::Event::call() + //virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments); + + private: + KAction* m_action; + + /// \return the text associated with this action. + Kross::Api::Object::Ptr getText(Kross::Api::List::Ptr); + /// Sets the text associated with this action. + Kross::Api::Object::Ptr setText(Kross::Api::List::Ptr); + + /// \return true if this action is enabled else false. + Kross::Api::Object::Ptr isEnabled(Kross::Api::List::Ptr); + /// Enables or disables this action. + Kross::Api::Object::Ptr setEnabled(Kross::Api::List::Ptr); + + /// Activates the action. + Kross::Api::Object::Ptr activate(Kross::Api::List::Ptr); + }; + +}} + +#endif + diff --git a/lib/kross/api/eventscript.cpp b/lib/kross/api/eventscript.cpp new file mode 100644 index 00000000..39ceb59d --- /dev/null +++ b/lib/kross/api/eventscript.cpp @@ -0,0 +1,47 @@ +/*************************************************************************** + * eventscript.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "eventscript.h" +//#include "object.h" +//#include "variant.h" +//#include "../main/scriptcontainer.h" + +using namespace Kross::Api; + +EventScript::EventScript(const QString& name) + : Event<EventScript>(name) +{ +} + +EventScript::~EventScript() +{ +} + +const QString EventScript::getClassName() const +{ + return "Kross::Api::EventScript"; +} + +Object::Ptr EventScript::call(const QString& name, KSharedPtr<List> arguments) +{ + krossdebug( QString("EventScript::call() name=%1 arguments=%2").arg(name).arg(arguments->toString()) ); + //TODO + return 0; +} + diff --git a/lib/kross/api/eventscript.h b/lib/kross/api/eventscript.h new file mode 100644 index 00000000..8ef9b7eb --- /dev/null +++ b/lib/kross/api/eventscript.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * eventscript.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_EVENTSCRIPT_H +#define KROSS_API_EVENTSCRIPT_H + +#include <qstring.h> +#include <qobject.h> + +#include "event.h" + +namespace Kross { namespace Api { + + /** + * \todo implement EventScript ?! + */ + class EventScript : public Event<EventScript> + { + + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<EventScript> Ptr; + + /** + * Constructor. + */ + EventScript(const QString& name); + + /** + * Destructor. + */ + virtual ~EventScript(); + + virtual const QString getClassName() const; + + virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments); + + private: + //ScriptContainer* m_scriptcontainer; + //Callable* m_callable; + }; + +}} + +#endif + diff --git a/lib/kross/api/eventsignal.cpp b/lib/kross/api/eventsignal.cpp new file mode 100644 index 00000000..455ca61f --- /dev/null +++ b/lib/kross/api/eventsignal.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * eventsignal.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "eventsignal.h" + +#include "variant.h" +#include "qtobject.h" + +#include <qmetaobject.h> +#include <private/qucom_p.h> // for the Qt QUObject API. + +using namespace Kross::Api; + +EventSignal::EventSignal(const QString& name, QObject* sender, QCString signal) + : Event<EventSignal>(name) + , m_sender(sender) + , m_signal(signal) //QObject::normalizeSignalSlot(signal) +{ +} + +EventSignal::~EventSignal() +{ +} + +const QString EventSignal::getClassName() const +{ + return "Kross::Api::EventSignal"; +} + +Object::Ptr EventSignal::call(const QString& /*name*/, KSharedPtr<List> arguments) +{ +#ifdef KROSS_API_EVENTSIGNAL_CALL_DEBUG + krossdebug( QString("EventSignal::call() m_signal=%1 arguments=%2").arg(m_signal).arg(arguments->toString()) ); +#endif + + QString n = m_signal; + + if(n.startsWith("2")) // Remove prefix of SIGNAL-macros + n.remove(0,1); + + int signalid = m_sender->metaObject()->findSignal(n.latin1(), false); + if(signalid < 0) + throw new Exception(QString("No such signal '%1'.").arg(n)); + + QUObject* uo = QtObject::toQUObject(n, arguments); + m_sender->qt_emit(signalid, uo); // emit the signal + delete [] uo; + + return new Variant( QVariant(true,0) ); +} diff --git a/lib/kross/api/eventsignal.h b/lib/kross/api/eventsignal.h new file mode 100644 index 00000000..aea58b12 --- /dev/null +++ b/lib/kross/api/eventsignal.h @@ -0,0 +1,79 @@ +/*************************************************************************** + * eventsignal.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_EVENTSIGNAL_H +#define KROSS_API_EVENTSIGNAL_H + +//#include <qstring.h> +//#include <qvaluelist.h> +//#include <qmap.h> +//#include <qvariant.h> +//#include <qsignalmapper.h> +//#include <qguardedptr.h> +#include <qobject.h> +#include <ksharedptr.h> + +#include "event.h" + +namespace Kross { namespace Api { + + /** + * Each Qt signal and slot connection between a QObject + * instance and a functionname is represented with + * a EventSignal and handled by \a EventManager. + */ + class EventSignal : public Event<EventSignal> + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<EventSignal> Ptr; + + /** + * Constructor. + */ + EventSignal(const QString& name, QObject* sender, QCString signal); + + /** + * Destructor. + */ + virtual ~EventSignal(); + + virtual const QString getClassName() const; + + virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments); + +/* + signals: + void callback(); + void callback(const QString&); + void callback(int); + void callback(bool); +*/ + private: + QObject* m_sender; + QCString m_signal; + }; + +}} + +#endif + diff --git a/lib/kross/api/eventslot.cpp b/lib/kross/api/eventslot.cpp new file mode 100644 index 00000000..df70c9c8 --- /dev/null +++ b/lib/kross/api/eventslot.cpp @@ -0,0 +1,222 @@ +/*************************************************************************** + * eventslot.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "eventslot.h" + +#include "variant.h" +#include "qtobject.h" + +#include <qmetaobject.h> +#include <private/qucom_p.h> // for the Qt QUObject API. + +using namespace Kross::Api; + +EventSlot::EventSlot(const QString& name, QObject* receiver, QCString slot) + : Event<EventSlot>(name) + , m_receiver(receiver) + , m_slot(slot) //QObject::normalizeSignalSlot(slot) +{ +} + +EventSlot::~EventSlot() +{ +} + +const QString EventSlot::getClassName() const +{ + return "Kross::Api::EventSlot"; +} + +Object::Ptr EventSlot::call(const QString& /*name*/, List::Ptr arguments) +{ +#ifdef KROSS_API_EVENTSLOT_CALL_DEBUG + krossdebug( QString("EventSlot::call() m_slot=%1 arguments=%2").arg(m_slot).arg(arguments->toString()) ); +#endif + + QString n = m_slot; //TODO name; //Variant::toString(args->item(0)); + + if(n.startsWith("1")) // Remove prefix of SLOT-macros + n.remove(0,1); + + int slotid = m_receiver->metaObject()->findSlot(n.latin1(), false); + if(slotid < 0) + throw Exception::Ptr( new Exception(QString("No such slot '%1'.").arg(n)) ); + + QUObject* uo = QtObject::toQUObject(n, arguments); + m_receiver->qt_invoke(slotid, uo); // invoke the slot + delete [] uo; + + return new Variant( QVariant(true,0) ); +} + +/* +QCString EventSlot::getSlot(const QCString& signal) +{ + QString signature = QString(signal).mid(1); + int startpos = signature.find("("); + int endpos = signature.findRev(")"); + if(startpos < 0 || startpos > endpos) { + krosswarning( QString("EventSlot::getSlot(%1) Invalid signal.").arg(signal) ); + return QCString(); + } + QString signalname = signature.left(startpos); + QString params = signature.mid(startpos + 1, endpos - startpos - 1); + //QStringList paramlist = QStringList::split(",", params); + QCString slot = QString("callback(" + params + ")").latin1(); //normalizeSignalSlot(); + + QMetaObject* mo = metaObject(); + int slotid = mo->findSlot(slot, false); + if(slotid < 0) { + krossdebug( QString("EventSlot::getSlot(%1) No such slot '%2' avaiable.").arg(signal).arg(slot) ); + return QCString(); + } + + const QMetaData* md = mo->slot(slotid, false); + if(md->access != QMetaData::Public) { + krossdebug( QString("EventSlot::getSlot(%1) The slot '%2' is not public.").arg(signal).arg(slot) ); + return QCString(); + } + +//QMember* member = md->member; +//const QUMethod *method = md->method; + + krossdebug( QString("signal=%1 slot=%2 slotid=%3 params=%4 mdname=%5") + .arg(signal).arg(slot).arg(slotid).arg(params).arg(md->name) ); + return QCString("1" + slot); // Emulate the SLOT(...) macro by adding as first char a "1". +} + +bool EventSlot::connect(EventManager* eventmanager, QObject* senderobj, const QCString& signal, QString function, const QCString& slot) +{ + if(m_sender && ! disconnect()) + return false; + + const QCString& myslot = slot.isEmpty() ? getSlot(signal) : slot; + if(! myslot) + return false; + + if(! m_eventmanager) { + EventSlot* eventslot = create(eventmanager); + eventslot->connect(eventmanager, senderobj, signal, function, slot); + m_slots.append(eventslot); + krossdebug( QString("EventSlot::connect(%1, %2, %3) added child EventSlot !!!").arg(senderobj->name()).arg(signal).arg(function) ); + } + else { + m_sender = senderobj; + m_signal = signal; + m_function = function; + m_slot = myslot; + if(! QObject::connect((QObject*)senderobj, signal, this, myslot)) { + krossdebug( QString("EventSlot::connect(%1, %2, %3) failed.").arg(senderobj->name()).arg(signal).arg(function) ); + return false; + } + krossdebug( QString("EventSlot::connect(%1, %2, %3) successfully connected.").arg(senderobj->name()).arg(signal).arg(function) ); + } + return true; +} + +bool EventSlot::disconnect() +{ + if(! m_sender) return false; + QObject::disconnect((QObject*)m_sender, m_signal, this, m_slot); + m_sender = 0; + m_signal = 0; + m_slot = 0; + m_function = QString::null; + return true; +} + +void EventSlot::call(const QVariant& variant) +{ + krossdebug( QString("EventSlot::call() sender='%1' signal='%2' function='%3'") + .arg(m_sender->name()).arg(m_signal).arg(m_function) ); + + Kross::Api::List* arglist = 0; + + QValueList<Kross::Api::Object*> args; + if(variant.isValid()) { + args.append(Kross::Api::Variant::create(variant)); + arglist = Kross::Api::List::create(args); + } + + try { + m_eventmanager->m_scriptcontainer->callFunction(m_function, arglist); + } + catch(Exception& e) { + //TODO add hadError(), getError() and setError() + krossdebug( QString("EXCEPTION in EventSlot::call('%1') type='%2' description='%3'").arg(variant.toString()).arg(e.type()).arg(e.description()) ); + } +} + +void EventSlot::callback() { + call(QVariant()); } +void EventSlot::callback(short s) { + call(QVariant(s)); } +void EventSlot::callback(int i) { + call(QVariant(i)); } +void EventSlot::callback(int i1, int i2) { + call(QVariant( QValueList<QVariant>() << i1 << i2 )); } +void EventSlot::callback(int i1, int i2, int i3) { + call(QVariant( QValueList<QVariant>() << i1 << i2 << i3 )); } +void EventSlot::callback(int i1, int i2, int i3, int i4) { + call(QVariant( QValueList<QVariant>() << i1 << i2 << i3 << i4 )); } +void EventSlot::callback(int i1, int i2, int i3, int i4, int i5) { + call(QVariant( QValueList<QVariant>() << i1 << i2 << i3 << i4 << i5 )); } +void EventSlot::callback(int i1, int i2, int i3, int i4, bool b) { + call(QVariant( QValueList<QVariant>() << i1 << i2 << i3 << i4 << b )); } +void EventSlot::callback(int i1, bool b) { + call(QVariant( QValueList<QVariant>() << i1 << b )); } +void EventSlot::callback(int i1, int i2, bool b) { + call(QVariant( QValueList<QVariant>() << i1 << i2 << b )); } +void EventSlot::callback(int i1, int i2, const QString& s) { + call(QVariant( QValueList<QVariant>() << i1 << i2 << s )); } +void EventSlot::callback(uint i) { + call(QVariant(i)); } +void EventSlot::callback(long l) { + call(QVariant((Q_LLONG)l)); } +void EventSlot::callback(ulong l) { + call(QVariant((Q_ULLONG)l)); } +void EventSlot::callback(double d) { + call(QVariant(d)); } +void EventSlot::callback(const char* c) { + call(QVariant(c)); } +void EventSlot::callback(bool b) { + call(QVariant(b)); } +void EventSlot::callback(const QString& s) { + call(QVariant(s)); } +void EventSlot::callback(const QString& s, int i) { + call(QVariant( QValueList<QVariant>() << s << i )); } +void EventSlot::callback(const QString& s, int i1, int i2) { + call(QVariant( QValueList<QVariant>() << s << i1 << i2 )); } +void EventSlot::callback(const QString& s, uint i) { + call(QVariant( QValueList<QVariant>() << s << i )); } +void EventSlot::callback(const QString& s, bool b) { + call(QVariant( QValueList<QVariant>() << s << b )); } +void EventSlot::callback(const QString& s, bool b1, bool b2) { + call(QVariant( QValueList<QVariant>() << s << b1 << b2 )); } +void EventSlot::callback(const QString& s, bool b, int i) { + call(QVariant( QValueList<QVariant>() << s << b << i )); } +void EventSlot::callback(const QString& s1, const QString& s2) { + call(QVariant( QValueList<QVariant>() << s1 << s2 )); } +void EventSlot::callback(const QString& s1, const QString& s2, const QString& s3) { + call(QVariant( QValueList<QVariant>() << s1 << s2 << s3 )); } +void EventSlot::callback(const QStringList& sl) { + call(QVariant(sl)); } +void EventSlot::callback(const QVariant& variant) { + call(variant); } +*/ diff --git a/lib/kross/api/eventslot.h b/lib/kross/api/eventslot.h new file mode 100644 index 00000000..8f564103 --- /dev/null +++ b/lib/kross/api/eventslot.h @@ -0,0 +1,123 @@ +/*************************************************************************** + * eventslot.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_EVENTSLOT_H +#define KROSS_API_EVENTSLOT_H + +#include <qstring.h> +#include <qobject.h> +#include <ksharedptr.h> + +#include "event.h" + +namespace Kross { namespace Api { + + /** + * Each Qt signal and slot connection between a QObject + * instance and a functionname is represented with + * a EventSlot and handled by the \a EventManager. + */ + class EventSlot : public Event<EventSlot> + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<EventSlot> Ptr; + + /** + * Constructor. + * + * \param name The name of the EventSlot. The EventSlot + * will be accessible by that unique name via + * it's parent. + * \param receiver The receiver of the event. + * \param slot The slot of the receiver which this + * EventSlot points to. + */ + EventSlot(const QString& name, QObject* receiver, QCString slot); + + /** + * Destructor. + */ + virtual ~EventSlot(); + + /// \see Kross::Api::Object::getClassName() + virtual const QString getClassName() const; + + /// \see Kross::Api::Event::call() + virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments); + +/* + private: + EventManager* m_eventmanager; + QGuardedPtr<QObject> m_sender; + QCString m_signal; + QCString m_slot; + QString m_function; + QValueList<EventSlot*> m_slots; + protected: + void call(const QVariant&); + public slots: + // Stupid signals and slots. To get the passed + // arguments we need to have all cases of slots + // avaiable for EventManager::connect() signals. + void callback(); + void callback(short); + void callback(int); + void callback(int, int); + void callback(int, int, int); + void callback(int, int, int, int); + void callback(int, int, int, int, int); + void callback(int, int, int, int, bool); + void callback(int, bool); + void callback(int, int, bool); + void callback(int, int, const QString&); + void callback(uint); + void callback(long); + void callback(ulong); + void callback(double); + void callback(const char*); + void callback(bool); + void callback(const QString&); + void callback(const QString&, int); + void callback(const QString&, int, int); + void callback(const QString&, uint); + void callback(const QString&, bool); + void callback(const QString&, bool, bool); + void callback(const QString&, bool, int); + void callback(const QString&, const QString&); + void callback(const QString&, const QString&, const QString&); + void callback(const QStringList&); + void callback(const QVariant&); + // The following both slots are more generic to + // handle Kross::Api::Object instances. + //void callback(Kross::Api::Object*); + //void callback(Kross::Api::List*); +*/ + private: + QObject* m_receiver; + QCString m_slot; + }; + +}} + +#endif + diff --git a/lib/kross/api/exception.cpp b/lib/kross/api/exception.cpp new file mode 100644 index 00000000..a54bae88 --- /dev/null +++ b/lib/kross/api/exception.cpp @@ -0,0 +1,71 @@ +/*************************************************************************** + * exception.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "object.h" +#include "exception.h" + +//#include <qstring.h> +//#include <ksharedptr.h> + +using namespace Kross::Api; + +Exception::Exception(const QString& error, long lineno) + : Object() + , m_error(error) + , m_lineno(lineno) +{ + krosswarning( QString("Kross::Api::Exception error='%1' lineno='%3'").arg(m_error).arg(m_lineno) ); +} + +Exception::~Exception() +{ +} + +const QString Exception::getClassName() const +{ + return "Kross::Api::Exception"; +} + +const QString Exception::toString() +{ + return (m_lineno != -1) + ? QString("Exception at line %1: %2").arg(m_lineno).arg(m_error) + : QString("Exception: %1").arg(m_error); +} + +const QString Exception::getError() const +{ + return m_error; +} + +const QString Exception::getTrace() const +{ + return m_trace; +} + +void Exception::setTrace(const QString& tracemessage) +{ + m_trace = tracemessage; +} + +long Exception::getLineNo() const +{ + return m_lineno; +} + diff --git a/lib/kross/api/exception.h b/lib/kross/api/exception.h new file mode 100644 index 00000000..6ee9597b --- /dev/null +++ b/lib/kross/api/exception.h @@ -0,0 +1,102 @@ +/*************************************************************************** + * exception.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_EXCEPTION_H +#define KROSS_API_EXCEPTION_H + +#include "object.h" + +#include <qstring.h> +#include <ksharedptr.h> + +namespace Kross { namespace Api { + + /** + * Common exception class used for representing exceptions + * in Kross. + * + * Internal we use \a Exception instances to throw and handle + * exceptions. Those exceptions are inherited from \a Object + * and therefore they are first class citizens in Kross. + */ + class Exception : public Object + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<Exception> Ptr; + + /** + * Constructor. + * + * \param error The error message. + * \param lineno The liner number in the scripting + * code where this exception got thrown. + */ + Exception(const QString& error, long lineno = -1); + + /** + * Destructor. + */ + virtual ~Exception(); + + /// \see Kross::Api::Object::getClassName() + virtual const QString getClassName() const; + + /// \see Kross::Api::Object::toString() + virtual const QString toString(); + + /** + * \return the error message. + */ + const QString getError() const; + + /** + * \return a more detailed tracemessage or QString::null if + * there is no trace avaiable. + */ + const QString getTrace() const; + + /** + * Set a more detailed tracemessage. + */ + void setTrace(const QString& tracemessage); + + /** + * \return the line number in the scripting code + * where the exception got thrown or -1 if there + * was no line number defined. + */ + long getLineNo() const; + + private: + /// The error message. + QString m_error; + /// The trace message. + QString m_trace; + /// The line number where the exception got thrown + long m_lineno; + }; + +}} + +#endif + diff --git a/lib/kross/api/function.h b/lib/kross/api/function.h new file mode 100644 index 00000000..6b75752a --- /dev/null +++ b/lib/kross/api/function.h @@ -0,0 +1,132 @@ +/*************************************************************************** + * function.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_FUNCTION_H +#define KROSS_API_FUNCTION_H + +#include "../main/krossconfig.h" +#include "object.h" +#include "list.h" + +#include <qstring.h> + +namespace Kross { namespace Api { + + /** + * The base class for functions. Classes like \a Function0 and + * \a ProxyFunction inheritate this class. + */ + class Function + { + public: + + /** + * Each function needs to implement the call-method which will + * be executed if the function itself should be executed. + */ + virtual Object::Ptr call(List::Ptr) = 0; + + }; + + /** + * This class implements the most abstract way to work with functions. It + * implements pointing to functions of the form + * @code + * Kross::Api::Object::Ptr myfunc(Kross::Api::List::Ptr) + * @endcode + * where a low-level \a Object got returned that represents the returnvalue + * of the function-call, and a \a List instance is passed that may contain + * optional \a Object instances as parameters. + */ + template<class INSTANCE> + class Function0 : public Function + { + private: + typedef Object::Ptr(INSTANCE::*Method)(List::Ptr); + INSTANCE* m_instance; + Method m_method; + public: + Function0(INSTANCE* instance, Method method) + : m_instance(instance), m_method(method) {} + Object::Ptr call(List::Ptr args) + { return (m_instance->*m_method)(args); } + }; + + /** + * Specialization of the \a Function0 which takes as additional parameter + * a const-value. This const-value will be hidden for the scripting backend + * and is only passed through on function-call. + * + * So, this class could be as example used to point to a function like; + * @code + * Kross::Api::Object::Ptr myfunc(Kross::Api::List::Ptr, int myinteger) + * @endcode + * and then we are able to point to the function with something like + * @code + * this->addFunction("myfunctionname", + * new Kross::Api::Function1< MYCLASS, int >( + * this, // pointer to an instance of MYCLASS + * &MYCLASS::myfunction, // the method which should be wrapped + * 17 // the const-value we like to pass to the function. + * ) ); + * @endcode + * The defined integer myinteger which has the value 17 will be passed + * transparently to myfunc. The scripting-backend won't know that there + * exists such an additional integer at all. So, it's hidden and the user + * aka the scripting code won't be able to manipulate that additional + * value. + */ + template<class INSTANCE, typename P1> + class Function1 : public Function + { + private: + typedef Object::Ptr(INSTANCE::*Method)(List::Ptr, P1); + INSTANCE* m_instance; + Method m_method; + P1 m_p1; + public: + Function1(INSTANCE* instance, Method method, P1 p1) + : m_instance(instance), m_method(method), m_p1(p1) {} + Object::Ptr call(List::Ptr args) + { return (m_instance->*m_method)(args, m_p1); } + }; + + /** + * Same as \a Function1 but with 2 additional parameters. + */ + template<class INSTANCE, typename P1, typename P2> + class Function2 : public Function + { + private: + typedef Object::Ptr(INSTANCE::*Method)(List::Ptr, P1, P2); + INSTANCE* m_instance; + Method m_method; + P1 m_p1; + P2 m_p2; + public: + Function2(INSTANCE* instance, Method method, P1 p1, P2 p2) + : m_instance(instance), m_method(method), m_p1(p1), m_p2(p2) {} + Object::Ptr call(List::Ptr args) + { return (m_instance->*m_method)(args, m_p1, m_p2); } + }; + +}} + +#endif + diff --git a/lib/kross/api/interpreter.cpp b/lib/kross/api/interpreter.cpp new file mode 100644 index 00000000..439063cd --- /dev/null +++ b/lib/kross/api/interpreter.cpp @@ -0,0 +1,151 @@ +/*************************************************************************** + * interpreter.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "interpreter.h" +#include "script.h" +#include "../main/manager.h" +#include "../main/scriptcontainer.h" + +#include <klibloader.h> + +extern "C" +{ + typedef int (*def_interpreter_func)(Kross::Api::InterpreterInfo*); +} + +using namespace Kross::Api; + +/************************************************************************* + * InterpreterInfo + */ + +InterpreterInfo::InterpreterInfo(const QString& interpretername, const QString& library, const QString& wildcard, QStringList mimetypes, Option::Map options) + : m_interpretername(interpretername) + , m_library(library) + , m_wildcard(wildcard) + , m_mimetypes(mimetypes) + , m_options(options) + , m_interpreter(0) +{ +} + +InterpreterInfo::~InterpreterInfo() +{ + for(Option::Map::Iterator it = m_options.begin(); it != m_options.end(); ++it) + delete it.data(); + + delete m_interpreter; + m_interpreter = 0; +} + +const QString InterpreterInfo::getInterpretername() +{ + return m_interpretername; +} + +const QString InterpreterInfo::getWildcard() +{ + return m_wildcard; +} + +const QStringList InterpreterInfo::getMimeTypes() +{ + return m_mimetypes; +} + +bool InterpreterInfo::hasOption(const QString& key) +{ + return m_options.contains(key); +} + +InterpreterInfo::Option* InterpreterInfo::getOption(const QString name) +{ + return m_options[name]; +} + +const QVariant InterpreterInfo::getOptionValue(const QString name, QVariant defaultvalue) +{ + Option* o = m_options[name]; + return o ? o->value : defaultvalue; +} + +InterpreterInfo::Option::Map InterpreterInfo::getOptions() +{ + return m_options; +} + +Interpreter* InterpreterInfo::getInterpreter() +{ + if(m_interpreter) // buffered + return m_interpreter; + + krossdebug( QString("Loading the interpreter library for %1").arg(m_interpretername) ); + + // Load the krosspython library. + KLibLoader *libloader = KLibLoader::self(); + + KLibrary* library = libloader->globalLibrary( m_library.latin1() ); + if(! library) { + /* + setException( + new Exception( QString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(m_library).arg(m_interpretername) ) + ); + */ + krosswarning( QString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(m_library).arg(m_interpretername) ); + return 0; + } + + // Get the extern "C" krosspython_instance function. + def_interpreter_func interpreter_func; + interpreter_func = (def_interpreter_func) library->symbol("krossinterpreter"); + if(! interpreter_func) { + //setException( new Exception("Failed to load symbol in krosspython library.") ); + krosswarning("Failed to load the 'krossinterpreter' symbol from the library."); + } + else { + // and execute the extern krosspython_instance function. + m_interpreter = (Interpreter*) (interpreter_func)(this); + if(! m_interpreter) { + krosswarning("Failed to load the Interpreter instance from library."); + } + else { + // Job done. The library is loaded and our Interpreter* points + // to the external Kross::Python::Interpreter* instance. + krossdebug("Successfully loaded Interpreter instance from library."); + } + } + + // finally unload the library. + library->unload(); + + return m_interpreter; +} + +/************************************************************************* + * Interpreter + */ + +Interpreter::Interpreter(InterpreterInfo* info) + : m_interpreterinfo(info) +{ +} + +Interpreter::~Interpreter() +{ +} diff --git a/lib/kross/api/interpreter.h b/lib/kross/api/interpreter.h new file mode 100644 index 00000000..5c73c303 --- /dev/null +++ b/lib/kross/api/interpreter.h @@ -0,0 +1,197 @@ +/*************************************************************************** + * interpreter.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_INTERPRETER_H +#define KROSS_API_INTERPRETER_H + +#include <qstring.h> +#include <qmap.h> + +#include "object.h" + +namespace Kross { namespace Api { + + // Forward declaration. + class Manager; + class ScriptContainer; + class Script; + class Interpreter; + + /** + * While the \a Interpreter is the implemented interpreter this class + * is used to provide some abstract informations about each interpreter + * we are able to use within the \a Manager singelton. + */ + class InterpreterInfo + { + public: + + /** + * Each interpreter is able to define options we could + * use to manipulate the interpreter behaviour. + */ + class Option + { + public: + + /** + * Map of options. + */ + typedef QMap<QString, Option*> Map; + + /** + * Constructor. + * + * \param name The name the option has. This is the + * displayed title and isn't used internaly. + * \param comment A comment that describes the option. + * \param value The QVariant value this option has. + */ + Option(const QString& name, const QString& comment, const QVariant& value) + : name(name), comment(comment), value(value) {} + + /// The short name of the option. + QString name; + + /// A description of the option. + QString comment; + + /// The value the option has. + QVariant value; + }; + + /** + * Constructor. + */ + InterpreterInfo(const QString& interpretername, const QString& library, const QString& wildcard, QStringList mimetypes, Option::Map options); + + /** + * Destructor. + */ + ~InterpreterInfo(); + + /** + * \return the name of the interpreter. For example "python" or "kjs". + */ + const QString getInterpretername(); + + /** + * \return the file-wildcard used to determinate by this interpreter + * used scriptingfiles. Those filter will be used e.g. with + * KGlobal::dirs()->findAllResources() as filtermask. For example + * python just defines it as "*py". + */ + const QString getWildcard(); + + /** + * List of mimetypes this interpreter supports. + * + * \return QStringList with mimetypes like + * "application/x-javascript". + */ + const QStringList getMimeTypes(); + + /** + * \return true if an \a Option with that \p key exists else false. + */ + bool hasOption(const QString& key); + + /** + * \return the option defined with \p name . + */ + Option* getOption(const QString name); + + /** + * \return the value of the option defined with \p name . If there + * doesn't exists an option with such a name, the \p defaultvalue + * is returned. + */ + const QVariant getOptionValue(const QString name, QVariant defaultvalue = QVariant()); + + /** + * \return a map of options. + */ + Option::Map getOptions(); + + /** + * \return the \a Interpreter instance this \a InterpreterInfo + * is the describer for. + */ + Interpreter* getInterpreter(); + + private: + /// The name the interpreter has. Could be something like "python" or "kjs". + QString m_interpretername; + /// The name of the library to load for the interpreter. + QString m_library; + /// The file wildcard used to determinate extensions. + QString m_wildcard; + /// List of mimetypes this interpreter supports. + QStringList m_mimetypes; + /// A \a Option::Map with options. + Option::Map m_options; + /// The \a Interpreter instance. + Interpreter* m_interpreter; + }; + + /** + * Base class for interpreters. + * + * Each scripting backend needs to inheritate it's own + * interpreter from this class and implementate there + * backend related stuff. + * The Interpreter will be managed by the \a Kross::Manager + * class. + */ + class Interpreter + { + public: + + /** + * Constructor. + * + * \param info is the \a InterpreterInfo instance + * that describes this interpreter. + */ + Interpreter(InterpreterInfo* info); + + /** + * Destructor. + */ + virtual ~Interpreter(); + + /** + * Create and return a new interpreter dependend + * \a Script instance. + * + * \param scriptcontainer The \a ScriptContainer + * to use for the \a Script instance. + * \return The from \a Script inherited instance. + */ + virtual Script* createScript(ScriptContainer* scriptcontainer) = 0; + + protected: + /// The \a InterpreterInfo instance this interpreter belongs to. + InterpreterInfo* m_interpreterinfo; + }; + +}} + +#endif + diff --git a/lib/kross/api/list.cpp b/lib/kross/api/list.cpp new file mode 100644 index 00000000..ad901e5e --- /dev/null +++ b/lib/kross/api/list.cpp @@ -0,0 +1,69 @@ +/*************************************************************************** + * list.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "list.h" +#include "exception.h" + +using namespace Kross::Api; + +List::List(QValueList<Object::Ptr> value) + : Value< List, QValueList<Object::Ptr> >(value) +{ +} + +List::~List() +{ +} + +const QString List::getClassName() const +{ + return "Kross::Api::List"; +} + +const QString List::toString() +{ + QString s = "["; + QValueList<Object::Ptr> list = getValue(); + for(QValueList<Object::Ptr>::Iterator it = list.begin(); it != list.end(); ++it) + s += "'" + (*it)->toString() + "', "; + return (s.endsWith(", ") ? s.left(s.length() - 2) : s) + "]"; +} + +Object::Ptr List::item(uint idx, Object* defaultobject) +{ + QValueList<Object::Ptr>& list = getValue(); + if(idx >= list.count()) { + if(defaultobject) + return defaultobject; + krossdebug( QString("List::item index=%1 is out of bounds. Raising TypeException.").arg(idx) ); + throw Exception::Ptr( new Exception(QString("List-index %1 out of bounds.").arg(idx)) ); + } + return list[idx]; +} + +uint List::count() +{ + return getValue().count(); +} + +void List::append(Object::Ptr object) +{ + getValue().append(object); +} + diff --git a/lib/kross/api/list.h b/lib/kross/api/list.h new file mode 100644 index 00000000..d74246a0 --- /dev/null +++ b/lib/kross/api/list.h @@ -0,0 +1,167 @@ +/*************************************************************************** + * list.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_LIST_H +#define KROSS_API_LIST_H + +#include <qstring.h> +#include <qvaluelist.h> +#include <qintdict.h> + +#include "object.h" +#include "value.h" + +namespace Kross { namespace Api { + + /** + * The List class implementates \a Value to handle + * lists and collections. + */ + class List : public Value< List, QValueList<Object::Ptr> > + { + friend class Value< List, QValueList<Object::Ptr> >; + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<List> Ptr; + +/* + operator QStringList () { + //QValueList<Object::Ptr> getValue() + krossdebug("999999999999 ..........................."); + return QStringList(); + } +*/ + + /** + * Constructor. + * + * \param value The list of \a Object instances this + * list has initialy. + */ + List(QValueList<Object::Ptr> value = QValueList<Object::Ptr>()); + + /** + * Destructor. + */ + virtual ~List(); + + /** + * See \see Kross::Api::Object::getClassName() + */ + virtual const QString getClassName() const; + + /** + * \return a string representation of the whole list. + * + * \see Kross::Api::Object::toString() + */ + virtual const QString toString(); + + /** + * Return the \a Object with defined index from the + * QValueList this list holds. + * + * \throw TypeException If index is out of bounds. + * \param idx The QValueList-index. + * \param defaultobject The default \a Object which should + * be used if there exists no item with such an + * index. This \a Object instance will be returned + * if not NULL and if the index is out of bounds. If + * its NULL a \a TypeException will be thrown. + * \return The \a Object instance. + */ + Object::Ptr item(uint idx, Object* defaultobject = 0); + + /** + * Return the number of items in the QValueList this + * list holds. + * + * \return The number of items. + */ + uint count(); + + /** + * Append an \a Kross::Api::Object to the list. + * + * \param object The \a Kross::Api::Object instance to + * append to this list. + */ + void append(Object::Ptr object); + + template<typename TYPE> + static Object::Ptr toObject(TYPE t) { return t; } + }; + + /** + * This template class extends the \a List class with + * generic functionality to deal with lists. + */ + template< class OBJECT > + class ListT : public List + { + public: + ListT() : List() {} + ListT(QValueList<OBJECT> values) : List(values) {} + + template< typename TYPE > + ListT(QValueList<TYPE> values) : List() + { + QValueListIterator<TYPE> it(values.begin()), end(values.end()); + for(; it != end; ++it) + this->append( new OBJECT(*it) ); + } + + template< typename TYPE > + ListT(QIntDict<TYPE> values) : List() + { + QIntDictIterator<TYPE> it( values ); + TYPE *t; + while( (t = it.current()) != 0 ) { + this->append( new OBJECT(t) ); + ++it; + } + } + + template< typename TYPE > + ListT(const QPtrList<TYPE> values) : List() + { + QPtrListIterator<TYPE> it(values); + TYPE *t; + while( (t = it.current()) != 0 ) { + this->append( new OBJECT(t) ); + ++it; + } + } + + virtual ~ListT() {} + + template<typename TYPE> + static Object::Ptr toObject(TYPE t) + { + return new ListT(t); + } + }; + +}} + +#endif + diff --git a/lib/kross/api/module.h b/lib/kross/api/module.h new file mode 100644 index 00000000..127609e0 --- /dev/null +++ b/lib/kross/api/module.h @@ -0,0 +1,80 @@ +/*************************************************************************** + * module.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_MODULE_H +#define KROSS_API_MODULE_H + +#include <qstring.h> + +#include "class.h" + +namespace Kross { namespace Api { + + /** + * The Module class. Modules are managed in the \a Manager singleton + * instance and are implemented as in scripting plugins as main + * entry point to load and work with them. + */ + class Module : public Class<Module> + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<Module> Ptr; + + /** + * Constructor. + * + * \param name The name of this module. + * Each module needs a unique name cause + * the application using Kross identifies + * modules with there names. + */ + explicit Module(const QString& name) + : Class<Module>(name) + { + krossdebug( QString("Kross::Api::Module %1 created").arg(name) ); + } + + /** + * Destructor. + */ + virtual ~Module() + { + krossdebug( QString("Kross::Api::Module %1 destroyed").arg(getName()) ); + } + + /** + * Method to load from \a Kross::Api::Object inherited classes + * this module implements from within other modules. + */ + virtual Kross::Api::Object::Ptr get(const QString& /*name*/, void* /*pointer*/ = 0) + { + return 0; + } + + }; + + +}} + +#endif + diff --git a/lib/kross/api/object.cpp b/lib/kross/api/object.cpp new file mode 100644 index 00000000..ff2e2fbd --- /dev/null +++ b/lib/kross/api/object.cpp @@ -0,0 +1,63 @@ +/*************************************************************************** + * object.cpp + * This file is part of the KDE project + * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "object.h" +#include "list.h" +#include "variant.h" +//#include "function.h" +#include "event.h" +#include "exception.h" + +using namespace Kross::Api; + +Object::Object() + : KShared() +{ +#ifdef KROSS_API_OBJECT_CTOR_DEBUG + krossdebug( QString("Kross::Api::Object::Constructor() name='%1' refcount='%2'").arg(m_name).arg(_KShared_count()) ); +#endif +} + +Object::~Object() +{ +#ifdef KROSS_API_OBJECT_DTOR_DEBUG + krossdebug( QString("Kross::Api::Object::Destructor() name='%1' refcount='%2'").arg(m_name).arg(_KShared_count()) ); +#endif + //removeAllChildren(); // not needed cause we use KShared to handle ref-couting and freeing. +} + +const QString Object::toString() +{ + return QString("%1").arg(getClassName()); +} + +Object::Ptr Object::call(const QString& name, List::Ptr arguments) +{ + Q_UNUSED(arguments); + +#ifdef KROSS_API_OBJECT_CALL_DEBUG + krossdebug( QString("Kross::Api::Object::call(%1) name=%2 class=%3").arg(name).arg(getName()).arg(getClassName()) ); +#endif + + if(name.isEmpty()) // return a self-reference if no functionname is defined. + return this; + + throw Exception::Ptr( new Exception(QString("No callable object named '%2'").arg(name)) ); +} + diff --git a/lib/kross/api/object.h b/lib/kross/api/object.h new file mode 100644 index 00000000..3c86ca4b --- /dev/null +++ b/lib/kross/api/object.h @@ -0,0 +1,152 @@ +/*************************************************************************** + * object.h + * This file is part of the KDE project + * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_OBJECT_H +#define KROSS_API_OBJECT_H + +#include <qstring.h> +#include <qvaluelist.h> +#include <qmap.h> +#include <qvariant.h> +//#include <qobject.h> +#include <ksharedptr.h> + +#include "../main/krossconfig.h" + +namespace Kross { namespace Api { + + // Forward declaration. + class List; + + /** + * The common Object class all other object-classes are + * inheritated from. + * + * The Object class is used as base class to provide + * common functionality. It's similar to what we know + * in Python as PyObject or in Qt as QObject. + * + * Inherited from e.g. \a Value, \a Module and \a Class . + * + * This class implementates reference counting for shared + * objects. So, no need to take care of freeing objects. + */ + class Object : public KShared + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<Object> Ptr; + + /** + * Constructor. + */ + explicit Object(); + + /** + * Destructor. + */ + virtual ~Object(); + + /** + * Return the class name. This could be something + * like "Kross::Api::Object" for this object. The + * value is mainly used for display purposes. + * + * \return The name of this class. + */ + virtual const QString getClassName() const = 0; + + /** + * \return a string representation of the object or + * it's content. This method is mainly used for + * debugging and testing purposes. + */ + virtual const QString toString(); + + /** + * Pass a call to the object and evaluated it recursive + * down the object-hierachy. Objects like \a Class are able + * to handle call's by just implementing this function. + * If the call is done the \a called() method will be + * executed recursive from bottom up the call hierachy. + * + * \throws TypeException if the object or the name + * is not callable. + * \param name Each call has a name that says what + * should be called. In the case of a \a Class + * the name is the functionname. + * \param arguments The list of arguments passed to + * the call. + * \return The call-result as \a Object::Ptr instance or + * NULL if the call has no result. + */ + virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments); + + /** + * Return a list of supported callable objects. + * + * \return List of supported calls. + */ + virtual QStringList getCalls() { return QStringList(); } + //FIXME replace function with getChildren() functionality ? + + /** + * Try to convert the \a Object instance to the + * template class T. + * + * \throw TypeException if the cast failed. + * \param object The Object to cast. + * \return The to a instance from template type T + * casted Object. + */ + template<class T> static T* fromObject(Object::Ptr object); + + /** + * This method got used by the \a ProxyFunction classes + * to translate an unknown \p TYPE to a \a Object instance. + * Classes like \a Value or \a ListT or \a Class are + * overwriting this method to transparently translate these + * passed type while this method just assumes that the + * type is already a \a Object instance. + */ + template<typename TYPE> + static Object::Ptr toObject(TYPE t) { return t; } + }; + +}} + +#include "exception.h" + +namespace Kross { namespace Api { + +template<class T> inline T* Object::fromObject(Object::Ptr object) +{ + T* t = (T*) object.data(); + if(! t) + throw KSharedPtr<Exception>( new Exception(QString("Object \"%1\" invalid.").arg(object ? object->getClassName() : "")) ); + return t; +} + +}} + +#endif + diff --git a/lib/kross/api/proxy.h b/lib/kross/api/proxy.h new file mode 100644 index 00000000..f25a4845 --- /dev/null +++ b/lib/kross/api/proxy.h @@ -0,0 +1,342 @@ +/*************************************************************************** + * proxy.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_PROXY_H +#define KROSS_API_PROXY_H + +#include "../main/krossconfig.h" +#include "object.h" +#include "variant.h" +#include "list.h" + +#include <qstring.h> + +namespace Kross { namespace Api { + + /** + * \internal used struct to translate an argument-value dynamicly. + */ + template<class OBJ> + struct ProxyArgTranslator { + OBJ* m_object; + ProxyArgTranslator(Kross::Api::Object* obj) { + m_object = Kross::Api::Object::fromObject<OBJ>(obj); + } + template<typename T> + inline operator T () { + return m_object->operator T(); + } + }; + + /** + * \internal used struct to translate a return-value dynamicly. + */ + struct ProxyRetTranslator { + template<class RETURNOBJ, typename TYPE> + inline static Object::Ptr cast(TYPE t) { + return RETURNOBJ::toObject(t); + } + }; + + /** + * The ProxyFunction template-class is used to publish any C/C++ + * method (not only slots) of a struct or class instance as a + * a \a Function to Kross. + * + * With this class we don't need to have a method-wrapper for + * each single function what a) should reduce the code needed for + * wrappers and b) is more typesafe cause the connection to the + * C/C++ method is done on compiletime. + * + * Example how a ProxyFunction may got used; + * @code + * #include "../api/class.h" + * #include "../api/proxy.h" + * // The class which should be published. + * class MyClass : public Kross::Api::Class<MyClass> { + * public: + * MyClass(const QString& name) : Kross::Api::Class<MyClass>(name) { + * // publish the function myfunc, so that scripting-code is able + * // to call that method. + * this->addProxyFunction < + * Kross::Api::Variant, // the uint returnvalue is handled with Variant. + * Kross::Api::Variant, // the QString argument is handled with Variant too. + * MyClass // the MyClass* is handled implicit by our class. + * > ( "myfuncname", // the name the function should be published as. + * this, // pointer to the class-instance which has the method. + * &TestPluginObject::myfunc ); // pointer to the method itself. + * } + * virtual ~MyClass() {} + * virtual const QString getClassName() const { return "MyClass"; } + * private: + * uint myfunc(const QCString&, MyClass* myotherclass) { + * // This method will be published to the scripting backend. So, scripting + * // code is able to call this method. + * } + * } + * @endcode + */ + template< class INSTANCE, // the objectinstance + typename METHOD, // the method-signature + class RETURNOBJ,// = Kross::Api::Object, // return-value + class ARG1OBJ = Kross::Api::Object, // first parameter-value + class ARG2OBJ = Kross::Api::Object, // second parameter-value + class ARG3OBJ = Kross::Api::Object, // theird parameter-value + class ARG4OBJ = Kross::Api::Object // forth parameter-value + > + class ProxyFunction : public Function + { + template<class PROXYFUNC, typename RETURNTYPE> + friend struct ProxyFunctionCaller; + private: + /// Pointer to the objectinstance which method should be called. + INSTANCE* m_instance; + /// Pointer to the method which should be called. + const METHOD m_method; + + /// First default argument. + KSharedPtr<ARG1OBJ> m_defarg1; + /// Second default argument. + KSharedPtr<ARG2OBJ> m_defarg2; + /// Theird default argument. + KSharedPtr<ARG3OBJ> m_defarg3; + /// Forth default argument. + KSharedPtr<ARG4OBJ> m_defarg4; + + /** + * \internal used struct that does the execution of the wrapped + * method. + */ + template<class PROXYFUNC, typename RETURNTYPE> + struct ProxyFunctionCaller { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3, Kross::Api::Object* arg4) { + return ProxyRetTranslator::cast<RETURNTYPE>( + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3), ProxyArgTranslator<ARG4OBJ>(arg4) ) + ); + } + }; + + /** + * \internal template-specialization of the \a ProxyFunctionCaller + * above which handles void-returnvalues. We need to handle this + * special case seperatly cause compilers deny to return void :-/ + */ + template<class PROXYFUNC> + struct ProxyFunctionCaller<PROXYFUNC, void> { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3, Kross::Api::Object* arg4) { + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG1OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3), ProxyArgTranslator<ARG4OBJ>(arg4) ); + return 0; // void return-value + } + }; + + public: + + /** + * Constructor. + * + * \param instance The objectinstance to which the \p method + * belongs to. + * \param method The method-pointer. + */ + ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0, ARG3OBJ* defarg3 = 0, ARG4OBJ* defarg4 = 0) + : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2), m_defarg3(defarg3), m_defarg4(defarg4) {} + + /** + * This method got called if the wrapped method should be executed. + * + * \param args The optional list of arguments passed to the + * execution-call. + * \return The returnvalue of the functioncall. It will be NULL if + * the functioncall doesn't provide us a returnvalue (e.g. + * if the function has void as returnvalue). + */ + Object::Ptr call(List::Ptr args) { + return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this, + args->item(0, m_defarg1), + args->item(1, m_defarg2), + args->item(2, m_defarg3), + args->item(3, m_defarg4) + ); + } + }; + + /** + * Template-specialization of the \a ProxyFunction above with three arguments. + */ + template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ> + class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ> : public Function + { + template<class PROXYFUNC, typename RETURNTYPE> + friend struct ProxyFunctionCaller; + private: + INSTANCE* m_instance; + const METHOD m_method; + KSharedPtr<ARG1OBJ> m_defarg1; + KSharedPtr<ARG2OBJ> m_defarg2; + KSharedPtr<ARG3OBJ> m_defarg3; + + template<class PROXYFUNC, typename RETURNTYPE> + struct ProxyFunctionCaller { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3) { + return ProxyRetTranslator::cast<RETURNTYPE>( + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3) ) + ); + } + }; + + template<class PROXYFUNC> + struct ProxyFunctionCaller<PROXYFUNC, void> { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3) { + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3) ); + return 0; + } + }; + + public: + ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0, ARG3OBJ* defarg3 = 0) + : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2), m_defarg3(defarg3) {} + Object::Ptr call(List::Ptr args) { + return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this, + args->item(0, m_defarg1), args->item(1, m_defarg2), args->item(2, m_defarg3) + ); + } + }; + + /** + * Template-specialization of the \a ProxyFunction above with two arguments. + */ + template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ, class ARG2OBJ> + class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ> : public Function + { + template<class PROXYFUNC, typename RETURNTYPE> + friend struct ProxyFunctionCaller; + private: + INSTANCE* m_instance; + const METHOD m_method; + KSharedPtr<ARG1OBJ> m_defarg1; + KSharedPtr<ARG2OBJ> m_defarg2; + + template<class PROXYFUNC, typename RETURNTYPE> + struct ProxyFunctionCaller { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2) { + return ProxyRetTranslator::cast<RETURNTYPE>( + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2) ) + ); + } + }; + + template<class PROXYFUNC> + struct ProxyFunctionCaller<PROXYFUNC, void> { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2) { + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2) ); + return 0; + } + }; + + public: + ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0) + : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2) {} + Object::Ptr call(List::Ptr args) { + return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this, + args->item(0, m_defarg1), args->item(1, m_defarg2) + ); + } + }; + + /** + * Template-specialization of the \a ProxyFunction above with one argument. + */ + template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ> + class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ> : public Function + { + template<class PROXYFUNC, typename RETURNTYPE> + friend struct ProxyFunctionCaller; + private: + INSTANCE* m_instance; + const METHOD m_method; + KSharedPtr<ARG1OBJ> m_defarg1; + + template<class PROXYFUNC, typename RETURNTYPE> + struct ProxyFunctionCaller { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1) { + return ProxyRetTranslator::cast<RETURNTYPE>( + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1) ) + ); + } + }; + + template<class PROXYFUNC> + struct ProxyFunctionCaller<PROXYFUNC, void> { + inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1) { + ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1) ); + return 0; + } + }; + + public: + ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0) + : m_instance(instance), m_method(method), m_defarg1(defarg1) {} + Object::Ptr call(List::Ptr args) { + return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this, + args->item(0, m_defarg1) + ); + } + }; + + /** + * Template-specialization of the \a ProxyFunction above with no arguments. + */ + template<class INSTANCE, typename METHOD, class RETURNOBJ> + class ProxyFunction<INSTANCE, METHOD, RETURNOBJ> : public Function + { + template<class PROXYFUNC, typename RETURNTYPE> + friend struct ProxyFunctionCaller; + private: + INSTANCE* m_instance; + const METHOD m_method; + + template<class PROXYFUNC, typename RETURNTYPE> + struct ProxyFunctionCaller { + inline static Object::Ptr exec(PROXYFUNC* self) { + return ProxyRetTranslator::cast<RETURNTYPE>( + ( (self->m_instance)->*(self->m_method) )() + ); + } + }; + + template<class PROXYFUNC> + struct ProxyFunctionCaller<PROXYFUNC, void> { + inline static Object::Ptr exec(PROXYFUNC* self) { + ( (self->m_instance)->*(self->m_method) )(); + return 0; + } + }; + + public: + ProxyFunction(INSTANCE* instance, const METHOD& method) + : m_instance(instance), m_method(method) {} + Object::Ptr call(List::Ptr) { + return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this); + } + }; + +}} + +#endif + diff --git a/lib/kross/api/qtobject.cpp b/lib/kross/api/qtobject.cpp new file mode 100644 index 00000000..c6eb082a --- /dev/null +++ b/lib/kross/api/qtobject.cpp @@ -0,0 +1,235 @@ +/*************************************************************************** + * qtobject.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "qtobject.h" +#include "object.h" +#include "variant.h" +#include "event.h" + +#include "../main/manager.h" +#include "eventslot.h" +#include "eventsignal.h" + +#include <qobject.h> +#include <qsignal.h> +//#include <qglobal.h> +//#include <qobjectdefs.h> +#include <qmetaobject.h> +#include <private/qucom_p.h> // for the Qt QUObject API. + +using namespace Kross::Api; + +QtObject::QtObject(QObject* object, const QString& name) + : Kross::Api::Class<QtObject>(name.isEmpty() ? object->name() : name) + , m_object(object) +{ + // Walk through the signals and slots the QObject has + // and attach them as events to this QtObject. + + QStrList slotnames = m_object->metaObject()->slotNames(false); + for(char* c = slotnames.first(); c; c = slotnames.next()) { + QCString s = c; + addChild(s, new EventSlot(s, object, s) ); + } + + QStrList signalnames = m_object->metaObject()->signalNames(false); + for(char* c = signalnames.first(); c; c = signalnames.next()) { + QCString s = c; + addChild(s, new EventSignal(s, object, s) ); + } + + // Add functions to wrap QObject methods into callable + // Kross objects. + + addFunction("propertyNames", &QtObject::propertyNames); + addFunction("hasProperty", &QtObject::hasProperty); + addFunction("getProperty", &QtObject::getProperty); + addFunction("setProperty", &QtObject::setProperty); + + addFunction("slotNames", &QtObject::slotNames); + addFunction("hasSlot", &QtObject::hasSlot); + addFunction("slot", &QtObject::callSlot); + + addFunction("signalNames", &QtObject::signalNames); + addFunction("hasSignal", &QtObject::hasSignal); + addFunction("signal", &QtObject::emitSignal); + + addFunction("connect", &QtObject::connectSignal); + addFunction("disconnect", &QtObject::disconnectSignal); +} + +QtObject::~QtObject() +{ +} + +const QString QtObject::getClassName() const +{ + return "Kross::Api::QtObject"; +} + +QObject* QtObject::getObject() +{ + return m_object; +} + +QUObject* QtObject::toQUObject(const QString& signature, List::Ptr arguments) +{ + int startpos = signature.find("("); + int endpos = signature.findRev(")"); + if(startpos < 0 || startpos > endpos) + throw Exception::Ptr( new Exception(QString("Invalid Qt signal or slot signature '%1'").arg(signature)) ); + + //QString sig = signature.left(startpos); + QString params = signature.mid(startpos + 1, endpos - startpos - 1); + QStringList paramlist = QStringList::split(",", params); // this will fail on something like myslot(QMap<QString,QString> arg), but we don't care jet. + uint paramcount = paramlist.size(); + + // The first item in the QUObject-array is for the returnvalue + // while everything >=1 are the passed parameters. + QUObject* uo = new QUObject[ paramcount + 1 ]; + uo[0] = QUObject(); // empty placeholder for the returnvalue. + +//QString t; +//for(int j=0; j<argcount; j++) t += "'" + Variant::toString(arguments->item(j)) + "' "; +//krossdebug( QString("1 ---------------------: (%1) %2").arg(argcount).arg(t) ); + + // Fill parameters. + uint argcount = arguments ? arguments->count() : 0; + for(uint i = 0; i < paramcount; i++) { + if(paramlist[i].find("QString") >= 0) { + const QString s = (argcount > i) ? Variant::toString(arguments->item(i)) : QString::null; + //krossdebug(QString("EventSlot::toQUObject s=%1").arg(s)); + static_QUType_QString.set( &(uo[i + 1]), s ); + } + //TODO handle int, long, char*, QStringList, etc. + else { + throw Exception::Ptr( new Exception(QString("Unknown Qt signal or slot argument '%1' in signature '%2'.").arg(paramlist[i]).arg(signature)) ); + } + } + + return uo; +} + +Kross::Api::Object::Ptr QtObject::propertyNames(Kross::Api::List::Ptr) +{ + return new Kross::Api::Variant( + QStringList::fromStrList(m_object->metaObject()->propertyNames(false))); +} + +Kross::Api::Object::Ptr QtObject::hasProperty(Kross::Api::List::Ptr args) +{ + return new Kross::Api::Variant( + m_object->metaObject()->findProperty(Kross::Api::Variant::toString(args->item(0)).latin1(), false)); +} + +Kross::Api::Object::Ptr QtObject::getProperty(Kross::Api::List::Ptr args) +{ + QVariant variant = m_object->property(Kross::Api::Variant::toString(args->item(0)).latin1()); + if(variant.type() == QVariant::Invalid) + return 0; + return new Kross::Api::Variant(variant); +} + +Kross::Api::Object::Ptr QtObject::setProperty(Kross::Api::List::Ptr args) +{ + return new Kross::Api::Variant( + m_object->setProperty( + Kross::Api::Variant::toString(args->item(0)).latin1(), + Kross::Api::Variant::toVariant(args->item(1)) + )); +} + +Kross::Api::Object::Ptr QtObject::slotNames(Kross::Api::List::Ptr) +{ + return new Kross::Api::Variant( + QStringList::fromStrList(m_object->metaObject()->slotNames(false))); +} + +Kross::Api::Object::Ptr QtObject::hasSlot(Kross::Api::List::Ptr args) +{ + return new Kross::Api::Variant( + bool(m_object->metaObject()->slotNames(false).find( + Kross::Api::Variant::toString(args->item(0)).latin1() + ) != -1)); +} + +Kross::Api::Object::Ptr QtObject::callSlot(Kross::Api::List::Ptr args) +{ +//TODO just call the child event ?! + QString name = Kross::Api::Variant::toString(args->item(0)); + int slotid = m_object->metaObject()->findSlot(name.latin1(), false); + if(slotid < 0) + throw Exception::Ptr( new Exception(QString("No such slot '%1'.").arg(name)) ); + + QUObject* uo = QtObject::toQUObject(name, args); + m_object->qt_invoke(slotid, uo); + delete [] uo; + + return new Variant( QVariant(true,0) ); +} + +Kross::Api::Object::Ptr QtObject::signalNames(Kross::Api::List::Ptr) +{ + return new Kross::Api::Variant( + QStringList::fromStrList(m_object->metaObject()->signalNames(false))); +} + +Kross::Api::Object::Ptr QtObject::hasSignal(Kross::Api::List::Ptr args) +{ + return new Kross::Api::Variant( + bool(m_object->metaObject()->signalNames(false).find( + Kross::Api::Variant::toString(args->item(0)).latin1() + ) != -1)); +} + +Kross::Api::Object::Ptr QtObject::emitSignal(Kross::Api::List::Ptr args) +{ + QString name = Kross::Api::Variant::toString(args->item(0)); + int signalid = m_object->metaObject()->findSignal(name.latin1(), false); + if(signalid < 0) + throw Exception::Ptr( new Exception(QString("No such signal '%1'.").arg(name)) ); + m_object->qt_invoke(signalid, 0); //TODO convert Kross::Api::List::Ptr => QUObject* + return 0; +} + +Kross::Api::Object::Ptr QtObject::connectSignal(Kross::Api::List::Ptr args) +{ + QString signalname = Kross::Api::Variant::toString(args->item(0)); + QString signalsignatur = QString("2%1").arg(signalname); + const char* signalsig = signalsignatur.latin1(); + + QtObject* obj = Kross::Api::Object::fromObject<Kross::Api::QtObject>(args->item(1)); + QObject* o = obj->getObject(); + if(! o) + throw Exception::Ptr( new Exception(QString("No such QObject receiver in '%1'.").arg(obj->getName())) ); + + QString slotname = Kross::Api::Variant::toString(args->item(2)); + QString slotsignatur = QString("1%1").arg(slotname); + const char* slotsig = slotsignatur.latin1(); + + return new Kross::Api::Variant( + QObject::connect(m_object, signalsig, o, slotsig)); +} + +Kross::Api::Object::Ptr QtObject::disconnectSignal(Kross::Api::List::Ptr) +{ + //TODO + return 0; +} + diff --git a/lib/kross/api/qtobject.h b/lib/kross/api/qtobject.h new file mode 100644 index 00000000..29f493a4 --- /dev/null +++ b/lib/kross/api/qtobject.h @@ -0,0 +1,135 @@ +/*************************************************************************** + * qtobject.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_QTOBJECT_H +#define KROSS_API_QTOBJECT_H + +#include "class.h" + +#include <qstring.h> +#include <qobject.h> + +// Forward-declaration of the builtin Qt QUObject struct. +struct QUObject; + +namespace Kross { namespace Api { + + // Forward declarations. + class Object; + class Variant; + class ScriptContainer; + class ScriptContrainer; + + /** + * Class to wrap \a QObject or inherited instances. + * + * This class publishs all SIGNAL's, SLOT's and Q_PROPERTY's + * the QObject has. + */ + class QtObject : public Kross::Api::Class<QtObject> + { + public: + + /** + * Shared pointer to implement reference-counting. + */ + typedef KSharedPtr<QtObject> Ptr; + + /** + * Constructor. + * + * \param object The \a QObject instance this + * class wraps. + * \param name The unique name this \a QtObject + * instance has. If not defined then the + * \a QObject::name() will be used. + */ + QtObject(QObject* object, const QString& name = QString::null); + + /** + * Destructor. + */ + virtual ~QtObject(); + + /// \see Kross::Api::Object::getClassName() + virtual const QString getClassName() const; + + /** + * Return the \a QObject instance this class wraps. + * + * \return The wrapped QObject. + */ + QObject* getObject(); + + /** + * Build a Qt QUObject struct out of the Qt signal or + * slot signature and the passed \a List arguments. + * + * \throw RuntimeException If the try to translate \p arguments + * failed. + * \param signature The Qt signal or slot signature. + * \param arguments The optional \a List of arguments. + * \return A QUObject array. + */ + static QUObject* toQUObject(const QString& signature, List::Ptr arguments); + + private: + /// The wrapped QObject. + QObject* m_object; + + // QProperty's + + /// Return a list of property names. + Kross::Api::Object::Ptr propertyNames(Kross::Api::List::Ptr); + /// Return true if the property exists else false. + Kross::Api::Object::Ptr hasProperty(Kross::Api::List::Ptr); + /// Return a property. + Kross::Api::Object::Ptr getProperty(Kross::Api::List::Ptr); + /// Set a property. + Kross::Api::Object::Ptr setProperty(Kross::Api::List::Ptr); + + // Slots + + /// Return a list of slot names. + Kross::Api::Object::Ptr slotNames(Kross::Api::List::Ptr); + /// Return true if the slot exists else false. + Kross::Api::Object::Ptr hasSlot(Kross::Api::List::Ptr); + /// Call a slot. + Kross::Api::Object::Ptr callSlot(Kross::Api::List::Ptr); + + // Signals + + /// Return a list of signal names. + Kross::Api::Object::Ptr signalNames(Kross::Api::List::Ptr); + /// Return true if the signal exists else false. + Kross::Api::Object::Ptr hasSignal(Kross::Api::List::Ptr); + /// Emit a signal. + Kross::Api::Object::Ptr emitSignal(Kross::Api::List::Ptr); + + /// Connect signal with a QObject slot. + Kross::Api::Object::Ptr connectSignal(Kross::Api::List::Ptr); + /// Disconnect signal from QObject slot. + Kross::Api::Object::Ptr disconnectSignal(Kross::Api::List::Ptr); + + }; + +}} + +#endif + diff --git a/lib/kross/api/script.cpp b/lib/kross/api/script.cpp new file mode 100644 index 00000000..d960ba43 --- /dev/null +++ b/lib/kross/api/script.cpp @@ -0,0 +1,59 @@ +/*************************************************************************** + * script.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "script.h" +#include "object.h" +#include "list.h" +#include "interpreter.h" +#include "exception.h" +#include "../main/scriptcontainer.h" + +using namespace Kross::Api; + +Script::Script(Interpreter* const interpreter, ScriptContainer* const scriptcontainer) + : m_interpreter(interpreter) + , m_scriptcontainer(scriptcontainer) + , m_exception(0) +{ +} + +Script::~Script() +{ +} + +bool Script::hadException() +{ + return m_exception != 0; +} + +Exception::Ptr Script::getException() +{ + return m_exception; +} + +void Script::setException(Exception::Ptr e) +{ + m_exception = e; +} + +void Script::clearException() +{ + m_exception = 0; +} + diff --git a/lib/kross/api/script.h b/lib/kross/api/script.h new file mode 100644 index 00000000..4f7d0c04 --- /dev/null +++ b/lib/kross/api/script.h @@ -0,0 +1,140 @@ +/*************************************************************************** + * script.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_SCRIPT_H +#define KROSS_API_SCRIPT_H + +#include <qstring.h> +#include <qstringlist.h> + +#include "class.h" + +namespace Kross { namespace Api { + + // Forward declarations. + class Object; + class Interpreter; + class ScriptContainer; + class List; + class Exception; + + /** + * Base class for interpreter dependend functionality + * each script provides. + * + * Each \a ScriptContainer holds a pointer to a class + * that implements the \a Script functionality for the + * defined \a Interpreter . + */ + class Script + { + public: + + /** + * Constructor. + * + * \param interpreter The \a Interpreter instance + * that uses this \a Script instance. + * \param scriptcontainer The \a ScriptContainer instance + * this script is associated with. + */ + Script(Interpreter* const interpreter, ScriptContainer* const scriptcontainer); + + /** + * Destructor. + */ + virtual ~Script(); + + /** + * \return true if the script throwed an exception + * else false. + */ + bool hadException(); + + /** + * \return the \a Exception the script throwed. + */ + Exception::Ptr getException(); + + /** + * Set a new exception this script throwed. + * + * \param e The \a Exception . + */ + void setException(Exception::Ptr e); + + /** + * Clear previous exceptions. If called \a hadException() + * will return false again. + */ + void clearException(); + + /** + * Execute the script. + * + * \throws Exception on error. + * \return The execution result. Could be NULL too. + */ + virtual Kross::Api::Object::Ptr execute() = 0; + + /** + * \return a list of callable functionnames this + * script spends. + */ + virtual const QStringList& getFunctionNames() = 0; + + /** + * Call a function. + * + * \throws Exception on error. + * \param name The name of the function to execute. + * \param args Optional arguments passed to the function. + * \return The result of the called function. Could be NULL. + */ + virtual Kross::Api::Object::Ptr callFunction(const QString& name, Kross::Api::List::Ptr args) = 0; + + /** + * \return a list of classnames. + */ + virtual const QStringList& getClassNames() = 0; + + /** + * Create and return a new class instance. + * + * \throws Exception on error. + * \param name The name of the class to create a instance of. + * \return The new classinstance. + */ + virtual Kross::Api::Object::Ptr classInstance(const QString& name) = 0; + + protected: + /// The \a Interpreter used to create this Script instance. + Interpreter* const m_interpreter; + /// The \a ScriptContainer associated with this Script. + ScriptContainer* const m_scriptcontainer; + + private: + /// The \a Exception this script throwed. + Exception::Ptr m_exception; + }; + +}} + +#endif + diff --git a/lib/kross/api/value.h b/lib/kross/api/value.h new file mode 100644 index 00000000..544d4a36 --- /dev/null +++ b/lib/kross/api/value.h @@ -0,0 +1,91 @@ +/*************************************************************************** + * value.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_VALUE_H +#define KROSS_API_VALUE_H + +#include <qstring.h> +#include <qvariant.h> + +#include "object.h" + +namespace Kross { namespace Api { + + /** + * Template class to represent values. + * + * Classes like \a Variant or \a List are implementing this + * class. That way we have a common base for all kind of + * values. + */ + template<class T, class V> + class Value : public Object + { + public: + + /** + * Constructor. + * + * \param value The initial value this + * Value has. + */ + Value(V value) + : Object() + , m_value(value) {} + + /** + * Destructor. + */ + virtual ~Value() {} + + //operator V&() const { return m_value; } + + /** + * Return the value. + * + * \return The value this Value-class holds. + */ + V& getValue() { return m_value; } + //operator V& () { return m_value; } + + template<typename TYPE> + static Object::Ptr toObject(TYPE t) { return new T(t); } + +#if 0 +//do we need it anyway? + /** + * Set the value. + * The value is call-by-value cause it may + * contain some KShared and therefore + * we need to keep a local copy to keep + * it from disappearing. + * + * \param value The value to set. + */ + void setValue(V& value) { m_value = value; } +#endif + + private: + V m_value; + }; + +}} + +#endif + diff --git a/lib/kross/api/variant.cpp b/lib/kross/api/variant.cpp new file mode 100644 index 00000000..92c0e3cb --- /dev/null +++ b/lib/kross/api/variant.cpp @@ -0,0 +1,168 @@ +/*************************************************************************** + * variant.cpp + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#include "variant.h" +#include "list.h" + +#include <klocale.h> + +using namespace Kross::Api; + +Variant::Variant(const QVariant& value) + : Value<Variant, QVariant>(value) +{ +} + +Variant::~Variant() +{ +} + +const QString Variant::getClassName() const +{ + return "Kross::Api::Variant"; +} + +const QString Variant::toString() +{ + return getValue().toString(); +} + +/* +const QString Variant::getVariantType(Object::Ptr object) +{ + switch( toVariant(object).type() ) { + + case QVariant::CString: + case QVariant::String: + return "Kross::Api::Variant::String"; + case QVariant::Map: + return "Kross::Api::Variant::Dict"; + case QVariant::StringList: + case QVariant::List: + return "Kross::Api::Variant::List"; + case QVariant::Double: + //return "Kross::Api::Variant::Double"; + case QVariant::UInt: + //return "Kross::Api::Variant::UInt"; // python isn't able to differ between int and uint :-( + case QVariant::LongLong: + case QVariant::ULongLong: + case QVariant::Int: + return "Kross::Api::Variant::Integer"; + case QVariant::Bool: + return "Kross::Api::Variant::Bool"; + default: //Date, Time, DateTime, ByteArray, BitArray, Rect, Size, Color, Invalid, etc. + return "Kross::Api::Variant"; + } +} +*/ + +const QVariant& Variant::toVariant(Object::Ptr object) +{ + return Object::fromObject<Variant>( object.data() )->getValue(); +} + +const QString Variant::toString(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::String)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::String expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toString(); +} + +int Variant::toInt(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::Int)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::Int expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toInt(); +} + +uint Variant::toUInt(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::UInt)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::UInt expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toUInt(); +} + +double Variant::toDouble(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::Double)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::Double expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toDouble(); +} + +Q_LLONG Variant::toLLONG(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::LongLong)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::LLONG expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toLongLong(); +} + +Q_ULLONG Variant::toULLONG(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::ULongLong)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::ULLONG expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toULongLong(); +} + +bool Variant::toBool(Object::Ptr object) +{ + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::Bool)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::Bool expected, but got %1.").arg(variant.typeName()).latin1()) ); + return variant.toBool(); +} + +QStringList Variant::toStringList(Object::Ptr object) +{ + List* list = dynamic_cast< List* >( object.data() ); + if(list) { + QStringList l; + QValueList<Object::Ptr> valuelist = list->getValue(); + QValueList<Object::Ptr>::Iterator it(valuelist.begin()), end(valuelist.end()); + for(; it != end; ++it) + l.append( toString(*it) ); + return l; + } + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::StringList)) + throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::StringList expected, but got '%1'.").arg(variant.typeName()).latin1()) ); + return variant.toStringList(); +} + +QValueList<QVariant> Variant::toList(Object::Ptr object) +{ + List* list = dynamic_cast< List* >( object.data() ); + if(list) { + QValueList<QVariant> l; + QValueList<Object::Ptr> valuelist = list->getValue(); + QValueList<Object::Ptr>::Iterator it(valuelist.begin()), end(valuelist.end()); + for(; it != end; ++it) + l.append( toVariant(*it) ); + return l; + } + const QVariant& variant = toVariant(object); + if(! variant.canCast(QVariant::List)) + throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::List expected, but got '%1'.").arg(variant.typeName()).latin1()) ); + return variant.toList(); +} diff --git a/lib/kross/api/variant.h b/lib/kross/api/variant.h new file mode 100644 index 00000000..020e6e51 --- /dev/null +++ b/lib/kross/api/variant.h @@ -0,0 +1,207 @@ +/*************************************************************************** + * variant.h + * This file is part of the KDE project + * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org) + * + * This program 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 program 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 program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ***************************************************************************/ + +#ifndef KROSS_API_VARIANT_H +#define KROSS_API_VARIANT_H + +#include <qstring.h> +#include <qvariant.h> + +#include "object.h" +#include "value.h" +#include "exception.h" + +namespace Kross { namespace Api { + + class List; + + /** + * Variant value to wrap a QVariant into a \a Kross::Api::Value + * to enable primitive types like strings or numerics. + */ + class Variant : public Value<Variant, QVariant> + { + friend class Value<Variant, QVariant>; + public: + + /** + * Constructor. + * + * \param value The initial QVariant-value + * this Variant-Object has. + * \param name The name this Value has. + */ + Variant(const QVariant& value); + + inline operator bool () { return getValue().toBool(); } + inline operator int () { return getValue().toInt(); } + inline operator uint () { return getValue().toUInt(); } + inline operator double () { return getValue().toDouble(); } + inline operator const char* () { return getValue().toString().latin1(); } + + inline operator QString () { return getValue().toString(); } + inline operator const QString () { return getValue().toString(); } + inline operator const QString& () { return getValue().asString(); } + + inline operator QCString () { return getValue().toCString(); } + inline operator const QCString () { return getValue().toCString(); } + inline operator const QCString& () { return getValue().asCString(); } + + inline operator QVariant () { return getValue(); } + inline operator const QVariant () { return getValue(); } + inline operator const QVariant& () { return getValue(); } + + /** + * Operator to return a QStringList. + * + * We can not just use getValue().toStringList() here cause maybe + * this Kross::Api::Variant is a Kross::Api::List which could be + * internaly used for list of strings as well. So, we use the + * toStringList() function which will take care of translating a + * Kross::Api::List to a QStringList if possible or to throw an + * exception if the Kross::Api::List isn't a QStringList. + */ + inline operator QStringList () { + return toStringList(this); + } + inline operator QValueList<QVariant> () { + return toList(this); + } + + /** + * Destructor. + */ + virtual ~Variant(); + + /// \see Kross::Api::Object::getClassName() + virtual const QString getClassName() const; + + /** + * \return a string representation of the variant. + * + * \see Kross::Api::Object::toString() + */ + virtual const QString toString(); + + /** + * Try to convert the given \a Object into + * a QVariant. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a QVariant converted object. + */ + static const QVariant& toVariant(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a QString. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a QString converted object. + */ + static const QString toString(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a int. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a int converted object. + */ + static int toInt(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a uint. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a uint converted object. + */ + static uint toUInt(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a uint. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a uint converted object. + */ + static double toDouble(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a Q_LLONG. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a Q_LLONG converted object. + */ + static Q_LLONG toLLONG(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a Q_ULLONG. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a Q_ULLONG converted object. + */ + static Q_ULLONG toULLONG(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a boolean value. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a bool converted object. + */ + static bool toBool(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a QStringList. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a QValueList converted object. + */ + static QStringList toStringList(Object::Ptr object); + + /** + * Try to convert the given \a Object into + * a QValueList of QVariant's. + * + * \throw TypeException If the convert failed. + * \param object The object to convert. + * \return The to a QValueList converted object. + */ + static QValueList<QVariant> toList(Object::Ptr object); + + }; + +}} + +#endif + |