wlanutilities/wlansniffer/src/wlansnifferlistview.cpp
changeset 19 10810c91db26
child 25 001d4d78e350
equal deleted inserted replaced
3:ff3b37722600 19:10810c91db26
       
     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     mIapItemMenuOpen(false)
       
    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     if (mIapItemMenuOpen == false) {
       
   117         QList<WlanQtUtilsWlanAp*> aps;
       
   118         QList<WlanQtUtilsWlanIap*> iaps;
       
   119         
       
   120         mAppRef->wlanQtUtils()->availableWlanAps(iaps, aps);
       
   121         
       
   122         mWlanList->clear();
       
   123     
       
   124         // Loop the IAPs and APs and create the list items (lists are ordered already by Wlan Qt Utilities)
       
   125         foreach (WlanQtUtilsWlanIap *iap, iaps) {
       
   126             mWlanList->addItem(wlanListItemIapCreate(iap));
       
   127         }
       
   128         // Loop the AP's and create the list items
       
   129         foreach (WlanQtUtilsWlanAp *ap, aps) {
       
   130             mWlanList->addItem(wlanListItemApCreate(ap));
       
   131         }
       
   132     }
       
   133 
       
   134     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_UPDATE_EXIT );
       
   135 }
       
   136 
       
   137 void WlanSnifferListView::updateConnectionOpened(int iapId)
       
   138 {
       
   139     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONOPENED_ENTRY );
       
   140     
       
   141     mStatusLabel->setPlainText(
       
   142         hbTrId("txt_occ_grid_connected_to_1").arg(mAppRef->wlanQtUtils()->iap(iapId)->name()));
       
   143     // Redraw the list so that the connected network moves on top
       
   144     update();
       
   145     mConnectingIapId = 0;
       
   146 
       
   147     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONOPENED_EXIT );
       
   148 }
       
   149 
       
   150 void WlanSnifferListView::updateConnectionClosed(int iapId)
       
   151 {
       
   152     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONCLOSED_ENTRY );
       
   153     
       
   154     (void)iapId;
       
   155     mStatusLabel->setPlainText(hbTrId("txt_occ_grid_not_connected"));
       
   156     mConnectingIapId = 0;
       
   157 
       
   158     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_UPDATECONNECTIONCLOSED_EXIT );
       
   159 }
       
   160 
       
   161 void WlanSnifferListView::handleListItemActivated(HbListWidgetItem *item)
       
   162 {
       
   163     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLELISTITEMACTIVATED_ENTRY );
       
   164     
       
   165     QVariant data = item->data();
       
   166     if (data.canConvert<int>()) {
       
   167         // Item was an IAP. Connect the IAP or show details view depending on connection state
       
   168         WlanQtUtilsIap *iap = mAppRef->wlanQtUtils()->iap(data.toInt());
       
   169         if (iap->connectionStatus() == WlanQtUtilsConnectionStatusConnected) {
       
   170             // Switch to details view
       
   171             emit detailsTriggered(data.toInt());
       
   172         } else {
       
   173             // Connect the IAP
       
   174             mConnectingIapId = iap->id();
       
   175             mStatusLabel->setPlainText(hbTrId("txt_occ_grid_connecting_to_1").arg(iap->name()));
       
   176             mAppRef->wlanQtUtils()->connectIap(mConnectingIapId);
       
   177         }
       
   178     } else {
       
   179         // Item has to be a WLAN AP, which can't already be connected. Create an IAP and connect it.
       
   180         Q_ASSERT(data.canConvert<WlanQtUtilsWlanAp>());
       
   181         WlanQtUtilsWlanAp ap = data.value<WlanQtUtilsWlanAp>();
       
   182         mConnectingIapId = mAppRef->wlanQtUtils()->createWlanIap(&ap);
       
   183         WlanQtUtilsIap *iap = mAppRef->wlanQtUtils()->iap(mConnectingIapId);
       
   184         Q_ASSERT(iap);
       
   185         mStatusLabel->setPlainText(hbTrId("txt_occ_grid_connecting_to_1").arg(iap->name()));
       
   186         mAppRef->wlanQtUtils()->connectIap(mConnectingIapId);
       
   187     }
       
   188 
       
   189     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLELISTITEMACTIVATED_EXIT );
       
   190 }
       
   191 
       
   192 void WlanSnifferListView::handleListItemLongPressed(HbListWidgetItem *item, const QPointF &coords)
       
   193 {
       
   194     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLELISTITEMLONGPRESSED_ENTRY );
       
   195     
       
   196     // Disable list refreshing while context menu is shown
       
   197     // Todo: Propably a more elegant way to do this is needed..
       
   198     mIapItemMenuOpen = true;
       
   199     
       
   200     (void)item;
       
   201     HbMenu *contextMenu = new HbMenu();
       
   202     HbAction *actionDetails = contextMenu->addAction(hbTrId("txt_occ_menu_details"));
       
   203     HbAction *actionSettings = contextMenu->addAction(hbTrId("txt_occ_menu_network_settings"));
       
   204     HbAction *actionDisable = contextMenu->addAction(hbTrId("txt_occ_menu_disable_network"));
       
   205     
       
   206     // Add disconnect option if the IAP is connected
       
   207     HbAction *actionDisconnect = 0;
       
   208     WlanQtUtilsIap *iap = 0;
       
   209     QVariant data = item->data();
       
   210     if (data.canConvert<int>()) {
       
   211         // Item was an IAP.
       
   212         iap = mAppRef->wlanQtUtils()->iap(data.toInt());
       
   213         if (iap->connectionStatus() == WlanQtUtilsConnectionStatusConnected) {
       
   214             actionDisconnect = contextMenu->addAction(hbTrId("txt_common_menu_disconnect"));
       
   215         }
       
   216     }
       
   217     
       
   218     // Show the menu
       
   219     HbAction *selectedAction = contextMenu->exec(coords);
       
   220     // Re-enable list refreshing
       
   221     mIapItemMenuOpen = false;
       
   222 
       
   223     if (selectedAction) {
       
   224         // Handle the "Disconnect" menu selection
       
   225         if (selectedAction == actionDisconnect) {
       
   226             mStatusLabel->setPlainText(hbTrId("txt_occ_grid_not_connected"));
       
   227             mAppRef->wlanQtUtils()->disconnectIap(iap->id());
       
   228             // Redraw the list so that disconnected network is not shown anymore
       
   229             update();
       
   230         }
       
   231     }        
       
   232 
       
   233     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLELISTITEMLONGPRESSED_EXIT );
       
   234 }
       
   235 
       
   236 // Todo: This is a temporary solution - real WLAN status indication & listening
       
   237 // is implemented later on.
       
   238 void WlanSnifferListView::handleDisconnect()
       
   239 {
       
   240     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_HANDLEDISCONNECT_ENTRY );
       
   241     
       
   242     mStatusLabel->setPlainText(hbTrId("txt_occ_grid_not_connected"));
       
   243     // Redraw the list so that disconnected network is not shown anymore
       
   244     update();    
       
   245 
       
   246     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_HANDLEDISCONNECT_EXIT );
       
   247 }
       
   248 
       
   249 // Todo: no place for separate secure WLAN icon in the widget -> custom layout needed?
       
   250 // Todo: check the signal strength values
       
   251 QString WlanSnifferListView::signalStrengthIconChoose(int signalStrength, WlanQtUtilsWlanSecMode secMode)
       
   252 {
       
   253     (void)secMode;
       
   254     QString iconName;
       
   255     if (signalStrength <= WlanQtUtilsWlanSignalStrengthGood) {
       
   256         iconName = "qtg_small_signal_good";
       
   257     } else if (signalStrength <= WlanQtUtilsWlanSignalStrengthLow) {
       
   258         iconName = "qtg_small_signal_medium";
       
   259     } else {
       
   260         iconName = "qtg_small_signal_low";
       
   261     }
       
   262     
       
   263     return iconName;
       
   264 }
       
   265 
       
   266 HbListWidgetItem *WlanSnifferListView::wlanListItemIapCreate(const WlanQtUtilsWlanIap *iap)
       
   267 {
       
   268     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_WLANLISTITEMIAPCREATE_ENTRY );
       
   269     
       
   270     HbListWidgetItem* item = new HbListWidgetItem();
       
   271     item->setText(iap->name());
       
   272 #if 1                                       // TODO: How to show secure/open network info??
       
   273     if (iap->securityMode() == WlanQtUtilsWlanSecModeOpen) {
       
   274         item->setSecondaryText("Open");
       
   275     } else {
       
   276         item->setSecondaryText("Secure");            
       
   277     }
       
   278 #endif
       
   279     
       
   280     if (iap->connectionStatus() == WlanQtUtilsConnectionStatusConnected) {
       
   281         item->setIcon(HbIcon("qtg_small_online"));
       
   282     } else {
       
   283         item->setIcon(HbIcon("qtg_small_wlan"));
       
   284     }
       
   285     item->setSecondaryIcon(HbIcon(signalStrengthIconChoose(iap->signalStrength(), iap->securityMode())));
       
   286     QVariant data(iap->id());
       
   287     item->setData(data);
       
   288 
       
   289     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_WLANLISTITEMIAPCREATE_EXIT );
       
   290     return item;
       
   291 }
       
   292 
       
   293 HbListWidgetItem *WlanSnifferListView::wlanListItemApCreate(const WlanQtUtilsWlanAp *ap)
       
   294 {
       
   295     OstTraceFunctionEntry0( WLANSNIFFERLISTVIEW_WLANLISTITEMAPCREATE_ENTRY );
       
   296     
       
   297     HbListWidgetItem* item = new HbListWidgetItem();
       
   298     item->setText(ap->ssid());
       
   299 #if 1                                       // TODO: How to show secure/open network info??
       
   300     if (ap->securityMode() == WlanQtUtilsWlanSecModeOpen) {
       
   301         item->setSecondaryText("Open");
       
   302     } else {
       
   303         item->setSecondaryText("Secure");            
       
   304     }
       
   305 #endif    
       
   306     item->setSecondaryIcon(HbIcon(signalStrengthIconChoose(ap->signalStrength(), ap->securityMode())));
       
   307     QVariant data;
       
   308     data.setValue(*ap);
       
   309     item->setData(data);
       
   310     
       
   311     OstTraceFunctionExit0( WLANSNIFFERLISTVIEW_WLANLISTITEMAPCREATE_EXIT );
       
   312     return item;
       
   313 }
       
   314 
       
   315 // End of File