resourceselectdialog.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #include <klocale.h>
00022 #include <kbuttonbox.h>
00023 #include <klistbox.h>
00024 
00025 #include <qgroupbox.h>
00026 #include <qlayout.h>
00027 
00028 #include "resource.h"
00029 #include "addressbook.h"
00030 
00031 #include "resourceselectdialog.h"
00032 
00033 using namespace KABC;
00034 
00035 ResourceSelectDialog::ResourceSelectDialog( AddressBook *ab, QWidget *parent, const char *name )
00036     : KDialog( parent, name, true )
00037 {
00038   setCaption( i18n( "Resource Selection" ) );
00039   resize( 300, 200 );
00040 
00041   QVBoxLayout *mainLayout = new QVBoxLayout( this );
00042   mainLayout->setMargin( marginHint() );
00043     
00044   QGroupBox *groupBox = new QGroupBox( 2, Qt::Horizontal,  this );
00045   groupBox->setTitle( i18n( "Resources" ) );
00046 
00047   mResourceId = new KListBox( groupBox );
00048 
00049   mainLayout->addWidget( groupBox );
00050 
00051   mainLayout->addSpacing( 10 );
00052 
00053   KButtonBox *buttonBox = new KButtonBox( this );
00054 
00055   buttonBox->addStretch();    
00056   buttonBox->addButton( i18n( "&OK" ), this, SLOT( accept() ) );
00057   buttonBox->addButton( i18n( "&Cancel" ), this, SLOT( reject() ) );
00058   buttonBox->layout();
00059 
00060   mainLayout->addWidget( buttonBox );
00061 
00062   
00063   uint counter = 0;
00064   QPtrList<Resource> list = ab->resources();
00065   for ( uint i = 0; i < list.count(); ++i ) {
00066     Resource *resource = list.at( i );
00067     if ( resource && !resource->readOnly() ) {
00068       mResourceMap.insert( counter, resource );
00069       mResourceId->insertItem( resource->resourceName() );
00070       counter++;
00071     }
00072   }
00073 
00074   mResourceId->setCurrentItem( 0 );
00075 }
00076 
00077 Resource *ResourceSelectDialog::resource()
00078 {
00079   if ( mResourceId->currentItem() != -1 )
00080     return mResourceMap[ mResourceId->currentItem() ];
00081   else
00082     return 0;
00083 }
00084 
00085 Resource *ResourceSelectDialog::getResource( AddressBook *ab, QWidget *parent )
00086 {
00087   QPtrList<Resource> resources = ab->resources();
00088   if ( resources.count() == 1 ) return resources.first();
00089 
00090   Resource *found = 0;
00091   Resource *r = resources.first();
00092   while( r ) {
00093     if ( !r->readOnly() ) {
00094       if ( found ) {
00095         found = 0;
00096         break;
00097       } else {
00098         found = r;
00099       }
00100     }
00101     r = resources.next();
00102   }
00103   if ( found ) return found;
00104 
00105   ResourceSelectDialog dlg( ab, parent );
00106   if ( dlg.exec() == KDialog::Accepted ) return dlg.resource();
00107   else return 0;
00108 }
00109 
00110 #include "resourceselectdialog.moc"
 
This file is part of the documentation for kabc Library Version 3.2.0.