How to initiate Internet connection from Qt appplication in Maemo 5
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
The article is believed to be still valid for the original topic scope.
The article is believed to be still valid for the original topic scope.
Overview
This article shows how to initiate an Internet connection from a Qt Application in Maemo 5
Basically it's done through conic library in Maemo 5. You can find details in the wiki: Requesting for Connection using ConIcConnection
However, it contains tricky point as conic expects that you call dbus_connection_setup_with_g_main() before using conic library. If you are using libosso for anything then you are safe, as it calls dbus_connection_setup_with_g_main() in osso_initialize(). However, libosso does many other things, which may be of no use for you. Then you have to do it yourself:
Implementation
#include <glib-object.h>
#include <conic/conic.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <QApplication>
#include <QMainWindow>
#include <QDebug>
static void
my_connection_handler(ConIcConnection *connection,
ConIcConnectionEvent *event,
gpointer user_data)
{
Q_UNUSED(connection);
Q_UNUSED(user_data);
ConIcConnectionStatus status = con_ic_connection_event_get_status(event);
switch(status) {
case CON_IC_STATUS_CONNECTED: {
const gchar *iap_id = con_ic_event_get_iap_id(CON_IC_EVENT(event));
const gchar *bearer = con_ic_event_get_bearer_type(CON_IC_EVENT(event));
qDebug("Hey, we are connected to IAP %s with bearer %s!", iap_id, bearer);
}
break;
case CON_IC_STATUS_DISCONNECTING:
qDebug("We are disconnecting...");
break;
case CON_IC_STATUS_DISCONNECTED: {
qDebug("And we are disconnected. Let's see what went wrong...");
ConIcConnectionError error = con_ic_connection_event_get_error(event);
switch(error) {
case CON_IC_CONNECTION_ERROR_NONE:
qDebug("Libconic thinks there was nothing wrong.");
break;
case CON_IC_CONNECTION_ERROR_INVALID_IAP:
qDebug("Invalid (non-existing?) IAP was requested.");
break;
case CON_IC_CONNECTION_ERROR_CONNECTION_FAILED:
qDebug("Connection just failed.");
break;
case CON_IC_CONNECTION_ERROR_USER_CANCELED:
qDebug("User canceled the connection attempt");
break;
}
}
break;
default:
qDebug("Unknown connection status received");
}
}
int
main (int argc,
char **argv)
{
DBusConnection *conn;
DBusError err;
dbus_error_init(&err);
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (conn) {
/* This is crucial for using conic library */
dbus_connection_setup_with_g_main(conn, NULL);
} else {
qDebug("Unable to connect to the D-BUS daemon: %s", err.message);
dbus_error_free(&err);
}
int ret;
QApplication app(argc, argv);
QMainWindow win;
gboolean success = FALSE;
ConIcConnection *connection = con_ic_connection_new();
g_signal_connect(G_OBJECT(connection), "connection-event",
G_CALLBACK(my_connection_handler), NULL);
win.show();
success = con_ic_connection_connect(connection, CON_IC_CONNECT_FLAG_NONE);
if (!success)
qDebug("Request for connection failed");
else
qDebug("Successful request for connection");
ret = app.exec();
success = con_ic_connection_disconnect(connection);
g_object_unref(connection);
return ret;
}
Please do not forget to add to your .pro file:
CONFIG += link_pkgconfig
PKGCONFIG += glib-2.0 conic


(no comments yet)