khtml Library API Documentation

khtml_factory.cpp

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include "khtml_factory.h"
00022 #include "khtml_part.h"
00023 #include "khtml_settings.h"
00024 
00025 #include "css/cssstyleselector.h"
00026 #include "html/html_imageimpl.h"
00027 #include "rendering/render_style.h"
00028 #include "misc/loader.h"
00029 #include "misc/arena.h"
00030 
00031 #include <kinstance.h>
00032 #include <kaboutdata.h>
00033 #include <klocale.h>
00034 
00035 #include <assert.h>
00036 
00037 #include <kdebug.h>
00038 
00039 template class QPtrList<KHTMLPart>;
00040 
00041 extern "C" KDE_EXPORT void *init_libkhtml()
00042 {
00043     // We can't use a plain self() here, because that would
00044     // return the global factory, which might already exist
00045     // at the time init_libkhtml is called! As soon as someone
00046     // does new KHTMLPart() in his application and loads up
00047     // an html document into that part which either embeds
00048     // embeds another KHTMLPart instance via <object> or
00049     // as html frame, then we cannot return self(), as
00050     // what we return here is what the KLibLoader deletes
00051     // in the end, and we don't want the libloader to
00052     // delete our global instance. Anyway, the new
00053     // KHTMLFactory we create here is very cheap :)
00054     // (Simon)
00055     return new KHTMLFactory( true );
00056 }
00057 
00058 KHTMLFactory *KHTMLFactory::s_self = 0;
00059 unsigned long int KHTMLFactory::s_refcnt = 0;
00060 KInstance *KHTMLFactory::s_instance = 0;
00061 KAboutData *KHTMLFactory::s_about = 0;
00062 KHTMLSettings *KHTMLFactory::s_settings = 0;
00063 QPtrList<KHTMLPart> *KHTMLFactory::s_parts = 0;
00064 QString *KHTMLSettings::avFamilies = 0;
00065 
00066 KHTMLFactory::KHTMLFactory( bool clone )
00067 {
00068     if ( clone )
00069         ref();
00070 }
00071 
00072 KHTMLFactory::~KHTMLFactory()
00073 {
00074     if ( s_self == this )
00075     {
00076         assert( !s_refcnt );
00077 
00078         delete s_instance;
00079         delete s_about;
00080         delete s_settings;
00081     delete KHTMLSettings::avFamilies;
00082         if ( s_parts )
00083         {
00084             assert( s_parts->isEmpty() );
00085             delete s_parts;
00086         }
00087 
00088         s_instance = 0;
00089         s_about = 0;
00090         s_settings = 0;
00091         s_parts = 0;
00092     KHTMLSettings::avFamilies = 0;
00093 
00094         // clean up static data
00095         khtml::CSSStyleSelector::clear();
00096         khtml::RenderStyle::cleanup();
00097         khtml::Cache::clear();
00098         khtml::ArenaFinish();
00099     }
00100     else
00101         deref();
00102 }
00103 
00104 KParts::Part *KHTMLFactory::createPartObject( QWidget *parentWidget, const char *widgetName, QObject *parent, const char *name, const char *className, const QStringList & )
00105 {
00106   KHTMLPart::GUIProfile prof = KHTMLPart::DefaultGUI;
00107   if ( strcmp( className, "Browser/View" ) == 0 )
00108     prof = KHTMLPart::BrowserViewGUI;
00109 
00110   return new KHTMLPart( parentWidget, widgetName, parent, name, prof );
00111 }
00112 
00113 void KHTMLFactory::ref()
00114 {
00115     if ( !s_refcnt && !s_self )
00116     {
00117         // we can't use a staticdeleter here, because that would mean
00118         // that the factory gets deleted from within a qPostRoutine, called
00119         // from the QApplication destructor. That however is too late, because
00120         // we want to destruct a KInstance object, which involves destructing
00121         // a KConfig object, which might call KGlobal::dirs() (in sync()) which
00122         // probably is not going to work ;-)
00123         // well, perhaps I'm wrong here, but as I'm unsure I try to stay on the
00124         // safe side ;-) -> let's use a simple reference counting scheme
00125         // (Simon)
00126         s_self = new KHTMLFactory;
00127         khtml::Cache::init();
00128     }
00129 
00130     s_refcnt++;
00131 }
00132 
00133 void KHTMLFactory::deref()
00134 {
00135     if ( !--s_refcnt && s_self )
00136     {
00137         delete s_self;
00138         s_self = 0;
00139     }
00140 }
00141 
00142 void KHTMLFactory::registerPart( KHTMLPart *part )
00143 {
00144     if ( !s_parts )
00145         s_parts = new QPtrList<KHTMLPart>;
00146 
00147     if ( !s_parts->containsRef( part ) )
00148     {
00149         s_parts->append( part );
00150         ref();
00151     }
00152 }
00153 
00154 void KHTMLFactory::deregisterPart( KHTMLPart *part )
00155 {
00156     assert( s_parts );
00157 
00158     if ( s_parts->removeRef( part ) )
00159     {
00160         if ( s_parts->isEmpty() )
00161         {
00162             delete s_parts;
00163             s_parts = 0;
00164         }
00165         deref();
00166     }
00167 }
00168 
00169 KInstance *KHTMLFactory::instance()
00170 {
00171   assert( s_self );
00172 
00173   if ( !s_instance )
00174   {
00175     s_about = new KAboutData( "khtml", I18N_NOOP( "KHTML" ), "4.0",
00176                               I18N_NOOP( "Embeddable HTML component" ),
00177                               KAboutData::License_LGPL );
00178     s_about->addAuthor( "Lars Knoll", 0, "knoll@kde.org" );
00179     s_about->addAuthor( "Antti Koivisto", 0, "koivisto@kde.org" );
00180     s_about->addAuthor( "Waldo Bastian", 0, "bastian@kde.org" );
00181     s_about->addAuthor( "Dirk Mueller", 0, "mueller@kde.org" );
00182     s_about->addAuthor( "Peter Kelly", 0, "pmk@kde.org" );
00183     s_about->addAuthor( "Torben Weis", 0, "weis@kde.org" );
00184     s_about->addAuthor( "Martin Jones", 0, "mjones@kde.org" );
00185     s_about->addAuthor( "Simon Hausmann", 0, "hausmann@kde.org" );
00186     s_about->addAuthor( "Tobias Anton", 0, "anton@stud.fbi.fh-darmstadt.de" );
00187 
00188     s_instance = new KInstance( s_about );
00189   }
00190 
00191   return s_instance;
00192 }
00193 
00194 KHTMLSettings *KHTMLFactory::defaultHTMLSettings()
00195 {
00196   assert( s_self );
00197   if ( !s_settings )
00198     s_settings = new KHTMLSettings();
00199 
00200   return s_settings;
00201 }
00202 
00203 using namespace KParts;
00204 #include "khtml_factory.moc"
00205 
KDE Logo
This file is part of the documentation for khtml Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:22:18 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003