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
)
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
);
74 /* Downloading the contacts can be slow and it is blocking. Will
75 * it mess up with akonadi?
77 if ((result
= gcal_get_contacts(gcal
, &all_contacts
)))
80 /* Each google entry has a unique ID and edit_url */
81 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
83 Item
item(QLatin1String("text/directory"));
84 gcal_contact_t contact
= gcal_contact_element(&all_contacts
, i
);
85 item
.setRemoteId(gcal_contact_get_id(contact
));
90 itemsRetrieved(items
);
93 bool GoogleDataResource::retrieveItem( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
96 const QString entry_id
= item
.remoteId();
99 gcal_contact_t contact
;
100 KABC::Addressee addressee
;
101 KABC::PhoneNumber number
;
105 * And another question, are the requests in the same sequence that
106 * I informed in 'retrieveItems'? For while, I try to locate the entry...
108 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
109 contact
= gcal_contact_element(&all_contacts
, i
);
110 if (entry_id
== gcal_contact_get_id(contact
)) {
112 temp
= gcal_contact_get_title(contact
);
113 addressee
.setNameFromString(temp
);
116 temp
= gcal_contact_get_email(contact
);
117 addressee
.insertEmail(temp
, true);
119 /* edit url: required to do edit/delete */
120 temp
= gcal_contact_get_url(contact
);
121 addressee
.setUid(temp
);
123 /* ETag: required by Google Data protocol 2.0 */
124 temp
= gcal_contact_get_etag(contact
);
126 addressee
.insertKey(key
);
128 /* TODO: telefone, address, etc */
130 newItem
.setPayload
<KABC::Addressee
>(addressee
);
139 void GoogleDataResource::aboutToQuit()
141 // TODO: any cleanup you need to do while there is still an active
142 // event loop. The resource will terminate after this method returns
145 void GoogleDataResource::configure( WId windowId
)
147 Q_UNUSED( windowId
);
148 dlgGoogleDataConf
*dlgConf
= new dlgGoogleDataConf
;
151 /* TODO: retrieve user + password */
158 void GoogleDataResource::itemAdded( const Akonadi::Item
&item
, const Akonadi::Collection
&collection
)
161 KABC::Addressee addressee
;
162 gcal_contact_t contact
;
166 if (item
.hasPayload
<KABC::Addressee
>())
167 addressee
= item
.payload
<KABC::Addressee
>();
169 if (!(contact
= gcal_contact_new(NULL
)))
172 temp
= addressee
.realName();
173 gcal_contact_set_title(contact
, const_cast<char *>(qPrintable(temp
)));
175 temp
= addressee
.fullEmail();
176 gcal_contact_set_email(contact
, const_cast<char *>(qPrintable(temp
)));
178 /* TODO: add remaining fields */
180 if ((result
= gcal_add_contact(gcal
, contact
)))
183 gcal_contact_delete(contact
);
187 void GoogleDataResource::itemChanged( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
190 KABC::Addressee addressee
;
191 gcal_contact_t contact
;
196 if (item
.hasPayload
<KABC::Addressee
>())
197 addressee
= item
.payload
<KABC::Addressee
>();
199 if (!(contact
= gcal_contact_new(NULL
)))
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 temp
= addressee
.uid();
209 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
211 /* I suppose that this retrieves the first element in the key list */
212 key
= addressee
.keys()[0];
214 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
217 /* TODO: add remaining fields */
219 if ((result
= gcal_update_contact(gcal
, contact
)))
223 /* Updates the ETag/url: I suppose that akonadi will save this object */
224 temp
= gcal_contact_get_url(contact
);
225 addressee
.setUid(temp
);
227 temp
= gcal_contact_get_etag(contact
);
229 addressee
.insertKey(key
);
232 gcal_contact_delete(contact
);
236 void GoogleDataResource::itemRemoved( const Akonadi::Item
&item
)
238 KABC::Addressee addressee
;
239 gcal_contact_t contact
;
244 if (item
.hasPayload
<KABC::Addressee
>())
245 addressee
= item
.payload
<KABC::Addressee
>();
247 if (!(contact
= gcal_contact_new(NULL
)))
250 temp
= addressee
.uid();
251 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
253 /* I suppose that this retrieves the first element in the key list */
254 key
= addressee
.keys()[0];
256 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
258 if ((result
= gcal_erase_contact(gcal
, contact
)))
261 gcal_contact_delete(contact
);
265 AKONADI_RESOURCE_MAIN( GoogleDataResource
)
267 #include "googledataresource.moc"