kmail

isubject.cpp

00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include "isubject.h"
00006 #include "interfaces/observer.h"
00007 
00008 #include <qtl.h>
00009 
00010 #include <kdebug.h>
00011 
00012 namespace KMail {
00013 
00014   ISubject::~ISubject()
00015   {
00016     mObserverList.clear();
00017   }
00018 
00019   void ISubject::attach( Interface::Observer * pObserver )
00020   {
00021     if ( qFind( mObserverList.begin(), mObserverList.end(), pObserver ) == mObserverList.end() )
00022       mObserverList.push_back( pObserver );
00023   }
00024 
00025   void ISubject::detach( Interface::Observer * pObserver ) {
00026     QValueVector<Interface::Observer*>::iterator it = qFind( mObserverList.begin(), mObserverList.end(), pObserver );
00027     if ( it != mObserverList.end() )
00028       mObserverList.erase( it );
00029   }
00030 
00031   void ISubject::notify()
00032   {
00033     kdDebug(5006) << "ISubject::notify " << mObserverList.size() << endl;
00034 
00035     // iterate over a copy (to prevent crashes when
00036     // {attach(),detach()} is called from an Observer::update()
00037     const QValueVector<Interface::Observer*> copy = mObserverList;
00038     for ( QValueVector<Interface::Observer*>::const_iterator it = copy.begin() ; it != copy.end() ; ++it ) {
00039       if ( (*it) ) {
00040         (*it)->update( this );
00041       }
00042     }
00043   }
00044 
00045 }
00046 
KDE Home | KDE Accessibility Home | Description of Access Keys