Adding a flag to hold if user correctly authenticated with google servers.
[akonadigoogledata.git] / googledataresource.cpp
blob4e7dabd12b53aaaf13476e34e5363d5a78d75534
1 #include "googledataresource.h"
3 #include "settings.h"
4 #include "settingsadaptor.h"
5 #include "dlgGoogleDataConf.h"
7 #include <QtDBus/QDBusConnection>
8 #include <kabc/addressee.h>
9 #include <kabc/phonenumber.h>
10 #include <kabc/key.h>
11 #include <kabc/errorhandler.h>
12 #include <qstring.h>
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)
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);
41 void GoogleDataResource::retrieveCollections()
43 // if ( mBaseResource == 0 ) {
44 // kError() << "No Google Contacts resource";
45 // const QString message = i18nc( "@info:status", "No Google Contact configured yet" );
46 // emit error( message );
48 // emit status( Broken, message );
49 // return;
50 // }
52 Collection c;
53 c.setParent(Collection::root());
54 c.setRemoteId("google-contacts");
55 c.setName(name());
57 QStringList mimeTypes;
58 mimeTypes << "text/directory";
59 c.setContentMimeTypes(mimeTypes);
61 Collection::List list;
62 list << c;
63 collectionsRetrieved(list);
67 void GoogleDataResource::retrieveItems( const Akonadi::Collection &collection )
69 Q_UNUSED( collection );
71 Item::List items;
72 int result;
74 if (!authenticated)
75 exit(1);
77 /* Downloading the contacts can be slow and it is blocking. Will
78 * it mess up with akonadi?
80 if ((result = gcal_get_contacts(gcal, &all_contacts)))
81 exit(1);
83 /* Each google entry has a unique ID and edit_url */
84 for (size_t i = 0; i < all_contacts.length; ++i) {
86 Item item(QLatin1String("text/directory"));
87 gcal_contact_t contact = gcal_contact_element(&all_contacts, i);
88 item.setRemoteId(gcal_contact_get_id(contact));
90 items << item;
93 itemsRetrieved(items);
96 bool GoogleDataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
98 Q_UNUSED( parts );
99 const QString entry_id = item.remoteId();
100 QString temp;
101 Item newItem(item);
102 gcal_contact_t contact;
103 KABC::Addressee addressee;
104 KABC::PhoneNumber number;
105 KABC::Key key;
107 if (!authenticated)
108 exit(1);
111 * And another question, are the requests in the same sequence that
112 * I informed in 'retrieveItems'? For while, I try to locate the entry...
114 for (size_t i = 0; i < all_contacts.length; ++i) {
115 contact = gcal_contact_element(&all_contacts, i);
116 if (entry_id == gcal_contact_get_id(contact)) {
117 /* name */
118 temp = gcal_contact_get_title(contact);
119 addressee.setNameFromString(temp);
121 /* email */
122 temp = gcal_contact_get_email(contact);
123 addressee.insertEmail(temp, true);
125 /* edit url: required to do edit/delete */
126 temp = gcal_contact_get_url(contact);
127 addressee.setUid(temp);
129 /* ETag: required by Google Data protocol 2.0 */
130 temp = gcal_contact_get_etag(contact);
131 key.setId(temp);
132 addressee.insertKey(key);
134 /* TODO: telefone, address, etc */
136 newItem.setPayload<KABC::Addressee>(addressee);
137 return true;
142 return false;
145 void GoogleDataResource::aboutToQuit()
147 // TODO: any cleanup you need to do while there is still an active
148 // event loop. The resource will terminate after this method returns
151 void GoogleDataResource::configure( WId windowId )
153 Q_UNUSED( windowId );
154 char *user, *pass;
155 int result = -1;
157 dlgGoogleDataConf *dlgConf = new dlgGoogleDataConf;
158 dlgConf->show();
160 user = const_cast<char *>(qPrintable(dlgConf->eAccount->text()));
161 pass = const_cast<char *>(qPrintable(dlgConf->ePass->text()));
162 if (user)
163 if (pass)
164 result = gcal_get_authentication(gcal, user, pass);
166 /* TODO: in case of authentication error, display an error
167 * message.
169 if (!result)
170 authenticated = true;
172 synchronize();
173 delete dlgConf;
176 void GoogleDataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
179 KABC::Addressee addressee;
180 gcal_contact_t contact;
181 QString temp;
182 int result;
184 if (!authenticated)
185 exit(1);
187 if (item.hasPayload<KABC::Addressee>())
188 addressee = item.payload<KABC::Addressee>();
190 if (!(contact = gcal_contact_new(NULL)))
191 exit(1);
193 temp = addressee.realName();
194 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
196 temp = addressee.fullEmail();
197 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
199 /* TODO: add remaining fields */
201 if ((result = gcal_add_contact(gcal, contact)))
202 exit(1);
204 gcal_contact_delete(contact);
208 void GoogleDataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
211 KABC::Addressee addressee;
212 gcal_contact_t contact;
213 KABC::Key key;
214 QString temp;
215 int result;
217 if (!authenticated)
218 exit(1);
220 if (item.hasPayload<KABC::Addressee>())
221 addressee = item.payload<KABC::Addressee>();
223 if (!(contact = gcal_contact_new(NULL)))
224 exit(1);
226 temp = addressee.realName();
227 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
229 temp = addressee.fullEmail();
230 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
232 temp = addressee.uid();
233 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
235 /* I suppose that this retrieves the first element in the key list */
236 key = addressee.keys()[0];
237 temp = key.id();
238 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
241 /* TODO: add remaining fields */
243 if ((result = gcal_update_contact(gcal, contact)))
244 exit(1);
247 /* Updates the ETag/url: I suppose that akonadi will save this object */
248 temp = gcal_contact_get_url(contact);
249 addressee.setUid(temp);
251 temp = gcal_contact_get_etag(contact);
252 key.setId(temp);
253 addressee.insertKey(key);
256 gcal_contact_delete(contact);
260 void GoogleDataResource::itemRemoved( const Akonadi::Item &item )
262 KABC::Addressee addressee;
263 gcal_contact_t contact;
264 KABC::Key key;
265 QString temp;
266 int result;
268 if (!authenticated)
269 exit(1);
271 if (item.hasPayload<KABC::Addressee>())
272 addressee = item.payload<KABC::Addressee>();
274 if (!(contact = gcal_contact_new(NULL)))
275 exit(1);
277 temp = addressee.uid();
278 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
280 /* I suppose that this retrieves the first element in the key list */
281 key = addressee.keys()[0];
282 temp = key.id();
283 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
285 if ((result = gcal_erase_contact(gcal, contact)))
286 exit(1);
288 gcal_contact_delete(contact);
292 AKONADI_RESOURCE_MAIN( GoogleDataResource )
294 #include "googledataresource.moc"