AkonadiBackend.cpp 5.49 KB
Newer Older
1
2
3
4
5
6
#include "AkonadiBackend.h"
#include <QtCore/QTimer>
#include <akonadi/control.h>
#include <akonadi/collectionfilterproxymodel.h>
#include <akonadi/collectionmodel.h>
#include <akonadi/kmime/messagemodel.h>
7
8
9
10
11
12
#include <kabc/contactgroup.h>
#include <kabc/phonenumber.h>
#include <akonadi/recursiveitemfetchjob.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/collectionfetchjob.h>
#include <akonadi/collectionfetchscope.h>
13
14
#include <akonadi/contact/contacteditor.h>
#include <kdialog.h>
15
16

#include "lib/Contact.h"
17
#include "SFLPhone.h"
18
19

AkonadiBackend*  AkonadiBackend::m_pInstance = 0;
20
21
22

AkonadiBackend::AkonadiBackend(QObject* parent) : QObject(parent)
{
23
24
25
26
27
28
29
   //QTimer::singleShot( 0, this, SLOT( delayedInit() ) );
   m_pSession = new Akonadi::Session( "SFLPhone::instance" );

    // fetching all collections containing emails recursively, starting at the root collection
   Akonadi::CollectionFetchJob *job = new Akonadi::CollectionFetchJob( Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive, this );
   job->fetchScope().setContentMimeTypes( QStringList() << "text/directory" );
   connect( job, SIGNAL( collectionsReceived( const Akonadi::Collection::List& ) ), this, SLOT( collectionsReceived( const Akonadi::Collection::List& ) ) );
30
31
}

32
AkonadiBackend::~AkonadiBackend()
33
34
35
36
37
38
{
   
}

AkonadiBackend* AkonadiBackend::getInstance()
{
39
40
41
42
   if (m_pInstance == NULL) {
      m_pInstance = new AkonadiBackend(0);
   }
   return m_pInstance;
43
44
}

45
ContactList AkonadiBackend::update(Akonadi::Collection collection)
46
{
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
   m_pCollection = collection;
   ContactList contacts;
   if ( !collection.isValid() ) {
      qDebug() << "The current collection is not valid";
      return contacts;
   }

   Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob( collection, QStringList() << KABC::Addressee::mimeType() << KABC::ContactGroup::mimeType());
   job->fetchScope().fetchFullPayload();
   if ( job->exec() ) {

      const Akonadi::Item::List items = job->items();

      foreach ( const Akonadi::Item &item, items ) {
         if ( item.hasPayload<KABC::ContactGroup>() ) {
            qDebug() << "Group:" << item.payload<KABC::ContactGroup>().name();
         }

         if ( item.hasPayload<KABC::Addressee>() ) {
            KABC::Addressee tmp = item.payload<KABC::Addressee>();
            Contact* aContact   = new Contact();
68
            m_pAddrHash[tmp.uid()] = tmp;
69
70
71
72
73
74
75
            
            KABC::PhoneNumber::List numbers = tmp.phoneNumbers();
            PhoneNumbers newNumbers;
            foreach (KABC::PhoneNumber number, numbers) {
               newNumbers << new Contact::PhoneNumber(number.number(),number.typeLabel());
               m_pContactByPhone[number.number()] = aContact;
            }
76
            m_pContactByUid[tmp.uid()] = aContact;
77
78
79
80
81
82
83
            
            aContact->setNickName       (tmp.nickName()       );
            aContact->setFormattedName  (tmp.formattedName()  );
            aContact->setFirstName      (tmp.givenName()      );
            aContact->setFamilyName     (tmp.familyName()     );
            aContact->setOrganization   (tmp.organization()   );
            aContact->setPreferredEmail (tmp.preferredEmail() );
84
            aContact->setUid            (tmp.uid()            );
85
86
87
88
89
90
91
92
93
            aContact->setPhoneNumbers   (newNumbers           );
            
            if (!tmp.photo().data().isNull())
               aContact->setPhoto(new QPixmap(QPixmap::fromImage( tmp.photo().data()).scaled(QSize(48,48))));
            else
               aContact->setPhoto(0);
            contacts << aContact;
         }
      }
94
   }
95
   return contacts;
96
97
}

98
99
100
101
102
103
104
ContactList AkonadiBackend::update()
{
   return update(m_pCollection);
}


Contact* AkonadiBackend::getContactByPhone(QString phoneNumber)
105
{
106
107
108
   return m_pContactByPhone[phoneNumber];
}

109
110
111
112
113
Contact* AkonadiBackend::getContactByUid(QString uid)
{
   return m_pContactByUid[uid];
}

114
115
116
117
118
119
void AkonadiBackend::collectionsReceived( const Akonadi::Collection::List&  list)
{
   foreach (Akonadi::Collection coll, list) {
      update(coll);
      emit collectionChanged();
   }
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
}

void AkonadiBackend::editContact(Contact* contact)
{
   KABC::Addressee ct = m_pAddrHash[contact->getUid()];
   if (ct.uid() != contact->getUid()) {
      qDebug() << "Contact not found";
      return;
   }
   Akonadi::ContactEditor *editor = new Akonadi::ContactEditor( Akonadi::ContactEditor::EditMode, SFLPhone::app()->view() );
   Akonadi::Item item;
   item.setPayload<KABC::Addressee>(ct);
   editor->loadContact(item);
   KDialog* dlg = new KDialog(SFLPhone::app()->view());
   dlg->setMainWidget(editor);
   dlg->exec();
}

void AkonadiBackend::addNewContact(Contact* contact)
{
   KABC::Addressee newContact;
   newContact.setNickName       ( contact->getNickName()        );
   newContact.setFormattedName  ( contact->getFormattedName()   );
   newContact.setGivenName      ( contact->getFirstName()       );
   newContact.setFamilyName     ( contact->getSecondName()      );
   newContact.setOrganization   ( contact->getOrganization()    );
   //newContact.setPreferredEmail ( contact->getPreferredEmail()  );//TODO

            
   //aContact->setPhoneNumbers   (newNumbers           );//TODO
   
    Akonadi::ContactEditor *editor = new Akonadi::ContactEditor( Akonadi::ContactEditor::CreateMode, SFLPhone::app()->view() );

   editor->setContactTemplate(newContact);
   
   KDialog* dlg = new KDialog(SFLPhone::app()->view());
   dlg->setMainWidget(editor);
   dlg->exec();
   
    
    
   if ( !editor->saveContact() ) {
      qDebug() << "Unable to save new contact to storage";
      return;
   }
165
}