khtml Library API Documentation

kjs_navigator.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2000 Harri Porten (porten@kde.org)
00005  *  Copyright (c) 2000 Daniel Molkentin (molkentin@kde.org)
00006  *  Copyright (c) 2000 Stefan Schimanski (schimmi@kde.org)
00007  *  Copyright (C) 2003 Apple Computer, Inc.
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Library General Public
00011  *  License as published by the Free Software Foundation; either
00012  *  version 2 of the License, or (at your option) any later version.
00013  *
00014  *  This library is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *  Library General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU Library General Public
00020  *  License along with this library; if not, write to the Free Software
00021  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include <klocale.h>
00025 
00026 #include <kstandarddirs.h>
00027 #include <kconfig.h>
00028 #include <kdebug.h>
00029 
00030 #include <kio/kprotocolmanager.h>
00031 #include <kio/kmimetype.h>
00032 #include <kio/kservice.h>
00033 #include <kio/ktrader.h>
00034 #include "kjs_navigator.h"
00035 #include "kjs/lookup.h"
00036 #include "kjs_binding.h"
00037 #include "khtml_part.h"
00038 #include <sys/utsname.h>
00039 #include "kjs_navigator.lut.h"
00040 
00041 using namespace KJS;
00042 
00043 namespace KJS {
00044 
00045     // All objects that need plugin info must inherit from PluginBase
00046     // Its ctor and dtor take care of the refcounting on the static lists.
00047     class PluginBase : public ObjectImp {
00048     public:
00049         PluginBase(ExecState *exec);
00050         virtual ~PluginBase();
00051 
00052         struct MimeClassInfo;
00053         struct PluginInfo;
00054 
00055         struct MimeClassInfo {
00056             QString type;
00057             QString desc;
00058             QString suffixes;
00059             PluginInfo *plugin;
00060         };
00061 
00062         struct PluginInfo {
00063             QString name;
00064             QString file;
00065             QString desc;
00066             QPtrList<MimeClassInfo> mimes;
00067         };
00068 
00069         static QPtrList<PluginInfo> *plugins;
00070         static QPtrList<MimeClassInfo> *mimes;
00071 
00072     private:
00073         static int m_refCount;
00074     };
00075 
00076 
00077     class Plugins : public PluginBase {
00078     public:
00079         Plugins(ExecState *exec) : PluginBase(exec) {};
00080         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00081         virtual const ClassInfo* classInfo() const { return &info; }
00082         static const ClassInfo info;
00083     private:
00084     };
00085     const ClassInfo Plugins::info = { "PluginArray", 0, 0, 0 };
00086 
00087 
00088     class MimeTypes : public PluginBase {
00089     public:
00090         MimeTypes(ExecState *exec) : PluginBase(exec) { };
00091         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00092         virtual const ClassInfo* classInfo() const { return &info; }
00093         static const ClassInfo info;
00094     private:
00095     };
00096     const ClassInfo MimeTypes::info = { "MimeTypeArray", 0, 0, 0 };
00097 
00098 
00099     class Plugin : public PluginBase {
00100     public:
00101         Plugin( ExecState *exec, PluginBase::PluginInfo *info )
00102           : PluginBase( exec )
00103         { m_info = info; };
00104         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00105         virtual const ClassInfo* classInfo() const { return &info; }
00106         static const ClassInfo info;
00107     private:
00108         PluginBase::PluginInfo *m_info;
00109     };
00110     const ClassInfo Plugin::info = { "Plugin", 0, 0, 0 };
00111 
00112 
00113     class MimeType : public PluginBase {
00114     public:
00115         MimeType( ExecState *exec, PluginBase::MimeClassInfo *info )
00116           : PluginBase( exec )
00117         { m_info = info; };
00118         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00119         virtual const ClassInfo* classInfo() const { return &info; }
00120         static const ClassInfo info;
00121     private:
00122         PluginBase::MimeClassInfo *m_info;
00123     };
00124     const ClassInfo MimeType::info = { "MimeType", 0, 0, 0 };
00125 
00126 }
00127 
00128 
00129 QPtrList<PluginBase::PluginInfo> *KJS::PluginBase::plugins = 0;
00130 QPtrList<PluginBase::MimeClassInfo> *KJS::PluginBase::mimes = 0;
00131 int KJS::PluginBase::m_refCount = 0;
00132 
00133 const ClassInfo Navigator::info = { "Navigator", 0, &NavigatorTable, 0 };
00134 /*
00135 @begin NavigatorTable 11
00136   appCodeName   Navigator::AppCodeName  DontDelete|ReadOnly
00137   appName   Navigator::AppName  DontDelete|ReadOnly
00138   appVersion    Navigator::AppVersion   DontDelete|ReadOnly
00139   language  Navigator::Language DontDelete|ReadOnly
00140   userAgent Navigator::UserAgent    DontDelete|ReadOnly
00141   userLanguage  Navigator::UserLanguage DontDelete|ReadOnly
00142   platform  Navigator::Platform DontDelete|ReadOnly
00143   plugins   Navigator::_Plugins DontDelete|ReadOnly
00144   mimeTypes Navigator::_MimeTypes   DontDelete|ReadOnly
00145   product   Navigator::Product  DontDelete|ReadOnly
00146   vendor    Navigator::Vendor   DontDelete|ReadOnly
00147   cookieEnabled Navigator::CookieEnabled DontDelete|ReadOnly
00148   javaEnabled   Navigator::JavaEnabled  DontDelete|Function 0
00149 @end
00150 */
00151 IMPLEMENT_PROTOFUNC_DOM(NavigatorFunc)
00152 
00153 Navigator::Navigator(ExecState *exec, KHTMLPart *p)
00154   : ObjectImp(exec->interpreter()->builtinObjectPrototype()), m_part(p) { }
00155 
00156 Value Navigator::get(ExecState *exec, const Identifier &propertyName) const
00157 {
00158 #ifdef KJS_VERBOSE
00159   kdDebug(6070) << "Navigator::get " << propertyName.ascii() << endl;
00160 #endif
00161   return lookupGet<NavigatorFunc,Navigator,ObjectImp>(exec,propertyName,&NavigatorTable,this);
00162 }
00163 
00164 Value Navigator::getValueProperty(ExecState *exec, int token) const
00165 {
00166   KURL url = m_part->url();
00167   QString userAgent = KProtocolManager::userAgentForHost(url.host());
00168   switch (token) {
00169   case AppCodeName:
00170     return String("Mozilla");
00171   case AppName:
00172     // If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape
00173     if (userAgent.find(QString::fromLatin1("Mozilla")) >= 0 &&
00174         userAgent.find(QString::fromLatin1("compatible")) == -1)
00175     {
00176       //kdDebug() << "appName -> Mozilla" << endl;
00177       return String("Netscape");
00178     }
00179     if (userAgent.find(QString::fromLatin1("Microsoft")) >= 0 ||
00180         userAgent.find(QString::fromLatin1("MSIE")) >= 0)
00181     {
00182       //kdDebug() << "appName -> IE" << endl;
00183       return String("Microsoft Internet Explorer");
00184     }
00185     //kdDebug() << "appName -> Konqueror" << endl;
00186     return String("Konqueror");
00187   case AppVersion:
00188     // We assume the string is something like Mozilla/version (properties)
00189     return String(userAgent.mid(userAgent.find('/') + 1));
00190   case Product:
00191     return String("Konqueror/khtml");
00192   case Vendor:
00193     return String("KDE");
00194   case Language:
00195   case UserLanguage:
00196     return String(KGlobal::locale()->language());
00197   case UserAgent:
00198     return String(userAgent);
00199   case Platform:
00200     // yet another evil hack, but necessary to spoof some sites...
00201     if ( (userAgent.find(QString::fromLatin1("Win"),0,false)>=0) )
00202       return String(QString::fromLatin1("Win32"));
00203     else if ( (userAgent.find(QString::fromLatin1("Macintosh"),0,false)>=0) ||
00204               (userAgent.find(QString::fromLatin1("Mac_PowerPC"),0,false)>=0) )
00205       return String(QString::fromLatin1("MacPPC"));
00206     else
00207     {
00208         struct utsname name;
00209         int ret = uname(&name);
00210         if ( ret >= 0 )
00211             return String(QString::fromLatin1("%1 %1 X11").arg(name.sysname).arg(name.machine));
00212         else // can't happen
00213             return String(QString::fromLatin1("Unix X11"));
00214     }
00215   case _Plugins:
00216     return Value(new Plugins(exec));
00217   case _MimeTypes:
00218     return Value(new MimeTypes(exec));
00219   case CookieEnabled:
00220     return Boolean(true); 
00221   default:
00222     kdDebug(6070) << "WARNING: Unhandled token in DOMEvent::getValueProperty : " << token << endl;
00223     return Value();
00224   }
00225 }
00226 
00227 /*******************************************************************/
00228 
00229 PluginBase::PluginBase(ExecState *exec)
00230   : ObjectImp(exec->interpreter()->builtinObjectPrototype() )
00231 {
00232     if ( !plugins ) {
00233         plugins = new QPtrList<PluginInfo>;
00234         mimes = new QPtrList<MimeClassInfo>;
00235         plugins->setAutoDelete( true );
00236         mimes->setAutoDelete( true );
00237 
00238         // FIXME: add domain support here
00239         KConfig kc("konquerorrc", true);
00240         if (!KConfigGroup(&kc, "Java/JavaScript Settings").readBoolEntry("EnablePlugins", true))
00241             return; // plugins disabled
00242 
00243         // read in using KTrader
00244         KTrader::OfferList offers = KTrader::self()->query("Browser/View");
00245         KTrader::OfferList::iterator it;
00246         for ( it = offers.begin(); it != offers.end(); ++it ) {
00247             PluginInfo *plugin = new PluginInfo;
00248 
00249             plugin->name = (**it).name();
00250             plugin->file = (**it).library();
00251             plugin->desc = (**it).comment();
00252 
00253             plugins->append( plugin );
00254 
00255             // get mime types from string
00256             QStringList types = (**it).serviceTypes();
00257             QStringList::Iterator type;
00258             for ( type=types.begin(); type!=types.end(); ++type ) {
00259 
00260                 // get mime information
00261                 QStringList tokens = QStringList::split(':', *type, true);
00262                 QStringList::Iterator token;
00263                 for ( token=tokens.begin(); token!=tokens.end(); ++token ) {
00264                     KMimeType::Ptr mimePtr = KMimeType::mimeType(*token);
00265                     QString name = (*mimePtr).name();
00266                     if ( name != KMimeType::defaultMimeType() )
00267                     {
00268                         MimeClassInfo *mime = new MimeClassInfo;
00269                         mime->type = name;
00270                         mime->suffixes = (*mimePtr).patterns().join(", ");
00271                         mime->desc = (*mimePtr).comment();
00272 
00273                         mime->plugin = plugin;
00274 
00275                         mimes->append( mime );
00276                         plugin->mimes.append( mime );
00277                     }
00278                }
00279             }
00280         }
00281     }
00282 
00283     m_refCount++;
00284 }
00285 
00286 PluginBase::~PluginBase()
00287 {
00288     m_refCount--;
00289     if ( m_refCount==0 ) {
00290         delete plugins;
00291         delete mimes;
00292         plugins = 0;
00293         mimes = 0;
00294     }
00295 }
00296 
00297 
00298 /*******************************************************************/
00299 IMPLEMENT_PROTOFUNC_DOM(PluginsFunc)
00300 
00301 Value Plugins::get(ExecState *exec, const Identifier &propertyName) const
00302 {
00303 #ifdef KJS_VERBOSE
00304   kdDebug(6070) << "Plugins::get " << propertyName.qstring() << endl;
00305 #endif
00306     if (propertyName == "refresh")
00307       return lookupOrCreateFunction<PluginsFunc>(exec,propertyName,this,0,0,DontDelete|Function);
00308     else if ( propertyName ==lengthPropertyName )
00309       return Number(plugins->count());
00310     else {
00311 
00312         // plugins[#]
00313         bool ok;
00314         unsigned int i = propertyName.toULong(&ok);
00315         if( ok && i<plugins->count() )
00316             return Value( new Plugin( exec, plugins->at(i) ) );
00317 
00318         // plugin[name]
00319         for ( PluginInfo *pl = plugins->first(); pl!=0; pl = plugins->next() ) {
00320             if ( pl->name==propertyName.string() )
00321                 return Value( new Plugin( exec, pl ) );
00322         }
00323     }
00324 
00325     return PluginBase::get(exec, propertyName);
00326 }
00327 
00328 /*******************************************************************/
00329 
00330 Value MimeTypes::get(ExecState *exec, const Identifier &propertyName) const
00331 {
00332 #ifdef KJS_VERBOSE
00333   kdDebug(6070) << "MimeTypes::get " << propertyName.qstring() << endl;
00334 #endif
00335     if( propertyName==lengthPropertyName )
00336         return Number( mimes->count() );
00337     else {
00338 
00339         // mimeTypes[#]
00340         bool ok;
00341         unsigned int i = propertyName.toULong(&ok);
00342         if( ok && i<mimes->count() )
00343             return Value( new MimeType( exec, mimes->at(i) ) );
00344 
00345         // mimeTypes[name]
00346         //kdDebug(6070) << "MimeTypes[" << propertyName.ascii() << "]" << endl;
00347         for ( MimeClassInfo *m=mimes->first(); m!=0; m=mimes->next() ) {
00348             //kdDebug(6070) << "m->type=" << m->type.ascii() << endl;
00349             if ( m->type == propertyName.string() )
00350                 return Value( new MimeType( exec, m ) );
00351         }
00352     }
00353 
00354     return PluginBase::get(exec, propertyName);
00355 }
00356 
00357 
00358 /************************************************************************/
00359 
00360 Value Plugin::get(ExecState *exec, const Identifier &propertyName) const
00361 {
00362 #ifdef KJS_VERBOSE
00363   kdDebug(6070) << "Plugin::get " << propertyName.qstring() << endl;
00364 #endif
00365     if ( propertyName=="name" )
00366         return String( m_info->name );
00367     else if ( propertyName == "filename" )
00368         return String( m_info->file );
00369     else if ( propertyName == "description" )
00370         return String( m_info->desc );
00371     else if ( propertyName == lengthPropertyName )
00372         return Number( m_info->mimes.count() );
00373     else {
00374 
00375         // plugin[#]
00376         bool ok;
00377         unsigned int i = propertyName.toULong(&ok);
00378         //kdDebug(6070) << "Plugin::get plugin[" << i << "]" << endl;
00379         if( ok && i<m_info->mimes.count() )
00380         {
00381             //kdDebug(6070) << "returning mimetype " << m_info->mimes.at(i)->type << endl;
00382             return Value(new MimeType(exec, m_info->mimes.at(i)));
00383         }
00384 
00385         // plugin["name"]
00386         for ( PluginBase::MimeClassInfo *m=m_info->mimes.first();
00387               m!=0; m=m_info->mimes.next() ) {
00388             if ( m->type==propertyName.string() )
00389                 return Value(new MimeType(exec, m));
00390         }
00391 
00392     }
00393 
00394     return ObjectImp::get(exec,propertyName);
00395 }
00396 
00397 
00398 /*****************************************************************************/
00399 
00400 Value MimeType::get(ExecState *exec, const Identifier &propertyName) const
00401 {
00402 #ifdef KJS_VERBOSE
00403   kdDebug(6070) << "MimeType::get " << propertyName.qstring() << endl;
00404 #endif
00405     if ( propertyName == "type" )
00406         return String( m_info->type );
00407     else if ( propertyName == "suffixes" )
00408         return String( m_info->suffixes );
00409     else if ( propertyName == "description" )
00410         return String( m_info->desc );
00411     else if ( propertyName == "enabledPlugin" )
00412         return Value(new Plugin(exec, m_info->plugin));
00413 
00414     return ObjectImp::get(exec,propertyName);
00415 }
00416 
00417 
00418 Value PluginsFunc::tryCall(ExecState *, Object &, const List &)
00419 {
00420   return Undefined();
00421 }
00422 
00423 
00424 Value NavigatorFunc::tryCall(ExecState *exec, Object &thisObj, const List &)
00425 {
00426   KJS_CHECK_THIS( KJS::Navigator, thisObj );
00427   Navigator *nav = static_cast<Navigator *>(thisObj.imp());
00428   // javaEnabled()
00429   return Boolean(nav->part()->javaEnabled());
00430 }
KDE Logo
This file is part of the documentation for khtml Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:45:44 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003