lib

callable.cpp

00001 /***************************************************************************
00002  * callable.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "callable.h"
00021 #include "variant.h"
00022 #include "dict.h"
00023 
00024 #include "../main/krossconfig.h"
00025 
00026 using namespace Kross::Api;
00027 
00028 Callable::Callable(const QString& name)
00029     : Object()
00030     , m_name(name)
00031 {
00032 }
00033 
00034 Callable::~Callable()
00035 {
00036 }
00037 
00038 const QString Callable::getName() const
00039 {
00040     return m_name;
00041 }
00042 
00043 const QString Callable::getClassName() const
00044 {
00045     return "Kross::Api::Callable";
00046 }
00047 
00048 bool Callable::hasChild(const QString& name) const
00049 {
00050     return m_children.contains(name);
00051 }
00052 
00053 Object::Ptr Callable::getChild(const QString& name) const
00054 {
00055     return m_children[name];
00056 }
00057 
00058 QMap<QString, Object::Ptr> Callable::getChildren() const
00059 {
00060     return m_children;
00061 }
00062 
00063 bool Callable::addChild(const QString& name, Object* object)
00064 {
00065 #ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG
00066     krossdebug( QString("Kross::Api::Callable::addChild() object.name='%1' object.classname='%2'")
00067         .arg(name).arg(object->getClassName()) );
00068 #endif
00069     m_children.replace(name, Object::Ptr(object));
00070     return true;
00071 }
00072 
00073 bool Callable::addChild(Callable* object)
00074 {
00075     return addChild(object->getName(), object);
00076 }
00077 
00078 void Callable::removeChild(const QString& name)
00079 {
00080 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00081     krossdebug( QString("Kross::Api::Callable::removeChild() name='%1'").arg(name) );
00082 #endif
00083     m_children.remove(name);
00084 }
00085 
00086 void Callable::removeAllChildren()
00087 {
00088 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00089     krossdebug( "Kross::Api::Callable::removeAllChildren()" );
00090 #endif
00091     m_children.clear();
00092 }
00093 
00094 Object::Ptr Callable::call(const QString& name, List::Ptr args)
00095 {
00096 #ifdef KROSS_API_CALLABLE_CALL_DEBUG
00097     krossdebug( QString("Kross::Api::Callable::call() name=%1 getName()=%2 arguments=%3").arg(name).arg(getName()).arg(args ? args->toString() : QString("")) );
00098 #endif
00099 
00100     if(name.isEmpty()) // return a self-reference if no functionname is defined.
00101         return this;
00102 
00103     // if name is defined try to get the matching child and pass the call to it.
00104     Object::Ptr object = getChild(name);
00105     if(object) {
00106         //TODO handle namespace, e.g. "mychild1.mychild2.myfunction"
00107         return object->call(name, args);
00108     }
00109 
00110     if(name == "get") {
00111         QString s = Variant::toString(args->item(0));
00112         Object::Ptr obj = getChild(s);
00113         if(! obj)
00114             throw Exception::Ptr( new Exception(QString("The object '%1' has no child object '%2'").arg(getName()).arg(s)) );
00115         return obj;
00116     }
00117     else if(name == "has") {
00118         return new Variant( hasChild( Variant::toString(args->item(0)) ) );
00119     }
00120     else if(name == "call") {
00121         //TODO should we remove first args-item?
00122         return Object::call(Variant::toString(args->item(0)), args);
00123     }
00124     else if(name == "list") {
00125         QStringList list;
00126         QMap<QString, Object::Ptr> children = getChildren();
00127         QMap<QString, Object::Ptr>::Iterator it( children.begin() );
00128         for(; it != children.end(); ++it)
00129             list.append( it.key() );
00130         return new Variant(list);
00131     }
00132     else if(name == "dict") {
00133         return new Dict( getChildren() );
00134     }
00135 
00136     // If there exists no such object return NULL.
00137     krossdebug( QString("Object '%1' has no callable object named '%2'.").arg(getName()).arg(name) );
00138     return 0;
00139 }
KDE Home | KDE Accessibility Home | Description of Access Keys