Development/Tutorials/Collaboration/Attica/Introduction: Difference between revisions
Line 94: | Line 94: | ||
====simplepersonrequest.h==== | ====simplepersonrequest.h==== | ||
====simplepersonrequest.cpp==== | ====simplepersonrequest.cpp==== | ||
<code cppqt> | |||
#include "simplepersonrequest.h" | |||
#include <KDebug> | |||
#include <QtGui/QBoxLayout> | |||
#include <QtGui/QSpacerItem> | |||
#include <attica/person.h> | |||
#include <attica/itemjob.h> | |||
SimplePersonRequest::SimplePersonRequest(Attica::Provider provider, QWidget* parent) | |||
: QWidget(parent) | |||
, m_provider(provider) | |||
{ | |||
QVBoxLayout* layout = new QVBoxLayout(this); | |||
mNickNameLineEdit = new KLineEdit(); | |||
layout->addWidget(mNickNameLineEdit); | |||
mNameLabel = new QLabel(this); | |||
mNameLabel->setText("Name"); | |||
layout->addWidget(mNameLabel); | |||
mLocationLabel = new QLabel(this); | |||
mLocationLabel->setText("Picture"); | |||
layout->addWidget(mLocationLabel); | |||
mNick = "fregl"; | |||
mNickNameLineEdit->setText(mNick); | |||
connect(mNickNameLineEdit, SIGNAL(returnPressed(QString)), SLOT(nickChanged(QString))); | |||
nickChanged(mNick); | |||
} | |||
void SimplePersonRequest::nickChanged(const QString& nick) | |||
{ | |||
mNick = nick; | |||
Attica::ItemJob<Attica::Person>* job = m_provider.requestPerson(mNick); | |||
connect(job, SIGNAL(finished(Attica::BaseJob*)), SLOT(onPersonJobFinished(Attica::BaseJob*))); | |||
job->start(); | |||
} | |||
void SimplePersonRequest::onPersonJobFinished( Attica::BaseJob *job ) | |||
{ | |||
kDebug() << "onJobFinished"; | |||
Attica::ItemJob<Attica::Person> *personJob = static_cast< Attica::ItemJob<Attica::Person> * >( job ); | |||
if( personJob->metadata().error() == Attica::Metadata::NoError ) { | |||
Attica::Person p(personJob->result()); | |||
mNameLabel->setText(p.firstName() + ' ' + p.lastName()); | |||
mLocationLabel->setText(p.city()); | |||
} else { | |||
mNameLabel->setText("Could not fetch information."); | |||
} | |||
} | |||
#include "simplepersonrequest.moc" | |||
</code> |
Revision as of 18:47, 17 November 2009
Development/Tutorials/Collaboration/Attica/Introduction
Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | Eesti | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenčina | Slovenščina | српски | Türkçe | Tiếng Việt | Українська | 简体中文 | 繁體中文
Tutorial Series | Attica |
Previous | Getting started with KDE development |
What's Next | n/a |
Further Reading | n/a |
Overview
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.
With Attica you can easily access services from providers such as openDesktop.org. It implements the freedesktop.org specification for the Open Collaboration Services API.
Initializing the Provider Manager
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 a default provider.
mainwindow.h
//mainwindow.h
- ifndef MAINWINDOW_H
- define MAINWINDOW_H
- include <KMainWindow>
- include <KLineEdit>
- include <QLabel>
- include <attica/providermanager.h>
- include <attica/provider.h>
class MainWindow : public KMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0);
public Q_SLOTS:
void providersChanged();
private:
Attica::Provider m_provider;
Attica::ProviderManager m_manager;
};
- 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(&m_manager, SIGNAL(providersChanged()), SLOT(providersChanged()));
m_manager.loadDefaultProviders();
}
void MainWindow::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;
}
QTabWidget* mainWidget = new QTabWidget(this);
setCentralWidget(mainWidget);
SimplePersonRequest* personWidget = new SimplePersonRequest(m_provider, this);
mainWidget->addTab(personWidget, tr("Person Search"));
ContentDownload* contentWidget = new ContentDownload(m_provider, this);
mainWidget->addTab(contentWidget, tr("Content"));
ContentCreation* contentCreationWidget = new ContentCreation(m_provider, this);
mainWidget->addTab(contentCreationWidget, tr("Add Content"));
}
}
- include "mainwindow.moc"
simplepersonrequest.h
simplepersonrequest.cpp
- include "simplepersonrequest.h"
- include <KDebug>
- include <QtGui/QBoxLayout>
- include <QtGui/QSpacerItem>
- include <attica/person.h>
- include <attica/itemjob.h>
SimplePersonRequest::SimplePersonRequest(Attica::Provider provider, QWidget* parent)
: QWidget(parent)
, m_provider(provider)
{
QVBoxLayout* layout = new QVBoxLayout(this);
mNickNameLineEdit = new KLineEdit();
layout->addWidget(mNickNameLineEdit);
mNameLabel = new QLabel(this);
mNameLabel->setText("Name");
layout->addWidget(mNameLabel);
mLocationLabel = new QLabel(this);
mLocationLabel->setText("Picture");
layout->addWidget(mLocationLabel);
mNick = "fregl";
mNickNameLineEdit->setText(mNick);
connect(mNickNameLineEdit, SIGNAL(returnPressed(QString)), SLOT(nickChanged(QString)));
nickChanged(mNick);
}
void SimplePersonRequest::nickChanged(const QString& nick)
{
mNick = nick;
Attica::ItemJob<Attica::Person>* job = m_provider.requestPerson(mNick);
connect(job, SIGNAL(finished(Attica::BaseJob*)), SLOT(onPersonJobFinished(Attica::BaseJob*)));
job->start();
}
void SimplePersonRequest::onPersonJobFinished( Attica::BaseJob *job )
{
kDebug() << "onJobFinished";
Attica::ItemJob<Attica::Person> *personJob = static_cast< Attica::ItemJob<Attica::Person> * >( job );
if( personJob->metadata().error() == Attica::Metadata::NoError ) {
Attica::Person p(personJob->result());
mNameLabel->setText(p.firstName() + ' ' + p.lastName());
mLocationLabel->setText(p.city());
} else {
mNameLabel->setText("Could not fetch information.");
}
}
- include "simplepersonrequest.moc"