bluetoothengine/btui/btcpplugin/btcpuideviceview.cpp
changeset 31 a0ea99b6fa53
child 40 997690c3397a
child 42 b72428996822
equal deleted inserted replaced
30:df7a93ede42e 31:a0ea99b6fa53
       
     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 "btcpuideviceview.h"
       
    19 #include "btuiviewutil.h"
       
    20 #include <QtGui/QGraphicsLinearLayout>
       
    21 #include <HbInstance>
       
    22 #include <hbdocumentloader.h>
       
    23 #include <hbdataform.h>
       
    24 #include <hbgroupbox.h>
       
    25 #include <hbpushbutton.h>
       
    26 #include <hblabel.h>
       
    27 #include <hbtextedit.h>
       
    28 #include <hblistview.h>
       
    29 #include <hbmenu.h>
       
    30 #include <qstring>
       
    31 #include <qstringlist>
       
    32 #include <qdebug>
       
    33 #include <bluetoothuitrace.h>
       
    34 #include "btcpuimainview.h"
       
    35 #include <btabstractdelegate.h>
       
    36 #include <btdelegatefactory.h>
       
    37 #include <QModelIndex>
       
    38 
       
    39 // docml to load
       
    40 const char* BTUI_DEVICEVIEW_DOCML = ":/docml/bt-device-view.docml";
       
    41 
       
    42 
       
    43 BtCpUiDeviceView::BtCpUiDeviceView(
       
    44         BtSettingModel &settingModel, 
       
    45         BtDeviceModel &deviceModel, 
       
    46         QGraphicsItem *parent) :
       
    47     BtCpUiBaseView(settingModel,deviceModel,parent),
       
    48     mPairStatus(false), mConnectStatus(false), mConnectable(false), mAbstractDelegate(0)   
       
    49 {
       
    50     mDeviceIndex = QModelIndex();//is it needed to initialize mIndex???
       
    51     
       
    52     mMainView = (BtCpUiMainView *) parent;
       
    53     
       
    54     mMainWindow = hbInstance->allMainWindows().first();
       
    55     
       
    56     mSoftKeyBackAction = new HbAction(Hb::BackNaviAction, this);
       
    57     BTUI_ASSERT_X(mSoftKeyBackAction, "BtCpUiBaseView::BtCpUiBaseView", "can't create back action");
       
    58 
       
    59     // read view info from docml file
       
    60 
       
    61     // Create view for the application.
       
    62     // Set the name for the view. The name should be same as the view's
       
    63     // name in docml.
       
    64     setObjectName("bt_device_view");
       
    65 
       
    66     QObjectList objectList;
       
    67     objectList.append(this);
       
    68     // Pass the view to documentloader. Document loader uses this view
       
    69     // when docml is parsed, instead of creating new view.
       
    70     mLoader = new HbDocumentLoader();
       
    71     mLoader->setObjectTree(objectList);
       
    72     
       
    73     bool ret = false;
       
    74 
       
    75     bool ok = false;
       
    76     mLoader->load( BTUI_DEVICEVIEW_DOCML, &ok );
       
    77     // Exit if the file format is invalid
       
    78     BTUI_ASSERT_X( ok, "bt-device-view", "Invalid docml file" );
       
    79     
       
    80     // Set title for the control panel
       
    81     // ToDo:  check if deprecated API
       
    82     setTitle("Control Panel");
       
    83 
       
    84     // assign automatically created widgets to local variables
       
    85     
       
    86     mGroupBox = 0;
       
    87     mGroupBox = qobject_cast<HbGroupBox *>( mLoader->findWidget( "groupBox_deviceView" ) );
       
    88     BTUI_ASSERT_X( mGroupBox != 0, "bt-device-view", "Device groupbox not found" );
       
    89     
       
    90     mDeviceIcon=0;
       
    91     //can't use qobject_cast since HbIcon is not derived from QObject!
       
    92     mDeviceIcon = qobject_cast<HbLabel *>( mLoader->findWidget( "deviceIcon" ) );  
       
    93     BTUI_ASSERT_X( mDeviceIcon != 0, "bt-device-view", "Device Icon not found" );
       
    94     
       
    95     mDeviceName=0;
       
    96     mDeviceName = qobject_cast<HbTextEdit *>( mLoader->findWidget( "deviceName" ) );
       
    97     BTUI_ASSERT_X( mDeviceName != 0, "bt-device-view", "Device Name not found" );
       
    98     ret = connect(mDeviceName, SIGNAL(editingFinished ()), this, SLOT(changeBtDeviceName()));
       
    99     
       
   100     mDeviceCategory=0;
       
   101     mDeviceCategory = qobject_cast<HbLabel *>( mLoader->findWidget( "deviceCategory" ) );  
       
   102     BTUI_ASSERT_X( mDeviceCategory != 0, "bt-device-view", "Device Category not found" );
       
   103     
       
   104     mDeviceStatus=0;
       
   105     mDeviceStatus = qobject_cast<HbLabel *>( mLoader->findWidget( "deviceStatus" ) );  
       
   106     BTUI_ASSERT_X( mDeviceStatus != 0, "bt-device-view", "Device status not found" );
       
   107     
       
   108     mPair_Unpair=0;
       
   109     mPair_Unpair = qobject_cast<HbPushButton *>( mLoader->findWidget( "pushButton_0" ) );
       
   110     BTUI_ASSERT_X( mPair_Unpair != 0, "bt-device-view", "pair/unpair button not found" );
       
   111     ret =  connect(mPair_Unpair, SIGNAL(clicked()), this, SLOT(pairUnpair()));
       
   112     BTUI_ASSERT_X( ret, "BtCpUiDeviceView::BtCpUiDeviceView", "can't connect pair button" );
       
   113  
       
   114     mConnect_Disconnect=0;
       
   115     mConnect_Disconnect = qobject_cast<HbPushButton *>( mLoader->findWidget( "pushButton_1" ) );
       
   116     BTUI_ASSERT_X( mConnect_Disconnect != 0, "bt-device-view", "connect/disconnect button not found" );
       
   117     ret =  connect(mConnect_Disconnect, SIGNAL(clicked()), this, SLOT(connectDisconnect()));
       
   118     BTUI_ASSERT_X( ret, "BtCpUiDeviceView::BtCpUiDeviceView", "can't connect disconnect button" );
       
   119       
       
   120     mDeviceSetting = 0;
       
   121     mDeviceSetting = qobject_cast<HbPushButton *>( mLoader->findWidget( "pushButton_2" ) );
       
   122     BTUI_ASSERT_X( mDeviceSetting != 0, "bt-device-view", "settings button not found" );
       
   123         
       
   124     // read landscape orientation section from docml file if needed
       
   125     // mOrientation = ((BTUIViewManager*)parent)->orientation();
       
   126     mOrientation = Qt::Vertical;
       
   127     if (mOrientation == Qt::Horizontal) {
       
   128         mLoader->load(BTUI_DEVICEVIEW_DOCML, "landscape", &ok);
       
   129         BTUI_ASSERT_X( ok, "bt-device-view", "Invalid docml file: landscape section problem" );
       
   130     }
       
   131  
       
   132 }
       
   133 
       
   134 BtCpUiDeviceView::~BtCpUiDeviceView()
       
   135 {
       
   136     setNavigationAction(0);
       
   137     delete mSoftKeyBackAction;
       
   138     if(mAbstractDelegate)
       
   139     {
       
   140         delete mAbstractDelegate;
       
   141         mAbstractDelegate = 0;
       
   142     }
       
   143 }
       
   144 
       
   145 
       
   146 void BtCpUiDeviceView::setSoftkeyBack()
       
   147 {
       
   148     if (navigationAction() != mSoftKeyBackAction) {
       
   149         setNavigationAction(mSoftKeyBackAction);
       
   150         connect( mSoftKeyBackAction, SIGNAL(triggered()), this, SLOT(switchToPreviousView()) );
       
   151     }
       
   152 }
       
   153 
       
   154 void BtCpUiDeviceView::switchToPreviousView()
       
   155 {
       
   156     BTUI_ASSERT_X(mMainView, "BtCpUiSearchView::switchToPreviousView", "invalid mMainView");
       
   157     mMainView->switchToPreviousView();
       
   158 }
       
   159 
       
   160 void BtCpUiDeviceView::activateView( const QVariant& value, int cmdId )
       
   161 {
       
   162     Q_UNUSED(cmdId);  
       
   163     
       
   164     setSoftkeyBack();
       
   165     
       
   166     QModelIndex index = value.value<QModelIndex>();
       
   167     mDeviceBdAddr = (mDeviceModel->data(index, BtDeviceModel::ReadableBdaddrRole));
       
   168     
       
   169     //activate view is called when device is selected
       
   170     clearViewData();
       
   171     updateDeviceData();
       
   172     
       
   173     bool ret(false);
       
   174     ret=connect(mDeviceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
       
   175            this, SLOT(updateDeviceData()));
       
   176     BTUI_ASSERT_X( ret, "Btui, BtCpUiDeviceView::activateView", "dataChanged() connect failed");
       
   177 }
       
   178 
       
   179 void BtCpUiDeviceView::deactivateView()
       
   180 {
       
   181 }
       
   182 
       
   183 void BtCpUiDeviceView::clearViewData()
       
   184 {
       
   185     mDeviceIcon->clear();
       
   186     mDeviceCategory->clear();
       
   187     mDeviceStatus->clear();
       
   188     
       
   189     mPairStatus = false;
       
   190     mConnectStatus = false;
       
   191     mConnectable = false;
       
   192 }
       
   193     
       
   194 void BtCpUiDeviceView::updateDeviceData()
       
   195 {
       
   196     QModelIndex localIndex = mSettingModel->index( BtSettingModel::LocalBtNameRow, 0);
       
   197     QString localName = (mSettingModel->data(localIndex,BtSettingModel::settingDisplayRole)).toString();
       
   198     QString groupBoxTitle (tr("Bluetooth-"));
       
   199     mGroupBox->setHeading(groupBoxTitle.append(localName));
       
   200     //Get the QModelIndex of the device using the device BDAddres
       
   201     QModelIndex start = mDeviceModel->index(0,0);
       
   202     QModelIndexList indexList = mDeviceModel->match(start,BtDeviceModel::ReadableBdaddrRole, mDeviceBdAddr);
       
   203     mDeviceIndex = indexList.at(0);
       
   204     
       
   205     //populate device view with device data fetched from UiModel
       
   206     QString deviceName = (mDeviceModel->data(mDeviceIndex, 
       
   207              BtDeviceModel::NameAliasRole)).toString(); 
       
   208     mDeviceName->setPlainText(deviceName);
       
   209      
       
   210     int cod = (mDeviceModel->data(mDeviceIndex,BtDeviceModel::CoDRole)).toInt();
       
   211      
       
   212     int majorRole = (mDeviceModel->data(mDeviceIndex,BtDeviceModel::MajorPropertyRole)).toInt();
       
   213     int minorRole = (mDeviceModel->data(mDeviceIndex,BtDeviceModel::MinorPropertyRole)).toInt();
       
   214     
       
   215 	setDeviceCategory(cod, majorRole, minorRole);
       
   216     setDeviceStatus(majorRole);
       
   217     setTextAndVisibilityOfButtons();
       
   218 }
       
   219 
       
   220 void BtCpUiDeviceView::setDeviceCategory(int cod,int majorRole, int minorRole)
       
   221 {
       
   222     //TODO: change the hardcoded numeric value to enumerations
       
   223     if (cod)
       
   224     {
       
   225         if (majorRole & 0x00020000)
       
   226         {
       
   227             //this is a phone
       
   228             mDeviceCategory->setPlainText(tr("Phone"));
       
   229         }
       
   230         else if (majorRole & 0x00010000)
       
   231         {
       
   232             //this is a computer
       
   233             mDeviceCategory->setPlainText(tr("Computer"));
       
   234         
       
   235         }
       
   236         else if (majorRole & 0x00080000)
       
   237         {  
       
   238             //this is a A/V device
       
   239             //int minorRole = (mDeviceModel->data(mIndex,BtDeviceModel::MinorPropertyRole)).toInt();
       
   240             if ( minorRole & 0x00000002)
       
   241             {
       
   242                 //this is a Headset, it is possible to connect
       
   243                 mConnectable = true;
       
   244                 mDeviceCategory->setPlainText(tr("Headset"));
       
   245             }  
       
   246         }
       
   247         else 
       
   248         {
       
   249             mDeviceCategory->setPlainText(tr("Uncategorized Dev"));
       
   250         }
       
   251     
       
   252     }
       
   253     
       
   254     
       
   255 }
       
   256 void BtCpUiDeviceView::setDeviceStatus(int majorRole)
       
   257 {
       
   258     //TODO: change the hardcoded numeric value to enumerations
       
   259     QString deviceStatus;
       
   260     if (majorRole & 0x00000001)
       
   261     {
       
   262         deviceStatus = deviceStatus.append(tr("Paired"));
       
   263         mPairStatus = true;
       
   264     }
       
   265     else
       
   266     {
       
   267         mPairStatus = false;
       
   268     }
       
   269     
       
   270     if ((majorRole & 0x00000020)&& (mConnectable))
       
   271     {
       
   272         //if the device is connected and it is a headset NOTE! two phone can be paired but not be connected
       
   273         deviceStatus = deviceStatus.append(tr(", connected"));
       
   274         mConnectStatus = true;
       
   275     }
       
   276     else 
       
   277     {
       
   278         mConnectStatus = false;
       
   279     }
       
   280     mDeviceStatus->setPlainText(deviceStatus);
       
   281     
       
   282     
       
   283     
       
   284 }
       
   285 
       
   286 void BtCpUiDeviceView::setTextAndVisibilityOfButtons()
       
   287 {
       
   288     if (mPairStatus)
       
   289     {
       
   290         mPair_Unpair->setText(tr("Unpair"));
       
   291     }
       
   292     else
       
   293     {
       
   294         mPair_Unpair->setText(tr("Pair"));
       
   295     }
       
   296     
       
   297     if (mConnectable)
       
   298     {
       
   299         if (mConnectStatus)
       
   300         {
       
   301             mConnect_Disconnect->setText(tr("Disconnect"));
       
   302         }
       
   303         else
       
   304         {
       
   305             mConnect_Disconnect->setText(tr("Connect"));
       
   306         }
       
   307         
       
   308     }
       
   309     else
       
   310     {
       
   311         //it is not possible to connect, set the button invisible
       
   312         mConnect_Disconnect->setVisible(false);
       
   313     }
       
   314     
       
   315     mDeviceSetting->setVisible(false);
       
   316 
       
   317 }
       
   318 
       
   319 void BtCpUiDeviceView::changeBtDeviceName()
       
   320 {
       
   321     /*
       
   322     if (!mAbstractDelegate) 
       
   323     {
       
   324         mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::DeviceName, mModel); 
       
   325         connect( mAbstractDelegate, SIGNAL(commandCompleted(int,QVariant)), this, SLOT(btNameDelegateCompleted(int,QVariant)) );
       
   326         mAbstractDelegate->exec(mDeviceNameEdit->text ());
       
   327     }
       
   328     */
       
   329     
       
   330 }
       
   331 
       
   332 void BtCpUiDeviceView::pairUnpair()
       
   333 {
       
   334     if (mPairStatus)
       
   335     {
       
   336         //if the device is paired, call unpairDevice() when the button is tabbed
       
   337         unpairDevice();
       
   338     }
       
   339     else
       
   340     {
       
   341         //if the device is unpaired, call pairDevice() when the button is tabbed
       
   342         pairDevice();
       
   343 
       
   344     }
       
   345     
       
   346     
       
   347 }
       
   348 
       
   349 void BtCpUiDeviceView::connectDisconnect()
       
   350 {
       
   351     if (mConnectStatus)
       
   352     {
       
   353         //if the device is connected, call disconnectDevice() when the button is tabbed
       
   354         disconnectDevice();
       
   355     }
       
   356     else
       
   357     {
       
   358         //if the device is disconnected, call connectDevice() when the button is tabbed
       
   359         connectDevice();
       
   360 
       
   361     }
       
   362 }
       
   363 
       
   364 void BtCpUiDeviceView::pairDevice()
       
   365 {
       
   366     if (!mAbstractDelegate)//if there is no other delegate running
       
   367     { 
       
   368         QVariant params;
       
   369         params.setValue(mDeviceIndex);
       
   370         mAbstractDelegate = BtDelegateFactory::newDelegate(
       
   371                 BtDelegate::Pair, mSettingModel, mDeviceModel); 
       
   372         connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(pairDelegateCompleted(int)) );
       
   373         mAbstractDelegate->exec(params);
       
   374     }
       
   375     
       
   376 }
       
   377 
       
   378 void BtCpUiDeviceView::pairDelegateCompleted(int status)
       
   379 {
       
   380     Q_UNUSED(status);
       
   381     //TODO: handle the error here
       
   382     if (mAbstractDelegate)
       
   383     {
       
   384         disconnect(mAbstractDelegate);
       
   385         delete mAbstractDelegate;
       
   386         mAbstractDelegate = 0;
       
   387     }
       
   388 }
       
   389 
       
   390 void BtCpUiDeviceView::unpairDevice()
       
   391 {
       
   392     if (!mAbstractDelegate)//if there is no other delegate running
       
   393     { 
       
   394         QVariant params;
       
   395         params.setValue(mDeviceIndex);
       
   396         mAbstractDelegate = BtDelegateFactory::newDelegate(
       
   397                 BtDelegate::Unpair, mSettingModel, mDeviceModel); 
       
   398         connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(unpairDelegateCompleted(int)) );
       
   399         mAbstractDelegate->exec(params);
       
   400     }
       
   401         
       
   402     
       
   403 }
       
   404 
       
   405 void BtCpUiDeviceView::unpairDelegateCompleted(int status)
       
   406 {
       
   407     Q_UNUSED(status);
       
   408     //TODO: handle the error here 
       
   409     if (mAbstractDelegate)
       
   410     {
       
   411         disconnect(mAbstractDelegate);
       
   412         delete mAbstractDelegate;
       
   413         mAbstractDelegate = 0;
       
   414     }
       
   415 }
       
   416 
       
   417 void BtCpUiDeviceView::connectDevice()
       
   418 {
       
   419     
       
   420     
       
   421     if (!mAbstractDelegate)//if there is no other delegate running
       
   422     { 
       
   423         QVariant params;
       
   424         params.setValue(mDeviceIndex);
       
   425         mAbstractDelegate = BtDelegateFactory::newDelegate(
       
   426                 BtDelegate::Connect, mSettingModel, mDeviceModel); 
       
   427         connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(connectDelegateCompleted(int)) );
       
   428         mAbstractDelegate->exec(params);
       
   429     }
       
   430     
       
   431     
       
   432 }
       
   433 
       
   434 void BtCpUiDeviceView::connectDelegateCompleted(int status)
       
   435 {
       
   436     Q_UNUSED(status);
       
   437     if (mAbstractDelegate)
       
   438     {
       
   439         disconnect(mAbstractDelegate);
       
   440         delete mAbstractDelegate;
       
   441         mAbstractDelegate = 0;
       
   442     }   
       
   443     
       
   444     
       
   445 }
       
   446 
       
   447 void BtCpUiDeviceView::disconnectDevice()
       
   448 {
       
   449     if (!mAbstractDelegate)//if there is no other delegate running
       
   450         { 
       
   451             QVariant params;
       
   452             params.setValue(mDeviceIndex);
       
   453             mAbstractDelegate = BtDelegateFactory::newDelegate(
       
   454                     BtDelegate::Disconnect, mSettingModel, mDeviceModel); 
       
   455             connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(disconnectDelegateCompleted(int)) );
       
   456             mAbstractDelegate->exec(params);
       
   457         }
       
   458     
       
   459 }
       
   460 
       
   461 void BtCpUiDeviceView::disconnectDelegateCompleted(int status)
       
   462 {
       
   463     Q_UNUSED(status);
       
   464     if (mAbstractDelegate)
       
   465     {
       
   466         disconnect(mAbstractDelegate);
       
   467         delete mAbstractDelegate;
       
   468         mAbstractDelegate = 0;
       
   469     }   
       
   470     
       
   471     
       
   472 }
       
   473