bluetoothengine/btui/btcpplugin/btcpuisearchview.cpp
changeset 57 5ebadcda06cb
parent 51 625f43ae9362
child 67 16e4b9007960
--- a/bluetoothengine/btui/btcpplugin/btcpuisearchview.cpp	Fri Aug 06 16:43:21 2010 +0300
+++ b/bluetoothengine/btui/btcpplugin/btcpuisearchview.cpp	Fri Sep 03 16:17:59 2010 +0300
@@ -16,8 +16,9 @@
  */
 
 #include "btcpuisearchview.h"
-#include "btuiviewutil.h"
+#include <HbStyleLoader>
 #include <QtGui/QGraphicsLinearLayout>
+#include <HbAction>
 #include <HbInstance>
 #include <HbDocumentLoader>
 #include <HbDataForm>
@@ -31,41 +32,30 @@
 #include <QStringList>
 #include <QDebug>
 #include <bluetoothuitrace.h>
-#include "btcpuimainview.h"
-#include "btdelegatefactory.h"
 #include "btabstractdelegate.h"
 #include "btcpuisearchlistviewitem.h"
 #include "btuidevtypemap.h"
-
+#include "btuimodelsortfilter.h"
+#include "btuiviewutil.h"
+#include "btcpuiviewmgr.h"
 
 // docml to load
 const char* BTUI_SEARCHVIEW_DOCML = ":/docml/bt-search-view.docml";
 
