Trying to use the window ID to make the dialog appear (no success).
[akonadigoogledata.git] / googledataresource.cpp
blob0778620a9a8dd38e7afc651e223cca06cf2e230f
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;
158 if (windowId)
159 dlgConf = new dlgGoogleDataConf(QWidget::find(windowId));
160 else
161 dlgConf = new dlgGoogleDataConf;
163 dlgConf->show();
164 sleep(10);
166 user = const_cast<char *>(qPrintable(dlgConf->eAccount->text()));
167 pass = const_cast<char *>(qPrintable(dlgConf->ePass->text()));
168 if (user)
169 if (pass)
170 result = gcal_get_authentication(gcal, user, pass);
172 /* TODO: in case of authentication error, display an error
173 * message.
175 if (!result)
176 authenticated = true;
178 synchronize();
179 delete dlgConf;
182 void GoogleDataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
185 KABC::Addressee addressee;
186 gcal_contact_t contact;
187 QString temp;
188 int result;
190 if (!authenticated)
191 exit(1);
193 if (item.hasPayload<KABC::Addressee>())
194 addressee = item.payload<KABC::Addressee>();
196 if (!(contact = gcal_contact_new(NULL)))
197 exit(1);
199 temp = addressee.realName();
200 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
202 temp = addressee.fullEmail();
203 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
205 /* TODO: add remaining fields */
207 if ((result = gcal_add_contact(gcal, contact)))
208 exit(1);
210 gcal_contact_delete(contact);
214 void GoogleDataResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
217 KABC::Addressee addressee;
218 gcal_contact_t contact;
219 KABC::Key key;
220 QString temp;
221 int result;
223 if (!authenticated)
224 exit(1);
226 if (item.hasPayload<KABC::Addressee>())
227 addressee = item.payload<KABC::Addressee>();
229 if (!(contact = gcal_contact_new(NULL)))
230 exit(1);
232 temp = addressee.realName();
233 gcal_contact_set_title(contact, const_cast<char *>(qPrintable(temp)));
235 temp = addressee.fullEmail();
236 gcal_contact_set_email(contact, const_cast<char *>(qPrintable(temp)));
238 temp = addressee.uid();
239 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
241 /* I suppose that this retrieves the first element in the key list */
242 key = addressee.keys()[0];
243 temp = key.id();
244 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
247 /* TODO: add remaining fields */
249 if ((result = gcal_update_contact(gcal, contact)))
250 exit(1);
253 /* Updates the ETag/url: I suppose that akonadi will save this object */
254 temp = gcal_contact_get_url(contact);
255 addressee.setUid(temp);
257 temp = gcal_contact_get_etag(contact);
258 key.setId(temp);
259 addressee.insertKey(key);
262 gcal_contact_delete(contact);
266 void GoogleDataResource::itemRemoved( const Akonadi::Item &item )
268 KABC::Addressee addressee;
269 gcal_contact_t contact;
270 KABC::Key key;
271 QString temp;
272 int result;
274 if (!authenticated)
275 exit(1);
277 if (item.hasPayload<KABC::Addressee>())
278 addressee = item.payload<KABC::Addressee>();
280 if (!(contact = gcal_contact_new(NULL)))
281 exit(1);
283 temp = addressee.uid();
284 gcal_contact_set_id(contact, const_cast<char *>(qPrintable(temp)));
286 /* I suppose that this retrieves the first element in the key list */
287 key = addressee.keys()[0];
288 temp = key.id();
289 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
291 if ((result = gcal_erase_contact(gcal, contact)))
292 exit(1);
294 gcal_contact_delete(contact);
298 AKONADI_RESOURCE_MAIN( GoogleDataResource )
300 #include "googledataresource.moc"