Development/Tutorials/Collaboration/Attica/Introduction: Difference between revisions

From KDE TechBase
Line 22: Line 22:


===Mainwindow===
===Mainwindow===
To make setting up the tutorial as easy as possible, we'll have a complete KDE application here. We'll start with the mainwindow and add things into it.
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====
<code cppqt>
<code cppqt>

Revision as of 19:05, 17 November 2009


Development/Tutorials/Collaboration/Attica/Introduction


Attica Introduction
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.

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

  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <KMainWindow>
  2. include <KLineEdit>
  3. include <QLabel>
  1. include <attica/providermanager.h>
  2. include <attica/provider.h>

class MainWindow : public KMainWindow {

   Q_OBJECT

public:

   MainWindow(QWidget *parent=0);

public Q_SLOTS:

   // Here we will get notified, when the provider is loaded
   void providersChanged();

private:

   // ProviderManager is the class that lets us "create" providers
   Attica::ProviderManager m_manager;
   // A provider that we will ask for data from openDesktop.org
   Attica::Provider m_provider;

};

  1. endif

mainwindow.cpp

//mainwindow.cpp

  1. include "mainwindow.h"
  1. include <KDebug>
  2. include <QVBoxLayout>
  1. include <attica/providermanager.h>
  2. include "simplepersonrequest.h"

MainWindow::MainWindow(QWidget *parent) : KMainWindow(parent) {

   // connect to get notified, when new Providers are loaded
   connect(&m_manager, SIGNAL(providersChanged()), SLOT(providersChanged()));
   // tell it to get the default Providers
   m_manager.loadDefaultProviders();

}

// called when a provider was loaded 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;
       }
       // Use a tab widget here, so we can add more in the next tutorial
       QTabWidget* mainWidget = new QTabWidget(this);
       setCentralWidget(mainWidget);
       // Create a widget that will make use of the Provider
       // and supply information about a Person
       SimplePersonRequest* personWidget = new SimplePersonRequest(m_provider, this);
       mainWidget->addTab(personWidget, tr("Person Search"));
   }

}

  1. 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:

simplepersonrequest.h

  1. ifndef SIMPLEPERSONREQUEST_H
  2. define SIMPLEPERSONREQUEST_H
  1. include <QtGui/QLabel>
  2. include <KLineEdit>
  3. include <attica/provider.h>

class SimplePersonRequest :public QWidget {

   Q_OBJECT

public:

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

public Q_SLOTS:

   void nickChanged(const QString& nick);
   void onPersonJobFinished( Attica::BaseJob *job );
   

private:

   KLineEdit* mNickNameLineEdit;
   QLabel* mNameLabel;
   QLabel* mLocationLabel;
   QString mNick;
   Attica::Provider m_provider;

};

  1. endif // SIMPLEPERSONREQUEST_H

simplepersonrequest.cpp

  1. include "simplepersonrequest.h"
  1. include <KDebug>
  2. include <QtGui/QBoxLayout>
  3. include <QtGui/QSpacerItem>
  1. include <attica/person.h>
  2. 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.");
   }

}

  1. 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:

main.cpp

  1. include <KApplication>
  2. include <KAboutData>
  3. include <KCmdLineArgs>
  4. include <KLocale>
  1. include "mainwindow.h"

int main (int argc, char *argv[]) {

   KAboutData aboutData( "opencollaborationexample", 0,
   ki18n("Open Collaboration Services Demo"), "1.0",
   ki18n("Show how to use libattica to access the Open Collaboration Services"),
   KAboutData::License_GPL,
   ki18n("Copyright (c) 2009 Frederik Gladhorn") );
   KCmdLineArgs::init( argc, argv, &aboutData );
   KApplication app;
    
   MainWindow* window = new MainWindow();
   window->show();
    
   return app.exec();

}

CMakeLists.txt

project (opencollaborationexample)

include_directories(${KDE4_INCLUDES} ${ATTICA_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})

set(opencollaborationexample_SRCS

   main.cpp
   mainwindow.cpp
   simplepersonrequest.cpp

)

kde4_add_executable(opencollaborationexample ${opencollaborationexample_SRCS}) target_link_libraries(opencollaborationexample ${ATTICA_LIBRARIES} ${KDE4_KIO_LIBS} ${KDE4_KDEUI_LIBS})