kaddressbook

imaddresswidget.cpp

00001 /*
00002   IM address editor widget for KAddressbook
00003 
00004   Copyright (c) 2004 Will Stephenson   <lists@stevello.free-online.co.uk>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU 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     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 #include <qcheckbox.h>
00025 #include <qcombobox.h>
00026 #include <qlineedit.h>
00027 #include <qlabel.h>
00028 
00029 #include <kdebug.h>
00030 #include <kiconloader.h>
00031 #include <klocale.h>
00032 #include <kplugininfo.h>
00033 
00034 #include "imaddresswidget.h"
00035 
00036 IMAddressWidget::IMAddressWidget( QWidget *parent, QValueList<KPluginInfo *> protocols )
00037   : IMAddressBase( parent )
00038 {
00039   mProtocols = protocols;
00040   populateProtocols();
00041   init();
00042 }
00043 
00044 IMAddressWidget::IMAddressWidget( QWidget *parent, QValueList<KPluginInfo *> protocols,
00045                                   KPluginInfo *protocol, const QString& address,
00046                                   const IMContext& context )
00047   : IMAddressBase( parent )
00048 {
00049   Q_UNUSED( context );
00050 
00051   mProtocols = protocols;
00052   populateProtocols();
00053   cmbProtocol->setCurrentItem( mProtocols.findIndex( protocol ) );
00054 
00055   edtAddress->setText( address.section( QChar( 0xE120 ), 0, 0 ) );
00056   edtNetwork->setText( address.section( QChar( 0xE120 ), 1 ) );
00057 
00058   init();
00059 }
00060 
00061 void IMAddressWidget::init()
00062 {
00063   connect( cmbProtocol, SIGNAL( activated( const QString& ) ),
00064            this, SLOT( slotProtocolChanged() ) );
00065   connect( edtAddress, SIGNAL( textChanged( const QString& ) ),
00066            this, SLOT( slotAddressChanged( const QString& ) ) );
00067 
00068   slotProtocolChanged();
00069 }
00070 
00071 void IMAddressWidget::slotAddressChanged( const QString &text )
00072 {
00073   emit inValidState( !text.stripWhiteSpace().isEmpty() );
00074 }
00075 
00076 KPluginInfo * IMAddressWidget::protocol() const
00077 {
00078   int protocolIndex = cmbProtocol->currentItem();
00079 
00080   return mProtocols[ protocolIndex ];
00081 }
00082 
00083 IMContext IMAddressWidget::context() const
00084 {
00085   IMContext context = Any;
00086 /*  if ( cmbContext->currentItem() )
00087   {
00088 
00089     int contextIndex = cmbContext->currentItem();
00090     switch ( contextIndex )
00091     {
00092     case 0:
00093       context = Any;
00094       break;
00095     case 1:
00096       context = Home;
00097       break;
00098     case 2:
00099       context = Work;
00100       break;
00101     }
00102   }
00103   */
00104 
00105   return context;
00106 }
00107 
00108 QString IMAddressWidget::address() const
00109 {
00110   // The protocol irc is a special case and hard coded in.
00111   // It's not nice, but the simplest way that I can see.
00112   if ( protocol()->name() == "IRC" && !edtNetwork->text().stripWhiteSpace().isEmpty() )
00113     return edtAddress->text().stripWhiteSpace() + QChar( 0xE120 ) + edtNetwork->text().stripWhiteSpace();
00114   else
00115     return edtAddress->text().stripWhiteSpace();
00116 }
00117 
00118 void IMAddressWidget::populateProtocols()
00119 {
00120   // insert the protocols in order
00121   QValueList<KPluginInfo *>::ConstIterator it;
00122   for ( it = mProtocols.begin(); it != mProtocols.end(); ++it )
00123     cmbProtocol->insertItem( SmallIcon( (*it)->icon() ), (*it)->name() );
00124 }
00125 
00126 void IMAddressWidget::slotProtocolChanged()
00127 {
00128   if ( protocol()->name() == "IRC" ) {
00129     edtNetwork->show();
00130     labelNetwork->show();
00131   } else {
00132     edtNetwork->hide();
00133     labelNetwork->hide();
00134   }
00135 }
00136 
00137 #include "imaddresswidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys