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 "kjs_navigator.h"
00032 #include "kjs/lookup.h"
00033 #include "kjs_binding.h"
00034 #include "khtml_part.h"
00035 #include <sys/utsname.h>
00036 #include "kjs_navigator.lut.h"
00037 
00038 using namespace KJS;
00039 
00040 namespace KJS {
00041 
00042     // All objects that need plugin info must inherit from PluginBase
00043     // Its ctor and dtor take care of the refcounting on the static lists.
00044     class PluginBase : public ObjectImp {
00045     public:
00046         PluginBase(ExecState *exec);
00047         virtual ~PluginBase();
00048 
00049         struct MimeClassInfo;
00050         struct PluginInfo;
00051 
00052         struct MimeClassInfo {
00053             QString type;
00054             QString desc;
00055             QString suffixes;
00056             PluginInfo *plugin;
00057         };
00058 
00059         struct PluginInfo {
00060             QString name;
00061             QString file;
00062             QString desc;
00063             QPtrList<MimeClassInfo> mimes;
00064         };
00065 
00066         static QPtrList<PluginInfo> *plugins;
00067         static QPtrList<MimeClassInfo> *mimes;
00068 
00069     private:
00070         static int m_refCount;
00071     };
00072 
00073 
00074     class Plugins : public PluginBase {
00075     public:
00076         Plugins(ExecState *exec) : PluginBase(exec) {};
00077         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00078         virtual const ClassInfo* classInfo() const { return &info; }
00079         static const ClassInfo info;
00080     private:
00081     };
00082     const ClassInfo Plugins::info = { "PluginArray", 0, 0, 0 };
00083 
00084 
00085     class MimeTypes : public PluginBase {
00086     public:
00087         MimeTypes(ExecState *exec) : PluginBase(exec) { };
00088         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00089         virtual const ClassInfo* classInfo() const { return &info; }
00090         static const ClassInfo info;
00091     private:
00092     };
00093     const ClassInfo MimeTypes::info = { "MimeTypeArray", 0, 0, 0 };
00094 
00095 
00096     class Plugin : public PluginBase {
00097     public:
00098         Plugin( ExecState *exec, PluginBase::PluginInfo *info )
00099           : PluginBase( exec )
00100         { m_info = info; };
00101         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00102         virtual const ClassInfo* classInfo() const { return &info; }
00103         static const ClassInfo info;
00104     private:
00105         PluginBase::PluginInfo *m_info;
00106     };
00107     const ClassInfo Plugin::info = { "Plugin", 0, 0, 0 };
00108 
00109 
00110     class MimeType : public PluginBase {
00111     public:
00112         MimeType( ExecState *exec, PluginBase::MimeClassInfo *info )
00113           : PluginBase( exec )
00114         { m_info = info; };
00115         virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00116         virtual const ClassInfo* classInfo() const { return &info; }
00117         static const ClassInfo info;
00118     private:
00119         PluginBase::MimeClassInfo *m_info;
00120     };
00121     const ClassInfo MimeType::info = { "MimeType", 0, 0, 0 };
00122 
00123 }
00124 
00125 
00126 QPtrList<PluginBase::PluginInfo> *KJS::PluginBase::plugins = 0;
00127 QPtrList<PluginBase::MimeClassInfo> *KJS::PluginBase::mimes = 0;
00128 int KJS::PluginBase::m_refCount = 0;
00129 
00130 const ClassInfo Navigator::info = { "Navigator", 0, &NavigatorTable, 0 };
00131 /*
00132 @begin NavigatorTable 11
00133   appCodeName   Navigator::AppCodeName  DontDelete|ReadOnly
00134   appName   Navigator::AppName  DontDelete|ReadOnly
00135   appVersion    Navigator::AppVersion   DontDelete|ReadOnly
00136   language  Navigator::Language DontDelete|ReadOnly
00137   userAgent Navigator::UserAgent    DontDelete|ReadOnly
00138   userLanguage  Navigator::UserLanguage DontDelete|ReadOnly
00139   platform  Navigator::Platform DontDelete|ReadOnly
00140   plugins   Navigator::_Plugins DontDelete|ReadOnly
00141   mimeTypes Navigator::_MimeTypes   DontDelete|ReadOnly
00142   product   Navigator::Product  DontDelete|ReadOnly
00143   vendor    Navigator::Vendor   DontDelete|ReadOnly
00144   cookieEnabled Navigator::CookieEnabled DontDelete|ReadOnly
00145   javaEnabled   Navigator::JavaEnabled  DontDelete|Function 0
00146 @end
00147 */
00148 IMPLEMENT_PROTOFUNC_DOM(NavigatorFunc)
00149 
00150 Navigator::Navigator(ExecState *exec, KHTMLPart *p)
00151   : ObjectImp(exec->interpreter()->builtinObjectPrototype()), m_part(p) { }
00152 
00153 Value Navigator::get(ExecState *exec, const Identifier &propertyName) const
00154 {
00155 #ifdef KJS_VERBOSE
00156   kdDebug(6070) << "Navigator::get " << propertyName.ascii() << endl;
00157 #endif
00158   return lookupGet<NavigatorFunc,Navigator,ObjectImp>(exec,propertyName,&NavigatorTable,this);
00159 }
00160 
00161 Value Navigator::getValueProperty(ExecState *exec, int token) const
00162 {
00163   KURL url = m_part->url();
00164   QString userAgent = KProtocolManager::userAgentForHost(url.host());
00165   switch (token) {
00166   case AppCodeName:
00167     return String("Mozilla");
00168   case AppName:
00169     // If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape
00170     if (userAgent.find(QString::fromLatin1("Mozilla")) >= 0 &&
00171         userAgent.find(QString::fromLatin1("compatible")) == -1)
00172     {
00173       //kdDebug() << "appName -> Mozilla" << endl;
00174       return String("Netscape");
00175     }
00176     if (userAgent.find(QString::fromLatin1("Microsoft")) >= 0 ||
00177         userAgent.find(QString::fromLatin1("MSIE")) >= 0)
00178     {
00179       //kdDebug() << "appName -> IE" << endl;
00180       return String("Microsoft Internet Explorer");
00181     }
00182     //kdDebug() << "appName -> Konqueror" << endl;
00183     return String("Konqueror");
00184   case AppVersion:
00185     // We assume the string is something like Mozilla/version (properties)
00186     return String(userAgent.mid(userAgent.find('/') + 1));
00187   case Product:
00188     return String("Konqueror/khtml");
00189   case Vendor:
00190     return String("KDE");
00191   case Language:
00192   case UserLanguage:
00193     return String(KGlobal::locale()->language());
00194   case UserAgent:
00195     return String(userAgent);
00196   case Platform:
00197     // yet another evil hack, but necessary to spoof some sites...
00198     if ( (userAgent.find(QString::fromLatin1("Win"),0,false)>=0) )
00199       return String(QString::fromLatin1("Win32"));
00200     else if ( (userAgent.find(QString::fromLatin1("Macintosh"),0,false)>=0) ||
00201               (userAgent.find(QString::fromLatin1("Mac_PowerPC"),0,false)>=0) )
00202       return String(QString::fromLatin1("MacPPC"));
00203     else
00204     {
00205         struct utsname name;
00206         int ret = uname(&name);
00207         if ( ret >= 0 )
00208             return String(QString::fromLatin1("%1 %1 X11").arg(name.sysname).arg(name.machine));
00209         else // can't happen
00210             return String(QString::fromLatin1("Unix X11"));
00211     }
00212   case _Plugins:
00213     return Value(new Plugins(exec));
00214   case _MimeTypes:
00215     return Value(new MimeTypes(exec));
00216   case CookieEnabled:
00217     return Boolean(true); 
00218   default:
00219     kdDebug(6070) << "WARNING: Unhandled token in DOMEvent::getValueProperty : " << token << endl;
00220     return Value();
00221   }
00222 }
00223 
00224 /*******************************************************************/
00225 
00226 PluginBase::PluginBase(ExecState *exec)
00227   : ObjectImp(exec->interpreter()->builtinObjectPrototype() )
00228 {
00229     if ( !plugins ) {
00230         plugins = new QPtrList<PluginInfo>;
00231         mimes = new QPtrList<MimeClassInfo>;
00232         plugins->setAutoDelete( true );
00233         mimes->setAutoDelete( true );
00234 
00235         // read configuration
00236         KConfig c(KGlobal::dirs()->saveLocation("data","nsplugins")+"/pluginsinfo");
00237         unsigned num = (unsigned int)c.readNumEntry("number");
00238         // FIXME: add domain support here
00239         KConfig kc("konquerorrc", true);
00240         bool enabled = KConfigGroup(&kc, "Java/JavaScript Settings").readBoolEntry("EnablePlugins", true);
00241         for ( unsigned n = 0; enabled && n < num; n++ ) {
00242 
00243             c.setGroup( QString::number(n) );
00244             PluginInfo *plugin = new PluginInfo;
00245 
00246             plugin->name = c.readEntry("name");
00247             plugin->file = c.readPathEntry("file");
00248             plugin->desc = c.readEntry("description");
00249 
00250             //kdDebug(6070) << "plugin : " << plugin->name << " - " << plugin->desc << endl;
00251 
00252             plugins->append( plugin );
00253 
00254             // get mime types from string
00255             QStringList types = QStringList::split( ';', c.readEntry("mime") );
00256             QStringList::Iterator type;
00257             for ( type=types.begin(); type!=types.end(); ++type ) {
00258 
00259                 // get mime information
00260                 QStringList tokens = QStringList::split(':', *type, true);
00261                 if ( tokens.count() < 3 ) // we need 3 items
00262                   continue;
00263 
00264                 MimeClassInfo *mime = new MimeClassInfo;
00265                 QStringList::Iterator token = tokens.begin();
00266                 mime->type = (*token).lower();
00267                 //kdDebug(6070) << "mime->type=" << mime->type << endl;
00268                 ++token;
00269 
00270                 mime->suffixes = *token;
00271                 ++token;
00272 
00273                 mime->desc = *token;
00274                 ++token;
00275 
00276                 mime->plugin = plugin;
00277 
00278                 mimes->append( mime );
00279                 plugin->mimes.append( mime );
00280             }
00281         }
00282     }
00283 
00284     m_refCount++;
00285 }
00286 
00287 PluginBase::~PluginBase()
00288 {
00289     m_refCount--;
00290     if ( m_refCount==0 ) {
00291         delete plugins;
00292         delete mimes;
00293         plugins = 0;
00294         mimes = 0;
00295     }
00296 }
00297 
00298 
00299 /*******************************************************************/
00300 IMPLEMENT_PROTOFUNC_DOM(PluginsFunc)
00301 
00302 Value Plugins::get(ExecState *exec, const Identifier &propertyName) const
00303 {
00304 #ifdef KJS_VERBOSE
00305   kdDebug(6070) << "Plugins::get " << propertyName.qstring() << endl;
00306 #endif
00307     if (propertyName == "refresh")
00308       return lookupOrCreateFunction<PluginsFunc>(exec,propertyName,this,0,0,DontDelete|Function);
00309     else if ( propertyName ==lengthPropertyName )
00310       return Number(plugins->count());
00311     else {
00312 
00313         // plugins[#]
00314         bool ok;
00315         unsigned int i = propertyName.toULong(&ok);
00316         if( ok && i<plugins->count() )
00317             return Value( new Plugin( exec, plugins->at(i) ) );
00318 
00319         // plugin[name]
00320         for ( PluginInfo *pl = plugins->first(); pl!=0; pl = plugins->next() ) {
00321             if ( pl->name==propertyName.string() )
00322                 return Value( new Plugin( exec, pl ) );
00323         }
00324     }
00325 
00326     return PluginBase::get(exec, propertyName);
00327 }
00328 
00329 /*******************************************************************/
00330 
00331 Value MimeTypes::get(ExecState *exec, const Identifier &propertyName) const
00332 {
00333 #ifdef KJS_VERBOSE
00334   kdDebug(6070) << "MimeTypes::get " << propertyName.qstring() << endl;
00335 #endif
00336     if( propertyName==lengthPropertyName )
00337         return Number( mimes->count() );
00338     else {
00339 
00340         // mimeTypes[#]
00341         bool ok;
00342         unsigned int i = propertyName.toULong(&ok);
00343         if( ok && i<mimes->count() )
00344             return Value( new MimeType( exec, mimes->at(i) ) );
00345 
00346         // mimeTypes[name]
00347         //kdDebug(6070) << "MimeTypes[" << propertyName.ascii() << "]" << endl;
00348         for ( MimeClassInfo *m=mimes->first(); m!=0; m=mimes->next() ) {
00349             //kdDebug(6070) << "m->type=" << m->type.ascii() << endl;
00350             if ( m->type == propertyName.string() )
00351                 return Value( new MimeType( exec, m ) );
00352         }
00353     }
00354 
00355     return PluginBase::get(exec, propertyName);
00356 }
00357 
00358 
00359 /************************************************************************/
00360 
00361 Value Plugin::get(ExecState *exec, const Identifier &propertyName) const
00362 {
00363 #ifdef KJS_VERBOSE
00364   kdDebug(6070) << "Plugin::get " << propertyName.qstring() << endl;
00365 #endif
00366     if ( propertyName=="name" )
00367         return String( m_info->name );
00368     else if ( propertyName == "filename" )
00369         return String( m_info->file );
00370     else if ( propertyName == "description" )
00371         return String( m_info->desc );
00372     else if ( propertyName == lengthPropertyName )
00373         return Number( m_info->mimes.count() );
00374     else {
00375 
00376         // plugin[#]
00377         bool ok;
00378         unsigned int i = propertyName.toULong(&ok);
00379         //kdDebug(6070) << "Plugin::get plugin[" << i << "]" << endl;
00380         if( ok && i<m_info->mimes.count() )
00381         {
00382             //kdDebug(6070) << "returning mimetype " << m_info->mimes.at(i)->type << endl;
00383             return Value(new MimeType(exec, m_info->mimes.at(i)));
00384         }
00385 
00386         // plugin["name"]
00387         for ( PluginBase::MimeClassInfo *m=m_info->mimes.first();
00388               m!=0; m=m_info->mimes.next() ) {
00389             if ( m->type==propertyName.string() )
00390                 return Value(new MimeType(exec, m));
00391         }
00392 
00393     }
00394 
00395     return ObjectImp::get(exec,propertyName);
00396 }
00397 
00398 
00399 /*****************************************************************************/
00400 
00401 Value MimeType::get(ExecState *exec, const Identifier &propertyName) const
00402 {
00403 #ifdef KJS_VERBOSE
00404   kdDebug(6070) << "MimeType::get " << propertyName.qstring() << endl;
00405 #endif
00406     if ( propertyName == "type" )
00407         return String( m_info->type );
00408     else if ( propertyName == "suffixes" )
00409         return String( m_info->suffixes );
00410     else if ( propertyName == "description" )
00411         return String( m_info->desc );
00412     else if ( propertyName == "enabledPlugin" )
00413         return Value(new Plugin(exec, m_info->plugin));
00414 
00415     return ObjectImp::get(exec,propertyName);
00416 }
00417 
00418 
00419 Value PluginsFunc::tryCall(ExecState *, Object &, const List &)
00420 {
00421   return Undefined();
00422 }
00423 
00424 
00425 Value NavigatorFunc::tryCall(ExecState *exec, Object &thisObj, const List &)
00426 {
00427   KJS_CHECK_THIS( KJS::Navigator, thisObj );
00428   Navigator *nav = static_cast<Navigator *>(thisObj.imp());
00429   // javaEnabled()
00430   return Boolean(nav->part()->javaEnabled());
00431 }
KDE Logo
This file is part of the documentation for khtml Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:37:22 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003