-BtCpUiSearchView::BtCpUiSearchView(
-        BtSettingModel &settingModel, 
+BtcpuiSearchView::BtcpuiSearchView(BtSettingModel &settingModel, 
         BtDeviceModel &deviceModel, 
         QGraphicsItem *parent) :
-    BtCpUiBaseView(settingModel,deviceModel, parent), mAbstractDelegate(0), mBtuiModelSortFilter(0)
+    BtcpuiBaseView(settingModel,deviceModel, parent)
 {
     bool ret(false);
-    
-    mQuery = 0;
-    mLoader = 0;
-    mSoftKeyBackAction = 0;
-    
-    mMainView = (BtCpUiMainView *) parent;
-    mMainWindow = hbInstance->allMainWindows().first();
-    
-    mSoftKeyBackAction = new HbAction(Hb::BackNaviAction, this);
-    BTUI_ASSERT_X(mSoftKeyBackAction, "BtCpUiSearchView::BtCpUiSearchView", "can't create back action");
-    
+
     // Create view for the application.
     // Set the name for the view. The name should be same as the view's
     // name in docml.
     setObjectName("bt_search_view");
 
     mLoader = new HbDocumentLoader();
-    BTUI_ASSERT_X( mLoader != 0, "BtCpUiSearchView::BtCpUiSearchView", "can't create mLoader" );
+    BTUI_ASSERT_X( mLoader != 0, "BtcpuiSearchView::BtcpuiSearchView", "can't create mLoader" );
     // Pass the view to documentloader. Document loader uses this view
     // when docml is parsed, instead of creating new view.
     QObjectList objectList;
@@ -86,77 +76,79 @@
 
     mDeviceIcon=0;
     // can't use qobject_cast since HbIcon is not derived from QObject!
-    mDeviceIcon = qobject_cast<HbLabel *>( mLoader->findWidget( "icon" ) );  
-    BTUI_ASSERT_X( mDeviceIcon != 0, "BtCpUiSearchView::BtCpUiSearchView", "Device Icon not found" );
+    mDeviceIcon = qobject_cast<HbLabel *>( mLoader->findWidget( "searchIcon" ) );  
+    BTUI_ASSERT_X( mDeviceIcon != 0, "BtcpuiSearchView::BtcpuiSearchView", "Device Icon not found" );
     
     mDataForm=0;
-    mDataForm = qobject_cast<HbDataForm *>( mLoader->findWidget( "dataForm" ) );
-    BTUI_ASSERT_X( mDataForm != 0, "BtCpUiSearchView::BtCpUiSearchView", "dataForm not found" );
+    mDataForm = qobject_cast<HbDataForm *>( mLoader->findWidget( "searchHeading" ) );
+    BTUI_ASSERT_X( mDataForm != 0, "BtcpuiSearchView::BtcpuiSearchView", "dataForm not found" );
     mDataForm->setHeading(hbTrId("txt_bt_subhead_bluetooth_found_devices"));
 
     mLabelSearching=0;
-    mLabelSearching = qobject_cast<HbLabel *>( mLoader->findWidget( "label_searching" ) );
-    BTUI_ASSERT_X( mLabelSearching != 0, "BtCpUiSearchView::BtCpUiSearchView", "Searching not found" );
+    mLabelSearching = qobject_cast<HbLabel *>( mLoader->findWidget( "searchStatus" ) );
+    BTUI_ASSERT_X( mLabelSearching != 0, "BtcpuiSearchView::BtcpuiSearchView", "Searching not found" );
     mLabelSearching->setPlainText(hbTrId("txt_bt_subhead_searching"));
     
     mDeviceList=0;
     mDeviceList = qobject_cast<HbListView *>( mLoader->findWidget( "deviceList" ) );
-    BTUI_ASSERT_X( mDeviceList != 0, "BtCpUiSearchView::BtCpUiSearchView", "Device List not found" );   
+    BTUI_ASSERT_X( mDeviceList != 0, "BtcpuiSearchView::BtcpuiSearchView", "Device List not found" );   
     
-    
+    ret = connect(mDeviceList,
+            SIGNAL(longPressed(HbAbstractViewItem*, QPointF)), this,
+            SLOT(showContextMenu(HbAbstractViewItem*, QPointF)));
+    BTUI_ASSERT_X( ret, "bt-search-view", "deviceSelected can't connect" ); 
+
     mDeviceList->setSelectionMode( HbAbstractItemView::SingleSelection );
     
     // read landscape orientation section from docml file if needed
     mOrientation = mMainWindow->orientation();
     
     if (mOrientation == Qt::Horizontal) {
-        mLoader->load(BTUI_SEARCHVIEW_DOCML, "landscape_ui", &ok);
-        BTUI_ASSERT_X( ok, "BtCpUiSearchView::BtCpUiSearchView", "Invalid docml file: landscape section problem" );
+        mLoader->load(BTUI_SEARCHVIEW_DOCML, "landscape", &ok);
+        BTUI_ASSERT_X( ok, "BtcpuiSearchView::BtcpuiSearchView", "Invalid docml file: landscape section problem" );
     } else {
-        mLoader->load(BTUI_SEARCHVIEW_DOCML, "portrait_ui", &ok);
-        BTUI_ASSERT_X( ok, "BtCpUiSearchView::BtCpUiSearchView", "Invalid docml file: portrait section problem" );        
+        mLoader->load(BTUI_SEARCHVIEW_DOCML, "portrait", &ok);
+        BTUI_ASSERT_X( ok, "BtcpuiSearchView::BtcpuiSearchView", "Invalid docml file: portrait section problem" );        
     }
 
     // listen for orientation changes
     ret = connect(mMainWindow, SIGNAL(orientationChanged(Qt::Orientation)),
             this, SLOT(changeOrientation(Qt::Orientation)));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView()", "connect orientationChanged() failed");
+    BTUI_ASSERT_X( ret, "BtcpuiSearchView::BtcpuiSearchView()", "connect orientationChanged() failed");
     
     // load tool bar actions
     mViewBy = static_cast<HbAction*>( mLoader->findObject( "viewByAction" ) );
-    BTUI_ASSERT_X( mViewBy, "BtCpUiSearchView::BtCpUiSearchView", "view by action missing" ); 
+    BTUI_ASSERT_X( mViewBy, "BtcpuiSearchView::BtcpuiSearchView", "view by action missing" ); 
     ret = connect(mViewBy, SIGNAL(triggered()), this, SLOT(viewByDeviceTypeDialog()));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView", "viewByAction can't connect" ); 
+    BTUI_ASSERT_X( ret, "BtcpuiSearchView::BtcpuiSearchView", "viewByAction can't connect" ); 
 
     mStop = static_cast<HbAction*>( mLoader->findObject( "stopAction" ) );
-    BTUI_ASSERT_X( mStop, "BtCpUiSearchView::BtCpUiSearchView", "stopAction missing" ); 
+    BTUI_ASSERT_X( mStop, "BtcpuiSearchView::BtcpuiSearchView", "stopAction missing" ); 
     ret = connect(mStop, SIGNAL(triggered()), this, SLOT(stopSearching()));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView", "stopAction can't connect" ); 
+    BTUI_ASSERT_X( ret, "BtcpuiSearchView::BtcpuiSearchView", "stopAction can't connect" ); 
     mStop->setEnabled(true);
     
     mRetry = static_cast<HbAction*>( mLoader->findObject( "retryAction" ) );
-    BTUI_ASSERT_X( mRetry, "BtCpUiSearchView::BtCpUiSearchView", "retryAction missing" ); 
+    BTUI_ASSERT_X( mRetry, "BtcpuiSearchView::BtcpuiSearchView", "retryAction missing" ); 
     ret = connect(mRetry, SIGNAL(triggered()), this, SLOT(retrySearch()));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView", "retryAction can't connect" ); 
+    BTUI_ASSERT_X( ret, "BtcpuiSearchView::BtcpuiSearchView", "retryAction can't connect" ); 
     // Disable for initial search
     mRetry->setEnabled(false);
     
     // load menu
     HbMenu *optionsMenu = qobject_cast<HbMenu *>(mLoader->findWidget("viewMenu"));
-    BTUI_ASSERT_X( optionsMenu != 0, "BtCpUiSearchView::BtCpUiSearchView", "Options menu not found" );   
+    BTUI_ASSERT_X( optionsMenu != 0, "BtcpuiSearchView::BtcpuiSearchView", "Options menu not found" );   
     this->setMenu(optionsMenu);      
     
     ret = connect(mDeviceList, SIGNAL(activated(QModelIndex)), this, SLOT(deviceSelected(QModelIndex)));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView", "deviceSelected can't connect" ); 
+    BTUI_ASSERT_X( ret, "BtcpuiSearchView::BtcpuiSearchView", "deviceSelected can't connect" ); 
     
-    // initialize device type list for "view by" option
-    // Note:  this list needs to be in the same order as enum devTypeSelectionList
-    mDevTypeList << hbTrId("txt_bt_list_audio_devices")
-            << hbTrId("txt_bt_list_computers") 
-            << hbTrId("txt_bt_list_input_devices") 
-            << hbTrId("txt_bt_list_phones") 
-            << hbTrId("txt_bt_list_other_devices");
-    
+    bool regStatus(false);
+    regStatus = HbStyleLoader::registerFilePath(":/docml/btcpuisearchlistviewitem.widgetml");
+    BTUI_ASSERT_X( regStatus, "BtcpuiSearchView::BtcpuiSearchView", "registerFilePath Widgetml Failed" );
+    regStatus = HbStyleLoader::registerFilePath(":/docml/btcpuisearchlistviewitem.css");
+    BTUI_ASSERT_X( regStatus, "BtcpuiSearchView::BtcpuiSearchView", "registerFilePath CSS Failed" );
+
     // initialize sort model & create
     // inquiry delegate
     // Sort is set to dynamic sort filter = true in the class, will sort automatically
@@ -165,6 +157,7 @@
     mBtuiModelSortFilter->addDeviceMajorFilter(
             BtuiDevProperty::InRange, BtuiModelSortFilter::AtLeastMatch);
     mDeviceList->setModel(mBtuiModelSortFilter);
+    mDeviceList->setLayoutName("custom-list");
     // add sort order to show devices in the order they are found
     mBtuiModelSortFilter->setSortRole(BtDeviceModel::SeqNumRole);
     // Sort according to the first column (and the only column) in the listview - sorted according SeqNumRole
@@ -173,34 +166,17 @@
     BtCpUiSearchListViewItem *prototype = new BtCpUiSearchListViewItem(mDeviceList);
     prototype->setModelSortFilter(mBtuiModelSortFilter);
     mDeviceList->setItemPrototype(prototype);
-
-    // Create the inquiry delegate.
-    mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::Inquiry, mSettingModel, mDeviceModel );
-    
-
 }
 
