Ouch! Forgot to set the collection Id. Now it compiles.
[akonadigoogledata.git] / googledataresource.cpp
blob4ab289f56e00993782e76291cdd90e087eac27e0
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 Collection c;
41 c.setParent(Collection::root());
42 c.setRemoteId("google-contacts");
43 c.setName(name());
45 QStringList mimeTypes;
46 mimeTypes << "text/directory";
47 c.setContentMimeTypes(mimeTypes);
49 Collection::List list;
50 list << c;
51 collectionsRetrieved(list);
55 void googledataResource::retrieveItems( const Akonadi::Collection &collection )
57 Q_UNUSED( collection );
59 Item::List items;
60 int result;
62 /* Downloading the contacts can be slow and it is blocking. Will
63 * it mess up with akonadi?
65 if ((result = gcal_get_contacts(gcal, &all_contacts)))
66 exit(1);
68 /* Each google entry has a unique ID and edit_url */
69 for (size_t i = 0; i < all_contacts.length; ++i) {
71 Item item(QLatin1String("text/directory"));
72 gcal_contact_t contact = gcal_contact_element(&all_contacts, i);
73 item.setRemoteId(gcal_contact_get_id(contact));
75 items << item;
78 itemsRetrieved(items);
81 bool googledataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
83 Q_UNUSED( parts );
84 const QString entry_id = item.remoteId();
85 QString temp;
86 Item newItem(item);
87 gcal_contact_t contact;
88 KABC::Addressee addressee;
89 KABC::PhoneNumber number;
90 KABC::Key key;
93 * And another question, are the requests in the same sequence that
94 * I informed in 'retrieveItems'? For while, I try to locate the entry...
96 for (size_t i = 0; i < all_contacts.length; ++i) {
97 contact = gcal_contact_element(&all_contacts, i);
98 if (entry_id == gcal_contact_get_id(contact)) {
99 /* name */
100 temp = gcal_contact_get_title(contact);
101 addressee.setNameFromString(temp);
103 /* email */
104 temp = gcal_contact_get_email(contact);
105 addressee.insertEmail(temp, true);
107 /* edit url: required to do edit/delete */
108 temp = gcal_contact_get_url(contact);
109 addressee.setUid(temp);
111 /* ETag: required by Google Data protocol 2.0 */
112 temp = gcal_contact_get_etag(contact);
113 key.setId(temp);
114 addressee.insertKey(key);
116 /* TODO: telefone, address, etc */
118 newItem.setPayload<KABC::Addressee>(addressee);
119 return true;
124 return false;
127 void googledataResource::aboutToQuit()
129 // TODO: any cleanup you need to do while there is still an active
130 // event loop. The resource will terminate after this method returns
133 void googledataResource::configure( WId windowId )
135 Q_UNUSED( windowId );
137 /* TODO:
138 * what kind of dialog to collect google acount username + password ?
140 synchronize();
143 void googledataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
145 Q_UNUSED( item );
146 Q_UNUSED( collection );
148 // TODO: this method is called when somebody else, e.g. a client application,
149 // has created an item in a collection managed by your resource.
151 // NOTE: There is an equivalent method for collections, but it isn't part
152 // of this template code to keep it simple
155 void googledataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
157 Q_UNUSED( item );
158 Q_UNUSED( parts );
160 // TODO: this method is called when somebody else, e.g. a client application,
161 // has changed an item managed by your resource.
163 // NOTE: There is an equivalent method for collections, but it isn't part
164 // of this template code to keep it simple
167 void googledataResource::itemRemoved( const Akonadi::Item &item )
169 Q_UNUSED( item );
171 // TODO: this method is called when somebody else, e.g. a client application,
172 // has deleted an item managed by your resource.
174 // NOTE: There is an equivalent method for collections, but it isn't part
175 // of this template code to keep it simple
178 AKONADI_RESOURCE_MAIN( googledataResource )
180 #include "googledataresource.moc"