kjs_navigator.cpp Source File (khtml Library)

| # khtml Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:34:24 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001