-BtCpUiSearchView::~BtCpUiSearchView()
+BtcpuiSearchView::~BtcpuiSearchView()
 {
     delete mLoader; // Also deletes all widgets that it constructed.
-    
-    setNavigationAction(0);
-    disconnect( mSoftKeyBackAction );
-    delete mSoftKeyBackAction;
-    
-    if(mAbstractDelegate) {
-        disconnect(mAbstractDelegate);   
-        delete mAbstractDelegate;
-    }
-    if(mBtuiModelSortFilter) {
-        delete mBtuiModelSortFilter;
-    }
-    if ( mQuery ) {
-        delete mQuery;
-    }
+    delete mBtuiModelSortFilter;
+    HbStyleLoader::unregisterFilePath(":/docml/btcpuisearchlistviewitem.widgetml");
+    HbStyleLoader::unregisterFilePath(":/docml/btcpuisearchlistviewitem.css");
 }
 
-void BtCpUiSearchView::changeOrientation( Qt::Orientation orientation )
+void BtcpuiSearchView::changeOrientation( Qt::Orientation orientation )
 {
     // ToDo:  this does not handle redrawing list view items differently for portrait/landscape
     bool ok = false;
@@ -208,181 +184,119 @@
 
     if( orientation == Qt::Vertical ) {
         // load "portrait" section
-        mLoader->load( BTUI_SEARCHVIEW_DOCML, "portrait_ui", &ok );
-        BTUI_ASSERT_X( ok, "BtCpUiSearchView::changeOrientation", "Invalid docml file: portrait section problem" );
+        mLoader->load( BTUI_SEARCHVIEW_DOCML, "portrait", &ok );
+        BTUI_ASSERT_X( ok, "BtcpuiSearchView::changeOrientation", "Invalid docml file: portrait section problem" );
     } else {
         // load "landscape" section
-        mLoader->load( BTUI_SEARCHVIEW_DOCML, "landscape_ui", &ok );
-        BTUI_ASSERT_X( ok, "BtCpUiSearchView::changeOrientation", "Invalid docml file: landscape section problem" );
+        mLoader->load( BTUI_SEARCHVIEW_DOCML, "landscape", &ok );
+        BTUI_ASSERT_X( ok, "BtcpuiSearchView::changeOrientation", "Invalid docml file: landscape section problem" );
     }
 }
 
