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>
12 #include <KWindowSystem>
15 #include <gcalendar.h>
17 #include <gcal_status.h>
20 using namespace Akonadi
;
22 GoogleDataResource::GoogleDataResource( const QString
&id
)
23 : ResourceBase( id
), authenticated(false), dlgConf(NULL
)
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
);
43 void GoogleDataResource::retrieveCollections()
45 // if ( mBaseResource == 0 ) {
46 // kError() << "No Google Contacts resource";
47 // const QString message = i18nc( "@info:status", "No Google Contact configured yet" );
48 // emit error( message );
50 // emit status( Broken, message );
55 c
.setParent(Collection::root());
56 c
.setRemoteId("google-contacts");
59 QStringList mimeTypes
;
60 mimeTypes
<< "text/directory";
61 c
.setContentMimeTypes(mimeTypes
);
63 Collection::List list
;
65 collectionsRetrieved(list
);
69 void GoogleDataResource::retrieveItems( const Akonadi::Collection
&collection
)
71 Q_UNUSED( collection
);
79 /* Downloading the contacts can be slow and it is blocking. Will
80 * it mess up with akonadi?
82 if ((result
= gcal_get_contacts(gcal
, &all_contacts
)))
85 /* Each google entry has a unique ID and edit_url */
86 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
88 Item
item(QLatin1String("text/directory"));
89 gcal_contact_t contact
= gcal_contact_element(&all_contacts
, i
);
90 item
.setRemoteId(gcal_contact_get_id(contact
));
95 itemsRetrieved(items
);
98 bool GoogleDataResource::retrieveItem( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
101 const QString entry_id
= item
.remoteId();
104 gcal_contact_t contact
;
105 KABC::Addressee addressee
;
106 KABC::PhoneNumber number
;
113 * And another question, are the requests in the same sequence that
114 * I informed in 'retrieveItems'? For while, I try to locate the entry...
116 for (size_t i
= 0; i
< all_contacts
.length
; ++i
) {
117 contact
= gcal_contact_element(&all_contacts
, i
);
118 if (entry_id
== gcal_contact_get_id(contact
)) {
120 temp
= gcal_contact_get_title(contact
);
121 addressee
.setNameFromString(temp
);
124 temp
= gcal_contact_get_email(contact
);
125 addressee
.insertEmail(temp
, true);
127 /* edit url: required to do edit/delete */
128 temp
= gcal_contact_get_url(contact
);
129 addressee
.setUid(temp
);
131 /* ETag: required by Google Data protocol 2.0 */
132 temp
= gcal_contact_get_etag(contact
);
134 addressee
.insertKey(key
);
136 /* TODO: telefone, address, etc */
138 newItem
.setPayload
<KABC::Addressee
>(addressee
);
147 void GoogleDataResource::aboutToQuit()
149 // TODO: any cleanup you need to do while there is still an active
150 // event loop. The resource will terminate after this method returns
153 void GoogleDataResource::configure( WId windowId
)
155 Q_UNUSED( windowId
);
158 QByteArray byteUser
, bytePass
;
161 dlgConf
= new dlgGoogleDataConf
;
163 if (windowId
&& dlgConf
)
164 KWindowSystem::setMainWindow(dlgConf
, windowId
);
168 byteUser
= dlgConf
->eAccount
->text().toLocal8Bit();
169 bytePass
= dlgConf
->ePass
->text().toLocal8Bit();
170 user
= const_cast<char *>(byteUser
.constData());
171 pass
= const_cast<char *>(bytePass
.constData());
174 result
= gcal_get_authentication(gcal
, user
, pass
);
176 /* TODO: in case of authentication error, display an error
180 authenticated
= true;
185 void GoogleDataResource::itemAdded( const Akonadi::Item
&item
, const Akonadi::Collection
&collection
)
188 KABC::Addressee addressee
;
189 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 /* TODO: add remaining fields */
210 if ((result
= gcal_add_contact(gcal
, contact
)))
213 gcal_contact_delete(contact
);
217 void GoogleDataResource::itemChanged( const Akonadi::Item
&item
, const QSet
<QByteArray
> &parts
)
220 KABC::Addressee addressee
;
221 gcal_contact_t contact
;
229 if (item
.hasPayload
<KABC::Addressee
>())
230 addressee
= item
.payload
<KABC::Addressee
>();
232 if (!(contact
= gcal_contact_new(NULL
)))
235 temp
= addressee
.realName();
236 gcal_contact_set_title(contact
, const_cast<char *>(qPrintable(temp
)));
238 temp
= addressee
.fullEmail();
239 gcal_contact_set_email(contact
, const_cast<char *>(qPrintable(temp
)));
241 temp
= addressee
.uid();
242 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
244 /* I suppose that this retrieves the first element in the key list */
245 key
= addressee
.keys()[0];
247 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
250 /* TODO: add remaining fields */
252 if ((result
= gcal_update_contact(gcal
, contact
)))
256 /* Updates the ETag/url: I suppose that akonadi will save this object */
257 temp
= gcal_contact_get_url(contact
);
258 addressee
.setUid(temp
);
260 temp
= gcal_contact_get_etag(contact
);
262 addressee
.insertKey(key
);
265 gcal_contact_delete(contact
);
269 void GoogleDataResource::itemRemoved( const Akonadi::Item
&item
)
271 KABC::Addressee addressee
;
272 gcal_contact_t contact
;
280 if (item
.hasPayload
<KABC::Addressee
>())
281 addressee
= item
.payload
<KABC::Addressee
>();
283 if (!(contact
= gcal_contact_new(NULL
)))
286 temp
= addressee
.uid();
287 gcal_contact_set_id(contact
, const_cast<char *>(qPrintable(temp
)));
289 /* I suppose that this retrieves the first element in the key list */
290 key
= addressee
.keys()[0];
292 gcal_contact_set_etag(contact
, const_cast<char *>(qPrintable(temp
)));
294 if ((result
= gcal_erase_contact(gcal
, contact
)))
297 gcal_contact_delete(contact
);
301 AKONADI_RESOURCE_MAIN( GoogleDataResource
)
303 #include "googledataresource.moc"