khtml Library API Documentation

loader.h

00001 /*
00002     This file is part of the KDE libraries
00003 
00004     Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
00005     Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, USA.
00021 
00022     This class provides all functionality needed for loading images, style sheets and html
00023     pages from the web. It has a memory cache for these objects.
00024 */
00025 #ifndef _khtml_loader_h
00026 #define _khtml_loader_h
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <time.h>
00033 
00034 #include "loader_client.h"
00035 #ifdef HAVE_LIBJPEG
00036 #include "loader_jpeg.h"
00037 #endif
00038 
00039 #include <stdlib.h>
00040 #include <qptrlist.h>
00041 #include <qobject.h>
00042 #include <qptrdict.h>
00043 #include <qdict.h>
00044 #include <qpixmap.h>
00045 #include <qbuffer.h>
00046 #include <qstringlist.h>
00047 #include <qtextcodec.h>
00048 
00049 #include <kurl.h>
00050 #include <kio/global.h>
00051 
00052 #include <khtml_settings.h>
00053 #include <dom/dom_string.h>
00054 
00055 class QMovie;
00056 class KHTMLPart;
00057 
00058 namespace KIO {
00059   class Job;
00060   class TransferJob;
00061 }
00062 
00063 namespace DOM
00064 {
00065     class CSSStyleSheetImpl;
00066     class DocumentImpl;
00067 };
00068 
00069 namespace khtml
00070 {
00071     class CachedObject;
00072     class Request;
00073     class DocLoader;
00074 
00083     class CachedObject
00084     {
00085     public:
00086     enum Type {
00087         Image,
00088         CSSStyleSheet,
00089         Script
00090     };
00091 
00092     enum Status {
00093         NotCached,    // this URL is not cached
00094         Unknown,      // let imagecache decide what to do with it
00095         New,          // inserting new image
00096             Pending,      // only partially loaded
00097         Persistent,   // never delete this pixmap
00098         Cached,       // regular case
00099         Uncacheable   // to big to be cached,
00100     };                // will be destroyed as soon as possible
00101 
00102     CachedObject(const DOM::DOMString &url, Type type, KIO::CacheControl _cachePolicy, time_t _expireDate)
00103     {
00104         m_url = url;
00105         m_type = type;
00106         m_status = Pending;
00107         m_size = 0;
00108         m_free = false;
00109         m_cachePolicy = _cachePolicy;
00110         m_request = 0;
00111         m_expireDate = _expireDate;
00112             m_deleted = false;
00113             m_expireDateChanged = false;
00114     }
00115     virtual ~CachedObject() {
00116             if(m_deleted) abort();
00117             m_deleted = true;
00118         }
00119 
00120     virtual void data( QBuffer &buffer, bool eof) = 0;
00121     virtual void error( int err, const char *text ) = 0;
00122 
00123     const DOM::DOMString &url() const { return m_url; }
00124     Type type() const { return m_type; }
00125 
00126     virtual void ref(CachedObjectClient *consumer) = 0;
00127     virtual void deref(CachedObjectClient *consumer) = 0;
00128 
00129     int count() const { return m_clients.count(); }
00130 
00131     void setStatus(Status s) { m_status = s; }
00132     Status status() const { return m_status; }
00133 
00134     int size() const { return m_size; }
00135 
00141     virtual void finish();
00142 
00148         void setFree( bool b ) { m_free = b; }
00149 
00150         KIO::CacheControl cachePolicy() const { return m_cachePolicy; }
00151 
00152         void setRequest(Request *_request);
00153 
00154         bool canDelete() const { return (m_clients.count() == 0 && !m_request); }
00155 
00156     void setExpireDate(time_t _expireDate, bool changeHttpCache);
00157 
00158     bool isExpired() const;
00159 
00160         virtual bool schedule() const { return false; }
00161 
00165         // e.g. "text/*"
00166         QString accept() const { return m_accept; }
00167         void setAccept(const QString &_accept) { m_accept = _accept; }
00168 
00169     protected:
00170         QPtrList<CachedObjectClient> m_clients;
00171 
00172     DOM::DOMString m_url;
00173         QString m_accept;
00174         Request *m_request;
00175     Type m_type;
00176     Status m_status;
00177     int m_size;
00178     time_t m_expireDate;
00179     KIO::CacheControl m_cachePolicy;
00180         bool m_free : 1;
00181         bool m_deleted : 1;
00182         bool m_loading : 1;
00183         bool m_expireDateChanged : 1;
00184     };
00185 
00186 
00190     class CachedCSSStyleSheet : public CachedObject
00191     {
00192     public:
00193     CachedCSSStyleSheet(DocLoader* dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, time_t _expireDate, const QString& charset);
00194     CachedCSSStyleSheet(const DOM::DOMString &url, const QString &stylesheet_data);
00195     virtual ~CachedCSSStyleSheet();
00196 
00197     const DOM::DOMString &sheet() const { return m_sheet; }
00198 
00199     virtual void ref(CachedObjectClient *consumer);
00200     virtual void deref(CachedObjectClient *consumer);
00201 
00202     virtual void data( QBuffer &buffer, bool eof );
00203     virtual void error( int err, const char *text );
00204 
00205         virtual bool schedule() const { return true; }
00206 
00207     void checkNotify();
00208 
00209     protected:
00210     DOM::DOMString m_sheet;
00211         QTextCodec* m_codec;
00212     };
00213 
00217     class CachedScript : public CachedObject
00218     {
00219     public:
00220     CachedScript(DocLoader* dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, time_t _expireDate, const QString& charset);
00221     CachedScript(const DOM::DOMString &url, const QString &script_data);
00222     virtual ~CachedScript();
00223 
00224     const DOM::DOMString &script() const { return m_script; }
00225 
00226     virtual void ref(CachedObjectClient *consumer);
00227     virtual void deref(CachedObjectClient *consumer);
00228 
00229     virtual void data( QBuffer &buffer, bool eof );
00230     virtual void error( int err, const char *text );
00231 
00232         virtual bool schedule() const { return false; }
00233 
00234     void checkNotify();
00235 
00236         bool isLoaded() const { return !m_loading; }
00237 
00238     protected:
00239     DOM::DOMString m_script;
00240         QTextCodec* m_codec;
00241     };
00242 
00243     class ImageSource;
00244 
00248     class CachedImage : public QObject, public CachedObject
00249     {
00250     Q_OBJECT
00251     public:
00252     CachedImage(DocLoader* dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, time_t _expireDate);
00253     virtual ~CachedImage();
00254 
00255     const QPixmap &pixmap() const;
00256     const QPixmap &tiled_pixmap(const QColor& bg);
00257 
00258         QSize pixmap_size() const;    // returns the size of the complete (i.e. when finished) loading
00259         QRect valid_rect() const;     // returns the rectangle of pixmap that has been loaded already
00260 
00261         void ref(CachedObjectClient *consumer);
00262     virtual void deref(CachedObjectClient *consumer);
00263 
00264     virtual void data( QBuffer &buffer, bool eof );
00265     virtual void error( int err, const char *text );
00266 
00267         bool isTransparent() const { return isFullyTransparent; }
00268         bool isErrorImage() const { return errorOccured; }
00269 
00270         void setShowAnimations( KHTMLSettings::KAnimationAdvice );
00271 
00272         virtual bool schedule() const { return true; }
00273 
00274     virtual void finish();
00275 
00276     protected:
00277     void clear();
00278 
00279     private slots:
00283     void movieUpdated( const QRect &rect );
00284         void movieStatus(int);
00285         void movieResize(const QSize&);
00286         void deleteMovie();
00287 
00288     private:
00289         void do_notify(const QPixmap& p, const QRect& r);
00290 
00291     QMovie* m;
00292         QPixmap* p;
00293     QPixmap* bg;
00294         QRgb bgColor;
00295         mutable QPixmap* pixPart;
00296 
00297         ImageSource* imgSource;
00298         const char* formatType;  // Is the name of the movie format type
00299 
00300     int width;
00301     int height;
00302 
00303     // Is set if movie format type ( incremental/animation) was checked
00304     bool typeChecked : 1;
00305         bool isFullyTransparent : 1;
00306         bool errorOccured : 1;
00307         bool monochrome : 1;
00308         KHTMLSettings::KAnimationAdvice m_showAnimations : 2;
00309 
00310         friend class Cache;
00311     };
00312 
00318     class DocLoader
00319     {
00320     public:
00321     DocLoader(KHTMLPart*, DOM::DocumentImpl*);
00322     ~DocLoader();
00323 
00324     CachedImage *requestImage( const DOM::DOMString &url);
00325     CachedCSSStyleSheet *requestStyleSheet( const DOM::DOMString &url, const QString& charset);
00326         CachedScript *requestScript( const DOM::DOMString &url, const QString& charset);
00327 
00328     bool autoloadImages() const { return m_bautoloadImages; }
00329         KIO::CacheControl cachePolicy() const { return m_cachePolicy; }
00330         KHTMLSettings::KAnimationAdvice showAnimations() const { return m_showAnimations; }
00331         time_t expireDate() const { return m_expireDate; }
00332         KHTMLPart* part() const { return m_part; }
00333         DOM::DocumentImpl* doc() const { return m_doc; }
00334 
00335         void setCacheCreationDate( time_t );
00336         void setExpireDate( time_t, bool relative );
00337         void setAutoloadImages( bool );
00338         void setCachePolicy( KIO::CacheControl cachePolicy );
00339         void setShowAnimations( KHTMLSettings::KAnimationAdvice );
00340         void removeCachedObject( CachedObject*) const;
00341 
00342     private:
00343         bool needReload(const KURL &fullUrl);
00344 
00345         friend class Cache;
00346         friend class DOM::DocumentImpl;
00347 
00348         QStringList m_reloadedURLs;
00349         mutable QPtrList<CachedObject> m_docObjects;
00350     time_t m_expireDate;
00351     time_t m_creationDate;
00352     KIO::CacheControl m_cachePolicy;
00353         bool m_bautoloadImages : 1;
00354         KHTMLSettings::KAnimationAdvice m_showAnimations : 2;
00355         KHTMLPart* m_part;
00356         DOM::DocumentImpl* m_doc;
00357     };
00358 
00362     class Request
00363     {
00364     public:
00365     Request(DocLoader* dl, CachedObject *_object, bool _incremental);
00366     ~Request();
00367     bool incremental;
00368     QBuffer m_buffer;
00369     CachedObject *object;
00370         DocLoader* m_docLoader;
00371     };
00372 
00376     class Loader : public QObject
00377     {
00378     Q_OBJECT
00379 
00380     public:
00381     Loader();
00382     ~Loader();
00383 
00384     void load(DocLoader* dl, CachedObject *object, bool incremental = true);
00385 
00386         int numRequests( DocLoader* dl ) const;
00387         void cancelRequests( DocLoader* dl );
00388 
00389         // may return 0L
00390         KIO::Job *jobForRequest( const DOM::DOMString &url ) const;
00391 
00392     signals:
00393         void requestStarted( khtml::DocLoader* dl, khtml::CachedObject* obj );
00394     void requestDone( khtml::DocLoader* dl, khtml::CachedObject *obj );
00395     void requestFailed( khtml::DocLoader* dl, khtml::CachedObject *obj );
00396 
00397     protected slots:
00398     void slotFinished( KIO::Job * );
00399     void slotData( KIO::Job *, const QByteArray & );
00400     void servePendingRequests();
00401 
00402     QPtrList<Request> m_requestsPending;
00403     QPtrDict<Request> m_requestsLoading;
00404 #ifdef HAVE_LIBJPEG
00405         KJPEGFormatType m_jpegloader;
00406 #endif
00407     };
00408 
00415     class Cache
00416     {
00417     friend class DocLoader;
00418     public:
00423     static void init();
00424 
00431     static CachedImage *requestImage( DocLoader* l, const DOM::DOMString &url, bool reload=false, time_t _expireDate=0);
00432 
00437     static CachedCSSStyleSheet *requestStyleSheet( DocLoader* l, const DOM::DOMString &url, bool reload=false, time_t _expireDate=0, const QString& charset = QString::null);
00438 
00442         static void preloadStyleSheet(const QString &url, const QString &stylesheet_data);
00443 
00448     static CachedScript *requestScript( DocLoader* l, const DOM::DOMString &url, bool reload=false, time_t _expireDate=0, const QString& charset=QString::null);
00449 
00453         static void preloadScript(const QString &url, const QString &script_data);
00454 
00459     static void setSize( int bytes );
00463     static int size() { return maxSize; };
00464 
00468     static void statistics();
00469 
00473     static void flush(bool force=false);
00474 
00480     static void clear();
00481 
00482     static Loader *loader() { return m_loader; }
00483 
00484         static QPixmap *nullPixmap;
00485         static QPixmap *brokenPixmap;
00486         static int cacheSize;
00487 
00488         static void removeCacheEntry( CachedObject *object );
00489 
00490         protected:
00491     /*
00492      * @internal
00493      */
00494     class LRUList : public QStringList
00495     {
00496     public:
00501         void touch( const QString &url )
00502         {
00503         remove( url );
00504         prepend( url );
00505         }
00506     };
00507 
00508 
00509     static QDict<CachedObject> *cache;
00510     static LRUList *lru;
00511         static QPtrList<DocLoader>* docloader;
00512 
00513     static int maxSize;
00514     static int flushCount;
00515 
00516     static Loader *m_loader;
00517 
00518         static unsigned long s_ulRefCnt;
00519     };
00520 
00521 };
00522 
00523 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:42 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001