qtmobility/tests/networkmanager/nmview.cpp
changeset 0 cfcbf08528c4
equal deleted inserted replaced
-1:000000000000 0:cfcbf08528c4
       
     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 "nmview.h"
       
    43 
       
    44 #include <QLabel>
       
    45 #include <QLayout>
       
    46 #include <QListWidget>
       
    47 #include <QDebug>
       
    48 #include <QPushButton>
       
    49 
       
    50 #include <QtDBus>
       
    51 #include <QtNetwork>
       
    52 #include <NetworkManager/NetworkManager.h>
       
    53 #include <arpa/inet.h>
       
    54 
       
    55 #include <QDBusConnection>
       
    56 #include <QDBusError>
       
    57 #include <QDBusInterface>
       
    58 #include <QDBusMessage>
       
    59 #include <QDBusReply>
       
    60 
       
    61 #include <QHostAddress>
       
    62 #include <QNetworkInterface>
       
    63 #include <QTreeWidgetItem>
       
    64 #include <QMessageBox>
       
    65 
       
    66 //#include "ui_dialog.h"
       
    67 
       
    68 #include <qnetworkconfiguration.h>
       
    69 #include <qnetworkconfigmanager.h>
       
    70 #include <qnetworksession.h>
       
    71 #include <qnetworkmanagerservice_p.h>
       
    72 
       
    73 //static QDBusConnection dbc = QDBusConnection::systemBus();
       
    74 //typedef QMap< QString, QMap<QString,QVariant> > SettingsMap;
       
    75 //Q_DECLARE_METATYPE(SettingsMap)
       
    76 
       
    77 
       
    78 NMView::NMView(QDialog* parent)
       
    79     : QDialog(parent, 0), dbc(QDBusConnection::systemBus())
       
    80 {
       
    81     init();
       
    82 
       
    83     if (!dbc.isConnected()) {
       
    84         qWarning() << "Unable to connect to D-Bus:" << dbc.lastError();
       
    85         return;
       
    86     }
       
    87     updateConnections();
       
    88     getDevices();
       
    89 }
       
    90 
       
    91 NMView::~NMView()
       
    92 {
       
    93 }
       
    94 void NMView::init()
       
    95 {
       
    96     setupUi(this);
       
    97     sess = 0;
       
    98 //    readSettings();
       
    99     connect(startButton, SIGNAL(clicked()), this, SLOT(activate()));
       
   100     connect(stopButton, SIGNAL(clicked()), this, SLOT(deactivate()));
       
   101     connect(scanButton, SIGNAL(clicked()), this, SLOT(findAccessPoints()));
       
   102     connect(updateButton, SIGNAL(clicked()), this, SLOT(update()));
       
   103 
       
   104     connect(cons, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
       
   105             this,SLOT(connectionItemActivated(QTreeWidgetItem*, int)));
       
   106 
       
   107 
       
   108     connect(devicesTreeWidget, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
       
   109             this, SLOT(deviceItemActivated(QTreeWidgetItem*, int)));
       
   110 
       
   111     cons->header()->setResizeMode(QHeaderView::ResizeToContents);
       
   112     devicesTreeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
       
   113     accessPointsTreeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
       
   114 //    accessPointsTreeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
       
   115 
       
   116    manager = new QNetworkConfigurationManager(this);
       
   117 
       
   118    connect(manager, SIGNAL(updateCompleted()), this, SLOT(updateCompleted()));
       
   119 
       
   120    connect(manager, SIGNAL(configurationAdded(const QNetworkConfiguration &)),
       
   121            this, SLOT(configurationAdded(const QNetworkConfiguration&)));
       
   122 
       
   123    devicesTreeWidget->header()->hideSection(0);
       
   124    devicesTreeWidget->header()->hideSection(1);
       
   125    devicesTreeWidget->header()->hideSection(3);
       
   126    devicesTreeWidget->header()->hideSection(5);
       
   127    devicesTreeWidget->header()->hideSection(6);
       
   128    devicesTreeWidget->header()->hideSection(7);
       
   129    devicesTreeWidget->header()->hideSection(8);
       
   130    devicesTreeWidget->header()->hideSection(9);
       
   131 }
       
   132 
       
   133 void NMView::updateConnections()
       
   134 {
       
   135     cons->clear();
       
   136     manager->updateConfigurations();
       
   137     QList<QNetworkConfiguration> configs = manager->allConfigurations();
       
   138     foreach(QNetworkConfiguration p, configs) {
       
   139         QStringList strlist;
       
   140         strlist << p.name();
       
   141         strlist << stateToString(p.state());
       
   142         strlist << p.identifier();
       
   143         strlist << typeToString(p.type());
       
   144         QTreeWidgetItem *item;
       
   145         item = new QTreeWidgetItem(strlist);
       
   146         cons->insertTopLevelItem(0, item);
       
   147     }
       
   148     cons->sortItems ( 1, Qt::AscendingOrder);
       
   149 }
       
   150 
       
   151 void NMView::getActiveConnections()
       
   152 {
       
   153     QList<QNetworkConfiguration> configs = manager->allConfigurations(QNetworkConfiguration::Active);
       
   154     foreach(QNetworkConfiguration p, configs) {
       
   155         qWarning() << __FUNCTION__ << p.name() << p.identifier();
       
   156     }
       
   157 
       
   158 //     QDBusInterface iface(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, dbc);
       
   159 //     if (!iface.isValid()) {
       
   160 //         qWarning() << "Could not find NetworkManager";
       
   161 //         return;
       
   162 //     }
       
   163 
       
   164 //     QVariant prop = iface.property("ActiveConnections");
       
   165 //     QList<QDBusObjectPath> connections = prop.value<QList<QDBusObjectPath> >();
       
   166 //     foreach(QDBusObjectPath path, connections) {
       
   167 //         QDBusInterface conDetails(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_ACTIVE_CONNECTION, dbc);
       
   168 //         if (conDetails.isValid()) {
       
   169 //             QString desc = path.path();
       
   170 //            conDetails.property("Connection Path").value<QDBusObjectPath>().path();
       
   171 
       
   172 //            QList<QDBusObjectPath> devices = conDetails.property("Devices").value<QList<QDBusObjectPath> >();
       
   173 //
       
   174 //            foreach(QDBusObjectPath devpath, devices) {
       
   175 //                QDBusInterface devIface(NM_DBUS_SERVICE, devpath.path(), NM_DBUS_INTERFACE_DEVICE, dbc);
       
   176 //                desc += " "+devIface.property("Interface").toString();
       
   177 //                desc +=  " "+QHostAddress(htonl(devIface.property("Ip4Address").toUInt())).toString();
       
   178 
       
   179 //            }
       
   180 
       
   181 //            qWarning() << conDetails.property("Connection").value<QDBusObjectPath>().path() << path.path();
       
   182 
       
   183 //            QListWidgetItem* item = new QListWidgetItem(desc,  activeCons);
       
   184 //            item->setData(Qt::UserRole, path.path());
       
   185 //      }
       
   186 //    }
       
   187 }
       
   188 
       
   189 void NMView::update()
       
   190 {
       
   191 //    QNetworkManagerInterface *dbIface;
       
   192 //    dbIface = new QNetworkManagerInterface;
       
   193 //    QList <QDBusObjectPath> connections = dbIface->activeConnections();
       
   194 //
       
   195 //    foreach(QDBusObjectPath conpath, connections) {
       
   196 //        QNetworkManagerConnectionActive *aConn;
       
   197 //        aConn = new QNetworkManagerConnectionActive(conpath.path());
       
   198 //        // in case of accesspoint, specificObject will hold the accessPOintObjectPath
       
   199 //        qWarning() << aConn->connection().path() << aConn->specificObject().path() << aConn->devices().count();
       
   200 //        QList <QDBusObjectPath>  devs = aConn->devices();
       
   201 //        foreach(QDBusObjectPath device, devs) {
       
   202 //            qWarning() << "    " << device.path();
       
   203 //        }
       
   204 //    }
       
   205 
       
   206     QStringList connectionServices;
       
   207     connectionServices << NM_DBUS_SERVICE_SYSTEM_SETTINGS;
       
   208     connectionServices << NM_DBUS_SERVICE_USER_SETTINGS;
       
   209     foreach (QString service, connectionServices) {
       
   210         QDBusInterface allCons(service,
       
   211                                NM_DBUS_PATH_SETTINGS,
       
   212                                NM_DBUS_IFACE_SETTINGS,
       
   213                                dbc);
       
   214         if (allCons.isValid()) {
       
   215             QDBusReply<QList<QDBusObjectPath> > reply = allCons.call("ListConnections");
       
   216             if ( reply.isValid() ) {
       
   217                 QList<QDBusObjectPath> list = reply.value();
       
   218                 foreach(QDBusObjectPath path, list) {
       
   219                     QDBusInterface sysIface(service,
       
   220                                             path.path(),
       
   221                                             NM_DBUS_IFACE_SETTINGS_CONNECTION,
       
   222                                             dbc);
       
   223                     if (sysIface.isValid()) {
       
   224                         qWarning() << "";
       
   225                         qWarning() << path.path();
       
   226 
       
   227                         //                        QDBusMessage r = sysIface.call("GetSettings");
       
   228                         QDBusReply< QNmSettingsMap > rep = sysIface.call("GetSettings");
       
   229 
       
   230                         QMap< QString, QMap<QString,QVariant> > map = rep.value();
       
   231                         QList<QString> list = map.keys();
       
   232                         foreach (QString key, list) {
       
   233                             QMap<QString,QVariant> innerMap = map[key];
       
   234                             qWarning() << "       Key: " << key;
       
   235                             QMap<QString,QVariant>::const_iterator i = innerMap.constBegin();
       
   236 
       
   237                             while (i != innerMap.constEnd()) {
       
   238                                 QString k = i.key();
       
   239                                 qWarning() << "          Key: " << k << " Entry: " << i.value();
       
   240                                 i++;
       
   241                             }//end innerMap
       
   242                         }//end foreach key
       
   243                     }//end settings connection
       
   244                 } // foreach path
       
   245             } //end ListConnections
       
   246         } //end settingsInterface
       
   247     }// end services
       
   248     QDBusInterface iface(NM_DBUS_SERVICE,
       
   249                          NM_DBUS_PATH,
       
   250                          NM_DBUS_INTERFACE,
       
   251                          dbc);
       
   252     if (iface.isValid()) {
       
   253         QVariant prop = iface.property("ActiveConnections");
       
   254         QList<QDBusObjectPath> connections = prop.value<QList<QDBusObjectPath> >();
       
   255         foreach(QDBusObjectPath conpath, connections) {
       
   256             qWarning() << "Active connection" << conpath.path();
       
   257             QDBusInterface conDetails(NM_DBUS_SERVICE,
       
   258                                       conpath.path(),
       
   259                                       NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
       
   260                                       dbc);
       
   261             if (conDetails.isValid()) {
       
   262 
       
   263                 QVariant prop = conDetails.property("Connection");
       
   264                 QDBusObjectPath connection = prop.value<QDBusObjectPath>();
       
   265                 qWarning() << conDetails.property("Default").toBool() << connection.path();
       
   266 
       
   267 //                QVariant Sprop = conDetails.property("Devices");
       
   268 //                QList<QDBusObjectPath> so = Sprop.value<QList<QDBusObjectPath> >();
       
   269 //                foreach(QDBusObjectPath device, so) {
       
   270 //                    if(device.path() == devicePath) {
       
   271 //                        path = connection.path();
       
   272 //                    }
       
   273 //                    break;
       
   274 //                }
       
   275             }
       
   276         }
       
   277     }
       
   278 qWarning() << "";
       
   279 }
       
   280 
       
   281 void NMView::deactivate()
       
   282 {
       
   283     QList<QNetworkConfiguration> configs = manager->allConfigurations(QNetworkConfiguration::Active);
       
   284     foreach(QNetworkConfiguration p, configs) {
       
   285         qWarning() << "Active cons" << p.name();
       
   286         if(p.name() == cons->currentItem()->text(0)
       
   287             && p.identifier() == cons->currentItem()->text(2)) {
       
   288             qWarning() <<__FUNCTION__<< p.name();
       
   289             if(!sess) {
       
   290                 sess = new QNetworkSession(p);
       
   291 
       
   292                 connect(sess,  SIGNAL(stateChanged(QNetworkSession::State)),
       
   293                         this, SLOT(stateChanged(QNetworkSession::State)));
       
   294 
       
   295                 connect(sess,  SIGNAL(error(QNetworkSession::SessionError)),
       
   296                         this,  SLOT(networkSessionError(QNetworkSession::SessionError)));
       
   297             }
       
   298             sess->close();
       
   299             delete sess;
       
   300             sess = 0;
       
   301         }
       
   302     }
       
   303 }
       
   304 
       
   305 void NMView::activate()
       
   306 {
       
   307     qWarning() << __FUNCTION__ << cons->currentItem()->text(0);
       
   308 
       
   309     QList<QNetworkConfiguration> configs = manager->allConfigurations();
       
   310     foreach(QNetworkConfiguration p, configs) {
       
   311         if(p.name() == cons->currentItem()->text(0)) {
       
   312 
       
   313             sess = new QNetworkSession(p);
       
   314 
       
   315             connect(sess, SIGNAL(newConfigurationActivated()),
       
   316                     this, SLOT(getActiveConnections()));
       
   317 
       
   318             connect(sess,  SIGNAL(stateChanged(QNetworkSession::State)),
       
   319                     this, SLOT(stateChanged(QNetworkSession::State)));
       
   320 
       
   321             connect(sess,  SIGNAL(error(QNetworkSession::SessionError)),
       
   322                     this,  SLOT(networkSessionError(QNetworkSession::SessionError)));
       
   323 
       
   324             sess->open();
       
   325         }
       
   326     }
       
   327 }
       
   328 
       
   329 void NMView::getDevices()
       
   330 {
       
   331     qWarning() << "";
       
   332     qWarning() << __FUNCTION__;
       
   333     devicesTreeWidget->clear();
       
   334     //qDBusRegisterMetaType<SettingsMap>();
       
   335 
       
   336     if (!dbc.isConnected()) {
       
   337         qWarning() << "Unable to connect to D-Bus:" << dbc.lastError();
       
   338         return;
       
   339     }
       
   340         QDBusInterface iface(NM_DBUS_SERVICE,
       
   341                              NM_DBUS_PATH,
       
   342                              NM_DBUS_INTERFACE,
       
   343                              dbc);
       
   344 //NetworkManager interface
       
   345         if (!iface.isValid()) {
       
   346             qWarning() << "Could not find NetworkManager";
       
   347             return;
       
   348         }
       
   349 
       
   350 //         uint state = iface.property("State").toUInt();
       
   351 //         switch(state) {
       
   352 //         case NM_STATE_UNKNOWN:
       
   353 //             qWarning() << "State: Unknown"; break;
       
   354 //         case NM_STATE_ASLEEP:
       
   355 //             qWarning() << "State: Asleep"; break;
       
   356 //         case NM_STATE_CONNECTING:
       
   357 //             qWarning() << "State: Connecting"; break;
       
   358 //         case NM_STATE_CONNECTED:
       
   359 //             qWarning() << "State: Connected"; break;
       
   360 //         case NM_STATE_DISCONNECTED:
       
   361 //             qWarning() << "State: Disconnected"; break;
       
   362 //         }
       
   363 
       
   364         //get list of network devices
       
   365         QTreeWidgetItem *item;
       
   366         QDBusReply<QList<QDBusObjectPath> > reply = iface.call("GetDevices");
       
   367         if ( reply.isValid() ) {
       
   368 //            qWarning() << "Current devices:";
       
   369             QList<QDBusObjectPath> list = reply.value();
       
   370             foreach(QDBusObjectPath path, list) {
       
   371                 QStringList devicesList;
       
   372                 qWarning() << "  " << path.path();
       
   373                 devicesList << path.path();
       
   374 
       
   375                 QDBusInterface devIface(NM_DBUS_SERVICE,
       
   376                                         path.path(),
       
   377                                         NM_DBUS_INTERFACE_DEVICE,
       
   378                                         dbc);
       
   379                 if (devIface.isValid()) {
       
   380 
       
   381 
       
   382 
       
   383                     ////// connect the dbus signal
       
   384 //                     /*if(*/dbc.connect(NM_DBUS_SERVICE,
       
   385 //                                    path.path(),
       
   386 //                                    NM_DBUS_INTERFACE_DEVICE,
       
   387 //                                    "StateChanged",
       
   388 //                                    this,SLOT(deviceStateChanged(quint32)));
       
   389 //                    {
       
   390 //                        qWarning() << "XXXXXXXXXX dbus connect successful" <<  path.path();
       
   391 //                    }
       
   392 
       
   393 
       
   394           //          qWarning() << "     Managed: " << devIface.property("Managed").toBool();
       
   395                     devicesList << devIface.property("Managed").toString();
       
   396  //                   qWarning() << "     Interface: " << devIface.property("Interface").toString();
       
   397                     devicesList <<  devIface.property("Interface").toString();
       
   398   //                  qWarning() << "     HAL UDI: " << devIface.property("Udi").toString();
       
   399    //                 qWarning() << "     Driver: " << devIface.property("Driver").toString();
       
   400                     devicesList  << devIface.property("Driver").toString();
       
   401 
       
   402                     QString x =  deviceTypeToString(devIface.property("DeviceType").toUInt());
       
   403  //                   qWarning() << "     Type:" << x;
       
   404                     devicesList << x;
       
   405 
       
   406                                  if( devIface.property("DeviceType").toUInt() == DEVICE_TYPE_802_11_WIRELESS) {
       
   407                                      qWarning() << "Device is WIFI";
       
   408                     //                //    findAccessPoints(path.path());
       
   409                                     }
       
   410 
       
   411                     QVariant v = devIface.property("State");
       
   412                     x = deviceStateToString(v.toUInt());
       
   413 //                    qWarning() << "     State:" << x;
       
   414                     devicesList << x;
       
   415 
       
   416                     quint32 ip = devIface.property("Ip4Address").toUInt();
       
   417 //                    qWarning() << "     IP4Address: " << QHostAddress(htonl(ip)).toString();
       
   418                     devicesList << QHostAddress(htonl(ip)).toString();
       
   419 
       
   420 
       
   421                     if (v.toUInt() == NM_DEVICE_STATE_ACTIVATED) {
       
   422                         QString path = devIface.property("Ip4Config").value<QDBusObjectPath>().path();
       
   423 //                        qWarning() << "     IP4Config: " << path;
       
   424                         devicesList << path;
       
   425                         QDBusInterface ipIface(NM_DBUS_SERVICE,
       
   426                                                path,
       
   427                                                NM_DBUS_INTERFACE_IP4_CONFIG,
       
   428                                                dbc);
       
   429                         if (ipIface.isValid()) {
       
   430       //                      qWarning() << "        Hostname: " << ipIface.property("Hostname").toString();
       
   431                             devicesList << ipIface.property("Hostname").toString();
       
   432 //                            qWarning() << "        Domains: " << ipIface.property("Domains").toStringList();
       
   433                             devicesList <<  ipIface.property("Domains").toStringList().join(", ");
       
   434 //                            qWarning() << "        NisDomain: " << ipIface.property("NisDomain").toString();
       
   435                             QDBusArgument arg=  ipIface.property("Addresses").value<QDBusArgument>();
       
   436 //                            qWarning() << "        Addresses: " << ipIface.property("Addresses");
       
   437 //                            qWarning() << "        Nameservers: " << ipIface.property("Nameservers");
       
   438 //                            qWarning() << "        NisServers: " << ipIface.property("NisServers");
       
   439                         }
       
   440 
       
   441                     }
       
   442 
       
   443                 }
       
   444                 item = new QTreeWidgetItem(devicesList);
       
   445                 devicesTreeWidget->insertTopLevelItem(0, item);
       
   446             }
       
   447         }
       
   448 
       
   449 
       
   450 //        netconfig();
       
   451 
       
   452 }
       
   453 
       
   454 void NMView::readSettings()
       
   455 {
       
   456         QDBusInterface settingsiface(NM_DBUS_SERVICE_SYSTEM_SETTINGS,
       
   457                                      NM_DBUS_PATH_SETTINGS,
       
   458                                      NM_DBUS_IFACE_SETTINGS,
       
   459                                      dbc);
       
   460         //NetworkManagerSettings interface
       
   461         if (settingsiface.isValid()) {
       
   462             QDBusReply<QList<QDBusObjectPath> > reply = settingsiface.call("ListConnections");
       
   463             if ( reply.isValid() ) {
       
   464                 QList<QDBusObjectPath> list = reply.value();
       
   465                 foreach(QDBusObjectPath path, list) {
       
   466                     qWarning() <<__FUNCTION__ << path.path();
       
   467 
       
   468                 }
       
   469             }
       
   470         }
       
   471 
       
   472         QDBusInterface iface(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, dbc);
       
   473         if (!iface.isValid()) {
       
   474             qWarning() << "Could not find NetworkManager";
       
   475             return;
       
   476         }
       
   477 
       
   478         //   QStringList list = item->text().split(" -> ");
       
   479 
       
   480         QVariant prop = iface.property("ActiveConnections");
       
   481         QList<QDBusObjectPath> connections = prop.value<QList<QDBusObjectPath> >();
       
   482         foreach(QDBusObjectPath path, connections) {
       
   483             QDBusInterface conDetails(NM_DBUS_SERVICE,
       
   484                                       path.path(),
       
   485                                       NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
       
   486                                       dbc);
       
   487 
       
   488             if (conDetails.isValid()) {
       
   489                 QString desc = path.path();
       
   490                 qWarning() << desc;
       
   491                 //if ( item->text(0) == conDetails.property("Connection").value<QDBusObjectPath>().path() ) {
       
   492                     //                QListWidgetItem* item = new QTreeWidgetItem( desc,
       
   493                     //                                                             cons);
       
   494                     //                item->setData(Qt::UserRole, desc);
       
   495                     //                activeItemActivated( item );
       
   496                 //}
       
   497             }
       
   498         }
       
   499 }
       
   500 
       
   501 void NMView::printConnectionDetails(const QString& service)
       
   502 {
       
   503     Q_UNUSED(service);
       
   504 
       
   505 //
       
   506 //    qWarning() << __FUNCTION__ << service;
       
   507 //
       
   508 //    QDBusConnection dbc = QDBusConnection::systemBus();
       
   509 //    if (!dbc.isConnected()) {
       
   510 //        qWarning() << "Unable to connect to D-Bus:" << dbc.lastError();
       
   511 //        return;
       
   512 //    }
       
   513 //    QDBusInterface allCons(service,
       
   514 //                           NM_DBUS_PATH_SETTINGS,
       
   515 //                           NM_DBUS_IFACE_SETTINGS,
       
   516 //                           dbc);
       
   517 //
       
   518 //    if (allCons.isValid()) {
       
   519 //        QDBusReply<QList<QDBusObjectPath> > reply = allCons.call("ListConnections");
       
   520 //
       
   521 //        if ( reply.isValid() ) {
       
   522 //            qWarning() << "Known connections:";
       
   523 //            QList<QDBusObjectPath> list = reply.value();
       
   524 //
       
   525 //            foreach(QDBusObjectPath path, list) {
       
   526 //                qWarning() << "  " << path.path();
       
   527 //
       
   528 //                QDBusInterface sysIface(NM_DBUS_SERVICE_SYSTEM_SETTINGS,
       
   529 //                                        path.path(),
       
   530 //                                        NM_DBUS_IFACE_SETTINGS_CONNECTION,
       
   531 //                                        dbc);
       
   532 //
       
   533 //                if (sysIface.isValid()) {
       
   534 //                    QDBusMessage r = sysIface.call("GetSettings");
       
   535 //                    QDBusReply< QSettingsMap > rep = sysIface.call("GetSettings");
       
   536 //
       
   537 //                    qWarning() << "     GetSettings:" << r.arguments() << r.signature() << rep.isValid() << sysIface.lastError();
       
   538 //
       
   539 //                    QMap< QString, QMap<QString,QVariant> > map = rep.value();
       
   540 //                    QList<QString> list = map.keys();
       
   541 //
       
   542 //                    foreach (QString key, list) {
       
   543 //                        QMap<QString,QVariant> innerMap = map[key];
       
   544 //                        qWarning() << "       Key: " << key;
       
   545 //                        QMap<QString,QVariant>::const_iterator i = innerMap.constBegin();
       
   546 //
       
   547 //                        while (i != innerMap.constEnd()) {
       
   548 //                            QString k = i.key();
       
   549 //                            qWarning() << "          Key: " << k << " Entry: " << i.value();
       
   550 //
       
   551 //                            if (k == "addresses" && i.value().canConvert<QDBusArgument>()) {
       
   552 //                                QDBusArgument arg = i.value().value<QDBusArgument>();
       
   553 //                                arg.beginArray();
       
   554 //
       
   555 //                                while (!arg.atEnd()) {
       
   556 //                                    QDBusVariant addr;
       
   557 //                                    arg >> addr;
       
   558 //                                    uint ip = addr.variant().toUInt();
       
   559 //                                    qWarning() << ip;
       
   560 //                                    qWarning() << "        " << QHostAddress(htonl(ip)).toString();
       
   561 //                                }
       
   562 //
       
   563 //                            }
       
   564 //                            i++;
       
   565 //                        }
       
   566 //                    }
       
   567 //                }
       
   568 //            }
       
   569 //        }
       
   570 //    }
       
   571     qWarning() << "";
       
   572 }
       
   573 
       
   574 void NMView::getNetworkDevices()
       
   575 {
       
   576 
       
   577 }
       
   578 
       
   579 void NMView::connectionItemActivated( QTreeWidgetItem * item, int col )
       
   580 {
       
   581     Q_UNUSED(col);
       
   582 
       
   583     qWarning() <<__FUNCTION__<< item->text(0);
       
   584     QDBusInterface iface(NM_DBUS_SERVICE,
       
   585                          NM_DBUS_PATH,
       
   586                          NM_DBUS_INTERFACE,
       
   587                          dbc);
       
   588     if (!iface.isValid()) {
       
   589         qWarning() << "Could not find NetworkManager";
       
   590         return;
       
   591     }
       
   592     QVariant prop = iface.property("ActiveConnections");
       
   593     QList<QDBusObjectPath> connections = prop.value<QList<QDBusObjectPath> >();
       
   594     foreach(QDBusObjectPath path, connections) {
       
   595         QDBusInterface conDetails(NM_DBUS_SERVICE,
       
   596                                   path.path(),
       
   597                                   NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
       
   598                                   dbc);
       
   599 
       
   600         if (conDetails.isValid()) {
       
   601             QString desc = path.path();
       
   602             qWarning() << desc;
       
   603             if ( item->text(0) == conDetails.property("Connection").value<QDBusObjectPath>().path() ) {
       
   604 //                QListWidgetItem* item = new QTreeWidgetItem( desc,
       
   605 //                                                             cons);
       
   606 //                item->setData(Qt::UserRole, desc);
       
   607 //                activeItemActivated( item );
       
   608             }
       
   609         }
       
   610     }
       
   611 }
       
   612 
       
   613 
       
   614 
       
   615 void NMView::deviceItemActivated( QTreeWidgetItem * item , int section)
       
   616 {
       
   617     Q_UNUSED(item);
       
   618     Q_UNUSED(section);
       
   619 
       
   620   //  qWarning() << item->text(section) << item->text(4);
       
   621     //if(item->text(4) == "Wireless") findAccessPoints(item->text(0));
       
   622 }
       
   623 
       
   624 
       
   625 void NMView::netconfig()
       
   626 {
       
   627 //    qWarning() << __FUNCTION__;
       
   628 
       
   629 //    qDBusRegisterMetaType<SettingsMap>();
       
   630     QDBusConnection dbc = QDBusConnection::systemBus();
       
   631     if (!dbc.isConnected()) {
       
   632         qWarning() << "Unable to connect to D-Bus:" << dbc.lastError();
       
   633         return;
       
   634     }
       
   635 
       
   636     QDBusInterface iface(NM_DBUS_SERVICE,
       
   637                          NM_DBUS_PATH,
       
   638                          NM_DBUS_IFACE_SETTINGS_CONNECTION,
       
   639                          dbc);
       
   640     if (!iface.isValid()) {
       
   641         qWarning() << "Could not find NetworkManager Settings";
       
   642         return;
       
   643     } else {
       
   644         QDBusReply<QList<QDBusObjectPath> > reply = iface.call("ListConnections");
       
   645             QList<QDBusObjectPath> list = reply.value();
       
   646 //            qWarning() << reply.value();
       
   647             foreach(QDBusObjectPath path, list) {
       
   648                 qWarning() << "  " << path.path();
       
   649             }
       
   650     }
       
   651 }
       
   652 
       
   653 
       
   654 void NMView::findAccessPoints()
       
   655 {
       
   656     accessPointsTreeWidget->clear();
       
   657 
       
   658     QDBusInterface iface(NM_DBUS_SERVICE,
       
   659                          NM_DBUS_PATH,
       
   660                          NM_DBUS_INTERFACE,
       
   661                          dbc);
       
   662 
       
   663     QDBusReply<QList<QDBusObjectPath> > reply = iface.call("GetDevices");
       
   664     if ( reply.isValid() ) {
       
   665         QList<QDBusObjectPath> list = reply.value();
       
   666         foreach(QDBusObjectPath path, list) {
       
   667             QDBusInterface devIface(NM_DBUS_SERVICE,
       
   668                                     path.path(),
       
   669                                     NM_DBUS_INTERFACE_DEVICE,
       
   670                                     dbc);
       
   671             if (devIface.isValid()) {
       
   672 
       
   673                 if( devIface.property("DeviceType").toUInt() == DEVICE_TYPE_802_11_WIRELESS) {
       
   674 
       
   675 //                    qWarning() <<"deviface"<< devIface.path();
       
   676                     QDBusInterface wififace(NM_DBUS_SERVICE,
       
   677                                             devIface.path(),
       
   678                                             NM_DBUS_INTERFACE_DEVICE_WIRELESS,
       
   679                                             dbc);
       
   680                     if (!wififace.isValid()) {
       
   681                         qWarning() << "Could not find NetworkManager wireless interface";
       
   682                         return;
       
   683                     }
       
   684 
       
   685 /////////////////////////
       
   686 //                     if(dbc.connect(NM_DBUS_SERVICE,
       
   687 //                                    path.path(),
       
   688 //                                    NM_DBUS_INTERFACE_DEVICE_WIRELESS,
       
   689 //                                    "PropertiesChanged",
       
   690 //                                    this,SLOT(aPPropertiesChanged( QMap<QString,QVariant>))) ) {
       
   691 //                         qWarning() << "PropertiesChanged connect";
       
   692 //                     } else {
       
   693 //                         qWarning() << "NOT connect";
       
   694 
       
   695 //                     }
       
   696 /////////////////////////
       
   697                     qWarning() <<"wififace"<< wififace.path();
       
   698                     QDBusReply<QList<QDBusObjectPath> > reply2 = wififace.call("GetAccessPoints");
       
   699                     if ( reply2.isValid() ) {
       
   700                         QTreeWidgetItem *item;
       
   701                         QList<QDBusObjectPath> list2 = reply2.value();
       
   702                         foreach(QDBusObjectPath path2, list2) {
       
   703                             QDBusInterface accessPointIface(NM_DBUS_SERVICE,
       
   704                                                             path2.path(),
       
   705                                                             NM_DBUS_INTERFACE_ACCESS_POINT,
       
   706                                                             dbc);
       
   707                             if (accessPointIface.isValid()) {
       
   708 ////                                qWarning() <<"path2" <<  path2.path();
       
   709 
       
   710 //                                if(dbc.connect(NM_DBUS_SERVICE, //signal strength
       
   711 //                                               path2.path(),
       
   712 //                                               NM_DBUS_INTERFACE_ACCESS_POINT,
       
   713 //                                               "PropertiesChanged",
       
   714 //                                               this,SLOT(aPPropertiesChanged( QMap<QString,QVariant>))) ) {
       
   715 //                                    qWarning() << "PropertiesChanged connect";
       
   716 //                                } else {
       
   717 //                                    qWarning() << "NOT connect";
       
   718 //
       
   719 //                                }
       
   720 
       
   721                                 QStringList accessPoints;
       
   722                                 accessPoints << accessPointIface.property("Ssid").toString();
       
   723                                 accessPoints << QString::number(accessPointIface.property("Strength").toInt());
       
   724                                 accessPoints << securityCapabilitiesToString(accessPointIface.property("WpaFlags" ).toInt());
       
   725                                 accessPoints << securityCapabilitiesToString(accessPointIface.property("RsnFlags" ).toInt());
       
   726                                 accessPoints << accessPointIface.property("Frequency" ).toString();
       
   727                                 accessPoints << accessPointIface.property("HwAddress" ).toString();
       
   728                                 accessPoints << deviceModeToString(accessPointIface.property("Mode" ).toInt());
       
   729                                 accessPoints << accessPointIface.property("MaxBitrate" ).toString();
       
   730                                 //HwAddress
       
   731                                 //                if(accessPointIface.property("Flags" ).toInt() == NM_802_11_AP_FLAGS_PRIVACY ) {
       
   732                                 //                    qWarning() << "Secure";
       
   733                                 //                }
       
   734                                 item = new QTreeWidgetItem(accessPoints);
       
   735                                 accessPointsTreeWidget->insertTopLevelItem(0, item);
       
   736                             }
       
   737                         }
       
   738                     }
       
   739                 }
       
   740             }
       
   741         }
       
   742     }
       
   743 }
       
   744 
       
   745 QString NMView::deviceStateToString(int state)
       
   746 {
       
   747     QString ret;
       
   748     switch(state) {
       
   749     case NM_DEVICE_STATE_UNKNOWN:
       
   750         ret = "Unknown";
       
   751         break;
       
   752     case NM_DEVICE_STATE_UNMANAGED:
       
   753         ret =  "Unmanaged";
       
   754         break;
       
   755     case NM_DEVICE_STATE_UNAVAILABLE:
       
   756         ret =  "Unavailable";
       
   757         break;
       
   758     case NM_DEVICE_STATE_DISCONNECTED:
       
   759         ret = "Disconnected";
       
   760         break;
       
   761     case NM_DEVICE_STATE_PREPARE:
       
   762         ret = "Preparing to connect";
       
   763         break;
       
   764     case NM_DEVICE_STATE_CONFIG:
       
   765         ret = "Being configured";
       
   766         break;
       
   767     case NM_DEVICE_STATE_NEED_AUTH:
       
   768         ret = "Awaiting secrets";
       
   769         break;
       
   770     case NM_DEVICE_STATE_IP_CONFIG:
       
   771         ret = "IP requested";
       
   772         break;
       
   773     case NM_DEVICE_STATE_ACTIVATED:
       
   774         ret = "Activated";
       
   775         break;
       
   776     case NM_DEVICE_STATE_FAILED:
       
   777         ret = "FAILED";
       
   778         break;
       
   779     };
       
   780     return ret;
       
   781 }
       
   782 
       
   783 QString NMView::deviceTypeToString(int device)
       
   784 {
       
   785     QString ret;
       
   786     switch(device) {
       
   787     case DEVICE_TYPE_UNKNOWN:
       
   788         ret = "Unknown";
       
   789         break;
       
   790     case DEVICE_TYPE_802_3_ETHERNET:
       
   791         ret = "Ethernet";
       
   792         break;
       
   793     case DEVICE_TYPE_802_11_WIRELESS:
       
   794         ret = "Wireless";
       
   795         break;
       
   796     case DEVICE_TYPE_GSM:
       
   797         ret = "GSM";
       
   798         break;
       
   799     case DEVICE_TYPE_CDMA:
       
   800         ret = "CDMA";
       
   801         break;
       
   802     };
       
   803     return ret;
       
   804 }
       
   805 
       
   806 QString NMView::securityCapabilitiesToString(int caps)
       
   807 {
       
   808     int check = caps;
       
   809 //    qWarning() << __FUNCTION__<< caps;
       
   810     QString ret;
       
   811     if( check == 0 )
       
   812         ret += "None.";
       
   813     if( (check & NM_802_11_AP_SEC_PAIR_WEP40))
       
   814         ret += " 40-bit WEP encryption. ";
       
   815     if( (check & NM_802_11_AP_SEC_PAIR_WEP104) )
       
   816         ret += " 104-bit WEP encryption. ";
       
   817     if( (check & NM_802_11_AP_SEC_PAIR_TKIP) )
       
   818         ret += " TKIP encryption. ";
       
   819     if( (check & NM_802_11_AP_SEC_PAIR_CCMP) )
       
   820         ret += " CCMP encryption. ";
       
   821     if( (check & NM_802_11_AP_SEC_GROUP_WEP40))
       
   822         ret += " 40-bit WEP cipher. ";
       
   823     if( (check & NM_802_11_AP_SEC_GROUP_WEP104))
       
   824         ret += " 104-bit WEP cipher. ";
       
   825     if( (check & NM_802_11_AP_SEC_GROUP_TKIP)  )
       
   826         ret += " TKIP cipher. ";
       
   827     if( (check & NM_802_11_AP_SEC_GROUP_CCMP)  )
       
   828         ret += " CCMP cipher. ";
       
   829     if( (check & NM_802_11_AP_SEC_KEY_MGMT_PSK))
       
   830         ret += " PSK key management. ";
       
   831     if( (check & NM_802_11_AP_SEC_KEY_MGMT_802_1X) )
       
   832         ret += " 802.1x key management. ";
       
   833     return ret;
       
   834 }
       
   835 
       
   836 QString NMView::deviceModeToString(int mode)
       
   837 {
       
   838     QString ret;
       
   839     switch (mode) {
       
   840     case NM_802_11_MODE_UNKNOWN:
       
   841         ret = "Unknown";
       
   842         break;
       
   843     case NM_802_11_MODE_ADHOC:
       
   844         ret = " (Adhoc).";
       
   845         break;
       
   846     case NM_802_11_MODE_INFRA:
       
   847         ret = " (Infrastructure)";
       
   848     };
       
   849     return ret;
       
   850 }
       
   851 
       
   852 void NMView::netManagerState(quint32 state)
       
   853 {
       
   854     qWarning() << __FUNCTION__ << state;// << old << reason;
       
   855     switch(state) {
       
   856     case NM_STATE_UNKNOWN:
       
   857         qWarning() << "The NetworkManager daemon is in an unknown state. ";
       
   858         break;
       
   859     case NM_STATE_ASLEEP:
       
   860         qWarning() << "The NetworkManager daemon is asleep and all interfaces managed by it are inactive. ";
       
   861         break;
       
   862     case NM_STATE_CONNECTING:
       
   863         qWarning() << "The NetworkManager daemon is connecting a device. FIXME: What does this mean when one device is active and another is connecting? ";
       
   864         break;
       
   865     case NM_STATE_CONNECTED:
       
   866         qWarning() <<"The NetworkManager daemon is connected. ";
       
   867         break;
       
   868     case NM_STATE_DISCONNECTED:
       
   869         qWarning() << "The NetworkManager daemon is disconnected.";
       
   870         break;
       
   871 
       
   872     };
       
   873 }
       
   874 
       
   875 void NMView::deviceStateChanged(quint32 state)
       
   876 {
       
   877   qWarning() << __FUNCTION__ << state;
       
   878     switch(state) {
       
   879     case NM_DEVICE_STATE_UNKNOWN :
       
   880         qWarning() <<"The device is in an unknown state. ";
       
   881         break;
       
   882     case NM_DEVICE_STATE_UNMANAGED:
       
   883         qWarning() <<"The device is not managed by NetworkManager.";
       
   884         break;
       
   885     case NM_DEVICE_STATE_UNAVAILABLE:
       
   886         qWarning() <<"The device cannot be used (carrier off, rfkill, etc).";
       
   887         break;
       
   888     case NM_DEVICE_STATE_DISCONNECTED:
       
   889         qWarning() <<"The device is not connected.";
       
   890         break;
       
   891     case NM_DEVICE_STATE_PREPARE:
       
   892         qWarning() <<"The device is preparing to connect.";
       
   893         break;
       
   894     case NM_DEVICE_STATE_CONFIG:
       
   895         qWarning() <<"The device is being configured.";
       
   896         break;
       
   897     case NM_DEVICE_STATE_NEED_AUTH:
       
   898         qWarning() <<"The device is awaiting secrets necessary to continue connection.";
       
   899         break;
       
   900     case NM_DEVICE_STATE_IP_CONFIG:
       
   901         qWarning() <<"The IP settings of the device are being requested and configured.";
       
   902         break;
       
   903     case NM_DEVICE_STATE_ACTIVATED:
       
   904         qWarning() <<"The device is active.";
       
   905         break;
       
   906     case NM_DEVICE_STATE_FAILED:
       
   907         qWarning() <<"The device is in a failure state following an attempt to activate it.";
       
   908         break;
       
   909     };
       
   910 }
       
   911 
       
   912 void NMView::updateCompleted()
       
   913 {
       
   914     qWarning() << __FUNCTION__;
       
   915 }
       
   916 
       
   917 void NMView::newConfigurationActivated()
       
   918 {
       
   919     qWarning() << __FUNCTION__;
       
   920     getActiveConnections();
       
   921 }
       
   922 
       
   923 void NMView::stateChanged(QNetworkSession::State state)
       
   924 {
       
   925   //  QString stateStringstr;
       
   926     switch (state) {
       
   927     case QNetworkSession::Invalid:
       
   928         stateString = "Invalid session";
       
   929         break;
       
   930     case QNetworkSession::NotAvailable:
       
   931         stateString = "Session is defined but not yet discovered";
       
   932         break;
       
   933     case QNetworkSession::Connecting:
       
   934         stateString = "Session is being established";
       
   935         break;
       
   936     case QNetworkSession::Connected:
       
   937         stateString = "Session is active and can be used for socket operations";
       
   938         cons->currentItem()->setText(1, "Active");
       
   939         break;
       
   940     case QNetworkSession::Closing:
       
   941         stateString = "Session is being shutdown";
       
   942         break;
       
   943     case QNetworkSession::Disconnected:
       
   944         qWarning() << __FUNCTION__;
       
   945         manager->updateConfigurations();
       
   946         stateString = "Session disconnected";
       
   947         cons->currentItem()->setText(1,"Discovered") ;
       
   948         break;
       
   949     case QNetworkSession::Roaming:
       
   950         stateString = "session is roaming from one AP to another";
       
   951         break;
       
   952     };
       
   953     qWarning() << __FUNCTION__ << state << stateString;
       
   954 }
       
   955 
       
   956 QString NMView::stateToString(int state)
       
   957 {
       
   958     switch (state) {
       
   959     case QNetworkConfiguration::Undefined:
       
   960         return "Undefined";
       
   961         break;
       
   962     case QNetworkConfiguration::Defined:
       
   963         return "Defined";
       
   964         break;
       
   965     case QNetworkConfiguration::Discovered:
       
   966         return "Discovered";
       
   967         break;
       
   968     case QNetworkConfiguration::Active:
       
   969         return "Active";
       
   970         break;
       
   971     };
       
   972     return "";
       
   973 }
       
   974 
       
   975 void NMView::configurationAdded(const QNetworkConfiguration &config)
       
   976 {
       
   977     qWarning() << __FUNCTION__<< config.name() << config.identifier();
       
   978 }
       
   979 
       
   980 QString NMView::typeToString(int type)
       
   981 {
       
   982     switch(type) {
       
   983     case QNetworkConfiguration::InternetAccessPoint:
       
   984         return "Internet AccessPoint";
       
   985         break;
       
   986     case QNetworkConfiguration::ServiceNetwork:
       
   987         return "Service Network";
       
   988         break;
       
   989     };
       
   990     return QString();
       
   991 }
       
   992 
       
   993 void NMView::aPPropertiesChanged( QMap<QString,QVariant> map)
       
   994 {
       
   995     //QMap<QString, QVariant>::const_iterator iterator = d->values.find(type + QLatin1String("Path"))
       
   996     QMapIterator<QString, QVariant> i(map);
       
   997     while (i.hasNext()) {
       
   998         i.next();
       
   999         if( i.key() == "State") {
       
  1000             qWarning()  << __FUNCTION__ << i.key() << ": " << i.value().toUInt();
       
  1001 //            deviceStateChanged(i.value().toUInt());
       
  1002         } else if( i.key() == "ActiveAccessPoint") {
       
  1003             qWarning()  << __FUNCTION__ << i.key() << ": " << i.value().value<QDBusObjectPath>().path();
       
  1004         } else if( i.key() == "Strength")
       
  1005             qWarning()  << __FUNCTION__ << i.key() << ": " << i.value().toUInt();
       
  1006         else
       
  1007             qWarning()  << __FUNCTION__ << i.key() << ": " << i.value();
       
  1008     }
       
  1009 }
       
  1010 
       
  1011 void NMView::networkSessionError(QNetworkSession::SessionError error)
       
  1012 {
       
  1013     QString errorStr;
       
  1014     switch(error) {
       
  1015     case QNetworkSession::RoamingError:
       
  1016         errorStr = "Roaming error";
       
  1017         break;
       
  1018     case QNetworkSession::SessionAbortedError:
       
  1019         errorStr = "Session aborted by user or system";
       
  1020         break;
       
  1021     default:
       
  1022     case QNetworkSession::UnknownSessionError:
       
  1023         errorStr = "Unidentified Error";
       
  1024         break;
       
  1025     };
       
  1026 
       
  1027     QMessageBox::warning(this, tr("NMView"), errorStr, QMessageBox::Ok);
       
  1028 }