libkpimidentities

identitycombo.cpp

00001 /*  -*- c++ -*-
00002     identitycombo.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2002 Marc Mutz <mutz@kde.org>
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMail is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
00030 */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035 
00036 #include "identitycombo.h"
00037 #include "identity.h"
00038 #include "identitymanager.h"
00039 
00040 #include <klocale.h>
00041 
00042 #include <assert.h>
00043 
00044 using namespace KPIM;
00045 
00046 IdentityCombo::IdentityCombo( IdentityManager* manager, QWidget * parent, const char * name )
00047   : QComboBox( false, parent, name ), mIdentityManager( manager )
00048 {
00049   reloadCombo();
00050   reloadUoidList();
00051   connect( this, SIGNAL(activated(int)), SLOT(slotEmitChanged(int)) );
00052   connect( manager, SIGNAL(changed()),
00053        SLOT(slotIdentityManagerChanged()) );
00054 }
00055 
00056 QString IdentityCombo::currentIdentityName() const {
00057   return mIdentityManager->identities()[ currentItem() ];
00058 }
00059 
00060 uint IdentityCombo::currentIdentity() const {
00061   return mUoidList[ currentItem() ];
00062 }
00063 
00064 void IdentityCombo::setCurrentIdentity( const Identity & identity ) {
00065   setCurrentIdentity( identity.uoid() );
00066 }
00067 
00068 void IdentityCombo::setCurrentIdentity( const QString & name ) {
00069   int idx = mIdentityManager->identities().findIndex( name );
00070   if ( idx < 0 ) return;
00071   if ( idx == currentItem() ) return;
00072 
00073   blockSignals( true ); // just in case Qt gets fixed to emit activated() here
00074   setCurrentItem( idx );
00075   blockSignals( false );
00076 
00077   slotEmitChanged( idx );
00078 }
00079 
00080 void IdentityCombo::setCurrentIdentity( uint uoid ) {
00081   int idx = mUoidList.findIndex( uoid );
00082   if ( idx < 0 ) return;
00083   if ( idx == currentItem() ) return;
00084 
00085   blockSignals( true ); // just in case Qt gets fixed to emit activated() here
00086   setCurrentItem( idx );
00087   blockSignals( false );
00088 
00089   slotEmitChanged( idx );
00090 }
00091 
00092 void IdentityCombo::reloadCombo() {
00093   QStringList identities = mIdentityManager->identities();
00094   // the IM should prevent this from happening:
00095   assert( !identities.isEmpty() );
00096   identities.first() = i18n("%1 (Default)").arg( identities.first() );
00097   clear();
00098   insertStringList( identities );
00099 }
00100 
00101 void IdentityCombo::reloadUoidList() {
00102   mUoidList.clear();
00103   IdentityManager::ConstIterator it;
00104   for ( it = mIdentityManager->begin() ; it != mIdentityManager->end() ; ++it )
00105     mUoidList << (*it).uoid();
00106 }
00107 
00108 void IdentityCombo::slotIdentityManagerChanged() {
00109   uint oldIdentity = mUoidList[ currentItem() ];
00110 
00111   reloadUoidList();
00112   int idx = mUoidList.findIndex( oldIdentity );
00113 
00114   blockSignals( true );
00115   reloadCombo();
00116   setCurrentItem( idx < 0 ? 0 : idx );
00117   blockSignals( false );
00118 
00119   if ( idx < 0 )
00120     // apparently our oldIdentity got deleted:
00121     slotEmitChanged( currentItem() );
00122 }
00123 
00124 void IdentityCombo::slotEmitChanged( int idx ) {
00125   emit identityChanged( mIdentityManager->identities()[idx] );
00126   emit identityChanged( mUoidList[idx] );
00127 }
00128 
00129 #include "identitycombo.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys