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 12 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 browserLanguage Navigator::BrowserLanguage DontDelete|ReadOnly 00143 platform Navigator::Platform DontDelete|ReadOnly 00144 cpuClass Navigator::CpuClass DontDelete|ReadOnly 00145 plugins Navigator::_Plugins DontDelete|ReadOnly 00146 mimeTypes Navigator::_MimeTypes DontDelete|ReadOnly 00147 product Navigator::Product DontDelete|ReadOnly 00148 vendor Navigator::Vendor DontDelete|ReadOnly 00149 productSub Navigator::ProductSub DontDelete|ReadOnly 00150 cookieEnabled Navigator::CookieEnabled DontDelete|ReadOnly 00151 javaEnabled Navigator::JavaEnabled DontDelete|Function 0 00152 @end 00153 */ 00154 IMPLEMENT_PROTOFUNC_DOM(NavigatorFunc) 00155 00156 Navigator::Navigator(ExecState *exec, KHTMLPart *p) 00157 : ObjectImp(exec->interpreter()->builtinObjectPrototype()), m_part(p) { } 00158 00159 Value Navigator::get(ExecState *exec, const Identifier &propertyName) const 00160 { 00161 #ifdef KJS_VERBOSE 00162 kdDebug(6070) << "Navigator::get " << propertyName.ascii() << endl; 00163 #endif 00164 return lookupGet<NavigatorFunc,Navigator,ObjectImp>(exec,propertyName,&NavigatorTable,this); 00165 } 00166 00167 Value Navigator::getValueProperty(ExecState *exec, int token) const 00168 { 00169 KURL url = m_part->url(); 00170 QString userAgent = KProtocolManager::userAgentForHost(url.host()); 00171 switch (token) { 00172 case AppCodeName: 00173 return String("Mozilla"); 00174 case AppName: 00175 // If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape 00176 if (userAgent.find(QString::fromLatin1("Mozilla")) >= 0 && 00177 userAgent.find(QString::fromLatin1("compatible")) == -1) 00178 { 00179 //kdDebug() << "appName -> Mozilla" << endl; 00180 return String("Netscape"); 00181 } 00182 if (userAgent.find(QString::fromLatin1("Microsoft")) >= 0 || 00183 userAgent.find(QString::fromLatin1("MSIE")) >= 0) 00184 { 00185 //kdDebug() << "appName -> IE" << endl; 00186 return String("Microsoft Internet Explorer"); 00187 } 00188 //kdDebug() << "appName -> Konqueror" << endl; 00189 return String("Konqueror"); 00190 case AppVersion: 00191 // We assume the string is something like Mozilla/version (properties) 00192 return String(userAgent.mid(userAgent.find('/') + 1)); 00193 case Product: 00194 return String("Konqueror/khtml"); 00195 case ProductSub: 00196 { 00197 int ix = userAgent.find("Gecko"); 00198 if (ix >= 0 && userAgent.length() >= (uint)ix+14 && userAgent.at(ix+5)== '/' && userAgent.find(QRegExp("\\d{8}"), ix+6) == ix+6) 00199 { 00200 // We have Gecko/<productSub> in the UA string 00201 return String(userAgent.mid(ix+6, 8)); 00202 } 00203 else if (ix >= 0) 00204 { 00205 return String("20040107"); 00206 } 00207 } 00208 return Undefined(); 00209 case Vendor: 00210 return String("KDE"); 00211 case BrowserLanguage: 00212 case Language: 00213 case UserLanguage: 00214 return String(KGlobal::locale()->language()); 00215 case UserAgent: 00216 return String(userAgent); 00217 case Platform: 00218 // yet another evil hack, but necessary to spoof some sites... 00219 if ( (userAgent.find(QString::fromLatin1("Win"),0,false)>=0) ) 00220 return String(QString::fromLatin1("Win32")); 00221 else if ( (userAgent.find(QString::fromLatin1("Macintosh"),0,false)>=0) || 00222 (userAgent.find(QString::fromLatin1("Mac_PowerPC"),0,false)>=0) ) 00223 return String(QString::fromLatin1("MacPPC")); 00224 else 00225 { 00226 struct utsname name; 00227 int ret = uname(&name); 00228 if ( ret >= 0 ) 00229 return String(QString::fromLatin1("%1 %1 X11").arg(name.sysname).arg(name.machine)); 00230 else // can't happen 00231 return String(QString::fromLatin1("Unix X11")); 00232 } 00233 case CpuClass: 00234 { 00235 struct utsname name; 00236 int ret = uname(&name); 00237 if ( ret >= 0 ) 00238 return String(name.machine); 00239 else // can't happen 00240 return String("x86"); 00241 } 00242 case _Plugins: 00243 return Value(new Plugins(exec)); 00244 case _MimeTypes: 00245 return Value(new MimeTypes(exec)); 00246 case CookieEnabled: 00247 return Boolean(true); 00248 default: 00249 kdDebug(6070) << "WARNING: Unhandled token in DOMEvent::getValueProperty : " << token << endl; 00250 return Value(); 00251 } 00252 } 00253 00254 /*******************************************************************/ 00255 00256 PluginBase::PluginBase(ExecState *exec) 00257 : ObjectImp(exec->interpreter()->builtinObjectPrototype() ) 00258 { 00259 if ( !plugins ) { 00260 plugins = new QPtrList<PluginInfo>; 00261 mimes = new QPtrList<MimeClassInfo>; 00262 plugins->setAutoDelete( true ); 00263 mimes->setAutoDelete( true ); 00264 00265 // FIXME: add domain support here 00266 KConfig kc("konquerorrc", true); 00267 if (!KConfigGroup(&kc, "Java/JavaScript Settings").readBoolEntry("EnablePlugins", true)) 00268 return; // plugins disabled 00269 00270 // read in using KTrader 00271 KTrader::OfferList offers = KTrader::self()->query("Browser/View"); 00272 KTrader::OfferList::iterator it; 00273 for ( it = offers.begin(); it != offers.end(); ++it ) { 00274 00275 QVariant pluginsinfo = (**it).property( "X-KDE-BrowserView-PluginsInfo" ); 00276 if ( !pluginsinfo.isValid() ) { 00277 // <backwards compatible> 00278 if ((**it).library() == QString("libnsplugin")) 00279 pluginsinfo = QVariant("nsplugins/pluginsinfo"); 00280 else 00281 // </backwards compatible> 00282 continue; 00283 } 00284 // read configuration 00285 KConfig kc( locate ("data", pluginsinfo.toString()) ); 00286 unsigned num = (unsigned int) kc.readNumEntry("number"); 00287 for ( unsigned n = 0; n < num; n++ ) { 00288 kc.setGroup( QString::number(n) ); 00289 PluginInfo *plugin = new PluginInfo; 00290 00291 plugin->name = kc.readEntry("name"); 00292 plugin->file = kc.readPathEntry("file"); 00293 plugin->desc = kc.readEntry("description"); 00294 00295 plugins->append( plugin ); 00296 00297 // get mime types from string 00298 QStringList types = QStringList::split( ';', kc.readEntry("mime") ); 00299 QStringList::Iterator type; 00300 for ( type=types.begin(); type!=types.end(); ++type ) { 00301 00302 // get mime information 00303 QStringList tokens = QStringList::split(':', *type, true); 00304 if ( tokens.count() < 3 ) // we need 3 items 00305 continue; 00306 00307 MimeClassInfo *mime = new MimeClassInfo; 00308 QStringList::Iterator token = tokens.begin(); 00309 mime->type = (*token).lower(); 00310 //kdDebug(6070) << "mime->type=" << mime->type << endl; 00311 ++token; 00312 00313 mime->suffixes = *token; 00314 ++token; 00315 00316 mime->desc = *token; 00317 ++token; 00318 00319 mime->plugin = plugin; 00320 00321 mimes->append( mime ); 00322 plugin->mimes.append( mime ); 00323 00324 } 00325 } 00326 } 00327 } 00328 00329 m_refCount++; 00330 } 00331 00332 PluginBase::~PluginBase() 00333 { 00334 m_refCount--; 00335 if ( m_refCount==0 ) { 00336 delete plugins; 00337 delete mimes; 00338 plugins = 0; 00339 mimes = 0; 00340 } 00341 } 00342 00343 00344 /*******************************************************************/ 00345 IMPLEMENT_PROTOFUNC_DOM(PluginsFunc) 00346 00347 Value Plugins::get(ExecState *exec, const Identifier &propertyName) const 00348 { 00349 #ifdef KJS_VERBOSE 00350 kdDebug(6070) << "Plugins::get " << propertyName.qstring() << endl; 00351 #endif 00352 if (propertyName == "refresh") 00353 return lookupOrCreateFunction<PluginsFunc>(exec,propertyName,this,0,0,DontDelete|Function); 00354 else if ( propertyName ==lengthPropertyName ) 00355 return Number(plugins->count()); 00356 else { 00357 00358 // plugins[#] 00359 bool ok; 00360 unsigned int i = propertyName.toULong(&ok); 00361 if( ok && i<plugins->count() ) 00362 return Value( new Plugin( exec, plugins->at(i) ) ); 00363 00364 // plugin[name] 00365 for ( PluginInfo *pl = plugins->first(); pl!=0; pl = plugins->next() ) { 00366 if ( pl->name==propertyName.string() ) 00367 return Value( new Plugin( exec, pl ) ); 00368 } 00369 } 00370 00371 return PluginBase::get(exec, propertyName); 00372 } 00373 00374 /*******************************************************************/ 00375 00376 Value MimeTypes::get(ExecState *exec, const Identifier &propertyName) const 00377 { 00378 #ifdef KJS_VERBOSE 00379 kdDebug(6070) << "MimeTypes::get " << propertyName.qstring() << endl; 00380 #endif 00381 if( propertyName==lengthPropertyName ) 00382 return Number( mimes->count() ); 00383 else { 00384 00385 // mimeTypes[#] 00386 bool ok; 00387 unsigned int i = propertyName.toULong(&ok); 00388 if( ok && i<mimes->count() ) 00389 return Value( new MimeType( exec, mimes->at(i) ) ); 00390 00391 // mimeTypes[name] 00392 //kdDebug(6070) << "MimeTypes[" << propertyName.ascii() << "]" << endl; 00393 for ( MimeClassInfo *m=mimes->first(); m!=0; m=mimes->next() ) { 00394 //kdDebug(6070) << "m->type=" << m->type.ascii() << endl; 00395 if ( m->type == propertyName.string() ) 00396 return Value( new MimeType( exec, m ) ); 00397 } 00398 } 00399 00400 return PluginBase::get(exec, propertyName); 00401 } 00402 00403 00404 /************************************************************************/ 00405 00406 Value Plugin::get(ExecState *exec, const Identifier &propertyName) const 00407 { 00408 #ifdef KJS_VERBOSE 00409 kdDebug(6070) << "Plugin::get " << propertyName.qstring() << endl; 00410 #endif 00411 if ( propertyName=="name" ) 00412 return String( m_info->name ); 00413 else if ( propertyName == "filename" ) 00414 return String( m_info->file ); 00415 else if ( propertyName == "description" ) 00416 return String( m_info->desc ); 00417 else if ( propertyName == lengthPropertyName ) 00418 return Number( m_info->mimes.count() ); 00419 else { 00420 00421 // plugin[#] 00422 bool ok; 00423 unsigned int i = propertyName.toULong(&ok); 00424 //kdDebug(6070) << "Plugin::get plugin[" << i << "]" << endl; 00425 if( ok && i<m_info->mimes.count() ) 00426 { 00427 //kdDebug(6070) << "returning mimetype " << m_info->mimes.at(i)->type << endl; 00428 return Value(new MimeType(exec, m_info->mimes.at(i))); 00429 } 00430 00431 // plugin["name"] 00432 for ( PluginBase::MimeClassInfo *m=m_info->mimes.first(); 00433 m!=0; m=m_info->mimes.next() ) { 00434 if ( m->type==propertyName.string() ) 00435 return Value(new MimeType(exec, m)); 00436 } 00437 00438 } 00439 00440 return ObjectImp::get(exec,propertyName); 00441 } 00442 00443 00444 /*****************************************************************************/ 00445 00446 Value MimeType::get(ExecState *exec, const Identifier &propertyName) const 00447 { 00448 #ifdef KJS_VERBOSE 00449 kdDebug(6070) << "MimeType::get " << propertyName.qstring() << endl; 00450 #endif 00451 if ( propertyName == "type" ) 00452 return String( m_info->type ); 00453 else if ( propertyName == "suffixes" ) 00454 return String( m_info->suffixes ); 00455 else if ( propertyName == "description" ) 00456 return String( m_info->desc ); 00457 else if ( propertyName == "enabledPlugin" ) 00458 return Value(new Plugin(exec, m_info->plugin)); 00459 00460 return ObjectImp::get(exec,propertyName); 00461 } 00462 00463 00464 Value PluginsFunc::tryCall(ExecState *, Object &, const List &) 00465 { 00466 return Undefined(); 00467 } 00468 00469 00470 Value NavigatorFunc::tryCall(ExecState *exec, Object &thisObj, const List &) 00471 { 00472 KJS_CHECK_THIS( KJS::Navigator, thisObj ); 00473 Navigator *nav = static_cast<Navigator *>(thisObj.imp()); 00474 // javaEnabled() 00475 return Boolean(nav->part()->javaEnabled()); 00476 }
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 Sun Oct 10 18:56:15 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003