kaddressbook

kaddressbookview.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qlayout.h>
00025 #include <qpopupmenu.h>
00026 
00027 #include <kabc/addressbook.h>
00028 #include <kabc/distributionlistdialog.h>
00029 #include <kconfig.h>
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <kxmlguifactory.h>
00033 #include <kxmlguiclient.h>
00034 
00035 #include "core.h"
00036 #include "searchmanager.h"
00037 
00038 #include "kaddressbookview.h"
00039 
00040 KAddressBookView::KAddressBookView( KAB::Core *core, QWidget *parent,
00041                                     const char *name )
00042     : QWidget( parent, name ), mCore( core ), mFieldList()
00043 {
00044   initGUI();
00045 
00046   connect( mCore->searchManager(), SIGNAL( contactsUpdated() ),
00047            SLOT( updateView() ) );
00048 }
00049 
00050 KAddressBookView::~KAddressBookView()
00051 {
00052   kdDebug(5720) << "KAddressBookView::~KAddressBookView: destroying - "
00053                 << name() << endl;
00054 }
00055 
00056 void KAddressBookView::readConfig( KConfig *config )
00057 {
00058   mFieldList = KABC::Field::restoreFields( config, "KABCFields" );
00059 
00060   if ( mFieldList.isEmpty() )
00061     mFieldList = KABC::Field::defaultFields();
00062 
00063   mDefaultFilterType = (DefaultFilterType)config->readNumEntry( "DefaultFilterType", 1 );
00064   mDefaultFilterName = config->readEntry( "DefaultFilterName" );
00065 }
00066 
00067 void KAddressBookView::writeConfig( KConfig* )
00068 {
00069   // Most of writing the config is handled by the ConfigureViewDialog
00070 }
00071 
00072 QString KAddressBookView::selectedEmails()
00073 {
00074   bool first = true;
00075   QString emailAddrs;
00076   const QStringList uidList = selectedUids();
00077   KABC::Addressee addr;
00078   QString email;
00079 
00080   QStringList::ConstIterator it;
00081   for ( it = uidList.begin(); it != uidList.end(); ++it ) {
00082     addr = mCore->addressBook()->findByUid( *it );
00083 
00084     if ( !addr.isEmpty() ) {
00085       QString m = QString::null;
00086 
00087       if ( addr.emails().count() > 1 )
00088         m = KABC::EmailSelector::getEmail( addr.emails(), addr.preferredEmail(), this );
00089 
00090       email = addr.fullEmail( m );
00091 
00092       if ( !first )
00093         emailAddrs += ", ";
00094       else
00095         first = false;
00096 
00097       emailAddrs += email;
00098     }
00099   }
00100 
00101   return emailAddrs;
00102 }
00103 
00104 KABC::Addressee::List KAddressBookView::addressees()
00105 {
00106   if ( mFilter.isEmpty() )
00107     return mCore->searchManager()->contacts();
00108 
00109   KABC::Addressee::List addresseeList;
00110   const KABC::Addressee::List contacts = mCore->searchManager()->contacts();
00111 
00112   KABC::Addressee::List::ConstIterator it;
00113   KABC::Addressee::List::ConstIterator contactsEnd( contacts.end() );
00114   for ( it = contacts.begin(); it != contactsEnd; ++it ) {
00115     if ( mFilter.filterAddressee( *it ) )
00116       addresseeList.append( *it );
00117   }
00118 
00119   return addresseeList;
00120 }
00121 
00122 void KAddressBookView::initGUI()
00123 {
00124   // Create the layout
00125   QVBoxLayout *layout = new QVBoxLayout( this );
00126 
00127   // Add the view widget
00128   mViewWidget = new QWidget( this );
00129   layout->addWidget( mViewWidget );
00130 }
00131 
00132 KABC::Field::List KAddressBookView::fields() const
00133 {
00134   return mFieldList;
00135 }
00136 
00137 void KAddressBookView::setFilter( const Filter &filter )
00138 {
00139   mFilter = filter;
00140 }
00141 
00142 KAddressBookView::DefaultFilterType KAddressBookView::defaultFilterType() const
00143 {
00144   return mDefaultFilterType;
00145 }
00146 
00147 const QString &KAddressBookView::defaultFilterName() const
00148 {
00149   return mDefaultFilterName;
00150 }
00151 
00152 KAB::Core *KAddressBookView::core() const
00153 {
00154   return mCore;
00155 }
00156 
00157 void KAddressBookView::popup( const QPoint &point )
00158 {
00159   if ( !mCore->guiClient() ) {
00160     kdWarning() << "No GUI client set!" << endl;
00161     return;
00162   }
00163 
00164   QPopupMenu *menu = static_cast<QPopupMenu*>( mCore->guiClient()->factory()->container( "RMBPopup",
00165                                                mCore->guiClient() ) );
00166   if ( menu )
00167     menu->popup( point );
00168 }
00169 
00170 QWidget *KAddressBookView::viewWidget()
00171 {
00172   return mViewWidget;
00173 }
00174 
00175 void KAddressBookView::updateView()
00176 {
00177   const QStringList uidList = selectedUids();
00178 
00179   refresh(); // This relists and deselects everything, in all views
00180 
00181   if ( !uidList.isEmpty() ) {
00182     // Keep previous selection
00183     QStringList::ConstIterator it, uidListEnd( uidList.end() );
00184     for ( it = uidList.begin(); it != uidListEnd; ++it )
00185       setSelected( *it, true );
00186 
00187   } else {
00188     const KABC::Addressee::List contacts = mCore->searchManager()->contacts();
00189     if ( !contacts.isEmpty() )
00190       setSelected( contacts.first().uid(), true );
00191     else
00192       emit selected( QString::null );
00193   }
00194 }
00195 
00196 ViewConfigureWidget *ViewFactory::configureWidget( KABC::AddressBook *ab,
00197                                                    QWidget *parent,
00198                                                    const char *name )
00199 {
00200   return new ViewConfigureWidget( ab, parent, name );
00201 }
00202 
00203 #include "kaddressbookview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys