messagingapp/msgui/unifiedviewer/src/univieweraddresswidget.cpp
changeset 27 e4592d119491
parent 25 84d9eb65b26f
child 47 5b14749788d7
equal deleted inserted replaced
25:84d9eb65b26f 27:e4592d119491
     9  * Initial Contributors:
     9  * Initial Contributors:
    10  * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11  *
    11  *
    12  * Contributors:
    12  * Contributors:
    13  *
    13  *
    14  * Description:
    14  * Description:Custom widget derived from HbTextEdit which provides rich text
       
    15  *              processing
    15  *
    16  *
    16  */
    17  */
    17 
    18 
    18 #include "univieweraddresswidget.h"
    19 #include "univieweraddresswidget.h"
    19 #include "univiewerfeeder_p.h"
    20 
    20 // SYSTEM INCLUDES
    21 // SYSTEM INCLUDES
    21 #include <HbApplication>
       
    22 #include <QString>
       
    23 #include <QTextCursor>
    22 #include <QTextCursor>
    24 #include <HbMenu>
    23 #include <HbMenu>
    25 
    24 #include <QGraphicsSceneMouseEvent>
    26 // USER INCLUDES
    25 #include <HbAction>
    27 #include "unitexteditor.h"
    26 #include <HbFrameItem>
       
    27 #include <QTextBlock>
       
    28 #include <QApplication>
       
    29 #include <QClipBoard>
       
    30 #include <xqservicerequest.h>
       
    31 
       
    32 #include <xqappmgr.h>
       
    33 #include <cntservicescontact.h>
       
    34 #include <qtcontacts.h>
       
    35 #include <XQServiceRequest.h>
       
    36 #include <xqaiwrequest.h>
       
    37 #include "msgcontacthandler.h"
       
    38 
       
    39 
       
    40 
       
    41 
    28 
    42 
    29 // LOCAL CONSTANTS
    43 // LOCAL CONSTANTS
    30 const QString ADDRESS_SEPARATOR("; ");
    44 const QString ADDRESS_SEPARATOR("; ");
    31 const QString ADDRESS_OPEN(" (");
    45 const QString ADDRESS_OPEN(" (");
    32 const QString ADDRESS_CLOSE(")");
    46 const QString ADDRESS_CLOSE(")");
    33 
    47 const QString SPACE(" ");
    34 //----------------------------------------------------------------------------
    48 
       
    49 //localization
       
    50 #define LOC_OPEN_CONTACT_INFO hbTrId("txt_messaging_menu_open_contact_info")
       
    51 #define LOC_CALL              hbTrId("txt_common_menu_call_verb")
       
    52 #define LOC_SEND_MESSAGE      hbTrId("txt_common_menu_send_message")
       
    53 #define LOC_SAVE_TO_CONTACTS  hbTrId("txt_common_menu_save_to_contacts")
       
    54 #define LOC_COPY              hbTrId("txt_common_menu_copy")
       
    55 
       
    56 const QString BG_FRAME_GRAPHICS("qtg_fr_lineedit_normal");
       
    57 
       
    58 //---------------------------------------------------------------
    35 // UniViewerAddressWidget::UniViewerAddressWidget
    59 // UniViewerAddressWidget::UniViewerAddressWidget
    36 // @see header file
    60 // @see header file
    37 //----------------------------------------------------------------------------
    61 //---------------------------------------------------------------
    38 UniViewerAddressWidget::UniViewerAddressWidget(QGraphicsItem *parent) :
    62 UniViewerAddressWidget::UniViewerAddressWidget(QGraphicsItem * parent) :
    39     HbWidget(parent), mAddress(NULL)
    63 HbTextEdit(parent),
    40 {
    64 mCursorPos(-1)
    41     mAddress = new UniTextEditor(this);
    65 {
    42     HbStyle::setItemName(mAddress, "addressField");
    66     this->setReadOnly(true);
    43 
    67     this->setCursorVisibility(Hb::TextCursorHidden);
    44     connect(mAddress, SIGNAL(aboutToShowContextMenu(HbMenu *,QPointF)), this,
    68     this->setScrollable(false);
    45         SLOT(handleAboutToShowContextMenu(HbMenu *,QPointF)));
    69     HbFrameItem *noBackground = new HbFrameItem(this);
    46 
    70     this->setBackgroundItem(noBackground);
    47     // TODO: Wrapping fix breaking normal scenarios.
    71 
    48     // this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    72     HbFontSpec fontSpec(HbFontSpec::Secondary);
    49 }
    73     QFont font = fontSpec.font();
    50 
    74     this->setFont(font);
    51 //----------------------------------------------------------------------------
    75 
       
    76     mFormatNormal.setForeground(palette().link());
       
    77     mFormatNormal.setBackground(Qt::transparent);
       
    78 
       
    79     mFormatHighlight.setBackground(palette().highlight());
       
    80     mFormatHighlight.setForeground(palette().highlightedText());
       
    81 
       
    82     connect(this, SIGNAL(aboutToShowContextMenu(HbMenu*,QPointF)),
       
    83             this, SLOT(aboutToShowContextMenu(HbMenu*,QPointF)));
       
    84 }
       
    85 
       
    86 //---------------------------------------------------------------
    52 // UniViewerAddressWidget::~UniViewerAddressWidget
    87 // UniViewerAddressWidget::~UniViewerAddressWidget
    53 // @see header file
    88 // @see header file
    54 //----------------------------------------------------------------------------
    89 //---------------------------------------------------------------
    55 UniViewerAddressWidget::~UniViewerAddressWidget()
    90 UniViewerAddressWidget::~UniViewerAddressWidget()
    56 {
    91 {
       
    92 }
       
    93 
       
    94 //---------------------------------------------------------------
       
    95 //UniViewerAddressWidget :: mousePressEvent
       
    96 // @see header file
       
    97 //---------------------------------------------------------------
       
    98 void UniViewerAddressWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
    99 {
       
   100     HbTextEdit::mousePressEvent(event);
       
   101 
       
   102     QTextDocument* doc = this->document();
       
   103 
       
   104     mCursorPos = doc->documentLayout()->hitTest(event->pos(), Qt::ExactHit);
       
   105 
       
   106     highlightText(true);
       
   107 }
       
   108 
       
   109 //---------------------------------------------------------------
       
   110 //UniViewerAddressWidget :: mouseReleaseEvent
       
   111 // @see header file
       
   112 //---------------------------------------------------------------
       
   113 void UniViewerAddressWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   114 {
       
   115     HbTextEdit::mouseReleaseEvent(event);
       
   116 
       
   117     highlightText(false);
       
   118 
       
   119     QString anchor = this->anchorAt(event->pos());
       
   120 
       
   121     if(!anchor.isEmpty() && !this->textCursor().hasSelection())
       
   122     {
       
   123         shortTapAction(anchor);
       
   124     }
    57 }
   125 }
    58 
   126 
    59 //----------------------------------------------------------------------------
   127 //----------------------------------------------------------------------------
    60 // UniViewerAddressWidget::populate
   128 // UniViewerAddressWidget::populate
    61 // @see header file
   129 // @see header file
    62 //----------------------------------------------------------------------------
   130 //----------------------------------------------------------------------------
    63 void UniViewerAddressWidget::populate(const QString &label,
   131 void UniViewerAddressWidget::populate(const QString &label,
    64                                       const QString &address,
   132                                       const QString &address,
    65                                       const QString &alias)
   133                                       const QString &alias)
    66 {
   134 {
    67 
   135     QString labelText = label;
    68     QTextCursor cursor(mAddress->document());
   136     labelText.trimmed();
    69 
   137     labelText += SPACE;
    70     QTextCharFormat addressFormat = cursor.charFormat();
   138     
    71     addressFormat.setFontWeight(QFont::Bold);
   139     //Font.
    72     addressFormat.setForeground(QApplication::palette().link());
   140     HbFontSpec fontSpec(HbFontSpec::Secondary);
    73     addressFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
   141     qreal fontHeight = 0.0;
       
   142     style()->parameter("hb-param-text-height-tiny", fontHeight);
       
   143     fontSpec.setTextHeight(fontHeight);    
       
   144     QFont font = fontSpec.font();
       
   145     
       
   146     QTextCharFormat labelFormat;
       
   147     labelFormat.setFont(font);
       
   148     
       
   149     QTextCharFormat addressFormat;
       
   150     addressFormat.setForeground(palette().link());
       
   151     addressFormat.setFontUnderline(true);
    74 
   152 
    75     // Insert the label then the addresses
   153     // Insert the label then the addresses
    76     cursor.insertText(label);
   154     QTextCursor cursor(this->textCursor());
       
   155     cursor.insertText(labelText,labelFormat);
       
   156     
    77     QString address1 = QString();
   157     QString address1 = QString();
    78     if (!(alias.isEmpty()))
   158     if (!(alias.isEmpty()))
    79     {
   159     {
    80         address1.append(alias);
   160         address1.append(alias);
    81         QString alias1 = QString();
   161         QString alias1 = QString();
    82         if (UniViewerFeederPrivate::GetNameFromContacts(address, alias1) > 1)
   162         
       
   163         int totalNumbers = 0;
       
   164         MsgContactHandler::resolveContactDisplayName(
       
   165                         address,
       
   166                         alias1,
       
   167                         totalNumbers);
       
   168         if (totalNumbers > 1)
    83         {
   169         {
    84             address1.append(ADDRESS_OPEN);
   170             address1.append(ADDRESS_OPEN);
    85             address1.append(address);
   171             address1.append(address);
    86             address1.append(ADDRESS_CLOSE);
   172             address1.append(ADDRESS_CLOSE);
    87         }
   173         }
    90     {
   176     {
    91         address1.append(address);
   177         address1.append(address);
    92     }
   178     }
    93     addressFormat.setAnchorHref(address);
   179     addressFormat.setAnchorHref(address);
    94     cursor.insertText(address1, addressFormat);
   180     cursor.insertText(address1, addressFormat);
    95     repolish();
       
    96 }
   181 }
    97 
   182 
    98 //----------------------------------------------------------------------------
   183 //----------------------------------------------------------------------------
    99 // UniViewerAddressWidget::populate
   184 // UniViewerAddressWidget::populate
   100 // @see header file
   185 // @see header file
   101 //----------------------------------------------------------------------------
   186 //----------------------------------------------------------------------------
   102 void UniViewerAddressWidget::populate(const QString &label,
   187 void UniViewerAddressWidget::populate(const QString &label,
   103                                       ConvergedMessageAddressList addressList)
   188                                       ConvergedMessageAddressList addressList)
   104 {
   189 {
   105     QTextCursor cursor(mAddress->document());
   190     QString labelText = label;
   106 
   191     labelText.trimmed();
   107     QTextCharFormat defaultFormat = cursor.charFormat();
   192     labelText += SPACE;
   108     defaultFormat.setFontWeight(QFont::Bold);
   193     
   109     defaultFormat.setForeground(QApplication::palette().link());
   194     //Font.
   110 
   195     HbFontSpec fontSpec(HbFontSpec::Secondary);
   111     QTextCharFormat addressFormat = cursor.charFormat();
   196     qreal fontHeight = 0.0;
   112     addressFormat.setFontWeight(QFont::Bold);
   197     style()->parameter("hb-param-text-height-tiny", fontHeight);
   113     addressFormat.setForeground(QApplication::palette().link());
   198     fontSpec.setTextHeight(fontHeight);    
   114     addressFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
   199     QFont font = fontSpec.font();
       
   200     
       
   201     QTextCharFormat labelFormat;
       
   202     labelFormat.setFont(font);
       
   203     
       
   204     QTextCharFormat defaultFormat;
       
   205     defaultFormat.setForeground(palette().link());
       
   206 
       
   207     QTextCharFormat addressFormat;
       
   208     addressFormat.setForeground(palette().link());
       
   209     addressFormat.setFontUnderline(true);
   115 
   210 
   116     // Insert the label then the addresses
   211     // Insert the label then the addresses
   117     cursor.insertText(label);
   212     QTextCursor cursor(this->document());
       
   213     cursor.insertText(labelText,labelFormat);
   118 
   214 
   119     int addressCount = addressList.count();
   215     int addressCount = addressList.count();
   120 
   216 
   121     for (int i = 0; i < addressCount; ++i)
   217     for (int i = 0; i < addressCount; ++i)
   122     {
   218     {
   124         QString address = QString();
   220         QString address = QString();
   125         if (! (addressList[i]->alias().isEmpty()))
   221         if (! (addressList[i]->alias().isEmpty()))
   126         {
   222         {
   127             address.append(addressList[i]->alias());
   223             address.append(addressList[i]->alias());
   128             QString alias = QString();
   224             QString alias = QString();
   129             if (UniViewerFeederPrivate::GetNameFromContacts(addressList[i]->address(),
   225             
   130                                                             alias) > 1)
   226             int totalNumbers = 0;
       
   227             MsgContactHandler::resolveContactDisplayName(
       
   228                             addressList[i]->address(),
       
   229                             alias,
       
   230                             totalNumbers);
       
   231             if (totalNumbers > 1)
   131             {
   232             {
   132                 address.append(ADDRESS_OPEN);
   233                 address.append(ADDRESS_OPEN);
   133                 address.append(addressList[i]->address());
   234                 address.append(addressList[i]->address());
   134                 address.append(ADDRESS_CLOSE);
   235                 address.append(ADDRESS_CLOSE);
   135             }
   236             }
   146         {
   247         {
   147             cursor.insertText(ADDRESS_SEPARATOR, defaultFormat);
   248             cursor.insertText(ADDRESS_SEPARATOR, defaultFormat);
   148         }
   249         }
   149         
   250         
   150     }
   251     }
   151     repolish();
       
   152 }
   252 }
   153 
   253 
   154 //----------------------------------------------------------------------------
   254 //----------------------------------------------------------------------------
   155 // UniViewerAddressWidget::clearContent
   255 // UniViewerAddressWidget::clearContent
   156 // @see header file
   256 // @see header file
   157 //----------------------------------------------------------------------------
   257 //----------------------------------------------------------------------------
   158 void UniViewerAddressWidget::clearContent()
   258 void UniViewerAddressWidget::clearContent()
   159 {
   259 {
   160     mAddress->document()->clear();
   260     this->document()->clear();
   161 }
   261 }
   162 
   262 
   163 //----------------------------------------------------------------------------
   263 //----------------------------------------------------------------------------
   164 // UniViewerAddressWidget::handleAboutToShowContextMenu
   264 // UniViewerAddressWidget::menuClosed
   165 // @see header file
   265 // @see header file
   166 //----------------------------------------------------------------------------
   266 //----------------------------------------------------------------------------
   167 void UniViewerAddressWidget::handleAboutToShowContextMenu(HbMenu *contextMenu, const QPointF &pos)
   267 void UniViewerAddressWidget::menuClosed()
   168 {
   268 {
   169     Q_UNUSED(pos)
   269     highlightText(false);
       
   270 }
       
   271 
       
   272 //----------------------------------------------------------------------------
       
   273 // UniViewerAddressWidget::highlightText
       
   274 // @see header file
       
   275 //----------------------------------------------------------------------------
       
   276 void UniViewerAddressWidget::highlightText(bool highlight)
       
   277 {
       
   278     QTextBlock textBlock = this->document()->findBlock(mCursorPos);
       
   279 
       
   280     QTextBlock::iterator it;
       
   281 
       
   282     for (it = textBlock.begin(); !(it.atEnd()); ++it)
       
   283     {
       
   284         QTextFragment currentFragment = it.fragment();
       
   285         
       
   286         if (currentFragment.isValid() && currentFragment.contains(mCursorPos)
       
   287             && currentFragment.charFormat().fontUnderline())
       
   288         {
       
   289             int start = currentFragment.position();
       
   290             int length = currentFragment.length();
       
   291 
       
   292             QTextCursor cursor = this->textCursor();
       
   293             cursor.clearSelection();
       
   294             cursor.setPosition(start);
       
   295             cursor.setPosition(start + length,QTextCursor::KeepAnchor);
       
   296 
       
   297             if(highlight)
       
   298             {
       
   299                 cursor.mergeCharFormat(mFormatHighlight);
       
   300             }
       
   301             else
       
   302             {
       
   303                 cursor.mergeCharFormat(mFormatNormal);
       
   304             }
       
   305 
       
   306             cursor.clearSelection();
       
   307             break;
       
   308         }
       
   309     }
       
   310 }
       
   311 
       
   312 void UniViewerAddressWidget::aboutToShowContextMenu(HbMenu *contextMenu, const QPointF &pos)
       
   313 {
       
   314     //remove default actions.
   170     contextMenu->clearActions();
   315     contextMenu->clearActions();
       
   316     
       
   317     // Check if there is an anchor at this pos
       
   318     QString  anchor = this->anchorAt(pos);
       
   319 
       
   320     if(!anchor.isEmpty() && !this->textCursor().hasSelection())
       
   321     {
       
   322 
       
   323         HbAction* action = NULL;
       
   324 
       
   325         action = contextMenu->addAction(LOC_OPEN_CONTACT_INFO, this, SLOT(openContactInfo()));
       
   326         action->setData(anchor);
       
   327 
       
   328         action = contextMenu->addAction(LOC_CALL, this, SLOT(call()));
       
   329         action->setData(anchor);
       
   330 
       
   331         action = contextMenu->addAction(LOC_SEND_MESSAGE, this, SLOT(sendMessage()));
       
   332         action->setData(anchor);
       
   333 
       
   334         action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts()));
       
   335         action->setData(anchor);
       
   336 
       
   337         action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard()));
       
   338         action->setData(anchor);
       
   339 
       
   340     }
       
   341 
       
   342     connect(contextMenu,SIGNAL(aboutToClose()),this,SLOT(menuClosed()));    
       
   343 }
       
   344 
       
   345 void UniViewerAddressWidget::shortTapAction(QString anchor)
       
   346 {
       
   347     HbAction action;
       
   348     action.setData(anchor);
       
   349     connect(&action,SIGNAL(triggered()),this,SLOT(openContactInfo()));
       
   350     action.trigger();
       
   351 }
       
   352 
       
   353 void UniViewerAddressWidget::copyToClipboard()
       
   354 {
       
   355     HbAction* action = qobject_cast<HbAction*>(sender());
       
   356 
       
   357     if(action)
       
   358     {
       
   359         QMimeData* data = new QMimeData();
       
   360         QString str = action->data().toString();
       
   361         data->setText(str);
       
   362         QApplication::clipboard()->setMimeData(data);
       
   363     }
       
   364 }
       
   365 
       
   366 void UniViewerAddressWidget::call()
       
   367 {
       
   368     HbAction* action = qobject_cast<HbAction*>(sender());
       
   369 
       
   370     if(action)
       
   371     {
       
   372         QString phoneNumber = action->data().toString();
       
   373         
       
   374         //invoke dialer service and pass phoneNumber.        
       
   375         QString serviceName("com.nokia.services.telephony");
       
   376         QString operation("dial(QString)");
       
   377         
       
   378         XQServiceRequest* serviceRequest = new XQServiceRequest(serviceName,operation,false);
       
   379         
       
   380         connect(serviceRequest, SIGNAL(requestCompleted(QVariant)),
       
   381                 this, SLOT(onServiceRequestCompleted()));
       
   382         
       
   383         connect(serviceRequest, SIGNAL(requestError(int)),
       
   384                 this, SLOT(onServiceRequestCompleted()));
       
   385         
       
   386         *serviceRequest << phoneNumber;
       
   387         serviceRequest->send();
       
   388     }
       
   389 }
       
   390 
       
   391 void UniViewerAddressWidget::onServiceRequestCompleted()
       
   392     {
       
   393     //service request is now complete. delete it.    
       
   394     XQServiceRequest* request = qobject_cast<XQServiceRequest*>(sender());
       
   395     
       
   396     if(request)
       
   397         {
       
   398         delete request;
       
   399         }
       
   400     }
       
   401 
       
   402 
       
   403 void UniViewerAddressWidget::openContactInfo()
       
   404 {
       
   405     HbAction* action = qobject_cast<HbAction*>(sender());
       
   406     
       
   407     if(action)
       
   408     {
       
   409         QList<QVariant> args;
       
   410         QString operation;
       
   411         
       
   412         QString data = action->data().toString();        
       
   413     
       
   414         int contactId = MsgContactHandler::resolveContactDisplayName(
       
   415                 data,
       
   416                 QContactPhoneNumber::DefinitionName,
       
   417                 QContactPhoneNumber::FieldNumber);
       
   418 
       
   419         if(contactId > 0)
       
   420         {
       
   421             //open contact card
       
   422             operation = QString("open(int)");
       
   423             args << contactId;
       
   424         }
       
   425         else
       
   426         {
       
   427             //save to contacts with phone number field prefilled.
       
   428 
       
   429             operation = QString("editCreateNew(QString,QString)");
       
   430             QString type = QContactPhoneNumber::DefinitionName;
       
   431 
       
   432             args << type;
       
   433             args << data;
       
   434         }
       
   435         
       
   436         //service stuff.
       
   437         QString serviceName("com.nokia.services.phonebookservices");
       
   438      
       
   439         XQAiwRequest* request;
       
   440         XQApplicationManager appManager;
       
   441         request = appManager.create(serviceName, "Fetch", operation, true); // embedded
       
   442         if ( request == NULL )
       
   443             {
       
   444             return;       
       
   445             }
       
   446 
       
   447         // Result handlers
       
   448         connect (request, SIGNAL(requestOk(const QVariant&)), 
       
   449             this, SLOT(handleOk(const QVariant&)));
       
   450         connect (request, SIGNAL(requestError(const QVariant&)), 
       
   451             this, SLOT(handleError(const QVariant&)));
       
   452         
       
   453         request->setArguments(args);
       
   454         request->send();
       
   455         delete request;
       
   456     }
       
   457 }
       
   458 
       
   459 void UniViewerAddressWidget::handleOk(const QVariant& result)
       
   460     {
       
   461     Q_UNUSED(result)
       
   462     }
       
   463 
       
   464 void UniViewerAddressWidget::handleError(int errorCode, const QString& errorMessage)
       
   465     {
       
   466     Q_UNUSED(errorMessage)
       
   467     Q_UNUSED(errorCode)
       
   468     }
       
   469 
       
   470 void UniViewerAddressWidget::saveToContacts()
       
   471 {
       
   472     //handler for save to contacts.
       
   473 }
       
   474 
       
   475 void UniViewerAddressWidget::sendMessage()
       
   476 {
       
   477     HbAction* action = qobject_cast<HbAction*>(sender());
       
   478 
       
   479     if(action)
       
   480     {
       
   481         QString phoneNumber = action->data().toString();
       
   482         QString alias;
       
   483 
       
   484         QTextBlock textBlock = this->document()->findBlock(mCursorPos);
       
   485 
       
   486         QTextBlock::iterator it;
       
   487 
       
   488         for (it = textBlock.begin(); !(it.atEnd()); ++it)
       
   489         {
       
   490             QTextFragment currentFragment = it.fragment();
       
   491 
       
   492             if (currentFragment.isValid() && currentFragment.contains(mCursorPos)
       
   493                 && currentFragment.charFormat().fontUnderline())
       
   494             {
       
   495                 QString txt = currentFragment.text();
       
   496                 if(txt != phoneNumber)
       
   497                 {
       
   498                     alias = txt;  
       
   499                 }
       
   500                 break;
       
   501             }
       
   502         }
       
   503         
       
   504 
       
   505         //invoke editor & pass phoneNumber.
       
   506         emit sendMessage(phoneNumber,alias);
       
   507     }
   171 }
   508 }
   172 
   509 
   173 // EOF
   510 // EOF