-
-void BtCpUiSearchView::viewByDeviceTypeDialog()
-{
-    if ( !mQuery ) {
-        mQuery = new HbSelectionDialog;
-        mQuery->setStringItems(mDevTypeList, 0);
-        mQuery->setSelectionMode(HbAbstractItemView::MultiSelection);
-    
-        QList<QVariant> current;
-        current.append(QVariant(0));
-        mQuery->setSelectedItems(current);
-    
-        // Set the heading for the dialog.
-        HbLabel *headingLabel = new HbLabel(hbTrId("txt_bt_title_show"), mQuery);
-        mQuery->setHeadingWidget(headingLabel);
-    }
-    mQuery->open(this,SLOT(viewByDialogClosed(HbAction*)));
-}
 /*!
    Callback for HbSelectionDialog closing
+   ReImplemented Slot from Base class
 
  */
-void BtCpUiSearchView::viewByDialogClosed(HbAction* action)
+void BtcpuiSearchView::viewByDialogClosed(HbAction* action)
+{
+    int devTypesWanted = 0;
+    
+    devTypesWanted = selectedDeviceTypes(action);
+    
+    if (devTypesWanted) {
+        mBtuiModelSortFilter->clearDeviceMajorFilters();
+        mBtuiModelSortFilter->addDeviceMajorFilter(BtuiDevProperty::InRange,
+                        BtuiModelSortFilter::AtLeastMatch);   // device must be in range
+        mBtuiModelSortFilter->addDeviceMajorFilter(devTypesWanted,
+                BtuiModelSortFilter::RoughMatch);             // device can be any one of selected ones
+    }
+}
+
+void BtcpuiSearchView::stopSearching()
 {
-    disconnect( mQuery );  // remove slot
-    if (action == mQuery->actions().first()) {  // user pressed "Ok"
-        // Get selected items.
-        QList<QVariant> selections;
-        selections = mQuery->selectedItems();
-        
-        int devTypesWanted = 0;
-        
-        for (int i=0; i < selections.count(); i++) {
-            switch (selections.at(i).toInt()) {
-            case BtUiDevAudioDevice:
-                devTypesWanted |= BtuiDevProperty::AVDev;
-                break;
-            case BtUiDevComputer:
-                devTypesWanted |= BtuiDevProperty::Computer;
-                break;
-            case BtUiDevInputDevice:
-                devTypesWanted |= BtuiDevProperty::Peripheral;
-                break;
-            case BtUiDevPhone:
-                devTypesWanted |= BtuiDevProperty::Phone;
-                break;
-            case BtUiDevOtherDevice:
-                devTypesWanted |= (BtuiDevProperty::LANAccessDev |
-                        BtuiDevProperty::Toy |
-                        BtuiDevProperty::WearableDev |
-                        BtuiDevProperty::ImagingDev |
-                        BtuiDevProperty::HealthDev |
-                        BtuiDevProperty::UncategorizedDev);
-                break;
-            default:
-                // should never get here
-                BTUI_ASSERT_X(false, "BtCpUiSearchView::viewByDialogClosed()", 
-                        "wrong device type in viewBy dialog!");
-            }
-        }
-        if (devTypesWanted) {
-            mBtuiModelSortFilter->clearDeviceMajorFilters();
-            mBtuiModelSortFilter->addDeviceMajorFilter(BtuiDevProperty::InRange,
-                            BtuiModelSortFilter::AtLeastMatch);   // device must be in range
-            mBtuiModelSortFilter->addDeviceMajorFilter(devTypesWanted,
-                    BtuiModelSortFilter::RoughMatch);             // device can be any one of selected ones
+    // Stop searching
+    if (mDelegate) {
+        mDelegate->cancel();
+        // update the states of view items 
+        deviceSearchCompleted(KErrNone);
+    }
+}
+
+void BtcpuiSearchView::take(BtAbstractDelegate *delegate)
+{
+    mDelegate = delegate;
+    if (mDelegate) {
+        disconnect(mDelegate, 0, 0, 0);
+        if (mDelegate->supportedEditorTypes() & BtDelegate::Inquiry) {
+            connect(mDelegate, SIGNAL(delegateCompleted(int,BtAbstractDelegate*)),
+                    this, SLOT(deviceSearchCompleted(int)));
+        } else {
+            connect(mDelegate, SIGNAL(delegateCompleted(int,BtAbstractDelegate*)),
+                this, SLOT(handleDelegateCompleted(int)));
         }
     }
 }
 
-void BtCpUiSearchView::stopSearching()
+void BtcpuiSearchView::startSearchDelegate()
 {
-    // Stop searching
-
-    // Stop search delegate
-    mAbstractDelegate->cancel();
-    // reset view 
-    deviceSearchCompleted(KErrNone);      
+    (void) createExecuteDelegate(BtDelegate::Inquiry, 
+            this, SLOT(deviceSearchCompleted(int)), QVariant());
 }
 
-void BtCpUiSearchView::startSearchDelegate ()
-{
-    bool ret = false;
-    
-    // Connect to the signal from the BtDelegateInquiry for completion of the search request
-    ret = connect(mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(searchDelegateCompleted(int)));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView", "searchDelegateCompleted can't connect" );
-    
-    // Connect to the signal from the BtuiModel when the search has been completed.
-    ret = connect(mDeviceModel, SIGNAL(deviceSearchCompleted(int)), this, SLOT(deviceSearchCompleted(int)));
-    BTUI_ASSERT_X( ret, "BtCpUiSearchView::BtCpUiSearchView", "deviceSearchCompleted can't connect" );     
-
-    mAbstractDelegate->exec(QVariant());
-}
-
-void BtCpUiSearchView::retrySearch()
+void BtcpuiSearchView::retrySearch()
 {
     // Retry search
-
     // Change label and buttons to reflect status
     mLabelSearching->setPlainText(hbTrId("txt_bt_subhead_searching"));
     mRetry->setEnabled(false);
     mStop->setEnabled(true);
     
     // Make use of the delegate and start the search.
-    startSearchDelegate ();
-}
-
-void BtCpUiSearchView::setSoftkeyBack()
-{
-    if (navigationAction() != mSoftKeyBackAction) {
-        setNavigationAction(mSoftKeyBackAction);
-        connect( mSoftKeyBackAction, SIGNAL(triggered()), this, SLOT(switchToPreviousView()) );
-    }
+    startSearchDelegate();
 }
 
-void BtCpUiSearchView::switchToPreviousView()
-{
-    BTUI_ASSERT_X(mMainView, "BtCpUiSearchView::switchToPreviousView", "invalid mMainView");
-    mMainView->switchToPreviousView();
-}
-
-void BtCpUiSearchView::activateView( const QVariant& value, bool fromBackButton )
+void BtcpuiSearchView::activateView( const QVariant& value, bool backNavi)
 {
     Q_UNUSED(value);
-
+    if (!backNavi) {
+        retrySearch();
+    }
+    
     // ToDo:  consider whether orientation should be updated here rather than when orientation really changes;
     // advantage:  if orientation changes temporarily in another view, but returns to previous setting before
     // search is reactived, there is no processing overhead
-    
-    setSoftkeyBack();
-            
-    if ( !fromBackButton ) {
-        // Sets the label and toolbar buttons
-        retrySearch();
-    }
 }
 
-void BtCpUiSearchView::deactivateView()
+void BtcpuiSearchView::deactivateView()
 {
-    mAbstractDelegate->cancel();          // Stop searching when leaving view
-    deviceSearchCompleted(KErrNone);      // reset view 
+    stopSearching();
 }
-
-void BtCpUiSearchView::searchDelegateCompleted(int error)
+void BtcpuiSearchView::deviceSearchCompleted(int error)
 {
     Q_UNUSED(error);
-    
-}
-
-void BtCpUiSearchView::deviceSearchCompleted(int error)
-{
-    //TODO - handle error.
-    Q_UNUSED(error);
-    
     mLabelSearching->setPlainText(hbTrId("txt_bt_subhead_search_done"));
     mRetry->setEnabled(true);
     mStop->setEnabled(false);
-    
-    // disconnect signals
-    disconnect(mAbstractDelegate);
-    disconnect(mDeviceModel);
+    handleDelegateCompleted(error, mDelegate);
 }
 
-void BtCpUiSearchView::deviceSelected(const QModelIndex& modelIndex)
+void BtcpuiSearchView::createContextMenuActions(int majorRole)
 {
-    QModelIndex index = mBtuiModelSortFilter->mapToSource(modelIndex);
-    static_cast<BtCpUiMainView*>(mMainView)->goToDeviceView(index);
+    if (majorRole & BtuiDevProperty::Connected) {
+        if(majorRole & BtuiDevProperty::AVDev) {
+            mContextMenu->addAction(hbTrId("txt_bt_menu_disconnect_audio"));
+        }
+        else {
+            mContextMenu->addAction(hbTrId("txt_bt_menu_disconnect"));
+        }
+    }
+    else {
+        if(majorRole & BtuiDevProperty::AVDev) {
+            mContextMenu->addAction(hbTrId("txt_bt_menu_connect_audio"));
+        }
+        else {
+            mContextMenu->addAction(hbTrId("txt_bt_menu_connect"));
+        }
+    }  
 }