22 #include <Akonadi/CollectionComboBox> 
   23 #include <Akonadi/EntityTreeModel> 
   24 #include <Akonadi/ItemFetchJob> 
   25 #include <Akonadi/ItemFetchScope> 
   26 #include <Akonadi/RecursiveItemFetchJob> 
   30 #include <QButtonGroup> 
   32 #include <QGridLayout> 
   34 #include <QItemSelectionModel> 
   36 #include <QRadioButton> 
   37 #include <QVBoxLayout> 
   41   : QWidget( parent ), mSelectionModel( selectionModel )
 
   45   mSelectedContactsButton->setEnabled( mSelectionModel->hasSelection() );
 
   46   mAddressBookSelection->setEnabled( 
false );
 
   47   mAddressBookSelectionRecursive->setEnabled( 
false );
 
   49   connect( mAddressBookContactsButton, SIGNAL(toggled(
bool)),
 
   50            mAddressBookSelection, SLOT(setEnabled(
bool)) );
 
   51   connect( mAddressBookContactsButton, SIGNAL(toggled(
bool)),
 
   52            mAddressBookSelectionRecursive, SLOT(setEnabled(
bool)) );
 
   55   if ( mSelectionModel->hasSelection() ) {
 
   56     mSelectedContactsButton->setChecked( 
true );
 
   58     mAllContactsButton->setChecked( 
true );
 
   64   mMessageLabel->setText( message );
 
   69   mAddressBookSelection->setDefaultCollection( addressBook );
 
   74   if ( mAllContactsButton->isChecked() ) {
 
   75     return collectAllContacts();
 
   76   } 
else if ( mSelectedContactsButton->isChecked() ) {
 
   77     return collectSelectedContacts();
 
   78   } 
else if ( mAddressBookContactsButton->isChecked() ) {
 
   79     return collectAddressBookContacts();
 
   82   return KABC::Addressee::List();
 
   85 void ContactSelectionWidget::initGui()
 
   87   QVBoxLayout *layout = 
new QVBoxLayout( 
this );
 
   89   mMessageLabel = 
new QLabel;
 
   90   layout->addWidget( mMessageLabel );
 
   92   QButtonGroup *group = 
new QButtonGroup( 
this );
 
   94   QGroupBox *groupBox = 
new QGroupBox;
 
   96   QGridLayout *boxLayout = 
new QGridLayout;
 
   97   groupBox->setLayout( boxLayout );
 
   99   mAllContactsButton = 
new QRadioButton( i18nc( 
"@option:radio", 
"All contacts" ) );
 
  100   mAllContactsButton->setToolTip(
 
  101     i18nc( 
"@info:tooltip", 
"All contacts from all your address books" ) );
 
  102   mAllContactsButton->setWhatsThis(
 
  103     i18nc( 
"@info:whatsthis",
 
  104            "Choose this option you want to select all your contacts from " 
  105            "all your address books." ) );
 
  107   mSelectedContactsButton = 
new QRadioButton( i18nc( 
"@option:radio",
"Selected contacts" ) );
 
  108   mSelectedContactsButton->setToolTip(
 
  109     i18nc( 
"@info:tooltip", 
"Only the contacts currently selected" ) );
 
  110   mSelectedContactsButton->setWhatsThis(
 
  111     i18nc( 
"@info:whatsthis",
 
  112            "Choose this option if you want only the contacts you have already " 
  113            "selected in the graphical interface." ) );
 
  115   mAddressBookContactsButton = 
new QRadioButton( i18nc( 
"@option:radio", 
"All contacts from:" ) );
 
  116   mAddressBookContactsButton->setToolTip(
 
  117     i18nc( 
"@info:tooltip", 
"All contacts from a chosen address book" ) );
 
  118   mAddressBookContactsButton->setWhatsThis(
 
  119     i18nc( 
"@info:whatsthis",
 
  120            "Choose this option if you want to select all the contacts from only one " 
  121            "of your address books.  Once this option is clicked you will be provided " 
  122            "a drop down box listing all those address books and permitted to select " 
  123            "the one you want." ) );
 
  125   mAddressBookSelection = 
new Akonadi::CollectionComboBox;
 
  126   mAddressBookSelection->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
 
  127   mAddressBookSelection->setAccessRightsFilter( Akonadi::Collection::ReadOnly );
 
  128   mAddressBookSelection->setExcludeVirtualCollections( 
true );
 
  129   mAddressBookSelectionRecursive = 
new QCheckBox( i18nc( 
"@option:check", 
"Include Subfolders" ) );
 
  130   mAddressBookSelectionRecursive->setToolTip(
 
  131     i18nc( 
"@info:tooltip", 
"Select all subfolders including the top-level folder" ) );
 
  132   mAddressBookSelectionRecursive->setWhatsThis(
 
  133     i18nc( 
"@info:whatsthis",
 
  134            "Check this box if you want to select all contacts from this folder, " 
  135            "including all subfolders.  If you only want the contacts from the " 
  136            "top-level folder then leave this box unchecked." ) );
 
  138   group->addButton( mAllContactsButton );
 
  139   group->addButton( mSelectedContactsButton );
 
  140   group->addButton( mAddressBookContactsButton );
 
  142   boxLayout->addWidget( mAllContactsButton, 0, 0, 1, 2 );
 
  143   boxLayout->addWidget( mSelectedContactsButton, 1, 0, 1, 2 );
 
  144   boxLayout->addWidget( mAddressBookContactsButton, 2, 0, Qt::AlignTop );
 
  146   QVBoxLayout *addressBookLayout = 
new QVBoxLayout;
 
  147   addressBookLayout->setMargin( 0 );
 
  148   addressBookLayout->addWidget( mAddressBookSelection );
 
  149   addressBookLayout->addWidget( mAddressBookSelectionRecursive );
 
  151   boxLayout->addLayout( addressBookLayout, 2, 1 );
 
  153   layout->addWidget( groupBox );
 
  154   layout->addStretch( 1 );
 
  157 KABC::Addressee::List ContactSelectionWidget::collectAllContacts()
 const 
  159   Akonadi::RecursiveItemFetchJob *job =
 
  160     new Akonadi::RecursiveItemFetchJob( Akonadi::Collection::root(),
 
  161                                         QStringList() << KABC::Addressee::mimeType() );
 
  162   job->fetchScope().fetchFullPayload();
 
  164   KABC::Addressee::List contacts;
 
  165   if ( !job->exec() ) {
 
  169   foreach ( 
const Akonadi::Item &item, job->items() ) {
 
  170     if ( item.isValid() && item.hasPayload<KABC::Addressee>() ) {
 
  171       contacts.append( item.payload<KABC::Addressee>() );
 
  178 KABC::Addressee::List ContactSelectionWidget::collectSelectedContacts()
 const 
  180   KABC::Addressee::List contacts;
 
  182   const QModelIndexList indexes = mSelectionModel->selectedRows( 0 );
 
  183   for ( 
int i = 0; i < indexes.count(); ++i ) {
 
  184     const QModelIndex index = indexes.at( i );
 
  185     if ( index.isValid() ) {
 
  186       const Akonadi::Item item =
 
  187         index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
 
  188       if ( item.isValid() && item.hasPayload<KABC::Addressee>() ) {
 
  189         contacts.append( item.payload<KABC::Addressee>() );
 
  197 KABC::Addressee::List ContactSelectionWidget::collectAddressBookContacts()
 const 
  199   KABC::Addressee::List contacts;
 
  201   const Akonadi::Collection collection = mAddressBookSelection->currentCollection();
 
  202   if ( !collection.isValid() ) {
 
  206   if ( mAddressBookSelectionRecursive->isChecked() ) {
 
  207     Akonadi::RecursiveItemFetchJob *job =
 
  208       new Akonadi::RecursiveItemFetchJob( collection,
 
  209                                           QStringList() << KABC::Addressee::mimeType() );
 
  210     job->fetchScope().fetchFullPayload();
 
  212     if ( !job->exec() ) {
 
  216     foreach ( 
const Akonadi::Item &item, job->items() ) {
 
  217       if ( item.hasPayload<KABC::Addressee>() ) {
 
  218         contacts.append( item.payload<KABC::Addressee>() );
 
  222     Akonadi::ItemFetchJob *job = 
new Akonadi::ItemFetchJob( collection );
 
  223     job->fetchScope().fetchFullPayload();
 
  225     if ( !job->exec() ) {
 
  229     foreach ( 
const Akonadi::Item &item, job->items() ) {
 
  230       if ( item.hasPayload<KABC::Addressee>() ) {
 
  231         contacts.append( item.payload<KABC::Addressee>() );
 
  239 #include "contactselectionwidget.moc"