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 // Base class for items in the list 00078 class CompletionItem 00079 { 00080 public: 00081 virtual ~CompletionItem() {} 00082 virtual QString label() const = 0; 00083 virtual int completionWeight() const = 0; 00084 virtual void setCompletionWeight( int weight ) = 0; 00085 virtual void save( CompletionOrderEditor* ) = 0; 00086 }; 00087 00088 int CompletionItemList::compareItems( QPtrCollection::Item s1, QPtrCollection::Item s2 ) 00089 { 00090 int w1 = ( (CompletionItem*)s1 )->completionWeight(); 00091 int w2 = ( (CompletionItem*)s2 )->completionWeight(); 00092 // s1 < s2 if it has a higher completion value, i.e. w1 > w2. 00093 return w2 - w1; 00094 } 00095 00096 class LDAPCompletionItem : public CompletionItem 00097 { 00098 public: 00099 LDAPCompletionItem( LdapClient* ldapClient ) : mLdapClient( ldapClient ) {} 00100 virtual QString label() const { return i18n( "LDAP server %1" ).arg( mLdapClient->host() ); } 00101 virtual int completionWeight() const { return mLdapClient->completionWeight(); } 00102 virtual void save( CompletionOrderEditor* ); 00103 protected: 00104 virtual void setCompletionWeight( int weight ) { mWeight = weight; } 00105 private: 00106 LdapClient* mLdapClient; 00107 int mWeight; 00108 }; 00109 00110 void LDAPCompletionItem::save( CompletionOrderEditor* ) 00111 { 00112 KConfig config( "kabldaprc" ); 00113 config.setGroup( "LDAP" ); 00114 config.writeEntry( QString( "SelectedCompletionWeight%1" ).arg( mLdapClient->clientNumber() ), 00115 mWeight ); 00116 config.sync(); 00117 } 00118 00119 // A simple item saved into kpimcompletionorder (no subresources, just name/identifier/weight) 00120 class SimpleCompletionItem : public CompletionItem 00121 { 00122 public: 00123 SimpleCompletionItem( CompletionOrderEditor* editor, const QString& label, const QString& identifier ) 00124 : mLabel( label ), mIdentifier( identifier ) { 00125 KConfigGroup group( editor->configFile(), "CompletionWeights" ); 00126 mWeight = group.readNumEntry( mIdentifier, 60 ); 00127 } 00128 virtual QString label() const { return mLabel; } 00129 virtual int completionWeight() const { return mWeight; } 00130 virtual void save( CompletionOrderEditor* ); 00131 protected: 00132 virtual void setCompletionWeight( int weight ) { mWeight = weight; } 00133 private: 00134 QString mLabel, mIdentifier; 00135 int mWeight; 00136 }; 00137 00138 void SimpleCompletionItem::save( CompletionOrderEditor* editor ) 00139 { 00140 // Maybe KABC::Resource could have a completionWeight setting (for readConfig/writeConfig) 00141 // But for kdelibs-3.2 compat purposes I can't do that. 00142 KConfigGroup group( editor->configFile(), "CompletionWeights" ); 00143 group.writeEntry( mIdentifier, mWeight ); 00144 } 00145 00146 // An imap subresource for kabc 00147 class KABCImapSubResCompletionItem : public CompletionItem 00148 { 00149 public: 00150 KABCImapSubResCompletionItem( ResourceABC* resource, const QString& subResource ) 00151 : mResource( resource ), mSubResource( subResource ), mWeight( completionWeight() ) {} 00152 virtual QString label() const { 00153 return QString( "%1 %2" ).arg( mResource->resourceName() ).arg( mResource->subresourceLabel( mSubResource ) ); 00154 } 00155 virtual int completionWeight() const { 00156 return mResource->subresourceCompletionWeight( mSubResource ); 00157 } 00158 virtual void setCompletionWeight( int weight ) { 00159 mWeight = weight; 00160 } 00161 virtual void save( CompletionOrderEditor* ) { 00162 mResource->setSubresourceCompletionWeight( mSubResource, mWeight ); 00163 } 00164 private: 00165 ResourceABC* mResource; 00166 QString mSubResource; 00167 int mWeight; 00168 }; 00169 00171 00172 class CompletionViewItem : public QListViewItem 00173 { 00174 public: 00175 CompletionViewItem( QListView* lv, CompletionItem* item ) 00176 : QListViewItem( lv, lv->lastItem(), item->label() ), mItem( item ) {} 00177 CompletionItem* item() const { return mItem; } 00178 void setItem( CompletionItem* i ) { mItem = i; setText( 0, mItem->label() ); } 00179 00180 private: 00181 CompletionItem* mItem; 00182 }; 00183 00184 CompletionOrderEditor::CompletionOrderEditor( KPIM::LdapSearch* ldapSearch, 00185 QWidget* parent, const char* name ) 00186 : KDialogBase( parent, name, true, i18n("Edit Completion Order"), Ok|Cancel, Ok, true ), 00187 mConfig( "kpimcompletionorder" ), mDirty( false ) 00188 { 00189 mItems.setAutoDelete( true ); 00190 // The first step is to gather all the data, creating CompletionItem objects 00191 QValueList< LdapClient* > ldapClients = ldapSearch->clients(); 00192 for( QValueList<LdapClient*>::const_iterator it = ldapClients.begin(); it != ldapClients.end(); ++it ) { 00193 //kdDebug(5300) << "LDAP: host " << (*it)->host() << " weight " << (*it)->completionWeight() << endl; 00194 mItems.append( new LDAPCompletionItem( *it ) ); 00195 } 00196 KABC::AddressBook *addressBook = KABC::StdAddressBook::self(); 00197 QPtrList<KABC::Resource> resources = addressBook->resources(); 00198 for( QPtrListIterator<KABC::Resource> resit( resources ); *resit; ++resit ) { 00199 //kdDebug(5300) << "KABC Resource: " << (*resit)->className() << endl; 00200 ResourceABC* res = dynamic_cast<ResourceABC *>( *resit ); 00201 if ( res ) { // IMAP KABC resource 00202 const QStringList subresources = res->subresources(); 00203 for( QStringList::const_iterator it = subresources.begin(); it != subresources.end(); ++it ) { 00204 mItems.append( new KABCImapSubResCompletionItem( res, *it ) ); 00205 } 00206 } else { // non-IMAP KABC resource 00207 mItems.append( new SimpleCompletionItem( this, (*resit)->resourceName(), 00208 (*resit)->identifier() ) ); 00209 } 00210 } 00211 // Add an item for distribution lists 00212 mItems.append( new SimpleCompletionItem( this, i18n( "Distribution Lists" ), "DistributionLists" ) ); 00213 00214 // Now sort the items, then create the GUI 00215 mItems.sort(); 00216 00217 QHBox* page = makeHBoxMainWidget(); 00218 mListView = new KListView( page ); 00219 mListView->setSorting( -1 ); 00220 mListView->addColumn( QString::null ); 00221 mListView->header()->hide(); 00222 00223 for( QPtrListIterator<CompletionItem> compit( mItems ); *compit; ++compit ) { 00224 new CompletionViewItem( mListView, *compit ); 00225 kdDebug(5300) << " " << (*compit)->label() << " " << (*compit)->completionWeight() << endl; 00226 } 00227 00228 QVBox* upDownBox = new QVBox( page ); 00229 mUpButton = new QToolButton( upDownBox, "mUpButton" ); 00230 mUpButton->setPixmap( BarIcon( "up", KIcon::SizeSmall ) ); 00231 mUpButton->setEnabled( false ); // b/c no item is selected yet 00232 mUpButton->setFocusPolicy( StrongFocus ); 00233 00234 mDownButton = new QToolButton( upDownBox, "mDownButton" ); 00235 mDownButton->setPixmap( BarIcon( "down", KIcon::SizeSmall ) ); 00236 mDownButton->setEnabled( false ); // b/c no item is selected yet 00237 mDownButton->setFocusPolicy( StrongFocus ); 00238 00239 QWidget* spacer = new QWidget( upDownBox ); 00240 upDownBox->setStretchFactor( spacer, 100 ); 00241 00242 connect( mListView, SIGNAL( selectionChanged( QListViewItem* ) ), 00243 SLOT( slotSelectionChanged( QListViewItem* ) ) ); 00244 connect( mUpButton, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) ); 00245 connect( mDownButton, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) ); 00246 } 00247 00248 CompletionOrderEditor::~CompletionOrderEditor() 00249 { 00250 } 00251 00252 void CompletionOrderEditor::slotSelectionChanged( QListViewItem *item ) 00253 { 00254 mDownButton->setEnabled( item && item->itemBelow() ); 00255 mUpButton->setEnabled( item && item->itemAbove() ); 00256 } 00257 00258 static void swapItems( CompletionViewItem *one, CompletionViewItem *other ) 00259 { 00260 CompletionItem* i = one->item(); 00261 one->setItem( other->item() ); 00262 other->setItem( i ); 00263 } 00264 00265 void CompletionOrderEditor::slotMoveUp() 00266 { 00267 CompletionViewItem *item = static_cast<CompletionViewItem *>( mListView->selectedItem() ); 00268 if ( !item ) return; 00269 CompletionViewItem *above = static_cast<CompletionViewItem *>( item->itemAbove() ); 00270 if ( !above ) return; 00271 swapItems( item, above ); 00272 mListView->setCurrentItem( above ); 00273 mListView->setSelected( above, true ); 00274 mDirty = true; 00275 } 00276 00277 void CompletionOrderEditor::slotMoveDown() 00278 { 00279 CompletionViewItem *item = static_cast<CompletionViewItem *>( mListView->selectedItem() ); 00280 if ( !item ) return; 00281 CompletionViewItem *below = static_cast<CompletionViewItem *>( item->itemBelow() ); 00282 if ( !below ) return; 00283 swapItems( item, below ); 00284 mListView->setCurrentItem( below ); 00285 mListView->setSelected( below, true ); 00286 mDirty = true; 00287 } 00288 00289 void CompletionOrderEditor::slotOk() 00290 { 00291 if ( mDirty ) { 00292 int w = 100; 00293 for ( QListViewItem* it = mListView->firstChild(); it; it = it->nextSibling() ) { 00294 CompletionViewItem *item = static_cast<CompletionViewItem *>( it ); 00295 item->item()->setCompletionWeight( w ); 00296 item->item()->save( this ); 00297 kdDebug(5300) << "slotOk: " << item->item()->label() << " " << w << endl; 00298 --w; 00299 } 00300 00301 // Emit DCOP signal 00302 // The emitter is always set to KPIM::IMAPCompletionOrder, so that the connect works 00303 // This is why we can't use k_dcop_signals here, but need to use emitDCOPSignal 00304 kapp->dcopClient()->emitDCOPSignal( "KPIM::IMAPCompletionOrder", "orderChanged()", QByteArray() ); 00305 } 00306 KDialogBase::slotOk(); 00307 } 00308 00309 } // namespace KPIM 00310 00311 #include "completionordereditor.moc"
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:29 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003