kaddressbook Library API Documentation

kaddressbooktableview.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 <qheader.h>
00026 #include <qvbox.h>
00027 #include <qlistbox.h>
00028 #include <qwidget.h>
00029 #include <qfile.h>
00030 #include <qimage.h>
00031 #include <qcombobox.h>
00032 #include <qapplication.h>
00033 #include <qdragobject.h>
00034 #include <qevent.h>
00035 #include <qurl.h>
00036 #include <qpixmap.h>
00037 
00038 #include <kabc/addressbook.h>
00039 #include <kapplication.h>
00040 #include <kconfig.h>
00041 #include <kcolorbutton.h>
00042 #include <kdebug.h>
00043 #include <kglobal.h>
00044 #include <kiconloader.h>
00045 #include <klineedit.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 #include <kurl.h>
00049 #include <kurlrequester.h>
00050 #include <libkdepim/kimproxy.h>
00051 
00052 #include "configuretableviewdialog.h"
00053 #include "contactlistview.h"
00054 #include "core.h"
00055 #include "kabprefs.h"
00056 #include "undocmds.h"
00057 
00058 #include "kaddressbooktableview.h"
00059 
00060 class TableViewFactory : public ViewFactory
00061 {
00062   public:
00063     KAddressBookView *view( KAB::Core *core, QWidget *parent, const char *name )
00064     {
00065       return new KAddressBookTableView( core, parent, name );
00066     }
00067 
00068     QString type() const { return I18N_NOOP("Table"); }
00069 
00070     QString description() const { return i18n( "A listing of contacts in a table. Each cell of "
00071                                   "the table holds a field of the contact." ); }
00072 
00073     ViewConfigureWidget *configureWidget( KABC::AddressBook *ab, QWidget *parent,
00074                                           const char *name = 0 )
00075     {
00076       return new ConfigureTableViewWidget( ab, parent, name );
00077     }
00078 };
00079 
00080 extern "C" {
00081   void *init_libkaddrbk_tableview()
00082   {
00083     return ( new TableViewFactory );
00084   }
00085 }
00086 
00087 KAddressBookTableView::KAddressBookTableView( KAB::Core *core,
00088                                               QWidget *parent, const char *name )
00089   : KAddressBookView( core, parent, name )
00090 {
00091   mainLayout = new QVBoxLayout( viewWidget(), 2 );
00092 
00093   // The list view will be created when the config is read.
00094   mListView = 0;
00095   mIMProxy = 0;
00096 }
00097 
00098 KAddressBookTableView::~KAddressBookTableView()
00099 {
00100 }
00101 
00102 void KAddressBookTableView::reconstructListView()
00103 {
00104     if (mListView)
00105     {
00106       disconnect(mListView, SIGNAL(selectionChanged()),
00107                   this, SLOT(addresseeSelected()));
00108       disconnect(mListView, SIGNAL(executed(QListViewItem*)),
00109                   this, SLOT(addresseeExecuted(QListViewItem*)));
00110       disconnect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
00111                   this, SLOT(addresseeExecuted(QListViewItem*)));
00112       disconnect(mListView, SIGNAL(startAddresseeDrag()), this,
00113                   SIGNAL(startDrag()));
00114       disconnect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
00115                   SIGNAL(dropped(QDropEvent*)));
00116       delete mListView;
00117     }
00118 
00119   mListView = new ContactListView( this, core()->addressBook(), viewWidget() );
00120   mListView->setFullWidth( true );
00121   
00122   mListView->setShowIM( mIMProxy != 0 );
00123   
00124   // Add the columns
00125   KABC::Field::List fieldList = fields();
00126   KABC::Field::List::ConstIterator it;
00127 
00128   int c = 0;
00129   for( it = fieldList.begin(); it != fieldList.end(); ++it ) {
00130       mListView->addColumn( (*it)->label() );
00131       mListView->setColumnWidthMode(c++, QListView::Manual);
00132   }
00133 
00134   if ( mListView->showIM() ) {
00135     // IM presence is added separately, because it's not a KABC field.
00136     // If you want to make this appear as the leftmost column by default, move
00137     // this block immediately before the preceding for loop
00138     // after the declaration of c.
00139     mListView->addColumn( i18n( "Presence" ) );
00140     mListView->setIMColumn( c++ );
00141   }
00142 
00143   connect(mListView, SIGNAL(selectionChanged()),
00144           this, SLOT(addresseeSelected()));
00145   connect(mListView, SIGNAL(startAddresseeDrag()), this,
00146           SIGNAL(startDrag()));
00147   connect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
00148           SIGNAL(dropped(QDropEvent*)));
00149   connect( mListView, SIGNAL( contextMenu( KListView*, QListViewItem*, const QPoint& ) ),
00150            this, SLOT( rmbClicked( KListView*, QListViewItem*, const QPoint& ) ) );
00151   connect( mListView->header(), SIGNAL( clicked(int) ),
00152            SIGNAL( sortFieldChanged() ) );
00153 
00154   if (KABPrefs::instance()->mHonorSingleClick)
00155     connect(mListView, SIGNAL(executed(QListViewItem*)),
00156           this, SLOT(addresseeExecuted(QListViewItem*)));
00157   else
00158     connect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
00159           this, SLOT(addresseeExecuted(QListViewItem*)));
00160 
00161   refresh();
00162 
00163   mListView->setSorting( 0, true );
00164   mainLayout->addWidget( mListView );
00165   mainLayout->activate();
00166   mListView->show();
00167 }
00168 
00169 KABC::Field *KAddressBookTableView::sortField() const
00170 {
00171   // we have hardcoded sorting, so we have to return a hardcoded field :(
00172   return ( mListView->sortColumn() == -1 ? fields()[ 0 ] : fields()[ mListView->sortColumn() ] );
00173 }
00174 
00175 void KAddressBookTableView::writeConfig(KConfig *config)
00176 {
00177   KAddressBookView::writeConfig(config);
00178 
00179   mListView->saveLayout(config, config->group());
00180 }
00181 
00182 void KAddressBookTableView::readConfig(KConfig *config)
00183 {
00184   KAddressBookView::readConfig( config );
00185 
00186   if ( config->readBoolEntry( "InstantMessagingPresence", false ) ) {
00187     if ( !mIMProxy )
00188     {
00189       mIMProxy = KIMProxy::instance( kapp->dcopClient() );
00190       connect( mIMProxy, SIGNAL( sigContactPresenceChanged( const QString & ) ),
00191                this, SLOT( updatePresence( const QString & ) ) );
00192     }
00193   }
00194   else {
00195     if ( mIMProxy )
00196     {
00197       disconnect( mIMProxy, SIGNAL( sigContactPresenceChanged( const QString & ) ),
00198                this, SLOT( updatePresence( const QString & ) ) );
00199       mIMProxy = 0;
00200     }
00201   }
00202   
00203   // The config could have changed the fields, so we need to reconstruct
00204   // the listview.
00205   reconstructListView();
00206 
00207   // Set the list view options
00208   mListView->setAlternateBackgroundEnabled(config->readBoolEntry("ABackground",
00209                                                                  true));
00210   mListView->setSingleLineEnabled(config->readBoolEntry("SingleLine", false));
00211   mListView->setToolTipsEnabled(config->readBoolEntry("ToolTips", true));
00212 
00213   if (config->readBoolEntry("Background", false))
00214     mListView->setBackgroundPixmap(config->readPathEntry("BackgroundName"));
00215 
00216   // Restore the layout of the listview
00217   mListView->restoreLayout(config, config->group());
00218 }
00219 
00220 void KAddressBookTableView::refresh(QString uid)
00221 {
00222   if (uid.isNull()) {
00223     // Clear the list view
00224     QString currentUID, nextUID;
00225     ContactListViewItem *currentItem = dynamic_cast<ContactListViewItem*>( mListView->currentItem() );
00226     if ( currentItem ) {
00227       ContactListViewItem *nextItem = dynamic_cast<ContactListViewItem*>( currentItem->itemBelow() );
00228       if ( nextItem )
00229         nextUID = nextItem->addressee().uid();
00230       currentUID = currentItem->addressee().uid();
00231     }
00232 
00233     mListView->clear();
00234 
00235     currentItem = 0;
00236     KABC::Addressee::List addresseeList = addressees();
00237     KABC::Addressee::List::Iterator it;
00238     for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
00239       ContactListViewItem *item = new ContactListViewItem(*it, mListView,
00240                                         core()->addressBook(), fields(), mIMProxy );
00241       if ( (*it).uid() == currentUID )
00242         currentItem = item;
00243       else if ( (*it).uid() == nextUID && !currentItem )
00244         currentItem = item;
00245     }
00246 
00247     // Sometimes the background pixmap gets messed up when we add lots
00248     // of items.
00249     mListView->repaint();
00250 
00251     if ( currentItem ) {
00252       mListView->setCurrentItem( currentItem );
00253       mListView->ensureItemVisible( currentItem );
00254     }
00255   } else {
00256     // Only need to update on entry. Iterate through and try to find it
00257     ContactListViewItem *ceItem;
00258     QPtrList<QListViewItem> selectedItems( mListView->selectedItems() );
00259     QListViewItem *it;
00260     for ( it = selectedItems.first(); it; it = selectedItems.next() ) {
00261       ceItem = dynamic_cast<ContactListViewItem*>( it );
00262       if ( ceItem && ceItem->addressee().uid() == uid ) {
00263         ceItem->refresh();
00264         return;
00265       }
00266     }
00267     refresh( QString::null );
00268   }
00269 }
00270 
00271 QStringList KAddressBookTableView::selectedUids()
00272 {
00273     QStringList uidList;
00274     QListViewItem *item;
00275     ContactListViewItem *ceItem;
00276 
00277     for(item = mListView->firstChild(); item; item = item->itemBelow())
00278     {
00279         if (mListView->isSelected( item ))
00280         {
00281             ceItem = dynamic_cast<ContactListViewItem*>(item);
00282             if (ceItem != 0L)
00283                 uidList << ceItem->addressee().uid();
00284         }
00285     }
00286 
00287     return uidList;
00288 }
00289 
00290 void KAddressBookTableView::setSelected(QString uid, bool selected)
00291 {
00292     QListViewItem *item;
00293     ContactListViewItem *ceItem;
00294 
00295     if (uid.isNull())
00296     {
00297         mListView->selectAll(selected);
00298     }
00299     else
00300     {
00301         for(item = mListView->firstChild(); item; item = item->itemBelow())
00302         {
00303             ceItem = dynamic_cast<ContactListViewItem*>(item);
00304             if ((ceItem != 0L) && (ceItem->addressee().uid() == uid))
00305             {
00306                 mListView->setSelected(item, selected);
00307 
00308                 if (selected)
00309                     mListView->ensureItemVisible(item);
00310             }
00311         }
00312     }
00313 }
00314 
00315 void KAddressBookTableView::addresseeSelected()
00316 {
00317     // We need to try to find the first selected item. This might not be the
00318     // last selected item, but when QListView is in multiselection mode,
00319     // there is no way to figure out which one was
00320     // selected last.
00321     QListViewItem *item;
00322     bool found =false;
00323     for (item = mListView->firstChild(); item && !found;
00324          item = item->nextSibling())
00325     {
00326         if (item->isSelected())
00327         {
00328             found = true;
00329             ContactListViewItem *ceItem
00330                  = dynamic_cast<ContactListViewItem*>(item);
00331             if ( ceItem ) emit selected(ceItem->addressee().uid());
00332         }
00333     }
00334 
00335     if (!found)
00336         emit selected(QString::null);
00337 }
00338 
00339 void KAddressBookTableView::addresseeExecuted(QListViewItem *item)
00340 {
00341     if (item)
00342     {
00343         ContactListViewItem *ceItem
00344                = dynamic_cast<ContactListViewItem*>(item);
00345 
00346        if (ceItem)
00347        {
00348            emit executed(ceItem->addressee().uid());
00349        }
00350     }
00351     else
00352     {
00353         emit executed(QString::null);
00354     }
00355 }
00356 
00357 void KAddressBookTableView::rmbClicked( KListView*, QListViewItem*, const QPoint &point )
00358 {
00359   popup( point );
00360 }
00361 
00362 void KAddressBookTableView::updatePresence( const QString &uid )
00363 {
00364   // find the LVI to update and refresh() it
00365   QListViewItem *item;
00366   ContactListViewItem *ceItem;
00367   for ( item = mListView->firstChild(); item; item = item->itemBelow() ) {
00368     ceItem = dynamic_cast<ContactListViewItem*>(item);
00369     if ( ( ceItem != 0L ) && ( ceItem->addressee().uid() == uid ) ) {
00370       ceItem->setHasIM( true );
00371       ceItem->refresh();
00372       break;
00373     }
00374   }
00375   if ( mListView->sortColumn() == mListView->imColumn() )
00376     mListView->sort();
00377 }
00378 
00379 #include "kaddressbooktableview.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:24:11 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003