Removing outdated comments.
[akonadigoogledata.git] / googledataresource.cpp
blob14a681cdddaaaac1f2bdfcb40e043ef82ca197bb
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 int result;
152 if (item.hasPayload<KABC::Addressee>())
153 addressee = item.payload<KABC::Addressee>();
155 if (!(contact = gcal_contact_new(NULL)))
156 exit(1);
158 temp = addressee.realName();
159 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
161 temp = addressee.fullEmail();
162 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
164 /* TODO: add remaining fields */
166 if ((result = gcal_add_contact(gcal, contact)))
167 exit(1);
169 gcal_contact_delete(contact);
173 void googledataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
176 KABC::Addressee addressee;
177 gcal_contact_t contact;
178 KABC::Key key;
179 QString temp;
180 int result;
182 if (item.hasPayload<KABC::Addressee>())
183 addressee = item.payload<KABC::Addressee>();
185 if (!(contact = gcal_contact_new(NULL)))
186 exit(1);
188 temp = addressee.realName();
189 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
191 temp = addressee.fullEmail();
192 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
194 temp = addressee.uid();
195 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
197 /* I suppose that this retrieves the first element in the key list */
198 key = addressee.keys()[0];
199 temp = key.id();
200 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
203 /* TODO: add remaining fields */
205 if ((result = gcal_update_contact(gcal, contact)))
206 exit(1);
209 /* Updates the ETag/url: I suppose that akonadi will save this object */
210 temp = gcal_contact_get_url(contact);
211 addressee.setUid(temp);
213 temp = gcal_contact_get_etag(contact);
214 key.setId(temp);
215 addressee.insertKey(key);
218 gcal_contact_delete(contact);
222 void googledataResource::itemRemoved( const Akonadi::Item &item )
224 KABC::Addressee addressee;
225 gcal_contact_t contact;
226 KABC::Key key;
227 QString temp;
228 int result;
230 if (item.hasPayload<KABC::Addressee>())
231 addressee = item.payload<KABC::Addressee>();
233 if (!(contact = gcal_contact_new(NULL)))
234 exit(1);
236 temp = addressee.uid();
237 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
239 /* I suppose that this retrieves the first element in the key list */
240 key = addressee.keys()[0];
241 temp = key.id();
242 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
244 if ((result = gcal_erase_contact(gcal, contact)))
245 exit(1);
247 gcal_contact_delete(contact);
251 AKONADI_RESOURCE_MAIN( googledataResource )
253 #include "googledataresource.moc"