Adding code to update the contact.
[akonadigoogledata.git] / googledataresource.cpp
blob17875acecec32da6bd79b609097b602c3ee19f17
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>
10 #include <qstring.h>
12 extern "C" {
13 #include <gcalendar.h>
14 #include <gcontact.h>
15 #include <gcal_status.h>
18 using namespace Akonadi;
20 googledataResource::googledataResource( const QString &id )
21 : ResourceBase( id )
23 new SettingsAdaptor( Settings::self() );
24 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
25 Settings::self(), QDBusConnection::ExportAdaptors );
28 if (!(gcal = gcal_new(GCONTACT)))
29 exit(1);
30 gcal_set_store_xml(gcal, 1);
33 googledataResource::~googledataResource()
35 gcal_delete(gcal);
36 gcal_cleanup_contacts(&all_contacts);
39 void googledataResource::retrieveCollections()
41 Collection c;
42 c.setParent(Collection::root());
43 c.setRemoteId("google-contacts");
44 c.setName(name());
46 QStringList mimeTypes;
47 mimeTypes << "text/directory";
48 c.setContentMimeTypes(mimeTypes);
50 Collection::List list;
51 list << c;
52 collectionsRetrieved(list);
56 void googledataResource::retrieveItems( const Akonadi::Collection &collection )
58 Q_UNUSED( collection );
60 Item::List items;
61 int result;
63 /* Downloading the contacts can be slow and it is blocking. Will
64 * it mess up with akonadi?
66 if ((result = gcal_get_contacts(gcal, &all_contacts)))
67 exit(1);
69 /* Each google entry has a unique ID and edit_url */
70 for (size_t i = 0; i < all_contacts.length; ++i) {
72 Item item(QLatin1String("text/directory"));
73 gcal_contact_t contact = gcal_contact_element(&all_contacts, i);
74 item.setRemoteId(gcal_contact_get_id(contact));
76 items << item;
79 itemsRetrieved(items);
82 bool googledataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
84 Q_UNUSED( parts );
85 const QString entry_id = item.remoteId();
86 QString temp;
87 Item newItem(item);
88 gcal_contact_t contact;
89 KABC::Addressee addressee;
90 KABC::PhoneNumber number;
91 KABC::Key key;
94 * And another question, are the requests in the same sequence that
95 * I informed in 'retrieveItems'? For while, I try to locate the entry...
97 for (size_t i = 0; i < all_contacts.length; ++i) {
98 contact = gcal_contact_element(&all_contacts, i);
99 if (entry_id == gcal_contact_get_id(contact)) {
100 /* name */
101 temp = gcal_contact_get_title(contact);
102 addressee.setNameFromString(temp);
104 /* email */
105 temp = gcal_contact_get_email(contact);
106 addressee.insertEmail(temp, true);
108 /* edit url: required to do edit/delete */
109 temp = gcal_contact_get_url(contact);
110 addressee.setUid(temp);
112 /* ETag: required by Google Data protocol 2.0 */
113 temp = gcal_contact_get_etag(contact);
114 key.setId(temp);
115 addressee.insertKey(key);
117 /* TODO: telefone, address, etc */
119 newItem.setPayload<KABC::Addressee>(addressee);
120 return true;
125 return false;
128 void googledataResource::aboutToQuit()
130 // TODO: any cleanup you need to do while there is still an active
131 // event loop. The resource will terminate after this method returns
134 void googledataResource::configure( WId windowId )
136 Q_UNUSED( windowId );
138 /* TODO:
139 * what kind of dialog to collect google acount username + password ?
141 synchronize();
144 void googledataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
147 KABC::Addressee addressee;
148 gcal_contact_t contact;
149 QString temp;
150 QByteArray ugly;
151 int result;
153 if (item.hasPayload<KABC::Addressee>())
154 addressee = item.payload<KABC::Addressee>();
156 if (!(contact = gcal_contact_new(NULL)))
157 exit(1);
159 /* Common... there must exist a better way! I'm using Qt 4.5.
160 * What about the good and old .c_str()?
162 temp = addressee.realName();
163 ugly = temp.toAscii();
164 gcal_contact_set_title(contact, const_cast<char *>(ugly.constData()));
166 temp = addressee.fullEmail();
167 ugly = temp.toAscii();
168 gcal_contact_set_email(contact, const_cast<char *>(ugly.constData()));
170 /* TODO: add remaining fields */
172 if ((result = gcal_add_contact(gcal, contact)))
173 exit(1);
175 gcal_contact_delete(contact);
179 void googledataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
182 KABC::Addressee addressee;
183 gcal_contact_t contact;
184 KABC::Key key;
185 QString temp;
186 int result;
188 if (item.hasPayload<KABC::Addressee>())
189 addressee = item.payload<KABC::Addressee>();
191 if (!(contact = gcal_contact_new(NULL)))
192 exit(1);
194 temp = addressee.realName();
195 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
197 temp = addressee.fullEmail();
198 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
200 temp = addressee.uid();
201 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
203 /* I suppose that this retrieves the first element in the key list */
204 key = addressee.keys()[0];
205 temp = key.id();
206 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
209 /* TODO: add remaining fields */
211 if ((result = gcal_update_contact(gcal, contact)))
212 exit(1);
215 /* Updates the ETag/url: I suppose that akonadi will save this object */
216 temp = gcal_contact_get_url(contact);
217 addressee.setUid(temp);
219 temp = gcal_contact_get_etag(contact);
220 key.setId(temp);
221 addressee.insertKey(key);
224 gcal_contact_delete(contact);
228 void googledataResource::itemRemoved( const Akonadi::Item &item )
230 Q_UNUSED( item );
232 // TODO: this method is called when somebody else, e.g. a client application,
233 // has deleted an item managed by your resource.
235 // NOTE: There is an equivalent method for collections, but it isn't part
236 // of this template code to keep it simple
239 AKONADI_RESOURCE_MAIN( googledataResource )
241 #include "googledataresource.moc"