00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qlayout.h>
00025 #include <qpushbutton.h>
00026 #include <qtimer.h>
00027
00028 #include <kabc/resource.h>
00029 #include <kdialog.h>
00030 #include <kinputdialog.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <kresources/configdialog.h>
00034
00035 #include "core.h"
00036
00037 #include "resourceselection.h"
00038
00039 class AddressBookWrapper : public KABC::AddressBook
00040 {
00041 public:
00042 AddressBookWrapper( KABC::AddressBook* );
00043
00044 KRES::Manager<KABC::Resource>* getResourceManager()
00045 {
00046 return resourceManager();
00047 }
00048 };
00049
00050 class ResourceItem : public QCheckListItem
00051 {
00052 public:
00053 ResourceItem( KListView *parent, KABC::Resource *resource )
00054 : QCheckListItem( parent, "", CheckBox ),
00055 mResource( resource ), mChecked( false )
00056 {
00057 setOn( resource->isActive() );
00058 mChecked = resource->isActive();
00059 }
00060
00061 virtual QString text( int column ) const
00062 {
00063 if ( column == 0 )
00064 return mResource->resourceName();
00065 else
00066 return QString::null;
00067 }
00068
00069 void setChecked( bool state )
00070 {
00071 mChecked = state;
00072 }
00073
00074 bool checked() const { return mChecked; }
00075 KABC::Resource *resource() const { return mResource; }
00076
00077 private:
00078 KABC::Resource *mResource;
00079 bool mChecked;
00080 };
00081
00082 ResourceSelection::ResourceSelection( KAB::Core *core, QWidget *parent, const char *name )
00083 : KAB::ExtensionWidget( core, parent, name ), mManager( 0 )
00084 {
00085 initGUI();
00086
00087 AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( core->addressBook() );
00088 mManager = wrapper->getResourceManager();
00089
00090 connect( mAddButton, SIGNAL( clicked() ), SLOT( add() ) );
00091 connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00092 connect( mRemoveButton, SIGNAL( clicked() ), SLOT( remove() ) );
00093
00094 connect( mView, SIGNAL( clicked( QListViewItem* ) ),
00095 SLOT( currentChanged( QListViewItem* ) ) );
00096
00097 QTimer::singleShot( 0, this, SLOT( updateView() ) );
00098 }
00099
00100 ResourceSelection::~ResourceSelection()
00101 {
00102 }
00103
00104 QString ResourceSelection::title() const
00105 {
00106 return i18n( "Address Books" );
00107 }
00108
00109 QString ResourceSelection::identifier() const
00110 {
00111 return "resourceselection";
00112 }
00113
00114 void ResourceSelection::add()
00115 {
00116 QStringList types = mManager->resourceTypeNames();
00117 QStringList descs = mManager->resourceTypeDescriptions();
00118
00119 bool ok = false;
00120 QString desc = KInputDialog::getItem( i18n( "Add Address Book" ),
00121 i18n( "Please select type of the new address book:" ),
00122 descs, 0, false, &ok, this );
00123 if ( !ok )
00124 return;
00125
00126 QString type = types[ descs.findIndex( desc ) ];
00127
00128
00129 KABC::Resource *resource = mManager->createResource( type );
00130 if( !resource ) {
00131 KMessageBox::error( this, i18n("<qt>Unable to create an address book of type <b>%1</b>.</qt>")
00132 .arg( type ) );
00133 return;
00134 }
00135
00136 resource->setResourceName( i18n( "%1 address book" ).arg( type ) );
00137
00138 KRES::ConfigDialog dlg( this, QString( "contact" ), resource );
00139
00140 if ( dlg.exec() ) {
00141 core()->addressBook()->addResource( resource );
00142 resource->asyncLoad();
00143
00144 mLastResource = resource->identifier();
00145 updateView();
00146 } else {
00147 delete resource;
00148 resource = 0;
00149 }
00150 }
00151
00152 void ResourceSelection::edit()
00153 {
00154 ResourceItem *item = selectedItem();
00155 if ( !item )
00156 return;
00157
00158 KRES::ConfigDialog dlg( this, QString( "contact" ), item->resource() );
00159
00160 if ( dlg.exec() ) {
00161 mManager->change( item->resource() );
00162 item->resource()->asyncLoad();
00163
00164 mLastResource = item->resource()->identifier();
00165 updateView();
00166 }
00167 }
00168
00169 void ResourceSelection::remove()
00170 {
00171 ResourceItem *item = selectedItem();
00172 if ( !item )
00173 return;
00174
00175 int result = KMessageBox::warningContinueCancel( this,
00176 i18n( "<qt>Do you really want to remove the address book <b>%1</b>?</qt>" )
00177 .arg( item->resource()->resourceName() ), "",
00178 KGuiItem( i18n( "&Remove" ), "editdelete" ) );
00179 if ( result == KMessageBox::Cancel )
00180 return;
00181
00182 mLastResource = item->resource()->identifier();
00183
00184 core()->addressBook()->removeResource( item->resource() );
00185 core()->addressBook()->emitAddressBookChanged();
00186
00187 updateView();
00188 }
00189
00190 void ResourceSelection::currentChanged( QListViewItem *item )
00191 {
00192 bool state = (item != 0);
00193
00194 mEditButton->setEnabled( state );
00195 mRemoveButton->setEnabled( state );
00196
00197 ResourceItem *resItem = static_cast<ResourceItem*>( item );
00198 if ( !resItem )
00199 return;
00200
00201 KABC::Resource *resource = resItem->resource();
00202
00203 if ( resItem->checked() != resItem->isOn() ) {
00204 resItem->setChecked( resItem->isOn() );
00205 resource->setActive( resItem->isOn() );
00206
00207 mManager->change( resource );
00208
00209 if ( resItem->checked() ) {
00210 if ( !resource->addressBook() )
00211 resource->setAddressBook( core()->addressBook() );
00212
00213 if ( !resource->isOpen() )
00214 resource->open();
00215
00216 resource->asyncLoad();
00217 }
00218
00219 mLastResource = resource->identifier();
00220 updateView();
00221 }
00222 }
00223
00224 void ResourceSelection::updateView()
00225 {
00226 if ( !mManager )
00227 return;
00228
00229 mView->clear();
00230
00231 KRES::Manager<KABC::Resource>::Iterator it;
00232 for ( it = mManager->begin(); it != mManager->end(); ++it )
00233 new ResourceItem( mView, *it );
00234
00235 QListViewItemIterator itemIt( mView );
00236 while ( itemIt.current() ) {
00237 ResourceItem *item = static_cast<ResourceItem*>( itemIt.current() );
00238 if ( item->resource()->identifier() == mLastResource ) {
00239 mView->setSelected( item, true );
00240 mView->ensureItemVisible( item );
00241 break;
00242 }
00243 ++itemIt;
00244 }
00245
00246 core()->addressBook()->emitAddressBookChanged();
00247 }
00248
00249 ResourceItem* ResourceSelection::selectedItem() const
00250 {
00251 return static_cast<ResourceItem*>( mView->selectedItem() );
00252 }
00253
00254 void ResourceSelection::initGUI()
00255 {
00256 QGridLayout *layout = new QGridLayout( this, 2, 3, KDialog::marginHint(),
00257 KDialog::spacingHint() );
00258
00259 mView = new KListView( this );
00260 mView->addColumn( i18n( "Address Books" ) );
00261 mView->setFullWidth( true );
00262 layout->addMultiCellWidget( mView, 0, 0, 0, 2 );
00263
00264 mAddButton = new QPushButton( i18n( "Add..." ), this );
00265 mEditButton = new QPushButton( i18n( "Edit..." ), this );
00266 mEditButton->setEnabled( false );
00267 mRemoveButton = new QPushButton( i18n( "Remove" ), this );
00268 mRemoveButton->setEnabled( false );
00269
00270 layout->addWidget( mAddButton, 1, 0 );
00271 layout->addWidget( mEditButton, 1, 1 );
00272 layout->addWidget( mRemoveButton, 1, 2 );
00273 }
00274
00275 class ResourceSelectionFactory : public KAB::ExtensionFactory
00276 {
00277 public:
00278 KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00279 {
00280 return new ResourceSelection( core, parent, name );
00281 }
00282
00283 QString identifier() const
00284 {
00285 return "resourceselection";
00286 }
00287 };
00288
00289 extern "C" {
00290 void *init_libkaddrbk_resourceselection()
00291 {
00292 return ( new ResourceSelectionFactory );
00293 }
00294 }
00295
00296 #include "resourceselection.moc"