• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KJSEmbed

builtins.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
00003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
00004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
00005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "builtins.h"
00024 
00025 #include <QtCore/QCoreApplication>
00026 #include <QtCore/QFile>
00027 #include <QtGui/QMessageBox>
00028 #include <QtCore/QTextStream>
00029 #include <QtCore/QDebug>
00030 #include <QtCore/QMetaType>
00031 
00032 #ifndef QT_ONLY
00033 #include <kstandarddirs.h>
00034 #endif // QT_ONLY
00035 
00036 
00037 #include "variant_binding.h"
00038 #include "object_binding.h"
00039 #include "static_binding.h"
00040 #include "kjsembed.h"
00041 
00042 using namespace KJSEmbed;
00043 
00044 KJS::JSValue *callExec( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00045 {
00046     Q_UNUSED(exec);
00047     Q_UNUSED(self);
00048     Q_UNUSED(args);
00049     return KJS::jsBoolean( QCoreApplication::exec() );
00050 }
00051 
00052 KJS::JSValue *callDump( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00053 {
00054     Q_UNUSED(self);
00055     if( args.size() == 1)
00056     {
00057         KJS::JSObject *object = args[0]->toObject(exec);
00058     }
00059     return KJS::jsNull();
00060 }
00061 
00062 KJS::JSValue *callInclude( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00063 {
00064     Q_UNUSED(self);
00065     if( args.size() == 1)
00066     {
00067         KJS::UString filename = args[0]->toString(exec);
00068         qDebug() << "include: " << toQString(filename);
00069 
00070         KJS::Completion c = Engine::runFile( exec->dynamicInterpreter(), filename );
00071         
00072         if ( c.complType() == KJS::Normal )
00073             return KJS::jsNull();
00074         
00075         if (c.complType() == KJS::ReturnValue)
00076         {
00077             if (c.isValueCompletion())
00078                 return c.value();
00079             
00080             return KJS::jsNull();
00081         }
00082         
00083         if (c.complType() == KJS::Throw)
00084         {
00085             QString message = toQString(c.value()->toString(exec));
00086             int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
00087             return throwError(exec, KJS::EvalError, 
00088                               toUString(i18n("Error seen while processing include '%1' line %2: %3", toQString(filename), line, message)));
00089         }
00090     }
00091     else
00092     {
00093         return throwError(exec, KJS::URIError, 
00094                           toUString(i18n("include only takes 1 argument, not %1.", args.size())));
00095     }
00096 
00097     return KJS::jsNull();
00098 }
00099 
00100 #ifndef QT_ONLY
00101 
00102 KJS::JSValue *callLibrary( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00103 {
00104     Q_UNUSED(self);
00105     if( args.size() == 1)
00106     {
00107         KJS::UString filename = args[0]->toString(exec);
00108         QString qualifiedFilename = KStandardDirs::locate( "scripts", toQString(filename) );
00109         if ( !qualifiedFilename.isEmpty() )
00110         {
00111             KJS::Completion c = Engine::runFile( exec->dynamicInterpreter(), toUString(qualifiedFilename) );
00112             if ( c.complType() == KJS::Normal )
00113                 return KJS::jsNull();
00114             
00115             if (c.complType() == KJS::ReturnValue)
00116             {
00117                 if (c.isValueCompletion())
00118                     return c.value();
00119                 
00120                 return KJS::jsNull();
00121             }
00122             
00123             if (c.complType() == KJS::Throw)
00124             {
00125                 QString message = toQString(c.value()->toString(exec));
00126                 int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
00127                 return throwError(exec, KJS::EvalError, 
00128                                   toUString(i18n("Error seen while processing include '%1' line %2: %3", toQString(filename), line, message)));
00129             }
00130         }
00131         else {
00132             QString msg = i18n( "File %1 not found.", toQString(filename) );
00133             return throwError( exec, KJS::URIError, toUString(msg) );
00134         }
00135     }
00136     else {
00137         return throwError(exec, KJS::URIError, 
00138                           toUString(i18n("library only takes 1 argument, not %1.", args.size())));
00139     }
00140 
00141     return KJS::jsNull();
00142 }
00143 
00144 #endif // QT_ONLY
00145 
00146 KJS::JSValue *callAlert( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00147 {
00148     Q_UNUSED(self)
00149     if (args.size() == 1)
00150     {
00151         (*KJSEmbed::conerr()) << "callAlert";
00152         QString message = toQString(args[0]->toString(exec));
00153         QMessageBox::warning(0, "Alert", message, QMessageBox::Ok, QMessageBox::NoButton); 
00154     }
00155     return KJS::jsNull();
00156 }
00157 
00158 KJS::JSValue *callConfirm( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00159 {
00160     Q_UNUSED(self)
00161     if (args.size() == 1)
00162     {
00163         QString message = toQString(args[0]->toString(exec));
00164         int result = QMessageBox::question (0, "Confirm", message, QMessageBox::Yes, QMessageBox::No);
00165         if (result == QMessageBox::Yes)
00166             return KJS::jsBoolean(true);
00167     }
00168     return KJS::jsBoolean(false);
00169 }
00170 
00171 KJS::JSValue *callIsVariantType( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00172 {
00173     Q_UNUSED(self)
00174     if (args.size() == 1)
00175     {
00176         QString thetypename = toQString(args[0]->toString(exec));
00177         return KJS::jsBoolean( QMetaType::type( thetypename.toLatin1().data() ) );
00178     }
00179     return KJS::jsBoolean(false);
00180 }
00181 
00182 KJS::JSValue *callIsVariant( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00183 {
00184     Q_UNUSED(self)
00185     if (args.size() == 1)
00186     {
00187         KJS::JSObject *obj = args[0]->toObject(exec);
00188         if (obj->inherits(&VariantBinding::info))
00189         {
00190             return KJS::jsBoolean(true);
00191         }
00192     }
00193     return KJS::jsBoolean(false);
00194 }
00195 
00196 KJS::JSValue *callIsObject( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
00197 {
00198     Q_UNUSED(self)
00199     if (args.size() == 1)
00200     {
00201         KJS::JSObject *obj = args[0]->toObject(exec);
00202         if (obj->inherits(&ObjectBinding::info))
00203         {
00204             return KJS::jsBoolean(true);
00205         }
00206     }
00207     return KJS::jsBoolean(false);
00208 }
00209 
00210 const Method BuiltinsFactory::BuiltinMethods[] =
00211 {
00212     {"exec", 0, KJS::DontDelete|KJS::ReadOnly, &callExec},
00213     {"dump", 1, KJS::DontDelete|KJS::ReadOnly, &callDump},
00214     {"include", 1, KJS::DontDelete|KJS::ReadOnly, &callInclude},
00215 #ifndef QT_ONLY
00216     {"library", 1, KJS::DontDelete|KJS::ReadOnly, &callLibrary},
00217 #endif // QT_ONLY
00218     {"alert", 1, KJS::DontDelete|KJS::ReadOnly, &callAlert},
00219     {"confirm", 1, KJS::DontDelete|KJS::ReadOnly, &callConfirm},
00220     {"isVariantType", 1, KJS::DontDelete|KJS::ReadOnly, &callIsVariantType},
00221     {"isVariant", 1, KJS::DontDelete|KJS::ReadOnly, &callIsVariant},
00222     {"isObject", 1, KJS::DontDelete|KJS::ReadOnly, &callIsObject},
00223     {0, 0, 0, 0 }
00224 };
00225 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;

KJSEmbed

Skip menu "KJSEmbed"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal