Not a really good commit, but it fixes:
[akonadigoogledata.git] / googledataresource.cpp
blob92339f02d358e2c8497b115b40a00e1f243ae63d
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>
12 #include <KWindowSystem>
14 extern "C" {
15 #include <gcalendar.h>
16 #include <gcontact.h>
17 #include <gcal_status.h>
20 using namespace Akonadi;
22 GoogleDataResource::GoogleDataResource( const QString &id )
23 : ResourceBase( id ), authenticated(false), dlgConf(NULL)
25 new SettingsAdaptor( Settings::self() );
26 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
27 Settings::self(), QDBusConnection::ExportAdaptors );
30 if (!(gcal = gcal_new(GCONTACT)))
31 exit(1);
32 gcal_set_store_xml(gcal, 1);
35 GoogleDataResource::~GoogleDataResource()
37 gcal_delete(gcal);
38 gcal_cleanup_contacts(&all_contacts);
39 if (dlgConf)
40 delete dlgConf;
43 void GoogleDataResource::retrieveCollections()
45 // if ( mBaseResource == 0 ) {
46 // kError() << "No Google Contacts resource";
47 // const QString message = i18nc( "@info:status", "No Google Contact configured yet" );
48 // emit error( message );
50 // emit status( Broken, message );
51 // return;
52 // }
54 Collection c;
55 c.setParent(Collection::root());
56 c.setRemoteId("google-contacts");
57 c.setName(name());
59 QStringList mimeTypes;
60 mimeTypes << "text/directory";
61 c.setContentMimeTypes(mimeTypes);
63 Collection::List list;
64 list << c;
65 collectionsRetrieved(list);
69 void GoogleDataResource::retrieveItems( const Akonadi::Collection &collection )
71 Q_UNUSED( collection );
73 Item::List items;
74 int result;
76 if (!authenticated)
77 exit(1);
79 /* Downloading the contacts can be slow and it is blocking. Will
80 * it mess up with akonadi?
82 if ((result = gcal_get_contacts(gcal, &all_contacts)))
83 exit(1);
85 /* Each google entry has a unique ID and edit_url */
86 for (size_t i = 0; i < all_contacts.length; ++i) {
88 Item item(QLatin1String("text/directory"));
89 gcal_contact_t contact = gcal_contact_element(&all_contacts, i);
90 item.setRemoteId(gcal_contact_get_id(contact));
92 items << item;
95 itemsRetrieved(items);
98 bool GoogleDataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
100 Q_UNUSED( parts );
101 const QString entry_id = item.remoteId();
102 QString temp;
103 Item newItem(item);
104 gcal_contact_t contact;
105 KABC::Addressee addressee;
106 KABC::PhoneNumber number;
107 KABC::Key key;
109 if (!authenticated)
110 exit(1);
113 * And another question, are the requests in the same sequence that
114 * I informed in 'retrieveItems'? For while, I try to locate the entry...
116 for (size_t i = 0; i < all_contacts.length; ++i) {
117 contact = gcal_contact_element(&all_contacts, i);
118 if (entry_id == gcal_contact_get_id(contact)) {
119 /* name */
120 temp = gcal_contact_get_title(contact);
121 addressee.setNameFromString(temp);
123 /* email */
124 temp = gcal_contact_get_email(contact);
125 addressee.insertEmail(temp, true);
127 /* edit url: required to do edit/delete */
128 temp = gcal_contact_get_url(contact);
129 addressee.setUid(temp);
131 /* ETag: required by Google Data protocol 2.0 */
132 temp = gcal_contact_get_etag(contact);
133 key.setId(temp);
134 addressee.insertKey(key);
136 /* TODO: telefone, address, etc */
138 newItem.setPayload<KABC::Addressee>(addressee);
139 return true;
144 return false;
147 void GoogleDataResource::aboutToQuit()
149 // TODO: any cleanup you need to do while there is still an active
150 // event loop. The resource will terminate after this method returns
153 void GoogleDataResource::configure( WId windowId )
155 Q_UNUSED( windowId );
156 char *user, *pass;
157 int result = -1;
158 QByteArray byteUser, bytePass;
160 if (!dlgConf)
161 dlgConf = new dlgGoogleDataConf;
163 if (windowId && dlgConf)
164 KWindowSystem::setMainWindow(dlgConf, windowId);
166 dlgConf->exec();
168 byteUser = dlgConf->eAccount->text().toLocal8Bit();
169 bytePass = dlgConf->ePass->text().toLocal8Bit();
170 user = const_cast<char *>(byteUser.constData());
171 pass = const_cast<char *>(bytePass.constData());
172 if (user)
173 if (pass)
174 result = gcal_get_authentication(gcal, user, pass);
176 /* TODO: in case of authentication error, display an error
177 * message.
179 if (!result)
180 authenticated = true;
182 synchronize();
185 void GoogleDataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
188 KABC::Addressee addressee;
189 gcal_contact_t contact;
190 QString temp;
191 int result;
193 if (!authenticated)
194 exit(1);
196 if (item.hasPayload<KABC::Addressee>())
197 addressee = item.payload<KABC::Addressee>();
199 if (!(contact = gcal_contact_new(NULL)))
200 exit(1);
202 temp = addressee.realName();
203 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
205 temp = addressee.fullEmail();
206 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
208 /* TODO: add remaining fields */
210 if ((result = gcal_add_contact(gcal, contact)))
211 exit(1);
213 gcal_contact_delete(contact);
217 void GoogleDataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
220 KABC::Addressee addressee;
221 gcal_contact_t contact;
222 KABC::Key key;
223 QString temp;
224 int result;
226 if (!authenticated)
227 exit(1);
229 if (item.hasPayload<KABC::Addressee>())
230 addressee = item.payload<KABC::Addressee>();
232 if (!(contact = gcal_contact_new(NULL)))
233 exit(1);
235 temp = addressee.realName();
236 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
238 temp = addressee.fullEmail();
239 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
241 temp = addressee.uid();
242 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
244 /* I suppose that this retrieves the first element in the key list */
245 key = addressee.keys()[0];
246 temp = key.id();
247 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
250 /* TODO: add remaining fields */
252 if ((result = gcal_update_contact(gcal, contact)))
253 exit(1);
256 /* Updates the ETag/url: I suppose that akonadi will save this object */
257 temp = gcal_contact_get_url(contact);
258 addressee.setUid(temp);
260 temp = gcal_contact_get_etag(contact);
261 key.setId(temp);
262 addressee.insertKey(key);
265 gcal_contact_delete(contact);
269 void GoogleDataResource::itemRemoved( const Akonadi::Item &item )
271 KABC::Addressee addressee;
272 gcal_contact_t contact;
273 KABC::Key key;
274 QString temp;
275 int result;
277 if (!authenticated)
278 exit(1);
280 if (item.hasPayload<KABC::Addressee>())
281 addressee = item.payload<KABC::Addressee>();
283 if (!(contact = gcal_contact_new(NULL)))
284 exit(1);
286 temp = addressee.uid();
287 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
289 /* I suppose that this retrieves the first element in the key list */
290 key = addressee.keys()[0];
291 temp = key.id();
292 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
294 if ((result = gcal_erase_contact(gcal, contact)))
295 exit(1);
297 gcal_contact_delete(contact);
301 AKONADI_RESOURCE_MAIN( GoogleDataResource )
303 #include "googledataresource.moc"