ginebra2/sym_iap_util.h
changeset 5 0f2326c2a325
parent 0 1450b09d0cfd
equal deleted inserted replaced
1:b0dd75e285d2 5:0f2326c2a325
     1 /*
     1 /****************************************************************************
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 **
     3 * All rights reserved.
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 * This component and the accompanying materials are made available
     4 ** All rights reserved.
     5 * under the terms of "Eclipse Public License v1.0"
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 * which accompanies this distribution, and is available
     6 **
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 ** This file is part of the examples of the Qt Toolkit.
     8 *
     8 **
     9 * Initial Contributors:
     9 ** $QT_BEGIN_LICENSE:LGPL$
    10 * Nokia Corporation - initial contribution.
    10 ** Commercial Usage
    11 *
    11 ** Licensees holding valid Qt Commercial licenses may use this file in
    12 * Contributors:
    12 ** accordance with the Qt Commercial License Agreement provided with the
    13 *
    13 ** Software or, alternatively, in accordance with the terms contained in
    14 * Description: 
    14 ** a written agreement between you and Nokia.
    15 *
    15 **
    16 */
    16 ** GNU Lesser General Public License Usage
    17 
    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 ** GNU General Public License Usage
       
    29 ** Alternatively, this file may be used under the terms of the GNU
       
    30 ** General Public License version 3.0 as published by the Free Software
       
    31 ** Foundation and appearing in the file LICENSE.GPL included in the
       
    32 ** packaging of this file.  Please review the following information to
       
    33 ** ensure the GNU General Public License version 3.0 requirements will be
       
    34 ** met: http://www.gnu.org/copyleft/gpl.html.
       
    35 **
       
    36 ** If you have questions regarding the use of this file, please contact
       
    37 ** Nokia at qt-info@nokia.com.
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
    18 #ifndef QSYM_IAP_UTIL_H
    41 #ifndef QSYM_IAP_UTIL_H
    19 #define QSYM_IAP_UTIL_H
    42 #define QSYM_IAP_UTIL_H
    20 
    43 
    21 // Symbian
    44 // Symbian
    22 #include <utf.h>
    45 #include <utf.h>
    86         settings.endGroup();
   109         settings.endGroup();
    87     settings.endGroup();
   110     settings.endGroup();
    88 }
   111 }
    89 
   112 
    90 static QString qt_TNameToQString(TName data) {
   113 static QString qt_TNameToQString(TName data) {
    91     if(utf16LETextCodec == 0)
   114     if (utf16LETextCodec == 0)
    92         utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
   115         utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
    93 
   116 
    94     QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
   117     QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
    95     return utf16LETextCodec->toUnicode(tmpByteArray);
   118     return utf16LETextCodec->toUnicode(tmpByteArray);
    96 }
   119 }
   116     printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
   139     printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
   117 
   140 
   118     TPckgBuf<TSoInetInterfaceInfo> info, next;
   141     TPckgBuf<TSoInetInterfaceInfo> info, next;
   119 
   142 
   120     TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
   143     TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
   121     if(res!=KErrNone)
   144     if (res!=KErrNone)
   122         User::Leave(res);
   145         User::Leave(res);
   123     TInt count = 0;
   146     TInt count = 0;
   124     while(res==KErrNone) {
   147     while (res==KErrNone) {
   125         res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
   148         res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
   126 
   149 
   127         if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
   150         if (info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
   128             printf("Interface %d\n", count++);
   151             printf("Interface %d\n", count++);
   129 
   152 
   130             printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
   153             printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
   131             printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
   154             printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
   132 
   155 
   163 
   186 
   164             TName address;
   187             TName address;
   165             info().iAddress.Output(address);
   188             info().iAddress.Output(address);
   166             printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
   189             printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
   167 
   190 
   168             if(info().iAddress.IsLinkLocal()) {
   191             if (info().iAddress.IsLinkLocal()) {
   169                 printf("  -link local\n");
   192                 printf("  -link local\n");
   170             } else if(info().iAddress.IsSiteLocal()) {
   193             } else if (info().iAddress.IsSiteLocal()) {
   171                 printf("  -site local\n");
   194                 printf("  -site local\n");
   172             } else {
   195             } else {
   173                 printf("  -global\n");
   196                 printf("  -global\n");
   174             }
   197             }
   175 
   198 
   189             printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
   212             printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
   190 
   213 
   191             if (info().iHwAddr.Family() != KAFUnspec) {
   214             if (info().iHwAddr.Family() != KAFUnspec) {
   192                 printf("Hardware address ");
   215                 printf("Hardware address ");
   193                 TUint j;
   216                 TUint j;
   194                 for(j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
   217                 for (j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
   195                     if(j < (TUint)info().iHwAddr.Length()) {
   218                     if (j < (TUint)info().iHwAddr.Length()) {
   196                         printf("%02X", info().iHwAddr[j]);
   219                         printf("%02X", info().iHwAddr[j]);
   197                     } else {
   220                     } else {
   198                         printf("??");
   221                         printf("??");
   199                     }
   222                     }
   200                     if(j < sizeof(SSockAddr) + 5)
   223                     if (j < sizeof(SSockAddr) + 5)
   201                         printf("-");
   224                         printf("-");
   202                     else
   225                     else
   203                         printf("\n");
   226                         printf("\n");
   204                     }
   227                     }
   205                 }
   228                 }
   206             }
   229             }
   207         if(res == KErrNone) {
   230         if (res == KErrNone) {
   208             info = next;
   231             info = next;
   209             printf("\n");
   232             printf("\n");
   210         } else {
   233         } else {
   211             printf("\n");
   234             printf("\n");
   212         }
   235         }
   238 
   261 
   239     // Begins enumeration of routes by setting this option
   262     // Begins enumeration of routes by setting this option
   240     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
   263     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
   241 
   264 
   242     // The TSoInetRouteInfo contains information for a new route each time GetOpt returns KErrNone
   265     // The TSoInetRouteInfo contains information for a new route each time GetOpt returns KErrNone
   243     for(TInt i = 0; sock.iObj.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone ; i++)
   266     for (TInt i = 0; sock.iObj.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone ; i++)
   244     {
   267     {
   245       // Extract the destination and netmask
   268       // Extract the destination and netmask
   246       routeInfo.iDstAddr.Output(destAddr);
   269       routeInfo.iDstAddr.Output(destAddr);
   247       routeInfo.iNetMask.Output(netMask);
   270       routeInfo.iNetMask.Output(netMask);
   248       routeInfo.iGateway.Output(gateway);
   271       routeInfo.iGateway.Output(gateway);
   249       routeInfo.iIfAddr.Output(ifAddr);
   272       routeInfo.iIfAddr.Output(ifAddr);
   250 /*
   273 /*
   251       if(destAddr.Length() <= 2)
   274       if (destAddr.Length() <= 2)
   252           continue;
   275           continue;
   253 
   276 
   254       if(netMask.Find(_L("255.255.255.255")) != KErrNotFound
   277       if (netMask.Find(_L("255.255.255.255")) != KErrNotFound
   255               || netMask.Find(_L("0.0.0.0")) != KErrNotFound
   278               || netMask.Find(_L("0.0.0.0")) != KErrNotFound
   256               || netMask.Find(_L("ffff:ffff:ffff:ffff")) != KErrNotFound)
   279               || netMask.Find(_L("ffff:ffff:ffff:ffff")) != KErrNotFound)
   257           continue;
   280           continue;
   258 */
   281 */
   259       printf("Route Info #[%i]\n", i);
   282       printf("Route Info #[%i]\n", i);
   285     memset(&ifReq, 0, sizeof(struct ifreq));
   308     memset(&ifReq, 0, sizeof(struct ifreq));
   286     // set IAP name value
   309     // set IAP name value
   287     // make sure it is in UTF8
   310     // make sure it is in UTF8
   288     strcpy(ifReq.ifr_name, iapName.toUtf8().data());
   311     strcpy(ifReq.ifr_name, iapName.toUtf8().data());
   289 
   312 
   290     if(setdefaultif(&ifReq) == 0) {
   313     if (setdefaultif(&ifReq) == 0) {
   291         // OK
   314         // OK
   292         error = 0;
   315         error = 0;
   293         return true;
   316         return true;
   294     } else {
   317     } else {
   295         error = errno;
   318         error = errno;
   302     // clear structure
   325     // clear structure
   303     memset(&ifReq, 0, sizeof(struct ifreq));
   326     memset(&ifReq, 0, sizeof(struct ifreq));
   304     // set SNAP ID value
   327     // set SNAP ID value
   305     ifReq.ifr_ifru.snap_id = snapId;
   328     ifReq.ifr_ifru.snap_id = snapId;
   306 
   329 
   307     if(setdefaultif(&ifReq) == 0) {
   330     if (setdefaultif(&ifReq) == 0) {
   308         // OK
   331         // OK
   309         error = 0;
   332         error = 0;
   310         return true;
   333         return true;
   311     } else {
   334     } else {
   312         error = errno;
   335         error = errno;
   314     }
   337     }
   315 
   338 
   316 }
   339 }
   317 
   340 
   318 static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
   341 static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
   319     if(iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
   342     if (iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
   320         // no need to update
   343         // no need to update
   321     } else {
   344     } else {
   322         if(iapNameValue != QString("Easy WLAN")) {
   345         if (iapNameValue != QString("Easy WLAN")) {
   323             // new selection alway on top
   346             // new selection alway on top
   324             iapNames.removeAll(iapNameValue);
   347             iapNames.removeAll(iapNameValue);
   325             iapNames.prepend(iapNameValue);
   348             iapNames.prepend(iapNameValue);
   326             writeIapNamesSettings(settings, iapNames);
   349             writeIapNamesSettings(settings, iapNames);
   327         } else {
   350         } else {
   353 
   376 
   354     iapName.ZeroTerminate();
   377     iapName.ZeroTerminate();
   355     QString strIapName((char*)iapName.Ptr());
   378     QString strIapName((char*)iapName.Ptr());
   356 
   379 
   357     int error = 0;
   380     int error = 0;
   358     if(!qt_SetDefaultIapName(strIapName, error)) {
   381     if (!qt_SetDefaultIapName(strIapName, error)) {
   359         //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
   382         //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
   360         strIapName = QString("");
   383         strIapName = QString("");
   361     }
   384     }
   362 
   385 
   363     CleanupStack::PopAndDestroy(&connection);
   386     CleanupStack::PopAndDestroy(&connection);
   384     TBuf8<256> iapServiceType;
   407     TBuf8<256> iapServiceType;
   385 
   408 
   386     QString strIapName;
   409     QString strIapName;
   387 
   410 
   388     if (conn.EnumerateConnections(count) == KErrNone) {
   411     if (conn.EnumerateConnections(count) == KErrNone) {
   389         if(count > 0) {
   412         if (count > 0) {
   390             for (TUint i = 1; i <= count; i++) {
   413             for (TUint i = 1; i <= count; i++) {
   391                 if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
   414                 if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
   392                     RConnection tempConn;
   415                     RConnection tempConn;
   393                     CleanupClosePushL(tempConn);
   416                     CleanupClosePushL(tempConn);
   394                     tempConn.Open(serv);
   417                     tempConn.Open(serv);
   395                     if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
   418                     if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
   396                        tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
   419                        tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
   397                        tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
   420                        tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
   398                        //tempConn.Stop();
   421                        //tempConn.Stop();
   399                        iapName.ZeroTerminate();
   422                        iapName.ZeroTerminate();
   400 		               iapServiceType.ZeroTerminate();
   423                        iapServiceType.ZeroTerminate();
   401 
   424 
   402 //                        if(iapServiceType.Find(_L8("LANService")) != KErrNotFound) {
   425 //                        if (iapServiceType.Find(_L8("LANService")) != KErrNotFound) {
   403 //                            activeLanConnectionFound = ETrue;
   426 //                            activeLanConnectionFound = ETrue;
   404 //                            break;
   427 //                            break;
   405 //                        }
   428 //                        }
   406 			            strIapName = QString((char*)iapName.Ptr());
   429                         strIapName = QString((char*)iapName.Ptr());
   407                         int error = 0;
   430                         int error = 0;
   408                         if(!qt_SetDefaultIapName(strIapName, error)) {
   431                         if (!qt_SetDefaultIapName(strIapName, error)) {
   409                             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
   432                             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
   410                             strIapName = QString("");
   433                             strIapName = QString("");
   411                         }
   434                         }
   412 
   435 
   413                         CleanupStack::PopAndDestroy(&tempConn);
   436                         CleanupStack::PopAndDestroy(&tempConn);
   426     return strIapName;
   449     return strIapName;
   427 }
   450 }
   428 
   451 
   429 static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
   452 static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
   430     QString strIapName;
   453     QString strIapName;
   431     for(int index = 0; index < iapNames.size(); ++index) {
   454     for (int index = 0; index < iapNames.size(); ++index) {
   432         strIapName = iapNames.at(index);
   455         strIapName = iapNames.at(index);
   433         int error = 0;
   456         int error = 0;
   434         if(!qt_SetDefaultIapName(strIapName, error)) {
   457         if (!qt_SetDefaultIapName(strIapName, error)) {
   435             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
   458             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
   436             strIapName = QString("");
   459             strIapName = QString("");
   437         } else {
   460         } else {
   438             return strIapName;
   461             return strIapName;
   439         }
   462         }
   451 
   474 
   452     QString iapNameValue;
   475     QString iapNameValue;
   453 
   476 
   454     iapNameValue = qt_CheckForActiveConnection();
   477     iapNameValue = qt_CheckForActiveConnection();
   455 
   478 
   456     if(!iapNameValue.isEmpty()) {
   479     if (!iapNameValue.isEmpty()) {
   457         qt_SaveIapName(settings, iapNames, iapNameValue);
   480         qt_SaveIapName(settings, iapNames, iapNameValue);
   458         return;
   481         return;
   459     }
   482     }
   460 
   483 
   461     iapNameValue = qt_CheckSettingsForConnection(iapNames);
   484     iapNameValue = qt_CheckSettingsForConnection(iapNames);
   462 
   485 
   463     if(!iapNameValue.isEmpty()) {
   486     if (!iapNameValue.isEmpty()) {
   464         qt_SaveIapName(settings, iapNames, iapNameValue);
   487         qt_SaveIapName(settings, iapNames, iapNameValue);
   465         return;
   488         return;
   466     }
   489     }
   467 
   490 
   468     /*
   491     /*
   487     return 0; // IAP dialog not required for emulator
   510     return 0; // IAP dialog not required for emulator
   488 //#endif
   511 //#endif
   489 }
   512 }
   490 
   513 
   491 #endif // QSYM_IAP_UTIL_H
   514 #endif // QSYM_IAP_UTIL_H
   492 /****************************************************************************
       
   493 **
       
   494 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
   495 ** All rights reserved.
       
   496 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
   497 **
       
   498 ** This file is part of the examples of the Qt Toolkit.
       
   499 **
       
   500 ** $QT_BEGIN_LICENSE:LGPL$
       
   501 ** Commercial Usage
       
   502 ** Licensees holding valid Qt Commercial licenses may use this file in
       
   503 ** accordance with the Qt Commercial License Agreement provided with the
       
   504 ** Software or, alternatively, in accordance with the terms contained in
       
   505 ** a written agreement between you and Nokia.
       
   506 **
       
   507 ** GNU Lesser General Public License Usage
       
   508 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
   509 ** General Public License version 2.1 as published by the Free Software
       
   510 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
   511 ** packaging of this file.  Please review the following information to
       
   512 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
   513 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
   514 **
       
   515 ** In addition, as a special exception, Nokia gives you certain additional
       
   516 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
   517 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
   518 **
       
   519 ** GNU General Public License Usage
       
   520 ** Alternatively, this file may be used under the terms of the GNU
       
   521 ** General Public License version 3.0 as published by the Free Software
       
   522 ** Foundation and appearing in the file LICENSE.GPL included in the
       
   523 ** packaging of this file.  Please review the following information to
       
   524 ** ensure the GNU General Public License version 3.0 requirements will be
       
   525 ** met: http://www.gnu.org/copyleft/gpl.html.
       
   526 **
       
   527 ** If you have questions regarding the use of this file, please contact
       
   528 ** Nokia at qt-info@nokia.com.
       
   529 ** $QT_END_LICENSE$
       
   530 **
       
   531 ****************************************************************************/
       
   532 #ifndef QSYM_IAP_UTIL_H
       
   533 #define QSYM_IAP_UTIL_H
       
   534 
       
   535 // Symbian
       
   536 #include <utf.h>
       
   537 #include <es_sock.h>
       
   538 #include <in_sock.h>
       
   539 #include <es_enum.h>
       
   540 #include <in_iface.h>
       
   541 #include <commdbconnpref.h>
       
   542 #include <e32cmn.h>
       
   543 
       
   544 // OpenC
       
   545 #include <sys/socket.h>
       
   546 #include <net/if.h>
       
   547 
       
   548 //Qt
       
   549 #include <QSettings>
       
   550 #include <QStringList>
       
   551 //#include <QTextCodec>
       
   552 
       
   553 _LIT(KIapNameSetting, "IAP\\Name");             // text - mandatory
       
   554 _LIT(KIapDialogPref, "IAP\\DialogPref");        // TUnit32 - optional
       
   555 _LIT(KIapService, "IAP\\IAPService");           // TUnit32 - mandatory
       
   556 _LIT(KIapServiceType, "IAP\\IAPServiceType");   // text - mandatory
       
   557 _LIT(KIapBearer, "IAP\\IAPBearer");             // TUint32 - optional
       
   558 _LIT(KIapBearerType, "IAP\\IAPBearerType");     // text - optional
       
   559 _LIT(KIapNetwork, "IAP\\IAPNetwork");           // TUint32 - optional
       
   560 
       
   561 const QLatin1String qtOrganizationTag("Trolltech");
       
   562 const QLatin1String qtNetworkModuleTag("QtNetwork");
       
   563 const QLatin1String iapGroupTag("IAP");
       
   564 const QLatin1String iapNamesArrayTag("Names");
       
   565 const QLatin1String iapNameItemTag("Name");
       
   566 
       
   567 static QTextCodec *utf16LETextCodec = 0;
       
   568 
       
   569 void clearIapNamesSettings(QSettings &settings) {
       
   570     settings.beginGroup(qtNetworkModuleTag);
       
   571         settings.beginGroup(iapGroupTag);
       
   572            settings.remove(iapNamesArrayTag);
       
   573         settings.endGroup();
       
   574     settings.endGroup();
       
   575 }
       
   576 
       
   577 void writeIapNamesSettings(QSettings &settings, const QStringList& iapNames) {
       
   578     clearIapNamesSettings(settings);
       
   579     settings.beginGroup(qtNetworkModuleTag);
       
   580         settings.beginGroup(iapGroupTag);
       
   581             settings.beginWriteArray(iapNamesArrayTag);
       
   582             for (int index = 0; index < iapNames.size(); ++index) {
       
   583                 settings.setArrayIndex(index);
       
   584                 settings.setValue(iapNameItemTag, iapNames.at(index));
       
   585             }
       
   586             settings.endArray();
       
   587         settings.endGroup();
       
   588     settings.endGroup();
       
   589 }
       
   590 
       
   591 void readIapNamesSettings(QSettings &settings, QStringList& iapNames) {
       
   592     settings.beginGroup(qtNetworkModuleTag);
       
   593         settings.beginGroup(iapGroupTag);
       
   594             int last = settings.beginReadArray(iapNamesArrayTag);
       
   595             for (int index = 0; index < last; ++index) {
       
   596                 settings.setArrayIndex(index);
       
   597                 iapNames.append(settings.value(iapNameItemTag).toString());
       
   598             }
       
   599             settings.endArray();
       
   600         settings.endGroup();
       
   601     settings.endGroup();
       
   602 }
       
   603 
       
   604 static QString qt_TNameToQString(TName data) {
       
   605     if(utf16LETextCodec == 0)
       
   606         utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
       
   607 
       
   608     QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
       
   609     return utf16LETextCodec->toUnicode(tmpByteArray);
       
   610 }
       
   611 
       
   612 static QString qt_InterfaceInfoL()
       
   613 {
       
   614     QString output;
       
   615 
       
   616     TBuf8<512> buffer;
       
   617     TBuf<128> t;
       
   618     TAutoClose<RSocketServ> ss;
       
   619     User::LeaveIfError(ss.iObj.Connect());
       
   620     ss.PushL();
       
   621 
       
   622     TAutoClose<RSocket> sock;
       
   623     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   624     sock.PushL();
       
   625 
       
   626     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
       
   627 
       
   628     TProtocolDesc in;
       
   629     User::LeaveIfError(sock.iObj.Info(in));
       
   630     printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
       
   631 
       
   632     TPckgBuf<TSoInetInterfaceInfo> info, next;
       
   633 
       
   634     TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
       
   635     if(res!=KErrNone)
       
   636         User::Leave(res);
       
   637     TInt count = 0;
       
   638     while(res==KErrNone) {
       
   639         res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
       
   640 
       
   641         if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
       
   642             printf("Interface %d\n", count++);
       
   643 
       
   644             printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
       
   645             printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
       
   646 
       
   647             printf("State ");
       
   648             switch (info().iState)
       
   649             {
       
   650                 case EIfPending:
       
   651                     printf("pending\n");
       
   652                     break;
       
   653                 case EIfUp:
       
   654                     printf("up\n");
       
   655                     break;
       
   656                 case EIfBusy:
       
   657                     printf("busy\n");
       
   658                     break;
       
   659                 default:
       
   660                     printf("down\n");
       
   661                     break;
       
   662             }
       
   663 
       
   664             printf("Mtu %d\n", info().iMtu);
       
   665             printf("Speed Metric %d\n", info().iSpeedMetric);
       
   666 
       
   667             printf("Features:");
       
   668             info().iFeatures & KIfIsLoopback         ? printf(" loopback") : printf("");
       
   669             info().iFeatures & KIfIsDialup           ? printf(" dialup") : printf("");
       
   670             info().iFeatures & KIfIsPointToPoint     ? printf(" pointtopoint") : printf("");
       
   671             info().iFeatures & KIfCanBroadcast       ? printf(" canbroadcast") : printf("");
       
   672             info().iFeatures & KIfCanMulticast       ? printf(" canmulticast") : printf("");
       
   673             info().iFeatures & KIfCanSetMTU          ? printf(" cansetmtu") : printf("");
       
   674             info().iFeatures & KIfHasHardwareAddr    ? printf(" hardwareaddr") : printf("");
       
   675             info().iFeatures & KIfCanSetHardwareAddr ? printf(" cansethardwareaddr") : printf("");
       
   676             printf("\n");
       
   677 
       
   678             TName address;
       
   679             info().iAddress.Output(address);
       
   680             printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   681 
       
   682             if(info().iAddress.IsLinkLocal()) {
       
   683                 printf("  -link local\n");
       
   684             } else if(info().iAddress.IsSiteLocal()) {
       
   685                 printf("  -site local\n");
       
   686             } else {
       
   687                 printf("  -global\n");
       
   688             }
       
   689 
       
   690             info().iNetMask.Output(address);
       
   691             printf("Netmask %s\n", qt_TNameToQString(address).toLatin1().data());
       
   692 
       
   693             info().iBrdAddr.Output(address);
       
   694             printf("Broadcast address %s\n", qt_TNameToQString(address).toLatin1().data());
       
   695 
       
   696             info().iDefGate.Output(address);
       
   697             printf("Gatew: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   698 
       
   699             info().iNameSer1.Output(address);
       
   700             printf("DNS 1: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   701 
       
   702             info().iNameSer2.Output(address);
       
   703             printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   704 
       
   705             if (info().iHwAddr.Family() != KAFUnspec) {
       
   706                 printf("Hardware address ");
       
   707                 TUint j;
       
   708                 for(j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
       
   709                     if(j < (TUint)info().iHwAddr.Length()) {
       
   710                         printf("%02X", info().iHwAddr[j]);
       
   711                     } else {
       
   712                         printf("??");
       
   713                     }
       
   714                     if(j < sizeof(SSockAddr) + 5)
       
   715                         printf("-");
       
   716                     else
       
   717                         printf("\n");
       
   718                     }
       
   719                 }
       
   720             }
       
   721         if(res == KErrNone) {
       
   722             info = next;
       
   723             printf("\n");
       
   724         } else {
       
   725             printf("\n");
       
   726         }
       
   727     }
       
   728 
       
   729     sock.Pop();
       
   730     ss.Pop();
       
   731 
       
   732     return output;
       
   733 }
       
   734 
       
   735 static QString qt_RouteInfoL() {
       
   736     QString output;
       
   737     TAutoClose<RSocketServ> ss;
       
   738     User::LeaveIfError(ss.iObj.Connect());
       
   739     ss.PushL();
       
   740 
       
   741     TAutoClose<RSocket> sock;
       
   742     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   743     sock.PushL();
       
   744 
       
   745     TSoInetRouteInfo routeInfo;
       
   746     TPckg<TSoInetRouteInfo> routeInfoPkg(routeInfo);
       
   747 
       
   748     TName destAddr;
       
   749     TName netMask;
       
   750     TName gateway;
       
   751     TName ifAddr;
       
   752 
       
   753     // Begins enumeration of routes by setting this option
       
   754     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
       
   755 
       
   756     // The TSoInetRouteInfo contains information for a new route each time GetOpt returns KErrNone
       
   757     for(TInt i = 0; sock.iObj.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone ; i++)
       
   758     {
       
   759       // Extract the destination and netmask
       
   760       routeInfo.iDstAddr.Output(destAddr);
       
   761       routeInfo.iNetMask.Output(netMask);
       
   762       routeInfo.iGateway.Output(gateway);
       
   763       routeInfo.iIfAddr.Output(ifAddr);
       
   764 /*
       
   765       if(destAddr.Length() <= 2)
       
   766           continue;
       
   767 
       
   768       if(netMask.Find(_L("255.255.255.255")) != KErrNotFound
       
   769               || netMask.Find(_L("0.0.0.0")) != KErrNotFound
       
   770               || netMask.Find(_L("ffff:ffff:ffff:ffff")) != KErrNotFound)
       
   771           continue;
       
   772 */
       
   773       printf("Route Info #[%i]\n", i);
       
   774       printf("DstAddr %s\n", qt_TNameToQString(destAddr).toLatin1().data());
       
   775       printf("NetMask %s\n", qt_TNameToQString(netMask).toLatin1().data());
       
   776       printf("Gateway %s\n", qt_TNameToQString(gateway).toLatin1().data());
       
   777       printf("IfAddr %s\n", qt_TNameToQString(ifAddr).toLatin1().data());
       
   778       printf("\n");
       
   779     }
       
   780 
       
   781     sock.Pop();
       
   782     ss.Pop();
       
   783 
       
   784     return output;
       
   785 }
       
   786 
       
   787 QString qt_TDesC2QStringL(const TDesC& aDescriptor)
       
   788 {
       
   789 #ifdef QT_NO_UNICODE
       
   790     return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
       
   791 #else
       
   792     return QString::fromUtf16(aDescriptor.Ptr(), aDescriptor.Length());
       
   793 #endif
       
   794 }
       
   795 
       
   796 static bool qt_SetDefaultIapName(const QString &iapName, int &error) {
       
   797     struct ifreq ifReq;
       
   798     // clear structure
       
   799     memset(&ifReq, 0, sizeof(struct ifreq));
       
   800     // set IAP name value
       
   801     // make sure it is in UTF8
       
   802     strcpy(ifReq.ifr_name, iapName.toUtf8().data());
       
   803 
       
   804     if(setdefaultif(&ifReq) == 0) {
       
   805         // OK
       
   806         error = 0;
       
   807         return true;
       
   808     } else {
       
   809         error = errno;
       
   810         return false;
       
   811     }
       
   812 
       
   813 }
       
   814 static bool qt_SetDefaultSnapId(const int snapId, int &error) {
       
   815     struct ifreq ifReq;
       
   816     // clear structure
       
   817     memset(&ifReq, 0, sizeof(struct ifreq));
       
   818     // set SNAP ID value
       
   819     ifReq.ifr_ifru.snap_id = snapId;
       
   820 
       
   821     if(setdefaultif(&ifReq) == 0) {
       
   822         // OK
       
   823         error = 0;
       
   824         return true;
       
   825     } else {
       
   826         error = errno;
       
   827         return false;
       
   828     }
       
   829 
       
   830 }
       
   831 
       
   832 static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
       
   833     if(iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
       
   834         // no need to update
       
   835     } else {
       
   836         if(iapNameValue != QString("Easy WLAN")) {
       
   837             // new selection alway on top
       
   838             iapNames.removeAll(iapNameValue);
       
   839             iapNames.prepend(iapNameValue);
       
   840             writeIapNamesSettings(settings, iapNames);
       
   841         } else {
       
   842             // Unbeliveable ... if IAP dodn't exist before
       
   843             // no matter what you choose from IAP selection list
       
   844             // you will get "Easy WLAN" as IAP name value
       
   845 
       
   846             // somehow commsdb is not in sync
       
   847         }
       
   848     }
       
   849 }
       
   850 
       
   851 static QString qt_OfferIapDialog() {
       
   852     TBuf8<256> iapName;
       
   853 
       
   854     RSocketServ socketServ;
       
   855     CleanupClosePushL(socketServ);
       
   856 
       
   857     RConnection connection;
       
   858     CleanupClosePushL(connection);
       
   859 
       
   860     socketServ.Connect();
       
   861     connection.Open(socketServ);
       
   862     connection.Start();
       
   863 
       
   864     connection.GetDesSetting(TPtrC(KIapNameSetting), iapName);
       
   865 
       
   866     //connection.Stop();
       
   867 
       
   868     iapName.ZeroTerminate();
       
   869     QString strIapName((char*)iapName.Ptr());
       
   870 
       
   871     int error = 0;
       
   872     if(!qt_SetDefaultIapName(strIapName, error)) {
       
   873         //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   874         strIapName = QString("");
       
   875     }
       
   876 
       
   877     CleanupStack::PopAndDestroy(&connection);
       
   878     CleanupStack::PopAndDestroy(&socketServ);
       
   879 
       
   880     return strIapName;
       
   881 }
       
   882 
       
   883 static QString qt_CheckForActiveConnection() {
       
   884     TUint count;
       
   885 
       
   886     RSocketServ serv;
       
   887     CleanupClosePushL(serv);
       
   888 
       
   889     RConnection conn;
       
   890     CleanupClosePushL(conn);
       
   891 
       
   892     serv.Connect();
       
   893     conn.Open(serv);
       
   894 
       
   895     TConnectionInfoBuf connInfo;
       
   896 
       
   897     TBuf8<256> iapName;
       
   898     TBuf8<256> iapServiceType;
       
   899 
       
   900     QString strIapName;
       
   901 
       
   902     if (conn.EnumerateConnections(count) == KErrNone) {
       
   903         if(count > 0) {
       
   904             for (TUint i = 1; i <= count; i++) {
       
   905                 if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
       
   906                     RConnection tempConn;
       
   907                     CleanupClosePushL(tempConn);
       
   908                     tempConn.Open(serv);
       
   909                     if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
       
   910                        tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
       
   911                        tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
       
   912                        //tempConn.Stop();
       
   913                        iapName.ZeroTerminate();
       
   914 		               iapServiceType.ZeroTerminate();
       
   915 
       
   916 //                        if(iapServiceType.Find(_L8("LANService")) != KErrNotFound) {
       
   917 //                            activeLanConnectionFound = ETrue;
       
   918 //                            break;
       
   919 //                        }
       
   920 			            strIapName = QString((char*)iapName.Ptr());
       
   921                         int error = 0;
       
   922                         if(!qt_SetDefaultIapName(strIapName, error)) {
       
   923                             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   924                             strIapName = QString("");
       
   925                         }
       
   926 
       
   927                         CleanupStack::PopAndDestroy(&tempConn);
       
   928                         break;
       
   929                     }
       
   930                 }
       
   931             }
       
   932         }
       
   933     }
       
   934 
       
   935     //conn.Stop();
       
   936 
       
   937     CleanupStack::PopAndDestroy(&conn);
       
   938     CleanupStack::PopAndDestroy(&serv);
       
   939 
       
   940     return strIapName;
       
   941 }
       
   942 
       
   943 static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
       
   944     QString strIapName;
       
   945     for(int index = 0; index < iapNames.size(); ++index) {
       
   946         strIapName = iapNames.at(index);
       
   947         int error = 0;
       
   948         if(!qt_SetDefaultIapName(strIapName, error)) {
       
   949             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   950             strIapName = QString("");
       
   951         } else {
       
   952             return strIapName;
       
   953         }
       
   954     }
       
   955     return strIapName;
       
   956 }
       
   957 
       
   958 static void qt_SetDefaultIapL()
       
   959 {
       
   960     // settings @ /c/data/.config/Trolltech.com
       
   961     QSettings settings(QSettings::UserScope, qtOrganizationTag);
       
   962     // populate iap name list
       
   963     QStringList iapNames;
       
   964     readIapNamesSettings(settings, iapNames);
       
   965 
       
   966     QString iapNameValue;
       
   967 
       
   968     iapNameValue = qt_CheckForActiveConnection();
       
   969 
       
   970     if(!iapNameValue.isEmpty()) {
       
   971         qt_SaveIapName(settings, iapNames, iapNameValue);
       
   972         return;
       
   973     }
       
   974 
       
   975     iapNameValue = qt_CheckSettingsForConnection(iapNames);
       
   976 
       
   977     if(!iapNameValue.isEmpty()) {
       
   978         qt_SaveIapName(settings, iapNames, iapNameValue);
       
   979         return;
       
   980     }
       
   981 
       
   982     /*
       
   983      * no active LAN connections yet
       
   984      * no IAP in settings
       
   985      * offer IAP dialog to user
       
   986      */
       
   987     iapNameValue = qt_OfferIapDialog();
       
   988     qt_SaveIapName(settings, iapNames, iapNameValue);
       
   989     return;
       
   990 
       
   991 }
       
   992 
       
   993 static int qt_SetDefaultIap()
       
   994 {
       
   995 //#ifndef __WINS__
       
   996     TRAPD(err1, qt_SetDefaultIapL());
       
   997 //    TRAPD(err2, qt_InterfaceInfoL());
       
   998 //    TRAPD(err3, qt_RouteInfoL());
       
   999     return err1;
       
  1000 //#else
       
  1001     return 0; // IAP dialog not required for emulator
       
  1002 //#endif
       
  1003 }
       
  1004 
       
  1005 #endif // QSYM_IAP_UTIL_H