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 00248 QVariant pluginsinfo = (**it).property( "X-KDE-BrowserView-PluginsInfo" ); 00249 if ( !pluginsinfo.isValid() ) { 00250 // <backwards compatible> 00251 if ((**it).library() == QString("libnsplugin")) 00252 pluginsinfo = QVariant("nsplugins/pluginsinfo"); 00253 else 00254 // </backwards compatible> 00255 continue; 00256 } 00257 // read configuration 00258 KConfig kc( locate ("data", pluginsinfo.toString()) ); 00259 unsigned num = (unsigned int) kc.readNumEntry("number"); 00260 for ( unsigned n = 0; n < num; n++ ) { 00261 kc.setGroup( QString::number(n) ); 00262 PluginInfo *plugin = new PluginInfo; 00263 00264 plugin->name = kc.readEntry("name"); 00265 plugin->file = kc.readPathEntry("file"); 00266 plugin->desc = kc.readEntry("description"); 00267 00268 plugins->append( plugin ); 00269 00270 // get mime types from string 00271 QStringList types = QStringList::split( ';', kc.readEntry("mime") ); 00272 QStringList::Iterator type; 00273 for ( type=types.begin(); type!=types.end(); ++type ) { 00274 00275 // get mime information 00276 QStringList tokens = QStringList::split(':', *type, true); 00277 if ( tokens.count() < 3 ) // we need 3 items 00278 continue; 00279 00280 MimeClassInfo *mime = new MimeClassInfo; 00281 QStringList::Iterator token = tokens.begin(); 00282 mime->type = (*token).lower(); 00283 //kdDebug(6070) << "mime->type=" << mime->type << endl; 00284 ++token; 00285 00286 mime->suffixes = *token; 00287 ++token; 00288 00289 mime->desc = *token; 00290 ++token; 00291 00292 mime->plugin = plugin; 00293 00294 mimes->append( mime ); 00295 plugin->mimes.append( mime ); 00296 00297 } 00298 } 00299 } 00300 } 00301 00302 m_refCount++; 00303 } 00304 00305 PluginBase::~PluginBase() 00306 { 00307 m_refCount--; 00308 if ( m_refCount==0 ) { 00309 delete plugins; 00310 delete mimes; 00311 plugins = 0; 00312 mimes = 0; 00313 } 00314 } 00315 00316 00317 /*******************************************************************/ 00318 IMPLEMENT_PROTOFUNC_DOM(PluginsFunc) 00319 00320 Value Plugins::get(ExecState *exec, const Identifier &propertyName) const 00321 { 00322 #ifdef KJS_VERBOSE 00323 kdDebug(6070) << "Plugins::get " << propertyName.qstring() << endl; 00324 #endif 00325 if (propertyName == "refresh") 00326 return lookupOrCreateFunction<PluginsFunc>(exec,propertyName,this,0,0,DontDelete|Function); 00327 else if ( propertyName ==lengthPropertyName ) 00328 return Number(plugins->count()); 00329 else { 00330 00331 // plugins[#] 00332 bool ok; 00333 unsigned int i = propertyName.toULong(&ok); 00334 if( ok && i<plugins->count() ) 00335 return Value( new Plugin( exec, plugins->at(i) ) ); 00336 00337 // plugin[name] 00338 for ( PluginInfo *pl = plugins->first(); pl!=0; pl = plugins->next() ) { 00339 if ( pl->name==propertyName.string() ) 00340 return Value( new Plugin( exec, pl ) ); 00341 } 00342 } 00343 00344 return PluginBase::get(exec, propertyName); 00345 } 00346 00347 /*******************************************************************/ 00348 00349 Value MimeTypes::get(ExecState *exec, const Identifier &propertyName) const 00350 { 00351 #ifdef KJS_VERBOSE 00352 kdDebug(6070) << "MimeTypes::get " << propertyName.qstring() << endl; 00353 #endif 00354 if( propertyName==lengthPropertyName ) 00355 return Number( mimes->count() ); 00356 else { 00357 00358 // mimeTypes[#] 00359 bool ok; 00360 unsigned int i = propertyName.toULong(&ok); 00361 if( ok && i<mimes->count() ) 00362 return Value( new MimeType( exec, mimes->at(i) ) ); 00363 00364 // mimeTypes[name] 00365 //kdDebug(6070) << "MimeTypes[" << propertyName.ascii() << "]" << endl; 00366 for ( MimeClassInfo *m=mimes->first(); m!=0; m=mimes->next() ) { 00367 //kdDebug(6070) << "m->type=" << m->type.ascii() << endl; 00368 if ( m->type == propertyName.string() ) 00369 return Value( new MimeType( exec, m ) ); 00370 } 00371 } 00372 00373 return PluginBase::get(exec, propertyName); 00374 } 00375 00376 00377 /************************************************************************/ 00378 00379 Value Plugin::get(ExecState *exec, const Identifier &propertyName) const 00380 { 00381 #ifdef KJS_VERBOSE 00382 kdDebug(6070) << "Plugin::get " << propertyName.qstring() << endl; 00383 #endif 00384 if ( propertyName=="name" ) 00385 return String( m_info->name ); 00386 else if ( propertyName == "filename" ) 00387 return String( m_info->file ); 00388 else if ( propertyName == "description" ) 00389 return String( m_info->desc ); 00390 else if ( propertyName == lengthPropertyName ) 00391 return Number( m_info->mimes.count() ); 00392 else { 00393 00394 // plugin[#] 00395 bool ok; 00396 unsigned int i = propertyName.toULong(&ok); 00397 //kdDebug(6070) << "Plugin::get plugin[" << i << "]" << endl; 00398 if( ok && i<m_info->mimes.count() ) 00399 { 00400 //kdDebug(6070) << "returning mimetype " << m_info->mimes.at(i)->type << endl; 00401 return Value(new MimeType(exec, m_info->mimes.at(i))); 00402 } 00403 00404 // plugin["name"] 00405 for ( PluginBase::MimeClassInfo *m=m_info->mimes.first(); 00406 m!=0; m=m_info->mimes.next() ) { 00407 if ( m->type==propertyName.string() ) 00408 return Value(new MimeType(exec, m)); 00409 } 00410 00411 } 00412 00413 return ObjectImp::get(exec,propertyName); 00414 } 00415 00416 00417 /*****************************************************************************/ 00418 00419 Value MimeType::get(ExecState *exec, const Identifier &propertyName) const 00420 { 00421 #ifdef KJS_VERBOSE 00422 kdDebug(6070) << "MimeType::get " << propertyName.qstring() << endl; 00423 #endif 00424 if ( propertyName == "type" ) 00425 return String( m_info->type ); 00426 else if ( propertyName == "suffixes" ) 00427 return String( m_info->suffixes ); 00428 else if ( propertyName == "description" ) 00429 return String( m_info->desc ); 00430 else if ( propertyName == "enabledPlugin" ) 00431 return Value(new Plugin(exec, m_info->plugin)); 00432 00433 return ObjectImp::get(exec,propertyName); 00434 } 00435 00436 00437 Value PluginsFunc::tryCall(ExecState *, Object &, const List &) 00438 { 00439 return Undefined(); 00440 } 00441 00442 00443 Value NavigatorFunc::tryCall(ExecState *exec, Object &thisObj, const List &) 00444 { 00445 KJS_CHECK_THIS( KJS::Navigator, thisObj ); 00446 Navigator *nav = static_cast<Navigator *>(thisObj.imp()); 00447 // javaEnabled() 00448 return Boolean(nav->part()->javaEnabled()); 00449 }
KDE Logo
This file is part of the documentation for khtml Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Aug 30 22:56:30 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003