ContactDock.cpp 6.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#include "ContactDock.h"

#include <QtGui/QVBoxLayout>

#include <akonadi/collectionfilterproxymodel.h>
#include <akonadi/contact/contactstreemodel.h>
#include <akonadi/kmime/messagemodel.h>
#include <akonadi/changerecorder.h>
#include <akonadi/session.h>
#include <kabc/addressee.h>
#include <kabc/picture.h>
#include <kabc/phonenumber.h>
#include <kabc/vcard.h>
#include <kabc/addressee.h>
#include <kabc/field.h>
#include <kabc/vcardline.h>
#include <kabc/contactgroup.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/entitydisplayattribute.h>
#include <akonadi/recursiveitemfetchjob.h>
#include <kicon.h>

ContactDock::ContactDock(QWidget* parent) : QDockWidget(parent)
{
   m_pCollViewCV = new Akonadi::EntityTreeView();
   m_pItemView   = new Akonadi::ItemView();
   m_pFilterLE   = new KLineEdit();
   m_pCollCCB    = new Akonadi::CollectionComboBox;
   m_pSplitter   = new QSplitter(Qt::Vertical,this);

   QWidget* mainWidget = new QWidget(this);
   setWidget(mainWidget);

   QVBoxLayout* mainLayout = new QVBoxLayout(mainWidget);

   mainLayout->addWidget(m_pCollCCB);
   mainLayout->addWidget(m_pSplitter);
   m_pSplitter->addWidget(m_pCollViewCV);
   m_pSplitter->addWidget(m_pItemView);
   mainLayout->addWidget(m_pFilterLE);
   
   m_pSplitter->setChildrenCollapsible(true);
   m_pSplitter->setStretchFactor(0,7);
   
   m_pCollCCB->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
   m_pCollCCB->setAccessRightsFilter( Akonadi::Collection::ReadOnly );


      /////////////

      // use a separated session for this model
   Akonadi::Session *session = new Akonadi::Session( "SFLPhone::instance" );

   Akonadi::ItemFetchScope scope;
   // fetch all content of the contacts, including images
   scope.fetchFullPayload( true );
   // fetch the EntityDisplayAttribute, which contains custom names and icons
   scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();

   Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder;
   changeRecorder->setSession( session );
   // include fetching the collection tree
   changeRecorder->fetchCollection( true );
   // set the fetch scope that shall be used
   changeRecorder->setItemFetchScope( scope );
   // monitor all collections below the root collection for changes
   changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
   // list only contacts and contact groups
   changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
   changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );

   Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder );

   Akonadi::ContactsTreeModel::Columns columns;
   columns << Akonadi::ContactsTreeModel::FullName;
   columns << Akonadi::ContactsTreeModel::AllEmails;
   model->setColumns( columns );

   //////////////

   for (int i=0;i<model->rowCount();i++) {
       qDebug() << "\n\n\nChild:" << model->hasChildren(model->index(i,0)) << "Model:" << model->rowCount(model->index(i,0));
   }
   qDebug() << "\n\n\nChild:" << model->hasChildren(model->index(0,0)) << "Model:" << model->rowCount(model->index(0,0));
   qDebug() << "\n\n\nChild:" << model->hasChildren(model->index(1,0)) << "Model:" << model->rowCount(model->index(1,0));

      //qDebug() << "Model:" << *model;

      m_pCollViewCV->setModel( model );
      m_pItemView->setModel( model );
      
      connect (m_pCollCCB, SIGNAL(currentChanged(Akonadi::Collection)),this,SLOT(collectAddressBookContacts()));
      
      
   //collectAllContacts(model);
   collectAddressBookContacts();
   
   setWindowTitle("Contact");
   //setDockIcon(KIcon("resource-group"));
}

ContactDock::~ContactDock()
{
   
}

KABC::Addressee::List ContactDock::collectAllContacts(Akonadi::ContactsTreeModel *mModel) const
{
  KABC::Addressee::List contacts;
  for ( int i = 0; i < mModel->rowCount(); ++i ) {
    const QModelIndex index = mModel->index( i, 0 );
    if ( index.isValid() ) {
      const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
      if ( item.isValid() && item.hasPayload<KABC::Addressee>() ) {
        contacts.append( item.payload<KABC::Addressee>() );
	qDebug() << item.payload<KABC::Addressee>().toString();
      }
    }
  }
  return contacts;
}

KABC::Addressee::List ContactDock::collectAddressBookContacts() const
{
   
   qDebug() << "In colect\n\n\n\n\n";
  KABC::Addressee::List contacts;

  
  const Akonadi::Collection collection = m_pCollCCB->currentCollection();
  if ( !collection.isValid() ) {
     qDebug() << "The current collection is not valid";
    return contacts;
  }
  qDebug() << "Valid collection";

  //if ( mAddressBookSelectionRecursive->isChecked() ) {
    Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob( collection, QStringList() << KABC::Addressee::mimeType() << KABC::ContactGroup::mimeType());
    job->fetchScope().fetchFullPayload();
//qDebug() << "Begin \n\n\n";
    if ( job->exec() ) {
       
      const Akonadi::Item::List items = job->items();

      foreach ( const Akonadi::Item &item, items ) {
	 //qDebug() << "In for" << item.payloadData() << item.mimeType();
        if ( item.hasPayload<KABC::ContactGroup>() ) {
          //contacts.append( item.payload<KABC::ContactGroup>() );
	  qDebug() << "Group:" << item.payload<KABC::ContactGroup>().name();
        }
        if ( item.hasPayload<KABC::Addressee>() ) {
          //contacts.append( item.payload<KABC::ContactGroup>() );
	  qDebug() << "Addressee:" << item.payload<KABC::Addressee>().name();
        }
//         if ( item.hasPayload<KABC::Field>() ) {
//           //contacts.append( item.payload<KABC::VCard>() );
// 	  qDebug() << "VCard:" << item.payload<KABC::VCardFormat>().identifiers();
//         }
      }
    }
//   } else {
//     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( collection );
//     job->fetchScope().fetchFullPayload();
// 
//     if ( job->exec() ) {
//       const Akonadi::Item::List items = job->items();
// 
//       foreach ( const Akonadi::Item &item, items ) {
//         if ( item.hasPayload<KABC::Addressee>() ) {
//           contacts.append( item.payload<KABC::Addressee>() );
//         }
//       }
//     }
//   }

  qDebug() << "End collect \n\n\n";
  return contacts;
}