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