ginebra2/sym_iap_util.h
changeset 0 1450b09d0cfd
child 3 0954f5dd2cd0
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef QSYM_IAP_UTIL_H
       
    19 #define QSYM_IAP_UTIL_H
       
    20 
       
    21 // Symbian
       
    22 #include <utf.h>
       
    23 #include <es_sock.h>
       
    24 #include <in_sock.h>
       
    25 #include <es_enum.h>
       
    26 #include <in_iface.h>
       
    27 #include <commdbconnpref.h>
       
    28 #include <e32cmn.h>
       
    29 
       
    30 // OpenC
       
    31 #include <sys/socket.h>
       
    32 #include <net/if.h>
       
    33 
       
    34 //Qt
       
    35 #include <QSettings>
       
    36 #include <QStringList>
       
    37 //#include <QTextCodec>
       
    38 
       
    39 _LIT(KIapNameSetting, "IAP\\Name");             // text - mandatory
       
    40 _LIT(KIapDialogPref, "IAP\\DialogPref");        // TUnit32 - optional
       
    41 _LIT(KIapService, "IAP\\IAPService");           // TUnit32 - mandatory
       
    42 _LIT(KIapServiceType, "IAP\\IAPServiceType");   // text - mandatory
       
    43 _LIT(KIapBearer, "IAP\\IAPBearer");             // TUint32 - optional
       
    44 _LIT(KIapBearerType, "IAP\\IAPBearerType");     // text - optional
       
    45 _LIT(KIapNetwork, "IAP\\IAPNetwork");           // TUint32 - optional
       
    46 
       
    47 const QLatin1String qtOrganizationTag("Trolltech");
       
    48 const QLatin1String qtNetworkModuleTag("QtNetwork");
       
    49 const QLatin1String iapGroupTag("IAP");
       
    50 const QLatin1String iapNamesArrayTag("Names");
       
    51 const QLatin1String iapNameItemTag("Name");
       
    52 
       
    53 static QTextCodec *utf16LETextCodec = 0;
       
    54 
       
    55 void clearIapNamesSettings(QSettings &settings) {
       
    56     settings.beginGroup(qtNetworkModuleTag);
       
    57         settings.beginGroup(iapGroupTag);
       
    58            settings.remove(iapNamesArrayTag);
       
    59         settings.endGroup();
       
    60     settings.endGroup();
       
    61 }
       
    62 
       
    63 void writeIapNamesSettings(QSettings &settings, const QStringList& iapNames) {
       
    64     clearIapNamesSettings(settings);
       
    65     settings.beginGroup(qtNetworkModuleTag);
       
    66         settings.beginGroup(iapGroupTag);
       
    67             settings.beginWriteArray(iapNamesArrayTag);
       
    68             for (int index = 0; index < iapNames.size(); ++index) {
       
    69                 settings.setArrayIndex(index);
       
    70                 settings.setValue(iapNameItemTag, iapNames.at(index));
       
    71             }
       
    72             settings.endArray();
       
    73         settings.endGroup();
       
    74     settings.endGroup();
       
    75 }
       
    76 
       
    77 void readIapNamesSettings(QSettings &settings, QStringList& iapNames) {
       
    78     settings.beginGroup(qtNetworkModuleTag);
       
    79         settings.beginGroup(iapGroupTag);
       
    80             int last = settings.beginReadArray(iapNamesArrayTag);
       
    81             for (int index = 0; index < last; ++index) {
       
    82                 settings.setArrayIndex(index);
       
    83                 iapNames.append(settings.value(iapNameItemTag).toString());
       
    84             }
       
    85             settings.endArray();
       
    86         settings.endGroup();
       
    87     settings.endGroup();
       
    88 }
       
    89 
       
    90 static QString qt_TNameToQString(TName data) {
       
    91     if(utf16LETextCodec == 0)
       
    92         utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
       
    93 
       
    94     QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
       
    95     return utf16LETextCodec->toUnicode(tmpByteArray);
       
    96 }
       
    97 
       
    98 static QString qt_InterfaceInfoL()
       
    99 {
       
   100     QString output;
       
   101 
       
   102     TBuf8<512> buffer;
       
   103     TBuf<128> t;
       
   104     TAutoClose<RSocketServ> ss;
       
   105     User::LeaveIfError(ss.iObj.Connect());
       
   106     ss.PushL();
       
   107 
       
   108     TAutoClose<RSocket> sock;
       
   109     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   110     sock.PushL();
       
   111 
       
   112     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
       
   113 
       
   114     TProtocolDesc in;
       
   115     User::LeaveIfError(sock.iObj.Info(in));
       
   116     printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
       
   117 
       
   118     TPckgBuf<TSoInetInterfaceInfo> info, next;
       
   119 
       
   120     TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
       
   121     if(res!=KErrNone)
       
   122         User::Leave(res);
       
   123     TInt count = 0;
       
   124     while(res==KErrNone) {
       
   125         res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
       
   126 
       
   127         if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
       
   128             printf("Interface %d\n", count++);
       
   129 
       
   130             printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
       
   131             printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
       
   132 
       
   133             printf("State ");
       
   134             switch (info().iState)
       
   135             {
       
   136                 case EIfPending:
       
   137                     printf("pending\n");
       
   138                     break;
       
   139                 case EIfUp:
       
   140                     printf("up\n");
       
   141                     break;
       
   142                 case EIfBusy:
       
   143                     printf("busy\n");
       
   144                     break;
       
   145                 default:
       
   146                     printf("down\n");
       
   147                     break;
       
   148             }
       
   149 
       
   150             printf("Mtu %d\n", info().iMtu);
       
   151             printf("Speed Metric %d\n", info().iSpeedMetric);
       
   152 
       
   153             printf("Features:");
       
   154             info().iFeatures & KIfIsLoopback         ? printf(" loopback") : printf("");
       
   155             info().iFeatures & KIfIsDialup           ? printf(" dialup") : printf("");
       
   156             info().iFeatures & KIfIsPointToPoint     ? printf(" pointtopoint") : printf("");
       
   157             info().iFeatures & KIfCanBroadcast       ? printf(" canbroadcast") : printf("");
       
   158             info().iFeatures & KIfCanMulticast       ? printf(" canmulticast") : printf("");
       
   159             info().iFeatures & KIfCanSetMTU          ? printf(" cansetmtu") : printf("");
       
   160             info().iFeatures & KIfHasHardwareAddr    ? printf(" hardwareaddr") : printf("");
       
   161             info().iFeatures & KIfCanSetHardwareAddr ? printf(" cansethardwareaddr") : printf("");
       
   162             printf("\n");
       
   163 
       
   164             TName address;
       
   165             info().iAddress.Output(address);
       
   166             printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   167 
       
   168             if(info().iAddress.IsLinkLocal()) {
       
   169                 printf("  -link local\n");
       
   170             } else if(info().iAddress.IsSiteLocal()) {
       
   171                 printf("  -site local\n");
       
   172             } else {
       
   173                 printf("  -global\n");
       
   174             }
       
   175 
       
   176             info().iNetMask.Output(address);
       
   177             printf("Netmask %s\n", qt_TNameToQString(address).toLatin1().data());
       
   178 
       
   179             info().iBrdAddr.Output(address);
       
   180             printf("Broadcast address %s\n", qt_TNameToQString(address).toLatin1().data());
       
   181 
       
   182             info().iDefGate.Output(address);
       
   183             printf("Gatew: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   184 
       
   185             info().iNameSer1.Output(address);
       
   186             printf("DNS 1: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   187 
       
   188             info().iNameSer2.Output(address);
       
   189             printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   190 
       
   191             if (info().iHwAddr.Family() != KAFUnspec) {
       
   192                 printf("Hardware address ");
       
   193                 TUint j;
       
   194                 for(j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
       
   195                     if(j < (TUint)info().iHwAddr.Length()) {
       
   196                         printf("%02X", info().iHwAddr[j]);
       
   197                     } else {
       
   198                         printf("??");
       
   199                     }
       
   200                     if(j < sizeof(SSockAddr) + 5)
       
   201                         printf("-");
       
   202                     else
       
   203                         printf("\n");
       
   204                     }
       
   205                 }
       
   206             }
       
   207         if(res == KErrNone) {
       
   208             info = next;
       
   209             printf("\n");
       
   210         } else {
       
   211             printf("\n");
       
   212         }
       
   213     }
       
   214 
       
   215     sock.Pop();
       
   216     ss.Pop();
       
   217 
       
   218     return output;
       
   219 }
       
   220 
       
   221 static QString qt_RouteInfoL() {
       
   222     QString output;
       
   223     TAutoClose<RSocketServ> ss;
       
   224     User::LeaveIfError(ss.iObj.Connect());
       
   225     ss.PushL();
       
   226 
       
   227     TAutoClose<RSocket> sock;
       
   228     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   229     sock.PushL();
       
   230 
       
   231     TSoInetRouteInfo routeInfo;
       
   232     TPckg<TSoInetRouteInfo> routeInfoPkg(routeInfo);
       
   233 
       
   234     TName destAddr;
       
   235     TName netMask;
       
   236     TName gateway;
       
   237     TName ifAddr;
       
   238 
       
   239     // Begins enumeration of routes by setting this option
       
   240     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
       
   241 
       
   242     // 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++)
       
   244     {
       
   245       // Extract the destination and netmask
       
   246       routeInfo.iDstAddr.Output(destAddr);
       
   247       routeInfo.iNetMask.Output(netMask);
       
   248       routeInfo.iGateway.Output(gateway);
       
   249       routeInfo.iIfAddr.Output(ifAddr);
       
   250 /*
       
   251       if(destAddr.Length() <= 2)
       
   252           continue;
       
   253 
       
   254       if(netMask.Find(_L("255.255.255.255")) != KErrNotFound
       
   255               || netMask.Find(_L("0.0.0.0")) != KErrNotFound
       
   256               || netMask.Find(_L("ffff:ffff:ffff:ffff")) != KErrNotFound)
       
   257           continue;
       
   258 */
       
   259       printf("Route Info #[%i]\n", i);
       
   260       printf("DstAddr %s\n", qt_TNameToQString(destAddr).toLatin1().data());
       
   261       printf("NetMask %s\n", qt_TNameToQString(netMask).toLatin1().data());
       
   262       printf("Gateway %s\n", qt_TNameToQString(gateway).toLatin1().data());
       
   263       printf("IfAddr %s\n", qt_TNameToQString(ifAddr).toLatin1().data());
       
   264       printf("\n");
       
   265     }
       
   266 
       
   267     sock.Pop();
       
   268     ss.Pop();
       
   269 
       
   270     return output;
       
   271 }
       
   272 
       
   273 QString qt_TDesC2QStringL(const TDesC& aDescriptor)
       
   274 {
       
   275 #ifdef QT_NO_UNICODE
       
   276     return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
       
   277 #else
       
   278     return QString::fromUtf16(aDescriptor.Ptr(), aDescriptor.Length());
       
   279 #endif
       
   280 }
       
   281 
       
   282 static bool qt_SetDefaultIapName(const QString &iapName, int &error) {
       
   283     struct ifreq ifReq;
       
   284     // clear structure
       
   285     memset(&ifReq, 0, sizeof(struct ifreq));
       
   286     // set IAP name value
       
   287     // make sure it is in UTF8
       
   288     strcpy(ifReq.ifr_name, iapName.toUtf8().data());
       
   289 
       
   290     if(setdefaultif(&ifReq) == 0) {
       
   291         // OK
       
   292         error = 0;
       
   293         return true;
       
   294     } else {
       
   295         error = errno;
       
   296         return false;
       
   297     }
       
   298 
       
   299 }
       
   300 static bool qt_SetDefaultSnapId(const int snapId, int &error) {
       
   301     struct ifreq ifReq;
       
   302     // clear structure
       
   303     memset(&ifReq, 0, sizeof(struct ifreq));
       
   304     // set SNAP ID value
       
   305     ifReq.ifr_ifru.snap_id = snapId;
       
   306 
       
   307     if(setdefaultif(&ifReq) == 0) {
       
   308         // OK
       
   309         error = 0;
       
   310         return true;
       
   311     } else {
       
   312         error = errno;
       
   313         return false;
       
   314     }
       
   315 
       
   316 }
       
   317 
       
   318 static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
       
   319     if(iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
       
   320         // no need to update
       
   321     } else {
       
   322         if(iapNameValue != QString("Easy WLAN")) {
       
   323             // new selection alway on top
       
   324             iapNames.removeAll(iapNameValue);
       
   325             iapNames.prepend(iapNameValue);
       
   326             writeIapNamesSettings(settings, iapNames);
       
   327         } else {
       
   328             // Unbeliveable ... if IAP dodn't exist before
       
   329             // no matter what you choose from IAP selection list
       
   330             // you will get "Easy WLAN" as IAP name value
       
   331 
       
   332             // somehow commsdb is not in sync
       
   333         }
       
   334     }
       
   335 }
       
   336 
       
   337 static QString qt_OfferIapDialog() {
       
   338     TBuf8<256> iapName;
       
   339 
       
   340     RSocketServ socketServ;
       
   341     CleanupClosePushL(socketServ);
       
   342 
       
   343     RConnection connection;
       
   344     CleanupClosePushL(connection);
       
   345 
       
   346     socketServ.Connect();
       
   347     connection.Open(socketServ);
       
   348     connection.Start();
       
   349 
       
   350     connection.GetDesSetting(TPtrC(KIapNameSetting), iapName);
       
   351 
       
   352     //connection.Stop();
       
   353 
       
   354     iapName.ZeroTerminate();
       
   355     QString strIapName((char*)iapName.Ptr());
       
   356 
       
   357     int error = 0;
       
   358     if(!qt_SetDefaultIapName(strIapName, error)) {
       
   359         //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   360         strIapName = QString("");
       
   361     }
       
   362 
       
   363     CleanupStack::PopAndDestroy(&connection);
       
   364     CleanupStack::PopAndDestroy(&socketServ);
       
   365 
       
   366     return strIapName;
       
   367 }
       
   368 
       
   369 static QString qt_CheckForActiveConnection() {
       
   370     TUint count;
       
   371 
       
   372     RSocketServ serv;
       
   373     CleanupClosePushL(serv);
       
   374 
       
   375     RConnection conn;
       
   376     CleanupClosePushL(conn);
       
   377 
       
   378     serv.Connect();
       
   379     conn.Open(serv);
       
   380 
       
   381     TConnectionInfoBuf connInfo;
       
   382 
       
   383     TBuf8<256> iapName;
       
   384     TBuf8<256> iapServiceType;
       
   385 
       
   386     QString strIapName;
       
   387 
       
   388     if (conn.EnumerateConnections(count) == KErrNone) {
       
   389         if(count > 0) {
       
   390             for (TUint i = 1; i <= count; i++) {
       
   391                 if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
       
   392                     RConnection tempConn;
       
   393                     CleanupClosePushL(tempConn);
       
   394                     tempConn.Open(serv);
       
   395                     if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
       
   396                        tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
       
   397                        tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
       
   398                        //tempConn.Stop();
       
   399                        iapName.ZeroTerminate();
       
   400 		               iapServiceType.ZeroTerminate();
       
   401 
       
   402 //                        if(iapServiceType.Find(_L8("LANService")) != KErrNotFound) {
       
   403 //                            activeLanConnectionFound = ETrue;
       
   404 //                            break;
       
   405 //                        }
       
   406 			            strIapName = QString((char*)iapName.Ptr());
       
   407                         int error = 0;
       
   408                         if(!qt_SetDefaultIapName(strIapName, error)) {
       
   409                             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   410                             strIapName = QString("");
       
   411                         }
       
   412 
       
   413                         CleanupStack::PopAndDestroy(&tempConn);
       
   414                         break;
       
   415                     }
       
   416                 }
       
   417             }
       
   418         }
       
   419     }
       
   420 
       
   421     //conn.Stop();
       
   422 
       
   423     CleanupStack::PopAndDestroy(&conn);
       
   424     CleanupStack::PopAndDestroy(&serv);
       
   425 
       
   426     return strIapName;
       
   427 }
       
   428 
       
   429 static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
       
   430     QString strIapName;
       
   431     for(int index = 0; index < iapNames.size(); ++index) {
       
   432         strIapName = iapNames.at(index);
       
   433         int error = 0;
       
   434         if(!qt_SetDefaultIapName(strIapName, error)) {
       
   435             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   436             strIapName = QString("");
       
   437         } else {
       
   438             return strIapName;
       
   439         }
       
   440     }
       
   441     return strIapName;
       
   442 }
       
   443 
       
   444 static void qt_SetDefaultIapL()
       
   445 {
       
   446     // settings @ /c/data/.config/Trolltech.com
       
   447     QSettings settings(QSettings::UserScope, qtOrganizationTag);
       
   448     // populate iap name list
       
   449     QStringList iapNames;
       
   450     readIapNamesSettings(settings, iapNames);
       
   451 
       
   452     QString iapNameValue;
       
   453 
       
   454     iapNameValue = qt_CheckForActiveConnection();
       
   455 
       
   456     if(!iapNameValue.isEmpty()) {
       
   457         qt_SaveIapName(settings, iapNames, iapNameValue);
       
   458         return;
       
   459     }
       
   460 
       
   461     iapNameValue = qt_CheckSettingsForConnection(iapNames);
       
   462 
       
   463     if(!iapNameValue.isEmpty()) {
       
   464         qt_SaveIapName(settings, iapNames, iapNameValue);
       
   465         return;
       
   466     }
       
   467 
       
   468     /*
       
   469      * no active LAN connections yet
       
   470      * no IAP in settings
       
   471      * offer IAP dialog to user
       
   472      */
       
   473     iapNameValue = qt_OfferIapDialog();
       
   474     qt_SaveIapName(settings, iapNames, iapNameValue);
       
   475     return;
       
   476 
       
   477 }
       
   478 
       
   479 static int qt_SetDefaultIap()
       
   480 {
       
   481 //#ifndef __WINS__
       
   482     TRAPD(err1, qt_SetDefaultIapL());
       
   483 //    TRAPD(err2, qt_InterfaceInfoL());
       
   484 //    TRAPD(err3, qt_RouteInfoL());
       
   485     return err1;
       
   486 //#else
       
   487     return 0; // IAP dialog not required for emulator
       
   488 //#endif
       
   489 }
       
   490 
       
   491 #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