1 #include "googledataresource.h"
4 #include "settingsadaptor.h"
5 #include "dlgGoogleDataConf.h"
7 #include <QtDBus/QDBusConnection>
8 #include <kabc/addressee.h>
9 #include <kabc/phonenumber.h>
11 #include <kabc/errorhandler.h>
15 #include <gcalendar.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
)))
32 gcal_set_store_xml(gcal
, 1);
35 GoogleDataResource::~GoogleDataResource()
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 );
53 c
.setParent(Collection::root());
54 c
.setRemoteId("google-contacts");
57 QStringList mimeTypes
;
58 mimeTypes
<< "text/directory";
59 c
.setContentMimeTypes(mimeTypes
);
61 Collection::List list
;
63 collectionsRetrieved(list
);
67 void GoogleDataResource::retrieveItems( const Akonadi::Collection
&collection
)
69 Q_UNUSED( collection
);
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
)))
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
));
93 itemsRetrieved(items
);
96 bool GoogleDataResource::retrieveItem( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
99 const QString entry_id
= item
.remoteId();
102 gcal_contact_t contact
;
103 KABC::Addressee addressee
;
104 KABC::PhoneNumber number
;
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
)) {
118 temp
= gcal_contact_get_title(contact
);
119 addressee
.setNameFromString(temp
);
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
);
132 addressee
.insertKey(key
);
134 /* TODO: telefone, address, etc */
136 newItem
.setPayload
<KABC::Addressee
>(addressee
);
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
);
157 dlgGoogleDataConf
*dlgConf
;
159 dlgConf
= new dlgGoogleDataConf(QWidget::find(windowId
));
161 dlgConf
= new dlgGoogleDataConf
;
166 user
= const_cast<char *>(qPrintable(dlgConf
->eAccount
->text()));
167 pass
= const_cast<char *>(qPrintable(dlgConf
->ePass
->text()));
170 result
= gcal_get_authentication(gcal
, user
, pass
);
172 /* TODO: in case of authentication error, display an error
176 authenticated
= true;
182 void GoogleDataResource::itemAdded( const Akonadi::Item
&item
, const Akonadi::Collection
&collection
)
185 KABC::Addressee addressee
;
186 gcal_contact_t contact
;
193 if (item
.hasPayload
<KABC::Addressee
>())
194 addressee
= item
.payload
<KABC::Addressee
>();
196 if (!(contact
= gcal_contact_new(NULL
)))
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
)))
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
;
226 if (item
.hasPayload
<KABC::Addressee
>())
227 addressee
= item
.payload
<KABC::Addressee
>();
229 if (!(contact
= gcal_contact_new(NULL
)))
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];
244 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
247 /* TODO: add remaining fields */
249 if ((result
= gcal_update_contact(gcal
, contact
)))
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
);
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
;
277 if (item
.hasPayload
<KABC::Addressee
>())
278 addressee
= item
.payload
<KABC::Addressee
>();
280 if (!(contact
= gcal_contact_new(NULL
)))
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];
289 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
291 if ((result
= gcal_erase_contact(gcal
, contact
)))
294 gcal_contact_delete(contact
);
298 AKONADI_RESOURCE_MAIN( GoogleDataResource
)
300 #include "googledataresource.moc"