1 #include "googledataresource.h"
4 #include "settingsadaptor.h"
6 #include <QtDBus/QDBusConnection>
7 #include <kabc/addressee.h>
8 #include <kabc/phonenumber.h>
13 #include <gcalendar.h>
15 #include <gcal_status.h>
18 using namespace Akonadi
;
20 googledataResource::googledataResource( const QString
&id
)
23 new SettingsAdaptor( Settings::self() );
24 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
25 Settings::self(), QDBusConnection::ExportAdaptors
);
28 if (!(gcal
= gcal_new(GCONTACT
)))
30 gcal_set_store_xml(gcal
, 1);
33 googledataResource::~googledataResource()
36 gcal_cleanup_contacts(&all_contacts
);
39 void googledataResource::retrieveCollections()
42 c
.setParent(Collection::root());
43 c
.setRemoteId("google-contacts");
46 QStringList mimeTypes
;
47 mimeTypes
<< "text/directory";
48 c
.setContentMimeTypes(mimeTypes
);
50 Collection::List list
;
52 collectionsRetrieved(list
);
56 void googledataResource::retrieveItems( const Akonadi::Collection
&collection
)
58 Q_UNUSED( collection
);
63 /* Downloading the contacts can be slow and it is blocking. Will
64 * it mess up with akonadi?
66 if ((result
= gcal_get_contacts(gcal
, &all_contacts
)))
69 /* Each google entry has a unique ID and edit_url */
70 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
72 Item
item(QLatin1String("text/directory"));
73 gcal_contact_t contact
= gcal_contact_element(&all_contacts
, i
);
74 item
.setRemoteId(gcal_contact_get_id(contact
));
79 itemsRetrieved(items
);
82 bool googledataResource::retrieveItem( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
85 const QString entry_id
= item
.remoteId();
88 gcal_contact_t contact
;
89 KABC::Addressee addressee
;
90 KABC::PhoneNumber number
;
94 * And another question, are the requests in the same sequence that
95 * I informed in 'retrieveItems'? For while, I try to locate the entry...
97 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
98 contact
= gcal_contact_element(&all_contacts
, i
);
99 if (entry_id
== gcal_contact_get_id(contact
)) {
101 temp
= gcal_contact_get_title(contact
);
102 addressee
.setNameFromString(temp
);
105 temp
= gcal_contact_get_email(contact
);
106 addressee
.insertEmail(temp
, true);
108 /* edit url: required to do edit/delete */
109 temp
= gcal_contact_get_url(contact
);
110 addressee
.setUid(temp
);
112 /* ETag: required by Google Data protocol 2.0 */
113 temp
= gcal_contact_get_etag(contact
);
115 addressee
.insertKey(key
);
117 /* TODO: telefone, address, etc */
119 newItem
.setPayload
<KABC::Addressee
>(addressee
);
128 void googledataResource::aboutToQuit()
130 // TODO: any cleanup you need to do while there is still an active
131 // event loop. The resource will terminate after this method returns
134 void googledataResource::configure( WId windowId
)
136 Q_UNUSED( windowId
);
139 * what kind of dialog to collect google acount username + password ?
144 void googledataResource::itemAdded( const Akonadi::Item
&item
, const Akonadi::Collection
&collection
)
147 KABC::Addressee addressee
;
148 gcal_contact_t contact
;
152 if (item
.hasPayload
<KABC::Addressee
>())
153 addressee
= item
.payload
<KABC::Addressee
>();
155 if (!(contact
= gcal_contact_new(NULL
)))
158 /* Common... there must exist a better way! I'm using Qt 4.5.
159 * What about the good and old .c_str()?
161 temp
= addressee
.realName();
162 gcal_contact_set_title(contact
, const_cast<char *>(qPrintable(temp
)));
164 temp
= addressee
.fullEmail();
165 gcal_contact_set_email(contact
, const_cast<char *>(qPrintable(temp
)));
167 /* TODO: add remaining fields */
169 if ((result
= gcal_add_contact(gcal
, contact
)))
172 gcal_contact_delete(contact
);
176 void googledataResource::itemChanged( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
179 KABC::Addressee addressee
;
180 gcal_contact_t contact
;
185 if (item
.hasPayload
<KABC::Addressee
>())
186 addressee
= item
.payload
<KABC::Addressee
>();
188 if (!(contact
= gcal_contact_new(NULL
)))
191 temp
= addressee
.realName();
192 gcal_contact_set_title(contact
, const_cast<char *>(qPrintable(temp
)));
194 temp
= addressee
.fullEmail();
195 gcal_contact_set_email(contact
, const_cast<char *>(qPrintable(temp
)));
197 temp
= addressee
.uid();
198 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
200 /* I suppose that this retrieves the first element in the key list */
201 key
= addressee
.keys()[0];
203 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
206 /* TODO: add remaining fields */
208 if ((result
= gcal_update_contact(gcal
, contact
)))
212 /* Updates the ETag/url: I suppose that akonadi will save this object */
213 temp
= gcal_contact_get_url(contact
);
214 addressee
.setUid(temp
);
216 temp
= gcal_contact_get_etag(contact
);
218 addressee
.insertKey(key
);
221 gcal_contact_delete(contact
);
225 void googledataResource::itemRemoved( const Akonadi::Item
&item
)
227 KABC::Addressee addressee
;
228 gcal_contact_t contact
;
233 if (item
.hasPayload
<KABC::Addressee
>())
234 addressee
= item
.payload
<KABC::Addressee
>();
236 if (!(contact
= gcal_contact_new(NULL
)))
239 temp
= addressee
.uid();
240 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
242 /* I suppose that this retrieves the first element in the key list */
243 key
= addressee
.keys()[0];
245 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
247 if ((result
= gcal_erase_contact(gcal
, contact
)))
250 gcal_contact_delete(contact
);
254 AKONADI_RESOURCE_MAIN( googledataResource
)
256 #include "googledataresource.moc"