diff options
author | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-10 19:21:59 -0200 |
---|---|---|
committer | Luiz Agostini <luiz.agostini@openbossa.org> | 2009-11-10 22:00:56 -0200 |
commit | 2d21ac1cbaf1c1d8be6e47bce8011e4911854ba5 (patch) | |
tree | 48aa935ab92c71d41841a6a6fbb90a29445f6a6b | |
parent | 7f321830e93f4830b02e34d70bd9fa7d41215d12 (diff) |
Weather: symbian network utilities.
Signed-off-by: Luiz Agostini <luiz.agostini@openbossa.org>
-rw-r--r-- | weather/main.cpp | 25 | ||||
-rw-r--r-- | weather/symbiannetwork.h (renamed from weather/sym_iap_util.h) | 153 |
2 files changed, 103 insertions, 75 deletions
diff --git a/weather/main.cpp b/weather/main.cpp index e90a959..8f63a7a 100644 --- a/weather/main.cpp +++ b/weather/main.cpp @@ -45,7 +45,7 @@ #include "networkforecastsource.h" #if defined (Q_OS_SYMBIAN) -#include "sym_iap_util.h" +#include "symbiannetwork.h" #endif int main(int argc, char **argv) @@ -57,18 +57,21 @@ int main(int argc, char **argv) QCoreApplication::setOrganizationDomain("openbossa.org"); QCoreApplication::setApplicationName("weather"); - MainView mainView; -// mainView.setFixedSize(windowSize); - - ForecastProvider::setForecastSource(new NetworkForecastSource()); - #ifdef Q_OS_SYMBIAN - mainView.showFullScreen(); + const bool connected = connect(); Settings::fixedPortraitOrientation(); - qt_SetDefaultIap(); #else - mainView.show(); + const bool connected = true; #endif - - return app.exec(); + + if (connected) { + MainView mainView; + ForecastProvider::setForecastSource(new NetworkForecastSource()); +#ifdef Q_OS_SYMBIAN + mainView.showFullScreen(); +#else + mainView.show(); +#endif + return app.exec(); + } } diff --git a/weather/sym_iap_util.h b/weather/symbiannetwork.h index 14df5af..52cf45c 100644 --- a/weather/sym_iap_util.h +++ b/weather/symbiannetwork.h @@ -38,14 +38,35 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#ifndef QSYM_IAP_UTIL_H -#define QSYM_IAP_UTIL_H +#ifndef SYMBIAN_NETWORK_H +#define SYMBIAN_NETWORK_H #include <es_sock.h> #include <sys/socket.h> #include <net/if.h> #include <rconnmon.h> + +#include <stdarg.h> +#include <e32std.h> + +/* +void writeDebugMsg(const char* msg, ...) +{ + static char buffer[1024]; + + va_list args; + va_start( args, msg); + vsprintf(buffer, msg, args); + va_end( args); + + FILE *logfile; + logfile = fopen("C:\\Data\\weather.log", "a"); + fprintf(logfile, "%s\n", buffer); + fclose(logfile); +} +*/ + QString qt_TDesC2QStringL(const TDesC& aDescriptor) { #ifdef QT_NO_UNICODE @@ -55,77 +76,81 @@ QString qt_TDesC2QStringL(const TDesC& aDescriptor) #endif } -static void qt_SetDefaultIapL() +static bool useCurrentConnectionL(RConnectionMonitor &monitor) { TUint count; TRequestStatus status; - TUint ids[15]; - - RSocketServ serv; - CleanupClosePushL(serv); + monitor.GetConnectionCount(count, status); + User::WaitForRequest(status); + User::LeaveIfError(status.Int()); + if (count <= 0) + return false; + + TUint connId; + TUint subConnCount; + User::LeaveIfError(monitor.GetConnectionInfo(1, connId, subConnCount)); + TBuf<50> iapName; + monitor.GetStringAttribute(connId, 0, KIAPName, iapName, status); + User::WaitForRequest(status); + User::LeaveIfError(status.Int()); - RConnection conn; - CleanupClosePushL(conn); + QString strIapName = qt_TDesC2QStringL(iapName); + struct ifreq ifReq; + strcpy(ifReq.ifr_name, strIapName.toLatin1().data()); + User::LeaveIfError(setdefaultif(&ifReq)); + return true; +} +static bool useCurrentConnectionL() +{ RConnectionMonitor monitor; - CleanupClosePushL(monitor); + if (monitor.ConnectL() != KErrNone) + return false; + CleanupClosePushL(monitor); + bool result = useCurrentConnectionL(monitor); + CleanupStack::PopAndDestroy(); + return result; +} - monitor.ConnectL(); - monitor.GetConnectionCount(count, status); - User::WaitForRequest(status); - if(status.Int() != KErrNone) { - User::Leave(status.Int()); - } - - TUint numSubConnections; - - if(count > 0) { - for (TInt i = 1; i <= count; i++) { - User::LeaveIfError(monitor.GetConnectionInfo(i, ids[i-1], numSubConnections)); - } - /* - * get IAP value for first active connection - */ - TBuf< 50 > iapName; - monitor.GetStringAttribute(ids[0], 0, KIAPName, iapName, status); - User::WaitForRequest(status); - if (status.Int() != KErrNone) { - User::Leave(status.Int()); - } else { - QString strIapName = qt_TDesC2QStringL(iapName); - struct ifreq ifReq; - strcpy(ifReq.ifr_name, strIapName.toLatin1().data()); - User::LeaveIfError(setdefaultif(&ifReq)); - } - } else { - /* - * no active connections yet - * use IAP dialog to select one - */ - User::LeaveIfError(serv.Connect()); - User::LeaveIfError(conn.Open(serv)); - User::LeaveIfError(conn.Start()); - - _LIT(KIapNameSetting, "IAP\\Name"); - TBuf8<50> iap8Name; - User::LeaveIfError(conn.GetDesSetting(TPtrC(KIapNameSetting), iap8Name)); - iap8Name.ZeroTerminate(); - - conn.Stop(); - - struct ifreq ifReq; - strcpy(ifReq.ifr_name, (char*)iap8Name.Ptr()); - User::LeaveIfError(setdefaultif(&ifReq)); - } - CleanupStack::PopAndDestroy(&monitor); - CleanupStack::PopAndDestroy(&conn); - CleanupStack::PopAndDestroy(&serv); +static bool useCurrentConnection() +{ + bool result = false; + TRAPD(error, result = useCurrentConnectionL()); + return error == KErrNone && result; +} + +static void createNewConnectionL() +{ + RSocketServ server; + User::LeaveIfError(server.Connect()); + CleanupClosePushL(server); + + RConnection connection; + User::LeaveIfError(connection.Open(server)); + CleanupClosePushL(connection); + User::LeaveIfError(connection.Start()); + + _LIT(KIapName, "IAP\\Name"); + TBuf8<50> iapName; + User::LeaveIfError(connection.GetDesSetting(TPtrC(KIapName), iapName)); + connection.Stop(); + CleanupStack::PopAndDestroy(2); + + iapName.ZeroTerminate(); + struct ifreq ifReq; + strcpy(ifReq.ifr_name, (char*)iapName.Ptr()); + User::LeaveIfError(setdefaultif(&ifReq)); +} + +static bool createNewConnection() +{ + TRAPD(error, createNewConnectionL()); + return error == KErrNone; } -static int qt_SetDefaultIap() +static bool connect() { - TRAPD(err, qt_SetDefaultIapL()); - return err; + return useCurrentConnection() || createNewConnection(); } -#endif // QSYM_IAP_UTIL_H +#endif |