libkdepim Library API Documentation

completionordereditor.cpp

00001 
00031 #include "completionordereditor.h"
00032 #include "ldapclient.h"
00033 #include "resourceabc.h"
00034 
00035 #include <kabc/stdaddressbook.h>
00036 #include <kabc/resource.h>
00037 
00038 #include <kdebug.h>
00039 #include <klocale.h>
00040 #include <kiconloader.h>
00041 #include <klistview.h>
00042 
00043 #include <qhbox.h>
00044 #include <qvbox.h>
00045 #include <qheader.h>
00046 #include <qtoolbutton.h>
00047 #include <kapplication.h>
00048 #include <dcopclient.h>
00049 
00050 /*
00051 
00052 Several items are used in addresseelineedit's completion object:
00053   LDAP servers, KABC resources (imap and non-imap), Recent addresses (in kmail only).
00054 
00055 The default completion weights are as follow:
00056   LDAP: 50, 49, 48 etc.          (see ldapclient.cpp)
00057   KABC non-imap resources: 60    (see addresseelineedit.cpp and SimpleCompletionItem here)
00058   Distribution lists: 60         (see addresseelineedit.cpp and SimpleCompletionItem here)
00059   KABC imap resources: 80        (see kresources/imap/kabc/resourceimap.cpp)
00060   Recent addresses (kmail) : 120 (see kmail/kmcomposewin.cpp)
00061 
00062 This dialog allows to change those weights, by showing one item per:
00063  - LDAP server
00064  - KABC non-imap resource
00065  - KABC imap subresource
00066  plus one item for Distribution Lists.
00067 
00068  Maybe 'recent addresses' should be configurable too, but first it might
00069  be better to add support for them in korganizer too.
00070 
00071 */
00072 
00073 using namespace KPIM;
00074 
00075 namespace KPIM {
00076 
00077 int CompletionItemList::compareItems( QPtrCollection::Item s1, QPtrCollection::Item s2 )
00078 {
00079   int w1 = ( (CompletionItem*)s1 )->completionWeight();
00080   int w2 = ( (CompletionItem*)s2 )->completionWeight();
00081   // s1 < s2 if it has a higher completion value, i.e. w1 > w2.
00082   return w2 - w1;
00083 }
00084 
00085 class LDAPCompletionItem : public CompletionItem
00086 {
00087 public:
00088   LDAPCompletionItem( LdapClient* ldapClient ) : mLdapClient( ldapClient ) {}
00089   virtual QString label() const { return i18n( "LDAP server %1" ).arg( mLdapClient->host() ); }
00090   virtual int completionWeight() const { return mLdapClient->completionWeight(); }
00091   virtual void save( CompletionOrderEditor* );
00092 protected:
00093   virtual void setCompletionWeight( int weight ) { mWeight = weight; }
00094 private:
00095   LdapClient* mLdapClient;
00096   int mWeight;
00097 };
00098 
00099 void LDAPCompletionItem::save( CompletionOrderEditor* )
00100 {
00101   KConfig config( "kabldaprc" );
00102   config.setGroup( "LDAP" );
00103   config.writeEntry( QString( "SelectedCompletionWeight%1" ).arg( mLdapClient->clientNumber() ),
00104                      mWeight );
00105   config.sync();
00106 }
00107 
00108 // A simple item saved into kpimcompletionorder (no subresources, just name/identifier/weight)
00109 class SimpleCompletionItem : public CompletionItem
00110 {
00111 public:
00112   SimpleCompletionItem( CompletionOrderEditor* editor, const QString& label, const QString& identifier )
00113     : mLabel( label ), mIdentifier( identifier ) {
00114       KConfigGroup group( editor->configFile(), "CompletionWeights" );
00115       mWeight = group.readNumEntry( mIdentifier, 60 );
00116     }
00117   virtual QString label() const { return mLabel; }
00118   virtual int completionWeight() const { return mWeight; }
00119   virtual void save( CompletionOrderEditor* );
00120 protected:
00121   virtual void setCompletionWeight( int weight ) { mWeight = weight; }
00122 private:
00123   QString mLabel, mIdentifier;
00124   int mWeight;
00125 };
00126 
00127 void SimpleCompletionItem::save( CompletionOrderEditor* editor )
00128 {
00129   // Maybe KABC::Resource could have a completionWeight setting (for readConfig/writeConfig)
00130   // But for kdelibs-3.2 compat purposes I can't do that.
00131   KConfigGroup group( editor->configFile(), "CompletionWeights" );
00132   group.writeEntry( mIdentifier, mWeight );
00133 }
00134 
00135 // An imap subresource for kabc
00136 class KABCImapSubResCompletionItem : public CompletionItem
00137 {
00138 public:
00139   KABCImapSubResCompletionItem( ResourceABC* resource, const QString& subResource )
00140     : mResource( resource ), mSubResource( subResource ), mWeight( completionWeight() ) {}
00141   virtual QString label() const {
00142     return QString( "%1 %2" ).arg( mResource->resourceName() ).arg( mResource->subresourceLabel( mSubResource ) );
00143   }
00144   virtual int completionWeight() const {
00145     return mResource->subresourceCompletionWeight( mSubResource );
00146   }
00147   virtual void setCompletionWeight( int weight ) {
00148     mWeight = weight;
00149   }
00150   virtual void save( CompletionOrderEditor* ) {
00151     mResource->setSubresourceCompletionWeight( mSubResource, mWeight );
00152   }
00153 private:
00154   ResourceABC* mResource;
00155   QString mSubResource;
00156   int mWeight;
00157 };
00158 
00160 
00161 class CompletionViewItem : public QListViewItem
00162 {
00163 public:
00164   CompletionViewItem( QListView* lv, CompletionItem* item )
00165     : QListViewItem( lv, lv->lastItem(), item->label() ), mItem( item ) {}
00166   CompletionItem* item() const { return mItem; }
00167   void setItem( CompletionItem* i ) { mItem = i; setText( 0, mItem->label() ); }
00168 
00169 private:
00170   CompletionItem* mItem;
00171 };
00172 
00173 CompletionOrderEditor::CompletionOrderEditor( KPIM::LdapSearch* ldapSearch,
00174                                               QWidget* parent, const char* name )
00175   : KDialogBase( parent, name, true, i18n("Edit Completion Order"), Ok|Cancel, Ok, true ),
00176     mConfig( "kpimcompletionorder" ), mDirty( false )
00177 {
00178   mItems.setAutoDelete( true );
00179   // The first step is to gather all the data, creating CompletionItem objects
00180   QValueList< LdapClient* > ldapClients = ldapSearch->clients();
00181   for( QValueList<LdapClient*>::const_iterator it = ldapClients.begin(); it != ldapClients.end(); ++it ) {
00182     //kdDebug(5300) << "LDAP: host " << (*it)->host() << " weight " << (*it)->completionWeight() << endl;
00183     mItems.append( new LDAPCompletionItem( *it ) );
00184   }
00185   KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00186   QPtrList<KABC::Resource> resources = addressBook->resources();
00187   for( QPtrListIterator<KABC::Resource> resit( resources ); *resit; ++resit ) {
00188     //kdDebug(5300) << "KABC Resource: " << (*resit)->className() << endl;
00189     ResourceABC* res = dynamic_cast<ResourceABC *>( *resit );
00190     if ( res ) { // IMAP KABC resource
00191       const QStringList subresources = res->subresources();
00192       for( QStringList::const_iterator it = subresources.begin(); it != subresources.end(); ++it ) {
00193         mItems.append( new KABCImapSubResCompletionItem( res, *it ) );
00194       }
00195     } else { // non-IMAP KABC resource
00196       mItems.append( new SimpleCompletionItem( this, (*resit)->resourceName(),
00197                                                (*resit)->identifier() ) );
00198     }
00199   }
00200   // Add an item for distribution lists
00201   mItems.append( new SimpleCompletionItem( this, i18n( "Distribution Lists" ), "DistributionLists" ) );
00202 
00203   // Now sort the items, then create the GUI
00204   mItems.sort();
00205 
00206   QHBox* page = makeHBoxMainWidget();
00207   mListView = new KListView( page );
00208   mListView->setSorting( -1 );
00209   mListView->addColumn( QString::null );
00210   mListView->header()->hide();
00211 
00212   for( QPtrListIterator<CompletionItem> compit( mItems ); *compit; ++compit ) {
00213     new CompletionViewItem( mListView, *compit );
00214     kdDebug(5300) << "  " << (*compit)->label() << " " << (*compit)->completionWeight() << endl;
00215   }
00216 
00217   QVBox* upDownBox = new QVBox( page );
00218   mUpButton = new QToolButton( upDownBox, "mUpButton" );
00219   mUpButton->setPixmap( BarIcon( "up", KIcon::SizeSmall ) );
00220   mUpButton->setEnabled( false ); // b/c no item is selected yet
00221   mUpButton->setFocusPolicy( StrongFocus );
00222 
00223   mDownButton = new QToolButton( upDownBox, "mDownButton" );
00224   mDownButton->setPixmap( BarIcon( "down", KIcon::SizeSmall ) );
00225   mDownButton->setEnabled( false ); // b/c no item is selected yet
00226   mDownButton->setFocusPolicy( StrongFocus );
00227 
00228   QWidget* spacer = new QWidget( upDownBox );
00229   upDownBox->setStretchFactor( spacer, 100 );
00230 
00231   connect( mListView, SIGNAL( selectionChanged( QListViewItem* ) ),
00232            SLOT( slotSelectionChanged( QListViewItem* ) ) );
00233   connect( mUpButton, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) );
00234   connect( mDownButton, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) );
00235 }
00236 
00237 CompletionOrderEditor::~CompletionOrderEditor()
00238 {
00239 }
00240 
00241 void CompletionOrderEditor::slotSelectionChanged( QListViewItem *item )
00242 {
00243   mDownButton->setEnabled( item && item->itemBelow() );
00244   mUpButton->setEnabled( item && item->itemAbove() );
00245 }
00246 
00247 static void swapItems( CompletionViewItem *one, CompletionViewItem *other )
00248 {
00249   CompletionItem* i = one->item();
00250   one->setItem( other->item() );
00251   other->setItem( i );
00252 }
00253 
00254 void CompletionOrderEditor::slotMoveUp()
00255 {
00256   CompletionViewItem *item = static_cast<CompletionViewItem *>( mListView->selectedItem() );
00257   if ( !item ) return;
00258   CompletionViewItem *above = static_cast<CompletionViewItem *>( item->itemAbove() );
00259   if ( !above ) return;
00260   swapItems( item, above );
00261   mListView->setCurrentItem( above );
00262   mListView->setSelected( above, true );
00263   mDirty = true;
00264 }
00265 
00266 void CompletionOrderEditor::slotMoveDown()
00267 {
00268   CompletionViewItem *item = static_cast<CompletionViewItem *>( mListView->selectedItem() );
00269   if ( !item ) return;
00270   CompletionViewItem *below = static_cast<CompletionViewItem *>( item->itemBelow() );
00271   if ( !below ) return;
00272   swapItems( item, below );
00273   mListView->setCurrentItem( below );
00274   mListView->setSelected( below, true );
00275   mDirty = true;
00276 }
00277 
00278 void CompletionOrderEditor::slotOk()
00279 {
00280   if ( mDirty ) {
00281     int w = 100;
00282     for ( QListViewItem* it = mListView->firstChild(); it; it = it->nextSibling() ) {
00283       CompletionViewItem *item = static_cast<CompletionViewItem *>( it );
00284       item->item()->setCompletionWeight( w );
00285       item->item()->save( this );
00286       kdDebug(5300) << "slotOk:   " << item->item()->label() << " " << w << endl;
00287       --w;
00288     }
00289 
00290     // Emit DCOP signal
00291     // The emitter is always set to KPIM::IMAPCompletionOrder, so that the connect works
00292     // This is why we can't use k_dcop_signals here, but need to use emitDCOPSignal
00293     kapp->dcopClient()->emitDCOPSignal( "KPIM::IMAPCompletionOrder", "orderChanged()", QByteArray() );
00294   }
00295   KDialogBase::slotOk();
00296 }
00297 
00298 } // namespace KPIM
00299 
00300 #include "completionordereditor.moc"
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:40:51 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003