emailuis/nmailuiwidgets/src/nmrecipientlineedit.cpp
changeset 18 578830873419
child 20 ecc8def7944a
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     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: nmrecipientlineedit.cpp
       
    15 * 
       
    16 */
       
    17 
       
    18 #include "nmailuiwidgetsheaders.h"
       
    19 
       
    20 static const QString Semicolon(";");
       
    21 static const QString Delimiter("; ");
       
    22 static const QRegExp CommaOrSemicolon("[,;]");
       
    23 
       
    24 
       
    25 /*!
       
    26    Constructor
       
    27 */
       
    28 NmRecipientLineEdit::NmRecipientLineEdit(QGraphicsItem *parent) 
       
    29     : NmHtmlLineEdit(parent),
       
    30     mNeedToGenerateEmailAddressList(true)
       
    31 {
       
    32     connect(this, SIGNAL(textChanged(const QString &)), 
       
    33             this, SLOT(handleTextChanged(const QString &)));
       
    34 }
       
    35 
       
    36 
       
    37 /*!
       
    38    Destructor
       
    39 */
       
    40 NmRecipientLineEdit::~NmRecipientLineEdit()
       
    41 {
       
    42 }
       
    43 
       
    44 
       
    45 /*!
       
    46    Get the emailaddress list generated from the content of the lineedit.
       
    47 */
       
    48 QList<NmAddress> NmRecipientLineEdit::emailAddressList()
       
    49 {
       
    50     if (mNeedToGenerateEmailAddressList) {
       
    51         // Empty mEmailAddressList.
       
    52         mEmailAddressList.clear();
       
    53         // Generate mEmailAddressList from the lineedit content.
       
    54         generateEmailAddressList();					
       
    55         mNeedToGenerateEmailAddressList = false;
       
    56     }
       
    57 
       
    58     return mEmailAddressList;	
       
    59 }
       
    60 
       
    61 
       
    62 #ifdef Q_OS_SYMBIAN
       
    63 /*!
       
    64    This Slot inserts the contacts selected from PhoneBook at the lineedit cursor position.
       
    65 */
       
    66 void NmRecipientLineEdit::insertSelectedContacts(const QVariant &selectedContacts)
       
    67 {
       
    68     CntServicesContactList contactList;
       
    69     contactList = qVariantValue<CntServicesContactList>(selectedContacts);
       
    70 
       
    71     if (contactList.count() == 0) {	
       
    72 // Using HbMessageBox or any other dialogs e.g HbNotificationDialog causes XQServiceRequest crash!
       
    73 //     // "No contact returned" will be replaced by a hbTrId.
       
    74 //     HbMessageBox note(tr("No contact returned"), HbMessageBox::MessageTypeInformation);
       
    75 //     note.setTimeout(HbMessageBox::NoTimeout);
       
    76 //     note.exec();
       
    77     }
       
    78     else {
       
    79         // Loop through all the contacts selected from PhoneBook.
       
    80         for (int i = 0; i < contactList.count(); ++i) {
       
    81             QString contactEmailAddress = contactList[i].mEmailAddress;
       
    82             QString contactName = contactList[i].mDisplayName;
       
    83 
       
    84             // If this contact has no name.
       
    85             if(contactName.isEmpty()) {	
       
    86             // Insert this contact's emailaddress.			
       
    87             insertText(contactEmailAddress);
       
    88             }
       
    89             else {
       
    90                 insertText(contactName); 
       
    91             }
       
    92 
       
    93             // Generate custom keyevent for Delimiter("; ").
       
    94             QKeyEvent delimiterKeyEvent(QEvent::KeyPress, Qt::Key_unknown, Qt::NoModifier, Delimiter);
       
    95             // Forward the delimiterKeyEvent to base class to handle.
       
    96             NmHtmlLineEdit::keyPressEvent(&delimiterKeyEvent);
       
    97 			
       
    98             // Form the contact into Qmail NmAddress format.
       
    99             NmAddress contact;
       
   100             contact.setAddress(contactEmailAddress);
       
   101             contact.setDisplayName(contactName);
       
   102             
       
   103             // Add this NmAddress formated contact into mContactsSelectedFromPhoneBook.
       
   104             mContactsSelectedFromPhoneBook.append(contact);
       
   105         }
       
   106     }
       
   107 }
       
   108 
       
   109 Q_IMPLEMENT_USER_METATYPE(CntServicesContact)
       
   110 Q_IMPLEMENT_USER_METATYPE_NO_OPERATORS(CntServicesContactList)
       
   111 #endif   // Q_OS_SYMBIAN
       
   112 
       
   113 
       
   114 /*!
       
   115    This Slot inserts text at the lineedit cursor position 
       
   116    In the future when underlining some recipient is requested, 
       
   117    the implementation will be replaced with QTextCursor, QTextCharFormat, so on.
       
   118 */
       
   119 void NmRecipientLineEdit::insertText(const QString &text)
       
   120 {
       
   121     // Loop through all the characters from the text.
       
   122     for (int i = 0; i != text.count(); ++i) {
       
   123         QString character(text[i]);
       
   124         // Generate custom keyevent for this character.
       
   125         QKeyEvent charKeyEvent(QEvent::KeyPress, Qt::Key_unknown, Qt::NoModifier, character);
       
   126         // Forward the charKeyEvent to base class to handle.
       
   127         NmHtmlLineEdit::keyPressEvent(&charKeyEvent);
       
   128     }
       
   129 }
       
   130 
       
   131 
       
   132 /*!
       
   133    keyPressEvent handles replacing user inputs "," or ";" from physical keyboard with "; "
       
   134    P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard,
       
   135    inputMethodEvent method handles user inputs "," or ";" from virtual keyboard.
       
   136 */
       
   137 void NmRecipientLineEdit::keyPressEvent(QKeyEvent *keyEvent)
       
   138 {
       
   139     if (keyEvent) {
       
   140         switch (keyEvent->key()) {    
       
   141         case Qt::Key_Comma:
       
   142         case Qt::Key_Semicolon:
       
   143             {
       
   144                 QString textBeforeCursor = (this->text()).left(this->cursorPosition());
       
   145                 
       
   146                 // No action when the lineedit is empty or cursor is after a Delimiter("; ") 
       
   147                 // or a Semicolon (";").
       
   148                 if ((this->text()).isEmpty() || textBeforeCursor.endsWith(Delimiter)
       
   149                     || textBeforeCursor.endsWith(Semicolon)) {
       
   150                     keyEvent->ignore();
       
   151                 }
       
   152                 else {
       
   153                     // Generate custom keyevent for Delimiter("; ") and 
       
   154                     // forward to the base class to handle.
       
   155                     QKeyEvent delimiterKeyEvent(keyEvent->type(), keyEvent->key(),
       
   156                     keyEvent->modifiers(), Delimiter);
       
   157                     NmHtmlLineEdit::keyPressEvent(&delimiterKeyEvent);
       
   158                 }
       
   159             }
       
   160         break;
       
   161 
       
   162         default:
       
   163             NmHtmlLineEdit::keyPressEvent(keyEvent);
       
   164         break;
       
   165         }
       
   166     }
       
   167     else {
       
   168         NmHtmlLineEdit::keyPressEvent(keyEvent);
       
   169     }
       
   170 }
       
   171 
       
   172 
       
   173 /*!
       
   174    inputMethodEvent handles replacing user inputs "," or ";" from virtual keyboard with "; ".
       
   175    P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard
       
   176 */
       
   177 void NmRecipientLineEdit::inputMethodEvent(QInputMethodEvent *event)
       
   178 {
       
   179     if (event) {
       
   180         QString eventText = event->commitString();
       
   181 		
       
   182         if (!eventText.isEmpty() || event->replacementLength()) {
       
   183             // If typed charater from virtual keyboard is "," or ";"
       
   184             if (eventText.contains(CommaOrSemicolon)) {
       
   185                 QString textBeforeCursor = (this->text()).left(this->cursorPosition());
       
   186 				
       
   187                 // No action when the lineedit is empty or cursor is after a Delimiter("; ") 
       
   188                 // or Semicolon (";").
       
   189                 if ((this->text()).isEmpty() || textBeforeCursor.endsWith(Delimiter)
       
   190                     || textBeforeCursor.endsWith(Semicolon)) {
       
   191                     event->ignore();
       
   192                 }
       
   193                 else {
       
   194                     // Modify event with Delimiter("; ") and forward to the base class to handle.
       
   195                     event->setCommitString(Delimiter, event->replacementStart(),
       
   196                                            event->replacementLength()); 
       
   197                     NmHtmlLineEdit::inputMethodEvent(event);
       
   198                 }
       
   199             }
       
   200             else {  
       
   201                 NmHtmlLineEdit::inputMethodEvent(event);
       
   202             }
       
   203         }
       
   204         else { 
       
   205             NmHtmlLineEdit::inputMethodEvent(event);
       
   206         }
       
   207     }
       
   208     else {   
       
   209         NmHtmlLineEdit::inputMethodEvent(event);
       
   210     }
       
   211 }
       
   212 
       
   213  
       
   214 /*!
       
   215    Generate emailaddress list from the content of the lineedit.
       
   216 */
       
   217 void NmRecipientLineEdit::generateEmailAddressList()
       
   218 {   
       
   219     // Remove whitespace from the start and the end of the lineedit content. 
       
   220     QString contentOfLineedit = (this->text()).trimmed();
       
   221     
       
   222     // Split the lineedit content into individual items wherever a Semicolon(";") occurs, 
       
   223     // empty entries don't appear in the result.
       
   224     QStringList itemsOfLineeditContent = contentOfLineedit.split(Semicolon, QString::SkipEmptyParts);
       
   225         
       
   226     // Loop through all the items in the itemsOfLineeditContent list.
       
   227     for (int i = 0; i != itemsOfLineeditContent.count(); ++i) {
       
   228         // Remove whitespace from the start and the end of the item.
       
   229         QString itemInLineedit = itemsOfLineeditContent.at(i).trimmed();
       
   230 
       
   231         if (mContactsSelectedFromPhoneBook.count() > 0) {
       
   232             // Loop through all the elements in the mContactsSelectedFromPhoneBook list.
       
   233             for (int j = 0; j != mContactsSelectedFromPhoneBook.count(); ++j) {
       
   234                 NmAddress contact = mContactsSelectedFromPhoneBook.at(j);		
       
   235                 // If the item matches either the name or the emailaddress of this contact.
       
   236                 if (itemInLineedit == contact.displayName() || itemInLineedit == contact.address()) {
       
   237                     // Add the contact into mEmailAddressList.
       
   238                     mEmailAddressList.append(contact);  
       
   239                 }
       
   240                 else {
       
   241                     // Form the item into Qmail NmAddress format.
       
   242                     NmAddress recipient;
       
   243                     recipient.setAddress(itemInLineedit);
       
   244                     recipient.setDisplayName(itemInLineedit);
       
   245                     // Add this NmAddress formated lineedit item into mEmailAddressList.
       
   246                     mEmailAddressList.append(recipient);  
       
   247                 }
       
   248             }
       
   249         }
       
   250         else {
       
   251             // Form the item into Qmail NmAddress format.
       
   252             NmAddress recipient;
       
   253             recipient.setAddress(itemInLineedit);
       
   254             recipient.setDisplayName(itemInLineedit);           
       
   255             // Add this NmAddress formated lineedit item into mEmailAddressList.
       
   256             mEmailAddressList.append(recipient);  
       
   257         }
       
   258     }
       
   259 }
       
   260 
       
   261 
       
   262 /*!
       
   263    This Slot is called when the lineedit text changes.
       
   264 */
       
   265 void NmRecipientLineEdit::handleTextChanged(const QString &text)
       
   266 {
       
   267     Q_UNUSED(text);
       
   268     mNeedToGenerateEmailAddressList = true;
       
   269 }