1 #include "googledataresource.h"
4 #include "settingsadaptor.h"
6 #include <QtDBus/QDBusConnection>
7 #include <kabc/addressee.h>
8 #include <kabc/phonenumber.h>
10 #include <kabc/errorhandler.h>
14 #include <gcalendar.h>
16 #include <gcal_status.h>
19 using namespace Akonadi
;
21 GoogleDataResource::GoogleDataResource( const QString
&id
)
24 new SettingsAdaptor( Settings::self() );
25 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
26 Settings::self(), QDBusConnection::ExportAdaptors
);
29 if (!(gcal
= gcal_new(GCONTACT
)))
31 gcal_set_store_xml(gcal
, 1);
34 GoogleDataResource::~GoogleDataResource()
37 gcal_cleanup_contacts(&all_contacts
);
40 void GoogleDataResource::retrieveCollections()
42 // if ( mBaseResource == 0 ) {
43 // kError() << "No Google Contacts resource";
44 // const QString message = i18nc( "@info:status", "No Google Contact configured yet" );
45 // emit error( message );
47 // emit status( Broken, message );
52 c
.setParent(Collection::root());
53 c
.setRemoteId("google-contacts");
56 QStringList mimeTypes
;
57 mimeTypes
<< "text/directory";
58 c
.setContentMimeTypes(mimeTypes
);
60 Collection::List list
;
62 collectionsRetrieved(list
);
66 void GoogleDataResource::retrieveItems( const Akonadi::Collection
&collection
)
68 Q_UNUSED( collection
);
73 /* Downloading the contacts can be slow and it is blocking. Will
74 * it mess up with akonadi?
76 if ((result
= gcal_get_contacts(gcal
, &all_contacts
)))
79 /* Each google entry has a unique ID and edit_url */
80 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
82 Item
item(QLatin1String("text/directory"));
83 gcal_contact_t contact
= gcal_contact_element(&all_contacts
, i
);
84 item
.setRemoteId(gcal_contact_get_id(contact
));
89 itemsRetrieved(items
);
92 bool GoogleDataResource::retrieveItem( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
95 const QString entry_id
= item
.remoteId();
98 gcal_contact_t contact
;
99 KABC::Addressee addressee
;
100 KABC::PhoneNumber number
;
104 * And another question, are the requests in the same sequence that
105 * I informed in 'retrieveItems'? For while, I try to locate the entry...
107 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
108 contact
= gcal_contact_element(&all_contacts
, i
);
109 if (entry_id
== gcal_contact_get_id(contact
)) {
111 temp
= gcal_contact_get_title(contact
);
112 addressee
.setNameFromString(temp
);
115 temp
= gcal_contact_get_email(contact
);
116 addressee
.insertEmail(temp
, true);
118 /* edit url: required to do edit/delete */
119 temp
= gcal_contact_get_url(contact
);
120 addressee
.setUid(temp
);
122 /* ETag: required by Google Data protocol 2.0 */
123 temp
= gcal_contact_get_etag(contact
);
125 addressee
.insertKey(key
);
127 /* TODO: telefone, address, etc */
129 newItem
.setPayload
<KABC::Addressee
>(addressee
);
138 void GoogleDataResource::aboutToQuit()
140 // TODO: any cleanup you need to do while there is still an active
141 // event loop. The resource will terminate after this method returns
144 void GoogleDataResource::configure( WId windowId
)
146 Q_UNUSED( windowId
);
149 * what kind of dialog to collect google acount username + password ?
154 void GoogleDataResource::itemAdded( const Akonadi::Item
&item
, const Akonadi::Collection
&collection
)
157 KABC::Addressee addressee
;
158 gcal_contact_t contact
;
162 if (item
.hasPayload
<KABC::Addressee
>())
163 addressee
= item
.payload
<KABC::Addressee
>();
165 if (!(contact
= gcal_contact_new(NULL
)))
168 temp
= addressee
.realName();
169 gcal_contact_set_title(contact
, const_cast<char *>(qPrintable(temp
)));
171 temp
= addressee
.fullEmail();
172 gcal_contact_set_email(contact
, const_cast<char *>(qPrintable(temp
)));
174 /* TODO: add remaining fields */
176 if ((result
= gcal_add_contact(gcal
, contact
)))
179 gcal_contact_delete(contact
);
183 void GoogleDataResource::itemChanged( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
186 KABC::Addressee addressee
;
187 gcal_contact_t contact
;
192 if (item
.hasPayload
<KABC::Addressee
>())
193 addressee
= item
.payload
<KABC::Addressee
>();
195 if (!(contact
= gcal_contact_new(NULL
)))
198 temp
= addressee
.realName();
199 gcal_contact_set_title(contact
, const_cast<char *>(qPrintable(temp
)));
201 temp
= addressee
.fullEmail();
202 gcal_contact_set_email(contact
, const_cast<char *>(qPrintable(temp
)));
204 temp
= addressee
.uid();
205 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
207 /* I suppose that this retrieves the first element in the key list */
208 key
= addressee
.keys()[0];
210 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
213 /* TODO: add remaining fields */
215 if ((result
= gcal_update_contact(gcal
, contact
)))
219 /* Updates the ETag/url: I suppose that akonadi will save this object */
220 temp
= gcal_contact_get_url(contact
);
221 addressee
.setUid(temp
);
223 temp
= gcal_contact_get_etag(contact
);
225 addressee
.insertKey(key
);
228 gcal_contact_delete(contact
);
232 void GoogleDataResource::itemRemoved( const Akonadi::Item
&item
)
234 KABC::Addressee addressee
;
235 gcal_contact_t contact
;
240 if (item
.hasPayload
<KABC::Addressee
>())
241 addressee
= item
.payload
<KABC::Addressee
>();
243 if (!(contact
= gcal_contact_new(NULL
)))
246 temp
= addressee
.uid();
247 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
249 /* I suppose that this retrieves the first element in the key list */
250 key
= addressee
.keys()[0];
252 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
254 if ((result
= gcal_erase_contact(gcal
, contact
)))
257 gcal_contact_delete(contact
);
261 AKONADI_RESOURCE_MAIN( GoogleDataResource
)
263 #include "googledataresource.moc"