IUP ISI/MediaWiki-Silk/coding-style: Difference between revisions
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= Introduction = | |||
For the coding style we follow the [http://techbase.kde.org/Policies/Kdelibs_Coding_Style kdelibs coding style]. | |||
Artistic Style (astyle) automatic code formatting: | |||
<code c> | |||
astyle --indent=spaces=4 --brackets=linux \ | |||
--indent-labels --pad-oper --unpad-paren \ | |||
--one-line=keep-statements --convert-tabs \ | |||
--indent-preprocessor \ | |||
`find -type f -name '*.cpp'` `find -type f -name '*.cc'` `find -type f -name '*.h'` | |||
</code> | |||
= Class type to return attributes = | = Class type to return attributes = | ||
Line 35: | Line 48: | ||
class KDE_EXPORT Attribute : public QObject | class KDE_EXPORT Attribute : public QObject | ||
{ | { | ||
Q_OBJECT | |||
public: | public: | ||
/** | |||
@brief Create an instance of Attribute and send a request. | |||
@param media | |||
@param parent | |||
*/ | |||
explicit Attribute(MediaWiki const & media, /*Put attributes here*/ QObject * parent = 0); | |||
/** | |||
@brief Destroy the AttributePrivate pointer. | |||
*/ | |||
virtual ~Attribute(); | |||
signals: | signals: | ||
/** | |||
@brief Emitted when a request has been completed. | |||
@param success true if the request was completed successfully. | |||
*/ | |||
void finished(bool); | |||
public slots: | public slots: | ||
/** | |||
@brief Aborts the currently running request. | |||
*/ | |||
void abort(); | |||
private slots: | private slots: | ||
/** | |||
@brief Reads the xml and build results | |||
@param reply | |||
*/ | |||
void onFinished(QNetworkReply *reply); | |||
private: | private: | ||
struct AttributePrivate * const d; | |||
}; | }; | ||
Line 114: | Line 127: | ||
#include "attribute.h" | #include "attribute.h" | ||
#include "mediawiki.h" | |||
struct AttributePrivate | struct AttributePrivate { | ||
{ | QNetworkAccessManager *manager; | ||
QNetworkReply *reply; | |||
QUrl apiUrl; | |||
/*Put attributes here*/ | |||
}; | }; | ||
Attribute::Attribute( MediaWiki const & media, /*Put attributes here*/, QObject * parent ) | Attribute::Attribute(MediaWiki const & media, /*Put attributes here*/, QObject * parent) | ||
: QObject(parent) | |||
, d(new AttributePrivate) | |||
{ | { | ||
d->apiUrl = media.url(); | |||
QUrl url = d->apiUrl; | |||
url.addQueryItem(QString("action"), QString("query")); | |||
// Set the request | |||
QNetworkRequest request(url); | |||
request.setRawHeader("User-Agent", "mediawiki-silk"); | |||
// Send the request | |||
d->manager = new QNetworkAccessManager(this); | |||
connect(d->manager, SIGNAL(onFinished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply *))); | |||
d->reply = d->manager->get(request); | |||
QTimer::singleShot(30 * 1000, this, SLOT(abort())); | |||
} | } | ||
Line 147: | Line 160: | ||
Attribute::~Attribute() | Attribute::~Attribute() | ||
{ | { | ||
delete d; | |||
} | } | ||
void Attribute::abort() | void Attribute::abort() | ||
{ | { | ||
qDebug() << "abort"; | |||
if (!d->reply) | |||
return; | |||
d->reply->abort(); | |||
d->reply = 0; | |||
} | } | ||
void Attribute::onFinished( QNetworkReply *reply ) | void Attribute::onFinished(QNetworkReply *reply) | ||
{ | { | ||
if (reply->error() != QNetworkReply::NoError) { | |||
qDebug() << "Request failed, " << reply->errorString(); | |||
emit finished(false); | |||
return; | |||
} | |||
QXmlStreamReader reader(reply); | |||
while (!reader.atEnd() && !reader.hasError()) { | |||
QXmlStreamReader::TokenType token = reader.readNext(); | |||
if (token == QXmlStreamReader::StartElement) { | |||
if (reader.name() == QString("goodName")) { | |||
QXmlStreamAttributes attrs = reader.attributes(); | |||
if (attrs.value(QString("result")).toString() == "goodResult") { | |||
qDebug() << "Request succed, " << reply->errorString(); | |||
d->lgtoken = attrs.value(QString("result")).toString() ; | |||
emit finished(true); | |||
} | |||
} | |||
} else if (token == QXmlStreamReader::Invalid) | |||
emit finished(false); | |||
} | |||
} | } | ||
</code> | </code> | ||
= Class type to return list = | = Class type to return list = | ||
Line 228: | Line 236: | ||
class KDE_EXPORT List : public QObject | class KDE_EXPORT List : public QObject | ||
{ | { | ||
Q_OBJECT | |||
public: | public: | ||
struct Result { | |||
/* Attribute result */ | |||
}; | |||
/** | |||
@brief Create an instance of List and send a request. | |||
@param media | |||
@param parent | |||
*/ | |||
explicit List(MediaWiki const & media, /*Put attributes here*/, QObject * parent = 0); | |||
/** | |||
@brief Destroy the ListPrivate pointer. | |||
*/ | |||
virtual ~List(); | |||
signals: | signals: | ||
/** | |||
@brief Emitted when a request has been completed. | |||
@param success true if the request was completed successfully. | |||
*/ | |||
void finished(QList<List::Result> list); | |||
public slots: | public slots: | ||
/** | |||
@brief Aborts the currently running request. | |||
*/ | |||
void abort(); | |||
private slots: | private slots: | ||
/** | |||
@brief Reads the xml and build results | |||
@param reply | |||
*/ | |||
void onFinished(QNetworkReply *reply); | |||
private: | private: | ||
struct ListPrivate *const d; | |||
}; | }; | ||
Line 308: | Line 316: | ||
#include "list.h" | #include "list.h" | ||
#include "mediawiki.h" | |||
struct ListPrivate | struct ListPrivate { | ||
{ | QNetworkAccessManager *manager; | ||
QNetworkReply *reply; | |||
QUrl apiUrl; | |||
/*Put attributes here*/ | |||
}; | }; | ||
List::List( MediaWiki const & media, /*Put attributes here*/, QObject * parent ) | List::List(MediaWiki const & media, /*Put attributes here*/, QObject * parent) | ||
: QObject(parent) | |||
, d(new ListPrivate) | |||
{ | { | ||
d->apiUrl = media.url(); | |||
QUrl url = d->apiUrl; | |||
url.addQueryItem(QString("action"), QString("query")); | |||
// Set the request | |||
QNetworkRequest request(url); | |||
request.setRawHeader("User-Agent", "mediawiki-silk"); | |||
// Send the request | |||
d->manager = new QNetworkAccessManager(this); | |||
connect(d->manager, SIGNAL(onFinished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply *))); | |||
d->reply = d->manager->get(request); | |||
} | } | ||
Line 340: | Line 348: | ||
List::~List() | List::~List() | ||
{ | { | ||
delete d; | |||
} | } | ||
void List::abort() | void List::abort() | ||
{ | { | ||
qDebug() << "abort"; | |||
if (!d->reply) | |||
return; | |||
d->reply->abort(); | |||
d->reply = 0; | |||
} | } | ||
void List::onFinished( QNetworkReply *reply ) | void List::onFinished(QNetworkReply *reply) | ||
{ | { | ||
if (reply->error() == QNetworkReply::NoError) { | |||
// Parse xml | |||
emit finished(list); | |||
} | |||
} | } | ||
</code> | </code> | ||
Latest revision as of 12:53, 29 October 2010
Introduction
For the coding style we follow the kdelibs coding style.
Artistic Style (astyle) automatic code formatting:
astyle --indent=spaces=4 --brackets=linux \
--indent-labels --pad-oper --unpad-paren \
--one-line=keep-statements --convert-tabs \
--indent-preprocessor \
`find -type f -name '*.cpp'` `find -type f -name '*.cc'` `find -type f -name '*.h'`
Class type to return attributes
attribute.h
/*
- Copyright 2010 by XXXX XXXX <[email protected]>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Library General Public License as
- published by the Free Software Foundation; either version 2, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details
- You should have received a copy of the GNU Library General Public
- License along with this program; if not, write to the
- Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- /
- ifndef ATTRIBUTE_H
- define ATTRIBUTE_H
- include <QtCore/QObject>
- include <kdemacros.h>
class MediaWiki;
class QNetworkReply;
class KDE_EXPORT Attribute : public QObject
{
Q_OBJECT
public:
/**
@brief Create an instance of Attribute and send a request.
@param media
@param parent
*/
explicit Attribute(MediaWiki const & media, /*Put attributes here*/ QObject * parent = 0);
/**
@brief Destroy the AttributePrivate pointer.
*/
virtual ~Attribute();
signals:
/**
@brief Emitted when a request has been completed.
@param success true if the request was completed successfully.
*/
void finished(bool);
public slots:
/**
@brief Aborts the currently running request.
*/
void abort();
private slots:
/**
@brief Reads the xml and build results
@param reply
*/
void onFinished(QNetworkReply *reply);
private:
struct AttributePrivate * const d;
};
- endif // ATTRIBUTE_H
attribute.cpp
/*
- Copyright 2010 by XXXX XXXX <[email protected]>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Library General Public License as
- published by the Free Software Foundation; either version 2, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details
- You should have received a copy of the GNU Library General Public
- License along with this program; if not, write to the
- Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- /
- include <QtNetwork/QNetworkAccessManager>
- include <QtNetwork/QNetworkRequest>
- include <QtNetwork/QNetworkReply>
- include <QtCore/QXmlStreamReader>
- include <QtCore/QTimer>
- include <QtCore/QDebug>
- include "attribute.h"
- include "mediawiki.h"
struct AttributePrivate {
QNetworkAccessManager *manager;
QNetworkReply *reply;
QUrl apiUrl;
/*Put attributes here*/
};
Attribute::Attribute(MediaWiki const & media, /*Put attributes here*/, QObject * parent)
: QObject(parent)
, d(new AttributePrivate)
{
d->apiUrl = media.url();
QUrl url = d->apiUrl;
url.addQueryItem(QString("action"), QString("query"));
// Set the request
QNetworkRequest request(url);
request.setRawHeader("User-Agent", "mediawiki-silk");
// Send the request
d->manager = new QNetworkAccessManager(this);
connect(d->manager, SIGNAL(onFinished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply *)));
d->reply = d->manager->get(request);
QTimer::singleShot(30 * 1000, this, SLOT(abort()));
}
Attribute::~Attribute()
{
delete d;
}
void Attribute::abort()
{
qDebug() << "abort";
if (!d->reply)
return;
d->reply->abort();
d->reply = 0;
}
void Attribute::onFinished(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Request failed, " << reply->errorString();
emit finished(false);
return;
}
QXmlStreamReader reader(reply);
while (!reader.atEnd() && !reader.hasError()) {
QXmlStreamReader::TokenType token = reader.readNext();
if (token == QXmlStreamReader::StartElement) {
if (reader.name() == QString("goodName")) {
QXmlStreamAttributes attrs = reader.attributes();
if (attrs.value(QString("result")).toString() == "goodResult") {
qDebug() << "Request succed, " << reply->errorString();
d->lgtoken = attrs.value(QString("result")).toString() ;
emit finished(true);
}
}
} else if (token == QXmlStreamReader::Invalid)
emit finished(false);
}
}
Class type to return list
list.h
/*
- Copyright 2010 by XXXX XXXX <[email protected]>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Library General Public License as
- published by the Free Software Foundation; either version 2, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details
- You should have received a copy of the GNU Library General Public
- License along with this program; if not, write to the
- Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- /
- ifndef LIST_H
- define LIST_H
- include <QtCore/QObject>
- include <kdemacros.h>
class MediaWiki;
class QNetworkReply;
class KDE_EXPORT List : public QObject
{
Q_OBJECT
public:
struct Result {
/* Attribute result */
};
/**
@brief Create an instance of List and send a request.
@param media
@param parent
*/
explicit List(MediaWiki const & media, /*Put attributes here*/, QObject * parent = 0);
/**
@brief Destroy the ListPrivate pointer.
*/
virtual ~List();
signals:
/**
@brief Emitted when a request has been completed.
@param success true if the request was completed successfully.
*/
void finished(QList<List::Result> list);
public slots:
/**
@brief Aborts the currently running request.
*/
void abort();
private slots:
/**
@brief Reads the xml and build results
@param reply
*/
void onFinished(QNetworkReply *reply);
private:
struct ListPrivate *const d;
};
- endif // LIST_H
list.cpp
/*
- Copyright 2010 by XXXX XXXX <[email protected]>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Library General Public License as
- published by the Free Software Foundation; either version 2, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details
- You should have received a copy of the GNU Library General Public
- License along with this program; if not, write to the
- Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- /
- include <QtNetwork/QNetworkAccessManager>
- include <QtNetwork/QNetworkRequest>
- include <QtNetwork/QNetworkReply>
- include <QtCore/QXmlStreamReader>
- include <QtCore/QDebug>
- include "list.h"
- include "mediawiki.h"
struct ListPrivate {
QNetworkAccessManager *manager;
QNetworkReply *reply;
QUrl apiUrl;
/*Put attributes here*/
};
List::List(MediaWiki const & media, /*Put attributes here*/, QObject * parent)
: QObject(parent)
, d(new ListPrivate)
{
d->apiUrl = media.url();
QUrl url = d->apiUrl;
url.addQueryItem(QString("action"), QString("query"));
// Set the request
QNetworkRequest request(url);
request.setRawHeader("User-Agent", "mediawiki-silk");
// Send the request
d->manager = new QNetworkAccessManager(this);
connect(d->manager, SIGNAL(onFinished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply *)));
d->reply = d->manager->get(request);
}
List::~List()
{
delete d;
}
void List::abort()
{
qDebug() << "abort";
if (!d->reply)
return;
d->reply->abort();
d->reply = 0;
}
void List::onFinished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError) {
// Parse xml
emit finished(list);
}
}