|
1 /* |
|
2 * Copyright (c) 2008, 2009 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 the License "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 |
|
19 #include "WidgetUiNetworkListener.h" |
|
20 |
|
21 CWidgetUiNetworkListener* CWidgetUiNetworkListener::NewL(CWidgetUiWindowManager& aWindowManager) |
|
22 { |
|
23 CWidgetUiNetworkListener* self = new (ELeave) CWidgetUiNetworkListener(aWindowManager); |
|
24 return self; |
|
25 } |
|
26 |
|
27 CWidgetUiNetworkListener::CWidgetUiNetworkListener(CWidgetUiWindowManager& aWindowManager) |
|
28 : CActive(0), iWindowManager(aWindowManager) |
|
29 { |
|
30 CActiveScheduler::Add(this); |
|
31 StartListening(); |
|
32 } |
|
33 |
|
34 CWidgetUiNetworkListener::~CWidgetUiNetworkListener() |
|
35 { |
|
36 Cancel(); |
|
37 Close(); |
|
38 } |
|
39 |
|
40 TInt CWidgetUiNetworkListener::StartListening() |
|
41 { |
|
42 TInt err = KErrNone; |
|
43 // If already listening do nothing |
|
44 if(!IsActive()) |
|
45 { |
|
46 if((err = Connect()) == KErrNone) |
|
47 { |
|
48 // check if there is a current active connection |
|
49 if ( CheckActiveNetworkConnection() ) |
|
50 { |
|
51 // notify widgets of an existing active network connection |
|
52 iWindowManager.NotifyConnecionChange( ETrue ); |
|
53 } |
|
54 iConn.AllInterfaceNotification( iNote, iStatus ); |
|
55 SetActive(); |
|
56 } |
|
57 } |
|
58 return err; |
|
59 } |
|
60 |
|
61 TBool CWidgetUiNetworkListener::CheckActiveNetworkConnection() |
|
62 { |
|
63 TUint iNumConnections, netConnections; |
|
64 TPckgBuf<TConnectionInfoV2> connectionInfo; |
|
65 |
|
66 iConn.EnumerateConnections(iNumConnections); |
|
67 netConnections = iNumConnections; |
|
68 for(TUint i = 1; i <= iNumConnections; i++) |
|
69 { |
|
70 iConn.GetConnectionInfo(i, connectionInfo); |
|
71 // dont count bluetooth connection |
|
72 if(connectionInfo().ConnectionType() >= EConnectionBTPAN) |
|
73 { |
|
74 netConnections--; |
|
75 } |
|
76 } |
|
77 |
|
78 if (netConnections > 0) |
|
79 { |
|
80 return ETrue; |
|
81 } |
|
82 |
|
83 return EFalse; |
|
84 } |
|
85 |
|
86 void CWidgetUiNetworkListener::RunL() |
|
87 { |
|
88 if ( iNote().iState == EInterfaceUp ) |
|
89 { |
|
90 // notify widgets of an active network connection |
|
91 iWindowManager.NotifyConnecionChange( ETrue ); |
|
92 } |
|
93 else if ( iNote().iState == EInterfaceDown ) |
|
94 { |
|
95 // check if there are no other active connections |
|
96 if ( !CheckActiveNetworkConnection() ) |
|
97 { |
|
98 // notify widgets of a network connection going down |
|
99 iWindowManager.NotifyConnecionChange( EFalse ); |
|
100 } |
|
101 } |
|
102 |
|
103 iConn.AllInterfaceNotification( iNote, iStatus ); |
|
104 SetActive(); |
|
105 } |
|
106 |
|
107 void CWidgetUiNetworkListener::DoCancel() |
|
108 { |
|
109 iConn.CancelAllInterfaceNotification(); |
|
110 Close(); |
|
111 } |
|
112 |
|
113 TInt CWidgetUiNetworkListener::Connect() |
|
114 { |
|
115 TInt err; |
|
116 |
|
117 if( ( err = iSocketServer.Connect() ) == KErrNone ) |
|
118 { |
|
119 if( ( err = iConn.Open(iSocketServer, KAfInet) ) != KErrNone ) |
|
120 { |
|
121 //failed, close server too |
|
122 iSocketServer.Close(); |
|
123 } |
|
124 } |
|
125 |
|
126 return err; |
|
127 } |
|
128 |
|
129 void CWidgetUiNetworkListener::Close() |
|
130 { |
|
131 iConn.Close(); // destructor does these |
|
132 iSocketServer.Close(); |
|
133 } |