fix logic
[personal-kdelibs.git] / khtml / khtml_global.cpp
blobeeba02edf0edf50af0f0332b021b15ae2956b496
1 /* This file is part of the KDE project
3 * Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
4 * Copyright (C) 2007 David Faure <faure@kde.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "khtml_global.h"
23 #include "khtml_part.h"
24 #include "khtml_settings.h"
26 #include "css/cssstyleselector.h"
27 #include "css/css_mediaquery.h"
28 #include "html/html_imageimpl.h"
29 #include "rendering/render_style.h"
30 #include "rendering/break_lines.h"
31 #include "misc/loader.h"
32 #include "misc/arena.h"
33 #include "misc/paintbuffer.h"
35 #include <QtCore/QLinkedList>
37 #include <kcomponentdata.h>
38 #include <kiconloader.h>
39 #include <kaboutdata.h>
40 #include <klocale.h>
42 #include <assert.h>
44 #include <kdebug.h>
46 // SVG
47 #include "svg/SVGNames.h"
49 KHTMLGlobal *KHTMLGlobal::s_self = 0;
50 unsigned long int KHTMLGlobal::s_refcnt = 0;
51 KComponentData *KHTMLGlobal::s_componentData = 0;
52 KIconLoader *KHTMLGlobal::s_iconLoader = 0;
53 KAboutData *KHTMLGlobal::s_about = 0;
54 KHTMLSettings *KHTMLGlobal::s_settings = 0;
56 static QLinkedList<KHTMLPart*> *s_parts = 0;
57 static QLinkedList<DOM::DocumentImpl*> *s_docs = 0;
59 KHTMLGlobal::KHTMLGlobal()
61 assert(!s_self);
62 s_self = this;
63 ref();
65 khtml::Cache::init();
67 khtml::NamespaceFactory::initIdTable();
68 khtml::PrefixFactory::initIdTable();
69 khtml::LocalNameFactory::initIdTable();
70 DOM::emptyLocalName = DOM::LocalName::fromId(0);
71 DOM::emptyPrefixName = DOM::PrefixName::fromId(0);
72 DOM::emptyNamespaceName = DOM::NamespaceName::fromId(0);
73 WebCore::SVGNames::init();
76 KHTMLGlobal::~KHTMLGlobal()
78 //kDebug(6000) << this;
79 if ( s_self == this )
81 finalCheck();
82 delete s_iconLoader;
83 delete s_componentData;
84 delete s_about;
85 delete s_settings;
86 delete KHTMLSettings::avFamilies;
87 if (s_parts) {
88 assert(s_parts->isEmpty());
89 delete s_parts;
91 if (s_docs) {
92 assert(s_docs->isEmpty());
93 delete s_docs;
96 s_iconLoader = 0;
97 s_componentData = 0;
98 s_about = 0;
99 s_settings = 0;
100 s_parts = 0;
101 s_docs = 0;
102 KHTMLSettings::avFamilies = 0;
104 // clean up static data
105 khtml::CSSStyleSelector::clear();
106 khtml::RenderStyle::cleanup();
107 khtml::RenderObject::cleanup();
108 khtml::PaintBuffer::cleanup();
109 khtml::MediaQueryEvaluator::cleanup();
110 khtml::Cache::clear();
111 khtml::cleanup_thaibreaks();
112 khtml::ArenaFinish();
114 else
115 deref();
118 void KHTMLGlobal::ref()
120 if ( !s_refcnt && !s_self )
122 //kDebug(6000) << "Creating KHTMLGlobal instance";
123 // we can't use a staticdeleter here, because that would mean
124 // that the KHTMLGlobal instance gets deleted from within a qPostRoutine, called
125 // from the QApplication destructor. That however is too late, because
126 // we want to destruct a KComponentData object, which involves destructing
127 // a KConfig object, which might call KGlobal::dirs() (in sync()) which
128 // probably is not going to work ;-)
129 // well, perhaps I'm wrong here, but as I'm unsure I try to stay on the
130 // safe side ;-) -> let's use a simple reference counting scheme
131 // (Simon)
132 new KHTMLGlobal; // does initial ref()
133 } else {
134 ++s_refcnt;
136 //kDebug(6000) << "s_refcnt=" << s_refcnt;
139 void KHTMLGlobal::deref()
141 //kDebug(6000) << "s_refcnt=" << s_refcnt - 1;
142 if ( !--s_refcnt && s_self )
144 delete s_self;
145 s_self = 0;
149 void KHTMLGlobal::registerPart( KHTMLPart *part )
151 //kDebug(6000) << part;
152 if ( !s_parts )
153 s_parts = new QLinkedList<KHTMLPart*>;
155 if ( !s_parts->contains( part ) ) {
156 s_parts->append( part );
157 ref();
161 void KHTMLGlobal::deregisterPart( KHTMLPart *part )
163 //kDebug(6000) << part;
164 assert( s_parts );
166 if ( s_parts->removeAll( part ) ) {
167 if ( s_parts->isEmpty() ) {
168 delete s_parts;
169 s_parts = 0;
171 deref();
175 void KHTMLGlobal::registerDocumentImpl( DOM::DocumentImpl *doc )
177 //kDebug(6000) << doc;
178 if ( !s_docs )
179 s_docs = new QLinkedList<DOM::DocumentImpl*>;
181 if ( !s_docs->contains( doc ) ) {
182 s_docs->append( doc );
183 ref();
187 void KHTMLGlobal::deregisterDocumentImpl( DOM::DocumentImpl *doc )
189 //kDebug(6000) << doc;
190 assert( s_docs );
192 if ( s_docs->removeAll( doc ) ) {
193 if ( s_docs->isEmpty() ) {
194 delete s_docs;
195 s_docs = 0;
197 deref();
201 const KComponentData &KHTMLGlobal::componentData()
203 assert( s_self );
205 if ( !s_componentData )
207 s_about = new KAboutData( "khtml", 0, ki18n( "KHTML" ), "4.0",
208 ki18n( "Embeddable HTML component" ),
209 KAboutData::License_LGPL );
210 s_about->addAuthor(ki18n("Lars Knoll"), KLocalizedString(), "knoll@kde.org");
211 s_about->addAuthor(ki18n("Antti Koivisto"), KLocalizedString(), "koivisto@kde.org");
212 s_about->addAuthor(ki18n("Waldo Bastian"), KLocalizedString(), "bastian@kde.org");
213 s_about->addAuthor(ki18n("Dirk Mueller"), KLocalizedString(), "mueller@kde.org");
214 s_about->addAuthor(ki18n("Peter Kelly"), KLocalizedString(), "pmk@kde.org");
215 s_about->addAuthor(ki18n("Torben Weis"), KLocalizedString(), "weis@kde.org");
216 s_about->addAuthor(ki18n("Martin Jones"), KLocalizedString(), "mjones@kde.org");
217 s_about->addAuthor(ki18n("Simon Hausmann"), KLocalizedString(), "hausmann@kde.org");
218 s_about->addAuthor(ki18n("Tobias Anton"), KLocalizedString(), "anton@stud.fbi.fh-darmstadt.de");
220 s_componentData = new KComponentData( s_about );
223 return *s_componentData;
226 KIconLoader *KHTMLGlobal::iconLoader()
228 if ( !s_iconLoader )
230 s_iconLoader = new KIconLoader(componentData().componentName(), componentData().dirs());
233 return s_iconLoader;
236 KHTMLSettings *KHTMLGlobal::defaultHTMLSettings()
238 assert( s_self );
239 if ( !s_settings )
240 s_settings = new KHTMLSettings();
242 return s_settings;
245 void KHTMLGlobal::finalCheck()
247 //kDebug(6000) << "s_refcnt=" << s_refcnt;
248 if (s_refcnt) {
249 if (s_parts && !s_parts->isEmpty()) {
250 kWarning(6000) << s_parts->count() << "parts not deleted";
251 kWarning(6000) << "Part" << s_parts->first() << "wasn't deleted";
253 if (s_docs && !s_docs->isEmpty()) {
254 kWarning(6000) << s_docs->count() << "docs not deleted";
255 kWarning(6000) << "Document" << s_docs->first() << "wasn't deleted";
258 assert( !s_refcnt );