×
Namespaces

Variants
Actions
(Difference between revisions)

Connection state aware applications for Maemo 5 with Qt Mobility

From Nokia Developer Wiki
Jump to: navigation, search
divanov (Talk | contribs)
(Solution)
divanov (Talk | contribs)
Line 31: Line 31:
 
CONFIG += mobility
 
CONFIG += mobility
 
MOBILITY = bearer
 
MOBILITY = bearer
 +
 +
CONFIG += link_pkgconfig
 +
PKGCONFIG += gq-gconf
  
 
# Input
 
# Input
Line 72: Line 75:
 
private slots:
 
private slots:
 
     void started();
 
     void started();
 +
    void valueChanged();
 
     void onlineStateChanged(bool isOnline);
 
     void onlineStateChanged(bool isOnline);
  
Line 77: Line 81:
 
     QNetworkConfigurationManager *configManager;
 
     QNetworkConfigurationManager *configManager;
 
     QNetworkSession *session;
 
     QNetworkSession *session;
 +
    GConfItem *autoConnect;
 +
    bool autoConnectValue;
 
};
 
};
  
Line 92: Line 98:
 
     QApplication(argc, argv), session(0)
 
     QApplication(argc, argv), session(0)
 
{
 
{
 +
    autoConnect = new GConfItem("/system/osso/connectivity/network_type/auto_connect",
 +
                                this);
 +
    valueChanged();
 +
    connect(autoConnect, SIGNAL(valueChanged()), this, SLOT(valueChanged()));
 
     configManager = new QNetworkConfigurationManager(this);
 
     configManager = new QNetworkConfigurationManager(this);
 
     QTimer::singleShot(0, this, SLOT(started()));
 
     QTimer::singleShot(0, this, SLOT(started()));
Line 97: Line 107:
 
</code>
 
</code>
  
QTimer::singleShot is used to make sure that slot ''started()'' is invoked when event loop is running.
+
QTimer::singleShot is used to make sure that slot ''started()'' is invoked when event loop is already running.
 +
 
 +
<code cpp>
 +
void NetworkApplication::valueChanged()
 +
{
 +
    qDebug() << autoConnect->value().toString();
 +
    autoConnectValue = !(autoConnect->value().toString().isEmpty());
 +
}
 +
</code>
 +
 
 +
valueChanged slot is used in monitoring Internet Connection settings, when it changes to/from "Always ask".
  
 
<code cpp>
 
<code cpp>
Line 115: Line 135:
 
{
 
{
 
     if (!isOnline) {
 
     if (!isOnline) {
         QMessageBox::StandardButton reply = QMessageBox::question(0,
+
         QMessageBox::StandardButton reply;
 +
        if (autoConnectValue)
 +
            reply = QMessageBox::Yes;
 +
        else
 +
            reply = QMessageBox::question(0,
 
             "Connection",
 
             "Connection",
 
             "This application requires connection to the Internet. "
 
             "This application requires connection to the Internet. "

Revision as of 15:41, 15 July 2010


Introduction

Currently, at the moment Maemo 5 Select connection dialog appears on every attempt to connect to the network. User decisions are not remembered thus application easily may become unusable. There are bugs filed to Qt on this issues http://bugreports.qt.nokia.com/browse/QTBUG-10343 http://bugreports.qt.nokia.com/browse/QTBUG-10503

Before Qt 4.7 is released Qt applications have to workaround this bug.

Solution

One of the possible solutions is to use Qt Mobility Bearer Management. It's available in PR 1.2 release. You can install it with a commands

 sudo gainroot
 apt-get update
 apt-get install libqtm-bearer

in XTerminal on N900 and inside scratchbox (skip sudo command there). Nokia Qt SDK contains it ready for use.

Project file should enable mobility

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
 
QT += network
 
CONFIG += mobility
MOBILITY = bearer
 
CONFIG += link_pkgconfig
PKGCONFIG += gq-gconf
 
# Input
HEADERS += networkapplication.h netmanager.h
SOURCES += main.cpp networkapplication.cpp netmanager.cpp

A very basic main.cpp

#include "networkapplication.h"
#include "netmanager.h"
 
int main(int argc, char *argv[])
{
NetworkApplication a(argc, argv);
 
NetManager netManager;
netManager.show();
 
return a.exec();
}

NetworkApplication header file:

#ifndef NETWORK_APPLICATION_H
#define NETWORK_APPLICATION_H
 
#include <QApplication>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
using namespace QtMobility;
 
class NetworkApplication : public QApplication
{
Q_OBJECT
 
public:
NetworkApplication(int &argc, char **argv);
 
private slots:
void started();
void valueChanged();
void onlineStateChanged(bool isOnline);
 
private:
QNetworkConfigurationManager *configManager;
QNetworkSession *session;
GConfItem *autoConnect;
bool autoConnectValue;
};
 
#endif //NETWORK_APPLICATION_H
#include <QMessageBox>
#include <QTimer>
#include <QNetworkConfiguration>
 
#include "networkapplication.h"
 
NetworkApplication::NetworkApplication(int &argc, char **argv) :
QApplication(argc, argv), session(0)
{
autoConnect = new GConfItem("/system/osso/connectivity/network_type/auto_connect",
this);
valueChanged();
connect(autoConnect, SIGNAL(valueChanged()), this, SLOT(valueChanged()));
configManager = new QNetworkConfigurationManager(this);
QTimer::singleShot(0, this, SLOT(started()));
}

QTimer::singleShot is used to make sure that slot started() is invoked when event loop is already running.

void NetworkApplication::valueChanged()
{
qDebug() << autoConnect->value().toString();
autoConnectValue = !(autoConnect->value().toString().isEmpty());
}

valueChanged slot is used in monitoring Internet Connection settings, when it changes to/from "Always ask".

void NetworkApplication::started()
{
QCoreApplication::processEvents();
onlineStateChanged(configManager->isOnline());
connect(configManager, SIGNAL(onlineStateChanged(bool)),
this, SLOT(onlineStateChanged(bool)));
}

We have to wait until all events are processed, to make sure that QNetworkConfiguationManager is properly initialised, then we test for connection.

void NetworkApplication::onlineStateChanged(bool isOnline)
{
if (!isOnline) {
QMessageBox::StandardButton reply;
if (autoConnectValue)
reply = QMessageBox::Yes;
else
reply = QMessageBox::question(0,
"Connection",
"This application requires connection to the Internet. "
"Do you want to connect or quit an application?",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (reply == QMessageBox::No)
QCoreApplication::quit();
else {
if(session) {
delete session;
session = 0;
}
QNetworkConfiguration config = configManager->defaultConfiguration();
session = new QNetworkSession(config, this);
session->open();
session->waitForOpened(-1);
onlineStateChanged(configManager->isOnline());
}
}
}

In case connection is not available user is proposed to quit application or choose a valid connection.

NetManager header file:

#ifndef NET_MANAGER_H
#define NET_MANAGER_H
 
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QLabel>
#include <QPushButton>
 
class NetManager : public QWidget
{
Q_OBJECT
 
public:
NetManager(QWidget *parent=0);
 
private slots:
void clicked(bool checked = false);
void finished(QNetworkReply* reply);
 
private:
QPushButton *button;
QLabel *label;
QNetworkAccessManager *nam;
};
 
#endif //NET_MANAGER_H

Implementation of NetManager, which is used to initiate network request and process a network reply.

#include <QDebug>
#include <QVBoxLayout>
 
#include "netmanager.h"
 
NetManager::NetManager(QWidget *parent)
: QWidget(parent), nam(0)
{
QVBoxLayout *layout = new QVBoxLayout;
button = new QPushButton("Get page");
connect(button, SIGNAL(clicked(bool)), this, SLOT(clicked(bool)));
layout->addWidget(button);
label = new QLabel("");
layout->addWidget(label);
setLayout(layout);
}
 
void NetManager::clicked(bool checked)
{
Q_UNUSED(checked);
 
if(!nam) {
nam = new QNetworkAccessManager(this);
connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finished(QNetworkReply*)));
}
nam->get(QNetworkRequest(QUrl("http://tycho.usno.navy.mil/cgi-bin/timer.pl")));
}
 
void NetManager::finished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError) {
label->setText(reply->readAll());
} else {
label->setText(reply->errorString());
}
 
reply->deleteLater();
}
110 page views in the last 30 days.
×