Adjusting class name plus member functions to akonadi coding style
[akonadigoogledata.git] / googledataresource.cpp
blobf2501f7c8f4bfe02757459ae0194b20265a07512
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 <kabc/errorhandler.h>
11 #include <qstring.h>
13 extern "C" {
14 #include <gcalendar.h>
15 #include <gcontact.h>
16 #include <gcal_status.h>
19 using namespace Akonadi;
21 GoogleDataResource::GoogleDataResource( const QString &id )
22 : ResourceBase( id )
24 new SettingsAdaptor( Settings::self() );
25 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
26 Settings::self(), QDBusConnection::ExportAdaptors );
29 if (!(gcal = gcal_new(GCONTACT)))
30 exit(1);
31 gcal_set_store_xml(gcal, 1);
34 GoogleDataResource::~GoogleDataResource()
36 gcal_delete(gcal);
37 gcal_cleanup_contacts(&all_contacts);
40 void GoogleDataResource::retrieveCollections()
42 // if ( mBaseResource == 0 ) {
43 // kError() << "No Google Contacts resource";
44 // const QString message = i18nc( "@info:status", "No Google Contact configured yet" );
45 // emit error( message );
47 // emit status( Broken, message );
48 // return;
49 // }
51 Collection c;
52 c.setParent(Collection::root());
53 c.setRemoteId("google-contacts");
54 c.setName(name());
56 QStringList mimeTypes;
57 mimeTypes << "text/directory";
58 c.setContentMimeTypes(mimeTypes);
60 Collection::List list;
61 list << c;
62 collectionsRetrieved(list);
66 void GoogleDataResource::retrieveItems( const Akonadi::Collection &collection )
68 Q_UNUSED( collection );
70 Item::List items;
71 int result;
73 /* Downloading the contacts can be slow and it is blocking. Will
74 * it mess up with akonadi?
76 if ((result = gcal_get_contacts(gcal, &all_contacts)))
77 exit(1);
79 /* Each google entry has a unique ID and edit_url */
80 for (size_t i = 0; i < all_contacts.length; ++i) {
82 Item item(QLatin1String("text/directory"));
83 gcal_contact_t contact = gcal_contact_element(&all_contacts, i);
84 item.setRemoteId(gcal_contact_get_id(contact));
86 items << item;
89 itemsRetrieved(items);
92 bool GoogleDataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
94 Q_UNUSED( parts );
95 const QString entry_id = item.remoteId();
96 QString temp;
97 Item newItem(item);
98 gcal_contact_t contact;
99 KABC::Addressee addressee;
100 KABC::PhoneNumber number;
101 KABC::Key key;
104 * And another question, are the requests in the same sequence that
105 * I informed in 'retrieveItems'? For while, I try to locate the entry...
107 for (size_t i = 0; i < all_contacts.length; ++i) {
108 contact = gcal_contact_element(&all_contacts, i);
109 if (entry_id == gcal_contact_get_id(contact)) {
110 /* name */
111 temp = gcal_contact_get_title(contact);
112 addressee.setNameFromString(temp);
114 /* email */
115 temp = gcal_contact_get_email(contact);
116 addressee.insertEmail(temp, true);
118 /* edit url: required to do edit/delete */
119 temp = gcal_contact_get_url(contact);
120 addressee.setUid(temp);
122 /* ETag: required by Google Data protocol 2.0 */
123 temp = gcal_contact_get_etag(contact);
124 key.setId(temp);
125 addressee.insertKey(key);
127 /* TODO: telefone, address, etc */
129 newItem.setPayload<KABC::Addressee>(addressee);
130 return true;
135 return false;
138 void GoogleDataResource::aboutToQuit()
140 // TODO: any cleanup you need to do while there is still an active
141 // event loop. The resource will terminate after this method returns
144 void GoogleDataResource::configure( WId windowId )
146 Q_UNUSED( windowId );
148 /* TODO:
149 * what kind of dialog to collect google acount username + password ?
151 synchronize();
154 void GoogleDataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
157 KABC::Addressee addressee;
158 gcal_contact_t contact;
159 QString temp;
160 int result;
162 if (item.hasPayload<KABC::Addressee>())
163 addressee = item.payload<KABC::Addressee>();
165 if (!(contact = gcal_contact_new(NULL)))
166 exit(1);
168 temp = addressee.realName();
169 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
171 temp = addressee.fullEmail();
172 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
174 /* TODO: add remaining fields */
176 if ((result = gcal_add_contact(gcal, contact)))
177 exit(1);
179 gcal_contact_delete(contact);
183 void GoogleDataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
186 KABC::Addressee addressee;
187 gcal_contact_t contact;
188 KABC::Key key;
189 QString temp;
190 int result;
192 if (item.hasPayload<KABC::Addressee>())
193 addressee = item.payload<KABC::Addressee>();
195 if (!(contact = gcal_contact_new(NULL)))
196 exit(1);
198 temp = addressee.realName();
199 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
201 temp = addressee.fullEmail();
202 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
204 temp = addressee.uid();
205 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
207 /* I suppose that this retrieves the first element in the key list */
208 key = addressee.keys()[0];
209 temp = key.id();
210 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
213 /* TODO: add remaining fields */
215 if ((result = gcal_update_contact(gcal, contact)))
216 exit(1);
219 /* Updates the ETag/url: I suppose that akonadi will save this object */
220 temp = gcal_contact_get_url(contact);
221 addressee.setUid(temp);
223 temp = gcal_contact_get_etag(contact);
224 key.setId(temp);
225 addressee.insertKey(key);
228 gcal_contact_delete(contact);
232 void GoogleDataResource::itemRemoved( const Akonadi::Item &item )
234 KABC::Addressee addressee;
235 gcal_contact_t contact;
236 KABC::Key key;
237 QString temp;
238 int result;
240 if (item.hasPayload<KABC::Addressee>())
241 addressee = item.payload<KABC::Addressee>();
243 if (!(contact = gcal_contact_new(NULL)))
244 exit(1);
246 temp = addressee.uid();
247 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
249 /* I suppose that this retrieves the first element in the key list */
250 key = addressee.keys()[0];
251 temp = key.id();
252 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
254 if ((result = gcal_erase_contact(gcal, contact)))
255 exit(1);
257 gcal_contact_delete(contact);
261 AKONADI_RESOURCE_MAIN( GoogleDataResource )
263 #include "googledataresource.moc"