connectionmonitoring/connectionview/src/connectionview.cpp
branchRCL_3
changeset 58 83ca720e2b9a
parent 57 05bc53fe583b
child 62 bb1f80fb7db2
equal deleted inserted replaced
57:05bc53fe583b 58:83ca720e2b9a
     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 #include <HbTranslator>
       
    19 #include <QLocale>
       
    20 #include <QList>
       
    21 #include <HbLabel>
       
    22 #include <HbPushButton>
       
    23 #include <HbMainWindow>
       
    24 #include <HbGroupBox>
       
    25 #include <HbListWidget>
       
    26 #include <QHBoxLayout>
       
    27 #include <HbScrollArea>
       
    28 #include <HbView>
       
    29 #include <HbIcon>
       
    30 #include <HbAction>
       
    31 #include <HbToolBar>
       
    32 #include <HbStyleLoader>
       
    33 #include <QGraphicsLinearLayout>
       
    34 #include <QtCore/QSignalMapper>
       
    35 #include <QtNetwork>
       
    36 #include "connectionview.h"
       
    37 #include "scrollareawidget.h"
       
    38 #include "OstTraceDefinitions.h"
       
    39 #ifdef OST_TRACE_COMPILER_IN_USE
       
    40 #include "connectionviewTraces.h"
       
    41 #endif
       
    42 
       
    43 
       
    44 QTM_USE_NAMESPACE
       
    45 
       
    46 const qreal typeLabelWidth = 18.0;
       
    47 const QString iapIdentifierPrefix = "I_";
       
    48 
       
    49 ConnectionView::ConnectionView():
       
    50     mNetConfigurationManager(new QNetworkConfigurationManager(this)),
       
    51     mSignalMapper(new QSignalMapper(this)),
       
    52     mConnectionCount(0),
       
    53     mClosingTimer(0)
       
    54 {
       
    55     OstTraceFunctionEntry0( CONNECTIONVIEW_CONNECTIONVIEW_ENTRY );
       
    56     // Install localization
       
    57     mTranslator = QSharedPointer<HbTranslator>(new HbTranslator("connectionview"));
       
    58    
       
    59     // Register custom layout location
       
    60     bool registerStatus = HbStyleLoader::registerFilePath(":/layout/");
       
    61     Q_ASSERT(registerStatus);
       
    62     
       
    63     // Map the configurationChanged signal to a slot in order to get
       
    64     // information about the changes in the connections
       
    65     bool connectStatus = connect(
       
    66             mNetConfigurationManager,
       
    67             SIGNAL(configurationChanged(const QNetworkConfiguration&)),
       
    68             this,
       
    69             SLOT(handleConfigurationChanged(const QNetworkConfiguration&)));
       
    70     
       
    71     // Create the view and show it
       
    72     createView();
       
    73     OstTraceFunctionExit0( CONNECTIONVIEW_CONNECTIONVIEW_EXIT );
       
    74 }
       
    75 
       
    76 
       
    77 ConnectionView::~ConnectionView()
       
    78 {
       
    79     OstTraceFunctionEntry0( DUP1_CONNECTIONVIEW_CONNECTIONVIEW_ENTRY );
       
    80     // other widgets are childs of this widget, so they will be
       
    81     // deleted along with mMainView. 
       
    82     delete mMainView;
       
    83     delete mNoConnView;
       
    84     OstTraceFunctionExit0( DUP1_CONNECTIONVIEW_CONNECTIONVIEW_EXIT );
       
    85 }
       
    86 
       
    87 /* !
       
    88      Quit the application when the timer is triggered
       
    89 */
       
    90 void ConnectionView::timerEvent(QTimerEvent * /*event*/)
       
    91 {
       
    92     killTimer(mClosingTimer);
       
    93     mClosingTimer = 0;
       
    94     qApp->quit();
       
    95 }
       
    96 
       
    97 
       
    98 /*!
       
    99     Builds the actual view for the main window
       
   100 */ 
       
   101 void ConnectionView::createView()
       
   102 {
       
   103     OstTraceFunctionEntry0( CONNECTIONVIEW_CREATEVIEW_ENTRY );
       
   104     
       
   105     // Create the secondary view for displaying the "No active connections"-text
       
   106     mNoConnView = new HbView();
       
   107     mNoConnView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   108     addView(mNoConnView);
       
   109     QGraphicsLinearLayout *noConnViewLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   110     noConnViewLayout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   111 
       
   112     HbLabel *infoLabel = new HbLabel;
       
   113     infoLabel->setObjectName("mInfoLabel");
       
   114     infoLabel->setPlainText(hbTrId("txt_occ_info_no_active_connections"));
       
   115     infoLabel->setAlignment(Qt::AlignCenter);
       
   116     noConnViewLayout->addItem(infoLabel);
       
   117     noConnViewLayout->setAlignment(infoLabel, Qt::AlignCenter);
       
   118     mNoConnView->setLayout(noConnViewLayout);
       
   119    
       
   120     mMainLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   121     mMainLayout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   122     mScrollArea = new HbScrollArea();
       
   123     mScrollArea->setScrollDirections(Qt::Vertical);
       
   124     mScrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   125     mMainLayout->addItem(mScrollArea);
       
   126 
       
   127     // Create the mainView and the layout for the window
       
   128     mMainView = new HbView();
       
   129     addView(mMainView);
       
   130     ScrollAreaWidget *scrollContent = new ScrollAreaWidget();
       
   131     scrollContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   132     mScrollArea->installEventFilter(scrollContent);
       
   133     
       
   134     mBoxLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   135     mBoxLayout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   136     scrollContent->setLayout(mBoxLayout);
       
   137     mMainView->setLayout(mMainLayout);
       
   138     mScrollArea->setContentWidget(scrollContent);
       
   139     
       
   140     // Create the toolbar and the disconnection action
       
   141     mToolBar = new HbToolBar();    
       
   142     HbIcon icon("qtg_mono_disconnect");
       
   143     HbAction *disconnectAction = new HbAction(mToolBar);
       
   144     disconnectAction->setIcon(icon);
       
   145     mToolBar->addAction(disconnectAction);
       
   146     mMainView->setToolBar(mToolBar);
       
   147     bool connectStatus = connect(
       
   148             disconnectAction,
       
   149             SIGNAL(triggered(bool)),
       
   150             this,
       
   151             SLOT(disconnectAll()));
       
   152     
       
   153     // Create the actual groupboxes for all the active connections
       
   154     createGroupBoxesForConnections();  
       
   155     show();
       
   156     
       
   157     OstTraceFunctionExit0( CONNECTIONVIEW_CREATEVIEW_EXIT );
       
   158 }
       
   159 
       
   160 
       
   161 /*!
       
   162     Loops all the active cellular connections and creates groupboxes for those
       
   163 */ 
       
   164 void ConnectionView::createGroupBoxesForConnections()
       
   165 {
       
   166     OstTraceFunctionEntry0( CONNECTIONVIEW_CREATEGROUPBOXESFORCONNECTIONS_ENTRY );
       
   167     
       
   168     // disconnect all the old connections and then make the new connections
       
   169     mSignalMapper->disconnect();
       
   170     
       
   171     //Let's find active connections if any
       
   172     QList<QNetworkConfiguration> activeConfigurations;
       
   173     getActiveCellularConnections(activeConfigurations);
       
   174     
       
   175     // If there are no active connections, then the dialog is not shown to the user
       
   176     mConnectionCount = activeConfigurations.count();
       
   177     
       
   178     // Toolbar is shown only if there are more than one connections active
       
   179     if (mConnectionCount > 1) {
       
   180         mMainView->setItemVisible(Hb::ToolBarItem, true);
       
   181     } else {
       
   182         mMainView->setItemVisible(Hb::ToolBarItem, false);
       
   183     }
       
   184     
       
   185     // if there are connections, the main view with the connections is shown
       
   186     if (mConnectionCount > 0) { 
       
   187         setCurrentView(mMainView);
       
   188 
       
   189         for (int i=0; i<mConnectionCount; i++) {
       
   190             // Get the iap id and the iap name for the UI construction
       
   191             bool ok = true;
       
   192             int iapId = activeConfigurations[i].identifier().remove(iapIdentifierPrefix).toInt(&ok);
       
   193             QString iapName = activeConfigurations[i].name();
       
   194 
       
   195             if (ok) {
       
   196                 addGroupBox(iapId, iapName);
       
   197             }        
       
   198         }
       
   199         // This connection must be done only once, thus it's done here separately
       
   200         bool connectStatus = connect(
       
   201             mSignalMapper, 
       
   202             SIGNAL(mapped(int)), 
       
   203             this, 
       
   204             SLOT(disconnectSelectedIap(int)));
       
   205         
       
   206         // there are no connections, show the view with the "no connections" label
       
   207     } else {
       
   208         setCurrentView(mNoConnView);
       
   209         // start the timer to close the application after 3 seconds
       
   210         if (mClosingTimer == 0) {
       
   211             mClosingTimer = startTimer(timerValue);
       
   212         }
       
   213     }
       
   214    
       
   215    OstTraceFunctionExit0( CONNECTIONVIEW_CREATEGROUPBOXESFORCONNECTIONS_EXIT );
       
   216 }
       
   217 
       
   218 
       
   219 /*
       
   220     Returns a list of all the available active cellular connections
       
   221 */
       
   222 void ConnectionView::getActiveCellularConnections(QList<QNetworkConfiguration> &activeConfigurations)
       
   223 {
       
   224     OstTraceFunctionEntry0( CONNECTIONVIEW_GETACTIVECELLULARCONNECTIONS_ENTRY );
       
   225     //Let's find active connections if any
       
   226     activeConfigurations.clear();
       
   227 
       
   228     QList<QNetworkConfiguration> configurations = 
       
   229         mNetConfigurationManager->allConfigurations(QNetworkConfiguration::Active);
       
   230 
       
   231     // Connection are open if they are in active state
       
   232     for (int i=0; i<configurations.count(); i++) {  
       
   233         if (configurations[i].type() == QNetworkConfiguration::InternetAccessPoint) {
       
   234             QString bearerName = configurations[i].bearerName();
       
   235             
       
   236             // WLAN connections are filtered out
       
   237             // TODO At some point QNetwork configuration will provide these constants
       
   238             if (bearerName==bearer2G || bearerName==bearerWCDMA || 
       
   239                 bearerName==bearerHSPA || bearerName==bearerCDMA2000) {
       
   240                 activeConfigurations.append(configurations[i]);
       
   241             }
       
   242         }
       
   243     }
       
   244     OstTraceFunctionExit0( CONNECTIONVIEW_GETACTIVECELLULARCONNECTIONS_EXIT );
       
   245 }
       
   246     
       
   247 
       
   248 /*!
       
   249     Handles the changes in the configurations whose states change to 
       
   250     active or discovered (meaning connected and disconnected)
       
   251 */ 
       
   252 void ConnectionView::handleConfigurationChanged(const QNetworkConfiguration& config)
       
   253 {
       
   254     OstTraceFunctionEntry0( CONNECTIONVIEW_HANDLECONFIGURATIONCHANGED_ENTRY );
       
   255     switch (config.state())
       
   256     {
       
   257         case QNetworkConfiguration::Undefined:
       
   258             //Nothing done
       
   259             break;
       
   260         case QNetworkConfiguration::Defined:
       
   261         case QNetworkConfiguration::Discovered:
       
   262         case QNetworkConfiguration::Active:
       
   263         {
       
   264             // If the Configuration change is to Discovered or Active, it means
       
   265             // that a connection was either opened or closed. Update the connections
       
   266             // on the ui
       
   267             QString bearerName = config.bearerName();
       
   268         
       
   269             // Update the view if there are changes in the cellular connections
       
   270             // TODO At some point QNetwork configuration will provide these constants
       
   271             if (bearerName==bearer2G || bearerName==bearerWCDMA || 
       
   272                 bearerName==bearerHSPA || bearerName==bearerCDMA2000) {     
       
   273                 // kill the application closing timer if it was started (there were no 
       
   274                 // active connections)
       
   275                 if (mClosingTimer != 0) {
       
   276                     killTimer(mClosingTimer);
       
   277                     mClosingTimer = 0;
       
   278                 }
       
   279                 clearGroupBoxView();
       
   280                 // redraw the boxes
       
   281                 createGroupBoxesForConnections();
       
   282                 update();
       
   283             } 
       
   284             break;
       
   285         }
       
   286     }    
       
   287     OstTraceFunctionExit0( CONNECTIONVIEW_HANDLECONFIGURATIONCHANGED_EXIT );
       
   288 }
       
   289 
       
   290 
       
   291 /*!
       
   292     Adds one groupbox into the boxlayout for the given iap
       
   293 */ 
       
   294 void ConnectionView::addGroupBox(int iapId, QString iapName)
       
   295 {
       
   296     OstTraceFunctionEntry0( CONNECTIONVIEW_ADDGROUPBOX_ENTRY );
       
   297     // Create the groupbox and its layouts
       
   298     HbGroupBox *groupBox = new HbGroupBox();
       
   299     groupBox->setObjectName("groupBox");
       
   300     groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
       
   301     groupBox->setHeading(hbTrId("txt_occ_subhead_connection_details"));
       
   302     groupBox->setCollapsable(true);
       
   303     
       
   304     // Create the disconnection button
       
   305     HbPushButton* button = new HbPushButton(
       
   306             hbTrId("txt_occ_button_disconnect"), mMainView);
       
   307     button->setObjectName("disconnectButton");
       
   308     button->setSizePolicy(QSizePolicy::Preferred, 
       
   309                           QSizePolicy::Preferred, 
       
   310                           QSizePolicy::PushButton);
       
   311     
       
   312     // Create the horizontal layout for the labels
       
   313     QGraphicsLinearLayout *labelLayout = new QGraphicsLinearLayout(Qt::Horizontal);
       
   314     labelLayout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   315     HbLabel *typeLabel = new HbLabel(hbTrId("txt_occ_list_name"));
       
   316 
       
   317     // get the pixel size matching the spesified 18 units using the HbDeviceProfile
       
   318     // and set the width of the label
       
   319     HbDeviceProfile profile = HbDeviceProfile::profile(mMainView);
       
   320     typeLabel->setPreferredWidth(typeLabelWidth*profile.unitValue());
       
   321     typeLabel->setObjectName("mConnectionLabel");
       
   322     
       
   323     HbLabel *nameLabel = new HbLabel(iapName);
       
   324     nameLabel->setAlignment(Qt::AlignRight);
       
   325     nameLabel->setObjectName("mConnectionLabel");
       
   326     labelLayout->addItem(typeLabel);
       
   327     labelLayout->addItem(nameLabel);
       
   328    
       
   329     HbWidget *labelWidget = new HbWidget();
       
   330     labelWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   331     labelWidget->setLayout(labelLayout);
       
   332     
       
   333     // a layout cannot be added directly to a layout, we need to use this container widget 
       
   334     HbWidget *boxWidget = new HbWidget();  
       
   335     boxWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   336     QGraphicsLinearLayout *boxLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   337     boxLayout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   338     boxWidget->setLayout(boxLayout);
       
   339 
       
   340     // Add the label layout and the button into the groupbox
       
   341     boxLayout->addItem(labelWidget);
       
   342     boxLayout->addItem(button);
       
   343     boxLayout->setAlignment(button, Qt::AlignHCenter);
       
   344     groupBox->setContentWidget(boxWidget);
       
   345     groupBox->setCollapsed(true);
       
   346     
       
   347     // add the group box to the main layout
       
   348     mBoxLayout->addItem(groupBox);
       
   349     mBoxLayout->setAlignment(groupBox, Qt::AlignHCenter);
       
   350         
       
   351     // Expand the groupbox if connection count is 1 
       
   352     if (mConnectionCount == 1) {
       
   353         groupBox->setCollapsed(false);
       
   354     }
       
   355     
       
   356     // Connect the button to the mapper
       
   357     bool connectStatus = connect(
       
   358         button, 
       
   359         SIGNAL(clicked()), 
       
   360         mSignalMapper, 
       
   361         SLOT(map()));
       
   362     mSignalMapper->setMapping(button, iapId);
       
   363     OstTraceFunctionExit0( CONNECTIONVIEW_ADDGROUPBOX_EXIT );
       
   364 }
       
   365 
       
   366 
       
   367 /*!
       
   368     Closes the connection to the given iap
       
   369 */ 
       
   370 void ConnectionView::disconnectSelectedIap(int iapId)
       
   371 {   
       
   372     OstTraceFunctionEntry0( CONNECTIONVIEW_DISCONNECTSELECTEDIAP_ENTRY );
       
   373    
       
   374     // Get all the active connections 
       
   375     QList<QNetworkConfiguration> activeConfigurations;
       
   376     getActiveCellularConnections(activeConfigurations);
       
   377     int count = activeConfigurations.count();
       
   378             
       
   379     for (int i=0; i < count; i++) {
       
   380         bool ok = true;
       
   381         int loopedIapId = activeConfigurations[i].identifier().remove(iapIdentifierPrefix).toInt(&ok);
       
   382         
       
   383         // if the looped iap matches the iap, stop the connection
       
   384         if (loopedIapId == iapId) {
       
   385             
       
   386             QNetworkSession *networkSession = new QNetworkSession(activeConfigurations[i], mMainView);
       
   387 
       
   388             bool connectStatus = connect(
       
   389                 networkSession, 
       
   390                 SIGNAL(opened()), 
       
   391                 this, 
       
   392                 SLOT(stopSessions()));
       
   393                       
       
   394             // This connection is done basically for memory handling
       
   395             // to be able to delete the session if one of the error signal is emitted
       
   396             connect(
       
   397                 networkSession, 
       
   398                 SIGNAL(error(QNetworkSession::SessionError)), 
       
   399                 this, 
       
   400                 SLOT(errorSessions(QNetworkSession::SessionError)));
       
   401             
       
   402             // open the session only if the connection succeeded, otherwise it's no use
       
   403             if (connectStatus) {
       
   404                 // session needs to be opened in order to stop all the sessions of this configurations
       
   405                 networkSession->open();
       
   406             } else {
       
   407                 OstTrace1( CONNECTSTATUS, CONNECTIONVIEW_DISCONNECTSELECTEDIAP, "ConnectionView::disconnectSelectedIap failed;iapId=%d", iapId );
       
   408             }
       
   409    
       
   410             // the searched iap was found, stop looping
       
   411             break;
       
   412         }
       
   413     }
       
   414     OstTraceFunctionExit0( CONNECTIONVIEW_DISCONNECTSELECTEDIAP_EXIT );
       
   415 }
       
   416 
       
   417 
       
   418 /*!
       
   419     Closes all the connections to active iaps
       
   420 */ 
       
   421 void ConnectionView::disconnectAll()
       
   422 {
       
   423     OstTraceFunctionEntry0( CONNECTIONVIEW_DISCONNECTALL_ENTRY );
       
   424     
       
   425     // Get all the active connections 
       
   426     QList<QNetworkConfiguration> activeConfigurations;
       
   427     getActiveCellularConnections(activeConfigurations);
       
   428     int count = activeConfigurations.count();
       
   429     
       
   430     for (int i=0; i < count; i++) {
       
   431         QNetworkSession *networkSession = new QNetworkSession(activeConfigurations[i], mMainView);
       
   432         bool connectStatus = connect(
       
   433             networkSession, 
       
   434             SIGNAL(opened()), 
       
   435             this, 
       
   436             SLOT(stopSessions()));
       
   437         // This connection is done basically for memory handling
       
   438         // to be able to delete the session if one of the error signal is emitted
       
   439         connect(
       
   440             networkSession, 
       
   441             SIGNAL(error(QNetworkSession::SessionError)), 
       
   442             this, 
       
   443             SLOT(errorSessions(QNetworkSession::SessionError)));
       
   444         
       
   445         // open the session only if the connection succeeded, otherwise it's no use
       
   446         if (connectStatus) {
       
   447             // session needs to be opened in order to stop all the sessions of this configurations
       
   448             networkSession->open();
       
   449         }
       
   450     }
       
   451     OstTraceFunctionExit0( CONNECTIONVIEW_DISCONNECTALL_EXIT );
       
   452 }
       
   453 
       
   454 
       
   455 void ConnectionView::stopSessions() 
       
   456 {
       
   457     OstTraceFunctionEntry0( CONNECTIONVIEW_STOPSESSIONS_ENTRY );
       
   458 
       
   459     // get the sender-session and stop the sessions
       
   460     QObject *senderObject = QObject::sender();
       
   461     if (senderObject != NULL) {
       
   462         QNetworkSession *session = qobject_cast<QNetworkSession *>(senderObject);
       
   463         session->stop();
       
   464         session->deleteLater();
       
   465     }
       
   466     OstTraceFunctionExit0( CONNECTIONVIEW_STOPSESSIONS_EXIT );
       
   467 }
       
   468 
       
   469 void ConnectionView::errorSessions(QNetworkSession::SessionError) 
       
   470 {
       
   471     OstTraceFunctionEntry0( DUP2_CONNECTIONVIEW_ERRORSESSIONS_ENTRY );
       
   472 
       
   473     // something went wrong in session opening
       
   474     // get the sender-session and delete the sender
       
   475     QObject *senderObject = QObject::sender();
       
   476     if (senderObject != NULL) {
       
   477         senderObject->deleteLater();
       
   478     }
       
   479     OstTraceFunctionExit0( DUP1_CONNECTIONVIEW_ERRORSESSIONS_EXIT );
       
   480 }
       
   481 
       
   482 /*!
       
   483     Removes the items from the boxlayout in order to be costructed again.
       
   484 */ 
       
   485 void ConnectionView::clearGroupBoxView()
       
   486 {
       
   487     OstTraceFunctionEntry0( CONNECTIONVIEW_CLEARGROUPBOXVIEW_ENTRY );
       
   488     // Loop all the groupbox items through and remove them from the view
       
   489     int count = mBoxLayout->count();
       
   490     for (int i = 0; i < count; i++ )  {
       
   491         // store the pointer to the item to be able to delete it
       
   492         QGraphicsLayoutItem *item = mBoxLayout->itemAt(0);
       
   493         mBoxLayout->removeAt(0);
       
   494         delete item;
       
   495     }
       
   496     OstTraceFunctionExit0( CONNECTIONVIEW_CLEARGROUPBOXVIEW_EXIT );
       
   497 }
       
   498 
       
   499 
       
   500 
       
   501