Adding the items to items list.
[akonadigoogledata.git] / googledataresource.cpp
blob866133addcbfe068d58649cbeb5f54f0d7b81792
1 #include "googledataresource.h"
3 #include "settings.h"
4 #include "settingsadaptor.h"
6 #include <QtDBus/QDBusConnection>
7 #include <kabc/addressee.h>
8 #include <kabc/phonenumber.h>
9 #include <kabc/key.h>
11 extern "C" {
12 #include <gcalendar.h>
13 #include <gcontact.h>
14 #include <gcal_status.h>
17 using namespace Akonadi;
19 googledataResource::googledataResource( const QString &id )
20 : ResourceBase( id )
22 new SettingsAdaptor( Settings::self() );
23 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
24 Settings::self(), QDBusConnection::ExportAdaptors );
27 if (!(gcal = gcal_new(GCONTACT)))
28 exit(1);
29 gcal_set_store_xml(gcal, 1);
32 googledataResource::~googledataResource()
34 gcal_delete(gcal);
35 gcal_cleanup_contacts(&all_contacts);
38 void googledataResource::retrieveCollections()
40 // TODO: this method is called when Akonadi wants to have all the
41 // collections your resource provides.
42 // Be sure to set the remote ID and the content MIME types
45 void googledataResource::retrieveItems( const Akonadi::Collection &collection )
47 Q_UNUSED( collection );
49 Item::List items;
50 int result;
52 /* Downloading the contacts can be slow and it is blocking. Will
53 * it mess up with akonadi?
55 if ((result = gcal_get_contacts(gcal, &all_contacts)))
56 exit(1);
58 /* Each google entry has a unique ID and edit_url */
59 for (size_t i = 0; i < all_contacts.length; ++i) {
61 Item item(QLatin1String("what_is_the_mime_type?"));
62 gcal_contact_t contact = gcal_contact_element(&all_contacts, i);
63 item.setRemoteId(gcal_contact_get_id(contact));
65 items << item;
68 itemsRetrieved(items);
71 bool googledataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
73 Q_UNUSED( parts );
74 const QString entry_id = item.remoteId();
75 QString temp;
76 Item newItem(item);
77 gcal_contact_t contact;
78 KABC::Addressee addressee;
79 KABC::PhoneNumber number;
80 KABC::Key key;
83 * And another question, are the requests in the same sequence that
84 * I informed in 'retrieveItems'? For while, I try to locate the entry...
86 for (size_t i = 0; i < all_contacts.length; ++i) {
87 contact = gcal_contact_element(&all_contacts, i);
88 if (entry_id == gcal_contact_get_id(contact)) {
89 /* name */
90 temp = gcal_contact_get_title(contact);
91 addressee.setNameFromString(temp);
93 /* email */
94 temp = gcal_contact_get_email(contact);
95 addressee.insertEmail(temp, true);
97 /* edit url: required to do edit/delete */
98 temp = gcal_contact_get_url(contact);
99 addressee.setUid(temp);
101 /* ETag: required by Google Data protocol 2.0 */
102 temp = gcal_contact_get_etag(contact);
103 key.setId(temp);
104 addressee.insertKey(key);
106 /* TODO: telefone, address, etc */
108 newItem.setPayload<KABC::Addressee>(addressee);
109 return true;
114 return false;
117 void googledataResource::aboutToQuit()
119 // TODO: any cleanup you need to do while there is still an active
120 // event loop. The resource will terminate after this method returns
123 void googledataResource::configure( WId windowId )
125 Q_UNUSED( windowId );
127 /* TODO:
128 * what kind of dialog to collect google acount username + password ?
130 synchronize();
133 void googledataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
135 Q_UNUSED( item );
136 Q_UNUSED( collection );
138 // TODO: this method is called when somebody else, e.g. a client application,
139 // has created an item in a collection managed by your resource.
141 // NOTE: There is an equivalent method for collections, but it isn't part
142 // of this template code to keep it simple
145 void googledataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
147 Q_UNUSED( item );
148 Q_UNUSED( parts );
150 // TODO: this method is called when somebody else, e.g. a client application,
151 // has changed an item managed by your resource.
153 // NOTE: There is an equivalent method for collections, but it isn't part
154 // of this template code to keep it simple
157 void googledataResource::itemRemoved( const Akonadi::Item &item )
159 Q_UNUSED( item );
161 // TODO: this method is called when somebody else, e.g. a client application,
162 // has deleted an item managed by your resource.
164 // NOTE: There is an equivalent method for collections, but it isn't part
165 // of this template code to keep it simple
168 AKONADI_RESOURCE_MAIN( googledataResource )
170 #include "googledataresource.moc"