📄 kjs_navigator.cpp
字号:
// -*- c-basic-offset: 2 -*-/* * This file is part of the KDE libraries * Copyright (C) 2000 Harri Porten (porten@kde.org) * Copyright (c) 2000 Daniel Molkentin (molkentin@kde.org) * Copyright (c) 2000 Stefan Schimanski (schimmi@kde.org) * Copyright (C) 2003 Apple Computer, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */#include <klocale.h>#include <kstandarddirs.h>#include <kconfig.h>#include <kdebug.h>#include <kio/kprotocolmanager.h>#include <kio/kmimetype.h>#include <kio/kservice.h>#include <kio/ktrader.h>#include "kjs_navigator.h"#include "kjs/lookup.h"#include "kjs_binding.h"#include "khtml_part.h"#include <sys/utsname.h>#include "kjs_navigator.lut.h"using namespace KJS;namespace KJS { // All objects that need plugin info must inherit from PluginBase // Its ctor and dtor take care of the refcounting on the static lists. class PluginBase : public ObjectImp { public: PluginBase(ExecState *exec); virtual ~PluginBase(); struct MimeClassInfo; struct PluginInfo; struct MimeClassInfo { QString type; QString desc; QString suffixes; PluginInfo *plugin; }; struct PluginInfo { QString name; QString file; QString desc; QPtrList<MimeClassInfo> mimes; }; static QPtrList<PluginInfo> *plugins; static QPtrList<MimeClassInfo> *mimes; private: static int m_refCount; }; class Plugins : public PluginBase { public: Plugins(ExecState *exec) : PluginBase(exec) {}; virtual Value get(ExecState *exec, const Identifier &propertyName) const; Value getValueProperty(ExecState *exec, int token) const; virtual const ClassInfo* classInfo() const { return &info; } static const ClassInfo info; Value pluginByName( ExecState* exec, const QString& name ) const; }; class MimeTypes : public PluginBase { public: MimeTypes(ExecState *exec) : PluginBase(exec) { }; virtual Value get(ExecState *exec, const Identifier &propertyName) const; virtual const ClassInfo* classInfo() const { return &info; } static const ClassInfo info; Value getValueProperty(ExecState *exec, int token) const; Value mimeTypeByName( ExecState* exec, const QString& name ) const; }; class Plugin : public PluginBase { public: Plugin( ExecState *exec, PluginBase::PluginInfo *info ) : PluginBase( exec ) { m_info = info; }; virtual Value get(ExecState *exec, const Identifier &propertyName) const; virtual const ClassInfo* classInfo() const { return &info; } static const ClassInfo info; Value mimeByName(ExecState* exec, const QString& name ) const; Value getValueProperty(ExecState *exec, int token) const; PluginBase::PluginInfo *pluginInfo() const { return m_info; } private: PluginBase::PluginInfo *m_info; }; class MimeType : public PluginBase { public: MimeType( ExecState *exec, PluginBase::MimeClassInfo *info ) : PluginBase( exec ) { m_info = info; }; virtual Value get(ExecState *exec, const Identifier &propertyName) const; virtual const ClassInfo* classInfo() const { return &info; } static const ClassInfo info; Value getValueProperty(ExecState *exec, int token) const; private: PluginBase::MimeClassInfo *m_info; };}QPtrList<PluginBase::PluginInfo> *KJS::PluginBase::plugins = 0;QPtrList<PluginBase::MimeClassInfo> *KJS::PluginBase::mimes = 0;int KJS::PluginBase::m_refCount = 0;const ClassInfo Navigator::info = { "Navigator", 0, &NavigatorTable, 0 };/*@begin NavigatorTable 12 appCodeName Navigator::AppCodeName DontDelete|ReadOnly appName Navigator::AppName DontDelete|ReadOnly appVersion Navigator::AppVersion DontDelete|ReadOnly language Navigator::Language DontDelete|ReadOnly userAgent Navigator::UserAgent DontDelete|ReadOnly userLanguage Navigator::UserLanguage DontDelete|ReadOnly browserLanguage Navigator::BrowserLanguage DontDelete|ReadOnly platform Navigator::Platform DontDelete|ReadOnly cpuClass Navigator::CpuClass DontDelete|ReadOnly plugins Navigator::_Plugins DontDelete|ReadOnly mimeTypes Navigator::_MimeTypes DontDelete|ReadOnly product Navigator::Product DontDelete|ReadOnly vendor Navigator::Vendor DontDelete|ReadOnly productSub Navigator::ProductSub DontDelete|ReadOnly cookieEnabled Navigator::CookieEnabled DontDelete|ReadOnly javaEnabled Navigator::JavaEnabled DontDelete|Function 0@end*/IMPLEMENT_PROTOFUNC_DOM(NavigatorFunc)Navigator::Navigator(ExecState *exec, KHTMLPart *p) : ObjectImp(exec->interpreter()->builtinObjectPrototype()), m_part(p) { }Value Navigator::get(ExecState *exec, const Identifier &propertyName) const{#ifdef KJS_VERBOSE kdDebug(6070) << "Navigator::get " << propertyName.ascii() << endl;#endif return lookupGet<NavigatorFunc,Navigator,ObjectImp>(exec,propertyName,&NavigatorTable,this);}Value Navigator::getValueProperty(ExecState *exec, int token) const{ KURL url = m_part->url(); QString userAgent = url.host(); if (userAgent.isEmpty()) userAgent = "localhost"; userAgent = KProtocolManager::userAgentForHost(userAgent); switch (token) { case AppCodeName: return String("Mozilla"); case AppName: // If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape if (userAgent.find(QString::fromLatin1("Mozilla")) >= 0 && userAgent.find(QString::fromLatin1("compatible")) == -1) { //kdDebug() << "appName -> Mozilla" << endl; return String("Netscape"); } if (userAgent.find(QString::fromLatin1("Microsoft")) >= 0 || userAgent.find(QString::fromLatin1("MSIE")) >= 0) { //kdDebug() << "appName -> IE" << endl; return String("Microsoft Internet Explorer"); } //kdDebug() << "appName -> Konqueror" << endl; return String("Konqueror"); case AppVersion: // We assume the string is something like Mozilla/version (properties) return String(userAgent.mid(userAgent.find('/') + 1)); case Product: // We are pretending to be Mozilla or Safari if (userAgent.find(QString::fromLatin1("Mozilla")) >= 0 && userAgent.find(QString::fromLatin1("compatible")) == -1) { return String("Gecko"); } // When spoofing as IE, we use Undefined(). if (userAgent.find(QString::fromLatin1("Microsoft")) >= 0 || userAgent.find(QString::fromLatin1("MSIE")) >= 0) { return Undefined(); } // We are acting straight return String("Konqueror/khtml"); case ProductSub: { int ix = userAgent.find("Gecko"); if (ix >= 0 && userAgent.length() >= (uint)ix+14 && userAgent.unicode()[ix+5] == QChar('/') && userAgent.find(QRegExp("\\d{8}"), ix+6) == ix+6) { // We have Gecko/<productSub> in the UA string return String(userAgent.mid(ix+6, 8)); } else if (ix >= 0) { return String("20040107"); } } return Undefined(); case Vendor: return String("KDE"); case BrowserLanguage: case Language: case UserLanguage: return String(KGlobal::locale()->language()); case UserAgent: return String(userAgent); case Platform: // yet another evil hack, but necessary to spoof some sites... if ( (userAgent.find(QString::fromLatin1("Win"),0,false)>=0) ) return String(QString::fromLatin1("Win32")); else if ( (userAgent.find(QString::fromLatin1("Macintosh"),0,false)>=0) || (userAgent.find(QString::fromLatin1("Mac_PowerPC"),0,false)>=0) ) return String(QString::fromLatin1("MacPPC")); else { struct utsname name; int ret = uname(&name); if ( ret >= 0 ) return String(QString::fromLatin1("%1 %1 X11").arg(name.sysname).arg(name.machine)); else // can't happen return String(QString::fromLatin1("Unix X11")); } case CpuClass: { struct utsname name; int ret = uname(&name); if ( ret >= 0 ) return String(name.machine); else // can't happen return String("x86"); } case _Plugins: return Value(new Plugins(exec)); case _MimeTypes: return Value(new MimeTypes(exec)); case CookieEnabled: return Boolean(true); /// ##### FIXME default: kdDebug(6070) << "WARNING: Unhandled token in DOMEvent::getValueProperty : " << token << endl; return Value(); }}/*******************************************************************/PluginBase::PluginBase(ExecState *exec) : ObjectImp(exec->interpreter()->builtinObjectPrototype() ){ if ( !plugins ) { plugins = new QPtrList<PluginInfo>; mimes = new QPtrList<MimeClassInfo>; plugins->setAutoDelete( true ); mimes->setAutoDelete( true ); // FIXME: add domain support here KConfig kc("konquerorrc", true);#ifndef KONQ_EMBEDDED // This should be fixed properly XXX if (!KConfigGroup(&kc, "Java/JavaScript Settings").readBoolEntry("EnablePlugins", true)) return; // plugins disabled // read in using KTrader KTrader::OfferList offers = KTrader::self()->query("Browser/View"); KTrader::OfferList::iterator it; for ( it = offers.begin(); it != offers.end(); ++it ) { QVariant pluginsinfo = (**it).property( "X-KDE-BrowserView-PluginsInfo" ); if ( !pluginsinfo.isValid() ) { // <backwards compatible> if ((**it).library() == QString("libnsplugin")) pluginsinfo = QVariant("nsplugins/pluginsinfo"); else // </backwards compatible> continue; } // read configuration KConfig kc( locate ("data", pluginsinfo.toString()) ); unsigned num = (unsigned int) kc.readNumEntry("number"); for ( unsigned n = 0; n < num; n++ ) { kc.setGroup( QString::number(n) ); PluginInfo *plugin = new PluginInfo; plugin->name = kc.readEntry("name"); plugin->file = kc.readPathEntry("file"); plugin->desc = kc.readEntry("description"); plugins->append( plugin ); // get mime types from string QStringList types = QStringList::split( ';', kc.readEntry("mime") ); QStringList::Iterator type; for ( type=types.begin(); type!=types.end(); ++type ) { // get mime information QStringList tokens = QStringList::split(':', *type, true); if ( tokens.count() < 3 ) // we need 3 items continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -