emailuis/nmailuiwidgets/src/nmrecipientlineedit.cpp
branchGCC_SURGE
changeset 55 cdd802add233
parent 54 997a02608b3a
child 75 47d84de1c893
equal deleted inserted replaced
28:011f79704660 55:cdd802add233
    27 */
    27 */
    28 NmRecipientLineEdit::NmRecipientLineEdit(QGraphicsItem *parent) 
    28 NmRecipientLineEdit::NmRecipientLineEdit(QGraphicsItem *parent) 
    29     : NmHtmlLineEdit(parent),
    29     : NmHtmlLineEdit(parent),
    30     mNeedToGenerateEmailAddressList(true)
    30     mNeedToGenerateEmailAddressList(true)
    31 {
    31 {
       
    32     NM_FUNCTION;
       
    33     
    32     connect(this, SIGNAL(textChanged(QString)), this, SLOT(handleTextChanged(QString)));
    34     connect(this, SIGNAL(textChanged(QString)), this, SLOT(handleTextChanged(QString)));
    33 }
    35 }
    34 
    36 
    35 
    37 
    36 /*!
    38 /*!
    37    Destructor
    39    Destructor
    38 */
    40 */
    39 NmRecipientLineEdit::~NmRecipientLineEdit()
    41 NmRecipientLineEdit::~NmRecipientLineEdit()
    40 {
    42 {
       
    43     NM_FUNCTION;
    41 }
    44 }
    42 
    45 
    43 
    46 
    44 /*!
    47 /*!
    45    Get the emailaddress list generated from the content of the lineedit.
    48    Get the emailaddress list generated from the content of the lineedit.
    46 */
    49 */
    47 QList<NmAddress> NmRecipientLineEdit::emailAddressList()
    50 QList<NmAddress> NmRecipientLineEdit::emailAddressList()
    48 {
    51 {
       
    52     NM_FUNCTION;
       
    53     
    49     if (mNeedToGenerateEmailAddressList) {
    54     if (mNeedToGenerateEmailAddressList) {
    50         // Empty mEmailAddressList.
    55         // Empty mEmailAddressList.
    51         mEmailAddressList.clear();
    56         mEmailAddressList.clear();
    52         // Generate mEmailAddressList from the lineedit content.
    57         // Generate mEmailAddressList from the lineedit content.
    53         generateEmailAddressList();					
    58         generateEmailAddressList();					
    58 }
    63 }
    59 
    64 
    60 
    65 
    61 #ifdef Q_OS_SYMBIAN
    66 #ifdef Q_OS_SYMBIAN
    62 /*!
    67 /*!
    63    This Slot inserts the selected contacts from Contacts-picker into the lineedit cursor position.    
    68    This Slot appends the selected contacts to the end of the lineedit content.    
    64    "You shouldn't be able to convert the parameter selectedContacts into a QStringlist or QString,
    69 */
    65    you need to convert selectedContacts into a CntServicesContactList." -- Comments from 
    70 void NmRecipientLineEdit::addSelectedContacts(const QVariant &selectedContacts)
    66    Contacts-picker author Erkinheimo Joonas (Nokia-D/Espoo)
    71 {
    67    Contacts-Picker should be working in TB 10.1 MCL wk16 release, 
    72     NM_FUNCTION;
    68    Custom metatypes problem will be fixed in wk16 by QtHighway.
    73     
    69 */
    74     // If user selected contact
    70 void NmRecipientLineEdit::insertSelectedContacts(const QVariant &selectedContacts)
    75     if (!selectedContacts.isNull()) {        
    71 {
    76 
    72     if (!selectedContacts.isNull()) {
    77         // If the lineedit is not empty and if there is no ";" or "; " at the end,
    73         CntServicesContactList contactList;
    78         // add a delimiter("; ") at the end.
    74         contactList = qVariantValue<CntServicesContactList>(selectedContacts);
    79         if (this->text().length() != 0 && !(this->text().endsWith(Semicolon)) && 
       
    80             !(this->text().endsWith(Delimiter))){
       
    81             
       
    82             // Move cursor to the end of the lineedit.
       
    83             this->setCursorPosition(this->text().length());       
       
    84             QTextCursor textCursor(this->textCursor());
       
    85             // Append delimiter("; ") to the end of the lineedit
       
    86             textCursor.insertText(Delimiter);
       
    87         }
       
    88         
       
    89         CntServicesContactList contactList = qVariantValue<CntServicesContactList>(selectedContacts);
    75 
    90 
    76         // Loop through all the selected contacts.
    91         // Loop through all the selected contacts.
    77         for (int i = 0; i < contactList.count(); ++i) {
    92         for (int i = 0; i < contactList.count(); ++i) {
       
    93             QString contactName = contactList[i].mDisplayName;
    78             QString contactEmailAddress = contactList[i].mEmailAddress;
    94             QString contactEmailAddress = contactList[i].mEmailAddress;
    79             QString contactName = contactList[i].mDisplayName;
    95             
    80 
    96             // If this contact has no name, use it's emailaddress as the display name
    81             // If this contact has no name.
    97             if(contactName.isEmpty()) {	
    82             if(contactName.isEmpty()) {				
    98                 // Move cursor to the end of the lineedit.
    83                 // Generate a custom keyevent for this contact's emailaddress.
    99                 this->setCursorPosition(this->text().length());       
    84                 QKeyEvent contactEmailAddressKeyEvent(QEvent::KeyPress, Qt::Key_unknown, 
   100                 QTextCursor textCursor(this->textCursor());
    85                 		                              Qt::NoModifier, contactEmailAddress);
   101                 // Append contactEmailAddress to the end of the lineedit
    86                 // Forward this contactEmailAddressKeyEvent to base class to handle.
   102                 textCursor.insertText(contactEmailAddress);
    87                 NmHtmlLineEdit::keyPressEvent(&contactEmailAddressKeyEvent);
   103             }
    88             }
   104             // If this contact has name, use the name as the display name
    89             else {
   105             else {
    90                 // Handle a rare case: there's another contact has same name but has different emailaddress.
   106                 // Handle a rare case: there are contacts has same name but different emailaddress.
    91                 for (int i = 0; i != mContactsSelectedFromPhoneBook.count(); ++i) {
   107                 for (int i = 0; i != mRecipientsAddedFromContacts.count(); ++i) {
    92                     if (mContactsSelectedFromPhoneBook.at(i).displayName() == contactName &&
   108                     if (mRecipientsAddedFromContacts.at(i).displayName() == contactName &&
    93                   	    mContactsSelectedFromPhoneBook.at(i).address() != contactEmailAddress) {
   109                   	    mRecipientsAddedFromContacts.at(i).address() != contactEmailAddress) {
    94                         // Differentiate this contact's name by adding a * mark
   110                         // Differentiate this contact by supplying it's emailaddress
    95                         contactName.append("*");
   111                         contactName.append("<");
       
   112                         contactName.append(contactEmailAddress);
       
   113                         contactName.append(">");
    96                     }
   114                     }
    97                 }
   115                 }
    98                 
   116                 
    99                 // Generate custom keyevent for this contact's name.
   117                 // Move cursor to the end of the lineedit.
   100                 QKeyEvent contactNameKeyEvent(QEvent::KeyPress, Qt::Key_unknown, 
   118                 this->setCursorPosition(this->text().length());       
   101                                               Qt::NoModifier, contactName);
   119                 QTextCursor textCursor(this->textCursor());
   102                 // Forward this contactNameKeyEvent to base class to handle.
   120                 // Append contactName to the end of the lineedit
   103                 NmHtmlLineEdit::keyPressEvent(&contactNameKeyEvent);
   121                 textCursor.insertText(contactName);
   104             }
   122             }
   105 
   123                 
   106             // Generate custom keyevent for Delimiter("; ").
   124             QTextCursor textCursor(this->textCursor());
   107             QKeyEvent delimiterKeyEvent(QEvent::KeyPress, Qt::Key_unknown, 
   125             // Append delimiter("; ")
   108                                         Qt::NoModifier, Delimiter);
   126             textCursor.insertText(Delimiter);
   109             // Forward the delimiterKeyEvent to base class to handle.
   127             
   110             NmHtmlLineEdit::keyPressEvent(&delimiterKeyEvent);
       
   111 			
       
   112             // Form the contact into Qmail NmAddress format.
   128             // Form the contact into Qmail NmAddress format.
   113             NmAddress contact;
   129             NmAddress contact;
   114             contact.setAddress(contactEmailAddress);
   130             contact.setAddress(contactEmailAddress);
   115             contact.setDisplayName(contactName);
   131             contact.setDisplayName(contactName);
   116             
   132             
   117             // Add this NmAddress formated contact into mContactsSelectedFromPhoneBook.
   133             // Add this NmAddress formated contact into mRecipientsAddedFromContacts.
   118             mContactsSelectedFromPhoneBook.append(contact);
   134             mRecipientsAddedFromContacts.append(contact);
   119         }
   135         } 
   120     }
   136     }
   121     else {
   137     else {
   122         //Request returned NULL 
   138         //Request returned NULL 
   123         NMLOG("ContactsPicker request returned NULL.");
   139         NM_COMMENT("ContactsPicker request returned NULL.");
   124     }
   140     }
   125         
   141         
   126 }
   142 }
   127 
   143 
   128 Q_IMPLEMENT_USER_METATYPE(CntServicesContact)
   144 Q_IMPLEMENT_USER_METATYPE(CntServicesContact)
   135    P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard,
   151    P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard,
   136    inputMethodEvent method handles user inputs "," or ";" from virtual keyboard.
   152    inputMethodEvent method handles user inputs "," or ";" from virtual keyboard.
   137 */
   153 */
   138 void NmRecipientLineEdit::keyPressEvent(QKeyEvent *keyEvent)
   154 void NmRecipientLineEdit::keyPressEvent(QKeyEvent *keyEvent)
   139 {
   155 {
       
   156     NM_FUNCTION;
       
   157     
   140 	bool eventHandled = false;
   158 	bool eventHandled = false;
   141 	
   159 	
   142     if (keyEvent) {
   160     if (keyEvent) {
   143         switch (keyEvent->key()) {    
   161         switch (keyEvent->key()) {    
   144         case Qt::Key_Comma:
   162         case Qt::Key_Comma:
   182    inputMethodEvent handles replacing user inputs "," or ";" from virtual keyboard with "; ".
   200    inputMethodEvent handles replacing user inputs "," or ";" from virtual keyboard with "; ".
   183    P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard
   201    P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard
   184 */
   202 */
   185 void NmRecipientLineEdit::inputMethodEvent(QInputMethodEvent *event)
   203 void NmRecipientLineEdit::inputMethodEvent(QInputMethodEvent *event)
   186 {
   204 {
       
   205     NM_FUNCTION;
       
   206     
   187 	bool eventHandled = false;
   207 	bool eventHandled = false;
   188 	
   208 	
   189     if (event) {
   209     if (event) {
   190         QString eventText = event->commitString();
   210         QString eventText = event->commitString();
   191 
   211 
   218     }
   238     }
   219 }
   239 }
   220 
   240 
   221  
   241  
   222 /*!
   242 /*!
   223    Generate emailaddress list from the content of the lineedit.
   243    Generate a list of all the email addresses from the content of the lineedit.
   224 */
   244 */
   225 void NmRecipientLineEdit::generateEmailAddressList()
   245 void NmRecipientLineEdit::generateEmailAddressList()
   226 {   
   246 {   
       
   247     NM_FUNCTION;
       
   248     
   227     // Remove whitespace from the start and the end of the lineedit content. 
   249     // Remove whitespace from the start and the end of the lineedit content. 
   228     QString contentOfLineedit = (this->text()).trimmed();
   250     QString contentOfLineedit = (this->text()).trimmed();
   229     
   251     
   230     // Split the lineedit content into individual items wherever a Semicolon(";") occurs, 
   252     // Split the lineedit content by semicolon(";").
   231     // empty entries don't appear in the result.
       
   232     QStringList itemsOfLineeditContent = contentOfLineedit.split(Semicolon, QString::SkipEmptyParts);
   253     QStringList itemsOfLineeditContent = contentOfLineedit.split(Semicolon, QString::SkipEmptyParts);
   233         
   254         
   234     // Loop through all the items in the itemsOfLineeditContent list.
   255     // Loop through all the items of the lineedit content.
   235     for (int i = 0; i != itemsOfLineeditContent.count(); ++i) {
   256     for (int i = 0; i != itemsOfLineeditContent.count(); ++i) {
   236         // Remove whitespace from the start and the end of the item.
   257         // Remove whitespace from the start and the end of the item.
   237         QString itemInLineedit = itemsOfLineeditContent.at(i).trimmed();
   258         QString itemInLineedit = itemsOfLineeditContent.at(i).trimmed();
   238 
   259 
   239         if (mContactsSelectedFromPhoneBook.count() > 0) {
   260         // Get the count of the recipients added from Contacts.
   240             // Loop through all the elements in the mContactsSelectedFromPhoneBook list.
   261         int countOfRecipientsAddedFromContacts = mRecipientsAddedFromContacts.count();
   241             for (int j = 0; j != mContactsSelectedFromPhoneBook.count(); ++j) {
   262         
   242                 NmAddress contact = mContactsSelectedFromPhoneBook.at(j);		
   263         // If there is recipient added from Contacts.
   243                 // If the item matches either the name or the emailaddress of this contact.
   264         if (countOfRecipientsAddedFromContacts > 0) {
   244                 if (itemInLineedit == contact.displayName() || itemInLineedit == contact.address()) {
   265             QStringList listOfAddedContactsName;
   245                     // Add the contact into mEmailAddressList.
   266             QStringList listOfAddedContactsAddress;
   246                     mEmailAddressList.append(contact);  
   267             
   247                 }
   268             // Loop through all the recipients added from Contacts.
   248                 else {
   269             for (int j = 0; j != countOfRecipientsAddedFromContacts; ++j) {
   249                     // Form the item into Qmail NmAddress format.
   270                 NmAddress contact = mRecipientsAddedFromContacts.at(j);          
   250                     NmAddress recipient;
   271                 listOfAddedContactsName.append(contact.displayName());
   251                     recipient.setAddress(itemInLineedit);
   272                 listOfAddedContactsAddress.append(contact.address());
   252                     // no display name info available, so don't us it
   273             }
   253                     recipient.setDisplayName(QString()); 
   274                 
   254                     // Add this NmAddress formated lineedit item into mEmailAddressList.
   275             int indexInAddedContactsName = listOfAddedContactsName.indexOf(itemInLineedit);
   255                     mEmailAddressList.append(recipient);  
   276             int indexInAddedContactsAddress = listOfAddedContactsAddress.indexOf(itemInLineedit);
   256                 }
   277             
   257             }
   278             // If this itemInLineedit matches the name of one added contact.
   258         }
   279             if (indexInAddedContactsName >= 0) {
   259         else {
   280                 // Add the recipient into mEmailAddressList.
   260             // Form the item into Qmail NmAddress format.
   281                 mEmailAddressList.append(mRecipientsAddedFromContacts.at(indexInAddedContactsName));  
       
   282             }
       
   283             // If this itemInLineedit matches the emailaddress of one added contact.
       
   284             else if (indexInAddedContactsAddress >= 0) { 
       
   285                 // Add the recipient into mEmailAddressList.
       
   286                 mEmailAddressList.append(mRecipientsAddedFromContacts.at(indexInAddedContactsAddress));  
       
   287             }
       
   288             // This itemInLineedit is not added from Contacts
       
   289             else { 
       
   290                 // Form the item into NmAddress format.
       
   291                 NmAddress recipient;
       
   292                 recipient.setAddress(itemInLineedit);
       
   293                 // There is no display name info available, so leave display name empty.
       
   294                 recipient.setDisplayName(QString()); 
       
   295                 // Add this NmAddress formated lineedit item into mEmailAddressList.
       
   296                 mEmailAddressList.append(recipient);  
       
   297             }
       
   298         }
       
   299         else { // There is no recipient is added from Contacts
       
   300             // Form the item into NmAddress format.
   261             NmAddress recipient;
   301             NmAddress recipient;
   262             recipient.setAddress(itemInLineedit);
   302             recipient.setAddress(itemInLineedit);
   263             // no display name info available, so don't us it
   303             // There is no display name info available, so leave display name emapty.
   264             recipient.setDisplayName(QString()); 
   304             recipient.setDisplayName(QString()); 
   265             // Add this NmAddress formated lineedit item into mEmailAddressList.
   305             // Add this NmAddress formated lineedit item into mEmailAddressList.
   266             mEmailAddressList.append(recipient);  
   306             mEmailAddressList.append(recipient);  
   267         }
   307         }
   268     }
   308     }
   272 /*!
   312 /*!
   273    This Slot is called when the lineedit text changes.
   313    This Slot is called when the lineedit text changes.
   274 */
   314 */
   275 void NmRecipientLineEdit::handleTextChanged(const QString &text)
   315 void NmRecipientLineEdit::handleTextChanged(const QString &text)
   276 {
   316 {
       
   317     NM_FUNCTION;
       
   318     
   277     Q_UNUSED(text);
   319     Q_UNUSED(text);
   278     mNeedToGenerateEmailAddressList = true;
   320     mNeedToGenerateEmailAddressList = true;
   279 }
   321 }