qtmobility/tests/networkmanager/networkmanagertest.cpp
branchRCL_3
changeset 6 4203353e74ea
parent 5 4ea83c148e84
child 7 e2d33e235f7e
equal deleted inserted replaced
5:4ea83c148e84 6:4203353e74ea
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QDBusConnection>
       
    43 #include <QDBusError>
       
    44 #include <QDBusInterface>
       
    45 #include <QDBusMessage>
       
    46 #include <QDBusReply>
       
    47 #include <QtDBus>
       
    48 #include <QHostAddress>
       
    49 #include <QDebug>
       
    50 #include <NetworkManager/NetworkManager.h>
       
    51 #include <QApplication>
       
    52 #include <QMainWindow>
       
    53 #include "nmview.h"
       
    54 
       
    55 #include <arpa/inet.h>
       
    56 
       
    57 typedef QMap< QString, QMap<QString,QVariant> > SettingsMap;
       
    58 Q_DECLARE_METATYPE(SettingsMap)
       
    59 
       
    60 void printConnectionDetails(const QString& service)
       
    61 {
       
    62     QDBusConnection dbc = QDBusConnection::systemBus();
       
    63     if (!dbc.isConnected()) {
       
    64         qWarning() << "Unable to connect to D-Bus:" << dbc.lastError();
       
    65         return;
       
    66     }
       
    67     QDBusInterface allCons(service, NM_DBUS_PATH_SETTINGS, NM_DBUS_IFACE_SETTINGS, dbc);
       
    68     if (allCons.isValid()) {
       
    69         QDBusReply<QList<QDBusObjectPath> > reply = allCons.call("ListConnections");
       
    70         if ( reply.isValid() ) {
       
    71             qWarning() << "Known connections:";
       
    72             QList<QDBusObjectPath> list = reply.value();
       
    73             foreach(QDBusObjectPath path, list) {
       
    74                 qWarning() << "  " << path.path();
       
    75                 QDBusInterface sysIface(NM_DBUS_SERVICE_SYSTEM_SETTINGS, path.path(), NM_DBUS_IFACE_SETTINGS_CONNECTION, dbc);
       
    76                 if (sysIface.isValid()) {
       
    77                     QDBusMessage r = sysIface.call("GetSettings");
       
    78                     QDBusReply< SettingsMap > rep = sysIface.call("GetSettings");
       
    79                     qWarning() << "     GetSettings:" << r.arguments() << r.signature() << rep.isValid() << sysIface.lastError();
       
    80                     QMap< QString, QMap<QString,QVariant> > map = rep.value();
       
    81                     QList<QString> list = map.keys();
       
    82                     foreach (QString key, list) {
       
    83                         QMap<QString,QVariant> innerMap = map[key];
       
    84                         qWarning() << "       Key: " << key;
       
    85                         QMap<QString,QVariant>::const_iterator i = innerMap.constBegin();
       
    86                         while (i != innerMap.constEnd()) {
       
    87                             QString k = i.key();
       
    88                             qWarning() << "          Key: " << k << " Entry: " << i.value();
       
    89                             if (k == "addresses" && i.value().canConvert<QDBusArgument>()) {
       
    90                                 QDBusArgument arg = i.value().value<QDBusArgument>();
       
    91                                 arg.beginArray();
       
    92                                 while (!arg.atEnd()) {
       
    93                                     QDBusVariant addr;
       
    94                                     arg >> addr;
       
    95                                     uint ip = addr.variant().toUInt();
       
    96                                     qWarning() << ip;
       
    97                                     qWarning() << "        " << QHostAddress(htonl(ip)).toString();
       
    98                                 }
       
    99 
       
   100                             }
       
   101                             i++;
       
   102                         }
       
   103                     }
       
   104                 }
       
   105             }
       
   106         }
       
   107     }
       
   108 
       
   109 
       
   110 }
       
   111 
       
   112 void readConnectionManagerDetails()
       
   113 {
       
   114     qDBusRegisterMetaType<SettingsMap>();
       
   115     QDBusConnection dbc = QDBusConnection::systemBus();
       
   116     if (!dbc.isConnected()) {
       
   117         qWarning() << "Unable to connect to D-Bus:" << dbc.lastError();
       
   118         return;
       
   119     }
       
   120     
       
   121     QDBusInterface iface(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, dbc);
       
   122     if (!iface.isValid()) {
       
   123         qWarning() << "Could not find NetworkManager";
       
   124         return;
       
   125     }
       
   126 
       
   127     uint state = iface.property("State").toUInt();
       
   128     switch(state) {
       
   129         case NM_STATE_UNKNOWN:
       
   130             qWarning() << "State: Unknown"; break;
       
   131         case NM_STATE_ASLEEP:
       
   132             qWarning() << "State: Asleep"; break;
       
   133         case NM_STATE_CONNECTING:
       
   134             qWarning() << "State: Connecting"; break;
       
   135         case NM_STATE_CONNECTED:
       
   136             qWarning() << "State: Connected"; break;
       
   137         case NM_STATE_DISCONNECTED:
       
   138             qWarning() << "State: Disconnected"; break;
       
   139     }
       
   140     //get list of network devices
       
   141     QDBusReply<QList<QDBusObjectPath> > reply = iface.call("GetDevices");
       
   142     if ( reply.isValid() ) {
       
   143         qWarning() << "Current devices:";
       
   144         QList<QDBusObjectPath> list = reply.value();
       
   145         foreach(QDBusObjectPath path, list) {
       
   146             qWarning() << "  " << path.path();
       
   147             QDBusInterface devIface(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_DEVICE, dbc);
       
   148             if (devIface.isValid()) {
       
   149                 qWarning() << "     Managed: " << devIface.property("Managed").toBool();
       
   150                 qWarning() << "     Interface: " << devIface.property("Interface").toString();
       
   151                 qWarning() << "     HAL UDI: " << devIface.property("Udi").toString();
       
   152                 qWarning() << "     Driver: " << devIface.property("Driver").toString();
       
   153                 QVariant v = devIface.property("DeviceType");
       
   154                 switch(v.toUInt()) {
       
   155                     case DEVICE_TYPE_UNKNOWN:
       
   156                         qWarning() << "     DeviceType: Unknown" ;
       
   157                         break;
       
   158                     case DEVICE_TYPE_802_3_ETHERNET: 
       
   159                         qWarning() << "     DeviceType: Ethernet" ;
       
   160                         break;
       
   161                     case DEVICE_TYPE_802_11_WIRELESS:
       
   162                         qWarning() << "     DeviceType: Wireless" ;
       
   163                         break;
       
   164                     case DEVICE_TYPE_GSM:
       
   165                         qWarning() << "     DeviceType: GSM" ;
       
   166                         break;
       
   167                     case DEVICE_TYPE_CDMA:
       
   168                         qWarning() << "     DeviceType: CDMA" ;
       
   169                         break;
       
   170                     
       
   171                 }
       
   172                 v = devIface.property("State");
       
   173                 switch(v.toUInt()) {
       
   174                     case NM_DEVICE_STATE_UNKNOWN:
       
   175                         qWarning() << "     State: Unknown" ; break;
       
   176                     case NM_DEVICE_STATE_UNMANAGED:
       
   177                         qWarning() << "     State: Unmanaged" ; break;
       
   178                     case NM_DEVICE_STATE_UNAVAILABLE:
       
   179                         qWarning() << "     State: Unavailable" ; break;
       
   180                     case NM_DEVICE_STATE_DISCONNECTED:
       
   181                         qWarning() << "     State: Disconnected" ; break;
       
   182                     case NM_DEVICE_STATE_PREPARE:
       
   183                         qWarning() << "     State: Preparing" ; break;
       
   184                     case NM_DEVICE_STATE_CONFIG:
       
   185                         qWarning() << "     State: Being configured" ; break;
       
   186                     case NM_DEVICE_STATE_NEED_AUTH:
       
   187                         qWarning() << "     State: Awaiting secrets" ; break;
       
   188                     case NM_DEVICE_STATE_IP_CONFIG:
       
   189                         qWarning() << "     State: IP requested" ; break;
       
   190                     case NM_DEVICE_STATE_ACTIVATED:
       
   191                         qWarning() << "     State: Activated" ; break;
       
   192                     case NM_DEVICE_STATE_FAILED:
       
   193                         qWarning() << "     State: FAILED" ; break;
       
   194                 }
       
   195                 quint32 ip = devIface.property("Ip4Address").toUInt();
       
   196                 qWarning() << "     IP4Address: " << QHostAddress(htonl(ip)).toString();
       
   197                 if (v.toUInt() == NM_DEVICE_STATE_ACTIVATED) {
       
   198                     QString path = devIface.property("Ip4Config").value<QDBusObjectPath>().path();
       
   199                     qWarning() << "     IP4Config: " << path;
       
   200                     QDBusInterface ipIface(NM_DBUS_SERVICE, path, NM_DBUS_INTERFACE_IP4_CONFIG, dbc);
       
   201                     if (ipIface.isValid()) {
       
   202                         qWarning() << "        Hostname: " << ipIface.property("Hostname").toString();
       
   203                         qWarning() << "        Domains: " << ipIface.property("Domains").toStringList();
       
   204                         qWarning() << "        NisDomain: " << ipIface.property("NisDomain").toString();
       
   205                         QDBusArgument arg=  ipIface.property("Addresses").value<QDBusArgument>();
       
   206                         //qWarning() << "        " << arg.currentType();
       
   207                         qWarning() << "        Addresses: " << ipIface.property("Addresses");
       
   208                         qWarning() << "        Nameservers: " << ipIface.property("Nameservers");
       
   209                         qWarning() << "        NisServers: " << ipIface.property("NisServers");
       
   210                     }
       
   211 
       
   212                 }
       
   213 
       
   214             }
       
   215         }
       
   216     }
       
   217 
       
   218     //get list of active connections
       
   219     QVariant prop = iface.property("ActiveConnections");
       
   220     QList<QDBusObjectPath> connections = prop.value<QList<QDBusObjectPath> >();
       
   221     QString activePath;
       
   222     if ( connections.count() )
       
   223         qWarning() << "Active connections:";
       
   224     foreach(QDBusObjectPath path, connections) {
       
   225         qWarning() << "  " << path.path();
       
   226         activePath = path.path();
       
   227         QString serviceName;
       
   228         QDBusInterface conIface(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_ACTIVE_CONNECTION, dbc);
       
   229         if (conIface.isValid()) {
       
   230             qWarning() << "     default connection: " << conIface.property("Default").toBool();
       
   231             serviceName = conIface.property("ServiceName").toString();
       
   232             qWarning() << "     service name: " << serviceName;
       
   233             qWarning() << "     connection path: " << conIface.property("Connection").value<QDBusObjectPath>().path();
       
   234             qWarning() << "     specific object:" << conIface.property("SpecificObject").value<QDBusObjectPath>().path();
       
   235             qWarning() << "     sharedServiceName: " << conIface.property("SharedServiceName").toString();
       
   236             QList<QDBusObjectPath> devs = conIface.property("Devices").value<QList<QDBusObjectPath> >();
       
   237             qWarning() << "     devices: ";
       
   238             foreach(QDBusObjectPath p, devs)
       
   239                 qWarning() << "         " << path.path();
       
   240             QVariant v = conIface.property("State");
       
   241             switch (v.toInt()) {
       
   242                 case NM_ACTIVE_CONNECTION_STATE_UNKNOWN:
       
   243                     qWarning()<< "     State: unknown"; break;
       
   244                 case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
       
   245                     qWarning()<< "     State: activating"; break;
       
   246                 case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
       
   247                     qWarning()<< "     State: activated"; break;
       
   248             }
       
   249         } else {
       
   250             qWarning() << conIface.lastError();
       
   251         }
       
   252 
       
   253     }
       
   254 
       
   255     printConnectionDetails(NM_DBUS_SERVICE_SYSTEM_SETTINGS);
       
   256     printConnectionDetails(NM_DBUS_SERVICE_USER_SETTINGS);
       
   257 
       
   258 
       
   259     //turn active connection off
       
   260     /*QDBusObjectPath dbop("/org/freedesktop/NetworkManager/ActiveConnection/1");
       
   261     QVariant asd = QVariant::fromValue(dbop);
       
   262     iface.call(QLatin1String("DeactivateConnection"), asd);
       
   263     qWarning() << iface.lastError();*/
       
   264 
       
   265     /*QDBusObjectPath p1device("/org/freedesktop/Hal/devices/net_00_60_6e_82_02_65");
       
   266     QVariant p1v = QVariant::fromValue(p1device);
       
   267     QDBusObjectPath p1con("/org/freedesktop/NetworkManagerSettings/0");
       
   268     QVariant p1c = QVariant::fromValue(p1con);
       
   269     QDBusObjectPath p1sp("");
       
   270     QVariant p1sp1 = QVariant::fromValue(p1sp);
       
   271     iface.call(QLatin1String("ActivateConnection"), 
       
   272             QString("/org/freedesktop/NetworkManagerSystemSettings"), p1c,p1v, p1v );
       
   273     qWarning() << iface.lastError();
       
   274     */
       
   275 }
       
   276 
       
   277 int main( int argc, char** argv)
       
   278 {
       
   279     QApplication app(argc, argv);
       
   280     //readConnectionManagerDetails();
       
   281     QMainWindow main;
       
   282     NMView view;
       
   283     main.setCentralWidget(&view);
       
   284     main.show();
       
   285     return app.exec();
       
   286 
       
   287 }