qtinternetradio/irqnetworkcontroller/src/irqnetworkcontroller.cpp
changeset 14 896e9dbc5f19
parent 11 f683e24efca3
equal deleted inserted replaced
12:608f67c22514 14:896e9dbc5f19
    15 *
    15 *
    16 */
    16 */
    17 #include <QStringList>
    17 #include <QStringList>
    18 
    18 
    19 #include "irqnetworkcontroller.h"
    19 #include "irqnetworkcontroller.h"
    20 #include "irqnetworkcontrollerbody.h" 
    20 #include "irqnetworkcontroller_p.h" 
       
    21 
       
    22 
       
    23 IRQNetworkController * IRQNetworkController::mInstance = NULL;
       
    24 QMutex IRQNetworkController::mMutex;
    21 
    25 
    22 // ---------------------------------------------------------------------------
    26 // ---------------------------------------------------------------------------
    23 // IRQNetworkController::openInstance()
    27 // IRQNetworkController::openInstance()
    24 // Static function to get a singleton instance of IRQNetworkController
    28 // Static function to get a singleton instance of IRQNetworkController
    25 // @return IRQNetworkController *
    29 // @return IRQNetworkController *
    26 // ---------------------------------------------------------------------------
    30 // ---------------------------------------------------------------------------
    27 //
    31 //
    28 EXPORT_C IRQNetworkController* IRQNetworkController::openInstance()
    32 IRQNetworkController* IRQNetworkController::openInstance()
    29 {
    33 {
    30     // Get singleton instance
    34     mMutex.lock();
    31     IRQNetworkController* irqnetworkcontroller =
    35     
    32                            reinterpret_cast<IRQNetworkController*>(Dll::Tls());
    36     if (NULL == mInstance) 
    33 
    37     {
    34     if (NULL == irqnetworkcontroller)
    38         mInstance = new IRQNetworkController();
    35     {
    39 
    36         TRAPD(error, irqnetworkcontroller = createInstanceL());
    40         if (!mInstance->mInitPrivateSuccess) 
    37         if (KErrNone != error)
       
    38         {
    41         {
    39             delete irqnetworkcontroller;
    42             delete mInstance;
    40             irqnetworkcontroller = NULL;
    43             mInstance = NULL;
    41             Dll::SetTls(NULL);
       
    42         }
    44         }
    43     }
    45     }
    44     else
    46     else
    45     {
    47     {
    46         irqnetworkcontroller->iSingletonInstances++;
    48         mInstance->mRefCount++;
    47     }
    49     }
    48 
    50 
    49     return irqnetworkcontroller;
    51     mMutex.unlock();
       
    52 
       
    53     return mInstance;
    50 }
    54 }
    51 
    55 
    52 // ---------------------------------------------------------------------------
    56 // ---------------------------------------------------------------------------
    53 // IRQNetworkController::closeInstance()
    57 // IRQNetworkController::closeInstance()
    54 // Close a singleton instance of IRQNetworkController
    58 // Close a singleton instance of IRQNetworkController
    55 // ---------------------------------------------------------------------------
    59 // ---------------------------------------------------------------------------
    56 //
    60 //
    57 EXPORT_C void IRQNetworkController::closeInstance()
    61 void IRQNetworkController::closeInstance()
    58 {
    62 {
    59     iSingletonInstances--;
    63     mMutex.lock();
    60 
    64     if ((--mRefCount) == 0)
    61     if (0 == iSingletonInstances)
    65     {
    62     {
    66         if(this == mInstance)
    63         Dll::SetTls(NULL);
    67         {
       
    68             mInstance = NULL;
       
    69         }
    64         delete this;
    70         delete this;
    65     }
    71     }
       
    72     mMutex.unlock();
    66 }
    73 }
    67 
    74 
    68 // ---------------------------------------------------------------------------
    75 // ---------------------------------------------------------------------------
    69 // IRQNetworkController::GetNetworkStatus()
    76 // IRQNetworkController::GetNetworkStatus()
    70 // Returns the variable which indicates if connection is active or not
    77 // Returns the variable which indicates if connection is active or not
    71 // @return bool
    78 // @return bool
    72 // ---------------------------------------------------------------------------
    79 // ---------------------------------------------------------------------------
    73 //
    80 //
    74 EXPORT_C bool IRQNetworkController::getNetworkStatus() const
    81 bool IRQNetworkController::getNetworkStatus() const
    75 {
    82 {
    76     return iBody->getNetworkStatus();
    83     return d_ptr->getNetworkStatus();
    77 }
    84 }
    78 
    85 
    79 // ---------------------------------------------------------------------------
    86 // ---------------------------------------------------------------------------
    80 // IRQNetworkController::getIAPId()
    87 // IRQNetworkController::getIAPId()
    81 // Gets the IAP Id of the chosen IAP
    88 // Gets the IAP Id of the chosen IAP
    82 // @return IRQError
    89 // @return IRQError
    83 // ---------------------------------------------------------------------------
    90 // ---------------------------------------------------------------------------
    84 //
    91 //
    85 EXPORT_C IRQError IRQNetworkController::getIAPId(unsigned long& aIapId) const
    92 IRQError IRQNetworkController::getIAPId(unsigned long& aIapId) const
    86 {
    93 {
    87     return iBody->getIAPId(aIapId);
    94     return d_ptr->getIAPId(aIapId);
    88 }
    95 }
    89 
    96 
    90 // ---------------------------------------------------------------------------
    97 // ---------------------------------------------------------------------------
    91 // IRQNetworkController::chooseAccessPoint()
    98 // IRQNetworkController::chooseAccessPoint()
    92 // Configures the Access Point which is used by all the components for network
    99 // Configures the Access Point which is used by all the components for network
    93 // connectivity
   100 // connectivity
    94 // ---------------------------------------------------------------------------
   101 // ---------------------------------------------------------------------------
    95 //
   102 //
    96 EXPORT_C void IRQNetworkController::chooseAccessPoint()
   103 void IRQNetworkController::chooseAccessPoint()
    97 {
   104 {
    98     iBody->chooseAccessPoint();
   105     d_ptr->chooseAccessPoint();
    99 }
   106 }
   100 
   107 
   101 /*
   108 /*
   102  * Cancel configuring access point
   109  * Cancel configuring access point
   103  */
   110  */
   104 EXPORT_C void IRQNetworkController::cancelConnecting()
   111 void IRQNetworkController::cancelConnecting()
   105 {
   112 {
   106     iBody->cancelConnecting();
   113     d_ptr->cancelConnecting();
   107     iBody->resetConnectionStatus();
   114     d_ptr->resetConnectionStatus();
   108 }
   115 }
   109 
   116 
   110 // ---------------------------------------------------------------------------
   117 // ---------------------------------------------------------------------------
   111 // IRQNetworkController::isOfflineMode()
   118 // IRQNetworkController::isOfflineMode()
   112 // Indicates if the phone is in offline mode
   119 // Indicates if the phone is in offline mode
   113 // @return True if the phone is in offline mode else False
   120 // @return True if the phone is in offline mode else False
   114 // ---------------------------------------------------------------------------
   121 // ---------------------------------------------------------------------------
   115 //
   122 //
   116 EXPORT_C bool IRQNetworkController::isOfflineMode()
   123 bool IRQNetworkController::isOfflineMode()
   117 {
   124 {
   118     return iBody->isOfflineMode();
   125     return d_ptr->isOfflineMode();
   119 }
   126 }
   120 
   127 
   121 // ---------------------------------------------------------------------------
   128 // ---------------------------------------------------------------------------
   122 // IRQNetworkController::isWlanSupported()
   129 // IRQNetworkController::isWlanSupported()
   123 // Indicates if the phone supports WLan usage
   130 // Indicates if the phone supports WLan usage
   124 // @return True if the phone supports else False
   131 // @return True if the phone supports else False
   125 // ---------------------------------------------------------------------------
   132 // ---------------------------------------------------------------------------
   126 //
   133 //
   127 EXPORT_C bool IRQNetworkController::isWlanSupported() const
   134 bool IRQNetworkController::isWlanSupported() const
   128 {
   135 {
   129     return iBody->isWlanSupported();
   136     return d_ptr->isWlanSupported();
   130 }
   137 }
   131 
   138 
   132 // ---------------------------------------------------------------------------
   139 // ---------------------------------------------------------------------------
   133 // IRQNetworkController::resetConnectionStatus()
   140 // IRQNetworkController::resetConnectionStatus()
   134 // Resets the connection status to Disconnected state
   141 // Resets the connection status to Disconnected state
   135 // ---------------------------------------------------------------------------
   142 // ---------------------------------------------------------------------------
   136 //
   143 //
   137 EXPORT_C void IRQNetworkController::resetConnectionStatus()
   144 void IRQNetworkController::resetConnectionStatus()
   138 {
   145 {
   139     iBody->resetConnectionStatus();
   146     d_ptr->resetConnectionStatus();
   140 }
   147 }
   141 
   148 
   142 // ---------------------------------------------------------------------------
   149 // ---------------------------------------------------------------------------
   143 // IRQNetworkController::identifyConnectionType()
   150 // IRQNetworkController::identifyConnectionType()
   144 // Indicates the type of connection
   151 // Indicates the type of connection
   145 // @return enum describing the type of connection ( GPRS/3G/WiFi )
   152 // @return enum describing the type of connection ( GPRS/3G/WiFi )
   146 // ---------------------------------------------------------------------------
   153 // ---------------------------------------------------------------------------
   147 //
   154 //
   148 EXPORT_C IRQConnectionType IRQNetworkController::identifyConnectionType() const
   155 IRQConnectionType IRQNetworkController::identifyConnectionType() const
   149 {
   156 {
   150     return iBody->identifyConnectionType();
   157     return d_ptr->identifyConnectionType();
   151 }
   158 }
   152 
   159 
   153 // ---------------------------------------------------------------------------
   160 // ---------------------------------------------------------------------------
   154 // IRQNetworkController::notifyActiveNetworkObservers()
   161 // IRQNetworkController::notifyActiveNetworkObservers()
   155 // Notifies all observers whose network request is active to reissue the request
   162 // Notifies all observers whose network request is active to reissue the request
   156 // ---------------------------------------------------------------------------
   163 // ---------------------------------------------------------------------------
   157 //
   164 //
   158 EXPORT_C void IRQNetworkController::notifyActiveNetworkObservers(IRQNetworkEvent aEvent)
   165 void IRQNetworkController::notifyActiveNetworkObservers(IRQNetworkEvent aEvent)
   159 {
   166 {
   160     iBody->notifyActiveNetworkObservers(aEvent);
   167     d_ptr->notifyActiveNetworkObservers(aEvent);
   161 }
   168 }
   162 
   169 
   163 // ---------------------------------------------------------------------------
   170 // ---------------------------------------------------------------------------
   164 // IRQNetworkController::isHandlingOverConnection()
   171 // IRQNetworkController::isHandlingOverConnection()
   165 // Indicates if the hand over of network connection has happened
   172 // Indicates if the hand over of network connection has happened
   166 // @return bool
   173 // @return bool
   167 // ---------------------------------------------------------------------------
   174 // ---------------------------------------------------------------------------
   168 //
   175 //
   169 EXPORT_C bool IRQNetworkController::isHandlingOverConnection()
   176 bool IRQNetworkController::isHandlingOverConnection()
   170 {
   177 {
   171     return iBody->isHandlingOverConnection();
   178     return d_ptr->isHandlingOverConnection();
   172 }
   179 }
   173 
   180 
   174 // ---------------------------------------------------------------------------
   181 // ---------------------------------------------------------------------------
   175 // IRQNetworkController::isConnectRequestIssued()
   182 // IRQNetworkController::isConnectRequestIssued()
   176 // Indicates if chooseAccessPoint is called
   183 // Indicates if chooseAccessPoint is called
   177 // @return bool
   184 // @return bool
   178 // ---------------------------------------------------------------------------
   185 // ---------------------------------------------------------------------------
   179 //
   186 //
   180 EXPORT_C bool IRQNetworkController::isConnectRequestIssued() const
   187 bool IRQNetworkController::isConnectRequestIssued() const
   181 {
   188 {
   182     return iBody->isConnectRequestIssued();
   189     return d_ptr->isConnectRequestIssued();
   183 }
   190 }
   184 
   191 
   185 // ---------------------------------------------------------------------------
   192 // ---------------------------------------------------------------------------
   186 // IRQNetworkController::IRQNetworkController()
   193 // IRQNetworkController::IRQNetworkController()
   187 // Default constructor
   194 // Default constructor
   188 // ---------------------------------------------------------------------------
   195 // ---------------------------------------------------------------------------
   189 //
   196 //
   190 IRQNetworkController::IRQNetworkController()
   197 IRQNetworkController::IRQNetworkController() :
   191 {
   198     mRefCount(1), d_ptr(new IRQNetworkControllerPrivate(this)), mInitPrivateSuccess(false)
       
   199 {
       
   200     Q_ASSERT(d_ptr);
       
   201     if (d_ptr) 
       
   202     {
       
   203         mInitPrivateSuccess = d_ptr->init();
       
   204     }
   192 }
   205 }
   193 
   206 
   194 // ---------------------------------------------------------------------------
   207 // ---------------------------------------------------------------------------
   195 // IRQNetworkController::~IRQNetworkController()
   208 // IRQNetworkController::~IRQNetworkController()
   196 // Destructor
   209 // Destructor
   197 //
   210 //
   198 // ---------------------------------------------------------------------------
   211 // ---------------------------------------------------------------------------
   199 //
   212 //
   200 IRQNetworkController::~IRQNetworkController()
   213 IRQNetworkController::~IRQNetworkController()
   201 {
   214 {
   202     delete iBody;
   215     delete d_ptr;
   203     iBody = NULL;
   216 }
   204 }
   217 
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // IRQNetworkController::constructL()
       
   208 // Two-Phase Constructor.
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void IRQNetworkController::constructL()
       
   212 {
       
   213     iBody = new (ELeave) IRQNetworkControllerBody();
       
   214     Q_ASSERT(iBody);
       
   215     iBody->initL();
       
   216     
       
   217     connect(iBody, SIGNAL(networkRequestNotified(IRQNetworkEvent)),
       
   218             this, SIGNAL(networkRequestNotified(IRQNetworkEvent)));
       
   219     connect(iBody, SIGNAL(pendingRequestsReset(bool)),
       
   220             this, SIGNAL(pendingRequestsReset(bool)));
       
   221     connect(iBody, SIGNAL(networkEventNotified(IRQNetworkEvent)),
       
   222             this, SIGNAL(networkEventNotified(IRQNetworkEvent)));
       
   223     connect(iBody, SIGNAL(errorOccured(IRQError)),
       
   224             this, SIGNAL(errorOccured(IRQError)));
       
   225 }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // IRQNetworkController::createInstanceL()
       
   229 // Creates IRQNetworkController instance
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 IRQNetworkController* IRQNetworkController::createInstanceL()
       
   233 {
       
   234     IRQNetworkController* nwkController = new (ELeave) IRQNetworkController();
       
   235     nwkController->constructL();
       
   236     User::LeaveIfError(Dll::SetTls(nwkController));
       
   237     nwkController->iSingletonInstances = 1;
       
   238     return nwkController;
       
   239 }