messagingapp/msgui/unifiedviewer/src/univiewertextitem.cpp
branchGCC_SURGE
changeset 47 5b14749788d7
parent 27 e4592d119491
parent 44 36f374c67aa8
equal deleted inserted replaced
35:a32b19fb291e 47:5b14749788d7
    19 
    19 
    20 #include "univiewertextitem.h"
    20 #include "univiewertextitem.h"
    21 #include "msgcontacthandler.h"
    21 #include "msgcontacthandler.h"
    22 
    22 
    23 #include <QRegExp>
    23 #include <QRegExp>
    24 #include <QGraphicsSceneMouseEvent>
       
    25 #include <QApplication>
    24 #include <QApplication>
    26 #include <QClipboard>
    25 #include <QClipboard>
    27 #include <QTextBlock>
    26 #include <QTextBlock>
    28 
    27 #include <QUrl>
       
    28 #include <QDesktopServices>
       
    29 #include <HbTapGesture>
    29 #include <HbMenu>
    30 #include <HbMenu>
    30 #include <HbAction>
    31 #include <HbAction>
    31 #include <HbFrameItem>
       
    32 #include <cntservicescontact.h>
    32 #include <cntservicescontact.h>
    33 #include <XQServiceRequest.h>
    33 #include <xqservicerequest.h>
    34 #include <xqaiwrequest.h>
    34 #include <xqaiwrequest.h>
    35 #include <xqappmgr.h>
    35 #include <xqappmgr.h>
    36 
    36 
    37 
    37 
    38 //consts
    38 //consts
    39 
    39 
    40 //regexp
    40 //regexp
    41 const QString NUMBER_PATTERN("(\\(|\\+|\\d)((?:\\d{2,})((?:[\\s-/.\\)\\()])*(?:(\\d+|\\))))*)|((\\*#)(?:\\d+(\\*|#)(?:\\d+#)?))");
    41 const QString NUMBER_PATTERN("(\\(?(\\+|\\d))((?:\\d)((?:[\\s-/.\\)\\(])*(?:(\\d+|\\))))*(?:\\d?|\\)))|((\\*#)(?:\\d+(\\*|#)(?:\\d+#)?))");
    42 
    42 
    43 const QString EMAIL_PATTERN("[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?");
    43 const QString EMAIL_PATTERN("[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?");
    44 
    44 
    45 const QString URL_PATTERN("(((ht|f|rt)(tp|sp)(s?)\\:\\/\\/)|(www|wap)(?:\\.))(([-\\w]*[0-9a-zA-Z])+(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\?\\,\'\\/\\\\+&amp;%\\$#_=~]*)(\\.)([-\\w]*[0-9a-zA-Z])+(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\?\\,\'\\/\\\\+&amp;%\\$#_=~]*))+");
    45 const QString URL_PATTERN("(((ht|f|rt)(tp|sp)(s?)\\:\\/\\/)|(www|wap)(?:\\.))(([-\\w]*[0-9a-zA-Z])+(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\?\\,\'\\/\\\\+&amp;%\\$#_=~]*)(\\.)([-\\w]*[0-9a-zA-Z])+(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\?\\,\'\\/\\\\+&amp;%\\$#_=~]*))+[a-zA-Z0-9/]");
    46 
    46 
    47 //rules
    47 //rules
    48 const QString NUMBER_RULE("NUMBER_RULE");
    48 const QString NUMBER_RULE("NUMBER_RULE");
    49 const QString EMAIL_RULE("EMAIL_RULE");
    49 const QString EMAIL_RULE("EMAIL_RULE");
    50 const QString URL_RULE("URL_RULE");
    50 const QString URL_RULE("URL_RULE");
    51 
    51 
    52 //localization
    52 //localization
    53 #define LOC_OPEN_CONTACT_INFO hbTrId("txt_messaging_menu_open_contact_info")
    53 #define LOC_CONTACT_INFO hbTrId("txt_messaging_menu_contact_info")
    54 #define LOC_CALL              hbTrId("txt_common_menu_call_verb")
    54 #define LOC_CALL              hbTrId("txt_common_menu_call_verb")
    55 #define LOC_SEND_MESSAGE      hbTrId("txt_common_menu_send_message")
    55 #define LOC_SEND_MESSAGE      hbTrId("txt_common_menu_send_message")
    56 #define LOC_SAVE_TO_CONTACTS  hbTrId("txt_common_menu_save_to_contacts")
    56 #define LOC_SAVE_TO_CONTACTS  hbTrId("txt_common_menu_save_to_contacts")
    57 #define LOC_COPY              hbTrId("txt_common_menu_copy")
    57 #define LOC_COPY              hbTrId("txt_common_menu_copy")
    58 #define LOC_CREATE_EMAIL      hbTrId("txt_messaging_menu_create_mail")
    58 #define LOC_CREATE_EMAIL      hbTrId("txt_messaging_menu_create_mail")
    66 mCursorPos(-1)
    66 mCursorPos(-1)
    67 {
    67 {
    68     this->setReadOnly(true);
    68     this->setReadOnly(true);
    69     this->setScrollable(false);
    69     this->setScrollable(false);
    70     this->setCursorVisibility(Hb::TextCursorHidden);
    70     this->setCursorVisibility(Hb::TextCursorHidden);
       
    71     this->setFlag(QGraphicsItem::ItemIsFocusable,false);
    71     this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    72     this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    72     HbFrameItem *noBackground = new HbFrameItem(this);
    73     this->setBackgroundItem(0);   
    73     this->setBackgroundItem(noBackground);
       
    74 
    74 
    75     //inserting rules and patterns to map.
    75     //inserting rules and patterns to map.
    76     mRules.insert(NUMBER_RULE,NUMBER_PATTERN);
    76     mRules.insert(NUMBER_RULE,NUMBER_PATTERN);
    77     mRules.insert(EMAIL_RULE,EMAIL_PATTERN);
    77     mRules.insert(EMAIL_RULE,EMAIL_PATTERN);
    78     mRules.insert(URL_RULE,URL_PATTERN);
    78     mRules.insert(URL_RULE,URL_PATTERN);
   115     for (i = mRules.constBegin(); i != mRules.constEnd(); ++i)
   115     for (i = mRules.constBegin(); i != mRules.constEnd(); ++i)
   116     {
   116     {
   117         QString ruleName = i.key();
   117         QString ruleName = i.key();
   118         QString rule = i.value();
   118         QString rule = i.value();
   119         QRegExp ruleExp(rule);
   119         QRegExp ruleExp(rule);
       
   120         ruleExp.setCaseSensitivity(Qt::CaseInsensitive);
   120 
   121 
   121         QTextCursor cursor = this->document()->find(ruleExp);
   122         QTextCursor cursor = this->document()->find(ruleExp);
   122 
   123 
   123         while(cursor.hasSelection())
   124         while(cursor.hasSelection())
   124         {
   125         {
   175 
   176 
   176     connect(contextMenu,SIGNAL(aboutToClose()),this,SLOT(menuClosed()));
   177     connect(contextMenu,SIGNAL(aboutToClose()),this,SLOT(menuClosed()));
   177     
   178     
   178 }
   179 }
   179 
   180 
   180 
   181 void UniViewerTextItem::gestureEvent(QGestureEvent* event)
   181 void UniViewerTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   182 {
   182 {
   183     //handle gesture to highlight and dehighlight find item.
   183     HbTextEdit::mouseReleaseEvent(event);
   184     
   184 
   185     if(HbTapGesture *tap = qobject_cast<HbTapGesture*>(event->gesture(Qt::TapGesture)))
   185     highlightText(false);
   186     {
   186 
   187         //capturing gesture position, and map to local co-ordinates.
   187     QString anchor = this->anchorAt(event->pos());
   188         QPointF pos = mapFromScene(tap->scenePosition());
   188 
   189         
   189     if(!anchor.isEmpty() && !this->textCursor().hasSelection())
   190         switch (tap->state()) 
   190     {
   191         {
   191         shortTapAction(anchor);
   192             case Qt::GestureStarted:
   192     }
   193             {
   193 }
   194                 //highlight find item.
   194 
   195                 QTextDocument* doc = this->document();
   195 void UniViewerTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
   196                 mCursorPos = doc->documentLayout()->hitTest(pos, Qt::ExactHit);
   196 {
   197                 highlightText(true);
   197     HbTextEdit::mousePressEvent(event);
   198                 break;
   198 
   199             }  
   199     QTextDocument* doc = this->document();
   200             
   200 
   201             case Qt::GestureFinished:
   201     mCursorPos = doc->documentLayout()->hitTest(event->pos(), Qt::ExactHit);
   202             {
   202 
   203                 if (HbTapGesture::Tap == tap->tapStyleHint()) 
   203     highlightText(true);
   204                 {
       
   205                     //gesture is finshed dehighlight text.
       
   206                     highlightText(false);
       
   207                     
       
   208                     QString anchor = this->anchorAt(pos);
       
   209                     
       
   210                     //do short tap action.
       
   211                     if (!anchor.isEmpty() && !this->textCursor().hasSelection())
       
   212                     {
       
   213                         handleShortTap(anchor,tap->scenePosition());
       
   214                     }
       
   215                 }
       
   216                 break;
       
   217             }
       
   218             
       
   219             case Qt::GestureCanceled:
       
   220             {
       
   221                 //gesture is canceled due to pan or swipe, dehighlight text.
       
   222                 if (HbTapGesture::Tap == tap->tapStyleHint()) 
       
   223                 {
       
   224                 highlightText(false);
       
   225                 break;
       
   226                 }
       
   227             }
       
   228             default:
       
   229                 break;
       
   230         }
       
   231         event->accept();
       
   232     }
       
   233     else
       
   234     {
       
   235         event->ignore();
       
   236     }
       
   237     
       
   238     //passing gesture event to base class.
       
   239     HbTextEdit::gestureEvent(event);
   204 }
   240 }
   205 
   241 
   206 void UniViewerTextItem::addNumberMenu(HbMenu* contextMenu,const QString& data)
   242 void UniViewerTextItem::addNumberMenu(HbMenu* contextMenu,const QString& data)
   207 {
   243 {
   208     HbAction* action = NULL;
   244     HbAction* action = NULL;
   209 
   245 
   210     action = contextMenu->addAction(LOC_OPEN_CONTACT_INFO, this, SLOT(openContactInfo()));
   246     QString number = data;
   211     action->setData(data);
   247     number.remove(NUMBER_RULE);
       
   248     int contactId = MsgContactHandler::resolveContactDisplayName(
       
   249                                  number, 
       
   250                                  QContactPhoneNumber::DefinitionName,
       
   251                                  QContactPhoneNumber::FieldNumber); 
       
   252     
       
   253     if(contactId > 0)
       
   254     {
       
   255         action = contextMenu->addAction(LOC_CONTACT_INFO, this, SLOT(openContactInfo()));
       
   256         action->setData(data);
       
   257     }
       
   258     else
       
   259     {
       
   260         action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts()));
       
   261         action->setData(data);  
       
   262     }
   212 
   263 
   213     action = contextMenu->addAction(LOC_CALL, this, SLOT(call()));
   264     action = contextMenu->addAction(LOC_CALL, this, SLOT(call()));
   214     action->setData(data);
   265     action->setData(data);
   215 
   266 
   216     action = contextMenu->addAction(LOC_SEND_MESSAGE, this, SLOT(sendMessage()));
   267     action = contextMenu->addAction(LOC_SEND_MESSAGE, this, SLOT(sendMessage()));
   217     action->setData(data);
   268     action->setData(data);
   218 
   269 
       
   270     action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard()));
       
   271     action->setData(data);
       
   272 }
       
   273 
       
   274 void UniViewerTextItem::addEmailMenu(HbMenu* contextMenu,const QString& data)
       
   275 {
       
   276     HbAction* action = NULL;
       
   277     
       
   278     action = contextMenu->addAction(LOC_CREATE_EMAIL, this, SLOT(createEmail()));
       
   279     action->setData(data);
       
   280     
       
   281     action = contextMenu->addAction(LOC_CONTACT_INFO, this, SLOT(openContactInfo()));
       
   282     action->setData(data);
       
   283 
   219     action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts()));
   284     action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts()));
   220     action->setData(data);
   285     action->setData(data);
   221 
   286 
   222     action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard()));
   287     action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard()));
   223     action->setData(data);
   288     action->setData(data);
   224 }
   289 }
   225 
   290 
   226 void UniViewerTextItem::addEmailMenu(HbMenu* contextMenu,const QString& data)
   291 void UniViewerTextItem::addUrlMenu(HbMenu* contextMenu,const QString& data)
   227 {
   292 {
   228     HbAction* action = NULL;
   293     HbAction* action = NULL;
   229     
   294     
   230     action = contextMenu->addAction(LOC_CREATE_EMAIL, this, SLOT(createEmail()));
       
   231     action->setData(data);
       
   232     
       
   233     action = contextMenu->addAction(LOC_OPEN_CONTACT_INFO, this, SLOT(openContactInfo()));
       
   234     action->setData(data);
       
   235 
       
   236     action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts()));
       
   237     action->setData(data);
       
   238 
       
   239     action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard()));
       
   240     action->setData(data);
       
   241 }
       
   242 
       
   243 void UniViewerTextItem::addUrlMenu(HbMenu* contextMenu,const QString& data)
       
   244 {
       
   245     HbAction* action = NULL;
       
   246     
       
   247     action = contextMenu->addAction(LOC_OPEN_LINK, this, SLOT(openLink()));
   295     action = contextMenu->addAction(LOC_OPEN_LINK, this, SLOT(openLink()));
   248     action->setData(data);
   296     action->setData(data);
   249 
   297 
   250     action = contextMenu->addAction(LOC_ADD_TO_BOOKMARKS, this, SLOT(addToBookmarks()));
   298     action = contextMenu->addAction(LOC_ADD_TO_BOOKMARKS, this, SLOT(addToBookmarks()));
   251     action->setData(data);
   299     action->setData(data);
   252 
   300 
   253     action = contextMenu->addAction(LOC_COPY_LINK, this, SLOT(copyToClipboard()));
   301     action = contextMenu->addAction(LOC_COPY_LINK, this, SLOT(copyToClipboard()));
   254     action->setData(data);
   302     action->setData(data);
   255 }
   303 }
   256 
   304 
   257 void UniViewerTextItem::shortTapAction(QString anchor)
   305 void UniViewerTextItem::handleShortTap(QString anchor,const QPointF& pos)
   258 {
   306 {
   259     HbAction action;
   307     HbAction action;
   260     action.setData(anchor);
   308     action.setData(anchor);
   261 
   309 
   262     if(anchor.contains(NUMBER_RULE))
   310     if(anchor.contains(NUMBER_RULE))
   263     {
   311     {
   264         //open vcard template.
   312         QString data = anchor;
   265         connect(&action,SIGNAL(triggered()),this,SLOT(openContactInfo()));
   313         data.remove(NUMBER_RULE);
       
   314         int contactId = MsgContactHandler::resolveContactDisplayName(
       
   315                                      data, 
       
   316                                      QContactPhoneNumber::DefinitionName,
       
   317                                      QContactPhoneNumber::FieldNumber);        
       
   318        
       
   319         if(contactId > 0 )
       
   320         {
       
   321             //if resolved conatct open contact card 
       
   322             connect(&action,SIGNAL(triggered()),this,SLOT(openContactInfo()));
       
   323         }
       
   324         else
       
   325         {
       
   326             //unresolved contact show popup.  
       
   327             highlightText(true);
       
   328 
       
   329             HbMenu* contextMenu = new HbMenu();
       
   330             contextMenu->setDismissPolicy(HbPopup::TapAnywhere);
       
   331             contextMenu->setAttribute(Qt::WA_DeleteOnClose, true);
       
   332             contextMenu->setPreferredPos(pos); 
       
   333             connect(contextMenu,SIGNAL(aboutToClose()),this,SLOT(menuClosed()));
       
   334             
       
   335             addNumberMenu(contextMenu,anchor);
       
   336             
       
   337             contextMenu->show();
       
   338         }
   266     }
   339     }
   267     else if(anchor.contains(EMAIL_RULE))
   340     else if(anchor.contains(EMAIL_RULE))
   268     {
   341     {
   269         //Create email
   342         //Create email
   270         connect(&action,SIGNAL(triggered()),this,SLOT(createEmail()));
   343         connect(&action,SIGNAL(triggered()),this,SLOT(createEmail()));
   306     {
   379     {
   307         QString phoneNumber = action->data().toString();
   380         QString phoneNumber = action->data().toString();
   308         phoneNumber.remove(NUMBER_RULE);
   381         phoneNumber.remove(NUMBER_RULE);
   309         
   382         
   310         //invoke dialer service and pass phoneNumber.        
   383         //invoke dialer service and pass phoneNumber.        
   311         QString serviceName("com.nokia.services.telephony");
   384         QString serviceName("com.nokia.symbian.ICallDial");
   312         QString operation("dial(QString)");
   385         QString operation("dial(QString)");
   313         
   386         
   314         XQServiceRequest* serviceRequest = new XQServiceRequest(serviceName,operation,false);
   387         XQServiceRequest* serviceRequest = new XQServiceRequest(serviceName,operation,false);
   315         
   388         
   316         connect(serviceRequest, SIGNAL(requestCompleted(QVariant)),
   389         connect(serviceRequest, SIGNAL(requestCompleted(QVariant)),
   346     {
   419     {
   347         QString emailId = action->data().toString();
   420         QString emailId = action->data().toString();
   348         emailId.remove(EMAIL_RULE);
   421         emailId.remove(EMAIL_RULE);
   349 
   422 
   350         // Launch email editor
   423         // Launch email editor
   351         QString serviceName("com.nokia.services.commonemail");
   424         QString interfaceName("com.nokia.symbian.IEmailMessageSend");
   352         QString interfaceName("imessage.send");
       
   353         QString operation("send(QVariant)");
   425         QString operation("send(QVariant)");
   354         XQApplicationManager appManager;
   426         XQApplicationManager appManager;
   355         XQAiwRequest* request = appManager.create(serviceName, interfaceName, 
   427         XQAiwRequest* request = appManager.create(interfaceName, 
   356 			operation, true); 
   428 			operation, true); 
   357         if ( request == NULL )
   429         if ( request == NULL )
   358             {
   430             {
   359             return;       
   431             return;       
   360             }
   432             }
   388     if(action)
   460     if(action)
   389     {
   461     {
   390         QString url = action->data().toString();
   462         QString url = action->data().toString();
   391         url.remove(URL_RULE);
   463         url.remove(URL_RULE);
   392         //invoke browser service  & pass url.
   464         //invoke browser service  & pass url.
       
   465         QUrl uri(url);
       
   466         QDesktopServices::openUrl(uri);
   393     }
   467     }
   394 }
   468 }
   395 
   469 
   396 void UniViewerTextItem::addToBookmarks()
   470 void UniViewerTextItem::addToBookmarks()
   397 {
   471 {
   494 }
   568 }
   495 
   569 
   496 
   570 
   497 void UniViewerTextItem::saveToContacts()
   571 void UniViewerTextItem::saveToContacts()
   498 {
   572 {
   499     //handler for save to contacts.
   573      openContactInfo();
   500 }
   574 }
   501 
   575 
   502 void UniViewerTextItem::onServiceRequestCompleted()
   576 void UniViewerTextItem::onServiceRequestCompleted()
   503     {
   577     {
   504     //service request is now complete. delete it.
   578     //service request is now complete. delete it.