contactwidgethsplugin/contactwidgeths/src/commlauncherwidget.cpp
branchRCL_3
changeset 62 5b6f26637ad3
equal deleted inserted replaced
58:d4f567ce2e7c 62:5b6f26637ad3
       
     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:  Communication Launcher widget for Friend widget 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <HbStyleLoader>
       
    20 #include <QGraphicsLinearLayout>
       
    21 #include <HbEffect>
       
    22 #include <HbPushButton>
       
    23 #include <qtcontacts.h>
       
    24 #include <hbdocumentloader.h>
       
    25 #include <xqappmgr.h>
       
    26 
       
    27 #include "commlauncherwidget.h"
       
    28 
       
    29 // ContactManager backend
       
    30 #ifdef Q_OS_SYMBIAN
       
    31     QString cmBackend = "symbian";
       
    32 #else
       
    33     QString cmBackend = "memory"; 
       
    34 #endif
       
    35 const QString commLauncherDocml = ":/commlauncherwidget.docml";
       
    36 // Note: these are case sensitive
       
    37 const QString callButtonName     = "ButtonCall";
       
    38 const QString emailButtonName    = "ButtonEmail";
       
    39 const QString messageButtonName  = "ButtonMessage";
       
    40 const QString mycardButtonName   = "ButtonMycard";
       
    41 
       
    42 const QString appearTL = "appear_tl";
       
    43 const QString appearTR = "appear_tr";
       
    44 const QString appearBL = "appear_bl";
       
    45 const QString appearBR = "appear_br"; 
       
    46 
       
    47 const int commLauncherMargin = 120;  // heights of titlebar & comm.launcher 
       
    48 
       
    49 /*!
       
    50   \class CommLauncherWidget
       
    51 */
       
    52 
       
    53 /*!
       
    54     Constructor
       
    55 */
       
    56 CommLauncherWidget::CommLauncherWidget(QGraphicsItem *parent) :
       
    57  HbPopup(parent),
       
    58  mContact(0),
       
    59  mButtonCount(0),
       
    60  mRequest(NULL),
       
    61  mCallButton(0),
       
    62  mSendMsgButton(0),
       
    63  mEmailButton(0),
       
    64  mPhonebookButton(0),
       
    65  mApplicationManager(0)
       
    66 {    
       
    67     
       
    68     HbStyleLoader::registerFilePath(":/commlauncherbuttons.css");
       
    69     // effect triggers
       
    70     connect(this, SIGNAL(aboutToShow()),  SLOT(popupAboutToShow()));
       
    71     connect(this, SIGNAL(aboutToClose()), SLOT(popupAboutToClose()));
       
    72     
       
    73     // create layout
       
    74     mLayout = new QGraphicsLinearLayout(this);
       
    75     qreal verticalMargin = 0.0;
       
    76     style()->parameter("hb-param-margin-gene-middle-vertical", verticalMargin);
       
    77     qreal horizontalMargin = 0.0;
       
    78     style()->parameter("hb-param-margin-gene-middle-horizontal", horizontalMargin);
       
    79     
       
    80     mLayout->setContentsMargins(horizontalMargin, verticalMargin,
       
    81                                 horizontalMargin, 2*verticalMargin);
       
    82     
       
    83     // create document loader
       
    84     HbDocumentLoader *documentLoader = new HbDocumentLoader();
       
    85     bool result = false;
       
    86     documentLoader->load(commLauncherDocml, &result);
       
    87     ASSERT(result);
       
    88     
       
    89     // create buttons
       
    90     const QString callIconName = "qtg_large_active_call";
       
    91     mCallButton = createButton(callIconName, callButtonName, documentLoader);
       
    92     connect(mCallButton, SIGNAL(clicked()), this, SLOT(makeCall()));
       
    93     
       
    94     const QString messagingIconName = "qtg_large_message";
       
    95     mSendMsgButton = createButton(messagingIconName, messageButtonName, documentLoader);
       
    96     connect(mSendMsgButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
       
    97     
       
    98     const QString emailIconName = "qtg_large_email";
       
    99     mEmailButton = createButton(emailIconName, emailButtonName, documentLoader);
       
   100     // EMAIL FUNCTIONALITY COMMENTED OUT BECAUSE PLATFORM DOES NOT SUPPORT IT!
       
   101     //connect(mEmailButton, SIGNAL(clicked()), this, SLOT(sendEmail()));
       
   102     
       
   103     const QString phonebookIconName = "qtg_large_mycard";
       
   104     mPhonebookButton = createButton(phonebookIconName, mycardButtonName, documentLoader);
       
   105     connect(mPhonebookButton, SIGNAL(clicked()), this, SLOT(openPhonebook()));
       
   106         
       
   107     delete documentLoader;
       
   108 }
       
   109 
       
   110 /*!
       
   111     Destructor
       
   112 */
       
   113 CommLauncherWidget::~CommLauncherWidget()
       
   114 {
       
   115     // Deleting request cancels all pending requests 
       
   116     if (mRequest) {
       
   117         delete mRequest;
       
   118         mRequest = NULL;
       
   119     }
       
   120 
       
   121 }
       
   122 
       
   123 /*!
       
   124     Sets the contact for the widget
       
   125 */
       
   126 void CommLauncherWidget::setContact(QContact &contact)
       
   127 {
       
   128     mContact = &contact;
       
   129 }
       
   130 
       
   131 /*!
       
   132     Sets the application manager for the widget
       
   133 */
       
   134 void CommLauncherWidget::setApplicationManager(XQApplicationManager &appManager)
       
   135 {
       
   136     mApplicationManager = &appManager;
       
   137 }
       
   138 
       
   139 /*!
       
   140     Creates the UI for the widget
       
   141 */
       
   142 void CommLauncherWidget::createUI()
       
   143 {        
       
   144     mButtonCount = 0;
       
   145     // Create call button, if number is available
       
   146     QList<QContactActionDescriptor> callActionDescriptors = 
       
   147             QContactAction::actionDescriptors("call", cmBackend);
       
   148     if (callActionDescriptors.count() > 0) {
       
   149         QList<QContactPhoneNumber> numberList = mContact->details<QContactPhoneNumber>();
       
   150         if (numberList.count() > 0) {
       
   151             mLayout->addItem(mCallButton);
       
   152             mCallButton->show();
       
   153             mButtonCount++;
       
   154 
       
   155         } else {
       
   156             mCallButton->hide();
       
   157             mLayout->removeItem(mCallButton);
       
   158             mLayout->invalidate();
       
   159             mLayout->activate();
       
   160         }
       
   161     } else {
       
   162         qDebug() << "Call button is not created";
       
   163     }
       
   164     
       
   165     // Create sms button, if number is available
       
   166     QList<QContactActionDescriptor> messageActionDescriptors =
       
   167             QContactAction::actionDescriptors("message", cmBackend);
       
   168     if (messageActionDescriptors.count() > 0) {
       
   169         QList<QContactPhoneNumber> numberList = mContact->details<QContactPhoneNumber>();
       
   170         if (numberList.count() > 0) {
       
   171             mLayout->addItem(mSendMsgButton);
       
   172             mSendMsgButton->show();
       
   173             mButtonCount++;
       
   174         } else {
       
   175             mSendMsgButton->hide();
       
   176             mLayout->removeItem(mSendMsgButton);
       
   177             mLayout->invalidate();
       
   178             mLayout->activate();
       
   179         }
       
   180     } else {
       
   181         qDebug() << "Sms button is not created";
       
   182     }
       
   183         
       
   184     // Create email button, if email count exists
       
   185     // EMAIL FUNCTIONALITY COMMENTED OUT BECAUSE PLATFORM DOES NOT SUPPORT IT!
       
   186     /*QList<QContactActionDescriptor> emailActionDescriptors =
       
   187             QContactAction::actionDescriptors("email", cmBackend);
       
   188     if (emailActionDescriptors.count() > 0) {
       
   189         QList<QContactEmailAddress> emailList = mContact->details<QContactEmailAddress>();
       
   190         if (emailList.count() > 0) {
       
   191             mLayout->addItem(mEmailButton);
       
   192             mEmailButton->show();
       
   193             mButtonCount++;
       
   194         } else {
       
   195             mEmailButton->hide();
       
   196             mLayout->removeItem(mEmailButton);
       
   197             mLayout->invalidate();
       
   198             mLayout->activate();
       
   199         }
       
   200     } else {
       
   201         qDebug() << "Email button not created";
       
   202     }
       
   203     */
       
   204 
       
   205     mLayout->addItem(mPhonebookButton);
       
   206     mButtonCount++;
       
   207 
       
   208     // add Effects
       
   209     HbEffect::add(this, QString(":/friend_expand_tl.fxml"), appearTL);
       
   210     HbEffect::add(this, QString(":/friend_expand_tr.fxml"), appearTR);
       
   211     HbEffect::add(this, QString(":/friend_expand_bl.fxml"), appearBL);
       
   212     HbEffect::add(this, QString(":/friend_expand_br.fxml"), appearBR);       
       
   213     HbEffect::add(this, QString(":/friend_minimize.fxml"),  "disappear");
       
   214     
       
   215     setLayout(mLayout);    
       
   216 }
       
   217 
       
   218 /*!
       
   219     This widget is about to show, show effect
       
   220 */
       
   221 void CommLauncherWidget::popupAboutToShow()
       
   222 { 
       
   223     HbEffect::start(this, mAppearEffect);  
       
   224 }
       
   225 
       
   226 /*
       
   227  * Select which appear effect to use depending on FriendWidget position
       
   228  */
       
   229 void CommLauncherWidget::selectAppearEffect(QPointF FriendPos, QPointF LauncherPos)
       
   230 {
       
   231     bool left=true;
       
   232     bool top=true;
       
   233     
       
   234     qDebug() << "FriendPos "   << FriendPos;
       
   235     qDebug() << "LauncherPos " << LauncherPos;
       
   236     
       
   237     if (LauncherPos.y() > FriendPos.y()) {
       
   238         top = false;
       
   239     }
       
   240     if (LauncherPos.x() > FriendPos.x()) {
       
   241         left = false;
       
   242     }	
       
   243 
       
   244     if (top && left) {
       
   245         mAppearEffect = appearTL;
       
   246     }
       
   247     else if (top && !left) {
       
   248         mAppearEffect = appearTR;
       
   249     }
       
   250     else if (!top && left) {
       
   251         mAppearEffect = appearBL;
       
   252     }
       
   253     else {
       
   254         mAppearEffect = appearBR;
       
   255     }
       
   256     qDebug() << "---------------top " << top << "--- left " << left << " " << mAppearEffect;
       
   257 }
       
   258 
       
   259 /*!
       
   260     This widget is about to close, show effect
       
   261 */
       
   262 void CommLauncherWidget::popupAboutToClose()
       
   263 {
       
   264     HbEffect::start(this, "disappear");  
       
   265     emit launcherClosed();
       
   266 }
       
   267 
       
   268 /*!
       
   269     Returns a button with a given name and icon to layout
       
   270 */
       
   271 HbPushButton* CommLauncherWidget::createButton(const QString iconName, const QString buttonName,
       
   272                                                const HbDocumentLoader *documentLoader)
       
   273 {
       
   274     HbPushButton *button = 0;
       
   275     button = qobject_cast<HbPushButton *>
       
   276                     (documentLoader->findWidget(buttonName));
       
   277     if (button) {
       
   278         button->setIcon(HbIcon(iconName));
       
   279     }
       
   280 
       
   281     return button;
       
   282 }
       
   283 
       
   284   
       
   285 /*!
       
   286     Returns screen position for communication launcher for a widget
       
   287 */
       
   288 QPointF CommLauncherWidget::commLauncherPosition(QPointF widgetPosition, QRectF& widgetBoundingRect,
       
   289                                               QRectF& sceneRect, QRectF& launcherRect)
       
   290 {    
       
   291     QPointF commLauncherPosition(widgetPosition.x(), commLauncherMargin);        
       
   292     qDebug() << "friend widgetPosition " << widgetPosition.x() << widgetPosition.y();
       
   293     
       
   294     qreal topMargin = 0.0; 
       
   295     style()->parameter("hb-param-margin-gene-top",topMargin);
       
   296     qDebug() << "topMargin------------ " << topMargin;
       
   297     qDebug() << "launcher size.y " << launcherRect.size().height();
       
   298     
       
   299     // Calculate vertical position
       
   300     if (widgetPosition.y() > commLauncherMargin) {
       
   301         commLauncherPosition.setY(widgetPosition.y() - launcherRect.size().height() + topMargin );
       
   302     } else {
       
   303         commLauncherPosition.setY(widgetPosition.y() + widgetBoundingRect.height() 
       
   304                                   - launcherRect.size().height()/2);
       
   305     }
       
   306         
       
   307     // Calculate horizontal position
       
   308     if ((widgetPosition.x() + widgetBoundingRect.width()/2) < sceneRect.width()/2 ) {
       
   309         // Communication launcher to the right
       
   310         qDebug() << "launcher to right";
       
   311         commLauncherPosition.setX(widgetPosition.x() + widgetBoundingRect.width()/2);
       
   312         
       
   313     } else {
       
   314         // Communication launcher to the left
       
   315         qDebug() << "launcher to left";
       
   316         // The width needs to be calculated based on visible buttons.
       
   317         launcherRect.setWidth(commLauncherWidth());
       
   318         commLauncherPosition.setX(widgetPosition.x() + widgetBoundingRect.width()/2 
       
   319         		                  - launcherRect.size().width());
       
   320     }
       
   321     
       
   322     // Check that the communication launcher is not positioned outside the scene in y-axis
       
   323     // HbPopup takes care that the launcher is not positioned outside the scene in x-axis
       
   324     if (commLauncherPosition.y() > sceneRect.height()) {
       
   325         commLauncherPosition.setY(sceneRect.height());
       
   326     } else if (commLauncherPosition.y() < sceneRect.y()){
       
   327         commLauncherPosition.setY(sceneRect.y());
       
   328     }
       
   329     
       
   330     qDebug() << "commLauncherPosition " << commLauncherPosition.x() << commLauncherPosition.y();
       
   331     return commLauncherPosition;
       
   332 }
       
   333 
       
   334 
       
   335 /*!
       
   336     Makes a phone call to contact.
       
   337 */
       
   338 void CommLauncherWidget::makeCall()
       
   339 {
       
   340     QList<QContactActionDescriptor> callActionDescriptors = 
       
   341                 QContactAction::actionDescriptors("call", cmBackend); 
       
   342     if (callActionDescriptors.count() > 0) {
       
   343         //Get preferred number
       
   344         QContactDetail detail = mContact->preferredDetail("call");
       
   345         QContactPhoneNumber phoneNumber;
       
   346             
       
   347         //If preferred number is available, cast it to phonenumber
       
   348         if(!detail.isEmpty()) {
       
   349             phoneNumber = static_cast<QContactPhoneNumber>(detail);
       
   350         } else {
       
   351             //if preferred is not set select the first number
       
   352             phoneNumber = mContact->detail<QContactPhoneNumber>();
       
   353         }
       
   354         // invoke action
       
   355         QContactAction *callAction = QContactAction::action(callActionDescriptors.at(0));
       
   356         if (!phoneNumber.isEmpty()) {
       
   357             callAction->invokeAction(*mContact, phoneNumber);
       
   358             
       
   359             qDebug() << "call to number " << phoneNumber.number();
       
   360         }
       
   361     } else {
       
   362         qDebug() << "contact has no Actions, can't make a call";
       
   363     }
       
   364     
       
   365     close();
       
   366 }
       
   367 
       
   368 /*!
       
   369     Sends a message to contact.
       
   370 */
       
   371 void CommLauncherWidget::sendMessage()
       
   372 {
       
   373     QList<QContactActionDescriptor> messageActionDescriptors = 
       
   374                     QContactAction::actionDescriptors("message", cmBackend); 
       
   375     if (messageActionDescriptors.count() > 0) {
       
   376         //Get preferred message number
       
   377         QContactDetail detail = mContact->preferredDetail("message");
       
   378         QContactPhoneNumber messageNumber;
       
   379             
       
   380         //If preferred number is available, cast it to phonenumber
       
   381         if(!detail.isEmpty()) {
       
   382             messageNumber = static_cast<QContactPhoneNumber>(detail);
       
   383         } else {
       
   384             //if preferred is not set select the first number
       
   385             messageNumber = mContact->detail<QContactPhoneNumber>();
       
   386         }
       
   387         // invoke action
       
   388         QContactAction *messageAction = QContactAction::action(messageActionDescriptors.at(0));
       
   389         if (!messageNumber.isEmpty()) {
       
   390             messageAction->invokeAction(*mContact, messageNumber);
       
   391             
       
   392             qDebug() << "send to number " << messageNumber.number();
       
   393         }
       
   394     } else {
       
   395         qDebug() << "contact has no Actions, can't send a message";
       
   396     }
       
   397     
       
   398     close();
       
   399 }
       
   400 
       
   401 /*!
       
   402     Sends an email to contact.
       
   403 */
       
   404 // EMAIL FUNCTIONALITY COMMENTED OUT BECAUSE WK20 PLATFORM DOES NOT SUPPORT IT!
       
   405 
       
   406 /*
       
   407 void CommLauncherWidget::sendEmail()
       
   408 {
       
   409 
       
   410     QList<QContactActionDescriptor> emailActionDescriptors =
       
   411                 QContactAction::actionDescriptors("email", cmBackend);
       
   412     if (emailActionDescriptors.count() > 0) {
       
   413         QContactAction *emailAction = QContactAction::action(emailActionDescriptors.at(0));
       
   414     //TODO: implement
       
   415         QList<QContactEmailAddress> emailList = mContact->details<QContactEmailAddress>();
       
   416         if (emailList.count() > 0) {
       
   417             emailAction->invokeAction(*mContact, emailList.at(0));
       
   418        
       
   419             QString emailAddress = emailList.at(0).emailAddress();
       
   420             qDebug() << "send to email " << emailAddress;
       
   421         }
       
   422     } else {
       
   423         qDebug() << "contact has no Actions, can't send a email";
       
   424     }
       
   425 
       
   426     // Deleting request cancels all pending requests
       
   427     if (mRequest) {
       
   428         delete mRequest;
       
   429         mRequest = NULL;
       
   430     }
       
   431     
       
   432     QMap<QString, QVariant> map;
       
   433     static const QString emailSendToKey = "to";
       
   434     QString emailAddress;
       
   435     QList<QContactEmailAddress> emailList = mContact->details<QContactEmailAddress>();    
       
   436     if (emailList.count() > 0) {
       
   437         emailAddress = emailList.at(0).emailAddress();
       
   438     }
       
   439     
       
   440     // Add to recipient:
       
   441     map.insert(emailSendToKey, QVariant::fromValue(emailAddress));
       
   442     QVariant mapAsVariant = QVariant::fromValue(map);
       
   443     
       
   444     mRequest = mApplicationManager->create("com.nokia.symbian.IMessage",
       
   445                                            "Send", "send(QVariant)", false);
       
   446     if (mRequest) {
       
   447         mRequest->setSynchronous(false);
       
   448         QList<QVariant> arguments;
       
   449         arguments.append(mapAsVariant);
       
   450         mRequest->setArguments(arguments);
       
   451         bool result = mRequest->send();
       
   452         if (!result) {
       
   453             qDebug() << "Sending request failed: " << mRequest->lastErrorMessage();
       
   454         }
       
   455     } else {
       
   456             qDebug() << "Creating service request failed";
       
   457     }
       
   458     
       
   459     close();
       
   460 }
       
   461 */
       
   462 
       
   463 /*!
       
   464     Opens contact card from phonebook to contact.
       
   465 */
       
   466 void CommLauncherWidget::openPhonebook()
       
   467 {
       
   468     // Deleting request cancels all pending requests
       
   469     if (mRequest) {
       
   470         delete mRequest;
       
   471         mRequest = NULL;
       
   472     }
       
   473     mRequest = mApplicationManager->create("com.nokia.services.phonebookservices",
       
   474                                            "Fetch", "open(int)", false);
       
   475     if (mRequest) {
       
   476         mRequest->setSynchronous(false);
       
   477         QList<QVariant> arguments;
       
   478         arguments.append(QVariant(mContact->localId()));
       
   479         mRequest->setArguments(arguments);
       
   480         bool result = mRequest->send();
       
   481         if (!result) {
       
   482             qDebug() << "Sending request failed: " << mRequest->lastErrorMessage();
       
   483         }
       
   484     } else {
       
   485         qDebug() << "Creating service request failed";
       
   486     }
       
   487     
       
   488     close();
       
   489 }
       
   490 
       
   491 /*!
       
   492     Opens phonebook to create a new contact.
       
   493 */
       
   494 void CommLauncherWidget::openPhonebookCreateNew()
       
   495 {
       
   496     // Deleting request cancels all pending requests
       
   497     if (mRequest) {
       
   498         delete mRequest;
       
   499         mRequest = NULL;
       
   500     }
       
   501     mRequest = mApplicationManager->create("com.nokia.services.phonebookservices",
       
   502                                            "Fetch", "editCreateNew(QString,QString)", false);
       
   503     if (mRequest) {
       
   504         QList<QVariant> arguments;
       
   505         QString type = QContactPhoneNumber::DefinitionName;
       
   506         arguments.append(QVariant( type ));
       
   507         arguments.append(QVariant( "" ));        
       
   508         mRequest->setArguments(arguments);
       
   509    
       
   510         bool result = mRequest->send();
       
   511         if (!result) {
       
   512             qDebug() << "Sending request failed: " << mRequest->lastErrorMessage();
       
   513         }
       
   514     
       
   515     } else {
       
   516         qDebug() << "Creating service request failed";
       
   517     }
       
   518 }
       
   519 
       
   520 /*!
       
   521     Handles call key events
       
   522 */
       
   523 void CommLauncherWidget::keyPressEvent(QKeyEvent *event)
       
   524 {
       
   525 	qDebug() << "keyPressEvent event=" << event->key();
       
   526     if (event->key() == Qt::Key_Yes) {
       
   527         // Call key initializes a call
       
   528         makeCall();
       
   529         event->accept();
       
   530     } else {
       
   531       close();
       
   532       //HbPopup::keyPressEvent(event);
       
   533     } 
       
   534 }
       
   535 
       
   536 /*!
       
   537     Counts the width for the launcher. 
       
   538     The width is not known before the launcher is drawn for the first time.
       
   539 */
       
   540 int CommLauncherWidget::commLauncherWidth()
       
   541 {
       
   542     int width = 0;    
       
   543     qreal verticalMargin = 0.0;
       
   544     style()->parameter("hb-param-margin-gene-middle-vertical", verticalMargin);
       
   545     qreal unit = HbDeviceProfile::current().unitValue(); 
       
   546     const int buttonSize = 9 * unit;
       
   547     
       
   548     width = mButtonCount * buttonSize + (mButtonCount + 1) * verticalMargin;
       
   549     
       
   550     return width;
       
   551 }
       
   552 
       
   553