Creating the configuration dialog and showing it.
[akonadigoogledata.git] / googledataresource.cpp
blob978d152643c9e688f0278e6c2274cfcdda748d5d
1 #include "googledataresource.h"
3 #include "settings.h"
4 #include "settingsadaptor.h"
5 #include "dlgGoogleDataConf.h"
7 #include <QtDBus/QDBusConnection>
8 #include <kabc/addressee.h>
9 #include <kabc/phonenumber.h>
10 #include <kabc/key.h>
11 #include <kabc/errorhandler.h>
12 #include <qstring.h>
14 extern "C" {
15 #include <gcalendar.h>
16 #include <gcontact.h>
17 #include <gcal_status.h>
20 using namespace Akonadi;
22 GoogleDataResource::GoogleDataResource( const QString &id )
23 : ResourceBase( id )
25 new SettingsAdaptor( Settings::self() );
26 QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
27 Settings::self(), QDBusConnection::ExportAdaptors );
30 if (!(gcal = gcal_new(GCONTACT)))
31 exit(1);
32 gcal_set_store_xml(gcal, 1);
35 GoogleDataResource::~GoogleDataResource()
37 gcal_delete(gcal);
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 );
49 // return;
50 // }
52 Collection c;
53 c.setParent(Collection::root());
54 c.setRemoteId("google-contacts");
55 c.setName(name());
57 QStringList mimeTypes;
58 mimeTypes << "text/directory";
59 c.setContentMimeTypes(mimeTypes);
61 Collection::List list;
62 list << c;
63 collectionsRetrieved(list);
67 void GoogleDataResource::retrieveItems( const Akonadi::Collection &collection )
69 Q_UNUSED( collection );
71 Item::List items;
72 int result;
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)))
78 exit(1);
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));
87 items << item;
90 itemsRetrieved(items);
93 bool GoogleDataResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
95 Q_UNUSED( parts );
96 const QString entry_id = item.remoteId();
97 QString temp;
98 Item newItem(item);
99 gcal_contact_t contact;
100 KABC::Addressee addressee;
101 KABC::PhoneNumber number;
102 KABC::Key key;
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)) {
111 /* name */
112 temp = gcal_contact_get_title(contact);
113 addressee.setNameFromString(temp);
115 /* email */
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);
125 key.setId(temp);
126 addressee.insertKey(key);
128 /* TODO: telefone, address, etc */
130 newItem.setPayload<KABC::Addressee>(addressee);
131 return true;
136 return false;
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;
149 dlgConf->show();
151 /* TODO: retrieve user + password */
153 synchronize();
155 delete dlgConf;
158 void GoogleDataResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
161 KABC::Addressee addressee;
162 gcal_contact_t contact;
163 QString temp;
164 int result;
166 if (item.hasPayload<KABC::Addressee>())
167 addressee = item.payload<KABC::Addressee>();
169 if (!(contact = gcal_contact_new(NULL)))
170 exit(1);
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)))
181 exit(1);
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;
192 KABC::Key key;
193 QString temp;
194 int result;
196 if (item.hasPayload<KABC::Addressee>())
197 addressee = item.payload<KABC::Addressee>();
199 if (!(contact = gcal_contact_new(NULL)))
200 exit(1);
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];
213 temp = key.id();
214 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
217 /* TODO: add remaining fields */
219 if ((result = gcal_update_contact(gcal, contact)))
220 exit(1);
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);
228 key.setId(temp);
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;
240 KABC::Key key;
241 QString temp;
242 int result;
244 if (item.hasPayload<KABC::Addressee>())
245 addressee = item.payload<KABC::Addressee>();
247 if (!(contact = gcal_contact_new(NULL)))
248 exit(1);
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];
255 temp = key.id();
256 gcal_contact_set_etag(contact, const_cast<char *>(qPrintable(temp)));
258 if ((result = gcal_erase_contact(gcal, contact)))
259 exit(1);
261 gcal_contact_delete(contact);
265 AKONADI_RESOURCE_MAIN( GoogleDataResource )
267 #include "googledataresource.moc"