emailuis/nmailui/src/nmutilities.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     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: UI utilities class
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmuiheaders.h"
       
    19 
       
    20 static const int NmMegabyte = 1048576;
       
    21 static const qreal NmMinAttachmentSize = 0.1;
       
    22 // taken from http://www.regular-expressions.info/email.html
       
    23 static const QRegExp NmEmailAddressPattern("[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
       
    24                                          "(?:"
       
    25                                          "\\."
       
    26                                          "[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
       
    27                                          ")*"
       
    28                                          "@"
       
    29                                          "(?:"
       
    30                                          "[A-Za-z\\d]"
       
    31                                          "(?:"
       
    32                                          "[A-Za-z\\d-]*[A-Za-z\\d]"
       
    33                                          ")?"
       
    34                                          "\\."
       
    35                                          ")+"
       
    36                                          "[A-Za-z\\d]"
       
    37                                          "(?:"
       
    38                                          "[A-Za-z\\d-]*[A-Za-z\\d]"
       
    39                                          ")?"
       
    40                                         );
       
    41 
       
    42 /*!
       
    43    Gets valid, invalid or all the recipients from a message
       
    44  */
       
    45 void NmUtilities::getRecipientsFromMessage( const NmMessage &message,
       
    46     QList<NmAddress> &recipients,
       
    47     NmAddressValidationType type )
       
    48 {
       
    49     NM_FUNCTION;
       
    50 
       
    51     // Get envelope from message
       
    52     const NmMessageEnvelope &env = message.envelope();
       
    53     
       
    54     // validate TO addresses
       
    55     QList<NmAddress> toRecipients = env.toRecipients();
       
    56     int recipientCount = toRecipients.count();
       
    57 
       
    58     for (int i = 0; i < recipientCount; ++i) {
       
    59         bool validAddress = isValidEmailAddress(toRecipients.at(i).address());
       
    60 
       
    61         if (type == NmDefault ||
       
    62             type == NmValidAddress && validAddress ||
       
    63             type == NmInvalidAddress && !validAddress) {
       
    64             recipients.append(toRecipients.at(i));
       
    65         }
       
    66     }
       
    67 
       
    68     // validate CC addresses
       
    69     QList<NmAddress> ccRecipients = env.ccRecipients();
       
    70     recipientCount = ccRecipients.count();
       
    71 
       
    72     for (int i = 0; i < recipientCount; ++i) {
       
    73         bool validAddress = isValidEmailAddress(ccRecipients.at(i).address());
       
    74 
       
    75         if (type == NmDefault ||
       
    76             type == NmValidAddress && validAddress ||
       
    77             type == NmInvalidAddress && !validAddress) {
       
    78             recipients.append(ccRecipients.at(i));
       
    79         }
       
    80     }
       
    81 
       
    82     // validate BCC addresses
       
    83     QList<NmAddress> bccRecipients = env.bccRecipients();
       
    84     recipientCount = bccRecipients.count();
       
    85 
       
    86     for (int i = 0; i < recipientCount; ++i) {
       
    87         bool validAddress = isValidEmailAddress(bccRecipients.at(i).address());
       
    88 
       
    89         if (type == NmDefault ||
       
    90             type == NmValidAddress && validAddress ||
       
    91             type == NmInvalidAddress && !validAddress) {
       
    92             recipients.append(bccRecipients.at(i));
       
    93         }
       
    94     }
       
    95 }
       
    96 
       
    97 /*!
       
    98   Validates the given string against an email address pattern.
       
    99 */
       
   100 bool NmUtilities::isValidEmailAddress( const QString &emailAddress )
       
   101 {
       
   102     NM_FUNCTION;
       
   103 
       
   104     return NmEmailAddressPattern.exactMatch(emailAddress);
       
   105 }
       
   106 
       
   107 /*!
       
   108   Generates a display string from an NmAddress object.
       
   109 */
       
   110 QString NmUtilities::addressToDisplayName( const NmAddress &address )
       
   111 {
       
   112     NM_FUNCTION;
       
   113 
       
   114     QString emailAddress = address.address();
       
   115     QString displayName = address.displayName();
       
   116 
       
   117     QString ret;
       
   118     if (displayName.length() > 0 && displayName != emailAddress) {
       
   119         ret = displayName + " <" + emailAddress + ">";
       
   120     }
       
   121     else {
       
   122         ret = emailAddress;
       
   123     }
       
   124     return ret;
       
   125 }
       
   126 
       
   127 /*!
       
   128   Returns an NmAddress object that is parsed from a display name string.
       
   129 */
       
   130 bool NmUtilities::parseEmailAddress( const QString &emailAddress, NmAddress &address )
       
   131 {
       
   132     NM_FUNCTION;
       
   133 
       
   134     bool foundAddress(false);
       
   135 
       
   136     QRegExp rx(NmEmailAddressPattern);
       
   137     // locate the email address in the string
       
   138     int pos = rx.indexIn(emailAddress);
       
   139     if (pos != -1) {
       
   140         // extract the email address...
       
   141         int matchedLen = rx.matchedLength();
       
   142         QString addr = emailAddress.mid(pos, matchedLen);
       
   143         address.setAddress(addr);
       
   144 
       
   145         // ...and use the rest as display name
       
   146         QString name = emailAddress.left(pos) + emailAddress.mid(pos + matchedLen);
       
   147         address.setDisplayName(cleanupDisplayName(name));
       
   148 
       
   149         foundAddress = true;
       
   150     }
       
   151 
       
   152     return foundAddress;
       
   153 }
       
   154 
       
   155 /*!
       
   156   Cleans up display name by stripping extra characters from the beginning and end of the string.
       
   157 */
       
   158 QString NmUtilities::cleanupDisplayName( const QString &displayName )
       
   159 {
       
   160     NM_FUNCTION;
       
   161 
       
   162     // find the first and last position that is NOT one of the characters below
       
   163     QRegExp rx("[^\\s\"<>]");
       
   164     int firstPos = std::max(rx.indexIn(displayName), 0);
       
   165     int lastPos = rx.lastIndexIn(displayName);
       
   166 
       
   167     if (lastPos < 0) {
       
   168         lastPos = displayName.length() - 1;
       
   169     }
       
   170 
       
   171     return displayName.mid(firstPos, lastPos - firstPos + 1);
       
   172 }
       
   173 
       
   174 /*!
       
   175   Opens file specified by XQSharableFile handle. Usually used by viewer
       
   176   for opening attachments from message store as RFiles
       
   177 */
       
   178 int NmUtilities::openFile(XQSharableFile &file)
       
   179 {
       
   180     NM_FUNCTION;
       
   181 
       
   182     int ret(NmNotFoundError);
       
   183     XQApplicationManager aiwMgr;
       
   184     XQAiwRequest *request(NULL);
       
   185     request = aiwMgr.create(file);
       
   186     // Create request for the sharable file
       
   187     if (request)
       
   188     {
       
   189         // Set request arguments
       
   190         QList<QVariant> args;
       
   191         args << qVariantFromValue(file);
       
   192         request->setArguments(args);
       
   193         // Send the request, ownership of request is transferred
       
   194         bool res = request->send();
       
   195         if (res) {
       
   196         // Request ok, set error status.
       
   197         ret = NmNoError;
       
   198         }
       
   199     }
       
   200     return ret;
       
   201 }
       
   202 
       
   203 /*!
       
   204  * Truncate a string to a specific length. If length is less than
       
   205  * the string, ellipses are added to the end of the string.
       
   206  */
       
   207 QString NmUtilities::truncate( const QString &string, int length )
       
   208 {
       
   209     NM_FUNCTION;
       
   210 
       
   211     if (string.length() <= length) {
       
   212         return string;
       
   213     }
       
   214 
       
   215     QString padding = "...";
       
   216 
       
   217     return string.mid(0, length - padding.length()) + padding;
       
   218 }
       
   219 
       
   220 /*!
       
   221  * Shows an error note. Used by at least editor and viewer classes.
       
   222  *
       
   223  */
       
   224 void NmUtilities::displayErrorNote(QString noteText)
       
   225 {
       
   226     NM_FUNCTION;
       
   227 
       
   228 	HbNotificationDialog *note = new HbNotificationDialog();
       
   229 
       
   230 	note->setIcon(HbIcon(QLatin1String("note_warning")));
       
   231 	note->setTitle(noteText);
       
   232 	note->setTitleTextWrapping(Hb::TextWordWrap);
       
   233 	note->setDismissPolicy(HbPopup::TapAnywhere);
       
   234 	note->setAttribute(Qt::WA_DeleteOnClose);
       
   235 	note->setSequentialShow(false);
       
   236 
       
   237 	note->show();
       
   238 }
       
   239 
       
   240 /*!
       
   241     Function returns localized attachment size string based
       
   242     on attachment size in bytes
       
   243  */
       
   244 QString NmUtilities::attachmentSizeString(const int sizeInBytes)
       
   245 {
       
   246     NM_FUNCTION;
       
   247 
       
   248     qreal sizeMb = (qreal)sizeInBytes / (qreal)NmMegabyte;
       
   249     if (sizeMb < NmMinAttachmentSize) {
       
   250         // NmMinAttachmentSize (0.1Mb) is the minimum size shown for attachment
       
   251         sizeMb = NmMinAttachmentSize;
       
   252     }   
       
   253     return QString().sprintf("(%.1f Mb)", sizeMb); // Use loc string when available
       
   254 }
       
   255 
       
   256 /*!
       
   257     Displays a note with Yes/No buttons. Note has no timeout, i.e. it has to be dismissed manually.
       
   258     Returns pointer to dialog so that caller can take ownership and handle deletion.
       
   259     Parameter 'receiver' is the object and 'member' is the slot where user selection is passed.
       
   260 */
       
   261 HbMessageBox* NmUtilities::displayQuestionNote(
       
   262     QString noteText, QObject* receiver, const char* member)
       
   263 {
       
   264     NM_FUNCTION;
       
   265 
       
   266     HbMessageBox *messageBox = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
       
   267     messageBox->setStandardButtons(HbMessageBox::Yes | HbMessageBox::No);  
       
   268     messageBox->setText(noteText);
       
   269     messageBox->setTimeout(HbMessageBox::NoTimeout); // Note has to be dismissed manually
       
   270     messageBox->open(receiver, member);
       
   271     return messageBox;
       
   272 }
       
   273 
       
   274 /*!
       
   275  * displays an warning note.
       
   276  */
       
   277 HbMessageBox* NmUtilities::displayWarningNote(QString noteText, QObject* receiver, const char* member)
       
   278 {
       
   279     NM_FUNCTION;
       
   280 
       
   281     HbMessageBox *messageBox = new HbMessageBox(HbMessageBox::MessageTypeWarning);
       
   282     messageBox->setText(noteText);
       
   283     messageBox->setTimeout(HbMessageBox::NoTimeout); // Note has to be dismissed manually
       
   284     messageBox->open(receiver, member);
       
   285     return messageBox;
       
   286 }
       
   287 
       
   288 /*!
       
   289     Function returns localized "Original message" header
       
   290     in html format based on envelope
       
   291 */
       
   292 QString NmUtilities::createReplyHeader(const NmMessageEnvelope &env)
       
   293 {
       
   294     NM_FUNCTION;
       
   295 
       
   296     QString ret = "<html><body>";
       
   297     // Two empty lines before reply header.
       
   298     ret+="<br><br>";
       
   299     // Append "----- Original message ----" text
       
   300     ret+=hbTrId("txt_mail_editor_reply_original_msg");
       
   301     // Append sender
       
   302     ret+="<br>";
       
   303     ret+=hbTrId("txt_mail_editor_reply_from");
       
   304     ret+=" ";
       
   305     if (env.sender().displayName().length()){
       
   306         ret+=env.sender().displayName();
       
   307     }
       
   308     else{
       
   309         ret+=env.sender().address();
       
   310     }
       
   311     // Append sent time
       
   312     ret+="<br>";
       
   313     ret+=hbTrId("txt_mail_editor_reply_sent");
       
   314     ret+=" ";
       
   315     HbExtendedLocale locale = HbExtendedLocale::system();
       
   316     QDateTime localTime = env.sentTime().addSecs(locale.universalTimeOffset());
       
   317     QDate sentLocalDate = localTime.date();
       
   318     ret+=locale.format(sentLocalDate, r_qtn_date_usual);
       
   319     // Append to recipients
       
   320     const QList<NmAddress> &toList = env.toRecipients();
       
   321     if (toList.count()){
       
   322         ret+="<br>";
       
   323         ret+=hbTrId("txt_mail_editor_reply_to");
       
   324         ret+=" ";
       
   325         for (int i=0;i<toList.count();i++){
       
   326             if (toList[i].displayName().length()){
       
   327                 ret+=toList[i].displayName();
       
   328             }
       
   329             else{
       
   330                 ret+=toList[i].address();
       
   331             }
       
   332             if (i!=toList.count()-1){
       
   333                 ret+=";";
       
   334             }
       
   335         }
       
   336     }
       
   337     // Append cc recipients
       
   338     const QList<NmAddress> &ccList = env.ccRecipients();
       
   339     if (ccList.count()){
       
   340         ret+="<br>";
       
   341         ret+=hbTrId("txt_mail_editor_reply_cc");
       
   342         ret+=" ";
       
   343         for (int i=0;i<ccList.count();i++){
       
   344             if (ccList[i].displayName().length()){
       
   345                 ret+=ccList[i].displayName();
       
   346             }
       
   347             else{
       
   348                 ret+=ccList[i].address();
       
   349             }
       
   350             if (i!=toList.count()-1){
       
   351                 ret+=";";
       
   352             }
       
   353         }
       
   354     }
       
   355     // Append subject if there is subject to display
       
   356     if (env.subject().length()){
       
   357         ret+="<br>";
       
   358         ret+=hbTrId("txt_mail_editor_reply_subject");
       
   359         ret+=" ";
       
   360         ret+=env.subject();
       
   361     }
       
   362     // Append priority if it is other than normal
       
   363     if (env.priority()!=NmMessagePriorityNormal){
       
   364         ret+="<br>";
       
   365         ret+=hbTrId("txt_mail_editor_reply_importance");
       
   366         ret+=" ";
       
   367         if (env.priority()==NmMessagePriorityLow){
       
   368             ret+=hbTrId("txt_mail_editor_reply_importance_low");
       
   369         }
       
   370         else {
       
   371             ret+=hbTrId("txt_mail_editor_reply_importance_high");
       
   372         }
       
   373     }
       
   374     ret+="<br></body></html>";
       
   375     return ret;
       
   376 }