In this tutorial you learn how to use Attica to access the Open Collaboration Services in your application.

+

−

Our goal is to get a list of friends for any user registered on this site.

+

In this tutorial you learn how to use Attica to access the Open Collaboration Services in your application.

−

With Attica you can easily access services from providers such as [http://opendesktop.org openDesktop.org].

+

Our goal is to get a list of friends for any user registered on this site.

−

It implements the [http://www.freedesktop.org freedesktop.org] specification for the [http://www.freedesktop.org/wiki/Specifications/open-collaboration-services Open Collaboration Services API].

+

−

==Providers==

+

With Attica you can easily access services from providers such as [http://opendesktop.org openDesktop.org]. It implements the [http://www.freedesktop.org freedesktop.org] specification for the [http://www.freedesktop.org/wiki/Specifications/open-collaboration-services Open Collaboration Services API].

−

Attica supports multiple Open Collaboration Service (OCS) providers at the same time.

+

−

Therefor the first thing to do is getting a ProviderManager and either asking it for a default provider (e.g. openDesktop.org for KDE) or setting a different provider by hand.

+

−

Let's create a mainwindow class that loads the openDesktop.org provider.

+

−

Make sure you have the KDE Plugin for Attica built, otherwise openDesktop.org will not be in the list of default providers.

+

−

===Mainwindow===

+

The code for this tutorial is also available in the kdeexamples module in [http://websvn.kde.org/trunk/KDE/kdeexamples/attica/ kde svn].

−

To make setting up the tutorial as easy as possible, here is a complete KDE application. We start with the mainwindow and add things into it.

+

−

====mainwindow.h====

+

== Providers ==

−

<code cppqt>

+

+

Attica supports multiple Open Collaboration Service (OCS) providers at the same time. Therefor the first thing to do is getting a ProviderManager and either asking it for a default provider (e.g. openDesktop.org for KDE) or setting a different provider by hand. Let's create a mainwindow class that loads the openDesktop.org provider. Make sure you have the KDE Plugin for Attica built, otherwise openDesktop.org will not be in the list of default providers.

+

+

=== Mainwindow ===

+

+

To make setting up the tutorial as easy as possible, here is a complete KDE application. We start with the mainwindow and add things into it.

Now we have a simple mainwindow with a tab widget. The real contents is still missing though. The first widget we'll add gives some information about a Person.

+

Now we have a simple mainwindow with a tab widget. The real contents is still missing though. The first widget we'll add gives some information about a Person.

−

==Person==

+

== Person ==

−

Now that we have a Provider, we can ask it for information.

+

−

As a first step, let's find out who I am. Entering any user nick into a KLineEdit will look up that Person.

+

−

Here is a class that subclasses QWidget and simply contains two labels that show the real name and city for a Person:

+

Now that we have a Provider, we can ask it for information. As a first step, let's find out who I am. Entering any user nick into a KLineEdit will look up that Person. [[Image:Tutorial attica person.png]] Here is a class that subclasses QWidget and simply contains two labels that show the real name and city for a Person. Since the Data has to be fetched from the server using a REST API, it is asynchronous. When the data arrived, we get notified by a signal.

−

====simplepersonrequest.h====

+

−

<code cppqt>

+

==== simplepersonrequest.h ====

+

+

<syntaxhighlight lang="cpp-qt">

#ifndef SIMPLEPERSONREQUEST_H

#ifndef SIMPLEPERSONREQUEST_H

#define SIMPLEPERSONREQUEST_H

#define SIMPLEPERSONREQUEST_H

−

+

#include <QtGui/QLabel>

#include <QtGui/QLabel>

#include <KLineEdit>

#include <KLineEdit>

#include <attica/provider.h>

#include <attica/provider.h>

−

+

class SimplePersonRequest :public QWidget

class SimplePersonRequest :public QWidget

{

{

Q_OBJECT

Q_OBJECT

public:

public:

−

SimplePersonRequest(Attica::Provider provider, QWidget* parent = 0);

SimplePersonRequest(Attica::Provider provider, QWidget* parent = 0);

−

+

public Q_SLOTS:

public Q_SLOTS:

+

// When the user entered a different nick name in the line edit and pressed enter.

void nickChanged(const QString& nick);

void nickChanged(const QString& nick);

+

// The information has been loaded.

void onPersonJobFinished( Attica::BaseJob *job );

void onPersonJobFinished( Attica::BaseJob *job );

−

+

private:

private:

KLineEdit* mNickNameLineEdit;

KLineEdit* mNickNameLineEdit;

Line 130:

Line 135:

Attica::Provider m_provider;

Attica::Provider m_provider;

};

};

−

#endif // SIMPLEPERSONREQUEST_H

#endif // SIMPLEPERSONREQUEST_H

−

</code>

+

</syntaxhighlight>

−

====simplepersonrequest.cpp====

+

==== simplepersonrequest.cpp ====

−

<code cppqt>

+

−

#include "simplepersonrequest.h"

+

+

Now to the actual implementation of our Widget that shows information about a person: <syntaxhighlight lang="cpp-qt">

The code for this tutorial is also available in the kdeexamples module in kde svn.

Providers

Attica supports multiple Open Collaboration Service (OCS) providers at the same time. Therefor the first thing to do is getting a ProviderManager and either asking it for a default provider (e.g. openDesktop.org for KDE) or setting a different provider by hand. Let's create a mainwindow class that loads the openDesktop.org provider. Make sure you have the KDE Plugin for Attica built, otherwise openDesktop.org will not be in the list of default providers.

Mainwindow

To make setting up the tutorial as easy as possible, here is a complete KDE application. We start with the mainwindow and add things into it.

mainwindow.h

//mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <KMainWindow>#include <KLineEdit>#include <QLabel>#include <attica/providermanager.h>#include <attica/provider.h>classMainWindow:publicKMainWindow{Q_OBJECTpublic:MainWindow(QWidget*parent=0);publicQ_SLOTS:// Here we will get notified, when the provider is loadedvoidprovidersChanged();private:// ProviderManager is the class that lets us "create" providersAttica::ProviderManagerm_manager;// A provider that we will ask for data from openDesktop.orgAttica::Providerm_provider;};#endif

mainwindow.cpp

//mainwindow.cpp#include "mainwindow.h"#include <KDebug>#include <QVBoxLayout>#include <attica/providermanager.h>#include "simplepersonrequest.h"MainWindow::MainWindow(QWidget*parent):KMainWindow(parent){// connect to get notified, when new Providers are loadedconnect(&m_manager,SIGNAL(defaultProvidersLoaded()),SLOT(providersChanged()));// tell it to get the default Providersm_manager.loadDefaultProviders();}// called when a provider was loadedvoidMainWindow::providersChanged(){if(!m_manager.providers().isEmpty()){m_provider=m_manager.providerByUrl(QUrl("https://api.opendesktop.org/v1/"));if(!m_provider.isValid()){kDebug()<<"Could not find opendesktop.org provider.";return;}// Use a tab widget here, so we can add more in the next tutorialQTabWidget*mainWidget=newQTabWidget(this);setCentralWidget(mainWidget);// Create a widget that will make use of the Provider// and supply information about a PersonSimplePersonRequest*personWidget=newSimplePersonRequest(m_provider,this);mainWidget->addTab(personWidget,tr("Person Search"));}}#include "mainwindow.moc"

Now we have a simple mainwindow with a tab widget. The real contents is still missing though. The first widget we'll add gives some information about a Person.

Person

Now that we have a Provider, we can ask it for information. As a first step, let's find out who I am. Entering any user nick into a KLineEdit will look up that Person. Here is a class that subclasses QWidget and simply contains two labels that show the real name and city for a Person. Since the Data has to be fetched from the server using a REST API, it is asynchronous. When the data arrived, we get notified by a signal.

simplepersonrequest.h

#ifndef SIMPLEPERSONREQUEST_H#define SIMPLEPERSONREQUEST_H#include <QtGui/QLabel>#include <KLineEdit>#include <attica/provider.h>classSimplePersonRequest:publicQWidget{Q_OBJECTpublic:SimplePersonRequest(Attica::Providerprovider,QWidget*parent=0);publicQ_SLOTS:// When the user entered a different nick name in the line edit and pressed enter.voidnickChanged(constQString&nick);// The information has been loaded.voidonPersonJobFinished(Attica::BaseJob*job);private:KLineEdit*mNickNameLineEdit;QLabel*mNameLabel;QLabel*mLocationLabel;QStringmNick;Attica::Providerm_provider;};#endif // SIMPLEPERSONREQUEST_H

simplepersonrequest.cpp

Now to the actual implementation of our Widget that shows information about a person:

#include "simplepersonrequest.h"#include <KDebug>#include <QtGui/QBoxLayout>#include <QtGui/QSpacerItem>#include <attica/person.h>#include <attica/itemjob.h>SimplePersonRequest::SimplePersonRequest(Attica::Providerprovider,QWidget*parent):QWidget(parent),m_provider(provider){QVBoxLayout*layout=newQVBoxLayout(this);mNickNameLineEdit=newKLineEdit();layout->addWidget(mNickNameLineEdit);mNameLabel=newQLabel(this);mNameLabel->setText("Name");layout->addWidget(mNameLabel);mLocationLabel=newQLabel(this);mLocationLabel->setText("Location");layout->addWidget(mLocationLabel);mNick="fregl";mNickNameLineEdit->setText(mNick);// watch the LineEdit for changesconnect(mNickNameLineEdit,SIGNAL(returnPressed(QString)),SLOT(nickChanged(QString)));// load the datanickChanged(mNick);}// The user pressed enter, try to get information about the nick namevoidSimplePersonRequest::nickChanged(constQString&nick){mNick=nick;// ask the provider for a job that loads the person information:Attica::ItemJob<Attica::Person>*job=m_provider.requestPerson(mNick);// connect that jobconnect(job,SIGNAL(finished(Attica::BaseJob*)),SLOT(onPersonJobFinished(Attica::BaseJob*)));// start the jobjob->start();}// New information has arrivedvoidSimplePersonRequest::onPersonJobFinished(Attica::BaseJob*job){kDebug()<<"onJobFinished";// Get the information about the person: we need to turn the job into its proper sub classAttica::ItemJob<Attica::Person>*personJob=static_cast<Attica::ItemJob<Attica::Person>*>(job);// check if the request actually workedif(personJob->metadata().error()==Attica::Metadata::NoError){// use the data to fill the labelsAttica::Personp(personJob->result());mNameLabel->setText(p.firstName()+' '+p.lastName());mLocationLabel->setText(p.city());}else{mNameLabel->setText("Could not fetch information.");}}#include "simplepersonrequest.moc"

Compiling the example

The last thing missing is a small main.cpp and CMakeLists.txt to complete the example and make it compile: