wlanutilities/wlansniffer/src/wlansnifferlistview.cpp
branchGCC_SURGE
changeset 47 b3d8f88532b7
parent 34 30a5f517c615
parent 46 2fbd1d709fe7
equal deleted inserted replaced
34:30a5f517c615 47:b3d8f88532b7
     1 /*
       
     2  * Copyright (c) 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 "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 <QGraphicsWidget>
       
    19 
       
    20 #include <HbLabel>
       
    21 #include <HbMenu>
       
    22 #include <HbAction>
       
    23 #include <HbListWidget>
       
    24 #include <HbListWidgetItem>
       
    25 #include <HbDocumentLoader>
       
    26 #include <HbInstance>
       
    27 
       
    28 #include <xqserviceutil.h>
       
    29 
       
    30 #include "wlanqtutils.h"
       
    31 #include "wlanqtutilswlanap.h"
       
    32 #include "wlanqtutilswlaniap.h"
       
    33 #include "wlanqtutilscommon.h"
       
    34 
       
    35 #include "wlansniffer.h"
       
    36 #include "wlansnifferlistview.h"
       
    37 #include "OstTraceDefinitions.h"
       
    38 #ifdef OST_TRACE_COMPILER_IN_USE
       
    39 #include "wlansnifferlistviewTraces.h"
       
    40 #endif
       
    41 
       
    42 WlanSnifferListView::WlanSnifferListView(WlanSniffer *appRef) : 
       
    43     mWlanList(NULL),
       
    44     mStatusLabel(NULL),
       
    45     mAppRef(appRef),
       
    46     mConnectingIapId(0),
       
    47     mContextMenu(NULL)
       
    48 {
       
    49     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_WLANSNIFFERLISTVIEW_ENTRY );
       
    50     
       
    51     bool ok = false;
       
    52     HbDocumentLoader loader;
       
    53     loader.load(":/docml/wlansnifferlistview.docml", &ok);
       
    54     Q_ASSERT_X(ok, "Wlan Sniffer", "Invalid docml file");
       
    55     
       
    56     // Load the view by name from the xml file
       
    57     QGraphicsWidget *widget = loader.findWidget("occ_list");
       
    58     Q_ASSERT_X(widget != 0, "Wlan Sniffer", "View not found");
       
    59     // Set the WlanListView view to be the widget that was loaded from the xml
       
    60     setWidget(widget);
       
    61 
       
    62     HbMenu *viewMenu = qobject_cast<HbMenu *>(loader.findWidget("viewMenu"));
       
    63     Q_ASSERT_X(viewMenu != 0, "Wlan Sniffer", "Menu not found");
       
    64     setMenu(viewMenu);
       
    65     
       
    66     mWlanList = qobject_cast<HbListWidget *>(loader.findWidget("listWidget"));
       
    67     Q_ASSERT_X(mWlanList != 0, "Wlan Sniffer", "List not found");
       
    68     
       
    69     mStatusLabel = qobject_cast<HbLabel *>(loader.findWidget("statusLabel"));
       
    70     Q_ASSERT_X(mStatusLabel != 0, "Wlan Sniffer", "Label not found");
       
    71 
       
    72     connect(
       
    73         mWlanList,
       
    74         SIGNAL(activated(HbListWidgetItem *)),
       
    75         this,
       
    76         SLOT(handleListItemActivated(HbListWidgetItem *)));
       
    77     connect(
       
    78         mWlanList,
       
    79         SIGNAL(longPressed(HbListWidgetItem *, const QPointF &)),
       
    80         this,
       
    81         SLOT(handleListItemLongPressed(HbListWidgetItem *, const QPointF &)));
       
    82     
       
    83     // Set correct orientation when drawing view first time. Orientation change
       
    84     // on the fly is not yet implemented
       
    85     if (HbInstance::instance()->allMainWindows().value(0)->orientation() == Qt::Horizontal) {
       
    86         bool loaded = false;
       
    87         loader.load( ":/docml/wlansnifferlistview.docml", "landscape", &loaded );
       
    88     } // else: portrait is ok by default
       
    89 
       
    90     // We need to create a custom navigation action in case the application was
       
    91     // launched as embedded via QtHighway
       
    92     if (XQServiceUtil::isEmbedded()) {
       
    93         HbAction *completeAction = new HbAction(Hb::BackNaviAction, this);
       
    94         setNavigationAction(completeAction);
       
    95         connect(
       
    96             completeAction,
       
    97             SIGNAL(triggered(bool)),
       
    98             this,
       
    99             SIGNAL(completeServiceTriggered()));
       
   100         // TODO: What to do when exit action is ran? Should we close whole control panel?
       
   101     }
       
   102     
       
   103     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_WLANSNIFFERLISTVIEW_EXIT );
       
   104 }
       
   105     
       
   106 WlanSnifferListView::~WlanSnifferListView()
       
   107 {
       
   108     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_WLANSNIFFERLISTVIEWDESTR_ENTRY );    
       
   109     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_WLANSNIFFERLISTVIEWDESTR_EXIT );
       
   110 }
       
   111 
       
   112 void WlanSnifferListView::update()
       
   113 {
       
   114     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_UPDATE_ENTRY );
       
   115     
       
   116     // Do not update list while the context menu is open
       
   117     if (mContextMenu == NULL) {
       
   118         QList<WlanQtUtilsWlanAp*> aps;
       
   119         QList<WlanQtUtilsWlanIap*> iaps;
       
   120         
       
   121         mAppRef->wlanQtUtils()->availableWlanAps(iaps, aps);
       
   122         
       
   123         mWlanList->clear();
       
   124     
       
   125         // Loop the IAPs and APs and create the list items (lists are ordered already by Wlan Qt Utilities)
       
   126         foreach (WlanQtUtilsWlanIap *iap, iaps) {
       
   127             mWlanList->addItem(wlanListItemIapCreate(iap));
       
   128         }
       
   129         // Loop the AP's and create the list items
       
   130         foreach (WlanQtUtilsWlanAp *ap, aps) {
       
   131             mWlanList->addItem(wlanListItemApCreate(ap));
       
   132         }
       
   133     }
       
   134 
       
   135     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_UPDATE_EXIT );
       
   136 }
       
   137 
       
   138 void WlanSnifferListView::updateConnectionOpened(int iapId)
       
   139 {
       
   140     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONOPENED_ENTRY );
       
   141     
       
   142     mStatusLabel->setPlainText(
       
   143         hbTrId("txt_occ_grid_connected_to_1").arg(mAppRef->wlanQtUtils()->iap(iapId)->name()));
       
   144     // Redraw the list so that the connected network moves on top
       
   145     update();
       
   146     mConnectingIapId = 0;
       
   147 
       
   148     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONOPENED_EXIT );
       
   149 }
       
   150 
       
   151 void WlanSnifferListView::updateConnectionClosed(int iapId)
       
   152 {
       
   153     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONCLOSED_ENTRY );
       
   154     
       
   155     (void)iapId;
       
   156     mStatusLabel->setPlainText(hbTrId("txt_occ_grid_not_connected"));
       
   157     mConnectingIapId = 0;
       
   158 
       
   159     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONCLOSED_EXIT );
       
   160 }
       
   161 
       
   162 void WlanSnifferListView::handleContextMenuClosed()
       
   163 {
       
   164     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLECONTEXTMENUCLOSED_ENTRY );
       
   165 
       
   166     // Menu deletes itself, but we need to cleanup the member variable
       
   167     // in order to enable list updating again
       
   168     mContextMenu = NULL;
       
   169     
       
   170     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLECONTEXTMENUCLOSED_EXIT );
       
   171 }
       
   172 
       
   173 void WlanSnifferListView::handleListItemActivated(HbListWidgetItem *item)
       
   174 {
       
   175     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLELISTITEMACTIVATED_ENTRY );
       
   176     
       
   177     QVariant data = item->data();
       
   178     if (data.canConvert<int>()) {
       
   179         // Item was an IAP. Connect the IAP or show details view depending on connection state
       
   180         WlanQtUtilsIap *iap = mAppRef->wlanQtUtils()->iap(data.toInt());
       
   181         if (iap->connectionStatus() == WlanQtUtilsConnectionStatusConnected) {
       
   182             // Switch to details view
       
   183             emit detailsTriggered(data.toInt());
       
   184         } else {
       
   185             // Connect the IAP
       
   186             mConnectingIapId = iap->id();
       
   187             mStatusLabel->setPlainText(hbTrId("txt_occ_grid_connecting_to_1").arg(iap->name()));
       
   188             mAppRef->wlanQtUtils()->connectIap(mConnectingIapId);
       
   189         }
       
   190     } else {
       
   191         // Item has to be a WLAN AP, which can't already be connected. Create an IAP and connect it.
       
   192         Q_ASSERT(data.canConvert<WlanQtUtilsWlanAp>());
       
   193         WlanQtUtilsWlanAp ap = data.value<WlanQtUtilsWlanAp>();
       
   194         mConnectingIapId = mAppRef->wlanQtUtils()->createWlanIap(&ap);
       
   195         WlanQtUtilsIap *iap = mAppRef->wlanQtUtils()->iap(mConnectingIapId);
       
   196         Q_ASSERT(iap);
       
   197         mStatusLabel->setPlainText(hbTrId("txt_occ_grid_connecting_to_1").arg(iap->name()));
       
   198         mAppRef->wlanQtUtils()->connectIap(mConnectingIapId);
       
   199     }
       
   200 
       
   201     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLELISTITEMACTIVATED_EXIT );
       
   202 }
       
   203 
       
   204 void WlanSnifferListView::handleListItemLongPressed(HbListWidgetItem *item, const QPointF &coords)
       
   205 {
       
   206     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLELISTITEMLONGPRESSED_ENTRY );
       
   207     
       
   208     Q_ASSERT(mContextMenu == NULL);
       
   209 
       
   210     if (item->data().canConvert<int>() && item->data().toInt() ==
       
   211         mAppRef->wlanQtUtils()->connectedWlanId()) {
       
   212         // Connected IAP, add "Disconnect" action
       
   213 		    mContextMenu = new HbMenu();
       
   214 		    // Remember the item that was long pressed
       
   215     		mContextMenuData = item->data();
       
   216         mContextMenu->addAction(
       
   217             hbTrId("txt_common_menu_disconnect"),
       
   218             this,
       
   219             SLOT(handleListItemDisconnect()));
       
   220 		    // Show the menu and connect closure signal (to re-enable list refreshing)
       
   221 		    bool connectStatus = connect(
       
   222 		        mContextMenu,
       
   223 		        SIGNAL(aboutToClose()),
       
   224 		        this,
       
   225 		        SLOT(handleContextMenuClosed()));
       
   226 		    Q_ASSERT(connectStatus);
       
   227 		    mContextMenu->setAttribute(Qt::WA_DeleteOnClose);
       
   228 		    mContextMenu->setTimeout(HbPopup::ContextMenuTimeout);
       
   229 		    mContextMenu->setPreferredPos(coords);
       
   230 		    mContextMenu->show();
       
   231     }
       
   232 
       
   233     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLELISTITEMLONGPRESSED_EXIT );
       
   234 }
       
   235 
       
   236 /*!
       
   237     Handles a "Disconnect" action selected from the context menu for a list item.
       
   238  */
       
   239 void WlanSnifferListView::handleListItemDisconnect()
       
   240 {
       
   241     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLELISTITEMDISCONNECT_ENTRY );
       
   242 
       
   243     // "Disconnect" was only added, if the item was an IAP and data was
       
   244     // the IAP ID
       
   245     Q_ASSERT(mContextMenuData.canConvert<int>());
       
   246     mAppRef->wlanQtUtils()->disconnectIap(mContextMenuData.toInt());
       
   247     
       
   248     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLELISTITEMDISCONNECT_EXIT );
       
   249 }
       
   250 
       
   251 // Todo: This is a temporary solution - real WLAN status indication & listening
       
   252 // is implemented later on.
       
   253 void WlanSnifferListView::handleDisconnect()
       
   254 {
       
   255     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLEDISCONNECT_ENTRY );
       
   256     
       
   257     mStatusLabel->setPlainText(hbTrId("txt_occ_grid_not_connected"));
       
   258     // Redraw the list so that disconnected network is not shown anymore
       
   259     update();    
       
   260 
       
   261     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLEDISCONNECT_EXIT );
       
   262 }
       
   263 
       
   264 // Todo: no place for separate secure WLAN icon in the widget -> custom layout needed?
       
   265 // Todo: check the signal strength values
       
   266 QString WlanSnifferListView::signalStrengthIconChoose(int signalStrength, WlanQtUtilsWlanSecMode secMode)
       
   267 {
       
   268     (void)secMode;
       
   269     QString iconName;
       
   270     if (signalStrength <= WlanQtUtilsWlanSignalStrengthGood) {
       
   271         iconName = "qtg_small_signal_good";
       
   272     } else if (signalStrength <= WlanQtUtilsWlanSignalStrengthLow) {
       
   273         iconName = "qtg_small_signal_medium";
       
   274     } else {
       
   275         iconName = "qtg_small_signal_low";
       
   276     }
       
   277     
       
   278     return iconName;
       
   279 }
       
   280 
       
   281 HbListWidgetItem *WlanSnifferListView::wlanListItemIapCreate(const WlanQtUtilsWlanIap *iap)
       
   282 {
       
   283     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_WLANLISTITEMIAPCREATE_ENTRY );
       
   284     
       
   285     HbListWidgetItem* item = new HbListWidgetItem();
       
   286     item->setText(iap->name());
       
   287 #if 1                                       // TODO: How to show secure/open network info??
       
   288     if (iap->securityMode() == WlanQtUtilsWlanSecModeOpen) {
       
   289         item->setSecondaryText("Open");
       
   290     } else {
       
   291         item->setSecondaryText("Secure");            
       
   292     }
       
   293 #endif
       
   294     
       
   295     if (iap->connectionStatus() == WlanQtUtilsConnectionStatusConnected) {
       
   296         item->setIcon(HbIcon("qtg_small_online"));
       
   297     } else {
       
   298         item->setIcon(HbIcon("qtg_small_wlan"));
       
   299     }
       
   300     item->setSecondaryIcon(HbIcon(signalStrengthIconChoose(iap->signalStrength(), iap->securityMode())));
       
   301     QVariant data(iap->id());
       
   302     item->setData(data);
       
   303 
       
   304     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_WLANLISTITEMIAPCREATE_EXIT );
       
   305     return item;
       
   306 }
       
   307 
       
   308 HbListWidgetItem *WlanSnifferListView::wlanListItemApCreate(const WlanQtUtilsWlanAp *ap)
       
   309 {
       
   310     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_WLANLISTITEMAPCREATE_ENTRY );
       
   311     
       
   312     HbListWidgetItem* item = new HbListWidgetItem();
       
   313     item->setText(ap->ssid());
       
   314 #if 1                                       // TODO: How to show secure/open network info??
       
   315     if (ap->securityMode() == WlanQtUtilsWlanSecModeOpen) {
       
   316         item->setSecondaryText("Open");
       
   317     } else {
       
   318         item->setSecondaryText("Secure");            
       
   319     }
       
   320 #endif    
       
   321     item->setSecondaryIcon(HbIcon(signalStrengthIconChoose(ap->signalStrength(), ap->securityMode())));
       
   322     QVariant data;
       
   323     data.setValue(*ap);
       
   324     item->setData(data);
       
   325     
       
   326     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_WLANLISTITEMAPCREATE_EXIT );
       
   327     return item;
       
   328 }
       
   329 
       
   330 // End of File